blob: 16b1e2b097c1a6d7f33133e3b2f5ba0c4e295a28 [file] [log] [blame]
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -03001/*
2 * V4L2 asynchronous subdevice registration API
3 *
4 * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#ifndef V4L2_ASYNC_H
12#define V4L2_ASYNC_H
13
14#include <linux/list.h>
15#include <linux/mutex.h>
16
17struct device;
Sylwester Nawrockie7359f82013-07-19 12:21:29 -030018struct device_node;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030019struct v4l2_device;
20struct v4l2_subdev;
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -040021struct v4l2_async_notifier;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030022
23/* A random max subdevice number, used to allocate an array on stack */
24#define V4L2_MAX_SUBDEVS 128U
25
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -030026/**
27 * enum v4l2_async_match_type - type of asynchronous subdevice logic to be used
28 * in order to identify a match
29 *
30 * @V4L2_ASYNC_MATCH_CUSTOM: Match will use the logic provided by &struct
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -050031 * v4l2_async_subdev.match ops
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -030032 * @V4L2_ASYNC_MATCH_DEVNAME: Match will use the device name
33 * @V4L2_ASYNC_MATCH_I2C: Match will check for I2C adapter ID and address
Sakari Ailusecdf0cf2016-08-16 06:54:59 -030034 * @V4L2_ASYNC_MATCH_FWNODE: Match will use firmware node
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -030035 *
36 * This enum is used by the asyncrhronous sub-device logic to define the
37 * algorithm that will be used to match an asynchronous device.
38 */
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030039enum v4l2_async_match_type {
40 V4L2_ASYNC_MATCH_CUSTOM,
41 V4L2_ASYNC_MATCH_DEVNAME,
42 V4L2_ASYNC_MATCH_I2C,
Sakari Ailusecdf0cf2016-08-16 06:54:59 -030043 V4L2_ASYNC_MATCH_FWNODE,
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030044};
45
46/**
47 * struct v4l2_async_subdev - sub-device descriptor, as known to a bridge
Mauro Carvalho Chehabf8b27372015-08-22 05:16:24 -030048 *
49 * @match_type: type of match that will be used
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030050 * @match: union of per-bus type matching data sets
Mauro Carvalho Chehabeb0f73b2017-09-27 11:43:00 -040051 * @match.fwnode:
52 * pointer to &struct fwnode_handle to be matched.
53 * Used if @match_type is %V4L2_ASYNC_MATCH_FWNODE.
54 * @match.device_name:
55 * string containing the device name to be matched.
56 * Used if @match_type is %V4L2_ASYNC_MATCH_DEVNAME.
57 * @match.i2c: embedded struct with I2C parameters to be matched.
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -050058 * Both @match.i2c.adapter_id and @match.i2c.address
Mauro Carvalho Chehabeb0f73b2017-09-27 11:43:00 -040059 * should be matched.
60 * Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
61 * @match.i2c.adapter_id:
62 * I2C adapter ID to be matched.
63 * Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
64 * @match.i2c.address:
65 * I2C address to be matched.
66 * Used if @match_type is %V4L2_ASYNC_MATCH_I2C.
67 * @match.custom:
68 * Driver-specific match criteria.
69 * Used if @match_type is %V4L2_ASYNC_MATCH_CUSTOM.
70 * @match.custom.match:
71 * Driver-specific match function to be used if
72 * %V4L2_ASYNC_MATCH_CUSTOM.
73 * @match.custom.priv:
74 * Driver-specific private struct with match parameters
75 * to be used if %V4L2_ASYNC_MATCH_CUSTOM.
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -040076 * @asd_list: used to add struct v4l2_async_subdev objects to the
77 * master notifier @asd_list
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030078 * @list: used to link struct v4l2_async_subdev objects, waiting to be
79 * probed, to a notifier->waiting list
Sakari Ailus9ca46532017-08-17 11:28:21 -040080 *
81 * When this struct is used as a member in a driver specific struct,
82 * the driver specific struct shall contain the &struct
83 * v4l2_async_subdev as its first member.
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030084 */
85struct v4l2_async_subdev {
Sylwester Nawrockicfca7642013-07-19 12:14:46 -030086 enum v4l2_async_match_type match_type;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030087 union {
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -040088 struct fwnode_handle *fwnode;
89 const char *device_name;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -030090 struct {
91 int adapter_id;
92 unsigned short address;
93 } i2c;
94 struct {
95 bool (*match)(struct device *,
96 struct v4l2_async_subdev *);
97 void *priv;
98 } custom;
99 } match;
100
101 /* v4l2-async core private: not to be used by drivers */
102 struct list_head list;
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400103 struct list_head asd_list;
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300104};
105
106/**
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -0400107 * struct v4l2_async_notifier_operations - Asynchronous V4L2 notifier operations
108 * @bound: a subdevice driver has successfully probed one of the subdevices
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400109 * @complete: All subdevices have been probed successfully. The complete
110 * callback is only executed for the root notifier.
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300111 * @unbind: a subdevice is leaving
112 */
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -0400113struct v4l2_async_notifier_operations {
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300114 int (*bound)(struct v4l2_async_notifier *notifier,
115 struct v4l2_subdev *subdev,
116 struct v4l2_async_subdev *asd);
117 int (*complete)(struct v4l2_async_notifier *notifier);
118 void (*unbind)(struct v4l2_async_notifier *notifier,
119 struct v4l2_subdev *subdev,
120 struct v4l2_async_subdev *asd);
121};
122
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300123/**
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -0400124 * struct v4l2_async_notifier - v4l2_device notifier data
125 *
126 * @ops: notifier operations
127 * @num_subdevs: number of subdevices used in the subdevs array
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -0400128 * @subdevs: array of pointers to subdevice descriptors
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400129 * @v4l2_dev: v4l2_device of the root notifier, NULL otherwise
130 * @sd: sub-device that registered the notifier, NULL otherwise
131 * @parent: parent notifier
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400132 * @asd_list: master list of struct v4l2_async_subdev, replaces @subdevs
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -0400133 * @waiting: list of struct v4l2_async_subdev, waiting for their drivers
134 * @done: list of struct v4l2_subdev, already probed
135 * @list: member in a global list of notifiers
136 */
137struct v4l2_async_notifier {
138 const struct v4l2_async_notifier_operations *ops;
139 unsigned int num_subdevs;
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -0400140 struct v4l2_async_subdev **subdevs;
141 struct v4l2_device *v4l2_dev;
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400142 struct v4l2_subdev *sd;
143 struct v4l2_async_notifier *parent;
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400144 struct list_head asd_list;
Laurent Pinchartb6ee3f02017-08-30 13:18:04 -0400145 struct list_head waiting;
146 struct list_head done;
147 struct list_head list;
148};
149
150/**
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400151 * v4l2_async_notifier_init - Initialize a notifier.
152 *
153 * @notifier: pointer to &struct v4l2_async_notifier
154 *
155 * This function initializes the notifier @asd_list. It must be called
156 * before the first call to @v4l2_async_notifier_add_subdev.
157 */
158void v4l2_async_notifier_init(struct v4l2_async_notifier *notifier);
159
160/**
161 * v4l2_async_notifier_add_subdev - Add an async subdev to the
162 * notifier's master asd list.
163 *
164 * @notifier: pointer to &struct v4l2_async_notifier
165 * @asd: pointer to &struct v4l2_async_subdev
166 *
167 * This can be used before registering a notifier to add an
168 * asd to the notifiers @asd_list. If the caller uses this
169 * method to compose an asd list, it must never allocate
170 * or place asd's in the @subdevs array.
171 */
172int v4l2_async_notifier_add_subdev(struct v4l2_async_notifier *notifier,
173 struct v4l2_async_subdev *asd);
174
175/**
Steve Longerbeam23989b42018-09-29 15:54:07 -0400176 * v4l2_async_notifier_add_fwnode_subdev - Allocate and add a fwnode async
177 * subdev to the notifier's master asd_list.
178 *
179 * @notifier: pointer to &struct v4l2_async_notifier
180 * @fwnode: fwnode handle of the sub-device to be matched
181 * @asd_struct_size: size of the driver's async sub-device struct, including
182 * sizeof(struct v4l2_async_subdev). The &struct
183 * v4l2_async_subdev shall be the first member of
184 * the driver's async sub-device struct, i.e. both
185 * begin at the same memory address.
186 *
187 * This can be used before registering a notifier to add a
188 * fwnode-matched asd to the notifiers master asd_list. If the caller
189 * uses this method to compose an asd list, it must never allocate
190 * or place asd's in the @subdevs array.
191 */
192struct v4l2_async_subdev *
193v4l2_async_notifier_add_fwnode_subdev(struct v4l2_async_notifier *notifier,
194 struct fwnode_handle *fwnode,
195 unsigned int asd_struct_size);
196
197/**
198 * v4l2_async_notifier_add_i2c_subdev - Allocate and add an i2c async
199 * subdev to the notifier's master asd_list.
200 *
201 * @notifier: pointer to &struct v4l2_async_notifier
202 * @adapter_id: I2C adapter ID to be matched
203 * @address: I2C address of sub-device to be matched
204 * @asd_struct_size: size of the driver's async sub-device struct, including
205 * sizeof(struct v4l2_async_subdev). The &struct
206 * v4l2_async_subdev shall be the first member of
207 * the driver's async sub-device struct, i.e. both
208 * begin at the same memory address.
209 *
210 * Same as above but for I2C matched sub-devices.
211 */
212struct v4l2_async_subdev *
213v4l2_async_notifier_add_i2c_subdev(struct v4l2_async_notifier *notifier,
214 int adapter_id, unsigned short address,
215 unsigned int asd_struct_size);
216
217/**
218 * v4l2_async_notifier_add_devname_subdev - Allocate and add a device-name
219 * async subdev to the notifier's master asd_list.
220 *
221 * @notifier: pointer to &struct v4l2_async_notifier
222 * @device_name: device name string to be matched
223 * @asd_struct_size: size of the driver's async sub-device struct, including
224 * sizeof(struct v4l2_async_subdev). The &struct
225 * v4l2_async_subdev shall be the first member of
226 * the driver's async sub-device struct, i.e. both
227 * begin at the same memory address.
228 *
229 * Same as above but for device-name matched sub-devices.
230 */
231struct v4l2_async_subdev *
232v4l2_async_notifier_add_devname_subdev(struct v4l2_async_notifier *notifier,
233 const char *device_name,
234 unsigned int asd_struct_size);
235
236
237/**
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300238 * v4l2_async_notifier_register - registers a subdevice asynchronous notifier
239 *
240 * @v4l2_dev: pointer to &struct v4l2_device
241 * @notifier: pointer to &struct v4l2_async_notifier
242 */
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300243int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
244 struct v4l2_async_notifier *notifier);
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300245
246/**
Sakari Ailus2cab00b2017-09-24 20:54:31 -0400247 * 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 */
253int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
254 struct v4l2_async_notifier *notifier);
255
256/**
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300257 * v4l2_async_notifier_unregister - unregisters a subdevice asynchronous notifier
258 *
259 * @notifier: pointer to &struct v4l2_async_notifier
260 */
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300261void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier);
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300262
263/**
Sakari Ailus9ca46532017-08-17 11:28:21 -0400264 * v4l2_async_notifier_cleanup - clean up notifier resources
265 * @notifier: the notifier the resources of which are to be cleaned up
266 *
267 * Release memory resources related to a notifier, including the async
268 * sub-devices allocated for the purposes of the notifier but not the notifier
269 * itself. The user is responsible for calling this function to clean up the
Steve Longerbeamb47d7ff2018-09-29 15:54:06 -0400270 * notifier after calling
271 * @v4l2_async_notifier_add_subdev,
272 * @v4l2_async_notifier_parse_fwnode_endpoints or
Sakari Ailus7a9ec802017-09-06 08:35:42 -0400273 * @v4l2_fwnode_reference_parse_sensor_common.
Sakari Ailus9ca46532017-08-17 11:28:21 -0400274 *
275 * There is no harm from calling v4l2_async_notifier_cleanup in other
276 * cases as long as its memory has been zeroed after it has been
277 * allocated.
278 */
279void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier);
280
281/**
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300282 * v4l2_async_register_subdev - registers a sub-device to the asynchronous
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500283 * subdevice framework
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300284 *
285 * @sd: pointer to &struct v4l2_subdev
286 */
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300287int v4l2_async_register_subdev(struct v4l2_subdev *sd);
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300288
289/**
Sakari Ailusaef69d52017-09-24 18:47:44 -0400290 * v4l2_async_register_subdev_sensor_common - registers a sensor sub-device to
291 * the asynchronous sub-device
292 * framework and parse set up common
293 * sensor related devices
294 *
295 * @sd: pointer to struct &v4l2_subdev
296 *
297 * This function is just like v4l2_async_register_subdev() with the exception
298 * that calling it will also parse firmware interfaces for remote references
299 * using v4l2_async_notifier_parse_fwnode_sensor_common() and registers the
300 * async sub-devices. The sub-device is similarly unregistered by calling
301 * v4l2_async_unregister_subdev().
302 *
303 * While registered, the subdev module is marked as in-use.
304 *
305 * An error is returned if the module is no longer loaded on any attempts
306 * to register it.
307 */
308int __must_check v4l2_async_register_subdev_sensor_common(
309 struct v4l2_subdev *sd);
310
311/**
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300312 * v4l2_async_unregister_subdev - unregisters a sub-device to the asynchronous
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500313 * subdevice framework
Mauro Carvalho Chehabab4f5a4a2016-07-21 09:24:55 -0300314 *
315 * @sd: pointer to &struct v4l2_subdev
316 */
Guennadi Liakhovetskie9e31042013-01-08 07:06:31 -0300317void v4l2_async_unregister_subdev(struct v4l2_subdev *sd);
318#endif