blob: fb3463c06185e74066fd0ddcb77ef3ff441947f1 [file] [log] [blame]
Helen Koikef2fe8902017-04-07 14:55:19 -03001/*
Helen Fornazier5ba0ae42017-06-19 14:00:11 -03002 * vimc-ccommon.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 Fornazier88ad71a2017-06-19 14:00:16 -030025#define VIMC_FRAME_MAX_WIDTH 4096
26#define VIMC_FRAME_MAX_HEIGHT 2160
27#define VIMC_FRAME_MIN_WIDTH 16
28#define VIMC_FRAME_MIN_HEIGHT 16
29
Helen Koikef2fe8902017-04-07 14:55:19 -030030/**
Helen Fornazier441c0db2017-06-19 14:00:15 -030031 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
32 *
33 * @fmt: the pointer to struct v4l2_pix_format or
34 * struct v4l2_mbus_framefmt
35 *
36 * Entities must check if colorimetry given by the userspace is valid, if not
37 * then set them as DEFAULT
38 */
39#define vimc_colorimetry_clamp(fmt) \
40do { \
41 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
42 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
43 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
44 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
45 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
46 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
47 } \
48 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
49 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
50 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
51 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
52 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
53 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
54} while (0)
55
56/**
Helen Koikef2fe8902017-04-07 14:55:19 -030057 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
58 *
59 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
60 * @bbp: number of bytes each pixel occupies
61 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
62 *
63 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
64 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
65 */
66struct vimc_pix_map {
67 unsigned int code;
68 unsigned int bpp;
69 u32 pixelformat;
70};
71
72/**
73 * 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
77 * @destroy: callback to destroy the node
78 * @process_frame: callback send a frame to that node
Helen Fornazier288a22d2017-06-19 14:00:14 -030079 * @vdev_get_format: callback that returns the current format a pad, used
80 * only when is_media_entity_v4l2_video_device(ent) returns
81 * true
Helen Koikef2fe8902017-04-07 14:55:19 -030082 *
83 * Each node of the topology must create a vimc_ent_device struct. Depending on
84 * the node it will be of an instance of v4l2_subdev or video_device struct
85 * where both contains a struct media_entity.
86 * Those structures should embedded the vimc_ent_device struct through
87 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
88 * vimc_ent_device struct to be retrieved from the corresponding struct
89 * media_entity
90 */
91struct vimc_ent_device {
92 struct media_entity *ent;
93 struct media_pad *pads;
94 void (*destroy)(struct vimc_ent_device *);
95 void (*process_frame)(struct vimc_ent_device *ved,
96 struct media_pad *sink, const void *frame);
Helen Fornazier288a22d2017-06-19 14:00:14 -030097 void (*vdev_get_format)(struct vimc_ent_device *ved,
98 struct v4l2_pix_format *fmt);
Helen Koikef2fe8902017-04-07 14:55:19 -030099};
100
101/**
102 * vimc_propagate_frame - propagate a frame through the topology
103 *
104 * @src: the source pad where the frame is being originated
105 * @frame: the frame to be propagated
106 *
107 * This function will call the process_frame callback from the vimc_ent_device
108 * struct of the nodes directly connected to the @src pad
109 */
110int vimc_propagate_frame(struct media_pad *src, const void *frame);
111
112/**
113 * vimc_pads_init - initialize pads
114 *
115 * @num_pads: number of pads to initialize
116 * @pads_flags: flags to use in each pad
117 *
118 * Helper functions to allocate/initialize pads
119 */
120struct media_pad *vimc_pads_init(u16 num_pads,
121 const unsigned long *pads_flag);
122
123/**
124 * vimc_pads_cleanup - free pads
125 *
126 * @pads: pointer to the pads
127 *
128 * Helper function to free the pads initialized with vimc_pads_init
129 */
130static inline void vimc_pads_cleanup(struct media_pad *pads)
131{
132 kfree(pads);
133}
134
135/**
Helen Fornazierbf5fb952017-06-19 14:00:13 -0300136 * vimc_pipeline_s_stream - start stream through the pipeline
137 *
138 * @ent: the pointer to struct media_entity for the node
139 * @enable: 1 to start the stream and 0 to stop
140 *
141 * Helper function to call the s_stream of the subdevices connected
142 * in all the sink pads of the entity
143 */
144int vimc_pipeline_s_stream(struct media_entity *ent, int enable);
145
146/**
Helen Fornazier88ad71a2017-06-19 14:00:16 -0300147 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
148 *
149 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
150 */
151const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
152
153/**
Helen Koikef2fe8902017-04-07 14:55:19 -0300154 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
155 *
156 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
157 */
158const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
159
160/**
161 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
162 *
163 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
164 */
165const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
166
Helen Fornazierc1495432017-06-19 14:00:12 -0300167/**
168 * vimc_ent_sd_register - initialize and register a subdev node
169 *
170 * @ved: the vimc_ent_device struct to be initialize
171 * @sd: the v4l2_subdev struct to be initialize and registered
172 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
173 * @name: name of the sub-device. Please notice that the name must be
174 * unique.
175 * @function: media entity function defined by MEDIA_ENT_F_* macros
176 * @num_pads: number of pads to initialize
177 * @pads_flag: flags to use in each pad
178 * @sd_ops: pointer to &struct v4l2_subdev_ops.
179 * @sd_destroy: callback to destroy the node
180 *
181 * Helper function initialize and register the struct vimc_ent_device and struct
182 * v4l2_subdev which represents a subdev node in the topology
183 */
184int vimc_ent_sd_register(struct vimc_ent_device *ved,
185 struct v4l2_subdev *sd,
186 struct v4l2_device *v4l2_dev,
187 const char *const name,
188 u32 function,
189 u16 num_pads,
190 const unsigned long *pads_flag,
191 const struct v4l2_subdev_ops *sd_ops,
192 void (*sd_destroy)(struct vimc_ent_device *));
193
194/**
195 * vimc_ent_sd_register - initialize and register a subdev node
196 *
197 * @ved: the vimc_ent_device struct to be initialize
198 * @sd: the v4l2_subdev struct to be initialize and registered
199 *
200 * Helper function cleanup and unregister the struct vimc_ent_device and struct
201 * v4l2_subdev which represents a subdev node in the topology
202 */
203void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
204 struct v4l2_subdev *sd);
205
Helen Fornazier288a22d2017-06-19 14:00:14 -0300206/**
207 * vimc_link_validate - validates a media link
208 *
209 * @link: pointer to &struct media_link
210 *
211 * This function calls validates if a media link is valid for streaming.
212 */
213int vimc_link_validate(struct media_link *link);
214
Helen Koikef2fe8902017-04-07 14:55:19 -0300215#endif