blob: f224b6c42c10d3358aafdec36f0edd49b4de1b65 [file] [log] [blame]
Sakari Ailusca50c192016-08-12 08:05:51 -03001/*
2 * V4L2 fwnode binding parsing library
3 *
4 * The origins of the V4L2 fwnode library are in V4L2 OF library that
5 * formerly was located in v4l2-of.c.
6 *
7 * Copyright (c) 2016 Intel Corporation.
8 * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
9 *
10 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
11 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
12 *
13 * Copyright (C) 2012 Renesas Electronics Corp.
14 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
19 */
20#include <linux/acpi.h>
21#include <linux/kernel.h>
Sakari Ailus9ca46532017-08-17 11:28:21 -040022#include <linux/mm.h>
Sakari Ailusca50c192016-08-12 08:05:51 -030023#include <linux/module.h>
24#include <linux/of.h>
25#include <linux/property.h>
26#include <linux/slab.h>
27#include <linux/string.h>
28#include <linux/types.h>
29
Sakari Ailus9ca46532017-08-17 11:28:21 -040030#include <media/v4l2-async.h>
Sakari Ailusca50c192016-08-12 08:05:51 -030031#include <media/v4l2-fwnode.h>
Sakari Ailusaef69d52017-09-24 18:47:44 -040032#include <media/v4l2-subdev.h>
Sakari Ailusca50c192016-08-12 08:05:51 -030033
Sakari Ailuse07a41f2015-02-25 14:51:01 -050034enum v4l2_fwnode_bus_type {
35 V4L2_FWNODE_BUS_TYPE_GUESS = 0,
36 V4L2_FWNODE_BUS_TYPE_CSI2_CPHY,
37 V4L2_FWNODE_BUS_TYPE_CSI1,
38 V4L2_FWNODE_BUS_TYPE_CCP2,
Sakari Ailusbf638562018-07-03 17:40:37 -040039 V4L2_FWNODE_BUS_TYPE_CSI2_DPHY,
40 V4L2_FWNODE_BUS_TYPE_PARALLEL,
41 V4L2_FWNODE_BUS_TYPE_BT656,
Sakari Ailuse07a41f2015-02-25 14:51:01 -050042 NR_OF_V4L2_FWNODE_BUS_TYPE,
43};
44
Sakari Ailusf3112732017-02-20 05:42:09 -050045static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
Sakari Ailus276565e2018-07-18 09:33:45 -040046 struct v4l2_fwnode_endpoint *vep,
47 enum v4l2_fwnode_bus_type bus_type)
Sakari Ailusca50c192016-08-12 08:05:51 -030048{
49 struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
Sakari Ailusb4357d22018-07-20 09:57:48 -040050 bool have_clk_lane = false, have_data_lanes = false,
51 have_lane_polarities = false;
Sakari Ailusca50c192016-08-12 08:05:51 -030052 unsigned int flags = 0, lanes_used = 0;
Sakari Ailus276565e2018-07-18 09:33:45 -040053 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
Sakari Ailusb4357d22018-07-20 09:57:48 -040054 u32 clock_lane = 0;
Sakari Ailus276565e2018-07-18 09:33:45 -040055 unsigned int num_data_lanes = 0;
Sakari Ailusb4357d22018-07-20 09:57:48 -040056 bool use_default_lane_mapping = false;
Sakari Ailusca50c192016-08-12 08:05:51 -030057 unsigned int i;
58 u32 v;
59 int rval;
60
Sakari Ailusc2475ae2018-07-20 06:14:14 -040061 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY) {
Sakari Ailusb4357d22018-07-20 09:57:48 -040062 use_default_lane_mapping = true;
63
Sakari Ailus276565e2018-07-18 09:33:45 -040064 num_data_lanes = min_t(u32, bus->num_data_lanes,
65 V4L2_FWNODE_CSI2_MAX_DATA_LANES);
66
Sakari Ailusb4357d22018-07-20 09:57:48 -040067 clock_lane = bus->clock_lane;
68 if (clock_lane)
69 use_default_lane_mapping = false;
70
71 for (i = 0; i < num_data_lanes; i++) {
Sakari Ailusc2475ae2018-07-20 06:14:14 -040072 array[i] = bus->data_lanes[i];
Sakari Ailusb4357d22018-07-20 09:57:48 -040073 if (array[i])
74 use_default_lane_mapping = false;
75 }
76
77 if (use_default_lane_mapping)
78 pr_debug("using default lane mapping\n");
Sakari Ailusc2475ae2018-07-20 06:14:14 -040079 }
80
Sakari Ailusca50c192016-08-12 08:05:51 -030081 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
82 if (rval > 0) {
Sakari Ailus276565e2018-07-18 09:33:45 -040083 num_data_lanes =
Sakari Ailusad3cdf32017-08-14 06:43:07 -040084 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
Sakari Ailusca50c192016-08-12 08:05:51 -030085
86 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
Sakari Ailus276565e2018-07-18 09:33:45 -040087 num_data_lanes);
Sakari Ailusb4357d22018-07-20 09:57:48 -040088
89 have_data_lanes = true;
Sakari Ailusc2475ae2018-07-20 06:14:14 -040090 }
Sakari Ailusca50c192016-08-12 08:05:51 -030091
Sakari Ailusc2475ae2018-07-20 06:14:14 -040092 for (i = 0; i < num_data_lanes; i++) {
Sakari Ailusb4357d22018-07-20 09:57:48 -040093 if (lanes_used & BIT(array[i])) {
94 if (have_data_lanes || !use_default_lane_mapping)
95 pr_warn("duplicated lane %u in data-lanes, using defaults\n",
96 array[i]);
97 use_default_lane_mapping = true;
98 }
Sakari Ailusc2475ae2018-07-20 06:14:14 -040099 lanes_used |= BIT(array[i]);
Sakari Ailusca50c192016-08-12 08:05:51 -0300100
Sakari Ailusb4357d22018-07-20 09:57:48 -0400101 if (have_data_lanes)
102 pr_debug("lane %u position %u\n", i, array[i]);
Sakari Ailus276565e2018-07-18 09:33:45 -0400103 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300104
Sakari Ailus276565e2018-07-18 09:33:45 -0400105 rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL,
106 0);
107 if (rval > 0) {
108 if (rval != 1 + num_data_lanes /* clock+data */) {
109 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
110 1 + num_data_lanes, rval);
111 return -EINVAL;
Sakari Ailusca50c192016-08-12 08:05:51 -0300112 }
Sakari Ailusb24f0212017-08-14 06:15:21 -0400113
Sakari Ailusaf11a742018-07-03 07:06:20 -0400114 have_lane_polarities = true;
Sakari Ailusca50c192016-08-12 08:05:51 -0300115 }
116
117 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
Sakari Ailusb4357d22018-07-20 09:57:48 -0400118 clock_lane = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500119 pr_debug("clock lane position %u\n", v);
Sakari Ailusb4357d22018-07-20 09:57:48 -0400120 have_clk_lane = true;
121 }
122
123 if (lanes_used & BIT(clock_lane)) {
124 if (have_clk_lane || !use_default_lane_mapping)
125 pr_warn("duplicated lane %u in clock-lanes, using defaults\n",
126 v);
127 use_default_lane_mapping = true;
Sakari Ailusca50c192016-08-12 08:05:51 -0300128 }
129
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500130 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300131 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500132 pr_debug("non-continuous clock\n");
Sakari Ailusd4865322018-07-20 05:04:29 -0400133 } else {
Sakari Ailusca50c192016-08-12 08:05:51 -0300134 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500135 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300136
Sakari Ailus276565e2018-07-18 09:33:45 -0400137 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY || lanes_used ||
138 have_clk_lane || (flags & ~V4L2_MBUS_CSI2_CONTINUOUS_CLOCK)) {
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400139 bus->flags = flags;
140 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
Sakari Ailus276565e2018-07-18 09:33:45 -0400141 bus->num_data_lanes = num_data_lanes;
Sakari Ailusb4357d22018-07-20 09:57:48 -0400142
143 if (use_default_lane_mapping) {
144 bus->clock_lane = 0;
145 for (i = 0; i < num_data_lanes; i++)
146 bus->data_lanes[i] = 1 + i;
147 } else {
148 bus->clock_lane = clock_lane;
149 for (i = 0; i < num_data_lanes; i++)
150 bus->data_lanes[i] = array[i];
151 }
Sakari Ailusaf11a742018-07-03 07:06:20 -0400152
153 if (have_lane_polarities) {
154 fwnode_property_read_u32_array(fwnode,
155 "lane-polarities", array,
156 1 + num_data_lanes);
157
158 for (i = 0; i < 1 + num_data_lanes; i++) {
159 bus->lane_polarities[i] = array[i];
160 pr_debug("lane %u polarity %sinverted",
161 i, array[i] ? "" : "not ");
162 }
163 } else {
164 pr_debug("no lane polarities defined, assuming not inverted\n");
165 }
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400166 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300167
168 return 0;
169}
170
Sakari Ailus175b18b2018-01-02 08:14:30 -0500171#define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
172 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
173 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
174 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
175 V4L2_MBUS_FIELD_EVEN_HIGH | \
176 V4L2_MBUS_FIELD_EVEN_LOW)
177
Sakari Ailusca50c192016-08-12 08:05:51 -0300178static void v4l2_fwnode_endpoint_parse_parallel_bus(
Sakari Ailus175b18b2018-01-02 08:14:30 -0500179 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep,
180 enum v4l2_fwnode_bus_type bus_type)
Sakari Ailusca50c192016-08-12 08:05:51 -0300181{
182 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
183 unsigned int flags = 0;
184 u32 v;
185
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500186 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300187 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
188 V4L2_MBUS_HSYNC_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500189 pr_debug("hsync-active %s\n", v ? "high" : "low");
190 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300191
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500192 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300193 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
194 V4L2_MBUS_VSYNC_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500195 pr_debug("vsync-active %s\n", v ? "high" : "low");
196 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300197
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500198 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300199 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
200 V4L2_MBUS_FIELD_EVEN_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500201 pr_debug("field-even-active %s\n", v ? "high" : "low");
202 }
203
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500204 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300205 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
206 V4L2_MBUS_PCLK_SAMPLE_FALLING;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500207 pr_debug("pclk-sample %s\n", v ? "high" : "low");
208 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300209
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500210 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300211 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
212 V4L2_MBUS_DATA_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500213 pr_debug("data-active %s\n", v ? "high" : "low");
214 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300215
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500216 if (fwnode_property_present(fwnode, "slave-mode")) {
217 pr_debug("slave mode\n");
Sakari Ailusca50c192016-08-12 08:05:51 -0300218 flags |= V4L2_MBUS_SLAVE;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500219 } else {
Sakari Ailusca50c192016-08-12 08:05:51 -0300220 flags |= V4L2_MBUS_MASTER;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500221 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300222
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500223 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300224 bus->bus_width = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500225 pr_debug("bus-width %u\n", v);
226 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300227
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500228 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300229 bus->data_shift = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500230 pr_debug("data-shift %u\n", v);
231 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300232
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500233 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300234 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
235 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500236 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
237 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300238
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500239 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
Jacopo Mondi9b04fcc2018-07-09 10:19:19 -0400240 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
241 V4L2_MBUS_DATA_ENABLE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500242 pr_debug("data-enable-active %s\n", v ? "high" : "low");
243 }
Jacopo Mondi9b04fcc2018-07-09 10:19:19 -0400244
Sakari Ailus175b18b2018-01-02 08:14:30 -0500245 switch (bus_type) {
246 default:
247 bus->flags = flags;
248 if (flags & PARALLEL_MBUS_FLAGS)
249 vep->bus_type = V4L2_MBUS_PARALLEL;
250 else
251 vep->bus_type = V4L2_MBUS_BT656;
252 break;
253 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400254 vep->bus_type = V4L2_MBUS_PARALLEL;
Sakari Ailus175b18b2018-01-02 08:14:30 -0500255 bus->flags = flags;
256 break;
257 case V4L2_FWNODE_BUS_TYPE_BT656:
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400258 vep->bus_type = V4L2_MBUS_BT656;
Sakari Ailus175b18b2018-01-02 08:14:30 -0500259 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
260 break;
261 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300262}
263
Mauro Carvalho Chehababc5b2c2017-07-20 16:27:27 -0400264static void
265v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
266 struct v4l2_fwnode_endpoint *vep,
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400267 enum v4l2_fwnode_bus_type bus_type)
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500268{
269 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
270 u32 v;
271
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500272 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500273 bus->clock_inv = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500274 pr_debug("clock-inv %u\n", v);
275 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500276
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500277 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500278 bus->strobe = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500279 pr_debug("strobe %u\n", v);
280 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500281
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500282 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500283 bus->data_lane = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500284 pr_debug("data-lanes %u\n", v);
285 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500286
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500287 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500288 bus->clock_lane = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500289 pr_debug("clock-lanes %u\n", v);
290 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500291
292 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
293 vep->bus_type = V4L2_MBUS_CCP2;
294 else
295 vep->bus_type = V4L2_MBUS_CSI1;
296}
297
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500298static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
299 struct v4l2_fwnode_endpoint *vep)
Sakari Ailusca50c192016-08-12 08:05:51 -0300300{
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500301 u32 bus_type = 0;
Sakari Ailusca50c192016-08-12 08:05:51 -0300302 int rval;
303
Sakari Ailus9a5b4b72018-07-31 05:21:53 -0400304 if (vep->bus_type == V4L2_MBUS_UNKNOWN) {
305 /* Zero fields from bus union to until the end */
306 memset(&vep->bus, 0,
307 sizeof(*vep) - offsetof(typeof(*vep), bus));
308 }
309
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500310 pr_debug("===== begin V4L2 endpoint properties\n");
311
Sakari Ailus32593dd2018-07-18 09:20:34 -0400312 /*
313 * Zero the fwnode graph endpoint memory in case we don't end up parsing
314 * the endpoint.
315 */
316 memset(&vep->base, 0, sizeof(vep->base));
Sakari Ailusca50c192016-08-12 08:05:51 -0300317
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500318 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
319
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500320 switch (bus_type) {
321 case V4L2_FWNODE_BUS_TYPE_GUESS:
Sakari Ailus276565e2018-07-18 09:33:45 -0400322 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
323 bus_type);
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500324 if (rval)
325 return rval;
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400326
327 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
Sakari Ailus175b18b2018-01-02 08:14:30 -0500328 v4l2_fwnode_endpoint_parse_parallel_bus(
329 fwnode, vep, V4L2_MBUS_UNKNOWN);
Sakari Ailusca50c192016-08-12 08:05:51 -0300330
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500331 break;
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500332 case V4L2_FWNODE_BUS_TYPE_CCP2:
333 case V4L2_FWNODE_BUS_TYPE_CSI1:
334 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
335
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500336 break;
Sakari Ailus175b18b2018-01-02 08:14:30 -0500337 case V4L2_FWNODE_BUS_TYPE_CSI2_DPHY:
338 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
Sakari Ailus276565e2018-07-18 09:33:45 -0400339 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
340 bus_type);
Sakari Ailus175b18b2018-01-02 08:14:30 -0500341 if (rval)
342 return rval;
343
344 break;
345 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
346 case V4L2_FWNODE_BUS_TYPE_BT656:
347 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep, bus_type);
348
349 break;
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500350 default:
351 pr_warn("unsupported bus type %u\n", bus_type);
352 return -EINVAL;
353 }
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500354
Sakari Ailus32593dd2018-07-18 09:20:34 -0400355 fwnode_graph_parse_endpoint(fwnode, &vep->base);
356
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500357 return 0;
358}
359
360int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
361 struct v4l2_fwnode_endpoint *vep)
362{
363 int ret;
364
365 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
366
367 pr_debug("===== end V4L2 endpoint properties\n");
368
369 return ret;
Sakari Ailusca50c192016-08-12 08:05:51 -0300370}
371EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
372
Sakari Ailusca50c192016-08-12 08:05:51 -0300373void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
374{
375 if (IS_ERR_OR_NULL(vep))
376 return;
377
378 kfree(vep->link_frequencies);
Sakari Ailusca50c192016-08-12 08:05:51 -0300379}
380EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
381
Sakari Ailus6970d372018-06-02 12:19:35 -0400382int v4l2_fwnode_endpoint_alloc_parse(
383 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
Sakari Ailusca50c192016-08-12 08:05:51 -0300384{
Sakari Ailusca50c192016-08-12 08:05:51 -0300385 int rval;
386
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500387 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
Sakari Ailusca50c192016-08-12 08:05:51 -0300388 if (rval < 0)
Sakari Ailus6970d372018-06-02 12:19:35 -0400389 return rval;
Sakari Ailusca50c192016-08-12 08:05:51 -0300390
391 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
392 NULL, 0);
Sakari Ailus06f81522017-06-20 09:14:43 -0400393 if (rval > 0) {
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500394 unsigned int i;
395
Sakari Ailus06f81522017-06-20 09:14:43 -0400396 vep->link_frequencies =
397 kmalloc_array(rval, sizeof(*vep->link_frequencies),
398 GFP_KERNEL);
Sakari Ailus6970d372018-06-02 12:19:35 -0400399 if (!vep->link_frequencies)
400 return -ENOMEM;
Sakari Ailusca50c192016-08-12 08:05:51 -0300401
Sakari Ailus06f81522017-06-20 09:14:43 -0400402 vep->nr_of_link_frequencies = rval;
403
404 rval = fwnode_property_read_u64_array(
405 fwnode, "link-frequencies", vep->link_frequencies,
406 vep->nr_of_link_frequencies);
Sakari Ailus6970d372018-06-02 12:19:35 -0400407 if (rval < 0) {
408 v4l2_fwnode_endpoint_free(vep);
409 return rval;
410 }
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500411
412 for (i = 0; i < vep->nr_of_link_frequencies; i++)
413 pr_info("link-frequencies %u value %llu\n", i,
414 vep->link_frequencies[i]);
Sakari Ailusca50c192016-08-12 08:05:51 -0300415 }
416
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500417 pr_debug("===== end V4L2 endpoint properties\n");
418
Sakari Ailus6970d372018-06-02 12:19:35 -0400419 return 0;
Sakari Ailusca50c192016-08-12 08:05:51 -0300420}
421EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
422
Sakari Ailusca50c192016-08-12 08:05:51 -0300423int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
424 struct v4l2_fwnode_link *link)
425{
426 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
427 struct fwnode_handle *fwnode;
428
429 memset(link, 0, sizeof(*link));
430
431 fwnode = fwnode_get_parent(__fwnode);
432 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
433 fwnode = fwnode_get_next_parent(fwnode);
434 if (is_of_node(fwnode) &&
435 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
436 fwnode = fwnode_get_next_parent(fwnode);
437 link->local_node = fwnode;
438
439 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
440 if (!fwnode) {
441 fwnode_handle_put(fwnode);
442 return -ENOLINK;
443 }
444
445 fwnode = fwnode_get_parent(fwnode);
446 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
447 fwnode = fwnode_get_next_parent(fwnode);
448 if (is_of_node(fwnode) &&
449 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
450 fwnode = fwnode_get_next_parent(fwnode);
451 link->remote_node = fwnode;
452
453 return 0;
454}
455EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
456
Sakari Ailusca50c192016-08-12 08:05:51 -0300457void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
458{
459 fwnode_handle_put(link->local_node);
460 fwnode_handle_put(link->remote_node);
461}
462EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
463
Sakari Ailus9ca46532017-08-17 11:28:21 -0400464static int v4l2_async_notifier_fwnode_parse_endpoint(
465 struct device *dev, struct v4l2_async_notifier *notifier,
466 struct fwnode_handle *endpoint, unsigned int asd_struct_size,
467 int (*parse_endpoint)(struct device *dev,
468 struct v4l2_fwnode_endpoint *vep,
469 struct v4l2_async_subdev *asd))
470{
Sakari Ailus6970d372018-06-02 12:19:35 -0400471 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
Sakari Ailus9ca46532017-08-17 11:28:21 -0400472 struct v4l2_async_subdev *asd;
Sakari Ailus6970d372018-06-02 12:19:35 -0400473 int ret;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400474
475 asd = kzalloc(asd_struct_size, GFP_KERNEL);
476 if (!asd)
477 return -ENOMEM;
478
479 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400480 asd->match.fwnode =
Sakari Ailus9ca46532017-08-17 11:28:21 -0400481 fwnode_graph_get_remote_port_parent(endpoint);
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400482 if (!asd->match.fwnode) {
Sakari Ailus9ca46532017-08-17 11:28:21 -0400483 dev_warn(dev, "bad remote port parent\n");
Steve Longerbeam4382f372018-09-29 15:54:04 -0400484 ret = -ENOTCONN;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400485 goto out_err;
486 }
487
Sakari Ailus6970d372018-06-02 12:19:35 -0400488 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
489 if (ret) {
Sakari Ailus9ca46532017-08-17 11:28:21 -0400490 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
491 ret);
492 goto out_err;
493 }
494
Sakari Ailus6970d372018-06-02 12:19:35 -0400495 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400496 if (ret == -ENOTCONN)
Sakari Ailus6970d372018-06-02 12:19:35 -0400497 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
498 vep.base.id);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400499 else if (ret < 0)
500 dev_warn(dev,
501 "driver could not parse port@%u/endpoint@%u (%d)\n",
Sakari Ailus6970d372018-06-02 12:19:35 -0400502 vep.base.port, vep.base.id, ret);
503 v4l2_fwnode_endpoint_free(&vep);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400504 if (ret < 0)
505 goto out_err;
506
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400507 ret = v4l2_async_notifier_add_subdev(notifier, asd);
508 if (ret < 0) {
509 /* not an error if asd already exists */
510 if (ret == -EEXIST)
511 ret = 0;
512 goto out_err;
513 }
Sakari Ailus9ca46532017-08-17 11:28:21 -0400514
515 return 0;
516
517out_err:
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400518 fwnode_handle_put(asd->match.fwnode);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400519 kfree(asd);
520
521 return ret == -ENOTCONN ? 0 : ret;
522}
523
524static int __v4l2_async_notifier_parse_fwnode_endpoints(
525 struct device *dev, struct v4l2_async_notifier *notifier,
526 size_t asd_struct_size, unsigned int port, bool has_port,
527 int (*parse_endpoint)(struct device *dev,
528 struct v4l2_fwnode_endpoint *vep,
529 struct v4l2_async_subdev *asd))
530{
531 struct fwnode_handle *fwnode;
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400532 int ret = 0;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400533
534 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
535 return -EINVAL;
536
Sakari Ailus106ee382017-12-21 07:11:19 -0500537 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
Sakari Ailus9ca46532017-08-17 11:28:21 -0400538 struct fwnode_handle *dev_fwnode;
539 bool is_available;
540
541 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
542 is_available = fwnode_device_is_available(dev_fwnode);
543 fwnode_handle_put(dev_fwnode);
544 if (!is_available)
545 continue;
546
547 if (has_port) {
548 struct fwnode_endpoint ep;
549
550 ret = fwnode_graph_parse_endpoint(fwnode, &ep);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400551 if (ret)
552 break;
553
554 if (ep.port != port)
555 continue;
556 }
557
558 ret = v4l2_async_notifier_fwnode_parse_endpoint(
559 dev, notifier, fwnode, asd_struct_size, parse_endpoint);
560 if (ret < 0)
561 break;
562 }
563
564 fwnode_handle_put(fwnode);
565
566 return ret;
567}
568
569int v4l2_async_notifier_parse_fwnode_endpoints(
570 struct device *dev, struct v4l2_async_notifier *notifier,
571 size_t asd_struct_size,
572 int (*parse_endpoint)(struct device *dev,
573 struct v4l2_fwnode_endpoint *vep,
574 struct v4l2_async_subdev *asd))
575{
576 return __v4l2_async_notifier_parse_fwnode_endpoints(
577 dev, notifier, asd_struct_size, 0, false, parse_endpoint);
578}
579EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
580
581int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
582 struct device *dev, struct v4l2_async_notifier *notifier,
583 size_t asd_struct_size, unsigned int port,
584 int (*parse_endpoint)(struct device *dev,
585 struct v4l2_fwnode_endpoint *vep,
586 struct v4l2_async_subdev *asd))
587{
588 return __v4l2_async_notifier_parse_fwnode_endpoints(
589 dev, notifier, asd_struct_size, port, true, parse_endpoint);
590}
591EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
592
Sakari Ailusd8428532017-06-20 07:47:24 -0400593/*
594 * v4l2_fwnode_reference_parse - parse references for async sub-devices
595 * @dev: the device node the properties of which are parsed for references
596 * @notifier: the async notifier where the async subdevs will be added
597 * @prop: the name of the property
598 *
599 * Return: 0 on success
600 * -ENOENT if no entries were found
601 * -ENOMEM if memory allocation failed
602 * -EINVAL if property parsing failed
603 */
604static int v4l2_fwnode_reference_parse(
605 struct device *dev, struct v4l2_async_notifier *notifier,
606 const char *prop)
607{
608 struct fwnode_reference_args args;
609 unsigned int index;
610 int ret;
611
612 for (index = 0;
613 !(ret = fwnode_property_get_reference_args(
614 dev_fwnode(dev), prop, NULL, 0, index, &args));
615 index++)
616 fwnode_handle_put(args.fwnode);
617
618 if (!index)
619 return -ENOENT;
620
621 /*
622 * Note that right now both -ENODATA and -ENOENT may signal
623 * out-of-bounds access. Return the error in cases other than that.
624 */
625 if (ret != -ENOENT && ret != -ENODATA)
626 return ret;
627
Sakari Ailusd8428532017-06-20 07:47:24 -0400628 for (index = 0; !fwnode_property_get_reference_args(
629 dev_fwnode(dev), prop, NULL, 0, index, &args);
630 index++) {
631 struct v4l2_async_subdev *asd;
632
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400633 asd = v4l2_async_notifier_add_fwnode_subdev(
634 notifier, args.fwnode, sizeof(*asd));
635 if (IS_ERR(asd)) {
636 ret = PTR_ERR(asd);
637 /* not an error if asd already exists */
638 if (ret == -EEXIST) {
639 fwnode_handle_put(args.fwnode);
640 continue;
641 }
642
Sakari Ailusd8428532017-06-20 07:47:24 -0400643 goto error;
644 }
Sakari Ailusd8428532017-06-20 07:47:24 -0400645 }
646
647 return 0;
648
649error:
650 fwnode_handle_put(args.fwnode);
651 return ret;
652}
653
Sakari Ailusa1699a42017-06-22 08:03:28 -0400654/*
655 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
656 * arguments
657 * @fwnode: fwnode to read @prop from
658 * @notifier: notifier for @dev
659 * @prop: the name of the property
660 * @index: the index of the reference to get
661 * @props: the array of integer property names
662 * @nprops: the number of integer property names in @nprops
663 *
664 * First find an fwnode referred to by the reference at @index in @prop.
665 *
666 * Then under that fwnode, @nprops times, for each property in @props,
667 * iteratively follow child nodes starting from fwnode such that they have the
668 * property in @props array at the index of the child node distance from the
669 * root node and the value of that property matching with the integer argument
670 * of the reference, at the same index.
671 *
672 * The child fwnode reched at the end of the iteration is then returned to the
673 * caller.
674 *
675 * The core reason for this is that you cannot refer to just any node in ACPI.
676 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
677 * provide a list of (property name, property value) tuples where each tuple
678 * uniquely identifies a child node. The first tuple identifies a child directly
679 * underneath the device fwnode, the next tuple identifies a child node
680 * underneath the fwnode identified by the previous tuple, etc. until you
681 * reached the fwnode you need.
682 *
683 * An example with a graph, as defined in Documentation/acpi/dsd/graph.txt:
684 *
685 * Scope (\_SB.PCI0.I2C2)
686 * {
687 * Device (CAM0)
688 * {
689 * Name (_DSD, Package () {
690 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
691 * Package () {
692 * Package () {
693 * "compatible",
694 * Package () { "nokia,smia" }
695 * },
696 * },
697 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
698 * Package () {
699 * Package () { "port0", "PRT0" },
700 * }
701 * })
702 * Name (PRT0, Package() {
703 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
704 * Package () {
705 * Package () { "port", 0 },
706 * },
707 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
708 * Package () {
709 * Package () { "endpoint0", "EP00" },
710 * }
711 * })
712 * Name (EP00, Package() {
713 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
714 * Package () {
715 * Package () { "endpoint", 0 },
716 * Package () {
717 * "remote-endpoint",
718 * Package() {
719 * \_SB.PCI0.ISP, 4, 0
720 * }
721 * },
722 * }
723 * })
724 * }
725 * }
726 *
727 * Scope (\_SB.PCI0)
728 * {
729 * Device (ISP)
730 * {
731 * Name (_DSD, Package () {
732 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
733 * Package () {
734 * Package () { "port4", "PRT4" },
735 * }
736 * })
737 *
738 * Name (PRT4, Package() {
739 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
740 * Package () {
741 * Package () { "port", 4 },
742 * },
743 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
744 * Package () {
745 * Package () { "endpoint0", "EP40" },
746 * }
747 * })
748 *
749 * Name (EP40, Package() {
750 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
751 * Package () {
752 * Package () { "endpoint", 0 },
753 * Package () {
754 * "remote-endpoint",
755 * Package () {
756 * \_SB.PCI0.I2C2.CAM0,
757 * 0, 0
758 * }
759 * },
760 * }
761 * })
762 * }
763 * }
764 *
765 * From the EP40 node under ISP device, you could parse the graph remote
766 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
767 *
768 * @fwnode: fwnode referring to EP40 under ISP.
769 * @prop: "remote-endpoint"
770 * @index: 0
771 * @props: "port", "endpoint"
772 * @nprops: 2
773 *
774 * And you'd get back fwnode referring to EP00 under CAM0.
775 *
776 * The same works the other way around: if you use EP00 under CAM0 as the
777 * fwnode, you'll get fwnode referring to EP40 under ISP.
778 *
779 * The same example in DT syntax would look like this:
780 *
781 * cam: cam0 {
782 * compatible = "nokia,smia";
783 *
784 * port {
785 * port = <0>;
786 * endpoint {
787 * endpoint = <0>;
788 * remote-endpoint = <&isp 4 0>;
789 * };
790 * };
791 * };
792 *
793 * isp: isp {
794 * ports {
795 * port@4 {
796 * port = <4>;
797 * endpoint {
798 * endpoint = <0>;
799 * remote-endpoint = <&cam 0 0>;
800 * };
801 * };
802 * };
803 * };
804 *
805 * Return: 0 on success
806 * -ENOENT if no entries (or the property itself) were found
807 * -EINVAL if property parsing otherwise failed
808 * -ENOMEM if memory allocation failed
809 */
810static struct fwnode_handle *v4l2_fwnode_reference_get_int_prop(
811 struct fwnode_handle *fwnode, const char *prop, unsigned int index,
812 const char * const *props, unsigned int nprops)
813{
814 struct fwnode_reference_args fwnode_args;
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300815 u64 *args = fwnode_args.args;
Sakari Ailusa1699a42017-06-22 08:03:28 -0400816 struct fwnode_handle *child;
817 int ret;
818
819 /*
820 * Obtain remote fwnode as well as the integer arguments.
821 *
822 * Note that right now both -ENODATA and -ENOENT may signal
823 * out-of-bounds access. Return -ENOENT in that case.
824 */
825 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
826 index, &fwnode_args);
827 if (ret)
828 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
829
830 /*
831 * Find a node in the tree under the referred fwnode corresponding to
832 * the integer arguments.
833 */
834 fwnode = fwnode_args.fwnode;
835 while (nprops--) {
836 u32 val;
837
838 /* Loop over all child nodes under fwnode. */
839 fwnode_for_each_child_node(fwnode, child) {
840 if (fwnode_property_read_u32(child, *props, &val))
841 continue;
842
843 /* Found property, see if its value matches. */
844 if (val == *args)
845 break;
846 }
847
848 fwnode_handle_put(fwnode);
849
850 /* No property found; return an error here. */
851 if (!child) {
852 fwnode = ERR_PTR(-ENOENT);
853 break;
854 }
855
856 props++;
857 args++;
858 fwnode = child;
859 }
860
861 return fwnode;
862}
863
864/*
865 * v4l2_fwnode_reference_parse_int_props - parse references for async
866 * sub-devices
867 * @dev: struct device pointer
868 * @notifier: notifier for @dev
869 * @prop: the name of the property
870 * @props: the array of integer property names
871 * @nprops: the number of integer properties
872 *
873 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
874 * property @prop with integer arguments with child nodes matching in properties
875 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
876 * accordingly.
877 *
878 * While it is technically possible to use this function on DT, it is only
879 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
880 * on ACPI the references are limited to devices.
881 *
882 * Return: 0 on success
883 * -ENOENT if no entries (or the property itself) were found
884 * -EINVAL if property parsing otherwisefailed
885 * -ENOMEM if memory allocation failed
886 */
887static int v4l2_fwnode_reference_parse_int_props(
888 struct device *dev, struct v4l2_async_notifier *notifier,
889 const char *prop, const char * const *props, unsigned int nprops)
890{
891 struct fwnode_handle *fwnode;
892 unsigned int index;
893 int ret;
894
Mauro Carvalho Chehab9879c9d2018-04-04 07:21:04 -0400895 index = 0;
896 do {
897 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
898 prop, index,
899 props, nprops);
900 if (IS_ERR(fwnode)) {
901 /*
902 * Note that right now both -ENODATA and -ENOENT may
903 * signal out-of-bounds access. Return the error in
904 * cases other than that.
905 */
906 if (PTR_ERR(fwnode) != -ENOENT &&
907 PTR_ERR(fwnode) != -ENODATA)
908 return PTR_ERR(fwnode);
909 break;
910 }
Sakari Ailusa1699a42017-06-22 08:03:28 -0400911 fwnode_handle_put(fwnode);
Mauro Carvalho Chehab9879c9d2018-04-04 07:21:04 -0400912 index++;
913 } while (1);
Sakari Ailusa1699a42017-06-22 08:03:28 -0400914
Sakari Ailusa1699a42017-06-22 08:03:28 -0400915 for (index = 0; !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(
916 dev_fwnode(dev), prop, index, props,
917 nprops))); index++) {
918 struct v4l2_async_subdev *asd;
919
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400920 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
921 sizeof(*asd));
922 if (IS_ERR(asd)) {
923 ret = PTR_ERR(asd);
924 /* not an error if asd already exists */
925 if (ret == -EEXIST) {
926 fwnode_handle_put(fwnode);
927 continue;
928 }
929
Sakari Ailusa1699a42017-06-22 08:03:28 -0400930 goto error;
931 }
Sakari Ailusa1699a42017-06-22 08:03:28 -0400932 }
933
934 return PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
935
936error:
937 fwnode_handle_put(fwnode);
938 return ret;
939}
940
Sakari Ailus7a9ec802017-09-06 08:35:42 -0400941int v4l2_async_notifier_parse_fwnode_sensor_common(
942 struct device *dev, struct v4l2_async_notifier *notifier)
943{
944 static const char * const led_props[] = { "led" };
945 static const struct {
946 const char *name;
947 const char * const *props;
948 unsigned int nprops;
949 } props[] = {
950 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
951 { "lens-focus", NULL, 0 },
952 };
953 unsigned int i;
954
955 for (i = 0; i < ARRAY_SIZE(props); i++) {
956 int ret;
957
958 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
959 ret = v4l2_fwnode_reference_parse_int_props(
960 dev, notifier, props[i].name,
961 props[i].props, props[i].nprops);
962 else
963 ret = v4l2_fwnode_reference_parse(
964 dev, notifier, props[i].name);
965 if (ret && ret != -ENOENT) {
966 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
967 props[i].name, ret);
968 return ret;
969 }
970 }
971
972 return 0;
973}
974EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
975
Sakari Ailusaef69d52017-09-24 18:47:44 -0400976int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
977{
978 struct v4l2_async_notifier *notifier;
979 int ret;
980
981 if (WARN_ON(!sd->dev))
982 return -ENODEV;
983
984 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
985 if (!notifier)
986 return -ENOMEM;
987
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400988 v4l2_async_notifier_init(notifier);
989
Sakari Ailusaef69d52017-09-24 18:47:44 -0400990 ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
991 notifier);
992 if (ret < 0)
993 goto out_cleanup;
994
995 ret = v4l2_async_subdev_notifier_register(sd, notifier);
996 if (ret < 0)
997 goto out_cleanup;
998
999 ret = v4l2_async_register_subdev(sd);
1000 if (ret < 0)
1001 goto out_unregister;
1002
1003 sd->subdev_notifier = notifier;
1004
1005 return 0;
1006
1007out_unregister:
1008 v4l2_async_notifier_unregister(notifier);
1009
1010out_cleanup:
1011 v4l2_async_notifier_cleanup(notifier);
1012 kfree(notifier);
1013
1014 return ret;
1015}
1016EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
1017
Steve Longerbeam1634f0e2018-09-29 15:54:09 -04001018int v4l2_async_register_fwnode_subdev(
1019 struct v4l2_subdev *sd, size_t asd_struct_size,
1020 unsigned int *ports, unsigned int num_ports,
1021 int (*parse_endpoint)(struct device *dev,
1022 struct v4l2_fwnode_endpoint *vep,
1023 struct v4l2_async_subdev *asd))
1024{
1025 struct v4l2_async_notifier *notifier;
1026 struct device *dev = sd->dev;
1027 struct fwnode_handle *fwnode;
1028 int ret;
1029
1030 if (WARN_ON(!dev))
1031 return -ENODEV;
1032
1033 fwnode = dev_fwnode(dev);
1034 if (!fwnode_device_is_available(fwnode))
1035 return -ENODEV;
1036
1037 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1038 if (!notifier)
1039 return -ENOMEM;
1040
1041 v4l2_async_notifier_init(notifier);
1042
1043 if (!ports) {
1044 ret = v4l2_async_notifier_parse_fwnode_endpoints(
1045 dev, notifier, asd_struct_size, parse_endpoint);
1046 if (ret < 0)
1047 goto out_cleanup;
1048 } else {
1049 unsigned int i;
1050
1051 for (i = 0; i < num_ports; i++) {
1052 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
1053 dev, notifier, asd_struct_size,
1054 ports[i], parse_endpoint);
1055 if (ret < 0)
1056 goto out_cleanup;
1057 }
1058 }
1059
1060 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1061 if (ret < 0)
1062 goto out_cleanup;
1063
1064 ret = v4l2_async_register_subdev(sd);
1065 if (ret < 0)
1066 goto out_unregister;
1067
1068 sd->subdev_notifier = notifier;
1069
1070 return 0;
1071
1072out_unregister:
1073 v4l2_async_notifier_unregister(notifier);
1074out_cleanup:
1075 v4l2_async_notifier_cleanup(notifier);
1076 kfree(notifier);
1077
1078 return ret;
1079}
1080EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
1081
Sakari Ailusca50c192016-08-12 08:05:51 -03001082MODULE_LICENSE("GPL");
1083MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1084MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1085MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");