1 // SPDX-License-Identifier: GPL-2.0-only
3 * V4L2 fwnode binding parsing library
5 * The origins of the V4L2 fwnode library are in V4L2 OF library that
6 * formerly was located in v4l2-of.c.
8 * Copyright (c) 2016 Intel Corporation.
9 * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
11 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
12 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
14 * Copyright (C) 2012 Renesas Electronics Corp.
15 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
17 #include <linux/acpi.h>
18 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/property.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/types.h>
27 #include <media/v4l2-async.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-subdev.h>
31 enum v4l2_fwnode_bus_type {
32 V4L2_FWNODE_BUS_TYPE_GUESS = 0,
33 V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
34 V4L2_FWNODE_BUS_TYPE_CSI1,
35 V4L2_FWNODE_BUS_TYPE_CCP2,
36 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
37 V4L2_FWNODE_BUS_TYPE_PARALLEL,
38 V4L2_FWNODE_BUS_TYPE_BT656,
39 NR_OF_V4L2_FWNODE_BUS_TYPE,
42 static const struct v4l2_fwnode_bus_conv {
43 enum v4l2_fwnode_bus_type fwnode_bus_type;
44 enum v4l2_mbus_type mbus_type;
48 V4L2_FWNODE_BUS_TYPE_GUESS,
52 V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
56 V4L2_FWNODE_BUS_TYPE_CSI1,
60 V4L2_FWNODE_BUS_TYPE_CCP2,
62 "compact camera port 2",
64 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
68 V4L2_FWNODE_BUS_TYPE_PARALLEL,
72 V4L2_FWNODE_BUS_TYPE_BT656,
78 static const struct v4l2_fwnode_bus_conv *
79 get_v4l2_fwnode_bus_conv_by_fwnode_bus(enum v4l2_fwnode_bus_type type)
83 for (i = 0; i < ARRAY_SIZE(buses); i++)
84 if (buses[i].fwnode_bus_type == type)
90 static enum v4l2_mbus_type
91 v4l2_fwnode_bus_type_to_mbus(enum v4l2_fwnode_bus_type type)
93 const struct v4l2_fwnode_bus_conv *conv =
94 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
96 return conv ? conv->mbus_type : V4L2_MBUS_UNKNOWN;
100 v4l2_fwnode_bus_type_to_string(enum v4l2_fwnode_bus_type type)
102 const struct v4l2_fwnode_bus_conv *conv =
103 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
105 return conv ? conv->name : "not found";
108 static const struct v4l2_fwnode_bus_conv *
109 get_v4l2_fwnode_bus_conv_by_mbus(enum v4l2_mbus_type type)
113 for (i = 0; i < ARRAY_SIZE(buses); i++)
114 if (buses[i].mbus_type == type)
121 v4l2_fwnode_mbus_type_to_string(enum v4l2_mbus_type type)
123 const struct v4l2_fwnode_bus_conv *conv =
124 get_v4l2_fwnode_bus_conv_by_mbus(type);
126 return conv ? conv->name : "not found";
129 static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
130 struct v4l2_fwnode_endpoint *vep,
131 enum v4l2_mbus_type bus_type)
133 struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
134 bool have_clk_lane = false, have_data_lanes = false,
135 have_lane_polarities = false;
136 unsigned int flags = 0, lanes_used = 0;
137 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
139 unsigned int num_data_lanes = 0;
140 bool use_default_lane_mapping = false;
145 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
146 bus_type == V4L2_MBUS_CSI2_CPHY) {
147 use_default_lane_mapping = true;
149 num_data_lanes = min_t(u32, bus->num_data_lanes,
150 V4L2_FWNODE_CSI2_MAX_DATA_LANES);
152 clock_lane = bus->clock_lane;
154 use_default_lane_mapping = false;
156 for (i = 0; i < num_data_lanes; i++) {
157 array[i] = bus->data_lanes[i];
159 use_default_lane_mapping = false;
162 if (use_default_lane_mapping)
163 pr_debug("no lane mapping given, using defaults\n");
166 rval = fwnode_property_count_u32(fwnode, "data-lanes");
169 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
171 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
174 have_data_lanes = true;
175 if (use_default_lane_mapping) {
176 pr_debug("data-lanes property exists; disabling default mapping\n");
177 use_default_lane_mapping = false;
181 for (i = 0; i < num_data_lanes; i++) {
182 if (lanes_used & BIT(array[i])) {
183 if (have_data_lanes || !use_default_lane_mapping)
184 pr_warn("duplicated lane %u in data-lanes, using defaults\n",
186 use_default_lane_mapping = true;
188 lanes_used |= BIT(array[i]);
191 pr_debug("lane %u position %u\n", i, array[i]);
194 rval = fwnode_property_count_u32(fwnode, "lane-polarities");
196 if (rval != 1 + num_data_lanes /* clock+data */) {
197 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
198 1 + num_data_lanes, rval);
202 have_lane_polarities = true;
205 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
207 pr_debug("clock lane position %u\n", v);
208 have_clk_lane = true;
211 if (have_clk_lane && lanes_used & BIT(clock_lane) &&
212 !use_default_lane_mapping) {
213 pr_warn("duplicated lane %u in clock-lanes, using defaults\n",
215 use_default_lane_mapping = true;
218 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
219 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
220 pr_debug("non-continuous clock\n");
222 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
225 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
226 bus_type == V4L2_MBUS_CSI2_CPHY || lanes_used ||
227 have_clk_lane || (flags & ~V4L2_MBUS_CSI2_CONTINUOUS_CLOCK)) {
228 /* Only D-PHY has a clock lane. */
229 unsigned int dfl_data_lane_index =
230 bus_type == V4L2_MBUS_CSI2_DPHY;
233 if (bus_type == V4L2_MBUS_UNKNOWN)
234 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
235 bus->num_data_lanes = num_data_lanes;
237 if (use_default_lane_mapping) {
239 for (i = 0; i < num_data_lanes; i++)
240 bus->data_lanes[i] = dfl_data_lane_index + i;
242 bus->clock_lane = clock_lane;
243 for (i = 0; i < num_data_lanes; i++)
244 bus->data_lanes[i] = array[i];
247 if (have_lane_polarities) {
248 fwnode_property_read_u32_array(fwnode,
249 "lane-polarities", array,
252 for (i = 0; i < 1 + num_data_lanes; i++) {
253 bus->lane_polarities[i] = array[i];
254 pr_debug("lane %u polarity %sinverted",
255 i, array[i] ? "" : "not ");
258 pr_debug("no lane polarities defined, assuming not inverted\n");
265 #define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
266 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
267 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
268 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
269 V4L2_MBUS_FIELD_EVEN_HIGH | \
270 V4L2_MBUS_FIELD_EVEN_LOW)
273 v4l2_fwnode_endpoint_parse_parallel_bus(struct fwnode_handle *fwnode,
274 struct v4l2_fwnode_endpoint *vep,
275 enum v4l2_mbus_type bus_type)
277 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
278 unsigned int flags = 0;
281 if (bus_type == V4L2_MBUS_PARALLEL || bus_type == V4L2_MBUS_BT656)
284 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
285 flags &= ~(V4L2_MBUS_HSYNC_ACTIVE_HIGH |
286 V4L2_MBUS_HSYNC_ACTIVE_LOW);
287 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
288 V4L2_MBUS_HSYNC_ACTIVE_LOW;
289 pr_debug("hsync-active %s\n", v ? "high" : "low");
292 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
293 flags &= ~(V4L2_MBUS_VSYNC_ACTIVE_HIGH |
294 V4L2_MBUS_VSYNC_ACTIVE_LOW);
295 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
296 V4L2_MBUS_VSYNC_ACTIVE_LOW;
297 pr_debug("vsync-active %s\n", v ? "high" : "low");
300 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
301 flags &= ~(V4L2_MBUS_FIELD_EVEN_HIGH |
302 V4L2_MBUS_FIELD_EVEN_LOW);
303 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
304 V4L2_MBUS_FIELD_EVEN_LOW;
305 pr_debug("field-even-active %s\n", v ? "high" : "low");
308 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
309 flags &= ~(V4L2_MBUS_PCLK_SAMPLE_RISING |
310 V4L2_MBUS_PCLK_SAMPLE_FALLING);
311 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
312 V4L2_MBUS_PCLK_SAMPLE_FALLING;
313 pr_debug("pclk-sample %s\n", v ? "high" : "low");
316 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
317 flags &= ~(V4L2_MBUS_DATA_ACTIVE_HIGH |
318 V4L2_MBUS_DATA_ACTIVE_LOW);
319 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
320 V4L2_MBUS_DATA_ACTIVE_LOW;
321 pr_debug("data-active %s\n", v ? "high" : "low");
324 if (fwnode_property_present(fwnode, "slave-mode")) {
325 pr_debug("slave mode\n");
326 flags &= ~V4L2_MBUS_MASTER;
327 flags |= V4L2_MBUS_SLAVE;
329 flags &= ~V4L2_MBUS_SLAVE;
330 flags |= V4L2_MBUS_MASTER;
333 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
335 pr_debug("bus-width %u\n", v);
338 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
340 pr_debug("data-shift %u\n", v);
343 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
344 flags &= ~(V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH |
345 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW);
346 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
347 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
348 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
351 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
352 flags &= ~(V4L2_MBUS_DATA_ENABLE_HIGH |
353 V4L2_MBUS_DATA_ENABLE_LOW);
354 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
355 V4L2_MBUS_DATA_ENABLE_LOW;
356 pr_debug("data-enable-active %s\n", v ? "high" : "low");
362 if (flags & PARALLEL_MBUS_FLAGS)
363 vep->bus_type = V4L2_MBUS_PARALLEL;
365 vep->bus_type = V4L2_MBUS_BT656;
367 case V4L2_MBUS_PARALLEL:
368 vep->bus_type = V4L2_MBUS_PARALLEL;
371 case V4L2_MBUS_BT656:
372 vep->bus_type = V4L2_MBUS_BT656;
373 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
379 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
380 struct v4l2_fwnode_endpoint *vep,
381 enum v4l2_mbus_type bus_type)
383 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
386 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
388 pr_debug("clock-inv %u\n", v);
391 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
393 pr_debug("strobe %u\n", v);
396 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
398 pr_debug("data-lanes %u\n", v);
401 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
403 pr_debug("clock-lanes %u\n", v);
406 if (bus_type == V4L2_MBUS_CCP2)
407 vep->bus_type = V4L2_MBUS_CCP2;
409 vep->bus_type = V4L2_MBUS_CSI1;
412 static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
413 struct v4l2_fwnode_endpoint *vep)
415 u32 bus_type = V4L2_FWNODE_BUS_TYPE_GUESS;
416 enum v4l2_mbus_type mbus_type;
419 if (vep->bus_type == V4L2_MBUS_UNKNOWN) {
420 /* Zero fields from bus union to until the end */
422 sizeof(*vep) - offsetof(typeof(*vep), bus));
425 pr_debug("===== begin V4L2 endpoint properties\n");
428 * Zero the fwnode graph endpoint memory in case we don't end up parsing
431 memset(&vep->base, 0, sizeof(vep->base));
433 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
434 pr_debug("fwnode video bus type %s (%u), mbus type %s (%u)\n",
435 v4l2_fwnode_bus_type_to_string(bus_type), bus_type,
436 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
438 mbus_type = v4l2_fwnode_bus_type_to_mbus(bus_type);
440 if (vep->bus_type != V4L2_MBUS_UNKNOWN) {
441 if (mbus_type != V4L2_MBUS_UNKNOWN &&
442 vep->bus_type != mbus_type) {
443 pr_debug("expecting bus type %s\n",
444 v4l2_fwnode_mbus_type_to_string(vep->bus_type));
448 vep->bus_type = mbus_type;
451 switch (vep->bus_type) {
452 case V4L2_MBUS_UNKNOWN:
453 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
458 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
459 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
462 pr_debug("assuming media bus type %s (%u)\n",
463 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
469 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, vep->bus_type);
472 case V4L2_MBUS_CSI2_DPHY:
473 case V4L2_MBUS_CSI2_CPHY:
474 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
480 case V4L2_MBUS_PARALLEL:
481 case V4L2_MBUS_BT656:
482 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
487 pr_warn("unsupported bus type %u\n", mbus_type);
491 fwnode_graph_parse_endpoint(fwnode, &vep->base);
496 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
497 struct v4l2_fwnode_endpoint *vep)
501 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
503 pr_debug("===== end V4L2 endpoint properties\n");
507 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
509 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
511 if (IS_ERR_OR_NULL(vep))
514 kfree(vep->link_frequencies);
516 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
518 int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
519 struct v4l2_fwnode_endpoint *vep)
523 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
527 rval = fwnode_property_count_u64(fwnode, "link-frequencies");
531 vep->link_frequencies =
532 kmalloc_array(rval, sizeof(*vep->link_frequencies),
534 if (!vep->link_frequencies)
537 vep->nr_of_link_frequencies = rval;
539 rval = fwnode_property_read_u64_array(fwnode,
541 vep->link_frequencies,
542 vep->nr_of_link_frequencies);
544 v4l2_fwnode_endpoint_free(vep);
548 for (i = 0; i < vep->nr_of_link_frequencies; i++)
549 pr_info("link-frequencies %u value %llu\n", i,
550 vep->link_frequencies[i]);
553 pr_debug("===== end V4L2 endpoint properties\n");
557 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
559 int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
560 struct v4l2_fwnode_link *link)
562 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
563 struct fwnode_handle *fwnode;
565 memset(link, 0, sizeof(*link));
567 fwnode = fwnode_get_parent(__fwnode);
568 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
569 fwnode = fwnode_get_next_parent(fwnode);
570 if (is_of_node(fwnode) && of_node_name_eq(to_of_node(fwnode), "ports"))
571 fwnode = fwnode_get_next_parent(fwnode);
572 link->local_node = fwnode;
574 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
576 fwnode_handle_put(fwnode);
580 fwnode = fwnode_get_parent(fwnode);
581 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
582 fwnode = fwnode_get_next_parent(fwnode);
583 if (is_of_node(fwnode) && of_node_name_eq(to_of_node(fwnode), "ports"))
584 fwnode = fwnode_get_next_parent(fwnode);
585 link->remote_node = fwnode;
589 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
591 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
593 fwnode_handle_put(link->local_node);
594 fwnode_handle_put(link->remote_node);
596 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
599 v4l2_async_notifier_fwnode_parse_endpoint(struct device *dev,
600 struct v4l2_async_notifier *notifier,
601 struct fwnode_handle *endpoint,
602 unsigned int asd_struct_size,
603 parse_endpoint_func parse_endpoint)
605 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
606 struct v4l2_async_subdev *asd;
609 asd = kzalloc(asd_struct_size, GFP_KERNEL);
613 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
615 fwnode_graph_get_remote_port_parent(endpoint);
616 if (!asd->match.fwnode) {
617 dev_dbg(dev, "no remote endpoint found\n");
622 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
624 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
629 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
630 if (ret == -ENOTCONN)
631 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
635 "driver could not parse port@%u/endpoint@%u (%d)\n",
636 vep.base.port, vep.base.id, ret);
637 v4l2_fwnode_endpoint_free(&vep);
641 ret = v4l2_async_notifier_add_subdev(notifier, asd);
643 /* not an error if asd already exists */
652 fwnode_handle_put(asd->match.fwnode);
655 return ret == -ENOTCONN ? 0 : ret;
659 __v4l2_async_notifier_parse_fwnode_ep(struct device *dev,
660 struct v4l2_async_notifier *notifier,
661 size_t asd_struct_size,
664 parse_endpoint_func parse_endpoint)
666 struct fwnode_handle *fwnode;
669 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
672 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
673 struct fwnode_handle *dev_fwnode;
676 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
677 is_available = fwnode_device_is_available(dev_fwnode);
678 fwnode_handle_put(dev_fwnode);
683 struct fwnode_endpoint ep;
685 ret = fwnode_graph_parse_endpoint(fwnode, &ep);
693 ret = v4l2_async_notifier_fwnode_parse_endpoint(dev,
702 fwnode_handle_put(fwnode);
708 v4l2_async_notifier_parse_fwnode_endpoints(struct device *dev,
709 struct v4l2_async_notifier *notifier,
710 size_t asd_struct_size,
711 parse_endpoint_func parse_endpoint)
713 return __v4l2_async_notifier_parse_fwnode_ep(dev, notifier,
715 false, parse_endpoint);
717 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
720 v4l2_async_notifier_parse_fwnode_endpoints_by_port(struct device *dev,
721 struct v4l2_async_notifier *notifier,
722 size_t asd_struct_size,
724 parse_endpoint_func parse_endpoint)
726 return __v4l2_async_notifier_parse_fwnode_ep(dev, notifier,
731 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
734 * v4l2_fwnode_reference_parse - parse references for async sub-devices
735 * @dev: the device node the properties of which are parsed for references
736 * @notifier: the async notifier where the async subdevs will be added
737 * @prop: the name of the property
739 * Return: 0 on success
740 * -ENOENT if no entries were found
741 * -ENOMEM if memory allocation failed
742 * -EINVAL if property parsing failed
744 static int v4l2_fwnode_reference_parse(struct device *dev,
745 struct v4l2_async_notifier *notifier,
748 struct fwnode_reference_args args;
753 !(ret = fwnode_property_get_reference_args(dev_fwnode(dev),
757 fwnode_handle_put(args.fwnode);
763 * Note that right now both -ENODATA and -ENOENT may signal
764 * out-of-bounds access. Return the error in cases other than that.
766 if (ret != -ENOENT && ret != -ENODATA)
770 !fwnode_property_get_reference_args(dev_fwnode(dev), prop, NULL,
773 struct v4l2_async_subdev *asd;
775 asd = v4l2_async_notifier_add_fwnode_subdev(notifier,
778 fwnode_handle_put(args.fwnode);
780 /* not an error if asd already exists */
781 if (PTR_ERR(asd) == -EEXIST)
792 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
794 * @fwnode: fwnode to read @prop from
795 * @notifier: notifier for @dev
796 * @prop: the name of the property
797 * @index: the index of the reference to get
798 * @props: the array of integer property names
799 * @nprops: the number of integer property names in @nprops
801 * First find an fwnode referred to by the reference at @index in @prop.
803 * Then under that fwnode, @nprops times, for each property in @props,
804 * iteratively follow child nodes starting from fwnode such that they have the
805 * property in @props array at the index of the child node distance from the
806 * root node and the value of that property matching with the integer argument
807 * of the reference, at the same index.
809 * The child fwnode reached at the end of the iteration is then returned to the
812 * The core reason for this is that you cannot refer to just any node in ACPI.
813 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
814 * provide a list of (property name, property value) tuples where each tuple
815 * uniquely identifies a child node. The first tuple identifies a child directly
816 * underneath the device fwnode, the next tuple identifies a child node
817 * underneath the fwnode identified by the previous tuple, etc. until you
818 * reached the fwnode you need.
820 * THIS EXAMPLE EXISTS MERELY TO DOCUMENT THIS FUNCTION. DO NOT USE IT AS A
821 * REFERENCE IN HOW ACPI TABLES SHOULD BE WRITTEN!! See documentation under
822 * Documentation/acpi/dsd instead and especially graph.txt,
823 * data-node-references.txt and leds.txt .
825 * Scope (\_SB.PCI0.I2C2)
829 * Name (_DSD, Package () {
830 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
834 * Package () { "nokia,smia" }
837 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
839 * Package () { "port0", "PRT0" },
842 * Name (PRT0, Package() {
843 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
845 * Package () { "port", 0 },
847 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
849 * Package () { "endpoint0", "EP00" },
852 * Name (EP00, Package() {
853 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
855 * Package () { "endpoint", 0 },
859 * \_SB.PCI0.ISP, 4, 0
871 * Name (_DSD, Package () {
872 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
874 * Package () { "port4", "PRT4" },
878 * Name (PRT4, Package() {
879 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
881 * Package () { "port", 4 },
883 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
885 * Package () { "endpoint0", "EP40" },
889 * Name (EP40, Package() {
890 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
892 * Package () { "endpoint", 0 },
896 * \_SB.PCI0.I2C2.CAM0,
905 * From the EP40 node under ISP device, you could parse the graph remote
906 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
908 * @fwnode: fwnode referring to EP40 under ISP.
909 * @prop: "remote-endpoint"
911 * @props: "port", "endpoint"
914 * And you'd get back fwnode referring to EP00 under CAM0.
916 * The same works the other way around: if you use EP00 under CAM0 as the
917 * fwnode, you'll get fwnode referring to EP40 under ISP.
919 * The same example in DT syntax would look like this:
922 * compatible = "nokia,smia";
928 * remote-endpoint = <&isp 4 0>;
939 * remote-endpoint = <&cam 0 0>;
945 * Return: 0 on success
946 * -ENOENT if no entries (or the property itself) were found
947 * -EINVAL if property parsing otherwise failed
948 * -ENOMEM if memory allocation failed
950 static struct fwnode_handle *
951 v4l2_fwnode_reference_get_int_prop(struct fwnode_handle *fwnode,
954 const char * const *props,
957 struct fwnode_reference_args fwnode_args;
958 u64 *args = fwnode_args.args;
959 struct fwnode_handle *child;
963 * Obtain remote fwnode as well as the integer arguments.
965 * Note that right now both -ENODATA and -ENOENT may signal
966 * out-of-bounds access. Return -ENOENT in that case.
968 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
969 index, &fwnode_args);
971 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
974 * Find a node in the tree under the referred fwnode corresponding to
975 * the integer arguments.
977 fwnode = fwnode_args.fwnode;
981 /* Loop over all child nodes under fwnode. */
982 fwnode_for_each_child_node(fwnode, child) {
983 if (fwnode_property_read_u32(child, *props, &val))
986 /* Found property, see if its value matches. */
991 fwnode_handle_put(fwnode);
993 /* No property found; return an error here. */
995 fwnode = ERR_PTR(-ENOENT);
1007 struct v4l2_fwnode_int_props {
1009 const char * const *props;
1010 unsigned int nprops;
1014 * v4l2_fwnode_reference_parse_int_props - parse references for async
1016 * @dev: struct device pointer
1017 * @notifier: notifier for @dev
1018 * @prop: the name of the property
1019 * @props: the array of integer property names
1020 * @nprops: the number of integer properties
1022 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
1023 * property @prop with integer arguments with child nodes matching in properties
1024 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
1027 * While it is technically possible to use this function on DT, it is only
1028 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
1029 * on ACPI the references are limited to devices.
1031 * Return: 0 on success
1032 * -ENOENT if no entries (or the property itself) were found
1033 * -EINVAL if property parsing otherwisefailed
1034 * -ENOMEM if memory allocation failed
1037 v4l2_fwnode_reference_parse_int_props(struct device *dev,
1038 struct v4l2_async_notifier *notifier,
1039 const struct v4l2_fwnode_int_props *p)
1041 struct fwnode_handle *fwnode;
1044 const char *prop = p->name;
1045 const char * const *props = p->props;
1046 unsigned int nprops = p->nprops;
1050 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1053 if (IS_ERR(fwnode)) {
1055 * Note that right now both -ENODATA and -ENOENT may
1056 * signal out-of-bounds access. Return the error in
1057 * cases other than that.
1059 if (PTR_ERR(fwnode) != -ENOENT &&
1060 PTR_ERR(fwnode) != -ENODATA)
1061 return PTR_ERR(fwnode);
1064 fwnode_handle_put(fwnode);
1069 !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1074 struct v4l2_async_subdev *asd;
1076 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
1078 fwnode_handle_put(fwnode);
1081 /* not an error if asd already exists */
1085 return PTR_ERR(asd);
1089 return !fwnode || PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
1092 int v4l2_async_notifier_parse_fwnode_sensor_common(struct device *dev,
1093 struct v4l2_async_notifier *notifier)
1095 static const char * const led_props[] = { "led" };
1096 static const struct v4l2_fwnode_int_props props[] = {
1097 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
1098 { "lens-focus", NULL, 0 },
1102 for (i = 0; i < ARRAY_SIZE(props); i++) {
1105 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
1106 ret = v4l2_fwnode_reference_parse_int_props(dev,
1110 ret = v4l2_fwnode_reference_parse(dev, notifier,
1112 if (ret && ret != -ENOENT) {
1113 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
1114 props[i].name, ret);
1121 EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
1123 int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
1125 struct v4l2_async_notifier *notifier;
1128 if (WARN_ON(!sd->dev))
1131 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1135 v4l2_async_notifier_init(notifier);
1137 ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
1142 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1146 ret = v4l2_async_register_subdev(sd);
1148 goto out_unregister;
1150 sd->subdev_notifier = notifier;
1155 v4l2_async_notifier_unregister(notifier);
1158 v4l2_async_notifier_cleanup(notifier);
1163 EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
1165 int v4l2_async_register_fwnode_subdev(struct v4l2_subdev *sd,
1166 size_t asd_struct_size,
1167 unsigned int *ports,
1168 unsigned int num_ports,
1169 parse_endpoint_func parse_endpoint)
1171 struct v4l2_async_notifier *notifier;
1172 struct device *dev = sd->dev;
1173 struct fwnode_handle *fwnode;
1179 fwnode = dev_fwnode(dev);
1180 if (!fwnode_device_is_available(fwnode))
1183 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1187 v4l2_async_notifier_init(notifier);
1190 ret = v4l2_async_notifier_parse_fwnode_endpoints(dev, notifier,
1198 for (i = 0; i < num_ports; i++) {
1199 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(dev, notifier, asd_struct_size, ports[i], parse_endpoint);
1205 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1209 ret = v4l2_async_register_subdev(sd);
1211 goto out_unregister;
1213 sd->subdev_notifier = notifier;
1218 v4l2_async_notifier_unregister(notifier);
1220 v4l2_async_notifier_cleanup(notifier);
1225 EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
1227 MODULE_LICENSE("GPL");
1228 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1229 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1230 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");