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 38 /** 39 * vimc_colorimetry_clamp - Adjust colorimetry parameters 40 * 41 * @fmt: the pointer to struct v4l2_pix_format or 42 * struct v4l2_mbus_framefmt 43 * 44 * Entities must check if colorimetry given by the userspace is valid, if not 45 * then set them as DEFAULT 46 */ 47 #define vimc_colorimetry_clamp(fmt) \ 48 do { \ 49 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \ 50 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \ 51 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \ 52 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \ 53 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \ 54 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \ 55 } \ 56 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \ 57 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \ 58 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \ 59 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \ 60 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \ 61 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \ 62 } while (0) 63 64 /** 65 * struct vimc_pix_map - maps media bus code with v4l2 pixel format 66 * 67 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros 68 * @bpp: number of bytes each pixel occupies 69 * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros 70 * @bayer: true if this is a bayer format 71 * 72 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding 73 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp) 74 */ 75 struct vimc_pix_map { 76 unsigned int code[VIMC_PIX_FMT_MAX_CODES]; 77 unsigned int bpp; 78 u32 pixelformat; 79 bool bayer; 80 }; 81 82 /** 83 * struct vimc_ent_device - core struct that represents an entity in the 84 * topology 85 * 86 * @dev: a pointer of the device struct of the driver 87 * @ent: the pointer to struct media_entity for the node 88 * @process_frame: callback send a frame to that node 89 * @vdev_get_format: callback that returns the current format a pad, used 90 * only when is_media_entity_v4l2_video_device(ent) returns 91 * true 92 * 93 * Each node of the topology must create a vimc_ent_device struct. Depending on 94 * the node it will be of an instance of v4l2_subdev or video_device struct 95 * where both contains a struct media_entity. 96 * Those structures should embedded the vimc_ent_device struct through 97 * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the 98 * vimc_ent_device struct to be retrieved from the corresponding struct 99 * media_entity 100 */ 101 struct vimc_ent_device { 102 struct device *dev; 103 struct media_entity *ent; 104 void * (*process_frame)(struct vimc_ent_device *ved, 105 const void *frame); 106 void (*vdev_get_format)(struct vimc_ent_device *ved, 107 struct v4l2_pix_format *fmt); 108 }; 109 110 /** 111 * struct vimc_device - main device for vimc driver 112 * 113 * @pipe_cfg: pointer to the vimc pipeline configuration structure 114 * @ent_devs: array of vimc_ent_device pointers 115 * @mdev: the associated media_device parent 116 * @v4l2_dev: Internal v4l2 parent device 117 */ 118 struct vimc_device { 119 const struct vimc_pipeline_config *pipe_cfg; 120 struct vimc_ent_device **ent_devs; 121 struct media_device mdev; 122 struct v4l2_device v4l2_dev; 123 }; 124 125 /** 126 * struct vimc_ent_type Structure for the callbacks of the entity types 127 * 128 * 129 * @add: initializes and registers 130 * vimc entity - called from vimc-core 131 * @unregister: unregisters vimc entity - called from vimc-core 132 * @release: releases vimc entity - called from the v4l2_dev 133 * release callback 134 */ 135 struct vimc_ent_type { 136 struct vimc_ent_device *(*add)(struct vimc_device *vimc, 137 const char *vcfg_name); 138 void (*unregister)(struct vimc_ent_device *ved); 139 void (*release)(struct vimc_ent_device *ved); 140 }; 141 142 /** 143 * struct vimc_ent_config Structure which describes individual 144 * configuration for each entity 145 * 146 * @name: entity name 147 * @type: contain the callbacks of this entity type 148 * 149 */ 150 struct vimc_ent_config { 151 const char *name; 152 struct vimc_ent_type *type; 153 }; 154 155 /** 156 * vimc_is_source - returns true if the entity has only source pads 157 * 158 * @ent: pointer to &struct media_entity 159 * 160 */ 161 bool vimc_is_source(struct media_entity *ent); 162 163 extern struct vimc_ent_type vimc_sen_type; 164 extern struct vimc_ent_type vimc_deb_type; 165 extern struct vimc_ent_type vimc_sca_type; 166 extern struct vimc_ent_type vimc_cap_type; 167 168 /** 169 * vimc_pix_map_by_index - get vimc_pix_map struct by its index 170 * 171 * @i: index of the vimc_pix_map struct in vimc_pix_map_list 172 */ 173 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i); 174 175 /** 176 * vimc_mbus_code_by_index - get mbus code by its index 177 * 178 * @index: index of the mbus code in vimc_pix_map_list 179 * 180 * Returns 0 if no mbus code is found for the given index. 181 */ 182 u32 vimc_mbus_code_by_index(unsigned int index); 183 184 /** 185 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code 186 * 187 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros 188 */ 189 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code); 190 191 /** 192 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format 193 * 194 * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros 195 */ 196 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat); 197 198 /** 199 * vimc_ent_sd_register - initialize and register a subdev node 200 * 201 * @ved: the vimc_ent_device struct to be initialize 202 * @sd: the v4l2_subdev struct to be initialize and registered 203 * @v4l2_dev: the v4l2 device to register the v4l2_subdev 204 * @name: name of the sub-device. Please notice that the name must be 205 * unique. 206 * @function: media entity function defined by MEDIA_ENT_F_* macros 207 * @num_pads: number of pads to initialize 208 * @pads: the array of pads of the entity, the caller should set the 209 * flags of the pads 210 * @sd_ops: pointer to &struct v4l2_subdev_ops. 211 * 212 * Helper function initialize and register the struct vimc_ent_device and struct 213 * v4l2_subdev which represents a subdev node in the topology 214 */ 215 int vimc_ent_sd_register(struct vimc_ent_device *ved, 216 struct v4l2_subdev *sd, 217 struct v4l2_device *v4l2_dev, 218 const char *const name, 219 u32 function, 220 u16 num_pads, 221 struct media_pad *pads, 222 const struct v4l2_subdev_ops *sd_ops); 223 224 /** 225 * vimc_vdev_link_validate - validates a media link 226 * 227 * @link: pointer to &struct media_link 228 * 229 * This function calls validates if a media link is valid for streaming. 230 */ 231 int vimc_vdev_link_validate(struct media_link *link); 232 233 #endif 234