Merge tag 'for-linus' of https://github.com/openrisc/linux
[linux-block.git] / drivers / media / v4l2-core / v4l2-fwnode.c
CommitLineData
25763b3c 1// SPDX-License-Identifier: GPL-2.0-only
ca50c197
SA
2/*
3 * V4L2 fwnode binding parsing library
4 *
5 * The origins of the V4L2 fwnode library are in V4L2 OF library that
6 * formerly was located in v4l2-of.c.
7 *
8 * Copyright (c) 2016 Intel Corporation.
9 * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
10 *
11 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
12 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
13 *
14 * Copyright (C) 2012 Renesas Electronics Corp.
15 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
ca50c197
SA
16 */
17#include <linux/acpi.h>
18#include <linux/kernel.h>
9ca46531 19#include <linux/mm.h>
ca50c197
SA
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/property.h>
23#include <linux/slab.h>
24#include <linux/string.h>
25#include <linux/types.h>
26
9ca46531 27#include <media/v4l2-async.h>
ca50c197 28#include <media/v4l2-fwnode.h>
aef69d54 29#include <media/v4l2-subdev.h>
ca50c197 30
b6e10ff6
HG
31#include "v4l2-subdev-priv.h"
32
26c1126c
SA
33static const struct v4l2_fwnode_bus_conv {
34 enum v4l2_fwnode_bus_type fwnode_bus_type;
35 enum v4l2_mbus_type mbus_type;
36 const char *name;
4faf7066 37} buses[] = {
26c1126c
SA
38 {
39 V4L2_FWNODE_BUS_TYPE_GUESS,
40 V4L2_MBUS_UNKNOWN,
41 "not specified",
42 }, {
43 V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
44 V4L2_MBUS_CSI2_CPHY,
45 "MIPI CSI-2 C-PHY",
46 }, {
47 V4L2_FWNODE_BUS_TYPE_CSI1,
48 V4L2_MBUS_CSI1,
49 "MIPI CSI-1",
50 }, {
51 V4L2_FWNODE_BUS_TYPE_CCP2,
52 V4L2_MBUS_CCP2,
53 "compact camera port 2",
54 }, {
55 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
56 V4L2_MBUS_CSI2_DPHY,
57 "MIPI CSI-2 D-PHY",
58 }, {
59 V4L2_FWNODE_BUS_TYPE_PARALLEL,
60 V4L2_MBUS_PARALLEL,
61 "parallel",
62 }, {
63 V4L2_FWNODE_BUS_TYPE_BT656,
64 V4L2_MBUS_BT656,
65 "Bt.656",
5e052a4d
XJ
66 }, {
67 V4L2_FWNODE_BUS_TYPE_DPI,
68 V4L2_MBUS_DPI,
69 "DPI",
26c1126c
SA
70 }
71};
72
73static const struct v4l2_fwnode_bus_conv *
74get_v4l2_fwnode_bus_conv_by_fwnode_bus(enum v4l2_fwnode_bus_type type)
75{
76 unsigned int i;
77
4faf7066
MCC
78 for (i = 0; i < ARRAY_SIZE(buses); i++)
79 if (buses[i].fwnode_bus_type == type)
80 return &buses[i];
26c1126c
SA
81
82 return NULL;
83}
84
85static enum v4l2_mbus_type
86v4l2_fwnode_bus_type_to_mbus(enum v4l2_fwnode_bus_type type)
87{
88 const struct v4l2_fwnode_bus_conv *conv =
89 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
90
69baf338 91 return conv ? conv->mbus_type : V4L2_MBUS_INVALID;
26c1126c
SA
92}
93
3eb32c26
SA
94static const char *
95v4l2_fwnode_bus_type_to_string(enum v4l2_fwnode_bus_type type)
96{
97 const struct v4l2_fwnode_bus_conv *conv =
98 get_v4l2_fwnode_bus_conv_by_fwnode_bus(type);
99
100 return conv ? conv->name : "not found";
101}
102
103static const struct v4l2_fwnode_bus_conv *
104get_v4l2_fwnode_bus_conv_by_mbus(enum v4l2_mbus_type type)
105{
106 unsigned int i;
107
4faf7066
MCC
108 for (i = 0; i < ARRAY_SIZE(buses); i++)
109 if (buses[i].mbus_type == type)
110 return &buses[i];
3eb32c26
SA
111
112 return NULL;
113}
114
115static const char *
116v4l2_fwnode_mbus_type_to_string(enum v4l2_mbus_type type)
117{
118 const struct v4l2_fwnode_bus_conv *conv =
119 get_v4l2_fwnode_bus_conv_by_mbus(type);
120
121 return conv ? conv->name : "not found";
122}
123
f3112735 124static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
276565ed 125 struct v4l2_fwnode_endpoint *vep,
26c1126c 126 enum v4l2_mbus_type bus_type)
ca50c197 127{
94d964e5 128 struct v4l2_mbus_config_mipi_csi2 *bus = &vep->bus.mipi_csi2;
b4357d21
SA
129 bool have_clk_lane = false, have_data_lanes = false,
130 have_lane_polarities = false;
ca50c197 131 unsigned int flags = 0, lanes_used = 0;
94d964e5 132 u32 array[1 + V4L2_MBUS_CSI2_MAX_DATA_LANES];
b4357d21 133 u32 clock_lane = 0;
276565ed 134 unsigned int num_data_lanes = 0;
b4357d21 135 bool use_default_lane_mapping = false;
ca50c197
SA
136 unsigned int i;
137 u32 v;
138 int rval;
139
edc6d56c
SA
140 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
141 bus_type == V4L2_MBUS_CSI2_CPHY) {
b4357d21
SA
142 use_default_lane_mapping = true;
143
276565ed 144 num_data_lanes = min_t(u32, bus->num_data_lanes,
94d964e5 145 V4L2_MBUS_CSI2_MAX_DATA_LANES);
276565ed 146
b4357d21
SA
147 clock_lane = bus->clock_lane;
148 if (clock_lane)
149 use_default_lane_mapping = false;
150
151 for (i = 0; i < num_data_lanes; i++) {
c2475aeb 152 array[i] = bus->data_lanes[i];
b4357d21
SA
153 if (array[i])
154 use_default_lane_mapping = false;
155 }
156
157 if (use_default_lane_mapping)
9d386373 158 pr_debug("no lane mapping given, using defaults\n");
c2475aeb
SA
159 }
160
3be9cc6a 161 rval = fwnode_property_count_u32(fwnode, "data-lanes");
ca50c197 162 if (rval > 0) {
276565ed 163 num_data_lanes =
94d964e5 164 min_t(int, V4L2_MBUS_CSI2_MAX_DATA_LANES, rval);
ca50c197
SA
165
166 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
276565ed 167 num_data_lanes);
b4357d21
SA
168
169 have_data_lanes = true;
9d386373
SA
170 if (use_default_lane_mapping) {
171 pr_debug("data-lanes property exists; disabling default mapping\n");
172 use_default_lane_mapping = false;
173 }
c2475aeb 174 }
ca50c197 175
c2475aeb 176 for (i = 0; i < num_data_lanes; i++) {
b4357d21
SA
177 if (lanes_used & BIT(array[i])) {
178 if (have_data_lanes || !use_default_lane_mapping)
179 pr_warn("duplicated lane %u in data-lanes, using defaults\n",
180 array[i]);
181 use_default_lane_mapping = true;
182 }
c2475aeb 183 lanes_used |= BIT(array[i]);
ca50c197 184
b4357d21
SA
185 if (have_data_lanes)
186 pr_debug("lane %u position %u\n", i, array[i]);
276565ed 187 }
ca50c197 188
3be9cc6a 189 rval = fwnode_property_count_u32(fwnode, "lane-polarities");
276565ed
SA
190 if (rval > 0) {
191 if (rval != 1 + num_data_lanes /* clock+data */) {
192 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
193 1 + num_data_lanes, rval);
194 return -EINVAL;
4ee23621 195 }
b24f0215 196
af11a74a 197 have_lane_polarities = true;
ca50c197
SA
198 }
199
200 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
b4357d21 201 clock_lane = v;
c8677aaf 202 pr_debug("clock lane position %u\n", v);
b4357d21
SA
203 have_clk_lane = true;
204 }
205
f8075c1c
SA
206 if (have_clk_lane && lanes_used & BIT(clock_lane) &&
207 !use_default_lane_mapping) {
208 pr_warn("duplicated lane %u in clock-lanes, using defaults\n",
209 v);
b4357d21 210 use_default_lane_mapping = true;
ca50c197
SA
211 }
212
c8677aaf 213 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
ca50c197 214 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
c8677aaf 215 pr_debug("non-continuous clock\n");
c8677aaf 216 }
ca50c197 217
edc6d56c 218 if (bus_type == V4L2_MBUS_CSI2_DPHY ||
b9f7caa7
LP
219 bus_type == V4L2_MBUS_CSI2_CPHY ||
220 lanes_used || have_clk_lane || flags) {
fff35d45
SA
221 /* Only D-PHY has a clock lane. */
222 unsigned int dfl_data_lane_index =
223 bus_type == V4L2_MBUS_CSI2_DPHY;
224
2835b5b1 225 bus->flags = flags;
edc6d56c
SA
226 if (bus_type == V4L2_MBUS_UNKNOWN)
227 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
276565ed 228 bus->num_data_lanes = num_data_lanes;
b4357d21
SA
229
230 if (use_default_lane_mapping) {
231 bus->clock_lane = 0;
232 for (i = 0; i < num_data_lanes; i++)
fff35d45 233 bus->data_lanes[i] = dfl_data_lane_index + i;
b4357d21
SA
234 } else {
235 bus->clock_lane = clock_lane;
236 for (i = 0; i < num_data_lanes; i++)
237 bus->data_lanes[i] = array[i];
238 }
af11a74a
SA
239
240 if (have_lane_polarities) {
241 fwnode_property_read_u32_array(fwnode,
242 "lane-polarities", array,
243 1 + num_data_lanes);
244
245 for (i = 0; i < 1 + num_data_lanes; i++) {
246 bus->lane_polarities[i] = array[i];
247 pr_debug("lane %u polarity %sinverted",
248 i, array[i] ? "" : "not ");
249 }
250 } else {
251 pr_debug("no lane polarities defined, assuming not inverted\n");
252 }
2835b5b1 253 }
ca50c197
SA
254
255 return 0;
256}
257
175b18b8
SA
258#define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
259 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
260 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
261 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
262 V4L2_MBUS_FIELD_EVEN_HIGH | \
263 V4L2_MBUS_FIELD_EVEN_LOW)
264
6087b215
MCC
265static void
266v4l2_fwnode_endpoint_parse_parallel_bus(struct fwnode_handle *fwnode,
267 struct v4l2_fwnode_endpoint *vep,
268 enum v4l2_mbus_type bus_type)
ca50c197 269{
94d964e5 270 struct v4l2_mbus_config_parallel *bus = &vep->bus.parallel;
ca50c197
SA
271 unsigned int flags = 0;
272 u32 v;
273
e9be1b86
SA
274 if (bus_type == V4L2_MBUS_PARALLEL || bus_type == V4L2_MBUS_BT656)
275 flags = bus->flags;
276
c8677aaf 277 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
e9be1b86
SA
278 flags &= ~(V4L2_MBUS_HSYNC_ACTIVE_HIGH |
279 V4L2_MBUS_HSYNC_ACTIVE_LOW);
ca50c197
SA
280 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
281 V4L2_MBUS_HSYNC_ACTIVE_LOW;
c8677aaf
SA
282 pr_debug("hsync-active %s\n", v ? "high" : "low");
283 }
ca50c197 284
c8677aaf 285 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
e9be1b86
SA
286 flags &= ~(V4L2_MBUS_VSYNC_ACTIVE_HIGH |
287 V4L2_MBUS_VSYNC_ACTIVE_LOW);
ca50c197
SA
288 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
289 V4L2_MBUS_VSYNC_ACTIVE_LOW;
c8677aaf
SA
290 pr_debug("vsync-active %s\n", v ? "high" : "low");
291 }
ca50c197 292
c8677aaf 293 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
e9be1b86
SA
294 flags &= ~(V4L2_MBUS_FIELD_EVEN_HIGH |
295 V4L2_MBUS_FIELD_EVEN_LOW);
ca50c197
SA
296 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
297 V4L2_MBUS_FIELD_EVEN_LOW;
c8677aaf
SA
298 pr_debug("field-even-active %s\n", v ? "high" : "low");
299 }
300
c8677aaf 301 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
e9be1b86 302 flags &= ~(V4L2_MBUS_PCLK_SAMPLE_RISING |
10694700
MR
303 V4L2_MBUS_PCLK_SAMPLE_FALLING |
304 V4L2_MBUS_PCLK_SAMPLE_DUALEDGE);
305 switch (v) {
306 case 0:
307 flags |= V4L2_MBUS_PCLK_SAMPLE_FALLING;
308 pr_debug("pclk-sample low\n");
309 break;
310 case 1:
311 flags |= V4L2_MBUS_PCLK_SAMPLE_RISING;
312 pr_debug("pclk-sample high\n");
313 break;
314 case 2:
315 flags |= V4L2_MBUS_PCLK_SAMPLE_DUALEDGE;
316 pr_debug("pclk-sample dual edge\n");
317 break;
318 default:
319 pr_warn("invalid argument for pclk-sample");
320 break;
321 }
c8677aaf 322 }
ca50c197 323
c8677aaf 324 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
fa09d065
OJ
325 flags &= ~(V4L2_MBUS_DATA_ACTIVE_HIGH |
326 V4L2_MBUS_DATA_ACTIVE_LOW);
ca50c197
SA
327 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
328 V4L2_MBUS_DATA_ACTIVE_LOW;
c8677aaf
SA
329 pr_debug("data-active %s\n", v ? "high" : "low");
330 }
ca50c197 331
c8677aaf
SA
332 if (fwnode_property_present(fwnode, "slave-mode")) {
333 pr_debug("slave mode\n");
e9be1b86 334 flags &= ~V4L2_MBUS_MASTER;
ca50c197 335 flags |= V4L2_MBUS_SLAVE;
c8677aaf 336 } else {
e9be1b86 337 flags &= ~V4L2_MBUS_SLAVE;
ca50c197 338 flags |= V4L2_MBUS_MASTER;
c8677aaf 339 }
ca50c197 340
c8677aaf 341 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
ca50c197 342 bus->bus_width = v;
c8677aaf
SA
343 pr_debug("bus-width %u\n", v);
344 }
ca50c197 345
c8677aaf 346 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
ca50c197 347 bus->data_shift = v;
c8677aaf
SA
348 pr_debug("data-shift %u\n", v);
349 }
ca50c197 350
c8677aaf 351 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
e9be1b86
SA
352 flags &= ~(V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH |
353 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW);
ca50c197
SA
354 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
355 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
c8677aaf
SA
356 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
357 }
ca50c197 358
c8677aaf 359 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
e9be1b86
SA
360 flags &= ~(V4L2_MBUS_DATA_ENABLE_HIGH |
361 V4L2_MBUS_DATA_ENABLE_LOW);
9b04fcc1
JM
362 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
363 V4L2_MBUS_DATA_ENABLE_LOW;
c8677aaf
SA
364 pr_debug("data-enable-active %s\n", v ? "high" : "low");
365 }
9b04fcc1 366
175b18b8
SA
367 switch (bus_type) {
368 default:
369 bus->flags = flags;
370 if (flags & PARALLEL_MBUS_FLAGS)
371 vep->bus_type = V4L2_MBUS_PARALLEL;
372 else
373 vep->bus_type = V4L2_MBUS_BT656;
374 break;
26c1126c 375 case V4L2_MBUS_PARALLEL:
2835b5b1 376 vep->bus_type = V4L2_MBUS_PARALLEL;
175b18b8
SA
377 bus->flags = flags;
378 break;
26c1126c 379 case V4L2_MBUS_BT656:
2835b5b1 380 vep->bus_type = V4L2_MBUS_BT656;
175b18b8
SA
381 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
382 break;
383 }
ca50c197
SA
384}
385
abc5b2cb
MCC
386static void
387v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
388 struct v4l2_fwnode_endpoint *vep,
26c1126c 389 enum v4l2_mbus_type bus_type)
97bbdf02 390{
94d964e5 391 struct v4l2_mbus_config_mipi_csi1 *bus = &vep->bus.mipi_csi1;
97bbdf02
SA
392 u32 v;
393
c8677aaf 394 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
97bbdf02 395 bus->clock_inv = v;
c8677aaf
SA
396 pr_debug("clock-inv %u\n", v);
397 }
97bbdf02 398
c8677aaf 399 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
97bbdf02 400 bus->strobe = v;
c8677aaf
SA
401 pr_debug("strobe %u\n", v);
402 }
97bbdf02 403
c8677aaf 404 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
97bbdf02 405 bus->data_lane = v;
c8677aaf
SA
406 pr_debug("data-lanes %u\n", v);
407 }
97bbdf02 408
c8677aaf 409 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
97bbdf02 410 bus->clock_lane = v;
c8677aaf
SA
411 pr_debug("clock-lanes %u\n", v);
412 }
97bbdf02 413
26c1126c 414 if (bus_type == V4L2_MBUS_CCP2)
97bbdf02
SA
415 vep->bus_type = V4L2_MBUS_CCP2;
416 else
417 vep->bus_type = V4L2_MBUS_CSI1;
418}
419
c8677aaf
SA
420static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
421 struct v4l2_fwnode_endpoint *vep)
ca50c197 422{
e7b2f518 423 u32 bus_type = V4L2_FWNODE_BUS_TYPE_GUESS;
26c1126c 424 enum v4l2_mbus_type mbus_type;
ca50c197
SA
425 int rval;
426
5adf3edd 427 pr_debug("===== begin parsing endpoint %pfw\n", fwnode);
c8677aaf 428
e07a41f9 429 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
3eb32c26
SA
430 pr_debug("fwnode video bus type %s (%u), mbus type %s (%u)\n",
431 v4l2_fwnode_bus_type_to_string(bus_type), bus_type,
432 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
433 vep->bus_type);
26c1126c 434 mbus_type = v4l2_fwnode_bus_type_to_mbus(bus_type);
69baf338
LP
435 if (mbus_type == V4L2_MBUS_INVALID) {
436 pr_debug("unsupported bus type %u\n", bus_type);
437 return -EINVAL;
438 }
26c1126c 439
e7b2f518
SA
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",
6087b215 444 v4l2_fwnode_mbus_type_to_string(vep->bus_type));
e7b2f518
SA
445 return -ENXIO;
446 }
447 } else {
448 vep->bus_type = mbus_type;
449 }
450
451 switch (vep->bus_type) {
26c1126c 452 case V4L2_MBUS_UNKNOWN:
276565ed 453 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
e7b2f518 454 V4L2_MBUS_UNKNOWN);
97bbdf02
SA
455 if (rval)
456 return rval;
2835b5b1
SA
457
458 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
6087b215
MCC
459 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
460 V4L2_MBUS_UNKNOWN);
97bbdf02 461
3eb32c26
SA
462 pr_debug("assuming media bus type %s (%u)\n",
463 v4l2_fwnode_mbus_type_to_string(vep->bus_type),
464 vep->bus_type);
465
c8677aaf 466 break;
26c1126c
SA
467 case V4L2_MBUS_CCP2:
468 case V4L2_MBUS_CSI1:
e7b2f518 469 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, vep->bus_type);
97bbdf02 470
175b18b8 471 break;
26c1126c 472 case V4L2_MBUS_CSI2_DPHY:
edc6d56c 473 case V4L2_MBUS_CSI2_CPHY:
276565ed 474 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
e7b2f518 475 vep->bus_type);
175b18b8
SA
476 if (rval)
477 return rval;
478
479 break;
26c1126c
SA
480 case V4L2_MBUS_PARALLEL:
481 case V4L2_MBUS_BT656:
e7b2f518
SA
482 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep,
483 vep->bus_type);
175b18b8 484
c8677aaf 485 break;
97bbdf02 486 default:
26c1126c 487 pr_warn("unsupported bus type %u\n", mbus_type);
97bbdf02
SA
488 return -EINVAL;
489 }
c8677aaf 490
32593dd0
SA
491 fwnode_graph_parse_endpoint(fwnode, &vep->base);
492
c8677aaf
SA
493 return 0;
494}
495
496int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
497 struct v4l2_fwnode_endpoint *vep)
498{
499 int ret;
500
501 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
502
5adf3edd 503 pr_debug("===== end parsing endpoint %pfw\n", fwnode);
c8677aaf
SA
504
505 return ret;
ca50c197
SA
506}
507EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
508
ca50c197
SA
509void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
510{
511 if (IS_ERR_OR_NULL(vep))
512 return;
513
514 kfree(vep->link_frequencies);
355047f4 515 vep->link_frequencies = NULL;
ca50c197
SA
516}
517EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
518
6087b215
MCC
519int v4l2_fwnode_endpoint_alloc_parse(struct fwnode_handle *fwnode,
520 struct v4l2_fwnode_endpoint *vep)
ca50c197 521{
ca50c197
SA
522 int rval;
523
c8677aaf 524 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
ca50c197 525 if (rval < 0)
6970d37c 526 return rval;
ca50c197 527
3be9cc6a 528 rval = fwnode_property_count_u64(fwnode, "link-frequencies");
06f81520 529 if (rval > 0) {
c8677aaf
SA
530 unsigned int i;
531
06f81520
SA
532 vep->link_frequencies =
533 kmalloc_array(rval, sizeof(*vep->link_frequencies),
534 GFP_KERNEL);
6970d37c
SA
535 if (!vep->link_frequencies)
536 return -ENOMEM;
ca50c197 537
06f81520 538 vep->nr_of_link_frequencies = rval;
ca50c197 539
6087b215
MCC
540 rval = fwnode_property_read_u64_array(fwnode,
541 "link-frequencies",
542 vep->link_frequencies,
543 vep->nr_of_link_frequencies);
6970d37c
SA
544 if (rval < 0) {
545 v4l2_fwnode_endpoint_free(vep);
546 return rval;
547 }
c8677aaf
SA
548
549 for (i = 0; i < vep->nr_of_link_frequencies; i++)
32b6e400
SA
550 pr_debug("link-frequencies %u value %llu\n", i,
551 vep->link_frequencies[i]);
06f81520 552 }
ca50c197 553
5adf3edd 554 pr_debug("===== end parsing endpoint %pfw\n", fwnode);
c8677aaf 555
6970d37c 556 return 0;
ca50c197
SA
557}
558EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
559
507a0ba9 560int v4l2_fwnode_parse_link(struct fwnode_handle *fwnode,
ca50c197
SA
561 struct v4l2_fwnode_link *link)
562{
507a0ba9 563 struct fwnode_endpoint fwep;
ca50c197
SA
564
565 memset(link, 0, sizeof(*link));
566
507a0ba9 567 fwnode_graph_parse_endpoint(fwnode, &fwep);
5e316ff5 568 link->local_id = fwep.id;
507a0ba9
MF
569 link->local_port = fwep.port;
570 link->local_node = fwnode_graph_get_port_parent(fwnode);
ca50c197 571
507a0ba9 572 fwnode = fwnode_graph_get_remote_endpoint(fwnode);
ca50c197
SA
573 if (!fwnode) {
574 fwnode_handle_put(fwnode);
575 return -ENOLINK;
576 }
577
507a0ba9 578 fwnode_graph_parse_endpoint(fwnode, &fwep);
5e316ff5 579 link->remote_id = fwep.id;
507a0ba9
MF
580 link->remote_port = fwep.port;
581 link->remote_node = fwnode_graph_get_port_parent(fwnode);
ca50c197
SA
582
583 return 0;
584}
585EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
586
ca50c197
SA
587void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
588{
589 fwnode_handle_put(link->local_node);
590 fwnode_handle_put(link->remote_node);
591}
592EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
593
dfc22c07
MF
594static const struct v4l2_fwnode_connector_conv {
595 enum v4l2_connector_type type;
596 const char *compatible;
597} connectors[] = {
598 {
599 .type = V4L2_CONN_COMPOSITE,
600 .compatible = "composite-video-connector",
601 }, {
602 .type = V4L2_CONN_SVIDEO,
603 .compatible = "svideo-connector",
604 },
605};
606
607static enum v4l2_connector_type
608v4l2_fwnode_string_to_connector_type(const char *con_str)
609{
610 unsigned int i;
611
612 for (i = 0; i < ARRAY_SIZE(connectors); i++)
613 if (!strcmp(con_str, connectors[i].compatible))
614 return connectors[i].type;
615
616 return V4L2_CONN_UNKNOWN;
617}
618
619static void
620v4l2_fwnode_connector_parse_analog(struct fwnode_handle *fwnode,
621 struct v4l2_fwnode_connector *vc)
622{
623 u32 stds;
624 int ret;
625
626 ret = fwnode_property_read_u32(fwnode, "sdtv-standards", &stds);
627
628 /* The property is optional. */
629 vc->connector.analog.sdtv_stds = ret ? V4L2_STD_ALL : stds;
630}
631
632void v4l2_fwnode_connector_free(struct v4l2_fwnode_connector *connector)
633{
634 struct v4l2_connector_link *link, *tmp;
635
636 if (IS_ERR_OR_NULL(connector) || connector->type == V4L2_CONN_UNKNOWN)
637 return;
638
639 list_for_each_entry_safe(link, tmp, &connector->links, head) {
640 v4l2_fwnode_put_link(&link->fwnode_link);
641 list_del(&link->head);
642 kfree(link);
643 }
644
645 kfree(connector->label);
646 connector->label = NULL;
647 connector->type = V4L2_CONN_UNKNOWN;
648}
649EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_free);
650
651static enum v4l2_connector_type
652v4l2_fwnode_get_connector_type(struct fwnode_handle *fwnode)
653{
654 const char *type_name;
655 int err;
656
657 if (!fwnode)
658 return V4L2_CONN_UNKNOWN;
659
660 /* The connector-type is stored within the compatible string. */
661 err = fwnode_property_read_string(fwnode, "compatible", &type_name);
662 if (err)
663 return V4L2_CONN_UNKNOWN;
664
665 return v4l2_fwnode_string_to_connector_type(type_name);
666}
667
668int v4l2_fwnode_connector_parse(struct fwnode_handle *fwnode,
669 struct v4l2_fwnode_connector *connector)
670{
671 struct fwnode_handle *connector_node;
672 enum v4l2_connector_type connector_type;
673 const char *label;
674 int err;
675
676 if (!fwnode)
677 return -EINVAL;
678
679 memset(connector, 0, sizeof(*connector));
680
681 INIT_LIST_HEAD(&connector->links);
682
683 connector_node = fwnode_graph_get_port_parent(fwnode);
684 connector_type = v4l2_fwnode_get_connector_type(connector_node);
685 if (connector_type == V4L2_CONN_UNKNOWN) {
686 fwnode_handle_put(connector_node);
687 connector_node = fwnode_graph_get_remote_port_parent(fwnode);
688 connector_type = v4l2_fwnode_get_connector_type(connector_node);
689 }
690
691 if (connector_type == V4L2_CONN_UNKNOWN) {
692 pr_err("Unknown connector type\n");
693 err = -ENOTCONN;
694 goto out;
695 }
696
697 connector->type = connector_type;
698 connector->name = fwnode_get_name(connector_node);
699 err = fwnode_property_read_string(connector_node, "label", &label);
700 connector->label = err ? NULL : kstrdup_const(label, GFP_KERNEL);
701
702 /* Parse the connector specific properties. */
703 switch (connector->type) {
704 case V4L2_CONN_COMPOSITE:
705 case V4L2_CONN_SVIDEO:
706 v4l2_fwnode_connector_parse_analog(connector_node, connector);
707 break;
708 /* Avoid compiler warnings */
709 case V4L2_CONN_UNKNOWN:
710 break;
711 }
712
713out:
714 fwnode_handle_put(connector_node);
715
716 return err;
717}
718EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_parse);
719
720int v4l2_fwnode_connector_add_link(struct fwnode_handle *fwnode,
721 struct v4l2_fwnode_connector *connector)
722{
723 struct fwnode_handle *connector_ep;
724 struct v4l2_connector_link *link;
725 int err;
726
727 if (!fwnode || !connector || connector->type == V4L2_CONN_UNKNOWN)
728 return -EINVAL;
729
730 connector_ep = fwnode_graph_get_remote_endpoint(fwnode);
731 if (!connector_ep)
732 return -ENOTCONN;
733
734 link = kzalloc(sizeof(*link), GFP_KERNEL);
735 if (!link) {
736 err = -ENOMEM;
737 goto err;
738 }
739
740 err = v4l2_fwnode_parse_link(connector_ep, &link->fwnode_link);
741 if (err)
742 goto err;
743
744 fwnode_handle_put(connector_ep);
745
746 list_add(&link->head, &connector->links);
747 connector->nr_of_links++;
748
749 return 0;
750
751err:
752 kfree(link);
753 fwnode_handle_put(connector_ep);
754
755 return err;
756}
757EXPORT_SYMBOL_GPL(v4l2_fwnode_connector_add_link);
758
344897ef
JM
759int v4l2_fwnode_device_parse(struct device *dev,
760 struct v4l2_fwnode_device_properties *props)
761{
762 struct fwnode_handle *fwnode = dev_fwnode(dev);
763 u32 val;
764 int ret;
765
766 memset(props, 0, sizeof(*props));
767
768 props->orientation = V4L2_FWNODE_PROPERTY_UNSET;
769 ret = fwnode_property_read_u32(fwnode, "orientation", &val);
770 if (!ret) {
771 switch (val) {
772 case V4L2_FWNODE_ORIENTATION_FRONT:
773 case V4L2_FWNODE_ORIENTATION_BACK:
774 case V4L2_FWNODE_ORIENTATION_EXTERNAL:
775 break;
776 default:
777 dev_warn(dev, "Unsupported device orientation: %u\n", val);
778 return -EINVAL;
779 }
780
781 props->orientation = val;
782 dev_dbg(dev, "device orientation: %u\n", val);
783 }
784
785 props->rotation = V4L2_FWNODE_PROPERTY_UNSET;
786 ret = fwnode_property_read_u32(fwnode, "rotation", &val);
787 if (!ret) {
788 if (val >= 360) {
789 dev_warn(dev, "Unsupported device rotation: %u\n", val);
790 return -EINVAL;
791 }
792
793 props->rotation = val;
794 dev_dbg(dev, "device rotation: %u\n", val);
795 }
796
797 return 0;
798}
799EXPORT_SYMBOL_GPL(v4l2_fwnode_device_parse);
800
6087b215 801static int
3c8c1539
SA
802v4l2_async_nf_fwnode_parse_endpoint(struct device *dev,
803 struct v4l2_async_notifier *notifier,
804 struct fwnode_handle *endpoint,
805 unsigned int asd_struct_size,
806 parse_endpoint_func parse_endpoint)
9ca46531 807{
6970d37c 808 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
9ca46531 809 struct v4l2_async_subdev *asd;
6970d37c 810 int ret;
9ca46531
SA
811
812 asd = kzalloc(asd_struct_size, GFP_KERNEL);
813 if (!asd)
814 return -ENOMEM;
815
816 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
4e48afec 817 asd->match.fwnode =
9ca46531 818 fwnode_graph_get_remote_port_parent(endpoint);
4e48afec 819 if (!asd->match.fwnode) {
dceccec1 820 dev_dbg(dev, "no remote endpoint found\n");
4382f37b 821 ret = -ENOTCONN;
9ca46531
SA
822 goto out_err;
823 }
824
6970d37c
SA
825 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
826 if (ret) {
9ca46531
SA
827 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
828 ret);
829 goto out_err;
830 }
831
6970d37c 832 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
9ca46531 833 if (ret == -ENOTCONN)
6970d37c
SA
834 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
835 vep.base.id);
9ca46531
SA
836 else if (ret < 0)
837 dev_warn(dev,
838 "driver could not parse port@%u/endpoint@%u (%d)\n",
6970d37c
SA
839 vep.base.port, vep.base.id, ret);
840 v4l2_fwnode_endpoint_free(&vep);
9ca46531
SA
841 if (ret < 0)
842 goto out_err;
843
3c8c1539 844 ret = __v4l2_async_nf_add_subdev(notifier, asd);
eae2aed1
SL
845 if (ret < 0) {
846 /* not an error if asd already exists */
847 if (ret == -EEXIST)
848 ret = 0;
849 goto out_err;
850 }
9ca46531
SA
851
852 return 0;
853
854out_err:
4e48afec 855 fwnode_handle_put(asd->match.fwnode);
9ca46531
SA
856 kfree(asd);
857
858 return ret == -ENOTCONN ? 0 : ret;
859}
860
10aacfec
NS
861int
862v4l2_async_nf_parse_fwnode_endpoints(struct device *dev,
863 struct v4l2_async_notifier *notifier,
864 size_t asd_struct_size,
865 parse_endpoint_func parse_endpoint)
9ca46531
SA
866{
867 struct fwnode_handle *fwnode;
eae2aed1 868 int ret = 0;
9ca46531
SA
869
870 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
871 return -EINVAL;
872
106ee387 873 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
9ca46531
SA
874 struct fwnode_handle *dev_fwnode;
875 bool is_available;
876
877 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
878 is_available = fwnode_device_is_available(dev_fwnode);
879 fwnode_handle_put(dev_fwnode);
1acce5f7 880 if (!is_available)
9ca46531
SA
881 continue;
882
9ca46531 883
3c8c1539
SA
884 ret = v4l2_async_nf_fwnode_parse_endpoint(dev, notifier,
885 fwnode,
886 asd_struct_size,
887 parse_endpoint);
9ca46531
SA
888 if (ret < 0)
889 break;
890 }
891
892 fwnode_handle_put(fwnode);
893
894 return ret;
895}
3c8c1539 896EXPORT_SYMBOL_GPL(v4l2_async_nf_parse_fwnode_endpoints);
9ca46531 897
d8428539
SA
898/*
899 * v4l2_fwnode_reference_parse - parse references for async sub-devices
900 * @dev: the device node the properties of which are parsed for references
901 * @notifier: the async notifier where the async subdevs will be added
902 * @prop: the name of the property
903 *
904 * Return: 0 on success
905 * -ENOENT if no entries were found
906 * -ENOMEM if memory allocation failed
907 * -EINVAL if property parsing failed
908 */
6087b215
MCC
909static int v4l2_fwnode_reference_parse(struct device *dev,
910 struct v4l2_async_notifier *notifier,
911 const char *prop)
d8428539
SA
912{
913 struct fwnode_reference_args args;
914 unsigned int index;
915 int ret;
916
917 for (index = 0;
5f1501fd
SA
918 !(ret = fwnode_property_get_reference_args(dev_fwnode(dev), prop,
919 NULL, 0, index, &args));
d8428539
SA
920 index++) {
921 struct v4l2_async_subdev *asd;
922
3c8c1539
SA
923 asd = v4l2_async_nf_add_fwnode(notifier, args.fwnode,
924 struct v4l2_async_subdev);
016413d9 925 fwnode_handle_put(args.fwnode);
eae2aed1 926 if (IS_ERR(asd)) {
eae2aed1 927 /* not an error if asd already exists */
016413d9 928 if (PTR_ERR(asd) == -EEXIST)
eae2aed1 929 continue;
d8428539 930
016413d9 931 return PTR_ERR(asd);
d8428539 932 }
d8428539
SA
933 }
934
5f1501fd
SA
935 /* -ENOENT here means successful parsing */
936 if (ret != -ENOENT)
937 return ret;
938
939 /* Return -ENOENT if no references were found */
940 return index ? 0 : -ENOENT;
d8428539
SA
941}
942
a1699a4e
SA
943/*
944 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
945 * arguments
946 * @fwnode: fwnode to read @prop from
947 * @notifier: notifier for @dev
948 * @prop: the name of the property
949 * @index: the index of the reference to get
950 * @props: the array of integer property names
951 * @nprops: the number of integer property names in @nprops
952 *
953 * First find an fwnode referred to by the reference at @index in @prop.
954 *
955 * Then under that fwnode, @nprops times, for each property in @props,
956 * iteratively follow child nodes starting from fwnode such that they have the
957 * property in @props array at the index of the child node distance from the
958 * root node and the value of that property matching with the integer argument
959 * of the reference, at the same index.
960 *
4faf7066 961 * The child fwnode reached at the end of the iteration is then returned to the
a1699a4e
SA
962 * caller.
963 *
964 * The core reason for this is that you cannot refer to just any node in ACPI.
965 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
966 * provide a list of (property name, property value) tuples where each tuple
967 * uniquely identifies a child node. The first tuple identifies a child directly
968 * underneath the device fwnode, the next tuple identifies a child node
969 * underneath the fwnode identified by the previous tuple, etc. until you
970 * reached the fwnode you need.
971 *
8a3946ca
SA
972 * THIS EXAMPLE EXISTS MERELY TO DOCUMENT THIS FUNCTION. DO NOT USE IT AS A
973 * REFERENCE IN HOW ACPI TABLES SHOULD BE WRITTEN!! See documentation under
3ecad8c2 974 * Documentation/firmware-guide/acpi/dsd/ instead and especially graph.txt,
8a3946ca 975 * data-node-references.txt and leds.txt .
a1699a4e
SA
976 *
977 * Scope (\_SB.PCI0.I2C2)
978 * {
979 * Device (CAM0)
980 * {
981 * Name (_DSD, Package () {
982 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
983 * Package () {
984 * Package () {
985 * "compatible",
986 * Package () { "nokia,smia" }
987 * },
988 * },
989 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
990 * Package () {
991 * Package () { "port0", "PRT0" },
992 * }
993 * })
994 * Name (PRT0, Package() {
995 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
996 * Package () {
997 * Package () { "port", 0 },
998 * },
999 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
1000 * Package () {
1001 * Package () { "endpoint0", "EP00" },
1002 * }
1003 * })
1004 * Name (EP00, Package() {
1005 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
1006 * Package () {
1007 * Package () { "endpoint", 0 },
1008 * Package () {
1009 * "remote-endpoint",
1010 * Package() {
1011 * \_SB.PCI0.ISP, 4, 0
1012 * }
1013 * },
1014 * }
1015 * })
1016 * }
1017 * }
1018 *
1019 * Scope (\_SB.PCI0)
1020 * {
1021 * Device (ISP)
1022 * {
1023 * Name (_DSD, Package () {
1024 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
1025 * Package () {
1026 * Package () { "port4", "PRT4" },
1027 * }
1028 * })
1029 *
1030 * Name (PRT4, Package() {
1031 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
1032 * Package () {
1033 * Package () { "port", 4 },
1034 * },
1035 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
1036 * Package () {
1037 * Package () { "endpoint0", "EP40" },
1038 * }
1039 * })
1040 *
1041 * Name (EP40, Package() {
1042 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
1043 * Package () {
1044 * Package () { "endpoint", 0 },
1045 * Package () {
1046 * "remote-endpoint",
1047 * Package () {
1048 * \_SB.PCI0.I2C2.CAM0,
1049 * 0, 0
1050 * }
1051 * },
1052 * }
1053 * })
1054 * }
1055 * }
1056 *
1057 * From the EP40 node under ISP device, you could parse the graph remote
1058 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
1059 *
1060 * @fwnode: fwnode referring to EP40 under ISP.
1061 * @prop: "remote-endpoint"
1062 * @index: 0
1063 * @props: "port", "endpoint"
1064 * @nprops: 2
1065 *
1066 * And you'd get back fwnode referring to EP00 under CAM0.
1067 *
1068 * The same works the other way around: if you use EP00 under CAM0 as the
1069 * fwnode, you'll get fwnode referring to EP40 under ISP.
1070 *
1071 * The same example in DT syntax would look like this:
1072 *
1073 * cam: cam0 {
1074 * compatible = "nokia,smia";
1075 *
1076 * port {
1077 * port = <0>;
1078 * endpoint {
1079 * endpoint = <0>;
1080 * remote-endpoint = <&isp 4 0>;
1081 * };
1082 * };
1083 * };
1084 *
1085 * isp: isp {
1086 * ports {
1087 * port@4 {
1088 * port = <4>;
1089 * endpoint {
1090 * endpoint = <0>;
1091 * remote-endpoint = <&cam 0 0>;
1092 * };
1093 * };
1094 * };
1095 * };
1096 *
1097 * Return: 0 on success
1098 * -ENOENT if no entries (or the property itself) were found
1099 * -EINVAL if property parsing otherwise failed
1100 * -ENOMEM if memory allocation failed
1101 */
6087b215
MCC
1102static struct fwnode_handle *
1103v4l2_fwnode_reference_get_int_prop(struct fwnode_handle *fwnode,
1104 const char *prop,
1105 unsigned int index,
1106 const char * const *props,
1107 unsigned int nprops)
a1699a4e
SA
1108{
1109 struct fwnode_reference_args fwnode_args;
977d5ad3 1110 u64 *args = fwnode_args.args;
a1699a4e
SA
1111 struct fwnode_handle *child;
1112 int ret;
1113
1114 /*
1115 * Obtain remote fwnode as well as the integer arguments.
1116 *
1117 * Note that right now both -ENODATA and -ENOENT may signal
1118 * out-of-bounds access. Return -ENOENT in that case.
1119 */
1120 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
1121 index, &fwnode_args);
1122 if (ret)
1123 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
1124
1125 /*
1126 * Find a node in the tree under the referred fwnode corresponding to
1127 * the integer arguments.
1128 */
1129 fwnode = fwnode_args.fwnode;
1130 while (nprops--) {
1131 u32 val;
1132
1133 /* Loop over all child nodes under fwnode. */
1134 fwnode_for_each_child_node(fwnode, child) {
1135 if (fwnode_property_read_u32(child, *props, &val))
1136 continue;
1137
1138 /* Found property, see if its value matches. */
1139 if (val == *args)
1140 break;
1141 }
1142
1143 fwnode_handle_put(fwnode);
1144
1145 /* No property found; return an error here. */
1146 if (!child) {
1147 fwnode = ERR_PTR(-ENOENT);
1148 break;
1149 }
1150
1151 props++;
1152 args++;
1153 fwnode = child;
1154 }
1155
1156 return fwnode;
1157}
1158
be9c03e4
MCC
1159struct v4l2_fwnode_int_props {
1160 const char *name;
1161 const char * const *props;
1162 unsigned int nprops;
1163};
1164
a1699a4e
SA
1165/*
1166 * v4l2_fwnode_reference_parse_int_props - parse references for async
1167 * sub-devices
1168 * @dev: struct device pointer
1169 * @notifier: notifier for @dev
1170 * @prop: the name of the property
1171 * @props: the array of integer property names
1172 * @nprops: the number of integer properties
1173 *
1174 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
1175 * property @prop with integer arguments with child nodes matching in properties
1176 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
1177 * accordingly.
1178 *
1179 * While it is technically possible to use this function on DT, it is only
1180 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
1181 * on ACPI the references are limited to devices.
1182 *
1183 * Return: 0 on success
1184 * -ENOENT if no entries (or the property itself) were found
1185 * -EINVAL if property parsing otherwisefailed
1186 * -ENOMEM if memory allocation failed
1187 */
6087b215
MCC
1188static int
1189v4l2_fwnode_reference_parse_int_props(struct device *dev,
1190 struct v4l2_async_notifier *notifier,
be9c03e4 1191 const struct v4l2_fwnode_int_props *p)
a1699a4e
SA
1192{
1193 struct fwnode_handle *fwnode;
1194 unsigned int index;
1195 int ret;
be9c03e4
MCC
1196 const char *prop = p->name;
1197 const char * const *props = p->props;
1198 unsigned int nprops = p->nprops;
a1699a4e 1199
9879c9d3
MCC
1200 index = 0;
1201 do {
1202 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1203 prop, index,
1204 props, nprops);
1205 if (IS_ERR(fwnode)) {
1206 /*
1207 * Note that right now both -ENODATA and -ENOENT may
1208 * signal out-of-bounds access. Return the error in
1209 * cases other than that.
1210 */
1211 if (PTR_ERR(fwnode) != -ENOENT &&
1212 PTR_ERR(fwnode) != -ENODATA)
1213 return PTR_ERR(fwnode);
1214 break;
1215 }
a1699a4e 1216 fwnode_handle_put(fwnode);
9879c9d3
MCC
1217 index++;
1218 } while (1);
a1699a4e 1219
6087b215
MCC
1220 for (index = 0;
1221 !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
1222 prop, index,
1223 props,
1224 nprops)));
1225 index++) {
a1699a4e
SA
1226 struct v4l2_async_subdev *asd;
1227
3c8c1539
SA
1228 asd = v4l2_async_nf_add_fwnode(notifier, fwnode,
1229 struct v4l2_async_subdev);
016413d9 1230 fwnode_handle_put(fwnode);
eae2aed1
SL
1231 if (IS_ERR(asd)) {
1232 ret = PTR_ERR(asd);
1233 /* not an error if asd already exists */
016413d9 1234 if (ret == -EEXIST)
eae2aed1 1235 continue;
a1699a4e 1236
016413d9 1237 return PTR_ERR(asd);
a1699a4e 1238 }
a1699a4e
SA
1239 }
1240
4ace2d28 1241 return !fwnode || PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
a1699a4e
SA
1242}
1243
9e7fabbc 1244/**
3c8c1539 1245 * v4l2_async_nf_parse_fwnode_sensor - parse common references on
9e7fabbc
SA
1246 * sensors for async sub-devices
1247 * @dev: the device node the properties of which are parsed for references
1248 * @notifier: the async notifier where the async subdevs will be added
1249 *
1250 * Parse common sensor properties for remote devices related to the
1251 * sensor and set up async sub-devices for them.
1252 *
1253 * Any notifier populated using this function must be released with a call to
3c8c1539 1254 * v4l2_async_nf_release() after it has been unregistered and the async
9e7fabbc
SA
1255 * sub-devices are no longer in use, even in the case the function returned an
1256 * error.
1257 *
1258 * Return: 0 on success
1259 * -ENOMEM if memory allocation failed
1260 * -EINVAL if property parsing failed
1261 */
1262static int
3c8c1539
SA
1263v4l2_async_nf_parse_fwnode_sensor(struct device *dev,
1264 struct v4l2_async_notifier *notifier)
7a9ec808
SA
1265{
1266 static const char * const led_props[] = { "led" };
be9c03e4 1267 static const struct v4l2_fwnode_int_props props[] = {
7a9ec808
SA
1268 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
1269 { "lens-focus", NULL, 0 },
1270 };
1271 unsigned int i;
1272
1273 for (i = 0; i < ARRAY_SIZE(props); i++) {
1274 int ret;
1275
1276 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
6087b215
MCC
1277 ret = v4l2_fwnode_reference_parse_int_props(dev,
1278 notifier,
be9c03e4 1279 &props[i]);
7a9ec808 1280 else
6087b215
MCC
1281 ret = v4l2_fwnode_reference_parse(dev, notifier,
1282 props[i].name);
7a9ec808
SA
1283 if (ret && ret != -ENOENT) {
1284 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
1285 props[i].name, ret);
1286 return ret;
1287 }
1288 }
1289
1290 return 0;
1291}
7a9ec808 1292
15786f7b 1293int v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd)
aef69d54
SA
1294{
1295 struct v4l2_async_notifier *notifier;
1296 int ret;
1297
1298 if (WARN_ON(!sd->dev))
1299 return -ENODEV;
1300
1301 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1302 if (!notifier)
1303 return -ENOMEM;
1304
3c8c1539 1305 v4l2_async_nf_init(notifier);
eae2aed1 1306
b6e10ff6
HG
1307 ret = v4l2_subdev_get_privacy_led(sd);
1308 if (ret < 0)
1309 goto out_cleanup;
1310
3c8c1539 1311 ret = v4l2_async_nf_parse_fwnode_sensor(sd->dev, notifier);
aef69d54
SA
1312 if (ret < 0)
1313 goto out_cleanup;
1314
3c8c1539 1315 ret = v4l2_async_subdev_nf_register(sd, notifier);
aef69d54
SA
1316 if (ret < 0)
1317 goto out_cleanup;
1318
1319 ret = v4l2_async_register_subdev(sd);
1320 if (ret < 0)
1321 goto out_unregister;
1322
1323 sd->subdev_notifier = notifier;
1324
1325 return 0;
1326
1327out_unregister:
3c8c1539 1328 v4l2_async_nf_unregister(notifier);
aef69d54
SA
1329
1330out_cleanup:
b6e10ff6 1331 v4l2_subdev_put_privacy_led(sd);
3c8c1539 1332 v4l2_async_nf_cleanup(notifier);
aef69d54
SA
1333 kfree(notifier);
1334
1335 return ret;
1336}
15786f7b 1337EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor);
aef69d54 1338
ca50c197
SA
1339MODULE_LICENSE("GPL");
1340MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1341MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1342MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");