blob: 84539430b5e7e591c3d45fd769d85e23cf029cc0 [file] [log] [blame]
Helen Koikef2fe8902017-04-07 14:55:19 -03001/*
Helen Fornazier4a29b702017-06-19 14:00:18 -03002 * vimc-common.h Virtual Media Controller Driver
Helen Koikef2fe8902017-04-07 14:55:19 -03003 *
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030018#ifndef _VIMC_COMMON_H_
19#define _VIMC_COMMON_H_
Helen Koikef2fe8902017-04-07 14:55:19 -030020
21#include <linux/slab.h>
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030022#include <media/media-device.h>
Helen Koikef2fe8902017-04-07 14:55:19 -030023#include <media/v4l2-device.h>
24
Hans Verkuil04ee6d62019-01-14 10:27:45 -020025#define VIMC_PDEV_NAME "vimc"
26
Hans Verkuil52cf1d92017-11-06 05:20:01 -050027/* VIMC-specific controls */
28#define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
29#define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
30#define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
31
Helen Fornazier88ad71a2017-06-19 14:00:16 -030032#define VIMC_FRAME_MAX_WIDTH 4096
33#define VIMC_FRAME_MAX_HEIGHT 2160
34#define VIMC_FRAME_MIN_WIDTH 16
35#define VIMC_FRAME_MIN_HEIGHT 16
36
Helen Fornazier88f42bf2017-06-19 14:00:19 -030037#define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
38
Helen Koikef2fe8902017-04-07 14:55:19 -030039/**
Helen Fornazier441c0db2017-06-19 14:00:15 -030040 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
41 *
42 * @fmt: the pointer to struct v4l2_pix_format or
43 * struct v4l2_mbus_framefmt
44 *
45 * Entities must check if colorimetry given by the userspace is valid, if not
46 * then set them as DEFAULT
47 */
48#define vimc_colorimetry_clamp(fmt) \
49do { \
50 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
51 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
52 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
53 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
54 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
55 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
56 } \
57 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
58 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
59 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
60 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
61 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
62 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
63} while (0)
64
65/**
Helen Fornazier4a29b702017-06-19 14:00:18 -030066 * struct vimc_platform_data - platform data to components
67 *
68 * @entity_name: The name of the entity to be created
69 *
70 * Board setup code will often provide additional information using the device's
71 * platform_data field to hold additional information.
72 * When injecting a new platform_device in the component system the core needs
73 * to provide to the corresponding submodules the name of the entity that should
74 * be used when registering the subdevice in the Media Controller system.
75 */
76struct vimc_platform_data {
77 char entity_name[32];
78};
79
80/**
Helen Koikef2fe8902017-04-07 14:55:19 -030081 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
82 *
83 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
84 * @bbp: number of bytes each pixel occupies
85 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
86 *
87 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
88 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
89 */
90struct vimc_pix_map {
91 unsigned int code;
92 unsigned int bpp;
93 u32 pixelformat;
Helen Fornazier6856ba72017-06-19 14:00:20 -030094 bool bayer;
Helen Koikef2fe8902017-04-07 14:55:19 -030095};
96
97/**
98 * struct vimc_ent_device - core struct that represents a node in the topology
99 *
100 * @ent: the pointer to struct media_entity for the node
101 * @pads: the list of pads of the node
Helen Koikef2fe8902017-04-07 14:55:19 -0300102 * @process_frame: callback send a frame to that node
Helen Fornazier288a22d2017-06-19 14:00:14 -0300103 * @vdev_get_format: callback that returns the current format a pad, used
104 * only when is_media_entity_v4l2_video_device(ent) returns
105 * true
Helen Koikef2fe8902017-04-07 14:55:19 -0300106 *
107 * Each node of the topology must create a vimc_ent_device struct. Depending on
108 * the node it will be of an instance of v4l2_subdev or video_device struct
109 * where both contains a struct media_entity.
110 * Those structures should embedded the vimc_ent_device struct through
111 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
112 * vimc_ent_device struct to be retrieved from the corresponding struct
113 * media_entity
114 */
115struct vimc_ent_device {
116 struct media_entity *ent;
117 struct media_pad *pads;
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500118 void * (*process_frame)(struct vimc_ent_device *ved,
119 const void *frame);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300120 void (*vdev_get_format)(struct vimc_ent_device *ved,
121 struct v4l2_pix_format *fmt);
Helen Koikef2fe8902017-04-07 14:55:19 -0300122};
123
124/**
Helen Koikef2fe8902017-04-07 14:55:19 -0300125 * vimc_pads_init - initialize pads
126 *
127 * @num_pads: number of pads to initialize
128 * @pads_flags: flags to use in each pad
129 *
130 * Helper functions to allocate/initialize pads
131 */
132struct media_pad *vimc_pads_init(u16 num_pads,
133 const unsigned long *pads_flag);
134
135/**
136 * vimc_pads_cleanup - free pads
137 *
138 * @pads: pointer to the pads
139 *
140 * Helper function to free the pads initialized with vimc_pads_init
141 */
142static inline void vimc_pads_cleanup(struct media_pad *pads)
143{
144 kfree(pads);
145}
146
147/**
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300148 * vimc_pipeline_s_stream - start stream through the pipeline
149 *
150 * @ent: the pointer to struct media_entity for the node
151 * @enable: 1 to start the stream and 0 to stop
152 *
153 * Helper function to call the s_stream of the subdevices connected
154 * in all the sink pads of the entity
155 */
156int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
157
158/**
Helen Fornazier88ad71a2017-06-19 14:00:16 -0300159 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
160 *
161 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
162 */
163const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
164
165/**
Helen Koikef2fe8902017-04-07 14:55:19 -0300166 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
167 *
168 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
169 */
170const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
171
172/**
173 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
174 *
175 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
176 */
177const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
178
Helen Fornazierc1495432017-06-19 14:00:12 -0300179/**
180 * vimc_ent_sd_register - initialize and register a subdev node
181 *
182 * @ved: the vimc_ent_device struct to be initialize
183 * @sd: the v4l2_subdev struct to be initialize and registered
184 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
185 * @name: name of the sub-device. Please notice that the name must be
186 * unique.
187 * @function: media entity function defined by MEDIA_ENT_F_* macros
188 * @num_pads: number of pads to initialize
189 * @pads_flag: flags to use in each pad
190 * @sd_ops: pointer to &struct v4l2_subdev_ops.
Helen Fornazierc1495432017-06-19 14:00:12 -0300191 *
192 * Helper function initialize and register the struct vimc_ent_device and struct
193 * v4l2_subdev which represents a subdev node in the topology
194 */
195int vimc_ent_sd_register(struct vimc_ent_device *ved,
196 struct v4l2_subdev *sd,
197 struct v4l2_device *v4l2_dev,
198 const char *const name,
199 u32 function,
200 u16 num_pads,
201 const unsigned long *pads_flag,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300202 const struct v4l2_subdev_ops *sd_ops);
Helen Fornazierc1495432017-06-19 14:00:12 -0300203
204/**
Helen Fornazier4a29b702017-06-19 14:00:18 -0300205 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
Helen Fornazierc1495432017-06-19 14:00:12 -0300206 *
Helen Fornazier4a29b702017-06-19 14:00:18 -0300207 * @ved: the vimc_ent_device struct to be cleaned up
208 * @sd: the v4l2_subdev struct to be unregistered
Helen Fornazierc1495432017-06-19 14:00:12 -0300209 *
210 * Helper function cleanup and unregister the struct vimc_ent_device and struct
211 * v4l2_subdev which represents a subdev node in the topology
212 */
213void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
214 struct v4l2_subdev *sd);
215
Helen Fornazier288a22d2017-06-19 14:00:14 -0300216/**
217 * vimc_link_validate - validates a media link
218 *
219 * @link: pointer to &struct media_link
220 *
221 * This function calls validates if a media link is valid for streaming.
222 */
223int vimc_link_validate(struct media_link *link);
224
Helen Koikef2fe8902017-04-07 14:55:19 -0300225#endif