blob: 7b4d988b208b10ea6ed64a0d8b8452e3e3b16ef6 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Helen Koikef2fe8902017-04-07 14:55:19 -03002/*
Helen Fornazier4a29b702017-06-19 14:00:18 -03003 * vimc-common.h Virtual Media Controller Driver
Helen Koikef2fe8902017-04-07 14:55:19 -03004 *
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
Helen Koikef2fe8902017-04-07 14:55:19 -03006 */
7
Helen Fornazier5ba0ae42017-06-19 14:00:11 -03008#ifndef _VIMC_COMMON_H_
9#define _VIMC_COMMON_H_
Helen Koikef2fe8902017-04-07 14:55:19 -030010
11#include <linux/slab.h>
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030012#include <media/media-device.h>
Helen Koikef2fe8902017-04-07 14:55:19 -030013#include <media/v4l2-device.h>
14
Helen Fornazierb6c61a62019-03-13 14:29:37 -040015#include "vimc-streamer.h"
16
Hans Verkuil04ee6d62019-01-14 10:27:45 -020017#define VIMC_PDEV_NAME "vimc"
18
Hans Verkuil52cf1d92017-11-06 05:20:01 -050019/* VIMC-specific controls */
20#define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
21#define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
22#define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
23
Helen Fornazier88ad71a2017-06-19 14:00:16 -030024#define VIMC_FRAME_MAX_WIDTH 4096
25#define VIMC_FRAME_MAX_HEIGHT 2160
26#define VIMC_FRAME_MIN_WIDTH 16
27#define VIMC_FRAME_MIN_HEIGHT 16
28
Helen Fornazier88f42bf2017-06-19 14:00:19 -030029#define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
30
Helen Koikef2fe8902017-04-07 14:55:19 -030031/**
Helen Fornazier441c0db2017-06-19 14:00:15 -030032 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
33 *
34 * @fmt: the pointer to struct v4l2_pix_format or
35 * struct v4l2_mbus_framefmt
36 *
37 * Entities must check if colorimetry given by the userspace is valid, if not
38 * then set them as DEFAULT
39 */
40#define vimc_colorimetry_clamp(fmt) \
41do { \
42 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
43 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
44 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
45 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
46 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
47 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
48 } \
49 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
50 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
51 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
52 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
53 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
54 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
55} while (0)
56
57/**
Helen Fornazier4a29b702017-06-19 14:00:18 -030058 * struct vimc_platform_data - platform data to components
59 *
60 * @entity_name: The name of the entity to be created
61 *
62 * Board setup code will often provide additional information using the device's
63 * platform_data field to hold additional information.
64 * When injecting a new platform_device in the component system the core needs
65 * to provide to the corresponding submodules the name of the entity that should
66 * be used when registering the subdevice in the Media Controller system.
67 */
68struct vimc_platform_data {
69 char entity_name[32];
70};
71
72/**
Helen Koikef2fe8902017-04-07 14:55:19 -030073 * struct vimc_ent_device - core struct that represents a node in the topology
74 *
75 * @ent: the pointer to struct media_entity for the node
76 * @pads: the list of pads of the node
Helen Koikef2fe8902017-04-07 14:55:19 -030077 * @process_frame: callback send a frame to that node
Helen Fornazier288a22d2017-06-19 14:00:14 -030078 * @vdev_get_format: callback that returns the current format a pad, used
79 * only when is_media_entity_v4l2_video_device(ent) returns
80 * true
Helen Koikef2fe8902017-04-07 14:55:19 -030081 *
82 * Each node of the topology must create a vimc_ent_device struct. Depending on
83 * the node it will be of an instance of v4l2_subdev or video_device struct
84 * where both contains a struct media_entity.
85 * Those structures should embedded the vimc_ent_device struct through
86 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
87 * vimc_ent_device struct to be retrieved from the corresponding struct
88 * media_entity
89 */
90struct vimc_ent_device {
91 struct media_entity *ent;
92 struct media_pad *pads;
Helen Fornazierb6c61a62019-03-13 14:29:37 -040093 struct vimc_stream *stream;
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -050094 void * (*process_frame)(struct vimc_ent_device *ved,
95 const void *frame);
Helen Fornazier288a22d2017-06-19 14:00:14 -030096 void (*vdev_get_format)(struct vimc_ent_device *ved,
97 struct v4l2_pix_format *fmt);
Helen Koikef2fe8902017-04-07 14:55:19 -030098};
99
100/**
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400101 * vimc_mbus_code_supported - helper to check supported mbus codes
102 *
103 * Helper function to check if mbus code is enumerated by vimc_enum_mbus_code()
104 */
105bool vimc_mbus_code_supported(__u32 code);
106
107/**
108 * vimc_enum_mbus_code - enumerate mbus codes
109 *
110 * Helper function to be pluged in .enum_mbus_code from
111 * struct v4l2_subdev_pad_ops.
112 */
113int vimc_enum_mbus_code(struct v4l2_subdev *sd,
114 struct v4l2_subdev_pad_config *cfg,
115 struct v4l2_subdev_mbus_code_enum *code);
116
117/**
Helen Koikef2fe8902017-04-07 14:55:19 -0300118 * vimc_pads_init - initialize pads
119 *
120 * @num_pads: number of pads to initialize
121 * @pads_flags: flags to use in each pad
122 *
123 * Helper functions to allocate/initialize pads
124 */
125struct media_pad *vimc_pads_init(u16 num_pads,
126 const unsigned long *pads_flag);
127
128/**
129 * vimc_pads_cleanup - free pads
130 *
131 * @pads: pointer to the pads
132 *
133 * Helper function to free the pads initialized with vimc_pads_init
134 */
135static inline void vimc_pads_cleanup(struct media_pad *pads)
136{
137 kfree(pads);
138}
139
140/**
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300141 * vimc_pipeline_s_stream - start stream through the pipeline
142 *
143 * @ent: the pointer to struct media_entity for the node
144 * @enable: 1 to start the stream and 0 to stop
145 *
146 * Helper function to call the s_stream of the subdevices connected
147 * in all the sink pads of the entity
148 */
149int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
150
151/**
Helen Fornazierc1495432017-06-19 14:00:12 -0300152 * vimc_ent_sd_register - initialize and register a subdev node
153 *
154 * @ved: the vimc_ent_device struct to be initialize
155 * @sd: the v4l2_subdev struct to be initialize and registered
156 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
157 * @name: name of the sub-device. Please notice that the name must be
158 * unique.
159 * @function: media entity function defined by MEDIA_ENT_F_* macros
160 * @num_pads: number of pads to initialize
161 * @pads_flag: flags to use in each pad
Hans Verkuil2b177f22019-03-05 03:36:20 -0500162 * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
Helen Fornazierc1495432017-06-19 14:00:12 -0300163 * @sd_ops: pointer to &struct v4l2_subdev_ops.
Helen Fornazierc1495432017-06-19 14:00:12 -0300164 *
165 * Helper function initialize and register the struct vimc_ent_device and struct
166 * v4l2_subdev which represents a subdev node in the topology
167 */
168int vimc_ent_sd_register(struct vimc_ent_device *ved,
169 struct v4l2_subdev *sd,
170 struct v4l2_device *v4l2_dev,
171 const char *const name,
172 u32 function,
173 u16 num_pads,
174 const unsigned long *pads_flag,
Hans Verkuil2b177f22019-03-05 03:36:20 -0500175 const struct v4l2_subdev_internal_ops *sd_int_ops,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300176 const struct v4l2_subdev_ops *sd_ops);
Helen Fornazierc1495432017-06-19 14:00:12 -0300177
178/**
Helen Fornazier4a29b702017-06-19 14:00:18 -0300179 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
Helen Fornazierc1495432017-06-19 14:00:12 -0300180 *
Helen Fornazier4a29b702017-06-19 14:00:18 -0300181 * @ved: the vimc_ent_device struct to be cleaned up
182 * @sd: the v4l2_subdev struct to be unregistered
Helen Fornazierc1495432017-06-19 14:00:12 -0300183 *
184 * Helper function cleanup and unregister the struct vimc_ent_device and struct
185 * v4l2_subdev which represents a subdev node in the topology
186 */
187void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
188 struct v4l2_subdev *sd);
189
Helen Fornazier288a22d2017-06-19 14:00:14 -0300190/**
191 * vimc_link_validate - validates a media link
192 *
193 * @link: pointer to &struct media_link
194 *
195 * This function calls validates if a media link is valid for streaming.
196 */
197int vimc_link_validate(struct media_link *link);
198
Helen Koikef2fe8902017-04-07 14:55:19 -0300199#endif