blob: be75d99006678612ecfa85d9f259264e4cc062f2 [file] [log] [blame]
Sakari Ailusca50c192016-08-12 08:05:51 -03001/*
2 * V4L2 fwnode binding parsing library
3 *
4 * The origins of the V4L2 fwnode library are in V4L2 OF library that
5 * formerly was located in v4l2-of.c.
6 *
7 * Copyright (c) 2016 Intel Corporation.
8 * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
9 *
10 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
11 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
12 *
13 * Copyright (C) 2012 Renesas Electronics Corp.
14 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
19 */
20#include <linux/acpi.h>
21#include <linux/kernel.h>
Sakari Ailus9ca46532017-08-17 11:28:21 -040022#include <linux/mm.h>
Sakari Ailusca50c192016-08-12 08:05:51 -030023#include <linux/module.h>
24#include <linux/of.h>
25#include <linux/property.h>
26#include <linux/slab.h>
27#include <linux/string.h>
28#include <linux/types.h>
29
Sakari Ailus9ca46532017-08-17 11:28:21 -040030#include <media/v4l2-async.h>
Sakari Ailusca50c192016-08-12 08:05:51 -030031#include <media/v4l2-fwnode.h>
Sakari Ailusaef69d52017-09-24 18:47:44 -040032#include <media/v4l2-subdev.h>
Sakari Ailusca50c192016-08-12 08:05:51 -030033
Sakari Ailuse07a41f2015-02-25 14:51:01 -050034enum v4l2_fwnode_bus_type {
35 V4L2_FWNODE_BUS_TYPE_GUESS = 0,
36 V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
37 V4L2_FWNODE_BUS_TYPE_CSI1,
38 V4L2_FWNODE_BUS_TYPE_CCP2,
39 NR_OF_V4L2_FWNODE_BUS_TYPE,
40};
41
Sakari Ailusf3112732017-02-20 05:42:09 -050042static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
43 struct v4l2_fwnode_endpoint *vep)
Sakari Ailusca50c192016-08-12 08:05:51 -030044{
45 struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
46 bool have_clk_lane = false;
47 unsigned int flags = 0, lanes_used = 0;
48 unsigned int i;
49 u32 v;
50 int rval;
51
52 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
53 if (rval > 0) {
Sakari Ailusad3cdf32017-08-14 06:43:07 -040054 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
Sakari Ailusca50c192016-08-12 08:05:51 -030055
Sakari Ailusad3cdf32017-08-14 06:43:07 -040056 bus->num_data_lanes =
57 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
Sakari Ailusca50c192016-08-12 08:05:51 -030058
59 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
60 bus->num_data_lanes);
61
62 for (i = 0; i < bus->num_data_lanes; i++) {
63 if (lanes_used & BIT(array[i]))
64 pr_warn("duplicated lane %u in data-lanes\n",
65 array[i]);
66 lanes_used |= BIT(array[i]);
67
68 bus->data_lanes[i] = array[i];
69 }
Sakari Ailusca50c192016-08-12 08:05:51 -030070
Mauro Carvalho Chehab4ee23622017-06-26 14:07:54 -040071 rval = fwnode_property_read_u32_array(fwnode,
Sakari Ailusb24f0212017-08-14 06:15:21 -040072 "lane-polarities", NULL,
73 0);
Mauro Carvalho Chehab4ee23622017-06-26 14:07:54 -040074 if (rval > 0) {
Sakari Ailusb24f0212017-08-14 06:15:21 -040075 if (rval != 1 + bus->num_data_lanes /* clock+data */) {
Mauro Carvalho Chehab4ee23622017-06-26 14:07:54 -040076 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
77 1 + bus->num_data_lanes, rval);
78 return -EINVAL;
79 }
Sakari Ailusca50c192016-08-12 08:05:51 -030080
Sakari Ailusb24f0212017-08-14 06:15:21 -040081 fwnode_property_read_u32_array(fwnode,
82 "lane-polarities", array,
83 1 + bus->num_data_lanes);
Mauro Carvalho Chehab4ee23622017-06-26 14:07:54 -040084
85 for (i = 0; i < 1 + bus->num_data_lanes; i++)
86 bus->lane_polarities[i] = array[i];
Sakari Ailusca50c192016-08-12 08:05:51 -030087 }
Sakari Ailusb24f0212017-08-14 06:15:21 -040088
Sakari Ailusca50c192016-08-12 08:05:51 -030089 }
90
91 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
92 if (lanes_used & BIT(v))
93 pr_warn("duplicated lane %u in clock-lanes\n", v);
94 lanes_used |= BIT(v);
95
96 bus->clock_lane = v;
97 have_clk_lane = true;
98 }
99
100 if (fwnode_property_present(fwnode, "clock-noncontinuous"))
101 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
102 else if (have_clk_lane || bus->num_data_lanes > 0)
103 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
104
105 bus->flags = flags;
106 vep->bus_type = V4L2_MBUS_CSI2;
107
108 return 0;
109}
110
111static void v4l2_fwnode_endpoint_parse_parallel_bus(
112 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
113{
114 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
115 unsigned int flags = 0;
116 u32 v;
117
118 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v))
119 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
120 V4L2_MBUS_HSYNC_ACTIVE_LOW;
121
122 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v))
123 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
124 V4L2_MBUS_VSYNC_ACTIVE_LOW;
125
126 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v))
127 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
128 V4L2_MBUS_FIELD_EVEN_LOW;
129 if (flags)
130 vep->bus_type = V4L2_MBUS_PARALLEL;
131 else
132 vep->bus_type = V4L2_MBUS_BT656;
133
134 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v))
135 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
136 V4L2_MBUS_PCLK_SAMPLE_FALLING;
137
138 if (!fwnode_property_read_u32(fwnode, "data-active", &v))
139 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
140 V4L2_MBUS_DATA_ACTIVE_LOW;
141
142 if (fwnode_property_present(fwnode, "slave-mode"))
143 flags |= V4L2_MBUS_SLAVE;
144 else
145 flags |= V4L2_MBUS_MASTER;
146
147 if (!fwnode_property_read_u32(fwnode, "bus-width", &v))
148 bus->bus_width = v;
149
150 if (!fwnode_property_read_u32(fwnode, "data-shift", &v))
151 bus->data_shift = v;
152
153 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v))
154 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
155 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
156
Jacopo Mondi9b04fcc2018-07-09 10:19:19 -0400157 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v))
158 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
159 V4L2_MBUS_DATA_ENABLE_LOW;
160
Sakari Ailusca50c192016-08-12 08:05:51 -0300161 bus->flags = flags;
162
163}
164
Mauro Carvalho Chehababc5b2c2017-07-20 16:27:27 -0400165static void
166v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
167 struct v4l2_fwnode_endpoint *vep,
168 u32 bus_type)
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500169{
170 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
171 u32 v;
172
173 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v))
174 bus->clock_inv = v;
175
176 if (!fwnode_property_read_u32(fwnode, "strobe", &v))
177 bus->strobe = v;
178
179 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v))
180 bus->data_lane = v;
181
182 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v))
183 bus->clock_lane = v;
184
185 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
186 vep->bus_type = V4L2_MBUS_CCP2;
187 else
188 vep->bus_type = V4L2_MBUS_CSI1;
189}
190
Sakari Ailusca50c192016-08-12 08:05:51 -0300191int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
192 struct v4l2_fwnode_endpoint *vep)
193{
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500194 u32 bus_type = 0;
Sakari Ailusca50c192016-08-12 08:05:51 -0300195 int rval;
196
197 fwnode_graph_parse_endpoint(fwnode, &vep->base);
198
199 /* Zero fields from bus_type to until the end */
200 memset(&vep->bus_type, 0, sizeof(*vep) -
201 offsetof(typeof(*vep), bus_type));
202
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500203 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
204
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500205 switch (bus_type) {
206 case V4L2_FWNODE_BUS_TYPE_GUESS:
207 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep);
208 if (rval)
209 return rval;
210 /*
211 * Parse the parallel video bus properties only if none
212 * of the MIPI CSI-2 specific properties were found.
213 */
214 if (vep->bus.mipi_csi2.flags == 0)
215 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep);
Sakari Ailusca50c192016-08-12 08:05:51 -0300216
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500217 return 0;
218 case V4L2_FWNODE_BUS_TYPE_CCP2:
219 case V4L2_FWNODE_BUS_TYPE_CSI1:
220 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
221
222 return 0;
223 default:
224 pr_warn("unsupported bus type %u\n", bus_type);
225 return -EINVAL;
226 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300227}
228EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
229
Sakari Ailusca50c192016-08-12 08:05:51 -0300230void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
231{
232 if (IS_ERR_OR_NULL(vep))
233 return;
234
235 kfree(vep->link_frequencies);
236 kfree(vep);
237}
238EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
239
Sakari Ailusca50c192016-08-12 08:05:51 -0300240struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
241 struct fwnode_handle *fwnode)
242{
243 struct v4l2_fwnode_endpoint *vep;
244 int rval;
245
246 vep = kzalloc(sizeof(*vep), GFP_KERNEL);
247 if (!vep)
248 return ERR_PTR(-ENOMEM);
249
250 rval = v4l2_fwnode_endpoint_parse(fwnode, vep);
251 if (rval < 0)
252 goto out_err;
253
254 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
255 NULL, 0);
Sakari Ailus06f81522017-06-20 09:14:43 -0400256 if (rval > 0) {
257 vep->link_frequencies =
258 kmalloc_array(rval, sizeof(*vep->link_frequencies),
259 GFP_KERNEL);
260 if (!vep->link_frequencies) {
261 rval = -ENOMEM;
262 goto out_err;
263 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300264
Sakari Ailus06f81522017-06-20 09:14:43 -0400265 vep->nr_of_link_frequencies = rval;
266
267 rval = fwnode_property_read_u64_array(
268 fwnode, "link-frequencies", vep->link_frequencies,
269 vep->nr_of_link_frequencies);
270 if (rval < 0)
271 goto out_err;
Sakari Ailusca50c192016-08-12 08:05:51 -0300272 }
273
Sakari Ailusca50c192016-08-12 08:05:51 -0300274 return vep;
275
276out_err:
277 v4l2_fwnode_endpoint_free(vep);
278 return ERR_PTR(rval);
279}
280EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
281
Sakari Ailusca50c192016-08-12 08:05:51 -0300282int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
283 struct v4l2_fwnode_link *link)
284{
285 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
286 struct fwnode_handle *fwnode;
287
288 memset(link, 0, sizeof(*link));
289
290 fwnode = fwnode_get_parent(__fwnode);
291 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
292 fwnode = fwnode_get_next_parent(fwnode);
293 if (is_of_node(fwnode) &&
294 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
295 fwnode = fwnode_get_next_parent(fwnode);
296 link->local_node = fwnode;
297
298 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
299 if (!fwnode) {
300 fwnode_handle_put(fwnode);
301 return -ENOLINK;
302 }
303
304 fwnode = fwnode_get_parent(fwnode);
305 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
306 fwnode = fwnode_get_next_parent(fwnode);
307 if (is_of_node(fwnode) &&
308 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
309 fwnode = fwnode_get_next_parent(fwnode);
310 link->remote_node = fwnode;
311
312 return 0;
313}
314EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
315
Sakari Ailusca50c192016-08-12 08:05:51 -0300316void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
317{
318 fwnode_handle_put(link->local_node);
319 fwnode_handle_put(link->remote_node);
320}
321EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
322
Sakari Ailus9ca46532017-08-17 11:28:21 -0400323static int v4l2_async_notifier_fwnode_parse_endpoint(
324 struct device *dev, struct v4l2_async_notifier *notifier,
325 struct fwnode_handle *endpoint, unsigned int asd_struct_size,
326 int (*parse_endpoint)(struct device *dev,
327 struct v4l2_fwnode_endpoint *vep,
328 struct v4l2_async_subdev *asd))
329{
330 struct v4l2_async_subdev *asd;
331 struct v4l2_fwnode_endpoint *vep;
332 int ret = 0;
333
334 asd = kzalloc(asd_struct_size, GFP_KERNEL);
335 if (!asd)
336 return -ENOMEM;
337
338 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400339 asd->match.fwnode =
Sakari Ailus9ca46532017-08-17 11:28:21 -0400340 fwnode_graph_get_remote_port_parent(endpoint);
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400341 if (!asd->match.fwnode) {
Sakari Ailus9ca46532017-08-17 11:28:21 -0400342 dev_warn(dev, "bad remote port parent\n");
Steve Longerbeam4382f372018-09-29 15:54:04 -0400343 ret = -ENOTCONN;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400344 goto out_err;
345 }
346
347 vep = v4l2_fwnode_endpoint_alloc_parse(endpoint);
348 if (IS_ERR(vep)) {
349 ret = PTR_ERR(vep);
350 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
351 ret);
352 goto out_err;
353 }
354
355 ret = parse_endpoint ? parse_endpoint(dev, vep, asd) : 0;
356 if (ret == -ENOTCONN)
357 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep->base.port,
358 vep->base.id);
359 else if (ret < 0)
360 dev_warn(dev,
361 "driver could not parse port@%u/endpoint@%u (%d)\n",
362 vep->base.port, vep->base.id, ret);
363 v4l2_fwnode_endpoint_free(vep);
364 if (ret < 0)
365 goto out_err;
366
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400367 ret = v4l2_async_notifier_add_subdev(notifier, asd);
368 if (ret < 0) {
369 /* not an error if asd already exists */
370 if (ret == -EEXIST)
371 ret = 0;
372 goto out_err;
373 }
Sakari Ailus9ca46532017-08-17 11:28:21 -0400374
375 return 0;
376
377out_err:
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400378 fwnode_handle_put(asd->match.fwnode);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400379 kfree(asd);
380
381 return ret == -ENOTCONN ? 0 : ret;
382}
383
384static int __v4l2_async_notifier_parse_fwnode_endpoints(
385 struct device *dev, struct v4l2_async_notifier *notifier,
386 size_t asd_struct_size, unsigned int port, bool has_port,
387 int (*parse_endpoint)(struct device *dev,
388 struct v4l2_fwnode_endpoint *vep,
389 struct v4l2_async_subdev *asd))
390{
391 struct fwnode_handle *fwnode;
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400392 int ret = 0;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400393
394 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
395 return -EINVAL;
396
397 for (fwnode = NULL; (fwnode = fwnode_graph_get_next_endpoint(
398 dev_fwnode(dev), fwnode)); ) {
399 struct fwnode_handle *dev_fwnode;
400 bool is_available;
401
402 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
403 is_available = fwnode_device_is_available(dev_fwnode);
404 fwnode_handle_put(dev_fwnode);
405 if (!is_available)
406 continue;
407
408 if (has_port) {
409 struct fwnode_endpoint ep;
410
411 ret = fwnode_graph_parse_endpoint(fwnode, &ep);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400412 if (ret)
413 break;
414
415 if (ep.port != port)
416 continue;
417 }
418
419 ret = v4l2_async_notifier_fwnode_parse_endpoint(
420 dev, notifier, fwnode, asd_struct_size, parse_endpoint);
421 if (ret < 0)
422 break;
423 }
424
425 fwnode_handle_put(fwnode);
426
427 return ret;
428}
429
430int v4l2_async_notifier_parse_fwnode_endpoints(
431 struct device *dev, struct v4l2_async_notifier *notifier,
432 size_t asd_struct_size,
433 int (*parse_endpoint)(struct device *dev,
434 struct v4l2_fwnode_endpoint *vep,
435 struct v4l2_async_subdev *asd))
436{
437 return __v4l2_async_notifier_parse_fwnode_endpoints(
438 dev, notifier, asd_struct_size, 0, false, parse_endpoint);
439}
440EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
441
442int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
443 struct device *dev, struct v4l2_async_notifier *notifier,
444 size_t asd_struct_size, unsigned int port,
445 int (*parse_endpoint)(struct device *dev,
446 struct v4l2_fwnode_endpoint *vep,
447 struct v4l2_async_subdev *asd))
448{
449 return __v4l2_async_notifier_parse_fwnode_endpoints(
450 dev, notifier, asd_struct_size, port, true, parse_endpoint);
451}
452EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
453
Sakari Ailusd8428532017-06-20 07:47:24 -0400454/*
455 * v4l2_fwnode_reference_parse - parse references for async sub-devices
456 * @dev: the device node the properties of which are parsed for references
457 * @notifier: the async notifier where the async subdevs will be added
458 * @prop: the name of the property
459 *
460 * Return: 0 on success
461 * -ENOENT if no entries were found
462 * -ENOMEM if memory allocation failed
463 * -EINVAL if property parsing failed
464 */
465static int v4l2_fwnode_reference_parse(
466 struct device *dev, struct v4l2_async_notifier *notifier,
467 const char *prop)
468{
469 struct fwnode_reference_args args;
470 unsigned int index;
471 int ret;
472
473 for (index = 0;
474 !(ret = fwnode_property_get_reference_args(
475 dev_fwnode(dev), prop, NULL, 0, index, &args));
476 index++)
477 fwnode_handle_put(args.fwnode);
478
479 if (!index)
480 return -ENOENT;
481
482 /*
483 * Note that right now both -ENODATA and -ENOENT may signal
484 * out-of-bounds access. Return the error in cases other than that.
485 */
486 if (ret != -ENOENT && ret != -ENODATA)
487 return ret;
488
Sakari Ailusd8428532017-06-20 07:47:24 -0400489 for (index = 0; !fwnode_property_get_reference_args(
490 dev_fwnode(dev), prop, NULL, 0, index, &args);
491 index++) {
492 struct v4l2_async_subdev *asd;
493
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400494 asd = v4l2_async_notifier_add_fwnode_subdev(
495 notifier, args.fwnode, sizeof(*asd));
496 if (IS_ERR(asd)) {
497 ret = PTR_ERR(asd);
498 /* not an error if asd already exists */
499 if (ret == -EEXIST) {
500 fwnode_handle_put(args.fwnode);
501 continue;
502 }
503
Sakari Ailusd8428532017-06-20 07:47:24 -0400504 goto error;
505 }
Sakari Ailusd8428532017-06-20 07:47:24 -0400506 }
507
508 return 0;
509
510error:
511 fwnode_handle_put(args.fwnode);
512 return ret;
513}
514
Sakari Ailusa1699a42017-06-22 08:03:28 -0400515/*
516 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
517 * arguments
518 * @fwnode: fwnode to read @prop from
519 * @notifier: notifier for @dev
520 * @prop: the name of the property
521 * @index: the index of the reference to get
522 * @props: the array of integer property names
523 * @nprops: the number of integer property names in @nprops
524 *
525 * First find an fwnode referred to by the reference at @index in @prop.
526 *
527 * Then under that fwnode, @nprops times, for each property in @props,
528 * iteratively follow child nodes starting from fwnode such that they have the
529 * property in @props array at the index of the child node distance from the
530 * root node and the value of that property matching with the integer argument
531 * of the reference, at the same index.
532 *
533 * The child fwnode reched at the end of the iteration is then returned to the
534 * caller.
535 *
536 * The core reason for this is that you cannot refer to just any node in ACPI.
537 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
538 * provide a list of (property name, property value) tuples where each tuple
539 * uniquely identifies a child node. The first tuple identifies a child directly
540 * underneath the device fwnode, the next tuple identifies a child node
541 * underneath the fwnode identified by the previous tuple, etc. until you
542 * reached the fwnode you need.
543 *
544 * An example with a graph, as defined in Documentation/acpi/dsd/graph.txt:
545 *
546 * Scope (\_SB.PCI0.I2C2)
547 * {
548 * Device (CAM0)
549 * {
550 * Name (_DSD, Package () {
551 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
552 * Package () {
553 * Package () {
554 * "compatible",
555 * Package () { "nokia,smia" }
556 * },
557 * },
558 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
559 * Package () {
560 * Package () { "port0", "PRT0" },
561 * }
562 * })
563 * Name (PRT0, Package() {
564 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
565 * Package () {
566 * Package () { "port", 0 },
567 * },
568 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
569 * Package () {
570 * Package () { "endpoint0", "EP00" },
571 * }
572 * })
573 * Name (EP00, Package() {
574 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
575 * Package () {
576 * Package () { "endpoint", 0 },
577 * Package () {
578 * "remote-endpoint",
579 * Package() {
580 * \_SB.PCI0.ISP, 4, 0
581 * }
582 * },
583 * }
584 * })
585 * }
586 * }
587 *
588 * Scope (\_SB.PCI0)
589 * {
590 * Device (ISP)
591 * {
592 * Name (_DSD, Package () {
593 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
594 * Package () {
595 * Package () { "port4", "PRT4" },
596 * }
597 * })
598 *
599 * Name (PRT4, Package() {
600 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
601 * Package () {
602 * Package () { "port", 4 },
603 * },
604 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
605 * Package () {
606 * Package () { "endpoint0", "EP40" },
607 * }
608 * })
609 *
610 * Name (EP40, Package() {
611 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
612 * Package () {
613 * Package () { "endpoint", 0 },
614 * Package () {
615 * "remote-endpoint",
616 * Package () {
617 * \_SB.PCI0.I2C2.CAM0,
618 * 0, 0
619 * }
620 * },
621 * }
622 * })
623 * }
624 * }
625 *
626 * From the EP40 node under ISP device, you could parse the graph remote
627 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
628 *
629 * @fwnode: fwnode referring to EP40 under ISP.
630 * @prop: "remote-endpoint"
631 * @index: 0
632 * @props: "port", "endpoint"
633 * @nprops: 2
634 *
635 * And you'd get back fwnode referring to EP00 under CAM0.
636 *
637 * The same works the other way around: if you use EP00 under CAM0 as the
638 * fwnode, you'll get fwnode referring to EP40 under ISP.
639 *
640 * The same example in DT syntax would look like this:
641 *
642 * cam: cam0 {
643 * compatible = "nokia,smia";
644 *
645 * port {
646 * port = <0>;
647 * endpoint {
648 * endpoint = <0>;
649 * remote-endpoint = <&isp 4 0>;
650 * };
651 * };
652 * };
653 *
654 * isp: isp {
655 * ports {
656 * port@4 {
657 * port = <4>;
658 * endpoint {
659 * endpoint = <0>;
660 * remote-endpoint = <&cam 0 0>;
661 * };
662 * };
663 * };
664 * };
665 *
666 * Return: 0 on success
667 * -ENOENT if no entries (or the property itself) were found
668 * -EINVAL if property parsing otherwise failed
669 * -ENOMEM if memory allocation failed
670 */
671static struct fwnode_handle *v4l2_fwnode_reference_get_int_prop(
672 struct fwnode_handle *fwnode, const char *prop, unsigned int index,
673 const char * const *props, unsigned int nprops)
674{
675 struct fwnode_reference_args fwnode_args;
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300676 u64 *args = fwnode_args.args;
Sakari Ailusa1699a42017-06-22 08:03:28 -0400677 struct fwnode_handle *child;
678 int ret;
679
680 /*
681 * Obtain remote fwnode as well as the integer arguments.
682 *
683 * Note that right now both -ENODATA and -ENOENT may signal
684 * out-of-bounds access. Return -ENOENT in that case.
685 */
686 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
687 index, &fwnode_args);
688 if (ret)
689 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
690
691 /*
692 * Find a node in the tree under the referred fwnode corresponding to
693 * the integer arguments.
694 */
695 fwnode = fwnode_args.fwnode;
696 while (nprops--) {
697 u32 val;
698
699 /* Loop over all child nodes under fwnode. */
700 fwnode_for_each_child_node(fwnode, child) {
701 if (fwnode_property_read_u32(child, *props, &val))
702 continue;
703
704 /* Found property, see if its value matches. */
705 if (val == *args)
706 break;
707 }
708
709 fwnode_handle_put(fwnode);
710
711 /* No property found; return an error here. */
712 if (!child) {
713 fwnode = ERR_PTR(-ENOENT);
714 break;
715 }
716
717 props++;
718 args++;
719 fwnode = child;
720 }
721
722 return fwnode;
723}
724
725/*
726 * v4l2_fwnode_reference_parse_int_props - parse references for async
727 * sub-devices
728 * @dev: struct device pointer
729 * @notifier: notifier for @dev
730 * @prop: the name of the property
731 * @props: the array of integer property names
732 * @nprops: the number of integer properties
733 *
734 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
735 * property @prop with integer arguments with child nodes matching in properties
736 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
737 * accordingly.
738 *
739 * While it is technically possible to use this function on DT, it is only
740 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
741 * on ACPI the references are limited to devices.
742 *
743 * Return: 0 on success
744 * -ENOENT if no entries (or the property itself) were found
745 * -EINVAL if property parsing otherwisefailed
746 * -ENOMEM if memory allocation failed
747 */
748static int v4l2_fwnode_reference_parse_int_props(
749 struct device *dev, struct v4l2_async_notifier *notifier,
750 const char *prop, const char * const *props, unsigned int nprops)
751{
752 struct fwnode_handle *fwnode;
753 unsigned int index;
754 int ret;
755
Mauro Carvalho Chehab9879c9d2018-04-04 07:21:04 -0400756 index = 0;
757 do {
758 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
759 prop, index,
760 props, nprops);
761 if (IS_ERR(fwnode)) {
762 /*
763 * Note that right now both -ENODATA and -ENOENT may
764 * signal out-of-bounds access. Return the error in
765 * cases other than that.
766 */
767 if (PTR_ERR(fwnode) != -ENOENT &&
768 PTR_ERR(fwnode) != -ENODATA)
769 return PTR_ERR(fwnode);
770 break;
771 }
Sakari Ailusa1699a42017-06-22 08:03:28 -0400772 fwnode_handle_put(fwnode);
Mauro Carvalho Chehab9879c9d2018-04-04 07:21:04 -0400773 index++;
774 } while (1);
Sakari Ailusa1699a42017-06-22 08:03:28 -0400775
Sakari Ailusa1699a42017-06-22 08:03:28 -0400776 for (index = 0; !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(
777 dev_fwnode(dev), prop, index, props,
778 nprops))); index++) {
779 struct v4l2_async_subdev *asd;
780
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400781 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
782 sizeof(*asd));
783 if (IS_ERR(asd)) {
784 ret = PTR_ERR(asd);
785 /* not an error if asd already exists */
786 if (ret == -EEXIST) {
787 fwnode_handle_put(fwnode);
788 continue;
789 }
790
Sakari Ailusa1699a42017-06-22 08:03:28 -0400791 goto error;
792 }
Sakari Ailusa1699a42017-06-22 08:03:28 -0400793 }
794
795 return PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
796
797error:
798 fwnode_handle_put(fwnode);
799 return ret;
800}
801
Sakari Ailus7a9ec802017-09-06 08:35:42 -0400802int v4l2_async_notifier_parse_fwnode_sensor_common(
803 struct device *dev, struct v4l2_async_notifier *notifier)
804{
805 static const char * const led_props[] = { "led" };
806 static const struct {
807 const char *name;
808 const char * const *props;
809 unsigned int nprops;
810 } props[] = {
811 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
812 { "lens-focus", NULL, 0 },
813 };
814 unsigned int i;
815
816 for (i = 0; i < ARRAY_SIZE(props); i++) {
817 int ret;
818
819 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
820 ret = v4l2_fwnode_reference_parse_int_props(
821 dev, notifier, props[i].name,
822 props[i].props, props[i].nprops);
823 else
824 ret = v4l2_fwnode_reference_parse(
825 dev, notifier, props[i].name);
826 if (ret && ret != -ENOENT) {
827 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
828 props[i].name, ret);
829 return ret;
830 }
831 }
832
833 return 0;
834}
835EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
836
Sakari Ailusaef69d52017-09-24 18:47:44 -0400837int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
838{
839 struct v4l2_async_notifier *notifier;
840 int ret;
841
842 if (WARN_ON(!sd->dev))
843 return -ENODEV;
844
845 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
846 if (!notifier)
847 return -ENOMEM;
848
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400849 v4l2_async_notifier_init(notifier);
850
Sakari Ailusaef69d52017-09-24 18:47:44 -0400851 ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
852 notifier);
853 if (ret < 0)
854 goto out_cleanup;
855
856 ret = v4l2_async_subdev_notifier_register(sd, notifier);
857 if (ret < 0)
858 goto out_cleanup;
859
860 ret = v4l2_async_register_subdev(sd);
861 if (ret < 0)
862 goto out_unregister;
863
864 sd->subdev_notifier = notifier;
865
866 return 0;
867
868out_unregister:
869 v4l2_async_notifier_unregister(notifier);
870
871out_cleanup:
872 v4l2_async_notifier_cleanup(notifier);
873 kfree(notifier);
874
875 return ret;
876}
877EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
878
Steve Longerbeam1634f0e2018-09-29 15:54:09 -0400879int v4l2_async_register_fwnode_subdev(
880 struct v4l2_subdev *sd, size_t asd_struct_size,
881 unsigned int *ports, unsigned int num_ports,
882 int (*parse_endpoint)(struct device *dev,
883 struct v4l2_fwnode_endpoint *vep,
884 struct v4l2_async_subdev *asd))
885{
886 struct v4l2_async_notifier *notifier;
887 struct device *dev = sd->dev;
888 struct fwnode_handle *fwnode;
889 int ret;
890
891 if (WARN_ON(!dev))
892 return -ENODEV;
893
894 fwnode = dev_fwnode(dev);
895 if (!fwnode_device_is_available(fwnode))
896 return -ENODEV;
897
898 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
899 if (!notifier)
900 return -ENOMEM;
901
902 v4l2_async_notifier_init(notifier);
903
904 if (!ports) {
905 ret = v4l2_async_notifier_parse_fwnode_endpoints(
906 dev, notifier, asd_struct_size, parse_endpoint);
907 if (ret < 0)
908 goto out_cleanup;
909 } else {
910 unsigned int i;
911
912 for (i = 0; i < num_ports; i++) {
913 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
914 dev, notifier, asd_struct_size,
915 ports[i], parse_endpoint);
916 if (ret < 0)
917 goto out_cleanup;
918 }
919 }
920
921 ret = v4l2_async_subdev_notifier_register(sd, notifier);
922 if (ret < 0)
923 goto out_cleanup;
924
925 ret = v4l2_async_register_subdev(sd);
926 if (ret < 0)
927 goto out_unregister;
928
929 sd->subdev_notifier = notifier;
930
931 return 0;
932
933out_unregister:
934 v4l2_async_notifier_unregister(notifier);
935out_cleanup:
936 v4l2_async_notifier_cleanup(notifier);
937 kfree(notifier);
938
939 return ret;
940}
941EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
942
Sakari Ailusca50c192016-08-12 08:05:51 -0300943MODULE_LICENSE("GPL");
944MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
945MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
946MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");