blob: 25ba75283ed490884f9086ab17910c781f2fdb56 [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 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 Fornazier88f42bf2017-06-19 14:00:19 -030030#define VIMC_FRAME_INDEX(lin, col, width, bpp) ((lin * width + col) * bpp)
31
Helen Koikef2fe8902017-04-07 14:55:19 -030032/**
Helen Fornazier441c0db2017-06-19 14:00:15 -030033 * struct vimc_colorimetry_clamp - Adjust colorimetry parameters
34 *
35 * @fmt: the pointer to struct v4l2_pix_format or
36 * struct v4l2_mbus_framefmt
37 *
38 * Entities must check if colorimetry given by the userspace is valid, if not
39 * then set them as DEFAULT
40 */
41#define vimc_colorimetry_clamp(fmt) \
42do { \
43 if ((fmt)->colorspace == V4L2_COLORSPACE_DEFAULT \
44 || (fmt)->colorspace > V4L2_COLORSPACE_DCI_P3) { \
45 (fmt)->colorspace = V4L2_COLORSPACE_DEFAULT; \
46 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
47 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
48 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
49 } \
50 if ((fmt)->ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) \
51 (fmt)->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; \
52 if ((fmt)->quantization > V4L2_QUANTIZATION_LIM_RANGE) \
53 (fmt)->quantization = V4L2_QUANTIZATION_DEFAULT; \
54 if ((fmt)->xfer_func > V4L2_XFER_FUNC_SMPTE2084) \
55 (fmt)->xfer_func = V4L2_XFER_FUNC_DEFAULT; \
56} while (0)
57
58/**
Helen Fornazier4a29b702017-06-19 14:00:18 -030059 * struct vimc_platform_data - platform data to components
60 *
61 * @entity_name: The name of the entity to be created
62 *
63 * Board setup code will often provide additional information using the device's
64 * platform_data field to hold additional information.
65 * When injecting a new platform_device in the component system the core needs
66 * to provide to the corresponding submodules the name of the entity that should
67 * be used when registering the subdevice in the Media Controller system.
68 */
69struct vimc_platform_data {
70 char entity_name[32];
71};
72
73/**
Helen Koikef2fe8902017-04-07 14:55:19 -030074 * struct vimc_pix_map - maps media bus code with v4l2 pixel format
75 *
76 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
77 * @bbp: number of bytes each pixel occupies
78 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
79 *
80 * Struct which matches the MEDIA_BUS_FMT_* codes with the corresponding
81 * V4L2_PIX_FMT_* fourcc pixelformat and its bytes per pixel (bpp)
82 */
83struct vimc_pix_map {
84 unsigned int code;
85 unsigned int bpp;
86 u32 pixelformat;
87};
88
89/**
90 * struct vimc_ent_device - core struct that represents a node in the topology
91 *
92 * @ent: the pointer to struct media_entity for the node
93 * @pads: the list of pads of the node
Helen Koikef2fe8902017-04-07 14:55:19 -030094 * @process_frame: callback send a frame to that node
Helen Fornazier288a22d2017-06-19 14:00:14 -030095 * @vdev_get_format: callback that returns the current format a pad, used
96 * only when is_media_entity_v4l2_video_device(ent) returns
97 * true
Helen Koikef2fe8902017-04-07 14:55:19 -030098 *
99 * Each node of the topology must create a vimc_ent_device struct. Depending on
100 * the node it will be of an instance of v4l2_subdev or video_device struct
101 * where both contains a struct media_entity.
102 * Those structures should embedded the vimc_ent_device struct through
103 * v4l2_set_subdevdata() and video_set_drvdata() respectivaly, allowing the
104 * vimc_ent_device struct to be retrieved from the corresponding struct
105 * media_entity
106 */
107struct vimc_ent_device {
108 struct media_entity *ent;
109 struct media_pad *pads;
Helen Koikef2fe8902017-04-07 14:55:19 -0300110 void (*process_frame)(struct vimc_ent_device *ved,
111 struct media_pad *sink, const void *frame);
Helen Fornazier288a22d2017-06-19 14:00:14 -0300112 void (*vdev_get_format)(struct vimc_ent_device *ved,
113 struct v4l2_pix_format *fmt);
Helen Koikef2fe8902017-04-07 14:55:19 -0300114};
115
116/**
117 * vimc_propagate_frame - propagate a frame through the topology
118 *
119 * @src: the source pad where the frame is being originated
120 * @frame: the frame to be propagated
121 *
122 * This function will call the process_frame callback from the vimc_ent_device
123 * struct of the nodes directly connected to the @src pad
124 */
125int vimc_propagate_frame(struct media_pad *src, const void *frame);
126
127/**
128 * 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 Fornazier88ad71a2017-06-19 14:00:16 -0300162 * vimc_pix_map_by_index - get vimc_pix_map struct by its index
163 *
164 * @i: index of the vimc_pix_map struct in vimc_pix_map_list
165 */
166const struct vimc_pix_map *vimc_pix_map_by_index(unsigned int i);
167
168/**
Helen Koikef2fe8902017-04-07 14:55:19 -0300169 * vimc_pix_map_by_code - get vimc_pix_map struct by media bus code
170 *
171 * @code: media bus format code defined by MEDIA_BUS_FMT_* macros
172 */
173const struct vimc_pix_map *vimc_pix_map_by_code(u32 code);
174
175/**
176 * vimc_pix_map_by_pixelformat - get vimc_pix_map struct by v4l2 pixel format
177 *
178 * @pixelformat: pixel format devined by V4L2_PIX_FMT_* macros
179 */
180const struct vimc_pix_map *vimc_pix_map_by_pixelformat(u32 pixelformat);
181
Helen Fornazierc1495432017-06-19 14:00:12 -0300182/**
183 * vimc_ent_sd_register - initialize and register a subdev node
184 *
185 * @ved: the vimc_ent_device struct to be initialize
186 * @sd: the v4l2_subdev struct to be initialize and registered
187 * @v4l2_dev: the v4l2 device to register the v4l2_subdev
188 * @name: name of the sub-device. Please notice that the name must be
189 * unique.
190 * @function: media entity function defined by MEDIA_ENT_F_* macros
191 * @num_pads: number of pads to initialize
192 * @pads_flag: flags to use in each pad
193 * @sd_ops: pointer to &struct v4l2_subdev_ops.
Helen Fornazierc1495432017-06-19 14:00:12 -0300194 *
195 * Helper function initialize and register the struct vimc_ent_device and struct
196 * v4l2_subdev which represents a subdev node in the topology
197 */
198int vimc_ent_sd_register(struct vimc_ent_device *ved,
199 struct v4l2_subdev *sd,
200 struct v4l2_device *v4l2_dev,
201 const char *const name,
202 u32 function,
203 u16 num_pads,
204 const unsigned long *pads_flag,
Helen Fornazier4a29b702017-06-19 14:00:18 -0300205 const struct v4l2_subdev_ops *sd_ops);
Helen Fornazierc1495432017-06-19 14:00:12 -0300206
207/**
Helen Fornazier4a29b702017-06-19 14:00:18 -0300208 * vimc_ent_sd_unregister - cleanup and unregister a subdev node
Helen Fornazierc1495432017-06-19 14:00:12 -0300209 *
Helen Fornazier4a29b702017-06-19 14:00:18 -0300210 * @ved: the vimc_ent_device struct to be cleaned up
211 * @sd: the v4l2_subdev struct to be unregistered
Helen Fornazierc1495432017-06-19 14:00:12 -0300212 *
213 * Helper function cleanup and unregister the struct vimc_ent_device and struct
214 * v4l2_subdev which represents a subdev node in the topology
215 */
216void vimc_ent_sd_unregister(struct vimc_ent_device *ved,
217 struct v4l2_subdev *sd);
218
Helen Fornazier288a22d2017-06-19 14:00:14 -0300219/**
220 * vimc_link_validate - validates a media link
221 *
222 * @link: pointer to &struct media_link
223 *
224 * This function calls validates if a media link is valid for streaming.
225 */
226int vimc_link_validate(struct media_link *link);
227
Helen Koikef2fe8902017-04-07 14:55:19 -0300228#endif