blob: d6d78230cdf831747f8bc00a1e18f19a647e308d [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;
50 bool have_clk_lane = false;
51 unsigned int flags = 0, lanes_used = 0;
Sakari Ailus276565e2018-07-18 09:33:45 -040052 u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
53 unsigned int num_data_lanes = 0;
Sakari Ailusca50c192016-08-12 08:05:51 -030054 unsigned int i;
55 u32 v;
56 int rval;
57
Sakari Ailus276565e2018-07-18 09:33:45 -040058 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY)
59 num_data_lanes = min_t(u32, bus->num_data_lanes,
60 V4L2_FWNODE_CSI2_MAX_DATA_LANES);
61
Sakari Ailusca50c192016-08-12 08:05:51 -030062 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
63 if (rval > 0) {
Sakari Ailus276565e2018-07-18 09:33:45 -040064 num_data_lanes =
Sakari Ailusad3cdf32017-08-14 06:43:07 -040065 min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
Sakari Ailusca50c192016-08-12 08:05:51 -030066
67 fwnode_property_read_u32_array(fwnode, "data-lanes", array,
Sakari Ailus276565e2018-07-18 09:33:45 -040068 num_data_lanes);
Sakari Ailusca50c192016-08-12 08:05:51 -030069
Sakari Ailus276565e2018-07-18 09:33:45 -040070 for (i = 0; i < num_data_lanes; i++) {
Sakari Ailusca50c192016-08-12 08:05:51 -030071 if (lanes_used & BIT(array[i]))
72 pr_warn("duplicated lane %u in data-lanes\n",
73 array[i]);
74 lanes_used |= BIT(array[i]);
75
76 bus->data_lanes[i] = array[i];
Sakari Ailusc8677aa2017-12-04 16:25:06 -050077 pr_debug("lane %u position %u\n", i, array[i]);
Sakari Ailusca50c192016-08-12 08:05:51 -030078 }
Sakari Ailus276565e2018-07-18 09:33:45 -040079 }
Sakari Ailusca50c192016-08-12 08:05:51 -030080
Sakari Ailus276565e2018-07-18 09:33:45 -040081 rval = fwnode_property_read_u32_array(fwnode, "lane-polarities", NULL,
82 0);
83 if (rval > 0) {
84 if (rval != 1 + num_data_lanes /* clock+data */) {
85 pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
86 1 + num_data_lanes, rval);
87 return -EINVAL;
Sakari Ailusca50c192016-08-12 08:05:51 -030088 }
Sakari Ailusb24f0212017-08-14 06:15:21 -040089
Sakari Ailus276565e2018-07-18 09:33:45 -040090 fwnode_property_read_u32_array(fwnode, "lane-polarities", array,
91 1 + num_data_lanes);
92
93 for (i = 0; i < 1 + num_data_lanes; i++) {
94 bus->lane_polarities[i] = array[i];
95 pr_debug("lane %u polarity %sinverted",
96 i, array[i] ? "" : "not ");
97 }
98 } else {
99 pr_debug("no lane polarities defined, assuming not inverted\n");
Sakari Ailusca50c192016-08-12 08:05:51 -0300100 }
101
102 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
103 if (lanes_used & BIT(v))
104 pr_warn("duplicated lane %u in clock-lanes\n", v);
105 lanes_used |= BIT(v);
106
107 bus->clock_lane = v;
108 have_clk_lane = true;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500109 pr_debug("clock lane position %u\n", v);
Sakari Ailusca50c192016-08-12 08:05:51 -0300110 }
111
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500112 if (fwnode_property_present(fwnode, "clock-noncontinuous")) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300113 flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500114 pr_debug("non-continuous clock\n");
Sakari Ailusd4865322018-07-20 05:04:29 -0400115 } else {
Sakari Ailusca50c192016-08-12 08:05:51 -0300116 flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500117 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300118
Sakari Ailus276565e2018-07-18 09:33:45 -0400119 if (bus_type == V4L2_FWNODE_BUS_TYPE_CSI2_DPHY || lanes_used ||
120 have_clk_lane || (flags & ~V4L2_MBUS_CSI2_CONTINUOUS_CLOCK)) {
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400121 bus->flags = flags;
122 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
Sakari Ailus276565e2018-07-18 09:33:45 -0400123 bus->num_data_lanes = num_data_lanes;
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400124 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300125
126 return 0;
127}
128
Sakari Ailus175b18b2018-01-02 08:14:30 -0500129#define PARALLEL_MBUS_FLAGS (V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
130 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
131 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
132 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
133 V4L2_MBUS_FIELD_EVEN_HIGH | \
134 V4L2_MBUS_FIELD_EVEN_LOW)
135
Sakari Ailusca50c192016-08-12 08:05:51 -0300136static void v4l2_fwnode_endpoint_parse_parallel_bus(
Sakari Ailus175b18b2018-01-02 08:14:30 -0500137 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep,
138 enum v4l2_fwnode_bus_type bus_type)
Sakari Ailusca50c192016-08-12 08:05:51 -0300139{
140 struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
141 unsigned int flags = 0;
142 u32 v;
143
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500144 if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300145 flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
146 V4L2_MBUS_HSYNC_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500147 pr_debug("hsync-active %s\n", v ? "high" : "low");
148 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300149
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500150 if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300151 flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
152 V4L2_MBUS_VSYNC_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500153 pr_debug("vsync-active %s\n", v ? "high" : "low");
154 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300155
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500156 if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300157 flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
158 V4L2_MBUS_FIELD_EVEN_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500159 pr_debug("field-even-active %s\n", v ? "high" : "low");
160 }
161
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500162 if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300163 flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
164 V4L2_MBUS_PCLK_SAMPLE_FALLING;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500165 pr_debug("pclk-sample %s\n", v ? "high" : "low");
166 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300167
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500168 if (!fwnode_property_read_u32(fwnode, "data-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300169 flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
170 V4L2_MBUS_DATA_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500171 pr_debug("data-active %s\n", v ? "high" : "low");
172 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300173
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500174 if (fwnode_property_present(fwnode, "slave-mode")) {
175 pr_debug("slave mode\n");
Sakari Ailusca50c192016-08-12 08:05:51 -0300176 flags |= V4L2_MBUS_SLAVE;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500177 } else {
Sakari Ailusca50c192016-08-12 08:05:51 -0300178 flags |= V4L2_MBUS_MASTER;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500179 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300180
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500181 if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300182 bus->bus_width = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500183 pr_debug("bus-width %u\n", v);
184 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300185
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500186 if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300187 bus->data_shift = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500188 pr_debug("data-shift %u\n", v);
189 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300190
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500191 if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) {
Sakari Ailusca50c192016-08-12 08:05:51 -0300192 flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
193 V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500194 pr_debug("sync-on-green-active %s\n", v ? "high" : "low");
195 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300196
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500197 if (!fwnode_property_read_u32(fwnode, "data-enable-active", &v)) {
Jacopo Mondi9b04fcc2018-07-09 10:19:19 -0400198 flags |= v ? V4L2_MBUS_DATA_ENABLE_HIGH :
199 V4L2_MBUS_DATA_ENABLE_LOW;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500200 pr_debug("data-enable-active %s\n", v ? "high" : "low");
201 }
Jacopo Mondi9b04fcc2018-07-09 10:19:19 -0400202
Sakari Ailus175b18b2018-01-02 08:14:30 -0500203 switch (bus_type) {
204 default:
205 bus->flags = flags;
206 if (flags & PARALLEL_MBUS_FLAGS)
207 vep->bus_type = V4L2_MBUS_PARALLEL;
208 else
209 vep->bus_type = V4L2_MBUS_BT656;
210 break;
211 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400212 vep->bus_type = V4L2_MBUS_PARALLEL;
Sakari Ailus175b18b2018-01-02 08:14:30 -0500213 bus->flags = flags;
214 break;
215 case V4L2_FWNODE_BUS_TYPE_BT656:
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400216 vep->bus_type = V4L2_MBUS_BT656;
Sakari Ailus175b18b2018-01-02 08:14:30 -0500217 bus->flags = flags & ~PARALLEL_MBUS_FLAGS;
218 break;
219 }
Sakari Ailusca50c192016-08-12 08:05:51 -0300220}
221
Mauro Carvalho Chehababc5b2c2017-07-20 16:27:27 -0400222static void
223v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
224 struct v4l2_fwnode_endpoint *vep,
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400225 enum v4l2_fwnode_bus_type bus_type)
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500226{
227 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
228 u32 v;
229
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500230 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500231 bus->clock_inv = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500232 pr_debug("clock-inv %u\n", v);
233 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500234
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500235 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500236 bus->strobe = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500237 pr_debug("strobe %u\n", v);
238 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500239
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500240 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500241 bus->data_lane = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500242 pr_debug("data-lanes %u\n", v);
243 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500244
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500245 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500246 bus->clock_lane = v;
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500247 pr_debug("clock-lanes %u\n", v);
248 }
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500249
250 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
251 vep->bus_type = V4L2_MBUS_CCP2;
252 else
253 vep->bus_type = V4L2_MBUS_CSI1;
254}
255
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500256static int __v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
257 struct v4l2_fwnode_endpoint *vep)
Sakari Ailusca50c192016-08-12 08:05:51 -0300258{
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500259 u32 bus_type = 0;
Sakari Ailusca50c192016-08-12 08:05:51 -0300260 int rval;
261
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500262 pr_debug("===== begin V4L2 endpoint properties\n");
263
Sakari Ailusca50c192016-08-12 08:05:51 -0300264 fwnode_graph_parse_endpoint(fwnode, &vep->base);
265
266 /* Zero fields from bus_type to until the end */
267 memset(&vep->bus_type, 0, sizeof(*vep) -
268 offsetof(typeof(*vep), bus_type));
269
Sakari Ailuse07a41f2015-02-25 14:51:01 -0500270 fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
271
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500272 switch (bus_type) {
273 case V4L2_FWNODE_BUS_TYPE_GUESS:
Sakari Ailus276565e2018-07-18 09:33:45 -0400274 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
275 bus_type);
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500276 if (rval)
277 return rval;
Sakari Ailus2835b5b2018-07-31 06:43:14 -0400278
279 if (vep->bus_type == V4L2_MBUS_UNKNOWN)
Sakari Ailus175b18b2018-01-02 08:14:30 -0500280 v4l2_fwnode_endpoint_parse_parallel_bus(
281 fwnode, vep, V4L2_MBUS_UNKNOWN);
Sakari Ailusca50c192016-08-12 08:05:51 -0300282
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500283 break;
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500284 case V4L2_FWNODE_BUS_TYPE_CCP2:
285 case V4L2_FWNODE_BUS_TYPE_CSI1:
286 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
287
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500288 break;
Sakari Ailus175b18b2018-01-02 08:14:30 -0500289 case V4L2_FWNODE_BUS_TYPE_CSI2_DPHY:
290 vep->bus_type = V4L2_MBUS_CSI2_DPHY;
Sakari Ailus276565e2018-07-18 09:33:45 -0400291 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep,
292 bus_type);
Sakari Ailus175b18b2018-01-02 08:14:30 -0500293 if (rval)
294 return rval;
295
296 break;
297 case V4L2_FWNODE_BUS_TYPE_PARALLEL:
298 case V4L2_FWNODE_BUS_TYPE_BT656:
299 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep, bus_type);
300
301 break;
Sakari Ailus97bbdf02015-02-25 14:39:11 -0500302 default:
303 pr_warn("unsupported bus type %u\n", bus_type);
304 return -EINVAL;
305 }
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500306
307 return 0;
308}
309
310int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
311 struct v4l2_fwnode_endpoint *vep)
312{
313 int ret;
314
315 ret = __v4l2_fwnode_endpoint_parse(fwnode, vep);
316
317 pr_debug("===== end V4L2 endpoint properties\n");
318
319 return ret;
Sakari Ailusca50c192016-08-12 08:05:51 -0300320}
321EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
322
Sakari Ailusca50c192016-08-12 08:05:51 -0300323void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
324{
325 if (IS_ERR_OR_NULL(vep))
326 return;
327
328 kfree(vep->link_frequencies);
Sakari Ailusca50c192016-08-12 08:05:51 -0300329}
330EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
331
Sakari Ailus6970d372018-06-02 12:19:35 -0400332int v4l2_fwnode_endpoint_alloc_parse(
333 struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
Sakari Ailusca50c192016-08-12 08:05:51 -0300334{
Sakari Ailusca50c192016-08-12 08:05:51 -0300335 int rval;
336
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500337 rval = __v4l2_fwnode_endpoint_parse(fwnode, vep);
Sakari Ailusca50c192016-08-12 08:05:51 -0300338 if (rval < 0)
Sakari Ailus6970d372018-06-02 12:19:35 -0400339 return rval;
Sakari Ailusca50c192016-08-12 08:05:51 -0300340
341 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
342 NULL, 0);
Sakari Ailus06f81522017-06-20 09:14:43 -0400343 if (rval > 0) {
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500344 unsigned int i;
345
Sakari Ailus06f81522017-06-20 09:14:43 -0400346 vep->link_frequencies =
347 kmalloc_array(rval, sizeof(*vep->link_frequencies),
348 GFP_KERNEL);
Sakari Ailus6970d372018-06-02 12:19:35 -0400349 if (!vep->link_frequencies)
350 return -ENOMEM;
Sakari Ailusca50c192016-08-12 08:05:51 -0300351
Sakari Ailus06f81522017-06-20 09:14:43 -0400352 vep->nr_of_link_frequencies = rval;
353
354 rval = fwnode_property_read_u64_array(
355 fwnode, "link-frequencies", vep->link_frequencies,
356 vep->nr_of_link_frequencies);
Sakari Ailus6970d372018-06-02 12:19:35 -0400357 if (rval < 0) {
358 v4l2_fwnode_endpoint_free(vep);
359 return rval;
360 }
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500361
362 for (i = 0; i < vep->nr_of_link_frequencies; i++)
363 pr_info("link-frequencies %u value %llu\n", i,
364 vep->link_frequencies[i]);
Sakari Ailusca50c192016-08-12 08:05:51 -0300365 }
366
Sakari Ailusc8677aa2017-12-04 16:25:06 -0500367 pr_debug("===== end V4L2 endpoint properties\n");
368
Sakari Ailus6970d372018-06-02 12:19:35 -0400369 return 0;
Sakari Ailusca50c192016-08-12 08:05:51 -0300370}
371EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
372
Sakari Ailusca50c192016-08-12 08:05:51 -0300373int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
374 struct v4l2_fwnode_link *link)
375{
376 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
377 struct fwnode_handle *fwnode;
378
379 memset(link, 0, sizeof(*link));
380
381 fwnode = fwnode_get_parent(__fwnode);
382 fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
383 fwnode = fwnode_get_next_parent(fwnode);
384 if (is_of_node(fwnode) &&
385 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
386 fwnode = fwnode_get_next_parent(fwnode);
387 link->local_node = fwnode;
388
389 fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
390 if (!fwnode) {
391 fwnode_handle_put(fwnode);
392 return -ENOLINK;
393 }
394
395 fwnode = fwnode_get_parent(fwnode);
396 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
397 fwnode = fwnode_get_next_parent(fwnode);
398 if (is_of_node(fwnode) &&
399 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
400 fwnode = fwnode_get_next_parent(fwnode);
401 link->remote_node = fwnode;
402
403 return 0;
404}
405EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
406
Sakari Ailusca50c192016-08-12 08:05:51 -0300407void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
408{
409 fwnode_handle_put(link->local_node);
410 fwnode_handle_put(link->remote_node);
411}
412EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
413
Sakari Ailus9ca46532017-08-17 11:28:21 -0400414static int v4l2_async_notifier_fwnode_parse_endpoint(
415 struct device *dev, struct v4l2_async_notifier *notifier,
416 struct fwnode_handle *endpoint, unsigned int asd_struct_size,
417 int (*parse_endpoint)(struct device *dev,
418 struct v4l2_fwnode_endpoint *vep,
419 struct v4l2_async_subdev *asd))
420{
Sakari Ailus6970d372018-06-02 12:19:35 -0400421 struct v4l2_fwnode_endpoint vep = { .bus_type = 0 };
Sakari Ailus9ca46532017-08-17 11:28:21 -0400422 struct v4l2_async_subdev *asd;
Sakari Ailus6970d372018-06-02 12:19:35 -0400423 int ret;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400424
425 asd = kzalloc(asd_struct_size, GFP_KERNEL);
426 if (!asd)
427 return -ENOMEM;
428
429 asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400430 asd->match.fwnode =
Sakari Ailus9ca46532017-08-17 11:28:21 -0400431 fwnode_graph_get_remote_port_parent(endpoint);
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400432 if (!asd->match.fwnode) {
Sakari Ailus9ca46532017-08-17 11:28:21 -0400433 dev_warn(dev, "bad remote port parent\n");
Steve Longerbeam4382f372018-09-29 15:54:04 -0400434 ret = -ENOTCONN;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400435 goto out_err;
436 }
437
Sakari Ailus6970d372018-06-02 12:19:35 -0400438 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &vep);
439 if (ret) {
Sakari Ailus9ca46532017-08-17 11:28:21 -0400440 dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
441 ret);
442 goto out_err;
443 }
444
Sakari Ailus6970d372018-06-02 12:19:35 -0400445 ret = parse_endpoint ? parse_endpoint(dev, &vep, asd) : 0;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400446 if (ret == -ENOTCONN)
Sakari Ailus6970d372018-06-02 12:19:35 -0400447 dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep.base.port,
448 vep.base.id);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400449 else if (ret < 0)
450 dev_warn(dev,
451 "driver could not parse port@%u/endpoint@%u (%d)\n",
Sakari Ailus6970d372018-06-02 12:19:35 -0400452 vep.base.port, vep.base.id, ret);
453 v4l2_fwnode_endpoint_free(&vep);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400454 if (ret < 0)
455 goto out_err;
456
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400457 ret = v4l2_async_notifier_add_subdev(notifier, asd);
458 if (ret < 0) {
459 /* not an error if asd already exists */
460 if (ret == -EEXIST)
461 ret = 0;
462 goto out_err;
463 }
Sakari Ailus9ca46532017-08-17 11:28:21 -0400464
465 return 0;
466
467out_err:
Mauro Carvalho Chehab4e48afe2017-09-27 10:12:00 -0400468 fwnode_handle_put(asd->match.fwnode);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400469 kfree(asd);
470
471 return ret == -ENOTCONN ? 0 : ret;
472}
473
474static int __v4l2_async_notifier_parse_fwnode_endpoints(
475 struct device *dev, struct v4l2_async_notifier *notifier,
476 size_t asd_struct_size, unsigned int port, bool has_port,
477 int (*parse_endpoint)(struct device *dev,
478 struct v4l2_fwnode_endpoint *vep,
479 struct v4l2_async_subdev *asd))
480{
481 struct fwnode_handle *fwnode;
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400482 int ret = 0;
Sakari Ailus9ca46532017-08-17 11:28:21 -0400483
484 if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
485 return -EINVAL;
486
Sakari Ailus106ee382017-12-21 07:11:19 -0500487 fwnode_graph_for_each_endpoint(dev_fwnode(dev), fwnode) {
Sakari Ailus9ca46532017-08-17 11:28:21 -0400488 struct fwnode_handle *dev_fwnode;
489 bool is_available;
490
491 dev_fwnode = fwnode_graph_get_port_parent(fwnode);
492 is_available = fwnode_device_is_available(dev_fwnode);
493 fwnode_handle_put(dev_fwnode);
494 if (!is_available)
495 continue;
496
497 if (has_port) {
498 struct fwnode_endpoint ep;
499
500 ret = fwnode_graph_parse_endpoint(fwnode, &ep);
Sakari Ailus9ca46532017-08-17 11:28:21 -0400501 if (ret)
502 break;
503
504 if (ep.port != port)
505 continue;
506 }
507
508 ret = v4l2_async_notifier_fwnode_parse_endpoint(
509 dev, notifier, fwnode, asd_struct_size, parse_endpoint);
510 if (ret < 0)
511 break;
512 }
513
514 fwnode_handle_put(fwnode);
515
516 return ret;
517}
518
519int v4l2_async_notifier_parse_fwnode_endpoints(
520 struct device *dev, struct v4l2_async_notifier *notifier,
521 size_t asd_struct_size,
522 int (*parse_endpoint)(struct device *dev,
523 struct v4l2_fwnode_endpoint *vep,
524 struct v4l2_async_subdev *asd))
525{
526 return __v4l2_async_notifier_parse_fwnode_endpoints(
527 dev, notifier, asd_struct_size, 0, false, parse_endpoint);
528}
529EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
530
531int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
532 struct device *dev, struct v4l2_async_notifier *notifier,
533 size_t asd_struct_size, unsigned int port,
534 int (*parse_endpoint)(struct device *dev,
535 struct v4l2_fwnode_endpoint *vep,
536 struct v4l2_async_subdev *asd))
537{
538 return __v4l2_async_notifier_parse_fwnode_endpoints(
539 dev, notifier, asd_struct_size, port, true, parse_endpoint);
540}
541EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
542
Sakari Ailusd8428532017-06-20 07:47:24 -0400543/*
544 * v4l2_fwnode_reference_parse - parse references for async sub-devices
545 * @dev: the device node the properties of which are parsed for references
546 * @notifier: the async notifier where the async subdevs will be added
547 * @prop: the name of the property
548 *
549 * Return: 0 on success
550 * -ENOENT if no entries were found
551 * -ENOMEM if memory allocation failed
552 * -EINVAL if property parsing failed
553 */
554static int v4l2_fwnode_reference_parse(
555 struct device *dev, struct v4l2_async_notifier *notifier,
556 const char *prop)
557{
558 struct fwnode_reference_args args;
559 unsigned int index;
560 int ret;
561
562 for (index = 0;
563 !(ret = fwnode_property_get_reference_args(
564 dev_fwnode(dev), prop, NULL, 0, index, &args));
565 index++)
566 fwnode_handle_put(args.fwnode);
567
568 if (!index)
569 return -ENOENT;
570
571 /*
572 * Note that right now both -ENODATA and -ENOENT may signal
573 * out-of-bounds access. Return the error in cases other than that.
574 */
575 if (ret != -ENOENT && ret != -ENODATA)
576 return ret;
577
Sakari Ailusd8428532017-06-20 07:47:24 -0400578 for (index = 0; !fwnode_property_get_reference_args(
579 dev_fwnode(dev), prop, NULL, 0, index, &args);
580 index++) {
581 struct v4l2_async_subdev *asd;
582
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400583 asd = v4l2_async_notifier_add_fwnode_subdev(
584 notifier, args.fwnode, sizeof(*asd));
585 if (IS_ERR(asd)) {
586 ret = PTR_ERR(asd);
587 /* not an error if asd already exists */
588 if (ret == -EEXIST) {
589 fwnode_handle_put(args.fwnode);
590 continue;
591 }
592
Sakari Ailusd8428532017-06-20 07:47:24 -0400593 goto error;
594 }
Sakari Ailusd8428532017-06-20 07:47:24 -0400595 }
596
597 return 0;
598
599error:
600 fwnode_handle_put(args.fwnode);
601 return ret;
602}
603
Sakari Ailusa1699a42017-06-22 08:03:28 -0400604/*
605 * v4l2_fwnode_reference_get_int_prop - parse a reference with integer
606 * arguments
607 * @fwnode: fwnode to read @prop from
608 * @notifier: notifier for @dev
609 * @prop: the name of the property
610 * @index: the index of the reference to get
611 * @props: the array of integer property names
612 * @nprops: the number of integer property names in @nprops
613 *
614 * First find an fwnode referred to by the reference at @index in @prop.
615 *
616 * Then under that fwnode, @nprops times, for each property in @props,
617 * iteratively follow child nodes starting from fwnode such that they have the
618 * property in @props array at the index of the child node distance from the
619 * root node and the value of that property matching with the integer argument
620 * of the reference, at the same index.
621 *
622 * The child fwnode reched at the end of the iteration is then returned to the
623 * caller.
624 *
625 * The core reason for this is that you cannot refer to just any node in ACPI.
626 * So to refer to an endpoint (easy in DT) you need to refer to a device, then
627 * provide a list of (property name, property value) tuples where each tuple
628 * uniquely identifies a child node. The first tuple identifies a child directly
629 * underneath the device fwnode, the next tuple identifies a child node
630 * underneath the fwnode identified by the previous tuple, etc. until you
631 * reached the fwnode you need.
632 *
633 * An example with a graph, as defined in Documentation/acpi/dsd/graph.txt:
634 *
635 * Scope (\_SB.PCI0.I2C2)
636 * {
637 * Device (CAM0)
638 * {
639 * Name (_DSD, Package () {
640 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
641 * Package () {
642 * Package () {
643 * "compatible",
644 * Package () { "nokia,smia" }
645 * },
646 * },
647 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
648 * Package () {
649 * Package () { "port0", "PRT0" },
650 * }
651 * })
652 * Name (PRT0, Package() {
653 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
654 * Package () {
655 * Package () { "port", 0 },
656 * },
657 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
658 * Package () {
659 * Package () { "endpoint0", "EP00" },
660 * }
661 * })
662 * Name (EP00, Package() {
663 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
664 * Package () {
665 * Package () { "endpoint", 0 },
666 * Package () {
667 * "remote-endpoint",
668 * Package() {
669 * \_SB.PCI0.ISP, 4, 0
670 * }
671 * },
672 * }
673 * })
674 * }
675 * }
676 *
677 * Scope (\_SB.PCI0)
678 * {
679 * Device (ISP)
680 * {
681 * Name (_DSD, Package () {
682 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
683 * Package () {
684 * Package () { "port4", "PRT4" },
685 * }
686 * })
687 *
688 * Name (PRT4, Package() {
689 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
690 * Package () {
691 * Package () { "port", 4 },
692 * },
693 * ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
694 * Package () {
695 * Package () { "endpoint0", "EP40" },
696 * }
697 * })
698 *
699 * Name (EP40, Package() {
700 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
701 * Package () {
702 * Package () { "endpoint", 0 },
703 * Package () {
704 * "remote-endpoint",
705 * Package () {
706 * \_SB.PCI0.I2C2.CAM0,
707 * 0, 0
708 * }
709 * },
710 * }
711 * })
712 * }
713 * }
714 *
715 * From the EP40 node under ISP device, you could parse the graph remote
716 * endpoint using v4l2_fwnode_reference_get_int_prop with these arguments:
717 *
718 * @fwnode: fwnode referring to EP40 under ISP.
719 * @prop: "remote-endpoint"
720 * @index: 0
721 * @props: "port", "endpoint"
722 * @nprops: 2
723 *
724 * And you'd get back fwnode referring to EP00 under CAM0.
725 *
726 * The same works the other way around: if you use EP00 under CAM0 as the
727 * fwnode, you'll get fwnode referring to EP40 under ISP.
728 *
729 * The same example in DT syntax would look like this:
730 *
731 * cam: cam0 {
732 * compatible = "nokia,smia";
733 *
734 * port {
735 * port = <0>;
736 * endpoint {
737 * endpoint = <0>;
738 * remote-endpoint = <&isp 4 0>;
739 * };
740 * };
741 * };
742 *
743 * isp: isp {
744 * ports {
745 * port@4 {
746 * port = <4>;
747 * endpoint {
748 * endpoint = <0>;
749 * remote-endpoint = <&cam 0 0>;
750 * };
751 * };
752 * };
753 * };
754 *
755 * Return: 0 on success
756 * -ENOENT if no entries (or the property itself) were found
757 * -EINVAL if property parsing otherwise failed
758 * -ENOMEM if memory allocation failed
759 */
760static struct fwnode_handle *v4l2_fwnode_reference_get_int_prop(
761 struct fwnode_handle *fwnode, const char *prop, unsigned int index,
762 const char * const *props, unsigned int nprops)
763{
764 struct fwnode_reference_args fwnode_args;
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300765 u64 *args = fwnode_args.args;
Sakari Ailusa1699a42017-06-22 08:03:28 -0400766 struct fwnode_handle *child;
767 int ret;
768
769 /*
770 * Obtain remote fwnode as well as the integer arguments.
771 *
772 * Note that right now both -ENODATA and -ENOENT may signal
773 * out-of-bounds access. Return -ENOENT in that case.
774 */
775 ret = fwnode_property_get_reference_args(fwnode, prop, NULL, nprops,
776 index, &fwnode_args);
777 if (ret)
778 return ERR_PTR(ret == -ENODATA ? -ENOENT : ret);
779
780 /*
781 * Find a node in the tree under the referred fwnode corresponding to
782 * the integer arguments.
783 */
784 fwnode = fwnode_args.fwnode;
785 while (nprops--) {
786 u32 val;
787
788 /* Loop over all child nodes under fwnode. */
789 fwnode_for_each_child_node(fwnode, child) {
790 if (fwnode_property_read_u32(child, *props, &val))
791 continue;
792
793 /* Found property, see if its value matches. */
794 if (val == *args)
795 break;
796 }
797
798 fwnode_handle_put(fwnode);
799
800 /* No property found; return an error here. */
801 if (!child) {
802 fwnode = ERR_PTR(-ENOENT);
803 break;
804 }
805
806 props++;
807 args++;
808 fwnode = child;
809 }
810
811 return fwnode;
812}
813
814/*
815 * v4l2_fwnode_reference_parse_int_props - parse references for async
816 * sub-devices
817 * @dev: struct device pointer
818 * @notifier: notifier for @dev
819 * @prop: the name of the property
820 * @props: the array of integer property names
821 * @nprops: the number of integer properties
822 *
823 * Use v4l2_fwnode_reference_get_int_prop to find fwnodes through reference in
824 * property @prop with integer arguments with child nodes matching in properties
825 * @props. Then, set up V4L2 async sub-devices for those fwnodes in the notifier
826 * accordingly.
827 *
828 * While it is technically possible to use this function on DT, it is only
829 * meaningful on ACPI. On Device tree you can refer to any node in the tree but
830 * on ACPI the references are limited to devices.
831 *
832 * Return: 0 on success
833 * -ENOENT if no entries (or the property itself) were found
834 * -EINVAL if property parsing otherwisefailed
835 * -ENOMEM if memory allocation failed
836 */
837static int v4l2_fwnode_reference_parse_int_props(
838 struct device *dev, struct v4l2_async_notifier *notifier,
839 const char *prop, const char * const *props, unsigned int nprops)
840{
841 struct fwnode_handle *fwnode;
842 unsigned int index;
843 int ret;
844
Mauro Carvalho Chehab9879c9d2018-04-04 07:21:04 -0400845 index = 0;
846 do {
847 fwnode = v4l2_fwnode_reference_get_int_prop(dev_fwnode(dev),
848 prop, index,
849 props, nprops);
850 if (IS_ERR(fwnode)) {
851 /*
852 * Note that right now both -ENODATA and -ENOENT may
853 * signal out-of-bounds access. Return the error in
854 * cases other than that.
855 */
856 if (PTR_ERR(fwnode) != -ENOENT &&
857 PTR_ERR(fwnode) != -ENODATA)
858 return PTR_ERR(fwnode);
859 break;
860 }
Sakari Ailusa1699a42017-06-22 08:03:28 -0400861 fwnode_handle_put(fwnode);
Mauro Carvalho Chehab9879c9d2018-04-04 07:21:04 -0400862 index++;
863 } while (1);
Sakari Ailusa1699a42017-06-22 08:03:28 -0400864
Sakari Ailusa1699a42017-06-22 08:03:28 -0400865 for (index = 0; !IS_ERR((fwnode = v4l2_fwnode_reference_get_int_prop(
866 dev_fwnode(dev), prop, index, props,
867 nprops))); index++) {
868 struct v4l2_async_subdev *asd;
869
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400870 asd = v4l2_async_notifier_add_fwnode_subdev(notifier, fwnode,
871 sizeof(*asd));
872 if (IS_ERR(asd)) {
873 ret = PTR_ERR(asd);
874 /* not an error if asd already exists */
875 if (ret == -EEXIST) {
876 fwnode_handle_put(fwnode);
877 continue;
878 }
879
Sakari Ailusa1699a42017-06-22 08:03:28 -0400880 goto error;
881 }
Sakari Ailusa1699a42017-06-22 08:03:28 -0400882 }
883
884 return PTR_ERR(fwnode) == -ENOENT ? 0 : PTR_ERR(fwnode);
885
886error:
887 fwnode_handle_put(fwnode);
888 return ret;
889}
890
Sakari Ailus7a9ec802017-09-06 08:35:42 -0400891int v4l2_async_notifier_parse_fwnode_sensor_common(
892 struct device *dev, struct v4l2_async_notifier *notifier)
893{
894 static const char * const led_props[] = { "led" };
895 static const struct {
896 const char *name;
897 const char * const *props;
898 unsigned int nprops;
899 } props[] = {
900 { "flash-leds", led_props, ARRAY_SIZE(led_props) },
901 { "lens-focus", NULL, 0 },
902 };
903 unsigned int i;
904
905 for (i = 0; i < ARRAY_SIZE(props); i++) {
906 int ret;
907
908 if (props[i].props && is_acpi_node(dev_fwnode(dev)))
909 ret = v4l2_fwnode_reference_parse_int_props(
910 dev, notifier, props[i].name,
911 props[i].props, props[i].nprops);
912 else
913 ret = v4l2_fwnode_reference_parse(
914 dev, notifier, props[i].name);
915 if (ret && ret != -ENOENT) {
916 dev_warn(dev, "parsing property \"%s\" failed (%d)\n",
917 props[i].name, ret);
918 return ret;
919 }
920 }
921
922 return 0;
923}
924EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_sensor_common);
925
Sakari Ailusaef69d52017-09-24 18:47:44 -0400926int v4l2_async_register_subdev_sensor_common(struct v4l2_subdev *sd)
927{
928 struct v4l2_async_notifier *notifier;
929 int ret;
930
931 if (WARN_ON(!sd->dev))
932 return -ENODEV;
933
934 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
935 if (!notifier)
936 return -ENOMEM;
937
Steve Longerbeameae2aed2018-09-29 15:54:08 -0400938 v4l2_async_notifier_init(notifier);
939
Sakari Ailusaef69d52017-09-24 18:47:44 -0400940 ret = v4l2_async_notifier_parse_fwnode_sensor_common(sd->dev,
941 notifier);
942 if (ret < 0)
943 goto out_cleanup;
944
945 ret = v4l2_async_subdev_notifier_register(sd, notifier);
946 if (ret < 0)
947 goto out_cleanup;
948
949 ret = v4l2_async_register_subdev(sd);
950 if (ret < 0)
951 goto out_unregister;
952
953 sd->subdev_notifier = notifier;
954
955 return 0;
956
957out_unregister:
958 v4l2_async_notifier_unregister(notifier);
959
960out_cleanup:
961 v4l2_async_notifier_cleanup(notifier);
962 kfree(notifier);
963
964 return ret;
965}
966EXPORT_SYMBOL_GPL(v4l2_async_register_subdev_sensor_common);
967
Steve Longerbeam1634f0e2018-09-29 15:54:09 -0400968int v4l2_async_register_fwnode_subdev(
969 struct v4l2_subdev *sd, size_t asd_struct_size,
970 unsigned int *ports, unsigned int num_ports,
971 int (*parse_endpoint)(struct device *dev,
972 struct v4l2_fwnode_endpoint *vep,
973 struct v4l2_async_subdev *asd))
974{
975 struct v4l2_async_notifier *notifier;
976 struct device *dev = sd->dev;
977 struct fwnode_handle *fwnode;
978 int ret;
979
980 if (WARN_ON(!dev))
981 return -ENODEV;
982
983 fwnode = dev_fwnode(dev);
984 if (!fwnode_device_is_available(fwnode))
985 return -ENODEV;
986
987 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
988 if (!notifier)
989 return -ENOMEM;
990
991 v4l2_async_notifier_init(notifier);
992
993 if (!ports) {
994 ret = v4l2_async_notifier_parse_fwnode_endpoints(
995 dev, notifier, asd_struct_size, parse_endpoint);
996 if (ret < 0)
997 goto out_cleanup;
998 } else {
999 unsigned int i;
1000
1001 for (i = 0; i < num_ports; i++) {
1002 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
1003 dev, notifier, asd_struct_size,
1004 ports[i], parse_endpoint);
1005 if (ret < 0)
1006 goto out_cleanup;
1007 }
1008 }
1009
1010 ret = v4l2_async_subdev_notifier_register(sd, notifier);
1011 if (ret < 0)
1012 goto out_cleanup;
1013
1014 ret = v4l2_async_register_subdev(sd);
1015 if (ret < 0)
1016 goto out_unregister;
1017
1018 sd->subdev_notifier = notifier;
1019
1020 return 0;
1021
1022out_unregister:
1023 v4l2_async_notifier_unregister(notifier);
1024out_cleanup:
1025 v4l2_async_notifier_cleanup(notifier);
1026 kfree(notifier);
1027
1028 return ret;
1029}
1030EXPORT_SYMBOL_GPL(v4l2_async_register_fwnode_subdev);
1031
Sakari Ailusca50c192016-08-12 08:05:51 -03001032MODULE_LICENSE("GPL");
1033MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
1034MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1035MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");