xref: /openbmc/linux/include/media/v4l2-subdev.h (revision 1c0a0af5)
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  * @pixelcode:	media bus pixel code, valid if @flags
350  *		%FRAME_DESC_FL_BLOB is not set.
351  * @length:	number of octets per frame, valid if @flags
352  *		%V4L2_MBUS_FRAME_DESC_FL_LEN_MAX is set.
353  * @bus:	Bus-specific frame descriptor parameters
354  * @bus.csi2:	CSI-2-specific bus configuration
355  */
356 struct v4l2_mbus_frame_desc_entry {
357 	enum v4l2_mbus_frame_desc_flags flags;
358 	u32 pixelcode;
359 	u32 length;
360 	union {
361 		struct v4l2_mbus_frame_desc_entry_csi2 csi2;
362 	} bus;
363 };
364 
365  /*
366   * If this number is too small, it should be dropped altogether and the
367   * API switched to a dynamic number of frame descriptor entries.
368   */
369 #define V4L2_FRAME_DESC_ENTRY_MAX	8
370 
371 /**
372  * enum v4l2_mbus_frame_desc_type - media bus frame description type
373  *
374  * @V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED:
375  *	Undefined frame desc type. Drivers should not use this, it is
376  *	for backwards compatibility.
377  * @V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL:
378  *	Parallel media bus.
379  * @V4L2_MBUS_FRAME_DESC_TYPE_CSI2:
380  *	CSI-2 media bus. Frame desc parameters must be set in
381  *	&struct v4l2_mbus_frame_desc_entry->csi2.
382  */
383 enum v4l2_mbus_frame_desc_type {
384 	V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED = 0,
385 	V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL,
386 	V4L2_MBUS_FRAME_DESC_TYPE_CSI2,
387 };
388 
389 /**
390  * struct v4l2_mbus_frame_desc - media bus data frame description
391  * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
392  * @entry: frame descriptors array
393  * @num_entries: number of entries in @entry array
394  */
395 struct v4l2_mbus_frame_desc {
396 	enum v4l2_mbus_frame_desc_type type;
397 	struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
398 	unsigned short num_entries;
399 };
400 
401 /**
402  * enum v4l2_subdev_pre_streamon_flags - Flags for pre_streamon subdev core op
403  *
404  * @V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP: Set the transmitter to either LP-11
405  *	or LP-111 mode before call to s_stream().
406  */
407 enum v4l2_subdev_pre_streamon_flags {
408 	V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP = BIT(0),
409 };
410 
411 /**
412  * struct v4l2_subdev_video_ops - Callbacks used when v4l device was opened
413  *				  in video mode.
414  *
415  * @s_routing: see s_routing in audio_ops, except this version is for video
416  *	devices.
417  *
418  * @s_crystal_freq: sets the frequency of the crystal used to generate the
419  *	clocks in Hz. An extra flags field allows device specific configuration
420  *	regarding clock frequency dividers, etc. If not used, then set flags
421  *	to 0. If the frequency is not supported, then -EINVAL is returned.
422  *
423  * @g_std: callback for VIDIOC_G_STD() ioctl handler code.
424  *
425  * @s_std: callback for VIDIOC_S_STD() ioctl handler code.
426  *
427  * @s_std_output: set v4l2_std_id for video OUTPUT devices. This is ignored by
428  *	video input devices.
429  *
430  * @g_std_output: get current standard for video OUTPUT devices. This is ignored
431  *	by video input devices.
432  *
433  * @querystd: callback for VIDIOC_QUERYSTD() ioctl handler code.
434  *
435  * @g_tvnorms: get &v4l2_std_id with all standards supported by the video
436  *	CAPTURE device. This is ignored by video output devices.
437  *
438  * @g_tvnorms_output: get v4l2_std_id with all standards supported by the video
439  *	OUTPUT device. This is ignored by video capture devices.
440  *
441  * @g_input_status: get input status. Same as the status field in the
442  *	&struct v4l2_input
443  *
444  * @s_stream: start (enabled == 1) or stop (enabled == 0) streaming on the
445  *	sub-device. Failure on stop will remove any resources acquired in
446  *	streaming start, while the error code is still returned by the driver.
447  *	Also see call_s_stream wrapper in v4l2-subdev.c.
448  *
449  * @g_pixelaspect: callback to return the pixelaspect ratio.
450  *
451  * @g_frame_interval: callback for VIDIOC_SUBDEV_G_FRAME_INTERVAL()
452  *		      ioctl handler code.
453  *
454  * @s_frame_interval: callback for VIDIOC_SUBDEV_S_FRAME_INTERVAL()
455  *		      ioctl handler code.
456  *
457  * @s_dv_timings: Set custom dv timings in the sub device. This is used
458  *	when sub device is capable of setting detailed timing information
459  *	in the hardware to generate/detect the video signal.
460  *
461  * @g_dv_timings: Get custom dv timings in the sub device.
462  *
463  * @query_dv_timings: callback for VIDIOC_QUERY_DV_TIMINGS() ioctl handler code.
464  *
465  * @s_rx_buffer: set a host allocated memory buffer for the subdev. The subdev
466  *	can adjust @size to a lower value and must not write more data to the
467  *	buffer starting at @data than the original value of @size.
468  *
469  * @pre_streamon: May be called before streaming is actually started, to help
470  *	initialising the bus. Current usage is to set a CSI-2 transmitter to
471  *	LP-11 or LP-111 mode before streaming. See &enum
472  *	v4l2_subdev_pre_streamon_flags.
473  *
474  *	pre_streamon shall return error if it cannot perform the operation as
475  *	indicated by the flags argument. In particular, -EACCES indicates lack
476  *	of support for the operation. The caller shall call post_streamoff for
477  *	each successful call of pre_streamon.
478  *
479  * @post_streamoff: Called after streaming is stopped, but if and only if
480  *	pre_streamon was called earlier.
481  */
482 struct v4l2_subdev_video_ops {
483 	int (*s_routing)(struct v4l2_subdev *sd, u32 input, u32 output, u32 config);
484 	int (*s_crystal_freq)(struct v4l2_subdev *sd, u32 freq, u32 flags);
485 	int (*g_std)(struct v4l2_subdev *sd, v4l2_std_id *norm);
486 	int (*s_std)(struct v4l2_subdev *sd, v4l2_std_id norm);
487 	int (*s_std_output)(struct v4l2_subdev *sd, v4l2_std_id std);
488 	int (*g_std_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
489 	int (*querystd)(struct v4l2_subdev *sd, v4l2_std_id *std);
490 	int (*g_tvnorms)(struct v4l2_subdev *sd, v4l2_std_id *std);
491 	int (*g_tvnorms_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
492 	int (*g_input_status)(struct v4l2_subdev *sd, u32 *status);
493 	int (*s_stream)(struct v4l2_subdev *sd, int enable);
494 	int (*g_pixelaspect)(struct v4l2_subdev *sd, struct v4l2_fract *aspect);
495 	int (*g_frame_interval)(struct v4l2_subdev *sd,
496 				struct v4l2_subdev_frame_interval *interval);
497 	int (*s_frame_interval)(struct v4l2_subdev *sd,
498 				struct v4l2_subdev_frame_interval *interval);
499 	int (*s_dv_timings)(struct v4l2_subdev *sd,
500 			struct v4l2_dv_timings *timings);
501 	int (*g_dv_timings)(struct v4l2_subdev *sd,
502 			struct v4l2_dv_timings *timings);
503 	int (*query_dv_timings)(struct v4l2_subdev *sd,
504 			struct v4l2_dv_timings *timings);
505 	int (*s_rx_buffer)(struct v4l2_subdev *sd, void *buf,
506 			   unsigned int *size);
507 	int (*pre_streamon)(struct v4l2_subdev *sd, u32 flags);
508 	int (*post_streamoff)(struct v4l2_subdev *sd);
509 };
510 
511 /**
512  * struct v4l2_subdev_vbi_ops - Callbacks used when v4l device was opened
513  *				  in video mode via the vbi device node.
514  *
515  *  @decode_vbi_line: video decoders that support sliced VBI need to implement
516  *	this ioctl. Field p of the &struct v4l2_decode_vbi_line is set to the
517  *	start of the VBI data that was generated by the decoder. The driver
518  *	then parses the sliced VBI data and sets the other fields in the
519  *	struct accordingly. The pointer p is updated to point to the start of
520  *	the payload which can be copied verbatim into the data field of the
521  *	&struct v4l2_sliced_vbi_data. If no valid VBI data was found, then the
522  *	type field is set to 0 on return.
523  *
524  * @s_vbi_data: used to generate VBI signals on a video signal.
525  *	&struct v4l2_sliced_vbi_data is filled with the data packets that
526  *	should be output. Note that if you set the line field to 0, then that
527  *	VBI signal is disabled. If no valid VBI data was found, then the type
528  *	field is set to 0 on return.
529  *
530  * @g_vbi_data: used to obtain the sliced VBI packet from a readback register.
531  *	Not all video decoders support this. If no data is available because
532  *	the readback register contains invalid or erroneous data %-EIO is
533  *	returned. Note that you must fill in the 'id' member and the 'field'
534  *	member (to determine whether CC data from the first or second field
535  *	should be obtained).
536  *
537  * @g_sliced_vbi_cap: callback for VIDIOC_G_SLICED_VBI_CAP() ioctl handler
538  *		      code.
539  *
540  * @s_raw_fmt: setup the video encoder/decoder for raw VBI.
541  *
542  * @g_sliced_fmt: retrieve the current sliced VBI settings.
543  *
544  * @s_sliced_fmt: setup the sliced VBI settings.
545  */
546 struct v4l2_subdev_vbi_ops {
547 	int (*decode_vbi_line)(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi_line);
548 	int (*s_vbi_data)(struct v4l2_subdev *sd, const struct v4l2_sliced_vbi_data *vbi_data);
549 	int (*g_vbi_data)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *vbi_data);
550 	int (*g_sliced_vbi_cap)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_cap *cap);
551 	int (*s_raw_fmt)(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt);
552 	int (*g_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
553 	int (*s_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
554 };
555 
556 /**
557  * struct v4l2_subdev_sensor_ops - v4l2-subdev sensor operations
558  * @g_skip_top_lines: number of lines at the top of the image to be skipped.
559  *		      This is needed for some sensors, which always corrupt
560  *		      several top lines of the output image, or which send their
561  *		      metadata in them.
562  * @g_skip_frames: number of frames to skip at stream start. This is needed for
563  *		   buggy sensors that generate faulty frames when they are
564  *		   turned on.
565  */
566 struct v4l2_subdev_sensor_ops {
567 	int (*g_skip_top_lines)(struct v4l2_subdev *sd, u32 *lines);
568 	int (*g_skip_frames)(struct v4l2_subdev *sd, u32 *frames);
569 };
570 
571 /**
572  * enum v4l2_subdev_ir_mode- describes the type of IR supported
573  *
574  * @V4L2_SUBDEV_IR_MODE_PULSE_WIDTH: IR uses struct ir_raw_event records
575  */
576 enum v4l2_subdev_ir_mode {
577 	V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
578 };
579 
580 /**
581  * struct v4l2_subdev_ir_parameters - Parameters for IR TX or TX
582  *
583  * @bytes_per_data_element: bytes per data element of data in read or
584  *	write call.
585  * @mode: IR mode as defined by &enum v4l2_subdev_ir_mode.
586  * @enable: device is active if true
587  * @interrupt_enable: IR interrupts are enabled if true
588  * @shutdown: if true: set hardware to low/no power, false: normal mode
589  *
590  * @modulation: if true, it uses carrier, if false: baseband
591  * @max_pulse_width:  maximum pulse width in ns, valid only for baseband signal
592  * @carrier_freq: carrier frequency in Hz, valid only for modulated signal
593  * @duty_cycle: duty cycle percentage, valid only for modulated signal
594  * @invert_level: invert signal level
595  *
596  * @invert_carrier_sense: Send 0/space as a carrier burst. used only in TX.
597  *
598  * @noise_filter_min_width: min time of a valid pulse, in ns. Used only for RX.
599  * @carrier_range_lower: Lower carrier range, in Hz, valid only for modulated
600  *	signal. Used only for RX.
601  * @carrier_range_upper: Upper carrier range, in Hz, valid only for modulated
602  *	signal. Used only for RX.
603  * @resolution: The receive resolution, in ns . Used only for RX.
604  */
605 struct v4l2_subdev_ir_parameters {
606 	unsigned int bytes_per_data_element;
607 	enum v4l2_subdev_ir_mode mode;
608 
609 	bool enable;
610 	bool interrupt_enable;
611 	bool shutdown;
612 
613 	bool modulation;
614 	u32 max_pulse_width;
615 	unsigned int carrier_freq;
616 	unsigned int duty_cycle;
617 	bool invert_level;
618 
619 	/* Tx only */
620 	bool invert_carrier_sense;
621 
622 	/* Rx only */
623 	u32 noise_filter_min_width;
624 	unsigned int carrier_range_lower;
625 	unsigned int carrier_range_upper;
626 	u32 resolution;
627 };
628 
629 /**
630  * struct v4l2_subdev_ir_ops - operations for IR subdevices
631  *
632  * @rx_read: Reads received codes or pulse width data.
633  *	The semantics are similar to a non-blocking read() call.
634  * @rx_g_parameters: Get the current operating parameters and state of
635  *	the IR receiver.
636  * @rx_s_parameters: Set the current operating parameters and state of
637  *	the IR receiver.  It is recommended to call
638  *	[rt]x_g_parameters first to fill out the current state, and only change
639  *	the fields that need to be changed.  Upon return, the actual device
640  *	operating parameters and state will be returned.  Note that hardware
641  *	limitations may prevent the actual settings from matching the requested
642  *	settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
643  *	was requested.  An exception is when the shutdown parameter is true.
644  *	The last used operational parameters will be returned, but the actual
645  *	state of the hardware be different to minimize power consumption and
646  *	processing when shutdown is true.
647  *
648  * @tx_write: Writes codes or pulse width data for transmission.
649  *	The semantics are similar to a non-blocking write() call.
650  * @tx_g_parameters: Get the current operating parameters and state of
651  *	the IR transmitter.
652  * @tx_s_parameters: Set the current operating parameters and state of
653  *	the IR transmitter.  It is recommended to call
654  *	[rt]x_g_parameters first to fill out the current state, and only change
655  *	the fields that need to be changed.  Upon return, the actual device
656  *	operating parameters and state will be returned.  Note that hardware
657  *	limitations may prevent the actual settings from matching the requested
658  *	settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
659  *	was requested.  An exception is when the shutdown parameter is true.
660  *	The last used operational parameters will be returned, but the actual
661  *	state of the hardware be different to minimize power consumption and
662  *	processing when shutdown is true.
663  */
664 struct v4l2_subdev_ir_ops {
665 	/* Receiver */
666 	int (*rx_read)(struct v4l2_subdev *sd, u8 *buf, size_t count,
667 				ssize_t *num);
668 
669 	int (*rx_g_parameters)(struct v4l2_subdev *sd,
670 				struct v4l2_subdev_ir_parameters *params);
671 	int (*rx_s_parameters)(struct v4l2_subdev *sd,
672 				struct v4l2_subdev_ir_parameters *params);
673 
674 	/* Transmitter */
675 	int (*tx_write)(struct v4l2_subdev *sd, u8 *buf, size_t count,
676 				ssize_t *num);
677 
678 	int (*tx_g_parameters)(struct v4l2_subdev *sd,
679 				struct v4l2_subdev_ir_parameters *params);
680 	int (*tx_s_parameters)(struct v4l2_subdev *sd,
681 				struct v4l2_subdev_ir_parameters *params);
682 };
683 
684 /**
685  * struct v4l2_subdev_pad_config - Used for storing subdev pad information.
686  *
687  * @try_fmt: &struct v4l2_mbus_framefmt
688  * @try_crop: &struct v4l2_rect to be used for crop
689  * @try_compose: &struct v4l2_rect to be used for compose
690  *
691  * This structure only needs to be passed to the pad op if the 'which' field
692  * of the main argument is set to %V4L2_SUBDEV_FORMAT_TRY. For
693  * %V4L2_SUBDEV_FORMAT_ACTIVE it is safe to pass %NULL.
694  *
695  * Note: This struct is also used in active state, and the 'try' prefix is
696  * historical and to be removed.
697  */
698 struct v4l2_subdev_pad_config {
699 	struct v4l2_mbus_framefmt try_fmt;
700 	struct v4l2_rect try_crop;
701 	struct v4l2_rect try_compose;
702 };
703 
704 /**
705  * struct v4l2_subdev_state - Used for storing subdev state information.
706  *
707  * @_lock: default for 'lock'
708  * @lock: mutex for the state. May be replaced by the user.
709  * @pads: &struct v4l2_subdev_pad_config array
710  *
711  * This structure only needs to be passed to the pad op if the 'which' field
712  * of the main argument is set to %V4L2_SUBDEV_FORMAT_TRY. For
713  * %V4L2_SUBDEV_FORMAT_ACTIVE it is safe to pass %NULL.
714  */
715 struct v4l2_subdev_state {
716 	/* lock for the struct v4l2_subdev_state fields */
717 	struct mutex _lock;
718 	struct mutex *lock;
719 	struct v4l2_subdev_pad_config *pads;
720 };
721 
722 /**
723  * struct v4l2_subdev_pad_ops - v4l2-subdev pad level operations
724  *
725  * @init_cfg: initialize the pad config to default values
726  * @enum_mbus_code: callback for VIDIOC_SUBDEV_ENUM_MBUS_CODE() ioctl handler
727  *		    code.
728  * @enum_frame_size: callback for VIDIOC_SUBDEV_ENUM_FRAME_SIZE() ioctl handler
729  *		     code.
730  *
731  * @enum_frame_interval: callback for VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL() ioctl
732  *			 handler code.
733  *
734  * @get_fmt: callback for VIDIOC_SUBDEV_G_FMT() ioctl handler code.
735  *
736  * @set_fmt: callback for VIDIOC_SUBDEV_S_FMT() ioctl handler code.
737  *
738  * @get_selection: callback for VIDIOC_SUBDEV_G_SELECTION() ioctl handler code.
739  *
740  * @set_selection: callback for VIDIOC_SUBDEV_S_SELECTION() ioctl handler code.
741  *
742  * @get_edid: callback for VIDIOC_SUBDEV_G_EDID() ioctl handler code.
743  *
744  * @set_edid: callback for VIDIOC_SUBDEV_S_EDID() ioctl handler code.
745  *
746  * @dv_timings_cap: callback for VIDIOC_SUBDEV_DV_TIMINGS_CAP() ioctl handler
747  *		    code.
748  *
749  * @enum_dv_timings: callback for VIDIOC_SUBDEV_ENUM_DV_TIMINGS() ioctl handler
750  *		     code.
751  *
752  * @link_validate: used by the media controller code to check if the links
753  *		   that belongs to a pipeline can be used for stream.
754  *
755  * @get_frame_desc: get the current low level media bus frame parameters.
756  *
757  * @set_frame_desc: set the low level media bus frame parameters, @fd array
758  *                  may be adjusted by the subdev driver to device capabilities.
759  *
760  * @get_mbus_config: get the media bus configuration of a remote sub-device.
761  *		     The media bus configuration is usually retrieved from the
762  *		     firmware interface at sub-device probe time, immediately
763  *		     applied to the hardware and eventually adjusted by the
764  *		     driver. Remote sub-devices (usually video receivers) shall
765  *		     use this operation to query the transmitting end bus
766  *		     configuration in order to adjust their own one accordingly.
767  *		     Callers should make sure they get the most up-to-date as
768  *		     possible configuration from the remote end, likely calling
769  *		     this operation as close as possible to stream on time. The
770  *		     operation shall fail if the pad index it has been called on
771  *		     is not valid or in case of unrecoverable failures.
772  */
773 struct v4l2_subdev_pad_ops {
774 	int (*init_cfg)(struct v4l2_subdev *sd,
775 			struct v4l2_subdev_state *state);
776 	int (*enum_mbus_code)(struct v4l2_subdev *sd,
777 			      struct v4l2_subdev_state *state,
778 			      struct v4l2_subdev_mbus_code_enum *code);
779 	int (*enum_frame_size)(struct v4l2_subdev *sd,
780 			       struct v4l2_subdev_state *state,
781 			       struct v4l2_subdev_frame_size_enum *fse);
782 	int (*enum_frame_interval)(struct v4l2_subdev *sd,
783 				   struct v4l2_subdev_state *state,
784 				   struct v4l2_subdev_frame_interval_enum *fie);
785 	int (*get_fmt)(struct v4l2_subdev *sd,
786 		       struct v4l2_subdev_state *state,
787 		       struct v4l2_subdev_format *format);
788 	int (*set_fmt)(struct v4l2_subdev *sd,
789 		       struct v4l2_subdev_state *state,
790 		       struct v4l2_subdev_format *format);
791 	int (*get_selection)(struct v4l2_subdev *sd,
792 			     struct v4l2_subdev_state *state,
793 			     struct v4l2_subdev_selection *sel);
794 	int (*set_selection)(struct v4l2_subdev *sd,
795 			     struct v4l2_subdev_state *state,
796 			     struct v4l2_subdev_selection *sel);
797 	int (*get_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
798 	int (*set_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
799 	int (*dv_timings_cap)(struct v4l2_subdev *sd,
800 			      struct v4l2_dv_timings_cap *cap);
801 	int (*enum_dv_timings)(struct v4l2_subdev *sd,
802 			       struct v4l2_enum_dv_timings *timings);
803 #ifdef CONFIG_MEDIA_CONTROLLER
804 	int (*link_validate)(struct v4l2_subdev *sd, struct media_link *link,
805 			     struct v4l2_subdev_format *source_fmt,
806 			     struct v4l2_subdev_format *sink_fmt);
807 #endif /* CONFIG_MEDIA_CONTROLLER */
808 	int (*get_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
809 			      struct v4l2_mbus_frame_desc *fd);
810 	int (*set_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
811 			      struct v4l2_mbus_frame_desc *fd);
812 	int (*get_mbus_config)(struct v4l2_subdev *sd, unsigned int pad,
813 			       struct v4l2_mbus_config *config);
814 };
815 
816 /**
817  * struct v4l2_subdev_ops - Subdev operations
818  *
819  * @core: pointer to &struct v4l2_subdev_core_ops. Can be %NULL
820  * @tuner: pointer to &struct v4l2_subdev_tuner_ops. Can be %NULL
821  * @audio: pointer to &struct v4l2_subdev_audio_ops. Can be %NULL
822  * @video: pointer to &struct v4l2_subdev_video_ops. Can be %NULL
823  * @vbi: pointer to &struct v4l2_subdev_vbi_ops. Can be %NULL
824  * @ir: pointer to &struct v4l2_subdev_ir_ops. Can be %NULL
825  * @sensor: pointer to &struct v4l2_subdev_sensor_ops. Can be %NULL
826  * @pad: pointer to &struct v4l2_subdev_pad_ops. Can be %NULL
827  */
828 struct v4l2_subdev_ops {
829 	const struct v4l2_subdev_core_ops	*core;
830 	const struct v4l2_subdev_tuner_ops	*tuner;
831 	const struct v4l2_subdev_audio_ops	*audio;
832 	const struct v4l2_subdev_video_ops	*video;
833 	const struct v4l2_subdev_vbi_ops	*vbi;
834 	const struct v4l2_subdev_ir_ops		*ir;
835 	const struct v4l2_subdev_sensor_ops	*sensor;
836 	const struct v4l2_subdev_pad_ops	*pad;
837 };
838 
839 /**
840  * struct v4l2_subdev_internal_ops - V4L2 subdev internal ops
841  *
842  * @registered: called when this subdev is registered. When called the v4l2_dev
843  *	field is set to the correct v4l2_device.
844  *
845  * @unregistered: called when this subdev is unregistered. When called the
846  *	v4l2_dev field is still set to the correct v4l2_device.
847  *
848  * @open: called when the subdev device node is opened by an application.
849  *
850  * @close: called when the subdev device node is closed. Please note that
851  *	it is possible for @close to be called after @unregistered!
852  *
853  * @release: called when the last user of the subdev device is gone. This
854  *	happens after the @unregistered callback and when the last open
855  *	filehandle to the v4l-subdevX device node was closed. If no device
856  *	node was created for this sub-device, then the @release callback
857  *	is called right after the @unregistered callback.
858  *	The @release callback is typically used to free the memory containing
859  *	the v4l2_subdev structure. It is almost certainly required for any
860  *	sub-device that sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag.
861  *
862  * .. note::
863  *	Never call this from drivers, only the v4l2 framework can call
864  *	these ops.
865  */
866 struct v4l2_subdev_internal_ops {
867 	int (*registered)(struct v4l2_subdev *sd);
868 	void (*unregistered)(struct v4l2_subdev *sd);
869 	int (*open)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
870 	int (*close)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
871 	void (*release)(struct v4l2_subdev *sd);
872 };
873 
874 #define V4L2_SUBDEV_NAME_SIZE 32
875 
876 /* Set this flag if this subdev is a i2c device. */
877 #define V4L2_SUBDEV_FL_IS_I2C			(1U << 0)
878 /* Set this flag if this subdev is a spi device. */
879 #define V4L2_SUBDEV_FL_IS_SPI			(1U << 1)
880 /* Set this flag if this subdev needs a device node. */
881 #define V4L2_SUBDEV_FL_HAS_DEVNODE		(1U << 2)
882 /*
883  * Set this flag if this subdev generates events.
884  * Note controls can send events, thus drivers exposing controls
885  * should set this flag.
886  */
887 #define V4L2_SUBDEV_FL_HAS_EVENTS		(1U << 3)
888 
889 struct regulator_bulk_data;
890 
891 /**
892  * struct v4l2_subdev_platform_data - regulators config struct
893  *
894  * @regulators: Optional regulators used to power on/off the subdevice
895  * @num_regulators: Number of regululators
896  * @host_priv: Per-subdevice data, specific for a certain video host device
897  */
898 struct v4l2_subdev_platform_data {
899 	struct regulator_bulk_data *regulators;
900 	int num_regulators;
901 
902 	void *host_priv;
903 };
904 
905 /**
906  * struct v4l2_subdev - describes a V4L2 sub-device
907  *
908  * @entity: pointer to &struct media_entity
909  * @list: List of sub-devices
910  * @owner: The owner is the same as the driver's &struct device owner.
911  * @owner_v4l2_dev: true if the &sd->owner matches the owner of @v4l2_dev->dev
912  *	owner. Initialized by v4l2_device_register_subdev().
913  * @flags: subdev flags. Can be:
914  *   %V4L2_SUBDEV_FL_IS_I2C - Set this flag if this subdev is a i2c device;
915  *   %V4L2_SUBDEV_FL_IS_SPI - Set this flag if this subdev is a spi device;
916  *   %V4L2_SUBDEV_FL_HAS_DEVNODE - Set this flag if this subdev needs a
917  *   device node;
918  *   %V4L2_SUBDEV_FL_HAS_EVENTS -  Set this flag if this subdev generates
919  *   events.
920  *
921  * @v4l2_dev: pointer to struct &v4l2_device
922  * @ops: pointer to struct &v4l2_subdev_ops
923  * @internal_ops: pointer to struct &v4l2_subdev_internal_ops.
924  *	Never call these internal ops from within a driver!
925  * @ctrl_handler: The control handler of this subdev. May be NULL.
926  * @name: Name of the sub-device. Please notice that the name must be unique.
927  * @grp_id: can be used to group similar subdevs. Value is driver-specific
928  * @dev_priv: pointer to private data
929  * @host_priv: pointer to private data used by the device where the subdev
930  *	is attached.
931  * @devnode: subdev device node
932  * @dev: pointer to the physical device, if any
933  * @fwnode: The fwnode_handle of the subdev, usually the same as
934  *	    either dev->of_node->fwnode or dev->fwnode (whichever is non-NULL).
935  * @async_list: Links this subdev to a global subdev_list or @notifier->done
936  *	list.
937  * @asd: Pointer to respective &struct v4l2_async_subdev.
938  * @notifier: Pointer to the managing notifier.
939  * @subdev_notifier: A sub-device notifier implicitly registered for the sub-
940  *		     device using v4l2_async_register_subdev_sensor().
941  * @pdata: common part of subdevice platform data
942  * @state_lock: A pointer to a lock used for all the subdev's states, set by the
943  *		driver. This is	optional. If NULL, each state instance will get
944  *		a lock of its own.
945  * @privacy_led: Optional pointer to a LED classdev for the privacy LED for sensors.
946  * @active_state: Active state for the subdev (NULL for subdevs tracking the
947  *		  state internally). Initialized by calling
948  *		  v4l2_subdev_init_finalize().
949  *
950  * Each instance of a subdev driver should create this struct, either
951  * stand-alone or embedded in a larger struct.
952  *
953  * This structure should be initialized by v4l2_subdev_init() or one of
954  * its variants: v4l2_spi_subdev_init(), v4l2_i2c_subdev_init().
955  */
956 struct v4l2_subdev {
957 #if defined(CONFIG_MEDIA_CONTROLLER)
958 	struct media_entity entity;
959 #endif
960 	struct list_head list;
961 	struct module *owner;
962 	bool owner_v4l2_dev;
963 	u32 flags;
964 	struct v4l2_device *v4l2_dev;
965 	const struct v4l2_subdev_ops *ops;
966 	const struct v4l2_subdev_internal_ops *internal_ops;
967 	struct v4l2_ctrl_handler *ctrl_handler;
968 	char name[V4L2_SUBDEV_NAME_SIZE];
969 	u32 grp_id;
970 	void *dev_priv;
971 	void *host_priv;
972 	struct video_device *devnode;
973 	struct device *dev;
974 	struct fwnode_handle *fwnode;
975 	struct list_head async_list;
976 	struct v4l2_async_subdev *asd;
977 	struct v4l2_async_notifier *notifier;
978 	struct v4l2_async_notifier *subdev_notifier;
979 	struct v4l2_subdev_platform_data *pdata;
980 	struct mutex *state_lock;
981 
982 	/*
983 	 * The fields below are private, and should only be accessed via
984 	 * appropriate functions.
985 	 */
986 
987 	struct led_classdev *privacy_led;
988 
989 	/*
990 	 * TODO: active_state should most likely be changed from a pointer to an
991 	 * embedded field. For the time being it's kept as a pointer to more
992 	 * easily catch uses of active_state in the cases where the driver
993 	 * doesn't support it.
994 	 */
995 	struct v4l2_subdev_state *active_state;
996 };
997 
998 
999 /**
1000  * media_entity_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1001  *    the &struct media_entity embedded in it.
1002  *
1003  * @ent: pointer to &struct media_entity.
1004  */
1005 #define media_entity_to_v4l2_subdev(ent)				\
1006 ({									\
1007 	typeof(ent) __me_sd_ent = (ent);				\
1008 									\
1009 	__me_sd_ent ?							\
1010 		container_of(__me_sd_ent, struct v4l2_subdev, entity) :	\
1011 		NULL;							\
1012 })
1013 
1014 /**
1015  * vdev_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1016  *	the &struct video_device embedded on it.
1017  *
1018  * @vdev: pointer to &struct video_device
1019  */
1020 #define vdev_to_v4l2_subdev(vdev) \
1021 	((struct v4l2_subdev *)video_get_drvdata(vdev))
1022 
1023 /**
1024  * struct v4l2_subdev_fh - Used for storing subdev information per file handle
1025  *
1026  * @vfh: pointer to &struct v4l2_fh
1027  * @state: pointer to &struct v4l2_subdev_state
1028  * @owner: module pointer to the owner of this file handle
1029  */
1030 struct v4l2_subdev_fh {
1031 	struct v4l2_fh vfh;
1032 	struct module *owner;
1033 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1034 	struct v4l2_subdev_state *state;
1035 #endif
1036 };
1037 
1038 /**
1039  * to_v4l2_subdev_fh - Returns a &struct v4l2_subdev_fh from
1040  *	the &struct v4l2_fh embedded on it.
1041  *
1042  * @fh: pointer to &struct v4l2_fh
1043  */
1044 #define to_v4l2_subdev_fh(fh)	\
1045 	container_of(fh, struct v4l2_subdev_fh, vfh)
1046 
1047 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1048 
1049 /**
1050  * v4l2_subdev_get_pad_format - ancillary routine to call
1051  *	&struct v4l2_subdev_pad_config->try_fmt
1052  *
1053  * @sd: pointer to &struct v4l2_subdev
1054  * @state: pointer to &struct v4l2_subdev_state
1055  * @pad: index of the pad in the &struct v4l2_subdev_state->pads array
1056  */
1057 static inline struct v4l2_mbus_framefmt *
1058 v4l2_subdev_get_pad_format(struct v4l2_subdev *sd,
1059 			   struct v4l2_subdev_state *state,
1060 			   unsigned int pad)
1061 {
1062 	if (WARN_ON(!state))
1063 		return NULL;
1064 	if (WARN_ON(pad >= sd->entity.num_pads))
1065 		pad = 0;
1066 	return &state->pads[pad].try_fmt;
1067 }
1068 
1069 /**
1070  * v4l2_subdev_get_pad_crop - ancillary routine to call
1071  *	&struct v4l2_subdev_pad_config->try_crop
1072  *
1073  * @sd: pointer to &struct v4l2_subdev
1074  * @state: pointer to &struct v4l2_subdev_state.
1075  * @pad: index of the pad in the &struct v4l2_subdev_state->pads array.
1076  */
1077 static inline struct v4l2_rect *
1078 v4l2_subdev_get_pad_crop(struct v4l2_subdev *sd,
1079 			 struct v4l2_subdev_state *state,
1080 			 unsigned int pad)
1081 {
1082 	if (WARN_ON(!state))
1083 		return NULL;
1084 	if (WARN_ON(pad >= sd->entity.num_pads))
1085 		pad = 0;
1086 	return &state->pads[pad].try_crop;
1087 }
1088 
1089 /**
1090  * v4l2_subdev_get_pad_compose - ancillary routine to call
1091  *	&struct v4l2_subdev_pad_config->try_compose
1092  *
1093  * @sd: pointer to &struct v4l2_subdev
1094  * @state: pointer to &struct v4l2_subdev_state.
1095  * @pad: index of the pad in the &struct v4l2_subdev_state->pads array.
1096  */
1097 static inline struct v4l2_rect *
1098 v4l2_subdev_get_pad_compose(struct v4l2_subdev *sd,
1099 			    struct v4l2_subdev_state *state,
1100 			    unsigned int pad)
1101 {
1102 	if (WARN_ON(!state))
1103 		return NULL;
1104 	if (WARN_ON(pad >= sd->entity.num_pads))
1105 		pad = 0;
1106 	return &state->pads[pad].try_compose;
1107 }
1108 
1109 /*
1110  * Temprary helpers until uses of v4l2_subdev_get_try_* functions have been
1111  * renamed
1112  */
1113 #define v4l2_subdev_get_try_format(sd, state, pad) \
1114 	v4l2_subdev_get_pad_format(sd, state, pad)
1115 
1116 #define v4l2_subdev_get_try_crop(sd, state, pad) \
1117 	v4l2_subdev_get_pad_crop(sd, state, pad)
1118 
1119 #define v4l2_subdev_get_try_compose(sd, state, pad) \
1120 	v4l2_subdev_get_pad_compose(sd, state, pad)
1121 
1122 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1123 
1124 extern const struct v4l2_file_operations v4l2_subdev_fops;
1125 
1126 /**
1127  * v4l2_set_subdevdata - Sets V4L2 dev private device data
1128  *
1129  * @sd: pointer to &struct v4l2_subdev
1130  * @p: pointer to the private device data to be stored.
1131  */
1132 static inline void v4l2_set_subdevdata(struct v4l2_subdev *sd, void *p)
1133 {
1134 	sd->dev_priv = p;
1135 }
1136 
1137 /**
1138  * v4l2_get_subdevdata - Gets V4L2 dev private device data
1139  *
1140  * @sd: pointer to &struct v4l2_subdev
1141  *
1142  * Returns the pointer to the private device data to be stored.
1143  */
1144 static inline void *v4l2_get_subdevdata(const struct v4l2_subdev *sd)
1145 {
1146 	return sd->dev_priv;
1147 }
1148 
1149 /**
1150  * v4l2_set_subdev_hostdata - Sets V4L2 dev private host data
1151  *
1152  * @sd: pointer to &struct v4l2_subdev
1153  * @p: pointer to the private data to be stored.
1154  */
1155 static inline void v4l2_set_subdev_hostdata(struct v4l2_subdev *sd, void *p)
1156 {
1157 	sd->host_priv = p;
1158 }
1159 
1160 /**
1161  * v4l2_get_subdev_hostdata - Gets V4L2 dev private data
1162  *
1163  * @sd: pointer to &struct v4l2_subdev
1164  *
1165  * Returns the pointer to the private host data to be stored.
1166  */
1167 static inline void *v4l2_get_subdev_hostdata(const struct v4l2_subdev *sd)
1168 {
1169 	return sd->host_priv;
1170 }
1171 
1172 #ifdef CONFIG_MEDIA_CONTROLLER
1173 
1174 /**
1175  * v4l2_subdev_get_fwnode_pad_1_to_1 - Get pad number from a subdev fwnode
1176  *                                     endpoint, assuming 1:1 port:pad
1177  *
1178  * @entity: Pointer to the subdev entity
1179  * @endpoint: Pointer to a parsed fwnode endpoint
1180  *
1181  * This function can be used as the .get_fwnode_pad operation for
1182  * subdevices that map port numbers and pad indexes 1:1. If the endpoint
1183  * is owned by the subdevice, the function returns the endpoint port
1184  * number.
1185  *
1186  * Returns the endpoint port number on success or a negative error code.
1187  */
1188 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1189 				      struct fwnode_endpoint *endpoint);
1190 
1191 /**
1192  * v4l2_subdev_link_validate_default - validates a media link
1193  *
1194  * @sd: pointer to &struct v4l2_subdev
1195  * @link: pointer to &struct media_link
1196  * @source_fmt: pointer to &struct v4l2_subdev_format
1197  * @sink_fmt: pointer to &struct v4l2_subdev_format
1198  *
1199  * This function ensures that width, height and the media bus pixel
1200  * code are equal on both source and sink of the link.
1201  */
1202 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1203 				      struct media_link *link,
1204 				      struct v4l2_subdev_format *source_fmt,
1205 				      struct v4l2_subdev_format *sink_fmt);
1206 
1207 /**
1208  * v4l2_subdev_link_validate - validates a media link
1209  *
1210  * @link: pointer to &struct media_link
1211  *
1212  * This function calls the subdev's link_validate ops to validate
1213  * if a media link is valid for streaming. It also internally
1214  * calls v4l2_subdev_link_validate_default() to ensure that
1215  * width, height and the media bus pixel code are equal on both
1216  * source and sink of the link.
1217  */
1218 int v4l2_subdev_link_validate(struct media_link *link);
1219 
1220 /**
1221  * __v4l2_subdev_state_alloc - allocate v4l2_subdev_state
1222  *
1223  * @sd: pointer to &struct v4l2_subdev for which the state is being allocated.
1224  * @lock_name: name of the state lock
1225  * @key: lock_class_key for the lock
1226  *
1227  * Must call __v4l2_subdev_state_free() when state is no longer needed.
1228  *
1229  * Not to be called directly by the drivers.
1230  */
1231 struct v4l2_subdev_state *__v4l2_subdev_state_alloc(struct v4l2_subdev *sd,
1232 						    const char *lock_name,
1233 						    struct lock_class_key *key);
1234 
1235 /**
1236  * __v4l2_subdev_state_free - free a v4l2_subdev_state
1237  *
1238  * @state: v4l2_subdev_state to be freed.
1239  *
1240  * Not to be called directly by the drivers.
1241  */
1242 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state);
1243 
1244 /**
1245  * v4l2_subdev_init_finalize() - Finalizes the initialization of the subdevice
1246  * @sd: The subdev
1247  *
1248  * This function finalizes the initialization of the subdev, including
1249  * allocation of the active state for the subdev.
1250  *
1251  * This function must be called by the subdev drivers that use the centralized
1252  * active state, after the subdev struct has been initialized and
1253  * media_entity_pads_init() has been called, but before registering the
1254  * subdev.
1255  *
1256  * The user must call v4l2_subdev_cleanup() when the subdev is being removed.
1257  */
1258 #define v4l2_subdev_init_finalize(sd)                                          \
1259 	({                                                                     \
1260 		static struct lock_class_key __key;                            \
1261 		const char *name = KBUILD_BASENAME                             \
1262 			":" __stringify(__LINE__) ":sd->active_state->lock";   \
1263 		__v4l2_subdev_init_finalize(sd, name, &__key);                 \
1264 	})
1265 
1266 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1267 				struct lock_class_key *key);
1268 
1269 /**
1270  * v4l2_subdev_cleanup() - Releases the resources allocated by the subdevice
1271  * @sd: The subdevice
1272  *
1273  * This function will release the resources allocated in
1274  * v4l2_subdev_init_finalize.
1275  */
1276 void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
1277 
1278 /**
1279  * v4l2_subdev_lock_state() - Locks the subdev state
1280  * @state: The subdevice state
1281  *
1282  * Locks the given subdev state.
1283  *
1284  * The state must be unlocked with v4l2_subdev_unlock_state() after use.
1285  */
1286 static inline void v4l2_subdev_lock_state(struct v4l2_subdev_state *state)
1287 {
1288 	mutex_lock(state->lock);
1289 }
1290 
1291 /**
1292  * v4l2_subdev_unlock_state() - Unlocks the subdev state
1293  * @state: The subdevice state
1294  *
1295  * Unlocks the given subdev state.
1296  */
1297 static inline void v4l2_subdev_unlock_state(struct v4l2_subdev_state *state)
1298 {
1299 	mutex_unlock(state->lock);
1300 }
1301 
1302 /**
1303  * v4l2_subdev_get_unlocked_active_state() - Checks that the active subdev state
1304  *					     is unlocked and returns it
1305  * @sd: The subdevice
1306  *
1307  * Returns the active state for the subdevice, or NULL if the subdev does not
1308  * support active state. If the state is not NULL, calls
1309  * lockdep_assert_not_held() to issue a warning if the state is locked.
1310  *
1311  * This function is to be used e.g. when getting the active state for the sole
1312  * purpose of passing it forward, without accessing the state fields.
1313  */
1314 static inline struct v4l2_subdev_state *
1315 v4l2_subdev_get_unlocked_active_state(struct v4l2_subdev *sd)
1316 {
1317 	if (sd->active_state)
1318 		lockdep_assert_not_held(sd->active_state->lock);
1319 	return sd->active_state;
1320 }
1321 
1322 /**
1323  * v4l2_subdev_get_locked_active_state() - Checks that the active subdev state
1324  *					   is locked and returns it
1325  *
1326  * @sd: The subdevice
1327  *
1328  * Returns the active state for the subdevice, or NULL if the subdev does not
1329  * support active state. If the state is not NULL, calls lockdep_assert_held()
1330  * to issue a warning if the state is not locked.
1331  *
1332  * This function is to be used when the caller knows that the active state is
1333  * already locked.
1334  */
1335 static inline struct v4l2_subdev_state *
1336 v4l2_subdev_get_locked_active_state(struct v4l2_subdev *sd)
1337 {
1338 	if (sd->active_state)
1339 		lockdep_assert_held(sd->active_state->lock);
1340 	return sd->active_state;
1341 }
1342 
1343 /**
1344  * v4l2_subdev_lock_and_get_active_state() - Locks and returns the active subdev
1345  *					     state for the subdevice
1346  * @sd: The subdevice
1347  *
1348  * Returns the locked active state for the subdevice, or NULL if the subdev
1349  * does not support active state.
1350  *
1351  * The state must be unlocked with v4l2_subdev_unlock_state() after use.
1352  */
1353 static inline struct v4l2_subdev_state *
1354 v4l2_subdev_lock_and_get_active_state(struct v4l2_subdev *sd)
1355 {
1356 	if (sd->active_state)
1357 		v4l2_subdev_lock_state(sd->active_state);
1358 	return sd->active_state;
1359 }
1360 
1361 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1362 
1363 /**
1364  * v4l2_subdev_get_fmt() - Fill format based on state
1365  * @sd: subdevice
1366  * @state: subdevice state
1367  * @format: pointer to &struct v4l2_subdev_format
1368  *
1369  * Fill @format->format field based on the information in the @format struct.
1370  *
1371  * This function can be used by the subdev drivers which support active state to
1372  * implement v4l2_subdev_pad_ops.get_fmt if the subdev driver does not need to
1373  * do anything special in their get_fmt op.
1374  *
1375  * Returns 0 on success, error value otherwise.
1376  */
1377 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1378 			struct v4l2_subdev_format *format);
1379 
1380 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1381 
1382 #endif /* CONFIG_MEDIA_CONTROLLER */
1383 
1384 /**
1385  * v4l2_subdev_init - initializes the sub-device struct
1386  *
1387  * @sd: pointer to the &struct v4l2_subdev to be initialized
1388  * @ops: pointer to &struct v4l2_subdev_ops.
1389  */
1390 void v4l2_subdev_init(struct v4l2_subdev *sd,
1391 		      const struct v4l2_subdev_ops *ops);
1392 
1393 extern const struct v4l2_subdev_ops v4l2_subdev_call_wrappers;
1394 
1395 /**
1396  * v4l2_subdev_call - call an operation of a v4l2_subdev.
1397  *
1398  * @sd: pointer to the &struct v4l2_subdev
1399  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1400  *     Each element there groups a set of callbacks functions.
1401  * @f: callback function to be called.
1402  *     The callback functions are defined in groups, according to
1403  *     each element at &struct v4l2_subdev_ops.
1404  * @args: arguments for @f.
1405  *
1406  * Example: err = v4l2_subdev_call(sd, video, s_std, norm);
1407  */
1408 #define v4l2_subdev_call(sd, o, f, args...)				\
1409 	({								\
1410 		struct v4l2_subdev *__sd = (sd);			\
1411 		int __result;						\
1412 		if (!__sd)						\
1413 			__result = -ENODEV;				\
1414 		else if (!(__sd->ops->o && __sd->ops->o->f))		\
1415 			__result = -ENOIOCTLCMD;			\
1416 		else if (v4l2_subdev_call_wrappers.o &&			\
1417 			 v4l2_subdev_call_wrappers.o->f)		\
1418 			__result = v4l2_subdev_call_wrappers.o->f(	\
1419 							__sd, ##args);	\
1420 		else							\
1421 			__result = __sd->ops->o->f(__sd, ##args);	\
1422 		__result;						\
1423 	})
1424 
1425 /**
1426  * v4l2_subdev_call_state_active - call an operation of a v4l2_subdev which
1427  *				   takes state as a parameter, passing the
1428  *				   subdev its active state.
1429  *
1430  * @sd: pointer to the &struct v4l2_subdev
1431  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1432  *     Each element there groups a set of callbacks functions.
1433  * @f: callback function to be called.
1434  *     The callback functions are defined in groups, according to
1435  *     each element at &struct v4l2_subdev_ops.
1436  * @args: arguments for @f.
1437  *
1438  * This is similar to v4l2_subdev_call(), except that this version can only be
1439  * used for ops that take a subdev state as a parameter. The macro will get the
1440  * active state, lock it before calling the op and unlock it after the call.
1441  */
1442 #define v4l2_subdev_call_state_active(sd, o, f, args...)		\
1443 	({								\
1444 		int __result;						\
1445 		struct v4l2_subdev_state *state;			\
1446 		state = v4l2_subdev_get_unlocked_active_state(sd);	\
1447 		if (state)						\
1448 			v4l2_subdev_lock_state(state);			\
1449 		__result = v4l2_subdev_call(sd, o, f, state, ##args);	\
1450 		if (state)						\
1451 			v4l2_subdev_unlock_state(state);		\
1452 		__result;						\
1453 	})
1454 
1455 /**
1456  * v4l2_subdev_call_state_try - call an operation of a v4l2_subdev which
1457  *				takes state as a parameter, passing the
1458  *				subdev a newly allocated try state.
1459  *
1460  * @sd: pointer to the &struct v4l2_subdev
1461  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1462  *     Each element there groups a set of callbacks functions.
1463  * @f: callback function to be called.
1464  *     The callback functions are defined in groups, according to
1465  *     each element at &struct v4l2_subdev_ops.
1466  * @args: arguments for @f.
1467  *
1468  * This is similar to v4l2_subdev_call_state_active(), except that as this
1469  * version allocates a new state, this is only usable for
1470  * V4L2_SUBDEV_FORMAT_TRY use cases.
1471  *
1472  * Note: only legacy non-MC drivers may need this macro.
1473  */
1474 #define v4l2_subdev_call_state_try(sd, o, f, args...)                 \
1475 	({                                                            \
1476 		int __result;                                         \
1477 		static struct lock_class_key __key;                   \
1478 		const char *name = KBUILD_BASENAME                    \
1479 			":" __stringify(__LINE__) ":state->lock";     \
1480 		struct v4l2_subdev_state *state =                     \
1481 			__v4l2_subdev_state_alloc(sd, name, &__key);  \
1482 		v4l2_subdev_lock_state(state);                        \
1483 		__result = v4l2_subdev_call(sd, o, f, state, ##args); \
1484 		v4l2_subdev_unlock_state(state);                      \
1485 		__v4l2_subdev_state_free(state);                      \
1486 		__result;                                             \
1487 	})
1488 
1489 /**
1490  * v4l2_subdev_has_op - Checks if a subdev defines a certain operation.
1491  *
1492  * @sd: pointer to the &struct v4l2_subdev
1493  * @o: The group of callback functions in &struct v4l2_subdev_ops
1494  * which @f is a part of.
1495  * @f: callback function to be checked for its existence.
1496  */
1497 #define v4l2_subdev_has_op(sd, o, f) \
1498 	((sd)->ops->o && (sd)->ops->o->f)
1499 
1500 /**
1501  * v4l2_subdev_notify_event() - Delivers event notification for subdevice
1502  * @sd: The subdev for which to deliver the event
1503  * @ev: The event to deliver
1504  *
1505  * Will deliver the specified event to all userspace event listeners which are
1506  * subscribed to the v42l subdev event queue as well as to the bridge driver
1507  * using the notify callback. The notification type for the notify callback
1508  * will be %V4L2_DEVICE_NOTIFY_EVENT.
1509  */
1510 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
1511 			      const struct v4l2_event *ev);
1512 
1513 #endif /* _V4L2_SUBDEV_H */
1514