2 * V4L2 fwnode binding parsing library
4 * The origins of the V4L2 fwnode library are in V4L2 OF library that
5 * formerly was located in v4l2-of.c.
7 * Copyright (c) 2016 Intel Corporation.
8 * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
10 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
11 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
13 * Copyright (C) 2012 Renesas Electronics Corp.
14 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
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.
20 #include <linux/acpi.h>
21 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/property.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
30 #include <media/v4l2-async.h>
31 #include <media/v4l2-fwnode.h>
32 #include <media/v4l2-subdev.h>
34 enum 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 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
40 V4L2_FWNODE_BUS_TYPE_PARALLEL,
41 V4L2_FWNODE_BUS_TYPE_BT656,
42 NR_OF_V4L2_FWNODE_BUS_TYPE,
45 static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
46 struct v4l2_fwnode_endpoint *vep,
47 enum v4l2_fwnode_bus_type bus_type)
49 struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
50 bool have_clk_lane = false, have_data_lanes = false,
51 have_lane_polarities = false;
52 unsigned int flags = 0, lanes_used = 0;
53 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
55 unsigned int num_data_lanes = 0;
56 bool use_default_lane_mapping = false;
61 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY) {
62 use_default_lane_mapping = true;
64 num_data_lanes = min_t(u32, bus->num_data_lanes,
65 V4L2_FWNODE_CSI2_MAX_DATA_LANES);
67 clock_lane = bus->clock_lane;
69 use_default_lane_mapping = false;
71 for (i = 0; i < num_data_lanes; i++) {
72 array[i] = bus->data_lanes[i];
74 use_default_lane_mapping = false;
77 if (use_default_lane_mapping)
78 pr_debug("using default lane mapping\n");
81 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
84 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
86 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
89 have_data_lanes = true;
92 for (i = 0; i < num_data_lanes; i++) {
93 if (lanes_used & BIT(array[i])) {
94 if (have_data_lanes || !use_default_lane_mapping)
95 pr_warn("duplicated lane %u in data-lanes, using defaults\n",
97 use_default_lane_mapping = true;
99 lanes_used |= BIT(array[i]);
102 pr_debug("lane %u position %u\n", i, array[i]);
105 rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL,
108 if (rval != 1 + num_data_lanes /* clock+data */) {
109 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
110 1 + num_data_lanes, rval);
114 have_lane_polarities = true;
117 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
119 pr_debug("clock lane position %u\n", v);
120 have_clk_lane = true;
123 if (lanes_used & BIT(clock_lane)) {
124 if (have_clk_lane || !use_default_lane_mapping)
125 pr_warn("duplicated lane %u in clock-lanes, using defaults\n",
127 use_default_lane_mapping = true;
130 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
131 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
132 pr_debug("non-continuous clock\n");
134 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
137 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY || lanes_used ||
138 have_clk_lane || (flags & ~V4L2_MBUS_CSI2_CONTINUOUS_CLOCK)) {
140 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
141 bus->num_data_lanes = num_data_lanes;
143 if (use_default_lane_mapping) {
145 for (i = 0; i < num_data_lanes; i++)
146 bus->data_lanes[i] = 1 + i;
148 bus->clock_lane = clock_lane;
149 for (i = 0; i < num_data_lanes; i++)
150 bus->data_lanes[i] = array[i];
153 if (have_lane_polarities) {
154 fwnode_property_read_u32_array(fwnode,
155 "lane-polarities", array,
158 for (i = 0; i < 1 + num_data_lanes; i++) {
159 bus->lane_polarities[i] = array[i];
160 pr_debug("lane %u polarity %sinverted",
161 i, array[i] ? "" : "not ");
164 pr_debug("no lane polarities defined, assuming not inverted\n");
171 #define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
172 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
173 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
174 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
175 V4L2_MBUS_FIELD_EVEN_HIGH | \
176 V4L2_MBUS_FIELD_EVEN_LOW)
178 static void v4l2_fwnode_endpoint_parse_parallel_bus(
179 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep,
180 enum v4l2_fwnode_bus_type bus_type)
182 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
183 unsigned int flags = 0;
186 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
187 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
188 V4L2_MBUS_HSYNC_ACTIVE_LOW;
189 pr_debug("hsync-active %s\n", v ? "high" : "low");
192 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
193 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
194 V4L2_MBUS_VSYNC_ACTIVE_LOW;
195 pr_debug("vsync-active %s\n", v ? "high" : "low");
198 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
199 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
200 V4L2_MBUS_FIELD_EVEN_LOW;
201 pr_debug("field-even-active %s\n", v ? "high" : "low");
204 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
205 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
206 V4L2_MBUS_PCLK_SAMPLE_FALLING;
207 pr_debug("pclk-sample %s\n", v ? "high" : "low");
210 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
211 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
212 V4L2_MBUS_DATA_ACTIVE_LOW;
213 pr_debug("data-active %s\n", v ? "high" : "low");
216 if (fwnode_property_present(fwnode, "slave-mode")) {
217 pr_debug("slave mode\n");
218 flags |= V4L2_MBUS_SLAVE;
220 flags |= V4L2_MBUS_MASTER;
223 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
225 pr_debug("bus-width %u\n", v);
228 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
230 pr_debug("data-shift %u\n", v);
233 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
234 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
235 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
236 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
239 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
240 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
241 V4L2_MBUS_DATA_ENABLE_LOW;
242 pr_debug("data-enable-active %s\n", v ? "high" : "low");
248 if (flags & PARALLEL_MBUS_FLAGS)
249 vep->bus_type = V4L2_MBUS_PARALLEL;
251 vep->bus_type = V4L2_MBUS_BT656;
253 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
254 vep->bus_type = V4L2_MBUS_PARALLEL;
257 case V4L2_FWNODE_BUS_TYPE_BT656:
258 vep->bus_type = V4L2_MBUS_BT656;
259 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
265 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
266 struct v4l2_fwnode_endpoint *vep,
267 enum v4l2_fwnode_bus_type bus_type)
269 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
272 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
274 pr_debug("clock-inv %u\n", v);
277 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
279 pr_debug("strobe %u\n", v);
282 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
284 pr_debug("data-lanes %u\n", v);
287 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
289 pr_debug("clock-lanes %u\n", v);
292 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
293 vep->bus_type = V4L2_MBUS_CCP2;
295 vep->bus_type = V4L2_MBUS_CSI1;
298 static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
299 struct v4l2_fwnode_endpoint *vep)
304 pr_debug("===== begin V4L2 endpoint properties\n");
306 fwnode_graph_parse_endpoint(fwnode, &vep->base);
308 /* Zero fields from bus_type to until the end */
309 memset(&vep->bus_type, 0, sizeof(*vep) -
310 offsetof(typeof(*vep), bus_type));
312 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
315 case V4L2_FWNODE_BUS_TYPE_GUESS:
316 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
321 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
322 v4l2_fwnode_endpoint_parse_parallel_bus(
323 fwnode, vep, V4L2_MBUS_UNKNOWN);
326 case V4L2_FWNODE_BUS_TYPE_CCP2:
327 case V4L2_FWNODE_BUS_TYPE_CSI1:
328 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
331 case V4L2_FWNODE_BUS_TYPE_CSI2_DPHY:
332 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
333 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
339 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
340 case V4L2_FWNODE_BUS_TYPE_BT656:
341 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep, bus_type);
345 pr_warn("unsupported bus type %u\n", bus_type);
352 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
353 struct v4l2_fwnode_endpoint *vep)
357 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
359 pr_debug("===== end V4L2 endpoint properties\n");
363 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
365 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
367 if (IS_ERR_OR_NULL(vep))
370 kfree(vep->link_frequencies);
372 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
374 int v4l2_fwnode_endpoint_alloc_parse(
375 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
379 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
383 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
388 vep->link_frequencies =
389 kmalloc_array(rval, sizeof(*vep->link_frequencies),
391 if (!vep->link_frequencies)
394 vep->nr_of_link_frequencies = rval;
396 rval = fwnode_property_read_u64_array(
397 fwnode, "link-frequencies", vep->link_frequencies,
398 vep->nr_of_link_frequencies);
400 v4l2_fwnode_endpoint_free(vep);
404 for (i = 0; i < vep->nr_of_link_frequencies; i++)
405 pr_info("link-frequencies %u value %llu\n", i,
406 vep->link_frequencies[i]);
409 pr_debug("===== end V4L2 endpoint properties\n");
413 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
415 int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
416 struct v4l2_fwnode_link *link)
418 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
419 struct fwnode_handle *fwnode;
421 memset(link, 0, sizeof(*link));
423 fwnode = fwnode_get_parent(__fwnode);
424 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
425 fwnode = fwnode_get_next_parent(fwnode);
426 if (is_of_node(fwnode) &&
427 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
428 fwnode = fwnode_get_next_parent(fwnode);
429 link->local_node = fwnode;
431 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
433 fwnode_handle_put(fwnode);
437 fwnode = fwnode_get_parent(fwnode);
438 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
439 fwnode = fwnode_get_next_parent(fwnode);
440 if (is_of_node(fwnode) &&
441 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
442 fwnode = fwnode_get_next_parent(fwnode);
443 link->remote_node = fwnode;
447 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
449 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
451 fwnode_handle_put(link->local_node);
452 fwnode_handle_put(link->remote_node);
454 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
456 static int v4l2_async_notifier_fwnode_parse_endpoint(
457 struct device *dev, struct v4l2_async_notifier *notifier,
458 struct fwnode_handle *endpoint, unsigned int asd_struct_size,
459 int (*parse_endpoint)(struct device *dev,
460 struct v4l2_fwnode_endpoint *vep,
461 struct v4l2_async_subdev *asd))
463 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
464 struct v4l2_async_subdev *asd;
467 asd = kzalloc(asd_struct_size, GFP_KERNEL);
471 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
473 fwnode_graph_get_remote_port_parent(endpoint);
474 if (!asd->match.fwnode) {
475 dev_warn(dev, "bad remote port parent\n");
480 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
482 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
487 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
488 if (ret == -ENOTCONN)
489 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
493 "driver could not parse port@%u/endpoint@%u (%d)\n",
494 vep.base.port, vep.base.id, ret);
495 v4l2_fwnode_endpoint_free(&vep);
499 ret = v4l2_async_notifier_add_subdev(notifier, asd);
501 /* not an error if asd already exists */
510 fwnode_handle_put(asd->match.fwnode);
513 return ret == -ENOTCONN ? 0 : ret;
516 static int __v4l2_async_notifier_parse_fwnode_endpoints(
517 struct device *dev, struct v4l2_async_notifier *notifier,
518 size_t asd_struct_size, unsigned int port, bool has_port,
519 int (*parse_endpoint)(struct device *dev,
520 struct v4l2_fwnode_endpoint *vep,
521 struct v4l2_async_subdev *asd))
523 struct fwnode_handle *fwnode;
526 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
529 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
530 struct fwnode_handle *dev_fwnode;
533 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
534 is_available = fwnode_device_is_available(dev_fwnode);
535 fwnode_handle_put(dev_fwnode);
540 struct fwnode_endpoint ep;
542 ret = fwnode_graph_parse_endpoint(fwnode, &ep);
550 ret = v4l2_async_notifier_fwnode_parse_endpoint(
551 dev, notifier, fwnode, asd_struct_size, parse_endpoint);
556 fwnode_handle_put(fwnode);
561 int v4l2_async_notifier_parse_fwnode_endpoints(
562 struct device *dev, struct v4l2_async_notifier *notifier,
563 size_t asd_struct_size,
564 int (*parse_endpoint)(struct device *dev,
565 struct v4l2_fwnode_endpoint *vep,
566 struct v4l2_async_subdev *asd))
568 return __v4l2_async_notifier_parse_fwnode_endpoints(
569 dev, notifier, asd_struct_size, 0, false, parse_endpoint);
571 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
573 int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
574 struct device *dev, struct v4l2_async_notifier *notifier,
575 size_t asd_struct_size, unsigned int port,
576 int (*parse_endpoint)(struct device *dev,
577 struct v4l2_fwnode_endpoint *vep,
578 struct v4l2_async_subdev *asd))
580 return __v4l2_async_notifier_parse_fwnode_endpoints(
581 dev, notifier, asd_struct_size, port, true, parse_endpoint);
583 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
586 * v4l2_fwnode_reference_parse - parse references for async sub-devices
587 * @dev: the device node the properties of which are parsed for references
588 * @notifier: the async notifier where the async subdevs will be added
589 * @prop: the name of the property
591 * Return: 0 on success
592 * -ENOENT if no entries were found
593 * -ENOMEM if memory allocation failed
594 * -EINVAL if property parsing failed
596 static int v4l2_fwnode_reference_parse(
597 struct device *dev, struct v4l2_async_notifier *notifier,
600 struct fwnode_reference_args args;
605 !(ret = fwnode_property_get_reference_args(
606 dev_fwnode(dev), prop, NULL, 0, index, &args));
608 fwnode_handle_put(args.fwnode);
614 * Note that right now both -ENODATA and -ENOENT may signal
615 * out-of-bounds access. Return the error in cases other than that.
617 if (ret != -ENOENT && ret != -ENODATA)
620 for (index = 0; !fwnode_property_get_reference_args(
621 dev_fwnode(dev), prop, NULL, 0, index, &args);
623 struct v4l2_async_subdev *asd;
625 asd = v4l2_async_notifier_add_fwnode_subdev(
626 notifier, args.fwnode, sizeof(*asd));
629 /* not an error if asd already exists */
630 if (ret == -EEXIST) {
631 fwnode_handle_put(args.fwnode);
642 fwnode_handle_put(args.fwnode);
647 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
649 * @fwnode: fwnode to read @prop from
650 * @notifier: notifier for @dev
651 * @prop: the name of the property
652 * @index: the index of the reference to get
653 * @props: the array of integer property names
654 * @nprops: the number of integer property names in @nprops
656 * First find an fwnode referred to by the reference at @index in @prop.
658 * Then under that fwnode, @nprops times, for each property in @props,
659 * iteratively follow child nodes starting from fwnode such that they have the
660 * property in @props array at the index of the child node distance from the
661 * root node and the value of that property matching with the integer argument
662 * of the reference, at the same index.
664 * The child fwnode reched at the end of the iteration is then returned to the
667 * The core reason for this is that you cannot refer to just any node in ACPI.
668 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
669 * provide a list of (property name, property value) tuples where each tuple
670 * uniquely identifies a child node. The first tuple identifies a child directly
671 * underneath the device fwnode, the next tuple identifies a child node
672 * underneath the fwnode identified by the previous tuple, etc. until you
673 * reached the fwnode you need.
675 * An example with a graph, as defined in Documentation/acpi/dsd/graph.txt:
677 * Scope (\_SB.PCI0.I2C2)
681 * Name (_DSD, Package () {
682 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
686 * Package () { "nokia,smia" }
689 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
691 * Package () { "port0", "PRT0" },
694 * Name (PRT0, Package() {
695 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
697 * Package () { "port", 0 },
699 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
701 * Package () { "endpoint0", "EP00" },
704 * Name (EP00, Package() {
705 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
707 * Package () { "endpoint", 0 },
711 * \_SB.PCI0.ISP, 4, 0
723 * Name (_DSD, Package () {
724 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
726 * Package () { "port4", "PRT4" },
730 * Name (PRT4, Package() {
731 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
733 * Package () { "port", 4 },
735 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
737 * Package () { "endpoint0", "EP40" },
741 * Name (EP40, Package() {
742 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
744 * Package () { "endpoint", 0 },
748 * \_SB.PCI0.I2C2.CAM0,
757 * From the EP40 node under ISP device, you could parse the graph remote
758 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
760 * @fwnode: fwnode referring to EP40 under ISP.
761 * @prop: "remote-endpoint"
763 * @props: "port", "endpoint"
766 * And you'd get back fwnode referring to EP00 under CAM0.
768 * The same works the other way around: if you use EP00 under CAM0 as the
769 * fwnode, you'll get fwnode referring to EP40 under ISP.
771 * The same example in DT syntax would look like this:
774 * compatible = "nokia,smia";
780 * remote-endpoint = <&isp 4 0>;
791 * remote-endpoint = <&cam 0 0>;
797 * Return: 0 on success
798 * -ENOENT if no entries (or the property itself) were found
799 * -EINVAL if property parsing otherwise failed
800 * -ENOMEM if memory allocation failed
802 static struct fwnode_handle *v4l2_fwnode_reference_get_int_prop(
803 struct fwnode_handle *fwnode, const char *prop, unsigned int index,
804 const char * const *props, unsigned int nprops)
806 struct fwnode_reference_args fwnode_args;
807 u64 *args = fwnode_args.args;
808 struct fwnode_handle *child;
812 * Obtain remote fwnode as well as the integer arguments.
814 * Note that right now both -ENODATA and -ENOENT may signal
815 * out-of-bounds access. Return -ENOENT in that case.
817 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
818 index, &fwnode_args);
820 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
823 * Find a node in the tree under the referred fwnode corresponding to
824 * the integer arguments.
826 fwnode = fwnode_args.fwnode;
830 /* Loop over all child nodes under fwnode. */
831 fwnode_for_each_child_node(fwnode, child) {
832 if (fwnode_property_read_u32(child, *props, &val))
835 /* Found property, see if its value matches. */
840 fwnode_handle_put(fwnode);
842 /* No property found; return an error here. */
844 fwnode = ERR_PTR(-ENOENT);
857 * v4l2_fwnode_reference_parse_int_props - parse references for async
859 * @dev: struct device pointer
860 * @notifier: notifier for @dev
861 * @prop: the name of the property
862 * @props: the array of integer property names
863 * @nprops: the number of integer properties
865 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
866 * property @prop with integer arguments with child nodes matching in properties
867 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
870 * While it is technically possible to use this function on DT, it is only
871 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
872 * on ACPI the references are limited to devices.
874 * Return: 0 on success
875 * -ENOENT if no entries (or the property itself) were found
876 * -EINVAL if property parsing otherwisefailed
877 * -ENOMEM if memory allocation failed
879 static int v4l2_fwnode_reference_parse_int_props(
880 struct device *dev, struct v4l2_async_notifier *notifier,
881 const char *prop, const char * const *props, unsigned int nprops)
883 struct fwnode_handle *fwnode;
889 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
892 if (IS_ERR(fwnode)) {
894 * Note that right now both -ENODATA and -ENOENT may
895 * signal out-of-bounds access. Return the error in
896 * cases other than that.
898 if (PTR_ERR(fwnode) != -ENOENT &&
899 PTR_ERR(fwnode) != -ENODATA)
900 return PTR_ERR(fwnode);
903 fwnode_handle_put(fwnode);
907 for (index = 0; !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(
908 dev_fwnode(dev), prop, index, props,
909 nprops))); index++) {
910 struct v4l2_async_subdev *asd;
912 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
916 /* not an error if asd already exists */
917 if (ret == -EEXIST) {
918 fwnode_handle_put(fwnode);
926 return PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
929 fwnode_handle_put(fwnode);
933 int v4l2_async_notifier_parse_fwnode_sensor_common(
934 struct device *dev, struct v4l2_async_notifier *notifier)
936 static const char * const led_props[] = { "led" };
937 static const struct {
939 const char * const *props;
942 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
943 { "lens-focus", NULL, 0 },
947 for (i = 0; i < ARRAY_SIZE(props); i++) {
950 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
951 ret = v4l2_fwnode_reference_parse_int_props(
952 dev, notifier, props[i].name,
953 props[i].props, props[i].nprops);
955 ret = v4l2_fwnode_reference_parse(
956 dev, notifier, props[i].name);
957 if (ret && ret != -ENOENT) {
958 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
966 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
968 int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
970 struct v4l2_async_notifier *notifier;
973 if (WARN_ON(!sd->dev))
976 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
980 v4l2_async_notifier_init(notifier);
982 ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
987 ret = v4l2_async_subdev_notifier_register(sd, notifier);
991 ret = v4l2_async_register_subdev(sd);
995 sd->subdev_notifier = notifier;
1000 v4l2_async_notifier_unregister(notifier);
1003 v4l2_async_notifier_cleanup(notifier);
1008 EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
1010 int v4l2_async_register_fwnode_subdev(
1011 struct v4l2_subdev *sd, size_t asd_struct_size,
1012 unsigned int *ports, unsigned int num_ports,
1013 int (*parse_endpoint)(struct device *dev,
1014 struct v4l2_fwnode_endpoint *vep,
1015 struct v4l2_async_subdev *asd))
1017 struct v4l2_async_notifier *notifier;
1018 struct device *dev = sd->dev;
1019 struct fwnode_handle *fwnode;
1025 fwnode = dev_fwnode(dev);
1026 if (!fwnode_device_is_available(fwnode))
1029 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1033 v4l2_async_notifier_init(notifier);
1036 ret = v4l2_async_notifier_parse_fwnode_endpoints(
1037 dev, notifier, asd_struct_size, parse_endpoint);
1043 for (i = 0; i < num_ports; i++) {
1044 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
1045 dev, notifier, asd_struct_size,
1046 ports[i], parse_endpoint);
1052 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1056 ret = v4l2_async_register_subdev(sd);
1058 goto out_unregister;
1060 sd->subdev_notifier = notifier;
1065 v4l2_async_notifier_unregister(notifier);
1067 v4l2_async_notifier_cleanup(notifier);
1072 EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
1074 MODULE_LICENSE("GPL");
1075 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1076 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1077 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");