blob: 142ddfa69b3dcf8b9b74bfa270c3c28ca689e4fb [file] [log] [blame]
Helen Koikef2fe8902017-04-07 14:55:19 -03001/*
Helen Fornazier4a29b702017-06-19 14:00:18 -03002 * vimc-common.h Virtual Media Controller Driver
Helen Koikef2fe8902017-04-07 14:55:19 -03003 *
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030018#ifndef _VIMC_COMMON_H_
19#define _VIMC_COMMON_H_
Helen Koikef2fe8902017-04-07 14:55:19 -030020
21#include <linux/slab.h>
Helen Fornazier5ba0ae42017-06-19 14:00:11 -030022#include <media/media-device.h>
Helen Koikef2fe8902017-04-07 14:55:19 -030023#include <media/v4l2-device.h>
24
Helen Fornazierb6c61a62019-03-13 14:29:37 -040025#include "vimc-streamer.h"
26
Hans Verkuil04ee6d62019-01-14 10:27:45 -020027#define VIMC_PDEV_NAME "vimc"
28
Hans Verkuil52cf1d92017-11-06 05:20:01 -050029/* VIMC-specific controls */
30#define VIMC_CID_VIMC_BASE (0x00f00000 | 0xf000)
31#define VIMC_CID_VIMC_CLASS (0x00f00000 | 1)
32#define VIMC_CID_TEST_PATTERN (VIMC_CID_VIMC_BASE + 0)
33
Helen Fornazier88ad71a2017-06-19 14:00:16 -030034#define VIMC_FRAME_MAX_WIDTH 4096
35#define VIMC_FRAME_MAX_HEIGHT 2160
36#define VIMC_FRAME_MIN_WIDTH 16
37#define VIMC_FRAME_MIN_HEIGHT 16
38
Helen Fornazier88f42bf2017-06-19 14:00:19 -030039#define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
40
Helen Koikef2fe8902017-04-07 14:55:19 -030041/**
Helen Fornazier441c0db2017-06-19 14:00:15 -030042 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
43 *
44 * @fmt: the pointer to struct v4l2_pix_format or
45 * struct v4l2_mbus_framefmt
46 *
47 * Entities must check if colorimetry given by the userspace is valid, if not
48 * then set them as DEFAULT
49 */
50#define vimc_colorimetry_clamp(fmt) \
51do { \
52 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
53 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
54 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
55 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
56 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
57 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
58 } \
59 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
60 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
61 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
62 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
63 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
64 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
65} while (0)
66
67/**
Helen Fornazier4a29b702017-06-19 14:00:18 -030068 * struct vimc_platform_data - platform data to components
69 *
70 * @entity_name: The name of the entity to be created
71 *
72 * Board setup code will often provide additional information using the device's
73 * platform_data field to hold additional information.
74 * When injecting a new platform_device in the component system the core needs
75 * to provide to the corresponding submodules the name of the entity that should
76 * be used when registering the subdevice in the Media Controller system.
77 */
78struct vimc_platform_data {
79 char entity_name[32];
80};
81
82/**
Helen Koikef2fe8902017-04-07 14:55:19 -030083 * struct vimc_ent_device - core struct that represents a node in the topology
84 *
85 * @ent: the pointer to struct media_entity for the node
86 * @pads: the list of pads of the node
Helen Koikef2fe8902017-04-07 14:55:19 -030087 * @process_frame: callback send a frame to that node
Helen Fornazier288a22d2017-06-19 14:00:14 -030088 * @vdev_get_format: callback that returns the current format a pad, used
89 * only when is_media_entity_v4l2_video_device(ent) returns
90 * true
Helen Koikef2fe8902017-04-07 14:55:19 -030091 *
92 * Each node of the topology must create a vimc_ent_device struct. Depending on
93 * the node it will be of an instance of v4l2_subdev or video_device struct
94 * where both contains a struct media_entity.
95 * Those structures should embedded the vimc_ent_device struct through
96 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
97 * vimc_ent_device struct to be retrieved from the corresponding struct
98 * media_entity
99 */
100struct vimc_ent_device {
101 struct media_entity *ent;
102 struct media_pad *pads;
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400103 struct vimc_stream *stream;
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500104 void * (*process_frame)(struct vimc_ent_device *ved,
105 const void *frame);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300106 void (*vdev_get_format)(struct vimc_ent_device *ved,
107 struct v4l2_pix_format *fmt);
Helen Koikef2fe8902017-04-07 14:55:19 -0300108};
109
110/**
Helen Fornazierb6c61a62019-03-13 14:29:37 -0400111 * vimc_mbus_code_supported - helper to check supported mbus codes
112 *
113 * Helper function to check if mbus code is enumerated by vimc_enum_mbus_code()
114 */
115bool vimc_mbus_code_supported(__u32 code);
116
117/**
118 * vimc_enum_mbus_code - enumerate mbus codes
119 *
120 * Helper function to be pluged in .enum_mbus_code from
121 * struct v4l2_subdev_pad_ops.
122 */
123int vimc_enum_mbus_code(struct v4l2_subdev *sd,
124 struct v4l2_subdev_pad_config *cfg,
125 struct v4l2_subdev_mbus_code_enum *code);
126
127/**
Helen Koikef2fe8902017-04-07 14:55:19 -0300128 * vimc_pads_init - initialize pads
129 *
130 * @num_pads: number of pads to initialize
131 * @pads_flags: flags to use in each pad
132 *
133 * Helper functions to allocate/initialize pads
134 */
135struct media_pad *vimc_pads_init(u16 num_pads,
136 const unsigned long *pads_flag);
137
138/**
139 * vimc_pads_cleanup - free pads
140 *
141 * @pads: pointer to the pads
142 *
143 * Helper function to free the pads initialized with vimc_pads_init
144 */
145static inline void vimc_pads_cleanup(struct media_pad *pads)
146{
147 kfree(pads);
148}
149
150/**
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300151 * vimc_pipeline_s_stream - start stream through the pipeline
152 *
153 * @ent: the pointer to struct media_entity for the node
154 * @enable: 1 to start the stream and 0 to stop
155 *
156 * Helper function to call the s_stream of the subdevices connected
157 * in all the sink pads of the entity
158 */
159int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
160
161/**
Helen Fornazierc1495432017-06-19 14:00:12 -0300162 * vimc_ent_sd_register - initialize and register a subdev node
163 *
164 * @ved: the vimc_ent_device struct to be initialize
165 * @sd: the v4l2_subdev struct to be initialize and registered
166 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
167 * @name: name of the sub-device. Please notice that the name must be
168 * unique.
169 * @function: media entity function defined by MEDIA_ENT_F_* macros
170 * @num_pads: number of pads to initialize
171 * @pads_flag: flags to use in each pad
Hans Verkuil2b177f22019-03-05 03:36:20 -0500172 * @sd_int_ops: pointer to &struct v4l2_subdev_internal_ops
Helen Fornazierc1495432017-06-19 14:00:12 -0300173 * @sd_ops: pointer to &struct v4l2_subdev_ops.
Helen Fornazierc1495432017-06-19 14:00:12 -0300174 *
175 * Helper function initialize and register the struct vimc_ent_device and struct
176 * v4l2_subdev which represents a subdev node in the topology
177 */
178int vimc_ent_sd_register(struct vimc_ent_device *ved,
179 struct v4l2_subdev *sd,
180 struct v4l2_device *v4l2_dev,
181 const char *const name,
182 u32 function,
183 u16 num_pads,
184 const unsigned long *pads_flag,
Hans Verkuil2b177f22019-03-05 03:36:20 -0500185 const struct v4l2_subdev_internal_ops *sd_int_ops,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300186 const struct v4l2_subdev_ops *sd_ops);
Helen Fornazierc1495432017-06-19 14:00:12 -0300187
188/**
Helen Fornazier4a29b702017-06-19 14:00:18 -0300189 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
Helen Fornazierc1495432017-06-19 14:00:12 -0300190 *
Helen Fornazier4a29b702017-06-19 14:00:18 -0300191 * @ved: the vimc_ent_device struct to be cleaned up
192 * @sd: the v4l2_subdev struct to be unregistered
Helen Fornazierc1495432017-06-19 14:00:12 -0300193 *
194 * Helper function cleanup and unregister the struct vimc_ent_device and struct
195 * v4l2_subdev which represents a subdev node in the topology
196 */
197void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
198 struct v4l2_subdev *sd);
199
Helen Fornazier288a22d2017-06-19 14:00:14 -0300200/**
201 * vimc_link_validate - validates a media link
202 *
203 * @link: pointer to &struct media_link
204 *
205 * This function calls validates if a media link is valid for streaming.
206 */
207int vimc_link_validate(struct media_link *link);
208
Helen Koikef2fe8902017-04-07 14:55:19 -0300209#endif