244 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2/*3 * vimc-common.h Virtual Media Controller Driver4 *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 409626#define VIMC_FRAME_MAX_HEIGHT 216027#define VIMC_FRAME_MIN_WIDTH 1628#define VIMC_FRAME_MIN_HEIGHT 1629 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 837 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 parameters47 *48 * @fmt: the pointer to struct v4l2_pix_format or49 * struct v4l2_mbus_framefmt50 *51 * Entities must check if colorimetry given by the userspace is valid, if not52 * then set them as DEFAULT53 */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 format73 *74 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros75 * @bpp: number of bytes each pixel occupies76 * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros77 * @bayer: true if this is a bayer format78 *79 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding80 * 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 the91 * topology92 *93 * @dev: a pointer of the device struct of the driver94 * @ent: the pointer to struct media_entity for the node95 * @process_frame: callback send a frame to that node96 * @vdev_get_format: callback that returns the current format a pad, used97 * only when is_media_entity_v4l2_video_device(ent) returns98 * true99 *100 * Each node of the topology must create a vimc_ent_device struct. Depending on101 * the node it will be of an instance of v4l2_subdev or video_device struct102 * where both contains a struct media_entity.103 * Those structures should embedded the vimc_ent_device struct through104 * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the105 * vimc_ent_device struct to be retrieved from the corresponding struct106 * media_entity107 */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 driver119 *120 * @pipe_cfg: pointer to the vimc pipeline configuration structure121 * @ent_devs: array of vimc_ent_device pointers122 * @mdev: the associated media_device parent123 * @v4l2_dev: Internal v4l2 parent device124 */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 types134 *135 *136 * @add: initializes and registers137 * vimc entity - called from vimc-core138 * @unregister: unregisters vimc entity - called from vimc-core139 * @release: releases vimc entity - called from the v4l2_dev140 * release callback141 */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 individual151 * configuration for each entity152 *153 * @name: entity name154 * @type: contain the callbacks of this entity type155 *156 */157struct vimc_ent_config {158 const char *name;159 const struct vimc_ent_type *type;160};161 162/**163 * vimc_is_source - returns true if the entity has only source pads164 *165 * @ent: pointer to &struct media_entity166 *167 */168bool vimc_is_source(struct media_entity *ent);169 170extern const struct vimc_ent_type vimc_sensor_type;171extern const struct vimc_ent_type vimc_debayer_type;172extern const struct vimc_ent_type vimc_scaler_type;173extern const struct vimc_ent_type vimc_capture_type;174extern const struct vimc_ent_type vimc_lens_type;175 176/**177 * vimc_pix_map_by_index - get vimc_pix_map struct by its index178 *179 * @i: index of the vimc_pix_map struct in vimc_pix_map_list180 */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 index185 *186 * @index: index of the mbus code in vimc_pix_map_list187 *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 code194 *195 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros196 */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 format201 *202 * @pixelformat: pixel format defined by V4L2_PIX_FMT_* macros203 */204const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);205 206/**207 * vimc_ent_sd_register - initialize and register a subdev node208 *209 * @ved: the vimc_ent_device struct to be initialize210 * @sd: the v4l2_subdev struct to be initialize and registered211 * @v4l2_dev: the v4l2 device to register the v4l2_subdev212 * @name: name of the sub-device. Please notice that the name must be213 * unique.214 * @function: media entity function defined by MEDIA_ENT_F_* macros215 * @num_pads: number of pads to initialize216 * @pads: the array of pads of the entity, the caller should set the217 * flags of the pads218 * @int_ops: pointer to &struct v4l2_subdev_internal_ops.219 * @sd_ops: pointer to &struct v4l2_subdev_ops.220 *221 * Helper function initialize and register the struct vimc_ent_device and struct222 * v4l2_subdev which represents a subdev node in the topology223 */224int vimc_ent_sd_register(struct vimc_ent_device *ved,225 struct v4l2_subdev *sd,226 struct v4l2_device *v4l2_dev,227 const char *const name,228 u32 function,229 u16 num_pads,230 struct media_pad *pads,231 const struct v4l2_subdev_internal_ops *int_ops,232 const struct v4l2_subdev_ops *sd_ops);233 234/**235 * vimc_vdev_link_validate - validates a media link236 *237 * @link: pointer to &struct media_link238 *239 * This function calls validates if a media link is valid for streaming.240 */241int vimc_vdev_link_validate(struct media_link *link);242 243#endif244