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