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 const struct v4l2_fwnode_bus_conv {
46 enum v4l2_fwnode_bus_type fwnode_bus_type;
47 enum v4l2_mbus_type mbus_type;
51 V4L2_FWNODE_BUS_TYPE_GUESS,
55 V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
59 V4L2_FWNODE_BUS_TYPE_CSI1,
63 V4L2_FWNODE_BUS_TYPE_CCP2,
65 "compact camera port 2",
67 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
71 V4L2_FWNODE_BUS_TYPE_PARALLEL,
75 V4L2_FWNODE_BUS_TYPE_BT656,
81 static const struct v4l2_fwnode_bus_conv *
82 get_v4l2_fwnode_bus_conv_by_fwnode_bus(enum v4l2_fwnode_bus_type type)
86 for (i = 0; i < ARRAY_SIZE(buses); i++)
87 if (buses[i].fwnode_bus_type == type)
93 static enum v4l2_mbus_type
94 v4l2_fwnode_bus_type_to_mbus(enum v4l2_fwnode_bus_type type)
96 const struct v4l2_fwnode_bus_conv *conv =
97 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
99 return conv ? conv->mbus_type : V4L2_MBUS_UNKNOWN;
103 v4l2_fwnode_bus_type_to_string(enum v4l2_fwnode_bus_type type)
105 const struct v4l2_fwnode_bus_conv *conv =
106 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
108 return conv ? conv->name : "not found";
111 static const struct v4l2_fwnode_bus_conv *
112 get_v4l2_fwnode_bus_conv_by_mbus(enum v4l2_mbus_type type)
116 for (i = 0; i < ARRAY_SIZE(buses); i++)
117 if (buses[i].mbus_type == type)
124 v4l2_fwnode_mbus_type_to_string(enum v4l2_mbus_type type)
126 const struct v4l2_fwnode_bus_conv *conv =
127 get_v4l2_fwnode_bus_conv_by_mbus(type);
129 return conv ? conv->name : "not found";
132 static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
133 struct v4l2_fwnode_endpoint *vep,
134 enum v4l2_mbus_type bus_type)
136 struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
137 bool have_clk_lane = false, have_data_lanes = false,
138 have_lane_polarities = false;
139 unsigned int flags = 0, lanes_used = 0;
140 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
142 unsigned int num_data_lanes = 0;
143 bool use_default_lane_mapping = false;
148 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
149 bus_type == V4L2_MBUS_CSI2_CPHY) {
150 use_default_lane_mapping = true;
152 num_data_lanes = min_t(u32, bus->num_data_lanes,
153 V4L2_FWNODE_CSI2_MAX_DATA_LANES);
155 clock_lane = bus->clock_lane;
157 use_default_lane_mapping = false;
159 for (i = 0; i < num_data_lanes; i++) {
160 array[i] = bus->data_lanes[i];
162 use_default_lane_mapping = false;
165 if (use_default_lane_mapping)
166 pr_debug("no lane mapping given, using defaults\n");
169 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
172 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
174 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
177 have_data_lanes = true;
178 if (use_default_lane_mapping) {
179 pr_debug("data-lanes property exists; disabling default mapping\n");
180 use_default_lane_mapping = false;
184 for (i = 0; i < num_data_lanes; i++) {
185 if (lanes_used & BIT(array[i])) {
186 if (have_data_lanes || !use_default_lane_mapping)
187 pr_warn("duplicated lane %u in data-lanes, using defaults\n",
189 use_default_lane_mapping = true;
191 lanes_used |= BIT(array[i]);
194 pr_debug("lane %u position %u\n", i, array[i]);
197 rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL,
200 if (rval != 1 + num_data_lanes /* clock+data */) {
201 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
202 1 + num_data_lanes, rval);
206 have_lane_polarities = true;
209 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
211 pr_debug("clock lane position %u\n", v);
212 have_clk_lane = true;
215 if (lanes_used & BIT(clock_lane)) {
216 if (have_clk_lane || !use_default_lane_mapping)
217 pr_warn("duplicated lane %u in clock-lanes, using defaults\n",
219 use_default_lane_mapping = true;
222 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
223 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
224 pr_debug("non-continuous clock\n");
226 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
229 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
230 bus_type == V4L2_MBUS_CSI2_CPHY || lanes_used ||
231 have_clk_lane || (flags & ~V4L2_MBUS_CSI2_CONTINUOUS_CLOCK)) {
232 /* Only D-PHY has a clock lane. */
233 unsigned int dfl_data_lane_index =
234 bus_type == V4L2_MBUS_CSI2_DPHY;
237 if (bus_type == V4L2_MBUS_UNKNOWN)
238 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
239 bus->num_data_lanes = num_data_lanes;
241 if (use_default_lane_mapping) {
243 for (i = 0; i < num_data_lanes; i++)
244 bus->data_lanes[i] = dfl_data_lane_index + i;
246 bus->clock_lane = clock_lane;
247 for (i = 0; i < num_data_lanes; i++)
248 bus->data_lanes[i] = array[i];
251 if (have_lane_polarities) {
252 fwnode_property_read_u32_array(fwnode,
253 "lane-polarities", array,
256 for (i = 0; i < 1 + num_data_lanes; i++) {
257 bus->lane_polarities[i] = array[i];
258 pr_debug("lane %u polarity %sinverted",
259 i, array[i] ? "" : "not ");
262 pr_debug("no lane polarities defined, assuming not inverted\n");
269 #define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
270 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
271 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
272 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
273 V4L2_MBUS_FIELD_EVEN_HIGH | \
274 V4L2_MBUS_FIELD_EVEN_LOW)
277 v4l2_fwnode_endpoint_parse_parallel_bus(struct fwnode_handle *fwnode,
278 struct v4l2_fwnode_endpoint *vep,
279 enum v4l2_mbus_type bus_type)
281 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
282 unsigned int flags = 0;
285 if (bus_type == V4L2_MBUS_PARALLEL || bus_type == V4L2_MBUS_BT656)
288 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
289 flags &= ~(V4L2_MBUS_HSYNC_ACTIVE_HIGH |
290 V4L2_MBUS_HSYNC_ACTIVE_LOW);
291 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
292 V4L2_MBUS_HSYNC_ACTIVE_LOW;
293 pr_debug("hsync-active %s\n", v ? "high" : "low");
296 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
297 flags &= ~(V4L2_MBUS_VSYNC_ACTIVE_HIGH |
298 V4L2_MBUS_VSYNC_ACTIVE_LOW);
299 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
300 V4L2_MBUS_VSYNC_ACTIVE_LOW;
301 pr_debug("vsync-active %s\n", v ? "high" : "low");
304 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
305 flags &= ~(V4L2_MBUS_FIELD_EVEN_HIGH |
306 V4L2_MBUS_FIELD_EVEN_LOW);
307 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
308 V4L2_MBUS_FIELD_EVEN_LOW;
309 pr_debug("field-even-active %s\n", v ? "high" : "low");
312 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
313 flags &= ~(V4L2_MBUS_PCLK_SAMPLE_RISING |
314 V4L2_MBUS_PCLK_SAMPLE_FALLING);
315 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
316 V4L2_MBUS_PCLK_SAMPLE_FALLING;
317 pr_debug("pclk-sample %s\n", v ? "high" : "low");
320 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
321 flags &= ~(V4L2_MBUS_DATA_ACTIVE_HIGH |
322 V4L2_MBUS_DATA_ACTIVE_LOW);
323 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
324 V4L2_MBUS_DATA_ACTIVE_LOW;
325 pr_debug("data-active %s\n", v ? "high" : "low");
328 if (fwnode_property_present(fwnode, "slave-mode")) {
329 pr_debug("slave mode\n");
330 flags &= ~V4L2_MBUS_MASTER;
331 flags |= V4L2_MBUS_SLAVE;
333 flags &= ~V4L2_MBUS_SLAVE;
334 flags |= V4L2_MBUS_MASTER;
337 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
339 pr_debug("bus-width %u\n", v);
342 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
344 pr_debug("data-shift %u\n", v);
347 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
348 flags &= ~(V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH |
349 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW);
350 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
351 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
352 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
355 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
356 flags &= ~(V4L2_MBUS_DATA_ENABLE_HIGH |
357 V4L2_MBUS_DATA_ENABLE_LOW);
358 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
359 V4L2_MBUS_DATA_ENABLE_LOW;
360 pr_debug("data-enable-active %s\n", v ? "high" : "low");
366 if (flags & PARALLEL_MBUS_FLAGS)
367 vep->bus_type = V4L2_MBUS_PARALLEL;
369 vep->bus_type = V4L2_MBUS_BT656;
371 case V4L2_MBUS_PARALLEL:
372 vep->bus_type = V4L2_MBUS_PARALLEL;
375 case V4L2_MBUS_BT656:
376 vep->bus_type = V4L2_MBUS_BT656;
377 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
383 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
384 struct v4l2_fwnode_endpoint *vep,
385 enum v4l2_mbus_type bus_type)
387 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
390 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
392 pr_debug("clock-inv %u\n", v);
395 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
397 pr_debug("strobe %u\n", v);
400 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
402 pr_debug("data-lanes %u\n", v);
405 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
407 pr_debug("clock-lanes %u\n", v);
410 if (bus_type == V4L2_MBUS_CCP2)
411 vep->bus_type = V4L2_MBUS_CCP2;
413 vep->bus_type = V4L2_MBUS_CSI1;
416 static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
417 struct v4l2_fwnode_endpoint *vep)
419 u32 bus_type = V4L2_FWNODE_BUS_TYPE_GUESS;
420 enum v4l2_mbus_type mbus_type;
423 if (vep->bus_type == V4L2_MBUS_UNKNOWN) {
424 /* Zero fields from bus union to until the end */
426 sizeof(*vep) - offsetof(typeof(*vep), bus));
429 pr_debug("===== begin V4L2 endpoint properties\n");
432 * Zero the fwnode graph endpoint memory in case we don't end up parsing
435 memset(&vep->base, 0, sizeof(vep->base));
437 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
438 pr_debug("fwnode video bus type %s (%u), mbus type %s (%u)\n",
439 v4l2_fwnode_bus_type_to_string(bus_type), bus_type,
440 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
442 mbus_type = v4l2_fwnode_bus_type_to_mbus(bus_type);
444 if (vep->bus_type != V4L2_MBUS_UNKNOWN) {
445 if (mbus_type != V4L2_MBUS_UNKNOWN &&
446 vep->bus_type != mbus_type) {
447 pr_debug("expecting bus type %s\n",
448 v4l2_fwnode_mbus_type_to_string(vep->bus_type));
452 vep->bus_type = mbus_type;
455 switch (vep->bus_type) {
456 case V4L2_MBUS_UNKNOWN:
457 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
462 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
463 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
466 pr_debug("assuming media bus type %s (%u)\n",
467 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
473 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, vep->bus_type);
476 case V4L2_MBUS_CSI2_DPHY:
477 case V4L2_MBUS_CSI2_CPHY:
478 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
484 case V4L2_MBUS_PARALLEL:
485 case V4L2_MBUS_BT656:
486 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
491 pr_warn("unsupported bus type %u\n", mbus_type);
495 fwnode_graph_parse_endpoint(fwnode, &vep->base);
500 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
501 struct v4l2_fwnode_endpoint *vep)
505 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
507 pr_debug("===== end V4L2 endpoint properties\n");
511 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
513 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
515 if (IS_ERR_OR_NULL(vep))
518 kfree(vep->link_frequencies);
520 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
522 int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
523 struct v4l2_fwnode_endpoint *vep)
527 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
531 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
536 vep->link_frequencies =
537 kmalloc_array(rval, sizeof(*vep->link_frequencies),
539 if (!vep->link_frequencies)
542 vep->nr_of_link_frequencies = rval;
544 rval = fwnode_property_read_u64_array(fwnode,
546 vep->link_frequencies,
547 vep->nr_of_link_frequencies);
549 v4l2_fwnode_endpoint_free(vep);
553 for (i = 0; i < vep->nr_of_link_frequencies; i++)
554 pr_info("link-frequencies %u value %llu\n", i,
555 vep->link_frequencies[i]);
558 pr_debug("===== end V4L2 endpoint properties\n");
562 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
564 int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
565 struct v4l2_fwnode_link *link)
567 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
568 struct fwnode_handle *fwnode;
570 memset(link, 0, sizeof(*link));
572 fwnode = fwnode_get_parent(__fwnode);
573 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
574 fwnode = fwnode_get_next_parent(fwnode);
575 if (is_of_node(fwnode) && of_node_name_eq(to_of_node(fwnode), "ports"))
576 fwnode = fwnode_get_next_parent(fwnode);
577 link->local_node = fwnode;
579 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
581 fwnode_handle_put(fwnode);
585 fwnode = fwnode_get_parent(fwnode);
586 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
587 fwnode = fwnode_get_next_parent(fwnode);
588 if (is_of_node(fwnode) && of_node_name_eq(to_of_node(fwnode), "ports"))
589 fwnode = fwnode_get_next_parent(fwnode);
590 link->remote_node = fwnode;
594 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
596 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
598 fwnode_handle_put(link->local_node);
599 fwnode_handle_put(link->remote_node);
601 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
604 v4l2_async_notifier_fwnode_parse_endpoint(struct device *dev,
605 struct v4l2_async_notifier *notifier,
606 struct fwnode_handle *endpoint,
607 unsigned int asd_struct_size,
608 parse_endpoint_func parse_endpoint)
610 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
611 struct v4l2_async_subdev *asd;
614 asd = kzalloc(asd_struct_size, GFP_KERNEL);
618 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
620 fwnode_graph_get_remote_port_parent(endpoint);
621 if (!asd->match.fwnode) {
622 dev_dbg(dev, "no remote endpoint found\n");
627 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
629 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
634 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
635 if (ret == -ENOTCONN)
636 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
640 "driver could not parse port@%u/endpoint@%u (%d)\n",
641 vep.base.port, vep.base.id, ret);
642 v4l2_fwnode_endpoint_free(&vep);
646 ret = v4l2_async_notifier_add_subdev(notifier, asd);
648 /* not an error if asd already exists */
657 fwnode_handle_put(asd->match.fwnode);
660 return ret == -ENOTCONN ? 0 : ret;
664 __v4l2_async_notifier_parse_fwnode_ep(struct device *dev,
665 struct v4l2_async_notifier *notifier,
666 size_t asd_struct_size,
669 parse_endpoint_func parse_endpoint)
671 struct fwnode_handle *fwnode;
674 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
677 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
678 struct fwnode_handle *dev_fwnode;
681 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
682 is_available = fwnode_device_is_available(dev_fwnode);
683 fwnode_handle_put(dev_fwnode);
688 struct fwnode_endpoint ep;
690 ret = fwnode_graph_parse_endpoint(fwnode, &ep);
698 ret = v4l2_async_notifier_fwnode_parse_endpoint(dev,
707 fwnode_handle_put(fwnode);
713 v4l2_async_notifier_parse_fwnode_endpoints(struct device *dev,
714 struct v4l2_async_notifier *notifier,
715 size_t asd_struct_size,
716 parse_endpoint_func parse_endpoint)
718 return __v4l2_async_notifier_parse_fwnode_ep(dev, notifier,
720 false, parse_endpoint);
722 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
725 v4l2_async_notifier_parse_fwnode_endpoints_by_port(struct device *dev,
726 struct v4l2_async_notifier *notifier,
727 size_t asd_struct_size,
729 parse_endpoint_func parse_endpoint)
731 return __v4l2_async_notifier_parse_fwnode_ep(dev, notifier,
736 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
739 * v4l2_fwnode_reference_parse - parse references for async sub-devices
740 * @dev: the device node the properties of which are parsed for references
741 * @notifier: the async notifier where the async subdevs will be added
742 * @prop: the name of the property
744 * Return: 0 on success
745 * -ENOENT if no entries were found
746 * -ENOMEM if memory allocation failed
747 * -EINVAL if property parsing failed
749 static int v4l2_fwnode_reference_parse(struct device *dev,
750 struct v4l2_async_notifier *notifier,
753 struct fwnode_reference_args args;
758 !(ret = fwnode_property_get_reference_args(dev_fwnode(dev),
762 fwnode_handle_put(args.fwnode);
768 * Note that right now both -ENODATA and -ENOENT may signal
769 * out-of-bounds access. Return the error in cases other than that.
771 if (ret != -ENOENT && ret != -ENODATA)
775 !fwnode_property_get_reference_args(dev_fwnode(dev), prop, NULL,
778 struct v4l2_async_subdev *asd;
780 asd = v4l2_async_notifier_add_fwnode_subdev(notifier,
785 /* not an error if asd already exists */
786 if (ret == -EEXIST) {
787 fwnode_handle_put(args.fwnode);
798 fwnode_handle_put(args.fwnode);
803 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
805 * @fwnode: fwnode to read @prop from
806 * @notifier: notifier for @dev
807 * @prop: the name of the property
808 * @index: the index of the reference to get
809 * @props: the array of integer property names
810 * @nprops: the number of integer property names in @nprops
812 * First find an fwnode referred to by the reference at @index in @prop.
814 * Then under that fwnode, @nprops times, for each property in @props,
815 * iteratively follow child nodes starting from fwnode such that they have the
816 * property in @props array at the index of the child node distance from the
817 * root node and the value of that property matching with the integer argument
818 * of the reference, at the same index.
820 * The child fwnode reached at the end of the iteration is then returned to the
823 * The core reason for this is that you cannot refer to just any node in ACPI.
824 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
825 * provide a list of (property name, property value) tuples where each tuple
826 * uniquely identifies a child node. The first tuple identifies a child directly
827 * underneath the device fwnode, the next tuple identifies a child node
828 * underneath the fwnode identified by the previous tuple, etc. until you
829 * reached the fwnode you need.
831 * THIS EXAMPLE EXISTS MERELY TO DOCUMENT THIS FUNCTION. DO NOT USE IT AS A
832 * REFERENCE IN HOW ACPI TABLES SHOULD BE WRITTEN!! See documentation under
833 * Documentation/acpi/dsd instead and especially graph.txt,
834 * data-node-references.txt and leds.txt .
836 * Scope (\_SB.PCI0.I2C2)
840 * Name (_DSD, Package () {
841 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
845 * Package () { "nokia,smia" }
848 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
850 * Package () { "port0", "PRT0" },
853 * Name (PRT0, Package() {
854 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
856 * Package () { "port", 0 },
858 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
860 * Package () { "endpoint0", "EP00" },
863 * Name (EP00, Package() {
864 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
866 * Package () { "endpoint", 0 },
870 * \_SB.PCI0.ISP, 4, 0
882 * Name (_DSD, Package () {
883 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
885 * Package () { "port4", "PRT4" },
889 * Name (PRT4, Package() {
890 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
892 * Package () { "port", 4 },
894 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
896 * Package () { "endpoint0", "EP40" },
900 * Name (EP40, Package() {
901 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
903 * Package () { "endpoint", 0 },
907 * \_SB.PCI0.I2C2.CAM0,
916 * From the EP40 node under ISP device, you could parse the graph remote
917 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
919 * @fwnode: fwnode referring to EP40 under ISP.
920 * @prop: "remote-endpoint"
922 * @props: "port", "endpoint"
925 * And you'd get back fwnode referring to EP00 under CAM0.
927 * The same works the other way around: if you use EP00 under CAM0 as the
928 * fwnode, you'll get fwnode referring to EP40 under ISP.
930 * The same example in DT syntax would look like this:
933 * compatible = "nokia,smia";
939 * remote-endpoint = <&isp 4 0>;
950 * remote-endpoint = <&cam 0 0>;
956 * Return: 0 on success
957 * -ENOENT if no entries (or the property itself) were found
958 * -EINVAL if property parsing otherwise failed
959 * -ENOMEM if memory allocation failed
961 static struct fwnode_handle *
962 v4l2_fwnode_reference_get_int_prop(struct fwnode_handle *fwnode,
965 const char * const *props,
968 struct fwnode_reference_args fwnode_args;
969 u64 *args = fwnode_args.args;
970 struct fwnode_handle *child;
974 * Obtain remote fwnode as well as the integer arguments.
976 * Note that right now both -ENODATA and -ENOENT may signal
977 * out-of-bounds access. Return -ENOENT in that case.
979 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
980 index, &fwnode_args);
982 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
985 * Find a node in the tree under the referred fwnode corresponding to
986 * the integer arguments.
988 fwnode = fwnode_args.fwnode;
992 /* Loop over all child nodes under fwnode. */
993 fwnode_for_each_child_node(fwnode, child) {
994 if (fwnode_property_read_u32(child, *props, &val))
997 /* Found property, see if its value matches. */
1002 fwnode_handle_put(fwnode);
1004 /* No property found; return an error here. */
1006 fwnode = ERR_PTR(-ENOENT);
1018 struct v4l2_fwnode_int_props {
1020 const char * const *props;
1021 unsigned int nprops;
1025 * v4l2_fwnode_reference_parse_int_props - parse references for async
1027 * @dev: struct device pointer
1028 * @notifier: notifier for @dev
1029 * @prop: the name of the property
1030 * @props: the array of integer property names
1031 * @nprops: the number of integer properties
1033 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
1034 * property @prop with integer arguments with child nodes matching in properties
1035 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
1038 * While it is technically possible to use this function on DT, it is only
1039 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
1040 * on ACPI the references are limited to devices.
1042 * Return: 0 on success
1043 * -ENOENT if no entries (or the property itself) were found
1044 * -EINVAL if property parsing otherwisefailed
1045 * -ENOMEM if memory allocation failed
1048 v4l2_fwnode_reference_parse_int_props(struct device *dev,
1049 struct v4l2_async_notifier *notifier,
1050 const struct v4l2_fwnode_int_props *p)
1052 struct fwnode_handle *fwnode;
1055 const char *prop = p->name;
1056 const char * const *props = p->props;
1057 unsigned int nprops = p->nprops;
1061 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1064 if (IS_ERR(fwnode)) {
1066 * Note that right now both -ENODATA and -ENOENT may
1067 * signal out-of-bounds access. Return the error in
1068 * cases other than that.
1070 if (PTR_ERR(fwnode) != -ENOENT &&
1071 PTR_ERR(fwnode) != -ENODATA)
1072 return PTR_ERR(fwnode);
1075 fwnode_handle_put(fwnode);
1080 !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1085 struct v4l2_async_subdev *asd;
1087 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
1091 /* not an error if asd already exists */
1092 if (ret == -EEXIST) {
1093 fwnode_handle_put(fwnode);
1101 return PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
1104 fwnode_handle_put(fwnode);
1108 int v4l2_async_notifier_parse_fwnode_sensor_common(struct device *dev,
1109 struct v4l2_async_notifier *notifier)
1111 static const char * const led_props[] = { "led" };
1112 static const struct v4l2_fwnode_int_props props[] = {
1113 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
1114 { "lens-focus", NULL, 0 },
1118 for (i = 0; i < ARRAY_SIZE(props); i++) {
1121 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
1122 ret = v4l2_fwnode_reference_parse_int_props(dev,
1126 ret = v4l2_fwnode_reference_parse(dev, notifier,
1128 if (ret && ret != -ENOENT) {
1129 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
1130 props[i].name, ret);
1137 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
1139 int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
1141 struct v4l2_async_notifier *notifier;
1144 if (WARN_ON(!sd->dev))
1147 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1151 v4l2_async_notifier_init(notifier);
1153 ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
1158 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1162 ret = v4l2_async_register_subdev(sd);
1164 goto out_unregister;
1166 sd->subdev_notifier = notifier;
1171 v4l2_async_notifier_unregister(notifier);
1174 v4l2_async_notifier_cleanup(notifier);
1179 EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
1181 int v4l2_async_register_fwnode_subdev(struct v4l2_subdev *sd,
1182 size_t asd_struct_size,
1183 unsigned int *ports,
1184 unsigned int num_ports,
1185 parse_endpoint_func parse_endpoint)
1187 struct v4l2_async_notifier *notifier;
1188 struct device *dev = sd->dev;
1189 struct fwnode_handle *fwnode;
1195 fwnode = dev_fwnode(dev);
1196 if (!fwnode_device_is_available(fwnode))
1199 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1203 v4l2_async_notifier_init(notifier);
1206 ret = v4l2_async_notifier_parse_fwnode_endpoints(dev, notifier,
1214 for (i = 0; i < num_ports; i++) {
1215 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(dev, notifier, asd_struct_size, ports[i], parse_endpoint);
1221 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1225 ret = v4l2_async_register_subdev(sd);
1227 goto out_unregister;
1229 sd->subdev_notifier = notifier;
1234 v4l2_async_notifier_unregister(notifier);
1236 v4l2_async_notifier_cleanup(notifier);
1241 EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
1243 MODULE_LICENSE("GPL");
1244 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1245 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1246 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");