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;
51 unsigned int flags = 0, lanes_used = 0;
52 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
53 unsigned int num_data_lanes = 0;
58 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY)
59 num_data_lanes = min_t(u32, bus->num_data_lanes,
60 V4L2_FWNODE_CSI2_MAX_DATA_LANES);
62 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
65 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
67 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
70 for (i = 0; i < num_data_lanes; i++) {
71 if (lanes_used & BIT(array[i]))
72 pr_warn("duplicated lane %u in data-lanes\n",
74 lanes_used |= BIT(array[i]);
76 bus->data_lanes[i] = array[i];
77 pr_debug("lane %u position %u\n", i, array[i]);
81 rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL,
84 if (rval != 1 + num_data_lanes /* clock+data */) {
85 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
86 1 + num_data_lanes, rval);
90 fwnode_property_read_u32_array(fwnode, "lane-polarities", array,
93 for (i = 0; i < 1 + num_data_lanes; i++) {
94 bus->lane_polarities[i] = array[i];
95 pr_debug("lane %u polarity %sinverted",
96 i, array[i] ? "" : "not ");
99 pr_debug("no lane polarities defined, assuming not inverted\n");
102 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
103 if (lanes_used & BIT(v))
104 pr_warn("duplicated lane %u in clock-lanes\n", v);
105 lanes_used |= BIT(v);
108 have_clk_lane = true;
109 pr_debug("clock lane position %u\n", v);
112 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
113 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
114 pr_debug("non-continuous clock\n");
116 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
119 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY || lanes_used ||
120 have_clk_lane || (flags & ~V4L2_MBUS_CSI2_CONTINUOUS_CLOCK)) {
122 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
123 bus->num_data_lanes = num_data_lanes;
129 #define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
130 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
131 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
132 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
133 V4L2_MBUS_FIELD_EVEN_HIGH | \
134 V4L2_MBUS_FIELD_EVEN_LOW)
136 static void v4l2_fwnode_endpoint_parse_parallel_bus(
137 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep,
138 enum v4l2_fwnode_bus_type bus_type)
140 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
141 unsigned int flags = 0;
144 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
145 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
146 V4L2_MBUS_HSYNC_ACTIVE_LOW;
147 pr_debug("hsync-active %s\n", v ? "high" : "low");
150 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
151 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
152 V4L2_MBUS_VSYNC_ACTIVE_LOW;
153 pr_debug("vsync-active %s\n", v ? "high" : "low");
156 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
157 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
158 V4L2_MBUS_FIELD_EVEN_LOW;
159 pr_debug("field-even-active %s\n", v ? "high" : "low");
162 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
163 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
164 V4L2_MBUS_PCLK_SAMPLE_FALLING;
165 pr_debug("pclk-sample %s\n", v ? "high" : "low");
168 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
169 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
170 V4L2_MBUS_DATA_ACTIVE_LOW;
171 pr_debug("data-active %s\n", v ? "high" : "low");
174 if (fwnode_property_present(fwnode, "slave-mode")) {
175 pr_debug("slave mode\n");
176 flags |= V4L2_MBUS_SLAVE;
178 flags |= V4L2_MBUS_MASTER;
181 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
183 pr_debug("bus-width %u\n", v);
186 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
188 pr_debug("data-shift %u\n", v);
191 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
192 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
193 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
194 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
197 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
198 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
199 V4L2_MBUS_DATA_ENABLE_LOW;
200 pr_debug("data-enable-active %s\n", v ? "high" : "low");
206 if (flags & PARALLEL_MBUS_FLAGS)
207 vep->bus_type = V4L2_MBUS_PARALLEL;
209 vep->bus_type = V4L2_MBUS_BT656;
211 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
212 vep->bus_type = V4L2_MBUS_PARALLEL;
215 case V4L2_FWNODE_BUS_TYPE_BT656:
216 vep->bus_type = V4L2_MBUS_BT656;
217 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
223 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
224 struct v4l2_fwnode_endpoint *vep,
225 enum v4l2_fwnode_bus_type bus_type)
227 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
230 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
232 pr_debug("clock-inv %u\n", v);
235 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
237 pr_debug("strobe %u\n", v);
240 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
242 pr_debug("data-lanes %u\n", v);
245 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
247 pr_debug("clock-lanes %u\n", v);
250 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
251 vep->bus_type = V4L2_MBUS_CCP2;
253 vep->bus_type = V4L2_MBUS_CSI1;
256 static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
257 struct v4l2_fwnode_endpoint *vep)
262 pr_debug("===== begin V4L2 endpoint properties\n");
264 fwnode_graph_parse_endpoint(fwnode, &vep->base);
266 /* Zero fields from bus_type to until the end */
267 memset(&vep->bus_type, 0, sizeof(*vep) -
268 offsetof(typeof(*vep), bus_type));
270 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
273 case V4L2_FWNODE_BUS_TYPE_GUESS:
274 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
279 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
280 v4l2_fwnode_endpoint_parse_parallel_bus(
281 fwnode, vep, V4L2_MBUS_UNKNOWN);
284 case V4L2_FWNODE_BUS_TYPE_CCP2:
285 case V4L2_FWNODE_BUS_TYPE_CSI1:
286 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
289 case V4L2_FWNODE_BUS_TYPE_CSI2_DPHY:
290 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
291 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
297 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
298 case V4L2_FWNODE_BUS_TYPE_BT656:
299 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep, bus_type);
303 pr_warn("unsupported bus type %u\n", bus_type);
310 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
311 struct v4l2_fwnode_endpoint *vep)
315 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
317 pr_debug("===== end V4L2 endpoint properties\n");
321 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
323 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
325 if (IS_ERR_OR_NULL(vep))
328 kfree(vep->link_frequencies);
330 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
332 int v4l2_fwnode_endpoint_alloc_parse(
333 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
337 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
341 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
346 vep->link_frequencies =
347 kmalloc_array(rval, sizeof(*vep->link_frequencies),
349 if (!vep->link_frequencies)
352 vep->nr_of_link_frequencies = rval;
354 rval = fwnode_property_read_u64_array(
355 fwnode, "link-frequencies", vep->link_frequencies,
356 vep->nr_of_link_frequencies);
358 v4l2_fwnode_endpoint_free(vep);
362 for (i = 0; i < vep->nr_of_link_frequencies; i++)
363 pr_info("link-frequencies %u value %llu\n", i,
364 vep->link_frequencies[i]);
367 pr_debug("===== end V4L2 endpoint properties\n");
371 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
373 int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
374 struct v4l2_fwnode_link *link)
376 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
377 struct fwnode_handle *fwnode;
379 memset(link, 0, sizeof(*link));
381 fwnode = fwnode_get_parent(__fwnode);
382 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
383 fwnode = fwnode_get_next_parent(fwnode);
384 if (is_of_node(fwnode) &&
385 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
386 fwnode = fwnode_get_next_parent(fwnode);
387 link->local_node = fwnode;
389 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
391 fwnode_handle_put(fwnode);
395 fwnode = fwnode_get_parent(fwnode);
396 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
397 fwnode = fwnode_get_next_parent(fwnode);
398 if (is_of_node(fwnode) &&
399 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
400 fwnode = fwnode_get_next_parent(fwnode);
401 link->remote_node = fwnode;
405 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
407 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
409 fwnode_handle_put(link->local_node);
410 fwnode_handle_put(link->remote_node);
412 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
414 static int v4l2_async_notifier_fwnode_parse_endpoint(
415 struct device *dev, struct v4l2_async_notifier *notifier,
416 struct fwnode_handle *endpoint, unsigned int asd_struct_size,
417 int (*parse_endpoint)(struct device *dev,
418 struct v4l2_fwnode_endpoint *vep,
419 struct v4l2_async_subdev *asd))
421 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
422 struct v4l2_async_subdev *asd;
425 asd = kzalloc(asd_struct_size, GFP_KERNEL);
429 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
431 fwnode_graph_get_remote_port_parent(endpoint);
432 if (!asd->match.fwnode) {
433 dev_warn(dev, "bad remote port parent\n");
438 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
440 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
445 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
446 if (ret == -ENOTCONN)
447 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
451 "driver could not parse port@%u/endpoint@%u (%d)\n",
452 vep.base.port, vep.base.id, ret);
453 v4l2_fwnode_endpoint_free(&vep);
457 ret = v4l2_async_notifier_add_subdev(notifier, asd);
459 /* not an error if asd already exists */
468 fwnode_handle_put(asd->match.fwnode);
471 return ret == -ENOTCONN ? 0 : ret;
474 static int __v4l2_async_notifier_parse_fwnode_endpoints(
475 struct device *dev, struct v4l2_async_notifier *notifier,
476 size_t asd_struct_size, unsigned int port, bool has_port,
477 int (*parse_endpoint)(struct device *dev,
478 struct v4l2_fwnode_endpoint *vep,
479 struct v4l2_async_subdev *asd))
481 struct fwnode_handle *fwnode;
484 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
487 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
488 struct fwnode_handle *dev_fwnode;
491 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
492 is_available = fwnode_device_is_available(dev_fwnode);
493 fwnode_handle_put(dev_fwnode);
498 struct fwnode_endpoint ep;
500 ret = fwnode_graph_parse_endpoint(fwnode, &ep);
508 ret = v4l2_async_notifier_fwnode_parse_endpoint(
509 dev, notifier, fwnode, asd_struct_size, parse_endpoint);
514 fwnode_handle_put(fwnode);
519 int v4l2_async_notifier_parse_fwnode_endpoints(
520 struct device *dev, struct v4l2_async_notifier *notifier,
521 size_t asd_struct_size,
522 int (*parse_endpoint)(struct device *dev,
523 struct v4l2_fwnode_endpoint *vep,
524 struct v4l2_async_subdev *asd))
526 return __v4l2_async_notifier_parse_fwnode_endpoints(
527 dev, notifier, asd_struct_size, 0, false, parse_endpoint);
529 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
531 int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
532 struct device *dev, struct v4l2_async_notifier *notifier,
533 size_t asd_struct_size, unsigned int port,
534 int (*parse_endpoint)(struct device *dev,
535 struct v4l2_fwnode_endpoint *vep,
536 struct v4l2_async_subdev *asd))
538 return __v4l2_async_notifier_parse_fwnode_endpoints(
539 dev, notifier, asd_struct_size, port, true, parse_endpoint);
541 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
544 * v4l2_fwnode_reference_parse - parse references for async sub-devices
545 * @dev: the device node the properties of which are parsed for references
546 * @notifier: the async notifier where the async subdevs will be added
547 * @prop: the name of the property
549 * Return: 0 on success
550 * -ENOENT if no entries were found
551 * -ENOMEM if memory allocation failed
552 * -EINVAL if property parsing failed
554 static int v4l2_fwnode_reference_parse(
555 struct device *dev, struct v4l2_async_notifier *notifier,
558 struct fwnode_reference_args args;
563 !(ret = fwnode_property_get_reference_args(
564 dev_fwnode(dev), prop, NULL, 0, index, &args));
566 fwnode_handle_put(args.fwnode);
572 * Note that right now both -ENODATA and -ENOENT may signal
573 * out-of-bounds access. Return the error in cases other than that.
575 if (ret != -ENOENT && ret != -ENODATA)
578 for (index = 0; !fwnode_property_get_reference_args(
579 dev_fwnode(dev), prop, NULL, 0, index, &args);
581 struct v4l2_async_subdev *asd;
583 asd = v4l2_async_notifier_add_fwnode_subdev(
584 notifier, args.fwnode, sizeof(*asd));
587 /* not an error if asd already exists */
588 if (ret == -EEXIST) {
589 fwnode_handle_put(args.fwnode);
600 fwnode_handle_put(args.fwnode);
605 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
607 * @fwnode: fwnode to read @prop from
608 * @notifier: notifier for @dev
609 * @prop: the name of the property
610 * @index: the index of the reference to get
611 * @props: the array of integer property names
612 * @nprops: the number of integer property names in @nprops
614 * First find an fwnode referred to by the reference at @index in @prop.
616 * Then under that fwnode, @nprops times, for each property in @props,
617 * iteratively follow child nodes starting from fwnode such that they have the
618 * property in @props array at the index of the child node distance from the
619 * root node and the value of that property matching with the integer argument
620 * of the reference, at the same index.
622 * The child fwnode reched at the end of the iteration is then returned to the
625 * The core reason for this is that you cannot refer to just any node in ACPI.
626 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
627 * provide a list of (property name, property value) tuples where each tuple
628 * uniquely identifies a child node. The first tuple identifies a child directly
629 * underneath the device fwnode, the next tuple identifies a child node
630 * underneath the fwnode identified by the previous tuple, etc. until you
631 * reached the fwnode you need.
633 * An example with a graph, as defined in Documentation/acpi/dsd/graph.txt:
635 * Scope (\_SB.PCI0.I2C2)
639 * Name (_DSD, Package () {
640 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
644 * Package () { "nokia,smia" }
647 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
649 * Package () { "port0", "PRT0" },
652 * Name (PRT0, Package() {
653 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
655 * Package () { "port", 0 },
657 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
659 * Package () { "endpoint0", "EP00" },
662 * Name (EP00, Package() {
663 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
665 * Package () { "endpoint", 0 },
669 * \_SB.PCI0.ISP, 4, 0
681 * Name (_DSD, Package () {
682 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
684 * Package () { "port4", "PRT4" },
688 * Name (PRT4, Package() {
689 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
691 * Package () { "port", 4 },
693 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
695 * Package () { "endpoint0", "EP40" },
699 * Name (EP40, Package() {
700 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
702 * Package () { "endpoint", 0 },
706 * \_SB.PCI0.I2C2.CAM0,
715 * From the EP40 node under ISP device, you could parse the graph remote
716 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
718 * @fwnode: fwnode referring to EP40 under ISP.
719 * @prop: "remote-endpoint"
721 * @props: "port", "endpoint"
724 * And you'd get back fwnode referring to EP00 under CAM0.
726 * The same works the other way around: if you use EP00 under CAM0 as the
727 * fwnode, you'll get fwnode referring to EP40 under ISP.
729 * The same example in DT syntax would look like this:
732 * compatible = "nokia,smia";
738 * remote-endpoint = <&isp 4 0>;
749 * remote-endpoint = <&cam 0 0>;
755 * Return: 0 on success
756 * -ENOENT if no entries (or the property itself) were found
757 * -EINVAL if property parsing otherwise failed
758 * -ENOMEM if memory allocation failed
760 static struct fwnode_handle *v4l2_fwnode_reference_get_int_prop(
761 struct fwnode_handle *fwnode, const char *prop, unsigned int index,
762 const char * const *props, unsigned int nprops)
764 struct fwnode_reference_args fwnode_args;
765 u64 *args = fwnode_args.args;
766 struct fwnode_handle *child;
770 * Obtain remote fwnode as well as the integer arguments.
772 * Note that right now both -ENODATA and -ENOENT may signal
773 * out-of-bounds access. Return -ENOENT in that case.
775 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
776 index, &fwnode_args);
778 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
781 * Find a node in the tree under the referred fwnode corresponding to
782 * the integer arguments.
784 fwnode = fwnode_args.fwnode;
788 /* Loop over all child nodes under fwnode. */
789 fwnode_for_each_child_node(fwnode, child) {
790 if (fwnode_property_read_u32(child, *props, &val))
793 /* Found property, see if its value matches. */
798 fwnode_handle_put(fwnode);
800 /* No property found; return an error here. */
802 fwnode = ERR_PTR(-ENOENT);
815 * v4l2_fwnode_reference_parse_int_props - parse references for async
817 * @dev: struct device pointer
818 * @notifier: notifier for @dev
819 * @prop: the name of the property
820 * @props: the array of integer property names
821 * @nprops: the number of integer properties
823 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
824 * property @prop with integer arguments with child nodes matching in properties
825 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
828 * While it is technically possible to use this function on DT, it is only
829 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
830 * on ACPI the references are limited to devices.
832 * Return: 0 on success
833 * -ENOENT if no entries (or the property itself) were found
834 * -EINVAL if property parsing otherwisefailed
835 * -ENOMEM if memory allocation failed
837 static int v4l2_fwnode_reference_parse_int_props(
838 struct device *dev, struct v4l2_async_notifier *notifier,
839 const char *prop, const char * const *props, unsigned int nprops)
841 struct fwnode_handle *fwnode;
847 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
850 if (IS_ERR(fwnode)) {
852 * Note that right now both -ENODATA and -ENOENT may
853 * signal out-of-bounds access. Return the error in
854 * cases other than that.
856 if (PTR_ERR(fwnode) != -ENOENT &&
857 PTR_ERR(fwnode) != -ENODATA)
858 return PTR_ERR(fwnode);
861 fwnode_handle_put(fwnode);
865 for (index = 0; !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(
866 dev_fwnode(dev), prop, index, props,
867 nprops))); index++) {
868 struct v4l2_async_subdev *asd;
870 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
874 /* not an error if asd already exists */
875 if (ret == -EEXIST) {
876 fwnode_handle_put(fwnode);
884 return PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
887 fwnode_handle_put(fwnode);
891 int v4l2_async_notifier_parse_fwnode_sensor_common(
892 struct device *dev, struct v4l2_async_notifier *notifier)
894 static const char * const led_props[] = { "led" };
895 static const struct {
897 const char * const *props;
900 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
901 { "lens-focus", NULL, 0 },
905 for (i = 0; i < ARRAY_SIZE(props); i++) {
908 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
909 ret = v4l2_fwnode_reference_parse_int_props(
910 dev, notifier, props[i].name,
911 props[i].props, props[i].nprops);
913 ret = v4l2_fwnode_reference_parse(
914 dev, notifier, props[i].name);
915 if (ret && ret != -ENOENT) {
916 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
924 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
926 int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
928 struct v4l2_async_notifier *notifier;
931 if (WARN_ON(!sd->dev))
934 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
938 v4l2_async_notifier_init(notifier);
940 ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
945 ret = v4l2_async_subdev_notifier_register(sd, notifier);
949 ret = v4l2_async_register_subdev(sd);
953 sd->subdev_notifier = notifier;
958 v4l2_async_notifier_unregister(notifier);
961 v4l2_async_notifier_cleanup(notifier);
966 EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
968 int v4l2_async_register_fwnode_subdev(
969 struct v4l2_subdev *sd, size_t asd_struct_size,
970 unsigned int *ports, unsigned int num_ports,
971 int (*parse_endpoint)(struct device *dev,
972 struct v4l2_fwnode_endpoint *vep,
973 struct v4l2_async_subdev *asd))
975 struct v4l2_async_notifier *notifier;
976 struct device *dev = sd->dev;
977 struct fwnode_handle *fwnode;
983 fwnode = dev_fwnode(dev);
984 if (!fwnode_device_is_available(fwnode))
987 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
991 v4l2_async_notifier_init(notifier);
994 ret = v4l2_async_notifier_parse_fwnode_endpoints(
995 dev, notifier, asd_struct_size, parse_endpoint);
1001 for (i = 0; i < num_ports; i++) {
1002 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
1003 dev, notifier, asd_struct_size,
1004 ports[i], parse_endpoint);
1010 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1014 ret = v4l2_async_register_subdev(sd);
1016 goto out_unregister;
1018 sd->subdev_notifier = notifier;
1023 v4l2_async_notifier_unregister(notifier);
1025 v4l2_async_notifier_cleanup(notifier);
1030 EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
1032 MODULE_LICENSE("GPL");
1033 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1034 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1035 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");