1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * V4L2 asynchronous subdevice registration API
4  *
5  * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6  */
7 
8 #ifndef V4L2_ASYNC_H
9 #define V4L2_ASYNC_H
10 
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 
14 struct device;
15 struct device_node;
16 struct v4l2_device;
17 struct v4l2_subdev;
18 struct v4l2_async_notifier;
19 
20 /**
21  * enum v4l2_async_match_type - type of asynchronous subdevice logic to be used
22  *    in order to identify a match
23  *
24  * @V4L2_ASYNC_MATCH_CUSTOM: Match will use the logic provided by &struct
25  *    v4l2_async_subdev.match ops
26  * @V4L2_ASYNC_MATCH_DEVNAME: Match will use the device name
27  * @V4L2_ASYNC_MATCH_I2C: Match will check for I2C adapter ID and address
28  * @V4L2_ASYNC_MATCH_FWNODE: Match will use firmware node
29  *
30  * This enum is used by the asyncrhronous sub-device logic to define the
31  * algorithm that will be used to match an asynchronous device.
32  */
33 enum v4l2_async_match_type {
34     V4L2_ASYNC_MATCH_CUSTOM,
35     V4L2_ASYNC_MATCH_DEVNAME,
36     V4L2_ASYNC_MATCH_I2C,
37     V4L2_ASYNC_MATCH_FWNODE,
38 };
39 
40 /**
41  * struct v4l2_async_subdev - sub-device descriptor, as known to a bridge
42  *
43  * @match_type:    type of match that will be used
44  * @match:    union of per-bus type matching data sets
45  * @match.fwnode:
46  *        pointer to &struct fwnode_handle to be matched.
47  *        Used if @match_type is %V4L2_ASYNC_MATCH_FWNODE.
48  * @match.device_name:
49  *        string containing the device name to be matched.
50  *        Used if @match_type is %V4L2_ASYNC_MATCH_DEVNAME.
51  * @match.i2c:    embedded struct with I2C parameters to be matched.
52  *        Both @match.i2c.adapter_id and @match.i2c.address
53  *        should be matched.
54  *        Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
55  * @match.i2c.adapter_id:
56  *        I2C adapter ID to be matched.
57  *        Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
58  * @match.i2c.address:
59  *        I2C address to be matched.
60  *        Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
61  * @match.custom:
62  *        Driver-specific match criteria.
63  *        Used if @match_type is %V4L2_ASYNC_MATCH_CUSTOM.
64  * @match.custom.match:
65  *        Driver-specific match function to be used if
66  *        %V4L2_ASYNC_MATCH_CUSTOM.
67  * @match.custom.priv:
68  *        Driver-specific private struct with match parameters
69  *        to be used if %V4L2_ASYNC_MATCH_CUSTOM.
70  * @asd_list:    used to add struct v4l2_async_subdev objects to the
71  *        master notifier @asd_list
72  * @list:    used to link struct v4l2_async_subdev objects, waiting to be
73  *        probed, to a notifier->waiting list
74  *
75  * When this struct is used as a member in a driver specific struct,
76  * the driver specific struct shall contain the &struct
77  * v4l2_async_subdev as its first member.
78  */
79 struct v4l2_async_subdev {
80     enum v4l2_async_match_type match_type;
81     union {
82         struct fwnode_handle *fwnode;
83         const char *device_name;
84         struct {
85             int adapter_id;
86             unsigned short address;
87         } i2c;
88         struct {
89             bool (*match)(struct device *dev, struct v4l2_async_subdev *sd);
90             void *priv;
91         } custom;
92     } match;
93 
94     /* v4l2-async core private: not to be used by drivers */
95     struct list_head list;
96     struct list_head asd_list;
97 };
98 
99 /**
100  * struct v4l2_async_notifier_operations - Asynchronous V4L2 notifier operations
101  * @bound:    a subdevice driver has successfully probed one of the subdevices
102  * @complete:    All subdevices have been probed successfully. The complete
103  *        callback is only executed for the root notifier.
104  * @unbind:    a subdevice is leaving
105  */
106 struct v4l2_async_notifier_operations {
107     int (*bound)(struct v4l2_async_notifier *notifier, struct v4l2_subdev *subdev, struct v4l2_async_subdev *asd);
108     int (*complete)(struct v4l2_async_notifier *notifier);
109     void (*unbind)(struct v4l2_async_notifier *notifier, struct v4l2_subdev *subdev, struct v4l2_async_subdev *asd);
110 };
111 
112 /**
113  * struct v4l2_async_notifier - v4l2_device notifier data
114  *
115  * @ops:    notifier operations
116  * @v4l2_dev:    v4l2_device of the root notifier, NULL otherwise
117  * @sd:        sub-device that registered the notifier, NULL otherwise
118  * @parent:    parent notifier
119  * @asd_list:    master list of struct v4l2_async_subdev
120  * @waiting:    list of struct v4l2_async_subdev, waiting for their drivers
121  * @done:    list of struct v4l2_subdev, already probed
122  * @list:    member in a global list of notifiers
123  */
124 struct v4l2_async_notifier {
125     const struct v4l2_async_notifier_operations *ops;
126     struct v4l2_device *v4l2_dev;
127     struct v4l2_subdev *sd;
128     struct v4l2_async_notifier *parent;
129     struct list_head asd_list;
130     struct list_head waiting;
131     struct list_head done;
132     struct list_head list;
133 };
134 
135 /**
136  * v4l2_async_notifier_init - Initialize a notifier.
137  *
138  * @notifier: pointer to &struct v4l2_async_notifier
139  *
140  * This function initializes the notifier @asd_list. It must be called
141  * before the first call to @v4l2_async_notifier_add_subdev.
142  */
143 void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier);
144 
145 /**
146  * v4l2_async_notifier_add_subdev - Add an async subdev to the
147  *                notifier's master asd list.
148  *
149  * @notifier: pointer to &struct v4l2_async_notifier
150  * @asd: pointer to &struct v4l2_async_subdev
151  *
152  * Call this function before registering a notifier to link the provided @asd to
153  * the notifiers master @asd_list. The @asd must be allocated with k*alloc() as
154  * it will be freed by the framework when the notifier is destroyed.
155  */
156 int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier, struct v4l2_async_subdev *asd);
157 
158 /**
159  * v4l2_async_notifier_add_fwnode_subdev - Allocate and add a fwnode async
160  *                subdev to the notifier's master asd_list.
161  *
162  * @notifier: pointer to &struct v4l2_async_notifier
163  * @fwnode: fwnode handle of the sub-device to be matched
164  * @asd_struct_size: size of the driver's async sub-device struct, including
165  *             sizeof(struct v4l2_async_subdev). The &struct
166  *             v4l2_async_subdev shall be the first member of
167  *             the driver's async sub-device struct, i.e. both
168  *             begin at the same memory address.
169  *
170  * Allocate a fwnode-matched asd of size asd_struct_size, and add it to the
171  * notifiers @asd_list. The function also gets a reference of the fwnode which
172  * is released later at notifier cleanup time.
173  */
174 struct v4l2_async_subdev *v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier,
175                                                                 struct fwnode_handle *fwnode,
176                                                                 unsigned int asd_struct_size);
177 
178 /**
179  * v4l2_async_notifier_add_fwnode_remote_subdev - Allocate and add a fwnode
180  *                          remote async subdev to the
181  *                          notifier's master asd_list.
182  *
183  * @notif: pointer to &struct v4l2_async_notifier
184  * @endpoint: local endpoint pointing to the remote sub-device to be matched
185  * @asd_struct_size: size of the driver's async sub-device struct, including
186  *             sizeof(struct v4l2_async_subdev). The &struct
187  *             v4l2_async_subdev shall be the first member of
188  *             the driver's async sub-device struct, i.e. both
189  *             begin at the same memory address.
190  *
191  * Gets the remote endpoint of a given local endpoint, set it up for fwnode
192  * matching and adds the async sub-device to the notifier's @asd_list. The
193  * function also gets a reference of the fwnode which is released later at
194  * notifier cleanup time.
195  *
196  * This is just like @v4l2_async_notifier_add_fwnode_subdev, but with the
197  * exception that the fwnode refers to a local endpoint, not the remote one.
198  */
199 struct v4l2_async_subdev *v4l2_async_notifier_add_fwnode_remote_subdev(struct v4l2_async_notifier *notif,
200                                                                        struct fwnode_handle *endpoint,
201                                                                        unsigned int asd_struct_size);
202 
203 /**
204  * v4l2_async_notifier_add_i2c_subdev - Allocate and add an i2c async
205  *                subdev to the notifier's master asd_list.
206  *
207  * @notifier: pointer to &struct v4l2_async_notifier
208  * @adapter_id: I2C adapter ID to be matched
209  * @address: I2C address of sub-device to be matched
210  * @asd_struct_size: size of the driver's async sub-device struct, including
211  *             sizeof(struct v4l2_async_subdev). The &struct
212  *             v4l2_async_subdev shall be the first member of
213  *             the driver's async sub-device struct, i.e. both
214  *             begin at the same memory address.
215  *
216  * Same as above but for I2C matched sub-devices.
217  */
218 struct v4l2_async_subdev *v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier, int adapter_id,
219                                                              unsigned short address, unsigned int asd_struct_size);
220 
221 /**
222  * v4l2_async_notifier_add_devname_subdev - Allocate and add a device-name
223  *                async subdev to the notifier's master asd_list.
224  *
225  * @notifier: pointer to &struct v4l2_async_notifier
226  * @device_name: device name string to be matched
227  * @asd_struct_size: size of the driver's async sub-device struct, including
228  *             sizeof(struct v4l2_async_subdev). The &struct
229  *             v4l2_async_subdev shall be the first member of
230  *             the driver's async sub-device struct, i.e. both
231  *             begin at the same memory address.
232  *
233  * Same as above but for device-name matched sub-devices.
234  */
235 struct v4l2_async_subdev *v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier,
236                                                                  const char *device_name, unsigned int asd_struct_size);
237 
238 /**
239  * v4l2_async_notifier_register - registers a subdevice asynchronous notifier
240  *
241  * @v4l2_dev: pointer to &struct v4l2_device
242  * @notifier: pointer to &struct v4l2_async_notifier
243  */
244 int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev, struct v4l2_async_notifier *notifier);
245 
246 /**
247  * v4l2_async_subdev_notifier_register - registers a subdevice asynchronous
248  *                     notifier for a sub-device
249  *
250  * @sd: pointer to &struct v4l2_subdev
251  * @notifier: pointer to &struct v4l2_async_notifier
252  */
253 int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd, struct v4l2_async_notifier *notifier);
254 
255 /**
256  * v4l2_async_notifier_clr_unready_dev - remove unready subdevice
257  *
258  * @notifier: pointer to &struct v4l2_async_notifier
259  */
260 #if IS_ENABLED(CONFIG_NO_GKI)
261 int v4l2_async_notifier_clr_unready_dev(struct v4l2_async_notifier *notifier);
262 #else
v4l2_async_notifier_clr_unready_dev(struct v4l2_async_notifier *notifier)263 static inline int v4l2_async_notifier_clr_unready_dev(struct v4l2_async_notifier *notifier)
264 {
265     return 0;
266 }
267 #endif
268 
269 /**
270  * v4l2_async_notifier_unregister - unregisters a subdevice
271  *    asynchronous notifier
272  *
273  * @notifier: pointer to &struct v4l2_async_notifier
274  */
275 void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier);
276 
277 /**
278  * v4l2_async_notifier_cleanup - clean up notifier resources
279  * @notifier: the notifier the resources of which are to be cleaned up
280  *
281  * Release memory resources related to a notifier, including the async
282  * sub-devices allocated for the purposes of the notifier but not the notifier
283  * itself. The user is responsible for calling this function to clean up the
284  * notifier after calling
285  * @v4l2_async_notifier_add_subdev,
286  * @v4l2_async_notifier_parse_fwnode_endpoints or
287  * @v4l2_fwnode_reference_parse_sensor_common.
288  *
289  * There is no harm from calling v4l2_async_notifier_cleanup in other
290  * cases as long as its memory has been zeroed after it has been
291  * allocated.
292  */
293 void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier);
294 
295 /**
296  * v4l2_async_register_subdev - registers a sub-device to the asynchronous
297  *    subdevice framework
298  *
299  * @sd: pointer to &struct v4l2_subdev
300  */
301 int v4l2_async_register_subdev(struct v4l2_subdev *sd);
302 
303 /**
304  * v4l2_async_register_subdev_sensor_common - registers a sensor sub-device to
305  *                          the asynchronous sub-device
306  *                          framework and parse set up common
307  *                          sensor related devices
308  *
309  * @sd: pointer to struct &v4l2_subdev
310  *
311  * This function is just like v4l2_async_register_subdev() with the exception
312  * that calling it will also parse firmware interfaces for remote references
313  * using v4l2_async_notifier_parse_fwnode_sensor_common() and registers the
314  * async sub-devices. The sub-device is similarly unregistered by calling
315  * v4l2_async_unregister_subdev().
316  *
317  * While registered, the subdev module is marked as in-use.
318  *
319  * An error is returned if the module is no longer loaded on any attempts
320  * to register it.
321  */
322 int __must_check v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd);
323 
324 /**
325  * v4l2_async_unregister_subdev - unregisters a sub-device to the asynchronous
326  *    subdevice framework
327  *
328  * @sd: pointer to &struct v4l2_subdev
329  */
330 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd);
331 #endif
332