Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * vimc-common.h Virtual Media Controller Driver
0004  *
0005  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
0006  */
0007 
0008 #ifndef _VIMC_COMMON_H_
0009 #define _VIMC_COMMON_H_
0010 
0011 #include <linux/platform_device.h>
0012 #include <linux/slab.h>
0013 #include <media/media-device.h>
0014 #include <media/v4l2-device.h>
0015 
0016 #define VIMC_PDEV_NAME "vimc"
0017 
0018 /* VIMC-specific controls */
0019 #define VIMC_CID_VIMC_BASE      (0x00f00000 | 0xf000)
0020 #define VIMC_CID_VIMC_CLASS     (0x00f00000 | 1)
0021 #define VIMC_CID_TEST_PATTERN       (VIMC_CID_VIMC_BASE + 0)
0022 #define VIMC_CID_MEAN_WIN_SIZE      (VIMC_CID_VIMC_BASE + 1)
0023 #define VIMC_CID_OSD_TEXT_MODE      (VIMC_CID_VIMC_BASE + 2)
0024 
0025 #define VIMC_FRAME_MAX_WIDTH 4096
0026 #define VIMC_FRAME_MAX_HEIGHT 2160
0027 #define VIMC_FRAME_MIN_WIDTH 16
0028 #define VIMC_FRAME_MIN_HEIGHT 16
0029 
0030 #define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
0031 
0032 /* Source and sink pad checks */
0033 #define VIMC_IS_SRC(pad)    (pad)
0034 #define VIMC_IS_SINK(pad)   (!(pad))
0035 
0036 #define VIMC_PIX_FMT_MAX_CODES 8
0037 
0038 extern unsigned int vimc_allocator;
0039 
0040 enum vimc_allocator_type {
0041     VIMC_ALLOCATOR_VMALLOC = 0,
0042     VIMC_ALLOCATOR_DMA_CONTIG = 1,
0043 };
0044 
0045 /**
0046  * vimc_colorimetry_clamp - Adjust colorimetry parameters
0047  *
0048  * @fmt:        the pointer to struct v4l2_pix_format or
0049  *          struct v4l2_mbus_framefmt
0050  *
0051  * Entities must check if colorimetry given by the userspace is valid, if not
0052  * then set them as DEFAULT
0053  */
0054 #define vimc_colorimetry_clamp(fmt)                 \
0055 do {                                    \
0056     if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT        \
0057         || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) {        \
0058         (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT;        \
0059         (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;      \
0060         (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;    \
0061         (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;      \
0062     }                               \
0063     if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M)        \
0064         (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;      \
0065     if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE)      \
0066         (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT;    \
0067     if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084)        \
0068         (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT;      \
0069 } while (0)
0070 
0071 /**
0072  * struct vimc_pix_map - maps media bus code with v4l2 pixel format
0073  *
0074  * @code:       media bus format code defined by MEDIA_BUS_FMT_* macros
0075  * @bpp:        number of bytes each pixel occupies
0076  * @pixelformat:    pixel format defined by V4L2_PIX_FMT_* macros
0077  * @bayer:      true if this is a bayer format
0078  *
0079  * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
0080  * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
0081  */
0082 struct vimc_pix_map {
0083     unsigned int code[VIMC_PIX_FMT_MAX_CODES];
0084     unsigned int bpp;
0085     u32 pixelformat;
0086     bool bayer;
0087 };
0088 
0089 /**
0090  * struct vimc_ent_device - core struct that represents an entity in the
0091  * topology
0092  *
0093  * @dev:        a pointer of the device struct of the driver
0094  * @ent:        the pointer to struct media_entity for the node
0095  * @process_frame:  callback send a frame to that node
0096  * @vdev_get_format:    callback that returns the current format a pad, used
0097  *          only when is_media_entity_v4l2_video_device(ent) returns
0098  *          true
0099  *
0100  * Each node of the topology must create a vimc_ent_device struct. Depending on
0101  * the node it will be of an instance of v4l2_subdev or video_device struct
0102  * where both contains a struct media_entity.
0103  * Those structures should embedded the vimc_ent_device struct through
0104  * v4l2_set_subdevdata() and video_set_drvdata() respectively, allowing the
0105  * vimc_ent_device struct to be retrieved from the corresponding struct
0106  * media_entity
0107  */
0108 struct vimc_ent_device {
0109     struct device *dev;
0110     struct media_entity *ent;
0111     void * (*process_frame)(struct vimc_ent_device *ved,
0112                 const void *frame);
0113     void (*vdev_get_format)(struct vimc_ent_device *ved,
0114                   struct v4l2_pix_format *fmt);
0115 };
0116 
0117 /**
0118  * struct vimc_device - main device for vimc driver
0119  *
0120  * @pipe_cfg:   pointer to the vimc pipeline configuration structure
0121  * @ent_devs:   array of vimc_ent_device pointers
0122  * @mdev:   the associated media_device parent
0123  * @v4l2_dev:   Internal v4l2 parent device
0124  */
0125 struct vimc_device {
0126     const struct vimc_pipeline_config *pipe_cfg;
0127     struct vimc_ent_device **ent_devs;
0128     struct media_device mdev;
0129     struct v4l2_device v4l2_dev;
0130 };
0131 
0132 /**
0133  * struct vimc_ent_type     Structure for the callbacks of the entity types
0134  *
0135  *
0136  * @add:            initializes and registers
0137  *              vimc entity - called from vimc-core
0138  * @unregister:         unregisters vimc entity - called from vimc-core
0139  * @release:            releases vimc entity - called from the v4l2_dev
0140  *              release callback
0141  */
0142 struct vimc_ent_type {
0143     struct vimc_ent_device *(*add)(struct vimc_device *vimc,
0144                        const char *vcfg_name);
0145     void (*unregister)(struct vimc_ent_device *ved);
0146     void (*release)(struct vimc_ent_device *ved);
0147 };
0148 
0149 /**
0150  * struct vimc_ent_config   Structure which describes individual
0151  *              configuration for each entity
0152  *
0153  * @name:           entity name
0154  * @type:           contain the callbacks of this entity type
0155  *
0156  */
0157 struct vimc_ent_config {
0158     const char *name;
0159     struct vimc_ent_type *type;
0160 };
0161 
0162 /**
0163  * vimc_is_source - returns true if the entity has only source pads
0164  *
0165  * @ent: pointer to &struct media_entity
0166  *
0167  */
0168 bool vimc_is_source(struct media_entity *ent);
0169 
0170 extern struct vimc_ent_type vimc_sensor_type;
0171 extern struct vimc_ent_type vimc_debayer_type;
0172 extern struct vimc_ent_type vimc_scaler_type;
0173 extern struct vimc_ent_type vimc_capture_type;
0174 extern struct vimc_ent_type vimc_lens_type;
0175 
0176 /**
0177  * vimc_pix_map_by_index - get vimc_pix_map struct by its index
0178  *
0179  * @i:          index of the vimc_pix_map struct in vimc_pix_map_list
0180  */
0181 const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
0182 
0183 /**
0184  * vimc_mbus_code_by_index - get mbus code by its index
0185  *
0186  * @index:      index of the mbus code in vimc_pix_map_list
0187  *
0188  * Returns 0 if no mbus code is found for the given index.
0189  */
0190 u32 vimc_mbus_code_by_index(unsigned int index);
0191 
0192 /**
0193  * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
0194  *
0195  * @code:       media bus format code defined by MEDIA_BUS_FMT_* macros
0196  */
0197 const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
0198 
0199 /**
0200  * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
0201  *
0202  * @pixelformat:    pixel format defined by V4L2_PIX_FMT_* macros
0203  */
0204 const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
0205 
0206 /**
0207  * vimc_ent_sd_register - initialize and register a subdev node
0208  *
0209  * @ved:    the vimc_ent_device struct to be initialize
0210  * @sd:     the v4l2_subdev struct to be initialize and registered
0211  * @v4l2_dev:   the v4l2 device to register the v4l2_subdev
0212  * @name:   name of the sub-device. Please notice that the name must be
0213  *      unique.
0214  * @function:   media entity function defined by MEDIA_ENT_F_* macros
0215  * @num_pads:   number of pads to initialize
0216  * @pads:   the array of pads of the entity, the caller should set the
0217  *      flags of the pads
0218  * @sd_ops: pointer to &struct v4l2_subdev_ops.
0219  *
0220  * Helper function initialize and register the struct vimc_ent_device and struct
0221  * v4l2_subdev which represents a subdev node in the topology
0222  */
0223 int vimc_ent_sd_register(struct vimc_ent_device *ved,
0224              struct v4l2_subdev *sd,
0225              struct v4l2_device *v4l2_dev,
0226              const char *const name,
0227              u32 function,
0228              u16 num_pads,
0229              struct media_pad *pads,
0230              const struct v4l2_subdev_ops *sd_ops);
0231 
0232 /**
0233  * vimc_vdev_link_validate - validates a media link
0234  *
0235  * @link: pointer to &struct media_link
0236  *
0237  * This function calls validates if a media link is valid for streaming.
0238  */
0239 int vimc_vdev_link_validate(struct media_link *link);
0240 
0241 #endif