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 static const struct v4l2_fwnode_bus_conv {
32 enum v4l2_fwnode_bus_type fwnode_bus_type;
33 enum v4l2_mbus_type mbus_type;
37 V4L2_FWNODE_BUS_TYPE_GUESS,
41 V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
45 V4L2_FWNODE_BUS_TYPE_CSI1,
49 V4L2_FWNODE_BUS_TYPE_CCP2,
51 "compact camera port 2",
53 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
57 V4L2_FWNODE_BUS_TYPE_PARALLEL,
61 V4L2_FWNODE_BUS_TYPE_BT656,
65 V4L2_FWNODE_BUS_TYPE_DPI,
71 static const struct v4l2_fwnode_bus_conv *
72 get_v4l2_fwnode_bus_conv_by_fwnode_bus(enum v4l2_fwnode_bus_type type)
76 for (i = 0; i < ARRAY_SIZE(buses); i++)
77 if (buses[i].fwnode_bus_type == type)
83 static enum v4l2_mbus_type
84 v4l2_fwnode_bus_type_to_mbus(enum v4l2_fwnode_bus_type type)
86 const struct v4l2_fwnode_bus_conv *conv =
87 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
89 return conv ? conv->mbus_type : V4L2_MBUS_INVALID;
93 v4l2_fwnode_bus_type_to_string(enum v4l2_fwnode_bus_type type)
95 const struct v4l2_fwnode_bus_conv *conv =
96 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
98 return conv ? conv->name : "not found";
101 static const struct v4l2_fwnode_bus_conv *
102 get_v4l2_fwnode_bus_conv_by_mbus(enum v4l2_mbus_type type)
106 for (i = 0; i < ARRAY_SIZE(buses); i++)
107 if (buses[i].mbus_type == type)
114 v4l2_fwnode_mbus_type_to_string(enum v4l2_mbus_type type)
116 const struct v4l2_fwnode_bus_conv *conv =
117 get_v4l2_fwnode_bus_conv_by_mbus(type);
119 return conv ? conv->name : "not found";
122 static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
123 struct v4l2_fwnode_endpoint *vep,
124 enum v4l2_mbus_type bus_type)
126 struct v4l2_mbus_config_mipi_csi2 *bus = &vep->bus.mipi_csi2;
127 bool have_clk_lane = false, have_data_lanes = false,
128 have_lane_polarities = false;
129 unsigned int flags = 0, lanes_used = 0;
130 u32 array[1 + V4L2_MBUS_CSI2_MAX_DATA_LANES];
132 unsigned int num_data_lanes = 0;
133 bool use_default_lane_mapping = false;
138 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
139 bus_type == V4L2_MBUS_CSI2_CPHY) {
140 use_default_lane_mapping = true;
142 num_data_lanes = min_t(u32, bus->num_data_lanes,
143 V4L2_MBUS_CSI2_MAX_DATA_LANES);
145 clock_lane = bus->clock_lane;
147 use_default_lane_mapping = false;
149 for (i = 0; i < num_data_lanes; i++) {
150 array[i] = bus->data_lanes[i];
152 use_default_lane_mapping = false;
155 if (use_default_lane_mapping)
156 pr_debug("no lane mapping given, using defaults\n");
159 rval = fwnode_property_count_u32(fwnode, "data-lanes");
162 min_t(int, V4L2_MBUS_CSI2_MAX_DATA_LANES, rval);
164 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
167 have_data_lanes = true;
168 if (use_default_lane_mapping) {
169 pr_debug("data-lanes property exists; disabling default mapping\n");
170 use_default_lane_mapping = false;
174 for (i = 0; i < num_data_lanes; i++) {
175 if (lanes_used & BIT(array[i])) {
176 if (have_data_lanes || !use_default_lane_mapping)
177 pr_warn("duplicated lane %u in data-lanes, using defaults\n",
179 use_default_lane_mapping = true;
181 lanes_used |= BIT(array[i]);
184 pr_debug("lane %u position %u\n", i, array[i]);
187 rval = fwnode_property_count_u32(fwnode, "lane-polarities");
189 if (rval != 1 + num_data_lanes /* clock+data */) {
190 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
191 1 + num_data_lanes, rval);
195 have_lane_polarities = true;
198 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
200 pr_debug("clock lane position %u\n", v);
201 have_clk_lane = true;
204 if (have_clk_lane && lanes_used & BIT(clock_lane) &&
205 !use_default_lane_mapping) {
206 pr_warn("duplicated lane %u in clock-lanes, using defaults\n",
208 use_default_lane_mapping = true;
211 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
212 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
213 pr_debug("non-continuous clock\n");
216 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
217 bus_type == V4L2_MBUS_CSI2_CPHY ||
218 lanes_used || have_clk_lane || flags) {
219 /* Only D-PHY has a clock lane. */
220 unsigned int dfl_data_lane_index =
221 bus_type == V4L2_MBUS_CSI2_DPHY;
224 if (bus_type == V4L2_MBUS_UNKNOWN)
225 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
226 bus->num_data_lanes = num_data_lanes;
228 if (use_default_lane_mapping) {
230 for (i = 0; i < num_data_lanes; i++)
231 bus->data_lanes[i] = dfl_data_lane_index + i;
233 bus->clock_lane = clock_lane;
234 for (i = 0; i < num_data_lanes; i++)
235 bus->data_lanes[i] = array[i];
238 if (have_lane_polarities) {
239 fwnode_property_read_u32_array(fwnode,
240 "lane-polarities", array,
243 for (i = 0; i < 1 + num_data_lanes; i++) {
244 bus->lane_polarities[i] = array[i];
245 pr_debug("lane %u polarity %sinverted",
246 i, array[i] ? "" : "not ");
249 pr_debug("no lane polarities defined, assuming not inverted\n");
256 #define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
257 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
258 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
259 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
260 V4L2_MBUS_FIELD_EVEN_HIGH | \
261 V4L2_MBUS_FIELD_EVEN_LOW)
264 v4l2_fwnode_endpoint_parse_parallel_bus(struct fwnode_handle *fwnode,
265 struct v4l2_fwnode_endpoint *vep,
266 enum v4l2_mbus_type bus_type)
268 struct v4l2_mbus_config_parallel *bus = &vep->bus.parallel;
269 unsigned int flags = 0;
272 if (bus_type == V4L2_MBUS_PARALLEL || bus_type == V4L2_MBUS_BT656)
275 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
276 flags &= ~(V4L2_MBUS_HSYNC_ACTIVE_HIGH |
277 V4L2_MBUS_HSYNC_ACTIVE_LOW);
278 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
279 V4L2_MBUS_HSYNC_ACTIVE_LOW;
280 pr_debug("hsync-active %s\n", v ? "high" : "low");
283 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
284 flags &= ~(V4L2_MBUS_VSYNC_ACTIVE_HIGH |
285 V4L2_MBUS_VSYNC_ACTIVE_LOW);
286 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
287 V4L2_MBUS_VSYNC_ACTIVE_LOW;
288 pr_debug("vsync-active %s\n", v ? "high" : "low");
291 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
292 flags &= ~(V4L2_MBUS_FIELD_EVEN_HIGH |
293 V4L2_MBUS_FIELD_EVEN_LOW);
294 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
295 V4L2_MBUS_FIELD_EVEN_LOW;
296 pr_debug("field-even-active %s\n", v ? "high" : "low");
299 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
300 flags &= ~(V4L2_MBUS_PCLK_SAMPLE_RISING |
301 V4L2_MBUS_PCLK_SAMPLE_FALLING);
302 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
303 V4L2_MBUS_PCLK_SAMPLE_FALLING;
304 pr_debug("pclk-sample %s\n", v ? "high" : "low");
307 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
308 flags &= ~(V4L2_MBUS_DATA_ACTIVE_HIGH |
309 V4L2_MBUS_DATA_ACTIVE_LOW);
310 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
311 V4L2_MBUS_DATA_ACTIVE_LOW;
312 pr_debug("data-active %s\n", v ? "high" : "low");
315 if (fwnode_property_present(fwnode, "slave-mode")) {
316 pr_debug("slave mode\n");
317 flags &= ~V4L2_MBUS_MASTER;
318 flags |= V4L2_MBUS_SLAVE;
320 flags &= ~V4L2_MBUS_SLAVE;
321 flags |= V4L2_MBUS_MASTER;
324 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
326 pr_debug("bus-width %u\n", v);
329 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
331 pr_debug("data-shift %u\n", v);
334 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
335 flags &= ~(V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH |
336 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW);
337 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
338 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
339 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
342 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
343 flags &= ~(V4L2_MBUS_DATA_ENABLE_HIGH |
344 V4L2_MBUS_DATA_ENABLE_LOW);
345 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
346 V4L2_MBUS_DATA_ENABLE_LOW;
347 pr_debug("data-enable-active %s\n", v ? "high" : "low");
353 if (flags & PARALLEL_MBUS_FLAGS)
354 vep->bus_type = V4L2_MBUS_PARALLEL;
356 vep->bus_type = V4L2_MBUS_BT656;
358 case V4L2_MBUS_PARALLEL:
359 vep->bus_type = V4L2_MBUS_PARALLEL;
362 case V4L2_MBUS_BT656:
363 vep->bus_type = V4L2_MBUS_BT656;
364 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
370 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
371 struct v4l2_fwnode_endpoint *vep,
372 enum v4l2_mbus_type bus_type)
374 struct v4l2_mbus_config_mipi_csi1 *bus = &vep->bus.mipi_csi1;
377 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
379 pr_debug("clock-inv %u\n", v);
382 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
384 pr_debug("strobe %u\n", v);
387 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
389 pr_debug("data-lanes %u\n", v);
392 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
394 pr_debug("clock-lanes %u\n", v);
397 if (bus_type == V4L2_MBUS_CCP2)
398 vep->bus_type = V4L2_MBUS_CCP2;
400 vep->bus_type = V4L2_MBUS_CSI1;
403 static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
404 struct v4l2_fwnode_endpoint *vep)
406 u32 bus_type = V4L2_FWNODE_BUS_TYPE_GUESS;
407 enum v4l2_mbus_type mbus_type;
410 pr_debug("===== begin parsing endpoint %pfw\n", fwnode);
412 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
413 pr_debug("fwnode video bus type %s (%u), mbus type %s (%u)\n",
414 v4l2_fwnode_bus_type_to_string(bus_type), bus_type,
415 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
417 mbus_type = v4l2_fwnode_bus_type_to_mbus(bus_type);
418 if (mbus_type == V4L2_MBUS_INVALID) {
419 pr_debug("unsupported bus type %u\n", bus_type);
423 if (vep->bus_type != V4L2_MBUS_UNKNOWN) {
424 if (mbus_type != V4L2_MBUS_UNKNOWN &&
425 vep->bus_type != mbus_type) {
426 pr_debug("expecting bus type %s\n",
427 v4l2_fwnode_mbus_type_to_string(vep->bus_type));
431 vep->bus_type = mbus_type;
434 switch (vep->bus_type) {
435 case V4L2_MBUS_UNKNOWN:
436 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
441 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
442 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
445 pr_debug("assuming media bus type %s (%u)\n",
446 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
452 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, vep->bus_type);
455 case V4L2_MBUS_CSI2_DPHY:
456 case V4L2_MBUS_CSI2_CPHY:
457 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
463 case V4L2_MBUS_PARALLEL:
464 case V4L2_MBUS_BT656:
465 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
470 pr_warn("unsupported bus type %u\n", mbus_type);
474 fwnode_graph_parse_endpoint(fwnode, &vep->base);
479 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
480 struct v4l2_fwnode_endpoint *vep)
484 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
486 pr_debug("===== end parsing endpoint %pfw\n", fwnode);
490 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
492 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
494 if (IS_ERR_OR_NULL(vep))
497 kfree(vep->link_frequencies);
498 vep->link_frequencies = NULL;
500 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
502 int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
503 struct v4l2_fwnode_endpoint *vep)
507 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
511 rval = fwnode_property_count_u64(fwnode, "link-frequencies");
515 vep->link_frequencies =
516 kmalloc_array(rval, sizeof(*vep->link_frequencies),
518 if (!vep->link_frequencies)
521 vep->nr_of_link_frequencies = rval;
523 rval = fwnode_property_read_u64_array(fwnode,
525 vep->link_frequencies,
526 vep->nr_of_link_frequencies);
528 v4l2_fwnode_endpoint_free(vep);
532 for (i = 0; i < vep->nr_of_link_frequencies; i++)
533 pr_debug("link-frequencies %u value %llu\n", i,
534 vep->link_frequencies[i]);
537 pr_debug("===== end parsing endpoint %pfw\n", fwnode);
541 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
543 int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
544 struct v4l2_fwnode_link *link)
546 struct fwnode_endpoint fwep;
548 memset(link, 0, sizeof(*link));
550 fwnode_graph_parse_endpoint(fwnode, &fwep);
551 link->local_id = fwep.id;
552 link->local_port = fwep.port;
553 link->local_node = fwnode_graph_get_port_parent(fwnode);
555 fwnode = fwnode_graph_get_remote_endpoint(fwnode);
557 fwnode_handle_put(fwnode);
561 fwnode_graph_parse_endpoint(fwnode, &fwep);
562 link->remote_id = fwep.id;
563 link->remote_port = fwep.port;
564 link->remote_node = fwnode_graph_get_port_parent(fwnode);
568 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
570 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
572 fwnode_handle_put(link->local_node);
573 fwnode_handle_put(link->remote_node);
575 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
577 static const struct v4l2_fwnode_connector_conv {
578 enum v4l2_connector_type type;
579 const char *compatible;
582 .type = V4L2_CONN_COMPOSITE,
583 .compatible = "composite-video-connector",
585 .type = V4L2_CONN_SVIDEO,
586 .compatible = "svideo-connector",
590 static enum v4l2_connector_type
591 v4l2_fwnode_string_to_connector_type(const char *con_str)
595 for (i = 0; i < ARRAY_SIZE(connectors); i++)
596 if (!strcmp(con_str, connectors[i].compatible))
597 return connectors[i].type;
599 return V4L2_CONN_UNKNOWN;
603 v4l2_fwnode_connector_parse_analog(struct fwnode_handle *fwnode,
604 struct v4l2_fwnode_connector *vc)
609 ret = fwnode_property_read_u32(fwnode, "sdtv-standards", &stds);
611 /* The property is optional. */
612 vc->connector.analog.sdtv_stds = ret ? V4L2_STD_ALL : stds;
615 void v4l2_fwnode_connector_free(struct v4l2_fwnode_connector *connector)
617 struct v4l2_connector_link *link, *tmp;
619 if (IS_ERR_OR_NULL(connector) || connector->type == V4L2_CONN_UNKNOWN)
622 list_for_each_entry_safe(link, tmp, &connector->links, head) {
623 v4l2_fwnode_put_link(&link->fwnode_link);
624 list_del(&link->head);
628 kfree(connector->label);
629 connector->label = NULL;
630 connector->type = V4L2_CONN_UNKNOWN;
632 EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_free);
634 static enum v4l2_connector_type
635 v4l2_fwnode_get_connector_type(struct fwnode_handle *fwnode)
637 const char *type_name;
641 return V4L2_CONN_UNKNOWN;
643 /* The connector-type is stored within the compatible string. */
644 err = fwnode_property_read_string(fwnode, "compatible", &type_name);
646 return V4L2_CONN_UNKNOWN;
648 return v4l2_fwnode_string_to_connector_type(type_name);
651 int v4l2_fwnode_connector_parse(struct fwnode_handle *fwnode,
652 struct v4l2_fwnode_connector *connector)
654 struct fwnode_handle *connector_node;
655 enum v4l2_connector_type connector_type;
662 memset(connector, 0, sizeof(*connector));
664 INIT_LIST_HEAD(&connector->links);
666 connector_node = fwnode_graph_get_port_parent(fwnode);
667 connector_type = v4l2_fwnode_get_connector_type(connector_node);
668 if (connector_type == V4L2_CONN_UNKNOWN) {
669 fwnode_handle_put(connector_node);
670 connector_node = fwnode_graph_get_remote_port_parent(fwnode);
671 connector_type = v4l2_fwnode_get_connector_type(connector_node);
674 if (connector_type == V4L2_CONN_UNKNOWN) {
675 pr_err("Unknown connector type\n");
680 connector->type = connector_type;
681 connector->name = fwnode_get_name(connector_node);
682 err = fwnode_property_read_string(connector_node, "label", &label);
683 connector->label = err ? NULL : kstrdup_const(label, GFP_KERNEL);
685 /* Parse the connector specific properties. */
686 switch (connector->type) {
687 case V4L2_CONN_COMPOSITE:
688 case V4L2_CONN_SVIDEO:
689 v4l2_fwnode_connector_parse_analog(connector_node, connector);
691 /* Avoid compiler warnings */
692 case V4L2_CONN_UNKNOWN:
697 fwnode_handle_put(connector_node);
701 EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_parse);
703 int v4l2_fwnode_connector_add_link(struct fwnode_handle *fwnode,
704 struct v4l2_fwnode_connector *connector)
706 struct fwnode_handle *connector_ep;
707 struct v4l2_connector_link *link;
710 if (!fwnode || !connector || connector->type == V4L2_CONN_UNKNOWN)
713 connector_ep = fwnode_graph_get_remote_endpoint(fwnode);
717 link = kzalloc(sizeof(*link), GFP_KERNEL);
723 err = v4l2_fwnode_parse_link(connector_ep, &link->fwnode_link);
727 fwnode_handle_put(connector_ep);
729 list_add(&link->head, &connector->links);
730 connector->nr_of_links++;
736 fwnode_handle_put(connector_ep);
740 EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_add_link);
742 int v4l2_fwnode_device_parse(struct device *dev,
743 struct v4l2_fwnode_device_properties *props)
745 struct fwnode_handle *fwnode = dev_fwnode(dev);
749 memset(props, 0, sizeof(*props));
751 props->orientation = V4L2_FWNODE_PROPERTY_UNSET;
752 ret = fwnode_property_read_u32(fwnode, "orientation", &val);
755 case V4L2_FWNODE_ORIENTATION_FRONT:
756 case V4L2_FWNODE_ORIENTATION_BACK:
757 case V4L2_FWNODE_ORIENTATION_EXTERNAL:
760 dev_warn(dev, "Unsupported device orientation: %u\n", val);
764 props->orientation = val;
765 dev_dbg(dev, "device orientation: %u\n", val);
768 props->rotation = V4L2_FWNODE_PROPERTY_UNSET;
769 ret = fwnode_property_read_u32(fwnode, "rotation", &val);
772 dev_warn(dev, "Unsupported device rotation: %u\n", val);
776 props->rotation = val;
777 dev_dbg(dev, "device rotation: %u\n", val);
782 EXPORT_SYMBOL_GPL(v4l2_fwnode_device_parse);
785 v4l2_async_nf_fwnode_parse_endpoint(struct device *dev,
786 struct v4l2_async_notifier *notifier,
787 struct fwnode_handle *endpoint,
788 unsigned int asd_struct_size,
789 parse_endpoint_func parse_endpoint)
791 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
792 struct v4l2_async_subdev *asd;
795 asd = kzalloc(asd_struct_size, GFP_KERNEL);
799 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
801 fwnode_graph_get_remote_port_parent(endpoint);
802 if (!asd->match.fwnode) {
803 dev_dbg(dev, "no remote endpoint found\n");
808 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
810 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
815 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
816 if (ret == -ENOTCONN)
817 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
821 "driver could not parse port@%u/endpoint@%u (%d)\n",
822 vep.base.port, vep.base.id, ret);
823 v4l2_fwnode_endpoint_free(&vep);
827 ret = __v4l2_async_nf_add_subdev(notifier, asd);
829 /* not an error if asd already exists */
838 fwnode_handle_put(asd->match.fwnode);
841 return ret == -ENOTCONN ? 0 : ret;
845 v4l2_async_nf_parse_fwnode_endpoints(struct device *dev,
846 struct v4l2_async_notifier *notifier,
847 size_t asd_struct_size,
848 parse_endpoint_func parse_endpoint)
850 struct fwnode_handle *fwnode;
853 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
856 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
857 struct fwnode_handle *dev_fwnode;
860 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
861 is_available = fwnode_device_is_available(dev_fwnode);
862 fwnode_handle_put(dev_fwnode);
867 ret = v4l2_async_nf_fwnode_parse_endpoint(dev, notifier,
875 fwnode_handle_put(fwnode);
879 EXPORT_SYMBOL_GPL(v4l2_async_nf_parse_fwnode_endpoints);
882 * v4l2_fwnode_reference_parse - parse references for async sub-devices
883 * @dev: the device node the properties of which are parsed for references
884 * @notifier: the async notifier where the async subdevs will be added
885 * @prop: the name of the property
887 * Return: 0 on success
888 * -ENOENT if no entries were found
889 * -ENOMEM if memory allocation failed
890 * -EINVAL if property parsing failed
892 static int v4l2_fwnode_reference_parse(struct device *dev,
893 struct v4l2_async_notifier *notifier,
896 struct fwnode_reference_args args;
901 !(ret = fwnode_property_get_reference_args(dev_fwnode(dev), prop,
902 NULL, 0, index, &args));
904 struct v4l2_async_subdev *asd;
906 asd = v4l2_async_nf_add_fwnode(notifier, args.fwnode,
907 struct v4l2_async_subdev);
908 fwnode_handle_put(args.fwnode);
910 /* not an error if asd already exists */
911 if (PTR_ERR(asd) == -EEXIST)
918 /* -ENOENT here means successful parsing */
922 /* Return -ENOENT if no references were found */
923 return index ? 0 : -ENOENT;
927 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
929 * @fwnode: fwnode to read @prop from
930 * @notifier: notifier for @dev
931 * @prop: the name of the property
932 * @index: the index of the reference to get
933 * @props: the array of integer property names
934 * @nprops: the number of integer property names in @nprops
936 * First find an fwnode referred to by the reference at @index in @prop.
938 * Then under that fwnode, @nprops times, for each property in @props,
939 * iteratively follow child nodes starting from fwnode such that they have the
940 * property in @props array at the index of the child node distance from the
941 * root node and the value of that property matching with the integer argument
942 * of the reference, at the same index.
944 * The child fwnode reached at the end of the iteration is then returned to the
947 * The core reason for this is that you cannot refer to just any node in ACPI.
948 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
949 * provide a list of (property name, property value) tuples where each tuple
950 * uniquely identifies a child node. The first tuple identifies a child directly
951 * underneath the device fwnode, the next tuple identifies a child node
952 * underneath the fwnode identified by the previous tuple, etc. until you
953 * reached the fwnode you need.
955 * THIS EXAMPLE EXISTS MERELY TO DOCUMENT THIS FUNCTION. DO NOT USE IT AS A
956 * REFERENCE IN HOW ACPI TABLES SHOULD BE WRITTEN!! See documentation under
957 * Documentation/firmware-guide/acpi/dsd/ instead and especially graph.txt,
958 * data-node-references.txt and leds.txt .
960 * Scope (\_SB.PCI0.I2C2)
964 * Name (_DSD, Package () {
965 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
969 * Package () { "nokia,smia" }
972 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
974 * Package () { "port0", "PRT0" },
977 * Name (PRT0, Package() {
978 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
980 * Package () { "port", 0 },
982 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
984 * Package () { "endpoint0", "EP00" },
987 * Name (EP00, Package() {
988 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
990 * Package () { "endpoint", 0 },
994 * \_SB.PCI0.ISP, 4, 0
1006 * Name (_DSD, Package () {
1007 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
1009 * Package () { "port4", "PRT4" },
1013 * Name (PRT4, Package() {
1014 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
1016 * Package () { "port", 4 },
1018 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
1020 * Package () { "endpoint0", "EP40" },
1024 * Name (EP40, Package() {
1025 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
1027 * Package () { "endpoint", 0 },
1029 * "remote-endpoint",
1031 * \_SB.PCI0.I2C2.CAM0,
1040 * From the EP40 node under ISP device, you could parse the graph remote
1041 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
1043 * @fwnode: fwnode referring to EP40 under ISP.
1044 * @prop: "remote-endpoint"
1046 * @props: "port", "endpoint"
1049 * And you'd get back fwnode referring to EP00 under CAM0.
1051 * The same works the other way around: if you use EP00 under CAM0 as the
1052 * fwnode, you'll get fwnode referring to EP40 under ISP.
1054 * The same example in DT syntax would look like this:
1057 * compatible = "nokia,smia";
1063 * remote-endpoint = <&isp 4 0>;
1074 * remote-endpoint = <&cam 0 0>;
1080 * Return: 0 on success
1081 * -ENOENT if no entries (or the property itself) were found
1082 * -EINVAL if property parsing otherwise failed
1083 * -ENOMEM if memory allocation failed
1085 static struct fwnode_handle *
1086 v4l2_fwnode_reference_get_int_prop(struct fwnode_handle *fwnode,
1089 const char * const *props,
1090 unsigned int nprops)
1092 struct fwnode_reference_args fwnode_args;
1093 u64 *args = fwnode_args.args;
1094 struct fwnode_handle *child;
1098 * Obtain remote fwnode as well as the integer arguments.
1100 * Note that right now both -ENODATA and -ENOENT may signal
1101 * out-of-bounds access. Return -ENOENT in that case.
1103 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
1104 index, &fwnode_args);
1106 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
1109 * Find a node in the tree under the referred fwnode corresponding to
1110 * the integer arguments.
1112 fwnode = fwnode_args.fwnode;
1116 /* Loop over all child nodes under fwnode. */
1117 fwnode_for_each_child_node(fwnode, child) {
1118 if (fwnode_property_read_u32(child, *props, &val))
1121 /* Found property, see if its value matches. */
1126 fwnode_handle_put(fwnode);
1128 /* No property found; return an error here. */
1130 fwnode = ERR_PTR(-ENOENT);
1142 struct v4l2_fwnode_int_props {
1144 const char * const *props;
1145 unsigned int nprops;
1149 * v4l2_fwnode_reference_parse_int_props - parse references for async
1151 * @dev: struct device pointer
1152 * @notifier: notifier for @dev
1153 * @prop: the name of the property
1154 * @props: the array of integer property names
1155 * @nprops: the number of integer properties
1157 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
1158 * property @prop with integer arguments with child nodes matching in properties
1159 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
1162 * While it is technically possible to use this function on DT, it is only
1163 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
1164 * on ACPI the references are limited to devices.
1166 * Return: 0 on success
1167 * -ENOENT if no entries (or the property itself) were found
1168 * -EINVAL if property parsing otherwisefailed
1169 * -ENOMEM if memory allocation failed
1172 v4l2_fwnode_reference_parse_int_props(struct device *dev,
1173 struct v4l2_async_notifier *notifier,
1174 const struct v4l2_fwnode_int_props *p)
1176 struct fwnode_handle *fwnode;
1179 const char *prop = p->name;
1180 const char * const *props = p->props;
1181 unsigned int nprops = p->nprops;
1185 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1188 if (IS_ERR(fwnode)) {
1190 * Note that right now both -ENODATA and -ENOENT may
1191 * signal out-of-bounds access. Return the error in
1192 * cases other than that.
1194 if (PTR_ERR(fwnode) != -ENOENT &&
1195 PTR_ERR(fwnode) != -ENODATA)
1196 return PTR_ERR(fwnode);
1199 fwnode_handle_put(fwnode);
1204 !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1209 struct v4l2_async_subdev *asd;
1211 asd = v4l2_async_nf_add_fwnode(notifier, fwnode,
1212 struct v4l2_async_subdev);
1213 fwnode_handle_put(fwnode);
1216 /* not an error if asd already exists */
1220 return PTR_ERR(asd);
1224 return !fwnode || PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
1228 * v4l2_async_nf_parse_fwnode_sensor - parse common references on
1229 * sensors for async sub-devices
1230 * @dev: the device node the properties of which are parsed for references
1231 * @notifier: the async notifier where the async subdevs will be added
1233 * Parse common sensor properties for remote devices related to the
1234 * sensor and set up async sub-devices for them.
1236 * Any notifier populated using this function must be released with a call to
1237 * v4l2_async_nf_release() after it has been unregistered and the async
1238 * sub-devices are no longer in use, even in the case the function returned an
1241 * Return: 0 on success
1242 * -ENOMEM if memory allocation failed
1243 * -EINVAL if property parsing failed
1246 v4l2_async_nf_parse_fwnode_sensor(struct device *dev,
1247 struct v4l2_async_notifier *notifier)
1249 static const char * const led_props[] = { "led" };
1250 static const struct v4l2_fwnode_int_props props[] = {
1251 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
1252 { "lens-focus", NULL, 0 },
1256 for (i = 0; i < ARRAY_SIZE(props); i++) {
1259 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
1260 ret = v4l2_fwnode_reference_parse_int_props(dev,
1264 ret = v4l2_fwnode_reference_parse(dev, notifier,
1266 if (ret && ret != -ENOENT) {
1267 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
1268 props[i].name, ret);
1276 int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd)
1278 struct v4l2_async_notifier *notifier;
1281 if (WARN_ON(!sd->dev))
1284 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1288 v4l2_async_nf_init(notifier);
1290 ret = v4l2_async_nf_parse_fwnode_sensor(sd->dev, notifier);
1294 ret = v4l2_async_subdev_nf_register(sd, notifier);
1298 ret = v4l2_async_register_subdev(sd);
1300 goto out_unregister;
1302 sd->subdev_notifier = notifier;
1307 v4l2_async_nf_unregister(notifier);
1310 v4l2_async_nf_cleanup(notifier);
1315 EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor);
1317 MODULE_LICENSE("GPL");
1318 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1319 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1320 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");