Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / media / v4l2-subdev.h
CommitLineData
c942fddf 1/* SPDX-License-Identifier: GPL-2.0-or-later */
2a1fcdf0 2/*
21c29de1
MCC
3 * V4L2 sub-device support header.
4 *
5 * Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
2a1fcdf0
HV
6 */
7
8#ifndef _V4L2_SUBDEV_H
9#define _V4L2_SUBDEV_H
10
29103119 11#include <linux/types.h>
333c8b97 12#include <linux/v4l2-subdev.h>
61f5db54 13#include <media/media-entity.h>
e9e31049 14#include <media/v4l2-async.h>
2a1fcdf0 15#include <media/v4l2-common.h>
2096a5dc 16#include <media/v4l2-dev.h>
7cd5a16b 17#include <media/v4l2-fh.h>
9a74251d 18#include <media/v4l2-mediabus.h>
2a1fcdf0 19
1d986add
AW
20/* generic v4l2_device notify callback notification values */
21#define V4L2_SUBDEV_IR_RX_NOTIFY _IOW('v', 0, u32)
22#define V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ 0x00000001
23#define V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED 0x00000002
24#define V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN 0x00000004
25#define V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN 0x00000008
26
27#define V4L2_SUBDEV_IR_TX_NOTIFY _IOW('v', 1, u32)
28#define V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ 0x00000001
29
17e48468 30#define V4L2_DEVICE_NOTIFY_EVENT _IOW('v', 2, struct v4l2_event)
31
2a1fcdf0 32struct v4l2_device;
0996517c 33struct v4l2_ctrl_handler;
8ae5640f 34struct v4l2_event;
02adb1cc
SA
35struct v4l2_event_subscription;
36struct v4l2_fh;
2a1fcdf0 37struct v4l2_subdev;
f0beea8f 38struct v4l2_subdev_fh;
2a1fcdf0 39struct tuner_setup;
29103119 40struct v4l2_mbus_frame_desc;
b6e10ff6 41struct led_classdev;
2a1fcdf0 42
21c29de1
MCC
43/**
44 * struct v4l2_decode_vbi_line - used to decode_vbi_line
45 *
46 * @is_second_field: Set to 0 for the first (odd) field;
47 * set to 1 for the second (even) field.
48 * @p: Pointer to the sliced VBI data from the decoder. On exit, points to
49 * the start of the payload.
50 * @line: Line number of the sliced VBI data (1-23)
51 * @type: VBI service type (V4L2_SLICED_*). 0 if no service found
52 */
78a3b4db 53struct v4l2_decode_vbi_line {
21c29de1
MCC
54 u32 is_second_field;
55 u8 *p;
56 u32 line;
57 u32 type;
78a3b4db
HV
58};
59
21c29de1
MCC
60/*
61 * Sub-devices are devices that are connected somehow to the main bridge
62 * device. These devices are usually audio/video muxers/encoders/decoders or
63 * sensors and webcam controllers.
64 *
e907bf3c 65 * Usually these devices are controlled through an i2c bus, but other buses
21c29de1
MCC
66 * may also be used.
67 *
68 * The v4l2_subdev struct provides a way of accessing these devices in a
69 * generic manner. Most operations that these sub-devices support fall in
70 * a few categories: core ops, audio ops, video ops and tuner ops.
71 *
72 * More categories can be added if needed, although this should remain a
73 * limited set (no more than approx. 8 categories).
74 *
75 * Each category has its own set of ops that subdev drivers can implement.
76 *
77 * A subdev driver can leave the pointer to the category ops NULL if
78 * it does not implement them (e.g. an audio subdev will generally not
79 * implement the video category ops). The exception is the core category:
80 * this must always be present.
81 *
82 * These ops are all used internally so it is no problem to change, remove
83 * or add ops or move ops from one to another category. Currently these
84 * ops are based on the original ioctls, but since ops are not limited to
85 * one argument there is room for improvement here once all i2c subdev
86 * drivers are converted to use these ops.
2a1fcdf0
HV
87 */
88
21c29de1
MCC
89/*
90 * Core ops: it is highly recommended to implement at least these ops:
91 *
92 * log_status
93 * g_register
94 * s_register
95 *
96 * This provides basic debugging support.
97 *
98 * The ioctl ops is meant for generic ioctl-like commands. Depending on
99 * the use-case it might be better to use subdev-specific ops (currently
100 * not yet implemented) since ops provide proper type-checking.
2a1fcdf0 101 */
78a3b4db 102
4eb2f557
MCC
103/**
104 * enum v4l2_subdev_io_pin_bits - Subdevice external IO pin configuration
105 * bits
106 *
107 * @V4L2_SUBDEV_IO_PIN_DISABLE: disables a pin config. ENABLE assumed.
108 * @V4L2_SUBDEV_IO_PIN_OUTPUT: set it if pin is an output.
109 * @V4L2_SUBDEV_IO_PIN_INPUT: set it if pin is an input.
110 * @V4L2_SUBDEV_IO_PIN_SET_VALUE: to set the output value via
4a3fad70 111 * &struct v4l2_subdev_io_pin_config->value.
4eb2f557
MCC
112 * @V4L2_SUBDEV_IO_PIN_ACTIVE_LOW: pin active is bit 0.
113 * Otherwise, ACTIVE HIGH is assumed.
114 */
115enum v4l2_subdev_io_pin_bits {
116 V4L2_SUBDEV_IO_PIN_DISABLE = 0,
117 V4L2_SUBDEV_IO_PIN_OUTPUT = 1,
118 V4L2_SUBDEV_IO_PIN_INPUT = 2,
119 V4L2_SUBDEV_IO_PIN_SET_VALUE = 3,
120 V4L2_SUBDEV_IO_PIN_ACTIVE_LOW = 4,
121};
66752f83 122
21c29de1
MCC
123/**
124 * struct v4l2_subdev_io_pin_config - Subdevice external IO pin configuration
125 *
4eb2f557
MCC
126 * @flags: bitmask with flags for this pin's config, whose bits are defined by
127 * &enum v4l2_subdev_io_pin_bits.
21c29de1
MCC
128 * @pin: Chip external IO pin to configure
129 * @function: Internal signal pad/function to route to IO pin
130 * @value: Initial value for pin - e.g. GPIO output value
131 * @strength: Pin drive strength
132 */
66752f83 133struct v4l2_subdev_io_pin_config {
21c29de1
MCC
134 u32 flags;
135 u8 pin;
136 u8 function;
137 u8 value;
138 u8 strength;
66752f83
AW
139};
140
5c662984 141/**
783001ad
MCC
142 * struct v4l2_subdev_core_ops - Define core ops callbacks for subdevs
143 *
3fb558f6 144 * @log_status: callback for VIDIOC_LOG_STATUS() ioctl handler code.
5c662984
MCC
145 *
146 * @s_io_pin_config: configure one or more chip I/O pins for chips that
147 * multiplex different internal signal pads out to IO pins. This function
148 * takes a pointer to an array of 'n' pin configuration entries, one for
149 * each pin being configured. This function could be called at times
150 * other than just subdevice initialization.
151 *
152 * @init: initialize the sensor registers to some sort of reasonable default
153 * values. Do not use for new drivers and should be removed in existing
154 * drivers.
155 *
156 * @load_fw: load firmware.
157 *
158 * @reset: generic reset command. The argument selects which subsystems to
159 * reset. Passing 0 will always reset the whole chip. Do not use for new
160 * drivers without discussing this first on the linux-media mailinglist.
161 * There should be no reason normally to reset a device.
162 *
163 * @s_gpio: set GPIO pins. Very simple right now, might need to be extended with
164 * a direction argument if needed.
165 *
0a7790be
AB
166 * @command: called by in-kernel drivers in order to call functions internal
167 * to subdev drivers driver that have a separate callback.
168 *
1d8955b2
MCC
169 * @ioctl: called at the end of ioctl() syscall handler at the V4L2 core.
170 * used to provide support for private ioctls used on the driver.
171 *
172 * @compat_ioctl32: called when a 32 bits application uses a 64 bits Kernel,
173 * in order to fix data passed from/to userspace.
174 *
3fb558f6 175 * @g_register: callback for VIDIOC_DBG_G_REGISTER() ioctl handler code.
783001ad 176 *
3fb558f6 177 * @s_register: callback for VIDIOC_DBG_S_REGISTER() ioctl handler code.
783001ad 178 *
5c662984 179 * @s_power: puts subdevice in power saving mode (on == 0) or normal operation
c2079f3e
SA
180 * mode (on == 1). DEPRECATED. See
181 * Documentation/driver-api/media/camera-sensor.rst . pre_streamon and
182 * post_streamoff callbacks can be used for e.g. setting the bus to LP-11
183 * mode before s_stream is called.
5c662984
MCC
184 *
185 * @interrupt_service_routine: Called by the bridge chip's interrupt service
186 * handler, when an interrupt status has be raised due to this subdev,
187 * so that this subdev can handle the details. It may schedule work to be
21c29de1 188 * performed later. It must not sleep. **Called from an IRQ context**.
1d8955b2
MCC
189 *
190 * @subscribe_event: used by the drivers to request the control framework that
191 * for it to be warned when the value of a control changes.
192 *
193 * @unsubscribe_event: remove event subscription from the control framework.
78a3b4db 194 */
2a1fcdf0 195struct v4l2_subdev_core_ops {
2a1fcdf0 196 int (*log_status)(struct v4l2_subdev *sd);
66752f83
AW
197 int (*s_io_pin_config)(struct v4l2_subdev *sd, size_t n,
198 struct v4l2_subdev_io_pin_config *pincfg);
2a1fcdf0 199 int (*init)(struct v4l2_subdev *sd, u32 val);
cc26b076 200 int (*load_fw)(struct v4l2_subdev *sd);
2a1fcdf0
HV
201 int (*reset)(struct v4l2_subdev *sd, u32 val);
202 int (*s_gpio)(struct v4l2_subdev *sd, u32 val);
0a7790be 203 long (*command)(struct v4l2_subdev *sd, unsigned int cmd, void *arg);
069b7479 204 long (*ioctl)(struct v4l2_subdev *sd, unsigned int cmd, void *arg);
ab58a301
HV
205#ifdef CONFIG_COMPAT
206 long (*compat_ioctl32)(struct v4l2_subdev *sd, unsigned int cmd,
207 unsigned long arg);
208#endif
2a1fcdf0 209#ifdef CONFIG_VIDEO_ADV_DEBUG
aecde8b5 210 int (*g_register)(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg);
977ba3b1 211 int (*s_register)(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg);
2a1fcdf0 212#endif
622b828a 213 int (*s_power)(struct v4l2_subdev *sd, int on);
260e689b
AW
214 int (*interrupt_service_routine)(struct v4l2_subdev *sd,
215 u32 status, bool *handled);
02adb1cc
SA
216 int (*subscribe_event)(struct v4l2_subdev *sd, struct v4l2_fh *fh,
217 struct v4l2_event_subscription *sub);
218 int (*unsubscribe_event)(struct v4l2_subdev *sd, struct v4l2_fh *fh,
219 struct v4l2_event_subscription *sub);
2a1fcdf0
HV
220};
221
5c662984 222/**
e383ce07
MCC
223 * struct v4l2_subdev_tuner_ops - Callbacks used when v4l device was opened
224 * in radio mode.
5c662984 225 *
3aab15af
HV
226 * @standby: puts the tuner in standby mode. It will be woken up
227 * automatically the next time it is used.
228 *
07bf9355
MCC
229 * @s_radio: callback that switches the tuner to radio mode.
230 * drivers should explicitly call it when a tuner ops should
231 * operate on radio mode, before being able to handle it.
232 * Used on devices that have both AM/FM radio receiver and TV.
91963aae 233 *
3fb558f6 234 * @s_frequency: callback for VIDIOC_S_FREQUENCY() ioctl handler code.
91963aae 235 *
3fb558f6 236 * @g_frequency: callback for VIDIOC_G_FREQUENCY() ioctl handler code.
21c29de1 237 * freq->type must be filled in. Normally done by video_ioctl2()
89cb3ddb 238 * or the bridge driver.
91963aae 239 *
3fb558f6 240 * @enum_freq_bands: callback for VIDIOC_ENUM_FREQ_BANDS() ioctl handler code.
91963aae 241 *
3fb558f6 242 * @g_tuner: callback for VIDIOC_G_TUNER() ioctl handler code.
91963aae 243 *
3fb558f6 244 * @s_tuner: callback for VIDIOC_S_TUNER() ioctl handler code. @vt->type must be
91963aae 245 * filled in. Normally done by video_ioctl2 or the
89cb3ddb 246 * bridge driver.
5c662984 247 *
3fb558f6 248 * @g_modulator: callback for VIDIOC_G_MODULATOR() ioctl handler code.
91963aae 249 *
3fb558f6 250 * @s_modulator: callback for VIDIOC_S_MODULATOR() ioctl handler code.
91963aae 251 *
5c662984 252 * @s_type_addr: sets tuner type and its I2C addr.
91963aae 253 *
5c662984 254 * @s_config: sets tda9887 specific stuff, like port1, port2 and qss
07bf9355
MCC
255 *
256 * .. note::
257 *
4a3fad70 258 * On devices that have both AM/FM and TV, it is up to the driver
07bf9355
MCC
259 * to explicitly call s_radio when the tuner should be switched to
260 * radio mode, before handling other &struct v4l2_subdev_tuner_ops
261 * that would require it. An example of such usage is::
262 *
263 * static void s_frequency(void *priv, const struct v4l2_frequency *f)
264 * {
4a3fad70 265 * ...
07bf9355
MCC
266 * if (f.type == V4L2_TUNER_RADIO)
267 * v4l2_device_call_all(v4l2_dev, 0, tuner, s_radio);
268 * ...
269 * v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency);
270 * }
78a3b4db 271 */
2a1fcdf0 272struct v4l2_subdev_tuner_ops {
3aab15af 273 int (*standby)(struct v4l2_subdev *sd);
2a1fcdf0 274 int (*s_radio)(struct v4l2_subdev *sd);
b530a447 275 int (*s_frequency)(struct v4l2_subdev *sd, const struct v4l2_frequency *freq);
2a1fcdf0 276 int (*g_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq);
c58d1de5 277 int (*enum_freq_bands)(struct v4l2_subdev *sd, struct v4l2_frequency_band *band);
2a1fcdf0 278 int (*g_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt);
2f73c7c5 279 int (*s_tuner)(struct v4l2_subdev *sd, const struct v4l2_tuner *vt);
d6bacea6 280 int (*g_modulator)(struct v4l2_subdev *sd, struct v4l2_modulator *vm);
3f70e1f5 281 int (*s_modulator)(struct v4l2_subdev *sd, const struct v4l2_modulator *vm);
2a1fcdf0
HV
282 int (*s_type_addr)(struct v4l2_subdev *sd, struct tuner_setup *type);
283 int (*s_config)(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *config);
284};
285
5c662984
MCC
286/**
287 * struct v4l2_subdev_audio_ops - Callbacks used for audio-related settings
288 *
289 * @s_clock_freq: set the frequency (in Hz) of the audio clock output.
290 * Used to slave an audio processor to the video decoder, ensuring that
291 * audio and video remain synchronized. Usual values for the frequency
292 * are 48000, 44100 or 32000 Hz. If the frequency is not supported, then
293 * -EINVAL is returned.
294 *
295 * @s_i2s_clock_freq: sets I2S speed in bps. This is used to provide a standard
296 * way to select I2S clock used by driving digital audio streams at some
297 * board designs. Usual values for the frequency are 1024000 and 2048000.
21c29de1 298 * If the frequency is not supported, then %-EINVAL is returned.
5c662984
MCC
299 *
300 * @s_routing: used to define the input and/or output pins of an audio chip,
301 * and any additional configuration data.
302 * Never attempt to use user-level input IDs (e.g. Composite, S-Video,
303 * Tuner) at this level. An i2c device shouldn't know about whether an
304 * input pin is connected to a Composite connector, become on another
305 * board or platform it might be connected to something else entirely.
306 * The calling driver is responsible for mapping a user-level input to
307 * the right pins on the i2c device.
1d8955b2
MCC
308 *
309 * @s_stream: used to notify the audio code that stream will start or has
310 * stopped.
78a3b4db 311 */
2a1fcdf0
HV
312struct v4l2_subdev_audio_ops {
313 int (*s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
314 int (*s_i2s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
5325b427 315 int (*s_routing)(struct v4l2_subdev *sd, u32 input, u32 output, u32 config);
3ccc646b 316 int (*s_stream)(struct v4l2_subdev *sd, int enable);
2a1fcdf0
HV
317};
318
897c45df
SA
319/**
320 * struct v4l2_mbus_frame_desc_entry_csi2
321 *
322 * @vc: CSI-2 virtual channel
323 * @dt: CSI-2 data type ID
324 */
325struct v4l2_mbus_frame_desc_entry_csi2 {
326 u8 vc;
327 u8 dt;
328};
329
bb92895a 330/**
b0649455 331 * enum v4l2_mbus_frame_desc_flags - media bus frame description flags
bb92895a
MCC
332 *
333 * @V4L2_MBUS_FRAME_DESC_FL_LEN_MAX:
334 * Indicates that &struct v4l2_mbus_frame_desc_entry->length field
335 * specifies maximum data length.
336 * @V4L2_MBUS_FRAME_DESC_FL_BLOB:
337 * Indicates that the format does not have line offsets, i.e.
338 * the receiver should use 1D DMA.
174d6a39 339 */
bb92895a
MCC
340enum v4l2_mbus_frame_desc_flags {
341 V4L2_MBUS_FRAME_DESC_FL_LEN_MAX = BIT(0),
342 V4L2_MBUS_FRAME_DESC_FL_BLOB = BIT(1),
343};
29103119
SN
344
345/**
346 * struct v4l2_mbus_frame_desc_entry - media bus frame description structure
5c662984 347 *
bb92895a 348 * @flags: bitmask flags, as defined by &enum v4l2_mbus_frame_desc_flags.
9037d130 349 * @stream: stream in routing configuration
bb92895a 350 * @pixelcode: media bus pixel code, valid if @flags
4a3fad70 351 * %FRAME_DESC_FL_BLOB is not set.
bb92895a
MCC
352 * @length: number of octets per frame, valid if @flags
353 * %V4L2_MBUS_FRAME_DESC_FL_LEN_MAX is set.
897c45df
SA
354 * @bus: Bus-specific frame descriptor parameters
355 * @bus.csi2: CSI-2-specific bus configuration
29103119
SN
356 */
357struct v4l2_mbus_frame_desc_entry {
bb92895a 358 enum v4l2_mbus_frame_desc_flags flags;
9037d130 359 u32 stream;
29103119
SN
360 u32 pixelcode;
361 u32 length;
897c45df
SA
362 union {
363 struct v4l2_mbus_frame_desc_entry_csi2 csi2;
364 } bus;
29103119
SN
365};
366
87d36eb8
TV
367 /*
368 * If this number is too small, it should be dropped altogether and the
369 * API switched to a dynamic number of frame descriptor entries.
370 */
371#define V4L2_FRAME_DESC_ENTRY_MAX 8
29103119 372
a34cc79c
SA
373/**
374 * enum v4l2_mbus_frame_desc_type - media bus frame description type
375 *
376 * @V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED:
377 * Undefined frame desc type. Drivers should not use this, it is
378 * for backwards compatibility.
379 * @V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL:
380 * Parallel media bus.
381 * @V4L2_MBUS_FRAME_DESC_TYPE_CSI2:
382 * CSI-2 media bus. Frame desc parameters must be set in
383 * &struct v4l2_mbus_frame_desc_entry->csi2.
384 */
385enum v4l2_mbus_frame_desc_type {
386 V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED = 0,
387 V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL,
388 V4L2_MBUS_FRAME_DESC_TYPE_CSI2,
389};
390
29103119
SN
391/**
392 * struct v4l2_mbus_frame_desc - media bus data frame description
a34cc79c 393 * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
29103119
SN
394 * @entry: frame descriptors array
395 * @num_entries: number of entries in @entry array
396 */
397struct v4l2_mbus_frame_desc {
a34cc79c 398 enum v4l2_mbus_frame_desc_type type;
29103119
SN
399 struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
400 unsigned short num_entries;
401};
402
253171a0
SA
403/**
404 * enum v4l2_subdev_pre_streamon_flags - Flags for pre_streamon subdev core op
405 *
406 * @V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP: Set the transmitter to either LP-11
407 * or LP-111 mode before call to s_stream().
408 */
409enum v4l2_subdev_pre_streamon_flags {
410 V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP = BIT(0),
411};
412
5c662984
MCC
413/**
414 * struct v4l2_subdev_video_ops - Callbacks used when v4l device was opened
21c29de1 415 * in video mode.
b84aeb03
MCC
416 *
417 * @s_routing: see s_routing in audio_ops, except this version is for video
418 * devices.
419 *
420 * @s_crystal_freq: sets the frequency of the crystal used to generate the
421 * clocks in Hz. An extra flags field allows device specific configuration
422 * regarding clock frequency dividers, etc. If not used, then set flags
423 * to 0. If the frequency is not supported, then -EINVAL is returned.
424 *
3fb558f6 425 * @g_std: callback for VIDIOC_G_STD() ioctl handler code.
914728ab 426 *
3fb558f6 427 * @s_std: callback for VIDIOC_S_STD() ioctl handler code.
914728ab 428 *
5c662984
MCC
429 * @s_std_output: set v4l2_std_id for video OUTPUT devices. This is ignored by
430 * video input devices.
431 *
432 * @g_std_output: get current standard for video OUTPUT devices. This is ignored
433 * by video input devices.
434 *
3fb558f6 435 * @querystd: callback for VIDIOC_QUERYSTD() ioctl handler code.
914728ab 436 *
21c29de1 437 * @g_tvnorms: get &v4l2_std_id with all standards supported by the video
5c662984
MCC
438 * CAPTURE device. This is ignored by video output devices.
439 *
440 * @g_tvnorms_output: get v4l2_std_id with all standards supported by the video
441 * OUTPUT device. This is ignored by video capture devices.
442 *
21c29de1 443 * @g_input_status: get input status. Same as the status field in the
8162a0b5 444 * &struct v4l2_input
5c662984 445 *
379c2586
SA
446 * @s_stream: start (enabled == 1) or stop (enabled == 0) streaming on the
447 * sub-device. Failure on stop will remove any resources acquired in
448 * streaming start, while the error code is still returned by the driver.
449 * Also see call_s_stream wrapper in v4l2-subdev.c.
1d8955b2 450 *
ecf37493 451 * @g_pixelaspect: callback to return the pixelaspect ratio.
914728ab 452 *
3fb558f6
MCC
453 * @g_frame_interval: callback for VIDIOC_SUBDEV_G_FRAME_INTERVAL()
454 * ioctl handler code.
914728ab 455 *
3fb558f6
MCC
456 * @s_frame_interval: callback for VIDIOC_SUBDEV_S_FRAME_INTERVAL()
457 * ioctl handler code.
914728ab
MCC
458 *
459 * @s_dv_timings: Set custom dv timings in the sub device. This is used
5c662984
MCC
460 * when sub device is capable of setting detailed timing information
461 * in the hardware to generate/detect the video signal.
462 *
914728ab
MCC
463 * @g_dv_timings: Get custom dv timings in the sub device.
464 *
3fb558f6 465 * @query_dv_timings: callback for VIDIOC_QUERY_DV_TIMINGS() ioctl handler code.
b84aeb03 466 *
5c662984
MCC
467 * @s_rx_buffer: set a host allocated memory buffer for the subdev. The subdev
468 * can adjust @size to a lower value and must not write more data to the
469 * buffer starting at @data than the original value of @size.
253171a0
SA
470 *
471 * @pre_streamon: May be called before streaming is actually started, to help
472 * initialising the bus. Current usage is to set a CSI-2 transmitter to
473 * LP-11 or LP-111 mode before streaming. See &enum
474 * v4l2_subdev_pre_streamon_flags.
475 *
476 * pre_streamon shall return error if it cannot perform the operation as
477 * indicated by the flags argument. In particular, -EACCES indicates lack
478 * of support for the operation. The caller shall call post_streamoff for
479 * each successful call of pre_streamon.
480 *
481 * @post_streamoff: Called after streaming is stopped, but if and only if
482 * pre_streamon was called earlier.
78a3b4db 483 */
2a1fcdf0 484struct v4l2_subdev_video_ops {
5325b427 485 int (*s_routing)(struct v4l2_subdev *sd, u32 input, u32 output, u32 config);
3ff4ad81 486 int (*s_crystal_freq)(struct v4l2_subdev *sd, u32 freq, u32 flags);
8774bed9
LP
487 int (*g_std)(struct v4l2_subdev *sd, v4l2_std_id *norm);
488 int (*s_std)(struct v4l2_subdev *sd, v4l2_std_id norm);
2a1fcdf0 489 int (*s_std_output)(struct v4l2_subdev *sd, v4l2_std_id std);
5930ab2b 490 int (*g_std_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
1b6f1d96 491 int (*querystd)(struct v4l2_subdev *sd, v4l2_std_id *std);
48353863 492 int (*g_tvnorms)(struct v4l2_subdev *sd, v4l2_std_id *std);
839cf813 493 int (*g_tvnorms_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
1b6f1d96 494 int (*g_input_status)(struct v4l2_subdev *sd, u32 *status);
2a1fcdf0 495 int (*s_stream)(struct v4l2_subdev *sd, int enable);
ecf37493 496 int (*g_pixelaspect)(struct v4l2_subdev *sd, struct v4l2_fract *aspect);
35c3017a
LP
497 int (*g_frame_interval)(struct v4l2_subdev *sd,
498 struct v4l2_subdev_frame_interval *interval);
499 int (*s_frame_interval)(struct v4l2_subdev *sd,
500 struct v4l2_subdev_frame_interval *interval);
b6456c0c
MK
501 int (*s_dv_timings)(struct v4l2_subdev *sd,
502 struct v4l2_dv_timings *timings);
503 int (*g_dv_timings)(struct v4l2_subdev *sd,
504 struct v4l2_dv_timings *timings);
5d7758ee
HV
505 int (*query_dv_timings)(struct v4l2_subdev *sd,
506 struct v4l2_dv_timings *timings);
a375e1df
SN
507 int (*s_rx_buffer)(struct v4l2_subdev *sd, void *buf,
508 unsigned int *size);
253171a0
SA
509 int (*pre_streamon)(struct v4l2_subdev *sd, u32 flags);
510 int (*post_streamoff)(struct v4l2_subdev *sd);
2a1fcdf0
HV
511};
512
5c662984
MCC
513/**
514 * struct v4l2_subdev_vbi_ops - Callbacks used when v4l device was opened
21c29de1 515 * in video mode via the vbi device node.
5c662984
MCC
516 *
517 * @decode_vbi_line: video decoders that support sliced VBI need to implement
9ef0b3f3 518 * this ioctl. Field p of the &struct v4l2_decode_vbi_line is set to the
5c662984
MCC
519 * start of the VBI data that was generated by the decoder. The driver
520 * then parses the sliced VBI data and sets the other fields in the
521 * struct accordingly. The pointer p is updated to point to the start of
522 * the payload which can be copied verbatim into the data field of the
21c29de1 523 * &struct v4l2_sliced_vbi_data. If no valid VBI data was found, then the
5c662984
MCC
524 * type field is set to 0 on return.
525 *
526 * @s_vbi_data: used to generate VBI signals on a video signal.
21c29de1
MCC
527 * &struct v4l2_sliced_vbi_data is filled with the data packets that
528 * should be output. Note that if you set the line field to 0, then that
529 * VBI signal is disabled. If no valid VBI data was found, then the type
530 * field is set to 0 on return.
5c662984
MCC
531 *
532 * @g_vbi_data: used to obtain the sliced VBI packet from a readback register.
533 * Not all video decoders support this. If no data is available because
21c29de1 534 * the readback register contains invalid or erroneous data %-EIO is
5c662984
MCC
535 * returned. Note that you must fill in the 'id' member and the 'field'
536 * member (to determine whether CC data from the first or second field
537 * should be obtained).
538 *
3fb558f6
MCC
539 * @g_sliced_vbi_cap: callback for VIDIOC_G_SLICED_VBI_CAP() ioctl handler
540 * code.
e44cc20a 541 *
5c662984
MCC
542 * @s_raw_fmt: setup the video encoder/decoder for raw VBI.
543 *
544 * @g_sliced_fmt: retrieve the current sliced VBI settings.
545 *
546 * @s_sliced_fmt: setup the sliced VBI settings.
32cd527f
HV
547 */
548struct v4l2_subdev_vbi_ops {
549 int (*decode_vbi_line)(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi_line);
550 int (*s_vbi_data)(struct v4l2_subdev *sd, const struct v4l2_sliced_vbi_data *vbi_data);
551 int (*g_vbi_data)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *vbi_data);
552 int (*g_sliced_vbi_cap)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_cap *cap);
54aefafb
HV
553 int (*s_raw_fmt)(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt);
554 int (*g_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
555 int (*s_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
32cd527f
HV
556};
557
32536108
GL
558/**
559 * struct v4l2_subdev_sensor_ops - v4l2-subdev sensor operations
560 * @g_skip_top_lines: number of lines at the top of the image to be skipped.
561 * This is needed for some sensors, which always corrupt
562 * several top lines of the output image, or which send their
563 * metadata in them.
f80a3ab0
LP
564 * @g_skip_frames: number of frames to skip at stream start. This is needed for
565 * buggy sensors that generate faulty frames when they are
566 * turned on.
32536108
GL
567 */
568struct v4l2_subdev_sensor_ops {
569 int (*g_skip_top_lines)(struct v4l2_subdev *sd, u32 *lines);
f80a3ab0 570 int (*g_skip_frames)(struct v4l2_subdev *sd, u32 *frames);
32536108
GL
571};
572
21c29de1
MCC
573/**
574 * enum v4l2_subdev_ir_mode- describes the type of IR supported
575 *
576 * @V4L2_SUBDEV_IR_MODE_PULSE_WIDTH: IR uses struct ir_raw_event records
1d986add 577 */
1d986add 578enum v4l2_subdev_ir_mode {
21c29de1 579 V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1d986add
AW
580};
581
21c29de1
MCC
582/**
583 * struct v4l2_subdev_ir_parameters - Parameters for IR TX or TX
584 *
585 * @bytes_per_data_element: bytes per data element of data in read or
586 * write call.
587 * @mode: IR mode as defined by &enum v4l2_subdev_ir_mode.
588 * @enable: device is active if true
589 * @interrupt_enable: IR interrupts are enabled if true
590 * @shutdown: if true: set hardware to low/no power, false: normal mode
591 *
592 * @modulation: if true, it uses carrier, if false: baseband
593 * @max_pulse_width: maximum pulse width in ns, valid only for baseband signal
594 * @carrier_freq: carrier frequency in Hz, valid only for modulated signal
595 * @duty_cycle: duty cycle percentage, valid only for modulated signal
596 * @invert_level: invert signal level
597 *
598 * @invert_carrier_sense: Send 0/space as a carrier burst. used only in TX.
599 *
600 * @noise_filter_min_width: min time of a valid pulse, in ns. Used only for RX.
601 * @carrier_range_lower: Lower carrier range, in Hz, valid only for modulated
602 * signal. Used only for RX.
603 * @carrier_range_upper: Upper carrier range, in Hz, valid only for modulated
604 * signal. Used only for RX.
605 * @resolution: The receive resolution, in ns . Used only for RX.
606 */
1d986add 607struct v4l2_subdev_ir_parameters {
21c29de1 608 unsigned int bytes_per_data_element;
1d986add
AW
609 enum v4l2_subdev_ir_mode mode;
610
611 bool enable;
612 bool interrupt_enable;
21c29de1 613 bool shutdown;
1d986add 614
21c29de1
MCC
615 bool modulation;
616 u32 max_pulse_width;
617 unsigned int carrier_freq;
618 unsigned int duty_cycle;
619 bool invert_level;
5a28d9a3
AW
620
621 /* Tx only */
21c29de1 622 bool invert_carrier_sense;
1d986add
AW
623
624 /* Rx only */
21c29de1
MCC
625 u32 noise_filter_min_width;
626 unsigned int carrier_range_lower;
627 unsigned int carrier_range_upper;
628 u32 resolution;
1d986add
AW
629};
630
21c29de1
MCC
631/**
632 * struct v4l2_subdev_ir_ops - operations for IR subdevices
633 *
634 * @rx_read: Reads received codes or pulse width data.
635 * The semantics are similar to a non-blocking read() call.
f2f475e4 636 * @rx_g_parameters: Get the current operating parameters and state of
21c29de1 637 * the IR receiver.
f2f475e4 638 * @rx_s_parameters: Set the current operating parameters and state of
21c29de1
MCC
639 * the IR receiver. It is recommended to call
640 * [rt]x_g_parameters first to fill out the current state, and only change
641 * the fields that need to be changed. Upon return, the actual device
642 * operating parameters and state will be returned. Note that hardware
643 * limitations may prevent the actual settings from matching the requested
644 * settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
645 * was requested. An exception is when the shutdown parameter is true.
646 * The last used operational parameters will be returned, but the actual
647 * state of the hardware be different to minimize power consumption and
648 * processing when shutdown is true.
649 *
650 * @tx_write: Writes codes or pulse width data for transmission.
651 * The semantics are similar to a non-blocking write() call.
f2f475e4 652 * @tx_g_parameters: Get the current operating parameters and state of
21c29de1 653 * the IR transmitter.
f2f475e4 654 * @tx_s_parameters: Set the current operating parameters and state of
21c29de1
MCC
655 * the IR transmitter. It is recommended to call
656 * [rt]x_g_parameters first to fill out the current state, and only change
657 * the fields that need to be changed. Upon return, the actual device
658 * operating parameters and state will be returned. Note that hardware
659 * limitations may prevent the actual settings from matching the requested
660 * settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
661 * was requested. An exception is when the shutdown parameter is true.
662 * The last used operational parameters will be returned, but the actual
663 * state of the hardware be different to minimize power consumption and
664 * processing when shutdown is true.
665 */
1d986add 666struct v4l2_subdev_ir_ops {
1d986add
AW
667 /* Receiver */
668 int (*rx_read)(struct v4l2_subdev *sd, u8 *buf, size_t count,
669 ssize_t *num);
670
671 int (*rx_g_parameters)(struct v4l2_subdev *sd,
672 struct v4l2_subdev_ir_parameters *params);
673 int (*rx_s_parameters)(struct v4l2_subdev *sd,
674 struct v4l2_subdev_ir_parameters *params);
675
676 /* Transmitter */
677 int (*tx_write)(struct v4l2_subdev *sd, u8 *buf, size_t count,
678 ssize_t *num);
679
680 int (*tx_g_parameters)(struct v4l2_subdev *sd,
681 struct v4l2_subdev_ir_parameters *params);
682 int (*tx_s_parameters)(struct v4l2_subdev *sd,
683 struct v4l2_subdev_ir_parameters *params);
684};
685
21c29de1
MCC
686/**
687 * struct v4l2_subdev_pad_config - Used for storing subdev pad information.
688 *
1059b687
BS
689 * @try_fmt: &struct v4l2_mbus_framefmt
690 * @try_crop: &struct v4l2_rect to be used for crop
691 * @try_compose: &struct v4l2_rect to be used for compose
21c29de1
MCC
692 *
693 * This structure only needs to be passed to the pad op if the 'which' field
694 * of the main argument is set to %V4L2_SUBDEV_FORMAT_TRY. For
695 * %V4L2_SUBDEV_FORMAT_ACTIVE it is safe to pass %NULL.
f69952a4
TV
696 *
697 * Note: This struct is also used in active state, and the 'try' prefix is
698 * historical and to be removed.
f7234138
HV
699 */
700struct v4l2_subdev_pad_config {
701 struct v4l2_mbus_framefmt try_fmt;
702 struct v4l2_rect try_crop;
703 struct v4l2_rect try_compose;
704};
705
2f91e10e
TV
706/**
707 * struct v4l2_subdev_stream_config - Used for storing stream configuration.
708 *
709 * @pad: pad number
710 * @stream: stream number
d0749adb 711 * @enabled: has the stream been enabled with v4l2_subdev_enable_stream()
2f91e10e
TV
712 * @fmt: &struct v4l2_mbus_framefmt
713 * @crop: &struct v4l2_rect to be used for crop
714 * @compose: &struct v4l2_rect to be used for compose
715 *
716 * This structure stores configuration for a stream.
717 */
718struct v4l2_subdev_stream_config {
719 u32 pad;
720 u32 stream;
d0749adb 721 bool enabled;
2f91e10e
TV
722
723 struct v4l2_mbus_framefmt fmt;
724 struct v4l2_rect crop;
725 struct v4l2_rect compose;
726};
727
728/**
729 * struct v4l2_subdev_stream_configs - A collection of stream configs.
730 *
731 * @num_configs: number of entries in @config.
732 * @configs: an array of &struct v4l2_subdev_stream_configs.
733 */
734struct v4l2_subdev_stream_configs {
735 u32 num_configs;
736 struct v4l2_subdev_stream_config *configs;
737};
738
a418bb3f
LP
739/**
740 * struct v4l2_subdev_krouting - subdev routing table
741 *
742 * @num_routes: number of routes
743 * @routes: &struct v4l2_subdev_route
744 *
745 * This structure contains the routing table for a subdev.
746 */
747struct v4l2_subdev_krouting {
748 unsigned int num_routes;
749 struct v4l2_subdev_route *routes;
750};
751
0d346d2a
TV
752/**
753 * struct v4l2_subdev_state - Used for storing subdev state information.
754 *
ed647ea6
TV
755 * @_lock: default for 'lock'
756 * @lock: mutex for the state. May be replaced by the user.
0d346d2a 757 * @pads: &struct v4l2_subdev_pad_config array
a418bb3f 758 * @routing: routing table for the subdev
2f91e10e 759 * @stream_configs: stream configurations (only for V4L2_SUBDEV_FL_STREAMS)
0d346d2a
TV
760 *
761 * This structure only needs to be passed to the pad op if the 'which' field
762 * of the main argument is set to %V4L2_SUBDEV_FORMAT_TRY. For
763 * %V4L2_SUBDEV_FORMAT_ACTIVE it is safe to pass %NULL.
764 */
765struct v4l2_subdev_state {
ed647ea6
TV
766 /* lock for the struct v4l2_subdev_state fields */
767 struct mutex _lock;
768 struct mutex *lock;
0d346d2a 769 struct v4l2_subdev_pad_config *pads;
a418bb3f 770 struct v4l2_subdev_krouting routing;
2f91e10e 771 struct v4l2_subdev_stream_configs stream_configs;
0d346d2a
TV
772};
773
29103119
SN
774/**
775 * struct v4l2_subdev_pad_ops - v4l2-subdev pad level operations
e44cc20a 776 *
9b02cbb3 777 * @init_cfg: initialize the pad config to default values
3fb558f6 778 * @enum_mbus_code: callback for VIDIOC_SUBDEV_ENUM_MBUS_CODE() ioctl handler
e44cc20a 779 * code.
3fb558f6 780 * @enum_frame_size: callback for VIDIOC_SUBDEV_ENUM_FRAME_SIZE() ioctl handler
e44cc20a
MCC
781 * code.
782 *
3fb558f6 783 * @enum_frame_interval: callback for VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL() ioctl
e44cc20a
MCC
784 * handler code.
785 *
3fb558f6 786 * @get_fmt: callback for VIDIOC_SUBDEV_G_FMT() ioctl handler code.
e44cc20a 787 *
3fb558f6 788 * @set_fmt: callback for VIDIOC_SUBDEV_S_FMT() ioctl handler code.
e44cc20a 789 *
3fb558f6 790 * @get_selection: callback for VIDIOC_SUBDEV_G_SELECTION() ioctl handler code.
e44cc20a 791 *
3fb558f6 792 * @set_selection: callback for VIDIOC_SUBDEV_S_SELECTION() ioctl handler code.
e44cc20a 793 *
3fb558f6 794 * @get_edid: callback for VIDIOC_SUBDEV_G_EDID() ioctl handler code.
e44cc20a 795 *
3fb558f6 796 * @set_edid: callback for VIDIOC_SUBDEV_S_EDID() ioctl handler code.
e44cc20a 797 *
3fb558f6 798 * @dv_timings_cap: callback for VIDIOC_SUBDEV_DV_TIMINGS_CAP() ioctl handler
e44cc20a
MCC
799 * code.
800 *
3fb558f6 801 * @enum_dv_timings: callback for VIDIOC_SUBDEV_ENUM_DV_TIMINGS() ioctl handler
e44cc20a
MCC
802 * code.
803 *
1d8955b2
MCC
804 * @link_validate: used by the media controller code to check if the links
805 * that belongs to a pipeline can be used for stream.
806 *
29103119 807 * @get_frame_desc: get the current low level media bus frame parameters.
e44cc20a 808 *
1d8955b2 809 * @set_frame_desc: set the low level media bus frame parameters, @fd array
29103119 810 * may be adjusted by the subdev driver to device capabilities.
38df0b85
JM
811 *
812 * @get_mbus_config: get the media bus configuration of a remote sub-device.
813 * The media bus configuration is usually retrieved from the
814 * firmware interface at sub-device probe time, immediately
815 * applied to the hardware and eventually adjusted by the
816 * driver. Remote sub-devices (usually video receivers) shall
817 * use this operation to query the transmitting end bus
818 * configuration in order to adjust their own one accordingly.
819 * Callers should make sure they get the most up-to-date as
820 * possible configuration from the remote end, likely calling
821 * this operation as close as possible to stream on time. The
822 * operation shall fail if the pad index it has been called on
823 * is not valid or in case of unrecoverable failures.
a418bb3f
LP
824 *
825 * @set_routing: enable or disable data connection routes described in the
826 * subdevice routing table.
d0749adb
LP
827 *
828 * @enable_streams: Enable the streams defined in streams_mask on the given
829 * source pad. Subdevs that implement this operation must use the active
830 * state management provided by the subdev core (enabled through a call to
831 * v4l2_subdev_init_finalize() at initialization time). Do not call
832 * directly, use v4l2_subdev_enable_streams() instead.
833 *
834 * @disable_streams: Disable the streams defined in streams_mask on the given
835 * source pad. Subdevs that implement this operation must use the active
836 * state management provided by the subdev core (enabled through a call to
837 * v4l2_subdev_init_finalize() at initialization time). Do not call
838 * directly, use v4l2_subdev_disable_streams() instead.
29103119 839 */
eb08f354 840struct v4l2_subdev_pad_ops {
9b02cbb3 841 int (*init_cfg)(struct v4l2_subdev *sd,
0d346d2a 842 struct v4l2_subdev_state *state);
f7234138 843 int (*enum_mbus_code)(struct v4l2_subdev *sd,
0d346d2a 844 struct v4l2_subdev_state *state,
333c8b97
LP
845 struct v4l2_subdev_mbus_code_enum *code);
846 int (*enum_frame_size)(struct v4l2_subdev *sd,
0d346d2a 847 struct v4l2_subdev_state *state,
333c8b97 848 struct v4l2_subdev_frame_size_enum *fse);
35c3017a 849 int (*enum_frame_interval)(struct v4l2_subdev *sd,
0d346d2a 850 struct v4l2_subdev_state *state,
35c3017a 851 struct v4l2_subdev_frame_interval_enum *fie);
f7234138 852 int (*get_fmt)(struct v4l2_subdev *sd,
0d346d2a 853 struct v4l2_subdev_state *state,
333c8b97 854 struct v4l2_subdev_format *format);
f7234138 855 int (*set_fmt)(struct v4l2_subdev *sd,
0d346d2a 856 struct v4l2_subdev_state *state,
333c8b97 857 struct v4l2_subdev_format *format);
f7234138 858 int (*get_selection)(struct v4l2_subdev *sd,
0d346d2a 859 struct v4l2_subdev_state *state,
ae184cda 860 struct v4l2_subdev_selection *sel);
f7234138 861 int (*set_selection)(struct v4l2_subdev *sd,
0d346d2a 862 struct v4l2_subdev_state *state,
ae184cda 863 struct v4l2_subdev_selection *sel);
dd519bb3
HV
864 int (*get_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
865 int (*set_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
0349f6a5
LP
866 int (*dv_timings_cap)(struct v4l2_subdev *sd,
867 struct v4l2_dv_timings_cap *cap);
868 int (*enum_dv_timings)(struct v4l2_subdev *sd,
869 struct v4l2_enum_dv_timings *timings);
8227c92b
SA
870#ifdef CONFIG_MEDIA_CONTROLLER
871 int (*link_validate)(struct v4l2_subdev *sd, struct media_link *link,
872 struct v4l2_subdev_format *source_fmt,
873 struct v4l2_subdev_format *sink_fmt);
874#endif /* CONFIG_MEDIA_CONTROLLER */
29103119
SN
875 int (*get_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
876 struct v4l2_mbus_frame_desc *fd);
877 int (*set_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
878 struct v4l2_mbus_frame_desc *fd);
38df0b85
JM
879 int (*get_mbus_config)(struct v4l2_subdev *sd, unsigned int pad,
880 struct v4l2_mbus_config *config);
a418bb3f
LP
881 int (*set_routing)(struct v4l2_subdev *sd,
882 struct v4l2_subdev_state *state,
883 enum v4l2_subdev_format_whence which,
884 struct v4l2_subdev_krouting *route);
d0749adb
LP
885 int (*enable_streams)(struct v4l2_subdev *sd,
886 struct v4l2_subdev_state *state, u32 pad,
887 u64 streams_mask);
888 int (*disable_streams)(struct v4l2_subdev *sd,
889 struct v4l2_subdev_state *state, u32 pad,
890 u64 streams_mask);
eb08f354
LP
891};
892
21c29de1
MCC
893/**
894 * struct v4l2_subdev_ops - Subdev operations
895 *
896 * @core: pointer to &struct v4l2_subdev_core_ops. Can be %NULL
897 * @tuner: pointer to &struct v4l2_subdev_tuner_ops. Can be %NULL
898 * @audio: pointer to &struct v4l2_subdev_audio_ops. Can be %NULL
899 * @video: pointer to &struct v4l2_subdev_video_ops. Can be %NULL
900 * @vbi: pointer to &struct v4l2_subdev_vbi_ops. Can be %NULL
901 * @ir: pointer to &struct v4l2_subdev_ir_ops. Can be %NULL
902 * @sensor: pointer to &struct v4l2_subdev_sensor_ops. Can be %NULL
903 * @pad: pointer to &struct v4l2_subdev_pad_ops. Can be %NULL
904 */
2a1fcdf0 905struct v4l2_subdev_ops {
32536108
GL
906 const struct v4l2_subdev_core_ops *core;
907 const struct v4l2_subdev_tuner_ops *tuner;
908 const struct v4l2_subdev_audio_ops *audio;
909 const struct v4l2_subdev_video_ops *video;
32cd527f 910 const struct v4l2_subdev_vbi_ops *vbi;
32536108
GL
911 const struct v4l2_subdev_ir_ops *ir;
912 const struct v4l2_subdev_sensor_ops *sensor;
eb08f354 913 const struct v4l2_subdev_pad_ops *pad;
2a1fcdf0
HV
914};
915
21c29de1
MCC
916/**
917 * struct v4l2_subdev_internal_ops - V4L2 subdev internal ops
45f6f84a 918 *
21c29de1 919 * @registered: called when this subdev is registered. When called the v4l2_dev
45f6f84a
HV
920 * field is set to the correct v4l2_device.
921 *
21c29de1 922 * @unregistered: called when this subdev is unregistered. When called the
45f6f84a 923 * v4l2_dev field is still set to the correct v4l2_device.
f0beea8f 924 *
21c29de1 925 * @open: called when the subdev device node is opened by an application.
f0beea8f 926 *
0e43734d
HV
927 * @close: called when the subdev device node is closed. Please note that
928 * it is possible for @close to be called after @unregistered!
929 *
930 * @release: called when the last user of the subdev device is gone. This
931 * happens after the @unregistered callback and when the last open
932 * filehandle to the v4l-subdevX device node was closed. If no device
933 * node was created for this sub-device, then the @release callback
934 * is called right after the @unregistered callback.
935 * The @release callback is typically used to free the memory containing
936 * the v4l2_subdev structure. It is almost certainly required for any
937 * sub-device that sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag.
21c29de1
MCC
938 *
939 * .. note::
940 * Never call this from drivers, only the v4l2 framework can call
941 * these ops.
45f6f84a
HV
942 */
943struct v4l2_subdev_internal_ops {
944 int (*registered)(struct v4l2_subdev *sd);
945 void (*unregistered)(struct v4l2_subdev *sd);
f0beea8f
LP
946 int (*open)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
947 int (*close)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
0e43734d 948 void (*release)(struct v4l2_subdev *sd);
45f6f84a
HV
949};
950
2a1fcdf0
HV
951#define V4L2_SUBDEV_NAME_SIZE 32
952
b5b2b7ed 953/* Set this flag if this subdev is a i2c device. */
2096a5dc 954#define V4L2_SUBDEV_FL_IS_I2C (1U << 0)
85e09219 955/* Set this flag if this subdev is a spi device. */
2096a5dc
LP
956#define V4L2_SUBDEV_FL_IS_SPI (1U << 1)
957/* Set this flag if this subdev needs a device node. */
958#define V4L2_SUBDEV_FL_HAS_DEVNODE (1U << 2)
d138091f
LC
959/*
960 * Set this flag if this subdev generates events.
961 * Note controls can send events, thus drivers exposing controls
962 * should set this flag.
963 */
02adb1cc 964#define V4L2_SUBDEV_FL_HAS_EVENTS (1U << 3)
d6cc9c7c
TV
965/*
966 * Set this flag if this subdev supports multiplexed streams. This means
967 * that the driver supports routing and handles the stream parameter in its
968 * v4l2_subdev_pad_ops handlers. More specifically, this means:
969 *
970 * - Centrally managed subdev active state is enabled
971 * - Legacy pad config is _not_ supported (state->pads is NULL)
972 * - Routing ioctls are available
973 * - Multiple streams per pad are supported
974 */
975#define V4L2_SUBDEV_FL_STREAMS (1U << 4)
b5b2b7ed 976
b1cbab05
GL
977struct regulator_bulk_data;
978
21c29de1
MCC
979/**
980 * struct v4l2_subdev_platform_data - regulators config struct
981 *
982 * @regulators: Optional regulators used to power on/off the subdevice
983 * @num_regulators: Number of regululators
984 * @host_priv: Per-subdevice data, specific for a certain video host device
985 */
b1cbab05 986struct v4l2_subdev_platform_data {
b1cbab05
GL
987 struct regulator_bulk_data *regulators;
988 int num_regulators;
989
b1cbab05
GL
990 void *host_priv;
991};
992
21c29de1
MCC
993/**
994 * struct v4l2_subdev - describes a V4L2 sub-device
995 *
996 * @entity: pointer to &struct media_entity
997 * @list: List of sub-devices
998 * @owner: The owner is the same as the driver's &struct device owner.
cb8d67cf 999 * @owner_v4l2_dev: true if the &sd->owner matches the owner of @v4l2_dev->dev
0722ef82 1000 * owner. Initialized by v4l2_device_register_subdev().
21c29de1
MCC
1001 * @flags: subdev flags. Can be:
1002 * %V4L2_SUBDEV_FL_IS_I2C - Set this flag if this subdev is a i2c device;
1003 * %V4L2_SUBDEV_FL_IS_SPI - Set this flag if this subdev is a spi device;
1004 * %V4L2_SUBDEV_FL_HAS_DEVNODE - Set this flag if this subdev needs a
1005 * device node;
1006 * %V4L2_SUBDEV_FL_HAS_EVENTS - Set this flag if this subdev generates
1007 * events.
1008 *
cb8d67cf
MCC
1009 * @v4l2_dev: pointer to struct &v4l2_device
1010 * @ops: pointer to struct &v4l2_subdev_ops
1011 * @internal_ops: pointer to struct &v4l2_subdev_internal_ops.
21c29de1
MCC
1012 * Never call these internal ops from within a driver!
1013 * @ctrl_handler: The control handler of this subdev. May be NULL.
1014 * @name: Name of the sub-device. Please notice that the name must be unique.
1015 * @grp_id: can be used to group similar subdevs. Value is driver-specific
1016 * @dev_priv: pointer to private data
1017 * @host_priv: pointer to private data used by the device where the subdev
1018 * is attached.
1019 * @devnode: subdev device node
1020 * @dev: pointer to the physical device, if any
ecdf0cfe
SA
1021 * @fwnode: The fwnode_handle of the subdev, usually the same as
1022 * either dev->of_node->fwnode or dev->fwnode (whichever is non-NULL).
21c29de1
MCC
1023 * @async_list: Links this subdev to a global subdev_list or @notifier->done
1024 * list.
1025 * @asd: Pointer to respective &struct v4l2_async_subdev.
1026 * @notifier: Pointer to the managing notifier.
aef69d54 1027 * @subdev_notifier: A sub-device notifier implicitly registered for the sub-
c3609c45 1028 * device using v4l2_async_register_subdev_sensor().
21c29de1 1029 * @pdata: common part of subdevice platform data
ed647ea6
TV
1030 * @state_lock: A pointer to a lock used for all the subdev's states, set by the
1031 * driver. This is optional. If NULL, each state instance will get
1032 * a lock of its own.
b6e10ff6 1033 * @privacy_led: Optional pointer to a LED classdev for the privacy LED for sensors.
f69952a4
TV
1034 * @active_state: Active state for the subdev (NULL for subdevs tracking the
1035 * state internally). Initialized by calling
1036 * v4l2_subdev_init_finalize().
d0749adb
LP
1037 * @enabled_streams: Bitmask of enabled streams used by
1038 * v4l2_subdev_enable_streams() and
1039 * v4l2_subdev_disable_streams() helper functions for fallback
1040 * cases.
21c29de1
MCC
1041 *
1042 * Each instance of a subdev driver should create this struct, either
1043 * stand-alone or embedded in a larger struct.
1044 *
1045 * This structure should be initialized by v4l2_subdev_init() or one of
1046 * its variants: v4l2_spi_subdev_init(), v4l2_i2c_subdev_init().
2a1fcdf0
HV
1047 */
1048struct v4l2_subdev {
61f5db54
LP
1049#if defined(CONFIG_MEDIA_CONTROLLER)
1050 struct media_entity entity;
1051#endif
2a1fcdf0
HV
1052 struct list_head list;
1053 struct module *owner;
b2a06aec 1054 bool owner_v4l2_dev;
b5b2b7ed 1055 u32 flags;
b0167600 1056 struct v4l2_device *v4l2_dev;
2a1fcdf0 1057 const struct v4l2_subdev_ops *ops;
45f6f84a 1058 const struct v4l2_subdev_internal_ops *internal_ops;
0996517c 1059 struct v4l2_ctrl_handler *ctrl_handler;
2a1fcdf0 1060 char name[V4L2_SUBDEV_NAME_SIZE];
2a1fcdf0 1061 u32 grp_id;
692d5522
LP
1062 void *dev_priv;
1063 void *host_priv;
3e0ec41c 1064 struct video_device *devnode;
668773b8 1065 struct device *dev;
ecdf0cfe 1066 struct fwnode_handle *fwnode;
b426b3a6 1067 struct list_head async_list;
b426b3a6 1068 struct v4l2_async_subdev *asd;
b426b3a6 1069 struct v4l2_async_notifier *notifier;
aef69d54 1070 struct v4l2_async_notifier *subdev_notifier;
b1cbab05 1071 struct v4l2_subdev_platform_data *pdata;
ed647ea6 1072 struct mutex *state_lock;
f69952a4
TV
1073
1074 /*
1075 * The fields below are private, and should only be accessed via
1076 * appropriate functions.
1077 */
1078
b6e10ff6
HG
1079 struct led_classdev *privacy_led;
1080
f69952a4
TV
1081 /*
1082 * TODO: active_state should most likely be changed from a pointer to an
1083 * embedded field. For the time being it's kept as a pointer to more
1084 * easily catch uses of active_state in the cases where the driver
1085 * doesn't support it.
1086 */
1087 struct v4l2_subdev_state *active_state;
d0749adb 1088 u64 enabled_streams;
2a1fcdf0
HV
1089};
1090
02679876
MCC
1091
1092/**
1093 * media_entity_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1094 * the &struct media_entity embedded in it.
1095 *
1096 * @ent: pointer to &struct media_entity.
1097 */
47f910f0
KB
1098#define media_entity_to_v4l2_subdev(ent) \
1099({ \
1100 typeof(ent) __me_sd_ent = (ent); \
1101 \
1102 __me_sd_ent ? \
1103 container_of(__me_sd_ent, struct v4l2_subdev, entity) : \
1104 NULL; \
1105})
1106
02679876
MCC
1107/**
1108 * vdev_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1109 * the &struct video_device embedded on it.
1110 *
1111 * @vdev: pointer to &struct video_device
1112 */
2096a5dc 1113#define vdev_to_v4l2_subdev(vdev) \
c5a766ce 1114 ((struct v4l2_subdev *)video_get_drvdata(vdev))
2096a5dc 1115
21c29de1
MCC
1116/**
1117 * struct v4l2_subdev_fh - Used for storing subdev information per file handle
1118 *
02679876 1119 * @vfh: pointer to &struct v4l2_fh
0d346d2a 1120 * @state: pointer to &struct v4l2_subdev_state
218bf10e 1121 * @owner: module pointer to the owner of this file handle
7cd5a16b
SV
1122 */
1123struct v4l2_subdev_fh {
1124 struct v4l2_fh vfh;
218bf10e 1125 struct module *owner;
7cd5a16b 1126#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
0d346d2a 1127 struct v4l2_subdev_state *state;
f57fa295 1128 u64 client_caps;
7cd5a16b
SV
1129#endif
1130};
1131
02679876
MCC
1132/**
1133 * to_v4l2_subdev_fh - Returns a &struct v4l2_subdev_fh from
1134 * the &struct v4l2_fh embedded on it.
1135 *
1136 * @fh: pointer to &struct v4l2_fh
1137 */
7cd5a16b
SV
1138#define to_v4l2_subdev_fh(fh) \
1139 container_of(fh, struct v4l2_subdev_fh, vfh)
1140
1141#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
02679876
MCC
1142
1143/**
2f91838c 1144 * v4l2_subdev_get_pad_format - ancillary routine to call
02679876
MCC
1145 * &struct v4l2_subdev_pad_config->try_fmt
1146 *
1147 * @sd: pointer to &struct v4l2_subdev
0d346d2a
TV
1148 * @state: pointer to &struct v4l2_subdev_state
1149 * @pad: index of the pad in the &struct v4l2_subdev_state->pads array
02679876 1150 */
8ecbde62 1151static inline struct v4l2_mbus_framefmt *
2f91838c 1152v4l2_subdev_get_pad_format(struct v4l2_subdev *sd,
0d346d2a 1153 struct v4l2_subdev_state *state,
8ecbde62 1154 unsigned int pad)
ab9bb73a 1155{
2ba3e385
SA
1156 if (WARN_ON(!state))
1157 return NULL;
ab9bb73a
MCC
1158 if (WARN_ON(pad >= sd->entity.num_pads))
1159 pad = 0;
0d346d2a 1160 return &state->pads[pad].try_fmt;
ab9bb73a
MCC
1161}
1162
02679876 1163/**
2f91838c 1164 * v4l2_subdev_get_pad_crop - ancillary routine to call
02679876
MCC
1165 * &struct v4l2_subdev_pad_config->try_crop
1166 *
1167 * @sd: pointer to &struct v4l2_subdev
0d346d2a
TV
1168 * @state: pointer to &struct v4l2_subdev_state.
1169 * @pad: index of the pad in the &struct v4l2_subdev_state->pads array.
02679876 1170 */
8ecbde62 1171static inline struct v4l2_rect *
2f91838c 1172v4l2_subdev_get_pad_crop(struct v4l2_subdev *sd,
0d346d2a 1173 struct v4l2_subdev_state *state,
8ecbde62 1174 unsigned int pad)
ab9bb73a 1175{
2ba3e385
SA
1176 if (WARN_ON(!state))
1177 return NULL;
ab9bb73a
MCC
1178 if (WARN_ON(pad >= sd->entity.num_pads))
1179 pad = 0;
0d346d2a 1180 return &state->pads[pad].try_crop;
ab9bb73a
MCC
1181}
1182
02679876 1183/**
2f91838c 1184 * v4l2_subdev_get_pad_compose - ancillary routine to call
02679876
MCC
1185 * &struct v4l2_subdev_pad_config->try_compose
1186 *
1187 * @sd: pointer to &struct v4l2_subdev
0d346d2a
TV
1188 * @state: pointer to &struct v4l2_subdev_state.
1189 * @pad: index of the pad in the &struct v4l2_subdev_state->pads array.
02679876 1190 */
8ecbde62 1191static inline struct v4l2_rect *
2f91838c 1192v4l2_subdev_get_pad_compose(struct v4l2_subdev *sd,
0d346d2a 1193 struct v4l2_subdev_state *state,
8ecbde62 1194 unsigned int pad)
ab9bb73a 1195{
2ba3e385
SA
1196 if (WARN_ON(!state))
1197 return NULL;
ab9bb73a
MCC
1198 if (WARN_ON(pad >= sd->entity.num_pads))
1199 pad = 0;
0d346d2a 1200 return &state->pads[pad].try_compose;
ab9bb73a 1201}
8ecbde62 1202
2f91838c
TV
1203/*
1204 * Temprary helpers until uses of v4l2_subdev_get_try_* functions have been
1205 * renamed
1206 */
1207#define v4l2_subdev_get_try_format(sd, state, pad) \
1208 v4l2_subdev_get_pad_format(sd, state, pad)
1209
1210#define v4l2_subdev_get_try_crop(sd, state, pad) \
1211 v4l2_subdev_get_pad_crop(sd, state, pad)
1212
1213#define v4l2_subdev_get_try_compose(sd, state, pad) \
1214 v4l2_subdev_get_pad_compose(sd, state, pad)
1215
dadd47d4 1216#endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
7cd5a16b 1217
2096a5dc
LP
1218extern const struct v4l2_file_operations v4l2_subdev_fops;
1219
21c29de1
MCC
1220/**
1221 * v4l2_set_subdevdata - Sets V4L2 dev private device data
1222 *
1223 * @sd: pointer to &struct v4l2_subdev
1224 * @p: pointer to the private device data to be stored.
1225 */
2a1fcdf0
HV
1226static inline void v4l2_set_subdevdata(struct v4l2_subdev *sd, void *p)
1227{
692d5522 1228 sd->dev_priv = p;
2a1fcdf0
HV
1229}
1230
21c29de1
MCC
1231/**
1232 * v4l2_get_subdevdata - Gets V4L2 dev private device data
1233 *
1234 * @sd: pointer to &struct v4l2_subdev
1235 *
1236 * Returns the pointer to the private device data to be stored.
1237 */
2a1fcdf0
HV
1238static inline void *v4l2_get_subdevdata(const struct v4l2_subdev *sd)
1239{
692d5522
LP
1240 return sd->dev_priv;
1241}
1242
21c29de1 1243/**
e383ce07 1244 * v4l2_set_subdev_hostdata - Sets V4L2 dev private host data
21c29de1
MCC
1245 *
1246 * @sd: pointer to &struct v4l2_subdev
1247 * @p: pointer to the private data to be stored.
1248 */
692d5522
LP
1249static inline void v4l2_set_subdev_hostdata(struct v4l2_subdev *sd, void *p)
1250{
1251 sd->host_priv = p;
1252}
1253
21c29de1 1254/**
e383ce07 1255 * v4l2_get_subdev_hostdata - Gets V4L2 dev private data
21c29de1
MCC
1256 *
1257 * @sd: pointer to &struct v4l2_subdev
1258 *
1259 * Returns the pointer to the private host data to be stored.
1260 */
692d5522
LP
1261static inline void *v4l2_get_subdev_hostdata(const struct v4l2_subdev *sd)
1262{
1263 return sd->host_priv;
2a1fcdf0
HV
1264}
1265
8227c92b 1266#ifdef CONFIG_MEDIA_CONTROLLER
21c29de1 1267
8fe784b9
SL
1268/**
1269 * v4l2_subdev_get_fwnode_pad_1_to_1 - Get pad number from a subdev fwnode
1270 * endpoint, assuming 1:1 port:pad
1271 *
2f501169
CL
1272 * @entity: Pointer to the subdev entity
1273 * @endpoint: Pointer to a parsed fwnode endpoint
8fe784b9
SL
1274 *
1275 * This function can be used as the .get_fwnode_pad operation for
1276 * subdevices that map port numbers and pad indexes 1:1. If the endpoint
1277 * is owned by the subdevice, the function returns the endpoint port
1278 * number.
1279 *
1280 * Returns the endpoint port number on success or a negative error code.
1281 */
1282int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1283 struct fwnode_endpoint *endpoint);
1284
21c29de1
MCC
1285/**
1286 * v4l2_subdev_link_validate_default - validates a media link
1287 *
1288 * @sd: pointer to &struct v4l2_subdev
1289 * @link: pointer to &struct media_link
1290 * @source_fmt: pointer to &struct v4l2_subdev_format
1291 * @sink_fmt: pointer to &struct v4l2_subdev_format
1292 *
1293 * This function ensures that width, height and the media bus pixel
1294 * code are equal on both source and sink of the link.
1295 */
8227c92b
SA
1296int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1297 struct media_link *link,
1298 struct v4l2_subdev_format *source_fmt,
1299 struct v4l2_subdev_format *sink_fmt);
21c29de1
MCC
1300
1301/**
1302 * v4l2_subdev_link_validate - validates a media link
1303 *
1304 * @link: pointer to &struct media_link
1305 *
1306 * This function calls the subdev's link_validate ops to validate
1307 * if a media link is valid for streaming. It also internally
1308 * calls v4l2_subdev_link_validate_default() to ensure that
1309 * width, height and the media bus pixel code are equal on both
1310 * source and sink of the link.
1311 */
8227c92b 1312int v4l2_subdev_link_validate(struct media_link *link);
9b02cbb3 1313
33c0ddbe
TV
1314/**
1315 * v4l2_subdev_has_pad_interdep - MC has_pad_interdep implementation for subdevs
1316 *
1317 * @entity: pointer to &struct media_entity
1318 * @pad0: pad number for the first pad
1319 * @pad1: pad number for the second pad
1320 *
1321 * This function is an implementation of the
1322 * media_entity_operations.has_pad_interdep operation for subdevs that
1323 * implement the multiplexed streams API (as indicated by the
1324 * V4L2_SUBDEV_FL_STREAMS subdev flag).
1325 *
1326 * It considers two pads interdependent if there is an active route between pad0
1327 * and pad1.
1328 */
1329bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1330 unsigned int pad0, unsigned int pad1);
1331
21c29de1 1332/**
40aaab9d 1333 * __v4l2_subdev_state_alloc - allocate v4l2_subdev_state
21c29de1 1334 *
0d346d2a 1335 * @sd: pointer to &struct v4l2_subdev for which the state is being allocated.
ed647ea6
TV
1336 * @lock_name: name of the state lock
1337 * @key: lock_class_key for the lock
0d346d2a 1338 *
40aaab9d
TV
1339 * Must call __v4l2_subdev_state_free() when state is no longer needed.
1340 *
1341 * Not to be called directly by the drivers.
21c29de1 1342 */
ed647ea6
TV
1343struct v4l2_subdev_state *__v4l2_subdev_state_alloc(struct v4l2_subdev *sd,
1344 const char *lock_name,
1345 struct lock_class_key *key);
21c29de1
MCC
1346
1347/**
40aaab9d 1348 * __v4l2_subdev_state_free - free a v4l2_subdev_state
21c29de1 1349 *
0d346d2a 1350 * @state: v4l2_subdev_state to be freed.
40aaab9d
TV
1351 *
1352 * Not to be called directly by the drivers.
21c29de1 1353 */
40aaab9d 1354void __v4l2_subdev_state_free(struct v4l2_subdev_state *state);
0d346d2a 1355
f69952a4
TV
1356/**
1357 * v4l2_subdev_init_finalize() - Finalizes the initialization of the subdevice
1358 * @sd: The subdev
1359 *
1360 * This function finalizes the initialization of the subdev, including
1361 * allocation of the active state for the subdev.
1362 *
1363 * This function must be called by the subdev drivers that use the centralized
1364 * active state, after the subdev struct has been initialized and
1365 * media_entity_pads_init() has been called, but before registering the
1366 * subdev.
1367 *
1368 * The user must call v4l2_subdev_cleanup() when the subdev is being removed.
1369 */
ed647ea6
TV
1370#define v4l2_subdev_init_finalize(sd) \
1371 ({ \
1372 static struct lock_class_key __key; \
1373 const char *name = KBUILD_BASENAME \
1374 ":" __stringify(__LINE__) ":sd->active_state->lock"; \
1375 __v4l2_subdev_init_finalize(sd, name, &__key); \
1376 })
1377
1378int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1379 struct lock_class_key *key);
f69952a4
TV
1380
1381/**
1382 * v4l2_subdev_cleanup() - Releases the resources allocated by the subdevice
1383 * @sd: The subdevice
1384 *
1385 * This function will release the resources allocated in
1386 * v4l2_subdev_init_finalize.
1387 */
1388void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
1389
1390/**
ed647ea6
TV
1391 * v4l2_subdev_lock_state() - Locks the subdev state
1392 * @state: The subdevice state
1393 *
1394 * Locks the given subdev state.
1395 *
1396 * The state must be unlocked with v4l2_subdev_unlock_state() after use.
1397 */
1398static inline void v4l2_subdev_lock_state(struct v4l2_subdev_state *state)
1399{
1400 mutex_lock(state->lock);
1401}
1402
1403/**
1404 * v4l2_subdev_unlock_state() - Unlocks the subdev state
1405 * @state: The subdevice state
1406 *
1407 * Unlocks the given subdev state.
1408 */
1409static inline void v4l2_subdev_unlock_state(struct v4l2_subdev_state *state)
1410{
1411 mutex_unlock(state->lock);
1412}
1413
1414/**
1415 * v4l2_subdev_get_unlocked_active_state() - Checks that the active subdev state
1416 * is unlocked and returns it
1417 * @sd: The subdevice
1418 *
1419 * Returns the active state for the subdevice, or NULL if the subdev does not
1420 * support active state. If the state is not NULL, calls
1421 * lockdep_assert_not_held() to issue a warning if the state is locked.
1422 *
1423 * This function is to be used e.g. when getting the active state for the sole
1424 * purpose of passing it forward, without accessing the state fields.
1425 */
1426static inline struct v4l2_subdev_state *
1427v4l2_subdev_get_unlocked_active_state(struct v4l2_subdev *sd)
1428{
1429 if (sd->active_state)
1430 lockdep_assert_not_held(sd->active_state->lock);
1431 return sd->active_state;
1432}
1433
1434/**
1435 * v4l2_subdev_get_locked_active_state() - Checks that the active subdev state
1436 * is locked and returns it
1437 *
f69952a4
TV
1438 * @sd: The subdevice
1439 *
1440 * Returns the active state for the subdevice, or NULL if the subdev does not
ed647ea6
TV
1441 * support active state. If the state is not NULL, calls lockdep_assert_held()
1442 * to issue a warning if the state is not locked.
1443 *
1444 * This function is to be used when the caller knows that the active state is
1445 * already locked.
1446 */
1447static inline struct v4l2_subdev_state *
1448v4l2_subdev_get_locked_active_state(struct v4l2_subdev *sd)
1449{
1450 if (sd->active_state)
1451 lockdep_assert_held(sd->active_state->lock);
1452 return sd->active_state;
1453}
1454
1455/**
1456 * v4l2_subdev_lock_and_get_active_state() - Locks and returns the active subdev
1457 * state for the subdevice
1458 * @sd: The subdevice
1459 *
1460 * Returns the locked active state for the subdevice, or NULL if the subdev
1461 * does not support active state.
1462 *
1463 * The state must be unlocked with v4l2_subdev_unlock_state() after use.
f69952a4
TV
1464 */
1465static inline struct v4l2_subdev_state *
ed647ea6 1466v4l2_subdev_lock_and_get_active_state(struct v4l2_subdev *sd)
f69952a4 1467{
ed647ea6
TV
1468 if (sd->active_state)
1469 v4l2_subdev_lock_state(sd->active_state);
f69952a4
TV
1470 return sd->active_state;
1471}
1472
14a6fca7
TV
1473#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1474
1475/**
1476 * v4l2_subdev_get_fmt() - Fill format based on state
1477 * @sd: subdevice
1478 * @state: subdevice state
1479 * @format: pointer to &struct v4l2_subdev_format
1480 *
1481 * Fill @format->format field based on the information in the @format struct.
1482 *
1483 * This function can be used by the subdev drivers which support active state to
1484 * implement v4l2_subdev_pad_ops.get_fmt if the subdev driver does not need to
1485 * do anything special in their get_fmt op.
1486 *
1487 * Returns 0 on success, error value otherwise.
1488 */
1489int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1490 struct v4l2_subdev_format *format);
1491
17bb9bf8
TV
1492/**
1493 * v4l2_subdev_set_routing() - Set given routing to subdev state
1494 * @sd: The subdevice
1495 * @state: The subdevice state
1496 * @routing: Routing that will be copied to subdev state
1497 *
1498 * This will release old routing table (if any) from the state, allocate
1499 * enough space for the given routing, and copy the routing.
1500 *
1501 * This can be used from the subdev driver's set_routing op, after validating
1502 * the routing.
1503 */
1504int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1505 struct v4l2_subdev_state *state,
1506 const struct v4l2_subdev_krouting *routing);
1507
837f92f0
JM
1508struct v4l2_subdev_route *
1509__v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1510 struct v4l2_subdev_route *route);
1511
1512/**
1513 * for_each_active_route - iterate on all active routes of a routing table
1514 * @routing: The routing table
1515 * @route: The route iterator
1516 */
1517#define for_each_active_route(routing, route) \
1518 for ((route) = NULL; \
1519 ((route) = __v4l2_subdev_next_active_route((routing), (route)));)
1520
5b0d85b3
TV
1521/**
1522 * v4l2_subdev_set_routing_with_fmt() - Set given routing and format to subdev
1523 * state
1524 * @sd: The subdevice
1525 * @state: The subdevice state
1526 * @routing: Routing that will be copied to subdev state
1527 * @fmt: Format used to initialize all the streams
1528 *
1529 * This is the same as v4l2_subdev_set_routing, but additionally initializes
1530 * all the streams using the given format.
1531 */
1532int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1533 struct v4l2_subdev_state *state,
1534 struct v4l2_subdev_krouting *routing,
1535 const struct v4l2_mbus_framefmt *fmt);
1536
2f91e10e
TV
1537/**
1538 * v4l2_subdev_state_get_stream_format() - Get pointer to a stream format
1539 * @state: subdevice state
1540 * @pad: pad id
1541 * @stream: stream id
1542 *
1543 * This returns a pointer to &struct v4l2_mbus_framefmt for the given pad +
1544 * stream in the subdev state.
1545 *
1546 * If the state does not contain the given pad + stream, NULL is returned.
1547 */
1548struct v4l2_mbus_framefmt *
1549v4l2_subdev_state_get_stream_format(struct v4l2_subdev_state *state,
1550 unsigned int pad, u32 stream);
1551
1552/**
1553 * v4l2_subdev_state_get_stream_crop() - Get pointer to a stream crop rectangle
1554 * @state: subdevice state
1555 * @pad: pad id
1556 * @stream: stream id
1557 *
1558 * This returns a pointer to crop rectangle for the given pad + stream in the
1559 * subdev state.
1560 *
1561 * If the state does not contain the given pad + stream, NULL is returned.
1562 */
1563struct v4l2_rect *
1564v4l2_subdev_state_get_stream_crop(struct v4l2_subdev_state *state,
1565 unsigned int pad, u32 stream);
1566
1567/**
1568 * v4l2_subdev_state_get_stream_compose() - Get pointer to a stream compose
1569 * rectangle
1570 * @state: subdevice state
1571 * @pad: pad id
1572 * @stream: stream id
1573 *
1574 * This returns a pointer to compose rectangle for the given pad + stream in the
1575 * subdev state.
1576 *
1577 * If the state does not contain the given pad + stream, NULL is returned.
1578 */
1579struct v4l2_rect *
1580v4l2_subdev_state_get_stream_compose(struct v4l2_subdev_state *state,
1581 unsigned int pad, u32 stream);
1582
d00f1a07
TV
1583/**
1584 * v4l2_subdev_routing_find_opposite_end() - Find the opposite stream
1585 * @routing: routing used to find the opposite side
1586 * @pad: pad id
1587 * @stream: stream id
1588 * @other_pad: pointer used to return the opposite pad
1589 * @other_stream: pointer used to return the opposite stream
1590 *
1591 * This function uses the routing table to find the pad + stream which is
1592 * opposite the given pad + stream.
1593 *
1594 * @other_pad and/or @other_stream can be NULL if the caller does not need the
1595 * value.
1596 *
1597 * Returns 0 on success, or -EINVAL if no matching route is found.
1598 */
1599int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1600 u32 pad, u32 stream, u32 *other_pad,
1601 u32 *other_stream);
1602
1603/**
1604 * v4l2_subdev_state_get_opposite_stream_format() - Get pointer to opposite
1605 * stream format
1606 * @state: subdevice state
1607 * @pad: pad id
1608 * @stream: stream id
1609 *
1610 * This returns a pointer to &struct v4l2_mbus_framefmt for the pad + stream
1611 * that is opposite the given pad + stream in the subdev state.
1612 *
1613 * If the state does not contain the given pad + stream, NULL is returned.
1614 */
1615struct v4l2_mbus_framefmt *
1616v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
1617 u32 pad, u32 stream);
1618
c4a73f31
LP
1619/**
1620 * v4l2_subdev_state_xlate_streams() - Translate streams from one pad to another
1621 *
1622 * @state: Subdevice state
1623 * @pad0: The first pad
1624 * @pad1: The second pad
1625 * @streams: Streams bitmask on the first pad
1626 *
1627 * Streams on sink pads of a subdev are routed to source pads as expressed in
1628 * the subdev state routing table. Stream numbers don't necessarily match on
1629 * the sink and source side of a route. This function translates stream numbers
1630 * on @pad0, expressed as a bitmask in @streams, to the corresponding streams
1631 * on @pad1 using the routing table from the @state. It returns the stream mask
1632 * on @pad1, and updates @streams with the streams that have been found in the
1633 * routing table.
1634 *
1635 * @pad0 and @pad1 must be a sink and a source, in any order.
1636 *
1637 * Return: The bitmask of streams of @pad1 that are routed to @streams on @pad0.
1638 */
1639u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
1640 u32 pad0, u32 pad1, u64 *streams);
1641
69c0fe7a
LP
1642/**
1643 * enum v4l2_subdev_routing_restriction - Subdevice internal routing restrictions
1644 *
1645 * @V4L2_SUBDEV_ROUTING_NO_1_TO_N:
a50ee4af 1646 * an input stream shall not be routed to multiple output streams (stream
69c0fe7a
LP
1647 * duplication)
1648 * @V4L2_SUBDEV_ROUTING_NO_N_TO_1:
a50ee4af 1649 * multiple input streams shall not be routed to the same output stream
69c0fe7a 1650 * (stream merging)
698a619a
TV
1651 * @V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX:
1652 * all streams from a sink pad must be routed to a single source pad
1653 * @V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX:
1654 * all streams on a source pad must originate from a single sink pad
a1299df6
TV
1655 * @V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING:
1656 * source pads shall not contain multiplexed streams
1657 * @V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING:
1658 * sink pads shall not contain multiplexed streams
69c0fe7a
LP
1659 * @V4L2_SUBDEV_ROUTING_ONLY_1_TO_1:
1660 * only non-overlapping 1-to-1 stream routing is allowed (a combination of
1661 * @V4L2_SUBDEV_ROUTING_NO_1_TO_N and @V4L2_SUBDEV_ROUTING_NO_N_TO_1)
698a619a
TV
1662 * @V4L2_SUBDEV_ROUTING_NO_STREAM_MIX:
1663 * all streams from a sink pad must be routed to a single source pad, and
1664 * that source pad shall not get routes from any other sink pad
1665 * (a combination of @V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX and
1666 * @V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX)
a1299df6
TV
1667 * @V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING:
1668 * no multiplexed streams allowed on either source or sink sides.
69c0fe7a
LP
1669 */
1670enum v4l2_subdev_routing_restriction {
1671 V4L2_SUBDEV_ROUTING_NO_1_TO_N = BIT(0),
1672 V4L2_SUBDEV_ROUTING_NO_N_TO_1 = BIT(1),
698a619a
TV
1673 V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX = BIT(2),
1674 V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX = BIT(3),
a1299df6
TV
1675 V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING = BIT(4),
1676 V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING = BIT(5),
69c0fe7a
LP
1677 V4L2_SUBDEV_ROUTING_ONLY_1_TO_1 =
1678 V4L2_SUBDEV_ROUTING_NO_1_TO_N |
1679 V4L2_SUBDEV_ROUTING_NO_N_TO_1,
698a619a
TV
1680 V4L2_SUBDEV_ROUTING_NO_STREAM_MIX =
1681 V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX |
1682 V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX,
a1299df6
TV
1683 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING =
1684 V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING |
1685 V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING,
69c0fe7a
LP
1686};
1687
1688/**
1689 * v4l2_subdev_routing_validate() - Verify that routes comply with driver
1690 * constraints
1691 * @sd: The subdevice
1692 * @routing: Routing to verify
1693 * @disallow: Restrictions on routes
1694 *
1695 * This verifies that the given routing complies with the @disallow constraints.
1696 *
1697 * Returns 0 on success, error value otherwise.
1698 */
1699int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
1700 const struct v4l2_subdev_krouting *routing,
1701 enum v4l2_subdev_routing_restriction disallow);
1702
d0749adb
LP
1703/**
1704 * v4l2_subdev_enable_streams() - Enable streams on a pad
1705 * @sd: The subdevice
1706 * @pad: The pad
1707 * @streams_mask: Bitmask of streams to enable
1708 *
1709 * This function enables streams on a source @pad of a subdevice. The pad is
1710 * identified by its index, while the streams are identified by the
1711 * @streams_mask bitmask. This allows enabling multiple streams on a pad at
1712 * once.
1713 *
1714 * Enabling a stream that is already enabled isn't allowed. If @streams_mask
1715 * contains an already enabled stream, this function returns -EALREADY without
1716 * performing any operation.
1717 *
1718 * Per-stream enable is only available for subdevs that implement the
1719 * .enable_streams() and .disable_streams() operations. For other subdevs, this
1720 * function implements a best-effort compatibility by calling the .s_stream()
1721 * operation, limited to subdevs that have a single source pad.
1722 *
1723 * Return:
1724 * * 0: Success
1725 * * -EALREADY: One of the streams in streams_mask is already enabled
1726 * * -EINVAL: The pad index is invalid, or doesn't correspond to a source pad
1727 * * -EOPNOTSUPP: Falling back to the legacy .s_stream() operation is
1728 * impossible because the subdev has multiple source pads
1729 */
1730int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
1731 u64 streams_mask);
1732
1733/**
1734 * v4l2_subdev_disable_streams() - Disable streams on a pad
1735 * @sd: The subdevice
1736 * @pad: The pad
1737 * @streams_mask: Bitmask of streams to disable
1738 *
1739 * This function disables streams on a source @pad of a subdevice. The pad is
1740 * identified by its index, while the streams are identified by the
1741 * @streams_mask bitmask. This allows disabling multiple streams on a pad at
1742 * once.
1743 *
1744 * Disabling a streams that is not enabled isn't allowed. If @streams_mask
1745 * contains a disabled stream, this function returns -EALREADY without
1746 * performing any operation.
1747 *
1748 * Per-stream disable is only available for subdevs that implement the
1749 * .enable_streams() and .disable_streams() operations. For other subdevs, this
1750 * function implements a best-effort compatibility by calling the .s_stream()
1751 * operation, limited to subdevs that have a single source pad.
1752 *
1753 * Return:
1754 * * 0: Success
1755 * * -EALREADY: One of the streams in streams_mask is not enabled
1756 * * -EINVAL: The pad index is invalid, or doesn't correspond to a source pad
1757 * * -EOPNOTSUPP: Falling back to the legacy .s_stream() operation is
1758 * impossible because the subdev has multiple source pads
1759 */
1760int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
1761 u64 streams_mask);
1762
34a315ce
LP
1763/**
1764 * v4l2_subdev_s_stream_helper() - Helper to implement the subdev s_stream
1765 * operation using enable_streams and disable_streams
1766 * @sd: The subdevice
1767 * @enable: Enable or disable streaming
1768 *
1769 * Subdevice drivers that implement the streams-aware
1770 * &v4l2_subdev_pad_ops.enable_streams and &v4l2_subdev_pad_ops.disable_streams
1771 * operations can use this helper to implement the legacy
1772 * &v4l2_subdev_video_ops.s_stream operation.
1773 *
1774 * This helper can only be used by subdevs that have a single source pad.
1775 *
1776 * Return: 0 on success, or a negative error code otherwise.
1777 */
1778int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable);
1779
14a6fca7
TV
1780#endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1781
8227c92b 1782#endif /* CONFIG_MEDIA_CONTROLLER */
9b02cbb3 1783
21c29de1
MCC
1784/**
1785 * v4l2_subdev_init - initializes the sub-device struct
1786 *
1787 * @sd: pointer to the &struct v4l2_subdev to be initialized
1788 * @ops: pointer to &struct v4l2_subdev_ops.
1789 */
3dd5ee08
LP
1790void v4l2_subdev_init(struct v4l2_subdev *sd,
1791 const struct v4l2_subdev_ops *ops);
2a1fcdf0 1792
a8fa5507
JK
1793extern const struct v4l2_subdev_ops v4l2_subdev_call_wrappers;
1794
02679876
MCC
1795/**
1796 * v4l2_subdev_call - call an operation of a v4l2_subdev.
1797 *
1798 * @sd: pointer to the &struct v4l2_subdev
1799 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1800 * Each element there groups a set of callbacks functions.
aab7b7d1 1801 * @f: callback function to be called.
02679876
MCC
1802 * The callback functions are defined in groups, according to
1803 * each element at &struct v4l2_subdev_ops.
60d70d75 1804 * @args: arguments for @f.
21c29de1
MCC
1805 *
1806 * Example: err = v4l2_subdev_call(sd, video, s_std, norm);
2a1fcdf0
HV
1807 */
1808#define v4l2_subdev_call(sd, o, f, args...) \
da48c948 1809 ({ \
3d769df5 1810 struct v4l2_subdev *__sd = (sd); \
da48c948 1811 int __result; \
3d769df5 1812 if (!__sd) \
da48c948 1813 __result = -ENODEV; \
3d769df5 1814 else if (!(__sd->ops->o && __sd->ops->o->f)) \
da48c948 1815 __result = -ENOIOCTLCMD; \
a8fa5507
JK
1816 else if (v4l2_subdev_call_wrappers.o && \
1817 v4l2_subdev_call_wrappers.o->f) \
1818 __result = v4l2_subdev_call_wrappers.o->f( \
1819 __sd, ##args); \
da48c948 1820 else \
3d769df5 1821 __result = __sd->ops->o->f(__sd, ##args); \
da48c948
AB
1822 __result; \
1823 })
2a1fcdf0 1824
ed647ea6
TV
1825/**
1826 * v4l2_subdev_call_state_active - call an operation of a v4l2_subdev which
1827 * takes state as a parameter, passing the
1828 * subdev its active state.
1829 *
1830 * @sd: pointer to the &struct v4l2_subdev
1831 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1832 * Each element there groups a set of callbacks functions.
1833 * @f: callback function to be called.
1834 * The callback functions are defined in groups, according to
1835 * each element at &struct v4l2_subdev_ops.
1836 * @args: arguments for @f.
1837 *
1838 * This is similar to v4l2_subdev_call(), except that this version can only be
1839 * used for ops that take a subdev state as a parameter. The macro will get the
1840 * active state, lock it before calling the op and unlock it after the call.
1841 */
1842#define v4l2_subdev_call_state_active(sd, o, f, args...) \
1843 ({ \
1844 int __result; \
1845 struct v4l2_subdev_state *state; \
1846 state = v4l2_subdev_get_unlocked_active_state(sd); \
1847 if (state) \
1848 v4l2_subdev_lock_state(state); \
1849 __result = v4l2_subdev_call(sd, o, f, state, ##args); \
1850 if (state) \
1851 v4l2_subdev_unlock_state(state); \
1852 __result; \
1853 })
1854
982c0487
TV
1855/**
1856 * v4l2_subdev_call_state_try - call an operation of a v4l2_subdev which
1857 * takes state as a parameter, passing the
1858 * subdev a newly allocated try state.
1859 *
1860 * @sd: pointer to the &struct v4l2_subdev
1861 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1862 * Each element there groups a set of callbacks functions.
1863 * @f: callback function to be called.
1864 * The callback functions are defined in groups, according to
1865 * each element at &struct v4l2_subdev_ops.
1866 * @args: arguments for @f.
1867 *
1868 * This is similar to v4l2_subdev_call_state_active(), except that as this
1869 * version allocates a new state, this is only usable for
1870 * V4L2_SUBDEV_FORMAT_TRY use cases.
1871 *
1872 * Note: only legacy non-MC drivers may need this macro.
1873 */
1874#define v4l2_subdev_call_state_try(sd, o, f, args...) \
1875 ({ \
1876 int __result; \
1877 static struct lock_class_key __key; \
1878 const char *name = KBUILD_BASENAME \
1879 ":" __stringify(__LINE__) ":state->lock"; \
1880 struct v4l2_subdev_state *state = \
1881 __v4l2_subdev_state_alloc(sd, name, &__key); \
1882 v4l2_subdev_lock_state(state); \
1883 __result = v4l2_subdev_call(sd, o, f, state, ##args); \
1884 v4l2_subdev_unlock_state(state); \
1885 __v4l2_subdev_state_free(state); \
1886 __result; \
1887 })
1888
02679876
MCC
1889/**
1890 * v4l2_subdev_has_op - Checks if a subdev defines a certain operation.
1891 *
1892 * @sd: pointer to the &struct v4l2_subdev
1893 * @o: The group of callback functions in &struct v4l2_subdev_ops
1894 * which @f is a part of.
1895 * @f: callback function to be checked for its existence.
1896 */
2180f92d
HV
1897#define v4l2_subdev_has_op(sd, o, f) \
1898 ((sd)->ops->o && (sd)->ops->o->f)
1899
21c29de1
MCC
1900/**
1901 * v4l2_subdev_notify_event() - Delivers event notification for subdevice
1902 * @sd: The subdev for which to deliver the event
1903 * @ev: The event to deliver
1904 *
1905 * Will deliver the specified event to all userspace event listeners which are
1906 * subscribed to the v42l subdev event queue as well as to the bridge driver
1907 * using the notify callback. The notification type for the notify callback
1908 * will be %V4L2_DEVICE_NOTIFY_EVENT.
1909 */
8ae5640f
LPC
1910void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
1911 const struct v4l2_event *ev);
1912
dadd47d4 1913#endif /* _V4L2_SUBDEV_H */