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