1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * vimc-common.h Virtual Media Controller Driver
4 *
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6 */
7
8#ifndef _VIMC_COMMON_H_
9#define _VIMC_COMMON_H_
10
11#include <linux/platform_device.h>
12#include <linux/slab.h>
13#include <media/media-device.h>
14#include <media/v4l2-device.h>
15
16#define VIMC_PDEV_NAME "vimc"
17
18/* VIMC-specific controls */
19#define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
20#define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
21#define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
22#define VIMC_CID_MEAN_WIN_SIZE (VIMC_CID_VIMC_BASE + 1)
23#define VIMC_CID_OSD_TEXT_MODE (VIMC_CID_VIMC_BASE + 2)
24
25#define VIMC_FRAME_MAX_WIDTH 4096
26#define VIMC_FRAME_MAX_HEIGHT 2160
27#define VIMC_FRAME_MIN_WIDTH 16
28#define VIMC_FRAME_MIN_HEIGHT 16
29
30#define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
31
32/* Source and sink pad checks */
33#define VIMC_IS_SRC(pad) (pad)
34#define VIMC_IS_SINK(pad) (!(pad))
35
36#define VIMC_PIX_FMT_MAX_CODES 8
37
38extern unsigned int vimc_allocator;
39
40enum vimc_allocator_type {
41 VIMC_ALLOCATOR_VMALLOC = 0,
42 VIMC_ALLOCATOR_DMA_CONTIG = 1,
43};
44
45/**
46 * vimc_colorimetry_clamp - Adjust colorimetry parameters
47 *
48 * @fmt: the pointer to struct v4l2_pix_format or
49 * struct v4l2_mbus_framefmt
50 *
51 * Entities must check if colorimetry given by the userspace is valid, if not
52 * then set them as DEFAULT
53 */
54#define vimc_colorimetry_clamp(fmt) \
55do { \
56 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
57 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
58 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
59 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
60 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
61 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
62 } \
63 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
64 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
65 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
66 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
67 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
68 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
69} while (0)
70
71/**
72 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
73 *
74 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
75 * @bpp: number of bytes each pixel occupies
76 * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros
77 * @bayer: true if this is a bayer format
78 *
79 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
80 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
81 */
82struct vimc_pix_map {
83 unsigned int code[VIMC_PIX_FMT_MAX_CODES];
84 unsigned int bpp;
85 u32 pixelformat;
86 bool bayer;
87};
88
89/**
90 * struct vimc_ent_device - core struct that represents an entity in the
91 * topology
92 *
93 * @dev: a pointer of the device struct of the driver
94 * @ent: the pointer to struct media_entity for the node
95 * @process_frame: callback send a frame to that node
96 * @vdev_get_format: callback that returns the current format a pad, used
97 * only when is_media_entity_v4l2_video_device(ent) returns
98 * true
99 *
100 * Each node of the topology must create a vimc_ent_device struct. Depending on
101 * the node it will be of an instance of v4l2_subdev or video_device struct
102 * where both contains a struct media_entity.
103 * Those structures should embedded the vimc_ent_device struct through
104 * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the
105 * vimc_ent_device struct to be retrieved from the corresponding struct
106 * media_entity
107 */
108struct vimc_ent_device {
109 struct device *dev;
110 struct media_entity *ent;
111 void * (*process_frame)(struct vimc_ent_device *ved,
112 const void *frame);
113 void (*vdev_get_format)(struct vimc_ent_device *ved,
114 struct v4l2_pix_format *fmt);
115};
116
117/**
118 * struct vimc_device - main device for vimc driver
119 *
120 * @pipe_cfg: pointer to the vimc pipeline configuration structure
121 * @ent_devs: array of vimc_ent_device pointers
122 * @mdev: the associated media_device parent
123 * @v4l2_dev: Internal v4l2 parent device
124 */
125struct vimc_device {
126 const struct vimc_pipeline_config *pipe_cfg;
127 struct vimc_ent_device **ent_devs;
128 struct media_device mdev;
129 struct v4l2_device v4l2_dev;
130};
131
132/**
133 * struct vimc_ent_type Structure for the callbacks of the entity types
134 *
135 *
136 * @add: initializes and registers
137 * vimc entity - called from vimc-core
138 * @unregister: unregisters vimc entity - called from vimc-core
139 * @release: releases vimc entity - called from the v4l2_dev
140 * release callback
141 */
142struct vimc_ent_type {
143 struct vimc_ent_device *(*add)(struct vimc_device *vimc,
144 const char *vcfg_name);
145 void (*unregister)(struct vimc_ent_device *ved);
146 void (*release)(struct vimc_ent_device *ved);
147};
148
149/**
150 * struct vimc_ent_config Structure which describes individual
151 * configuration for each entity
152 *
153 * @name: entity name
154 * @type: contain the callbacks of this entity type
155 *
156 */
157struct vimc_ent_config {
158 const char *name;
159 struct vimc_ent_type *type;
160};
161
162/**
163 * vimc_is_source - returns true if the entity has only source pads
164 *
165 * @ent: pointer to &struct media_entity
166 *
167 */
168bool vimc_is_source(struct media_entity *ent);
169
170extern struct vimc_ent_type vimc_sensor_type;
171extern struct vimc_ent_type vimc_debayer_type;
172extern struct vimc_ent_type vimc_scaler_type;
173extern struct vimc_ent_type vimc_capture_type;
174extern struct vimc_ent_type vimc_lens_type;
175
176/**
177 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
178 *
179 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
180 */
181const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
182
183/**
184 * vimc_mbus_code_by_index - get mbus code by its index
185 *
186 * @index: index of the mbus code in vimc_pix_map_list
187 *
188 * Returns 0 if no mbus code is found for the given index.
189 */
190u32 vimc_mbus_code_by_index(unsigned int index);
191
192/**
193 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
194 *
195 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
196 */
197const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
198
199/**
200 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
201 *
202 * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros
203 */
204const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
205
206/**
207 * vimc_ent_sd_register - initialize and register a subdev node
208 *
209 * @ved: the vimc_ent_device struct to be initialize
210 * @sd: the v4l2_subdev struct to be initialize and registered
211 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
212 * @name: name of the sub-device. Please notice that the name must be
213 * unique.
214 * @function: media entity function defined by MEDIA_ENT_F_* macros
215 * @num_pads: number of pads to initialize
216 * @pads: the array of pads of the entity, the caller should set the
217 * flags of the pads
218 * @sd_ops: pointer to &struct v4l2_subdev_ops.
219 *
220 * Helper function initialize and register the struct vimc_ent_device and struct
221 * v4l2_subdev which represents a subdev node in the topology
222 */
223int vimc_ent_sd_register(struct vimc_ent_device *ved,
224 struct v4l2_subdev *sd,
225 struct v4l2_device *v4l2_dev,
226 const char *const name,
227 u32 function,
228 u16 num_pads,
229 struct media_pad *pads,
230 const struct v4l2_subdev_ops *sd_ops);
231
232/**
233 * vimc_vdev_link_validate - validates a media link
234 *
235 * @link: pointer to &struct media_link
236 *
237 * This function calls validates if a media link is valid for streaming.
238 */
239int vimc_vdev_link_validate(struct media_link *link);
240
241#endif
242

source code of linux/drivers/media/test-drivers/vimc/vimc-common.h