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