Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 1 | /* |
| 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 Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 22 | #include <linux/mm.h> |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 23 | #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 Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 30 | #include <media/v4l2-async.h> |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 31 | #include <media/v4l2-fwnode.h> |
| 32 | |
Sakari Ailus | e07a41f | 2015-02-25 14:51:01 -0500 | [diff] [blame] | 33 | enum v4l2_fwnode_bus_type { |
| 34 | V4L2_FWNODE_BUS_TYPE_GUESS = 0, |
| 35 | V4L2_FWNODE_BUS_TYPE_CSI2_CPHY, |
| 36 | V4L2_FWNODE_BUS_TYPE_CSI1, |
| 37 | V4L2_FWNODE_BUS_TYPE_CCP2, |
| 38 | NR_OF_V4L2_FWNODE_BUS_TYPE, |
| 39 | }; |
| 40 | |
Sakari Ailus | f311273 | 2017-02-20 05:42:09 -0500 | [diff] [blame] | 41 | static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode, |
| 42 | struct v4l2_fwnode_endpoint *vep) |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 43 | { |
| 44 | struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2; |
| 45 | bool have_clk_lane = false; |
| 46 | unsigned int flags = 0, lanes_used = 0; |
| 47 | unsigned int i; |
| 48 | u32 v; |
| 49 | int rval; |
| 50 | |
| 51 | rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0); |
| 52 | if (rval > 0) { |
Sakari Ailus | ad3cdf3 | 2017-08-14 06:43:07 -0400 | [diff] [blame] | 53 | u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES]; |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 54 | |
Sakari Ailus | ad3cdf3 | 2017-08-14 06:43:07 -0400 | [diff] [blame] | 55 | bus->num_data_lanes = |
| 56 | min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval); |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 57 | |
| 58 | fwnode_property_read_u32_array(fwnode, "data-lanes", array, |
| 59 | bus->num_data_lanes); |
| 60 | |
| 61 | for (i = 0; i < bus->num_data_lanes; i++) { |
| 62 | if (lanes_used & BIT(array[i])) |
| 63 | pr_warn("duplicated lane %u in data-lanes\n", |
| 64 | array[i]); |
| 65 | lanes_used |= BIT(array[i]); |
| 66 | |
| 67 | bus->data_lanes[i] = array[i]; |
| 68 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 69 | |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame] | 70 | rval = fwnode_property_read_u32_array(fwnode, |
Sakari Ailus | b24f021 | 2017-08-14 06:15:21 -0400 | [diff] [blame] | 71 | "lane-polarities", NULL, |
| 72 | 0); |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame] | 73 | if (rval > 0) { |
Sakari Ailus | b24f021 | 2017-08-14 06:15:21 -0400 | [diff] [blame] | 74 | if (rval != 1 + bus->num_data_lanes /* clock+data */) { |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame] | 75 | pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n", |
| 76 | 1 + bus->num_data_lanes, rval); |
| 77 | return -EINVAL; |
| 78 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 79 | |
Sakari Ailus | b24f021 | 2017-08-14 06:15:21 -0400 | [diff] [blame] | 80 | fwnode_property_read_u32_array(fwnode, |
| 81 | "lane-polarities", array, |
| 82 | 1 + bus->num_data_lanes); |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame] | 83 | |
| 84 | for (i = 0; i < 1 + bus->num_data_lanes; i++) |
| 85 | bus->lane_polarities[i] = array[i]; |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 86 | } |
Sakari Ailus | b24f021 | 2017-08-14 06:15:21 -0400 | [diff] [blame] | 87 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) { |
| 91 | if (lanes_used & BIT(v)) |
| 92 | pr_warn("duplicated lane %u in clock-lanes\n", v); |
| 93 | lanes_used |= BIT(v); |
| 94 | |
| 95 | bus->clock_lane = v; |
| 96 | have_clk_lane = true; |
| 97 | } |
| 98 | |
| 99 | if (fwnode_property_present(fwnode, "clock-noncontinuous")) |
| 100 | flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK; |
| 101 | else if (have_clk_lane || bus->num_data_lanes > 0) |
| 102 | flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK; |
| 103 | |
| 104 | bus->flags = flags; |
| 105 | vep->bus_type = V4L2_MBUS_CSI2; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void v4l2_fwnode_endpoint_parse_parallel_bus( |
| 111 | struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep) |
| 112 | { |
| 113 | struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel; |
| 114 | unsigned int flags = 0; |
| 115 | u32 v; |
| 116 | |
| 117 | if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) |
| 118 | flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH : |
| 119 | V4L2_MBUS_HSYNC_ACTIVE_LOW; |
| 120 | |
| 121 | if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) |
| 122 | flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH : |
| 123 | V4L2_MBUS_VSYNC_ACTIVE_LOW; |
| 124 | |
| 125 | if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) |
| 126 | flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH : |
| 127 | V4L2_MBUS_FIELD_EVEN_LOW; |
| 128 | if (flags) |
| 129 | vep->bus_type = V4L2_MBUS_PARALLEL; |
| 130 | else |
| 131 | vep->bus_type = V4L2_MBUS_BT656; |
| 132 | |
| 133 | if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) |
| 134 | flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING : |
| 135 | V4L2_MBUS_PCLK_SAMPLE_FALLING; |
| 136 | |
| 137 | if (!fwnode_property_read_u32(fwnode, "data-active", &v)) |
| 138 | flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH : |
| 139 | V4L2_MBUS_DATA_ACTIVE_LOW; |
| 140 | |
| 141 | if (fwnode_property_present(fwnode, "slave-mode")) |
| 142 | flags |= V4L2_MBUS_SLAVE; |
| 143 | else |
| 144 | flags |= V4L2_MBUS_MASTER; |
| 145 | |
| 146 | if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) |
| 147 | bus->bus_width = v; |
| 148 | |
| 149 | if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) |
| 150 | bus->data_shift = v; |
| 151 | |
| 152 | if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) |
| 153 | flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH : |
| 154 | V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW; |
| 155 | |
| 156 | bus->flags = flags; |
| 157 | |
| 158 | } |
| 159 | |
Mauro Carvalho Chehab | abc5b2c | 2017-07-20 16:27:27 -0400 | [diff] [blame] | 160 | static void |
| 161 | v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode, |
| 162 | struct v4l2_fwnode_endpoint *vep, |
| 163 | u32 bus_type) |
Sakari Ailus | 97bbdf0 | 2015-02-25 14:39:11 -0500 | [diff] [blame] | 164 | { |
| 165 | struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1; |
| 166 | u32 v; |
| 167 | |
| 168 | if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) |
| 169 | bus->clock_inv = v; |
| 170 | |
| 171 | if (!fwnode_property_read_u32(fwnode, "strobe", &v)) |
| 172 | bus->strobe = v; |
| 173 | |
| 174 | if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) |
| 175 | bus->data_lane = v; |
| 176 | |
| 177 | if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) |
| 178 | bus->clock_lane = v; |
| 179 | |
| 180 | if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2) |
| 181 | vep->bus_type = V4L2_MBUS_CCP2; |
| 182 | else |
| 183 | vep->bus_type = V4L2_MBUS_CSI1; |
| 184 | } |
| 185 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 186 | int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode, |
| 187 | struct v4l2_fwnode_endpoint *vep) |
| 188 | { |
Sakari Ailus | e07a41f | 2015-02-25 14:51:01 -0500 | [diff] [blame] | 189 | u32 bus_type = 0; |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 190 | int rval; |
| 191 | |
| 192 | fwnode_graph_parse_endpoint(fwnode, &vep->base); |
| 193 | |
| 194 | /* Zero fields from bus_type to until the end */ |
| 195 | memset(&vep->bus_type, 0, sizeof(*vep) - |
| 196 | offsetof(typeof(*vep), bus_type)); |
| 197 | |
Sakari Ailus | e07a41f | 2015-02-25 14:51:01 -0500 | [diff] [blame] | 198 | fwnode_property_read_u32(fwnode, "bus-type", &bus_type); |
| 199 | |
Sakari Ailus | 97bbdf0 | 2015-02-25 14:39:11 -0500 | [diff] [blame] | 200 | switch (bus_type) { |
| 201 | case V4L2_FWNODE_BUS_TYPE_GUESS: |
| 202 | rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep); |
| 203 | if (rval) |
| 204 | return rval; |
| 205 | /* |
| 206 | * Parse the parallel video bus properties only if none |
| 207 | * of the MIPI CSI-2 specific properties were found. |
| 208 | */ |
| 209 | if (vep->bus.mipi_csi2.flags == 0) |
| 210 | v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep); |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 211 | |
Sakari Ailus | 97bbdf0 | 2015-02-25 14:39:11 -0500 | [diff] [blame] | 212 | return 0; |
| 213 | case V4L2_FWNODE_BUS_TYPE_CCP2: |
| 214 | case V4L2_FWNODE_BUS_TYPE_CSI1: |
| 215 | v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type); |
| 216 | |
| 217 | return 0; |
| 218 | default: |
| 219 | pr_warn("unsupported bus type %u\n", bus_type); |
| 220 | return -EINVAL; |
| 221 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 222 | } |
| 223 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse); |
| 224 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 225 | void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep) |
| 226 | { |
| 227 | if (IS_ERR_OR_NULL(vep)) |
| 228 | return; |
| 229 | |
| 230 | kfree(vep->link_frequencies); |
| 231 | kfree(vep); |
| 232 | } |
| 233 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free); |
| 234 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 235 | struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse( |
| 236 | struct fwnode_handle *fwnode) |
| 237 | { |
| 238 | struct v4l2_fwnode_endpoint *vep; |
| 239 | int rval; |
| 240 | |
| 241 | vep = kzalloc(sizeof(*vep), GFP_KERNEL); |
| 242 | if (!vep) |
| 243 | return ERR_PTR(-ENOMEM); |
| 244 | |
| 245 | rval = v4l2_fwnode_endpoint_parse(fwnode, vep); |
| 246 | if (rval < 0) |
| 247 | goto out_err; |
| 248 | |
| 249 | rval = fwnode_property_read_u64_array(fwnode, "link-frequencies", |
| 250 | NULL, 0); |
Sakari Ailus | 06f8152 | 2017-06-20 09:14:43 -0400 | [diff] [blame] | 251 | if (rval > 0) { |
| 252 | vep->link_frequencies = |
| 253 | kmalloc_array(rval, sizeof(*vep->link_frequencies), |
| 254 | GFP_KERNEL); |
| 255 | if (!vep->link_frequencies) { |
| 256 | rval = -ENOMEM; |
| 257 | goto out_err; |
| 258 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 259 | |
Sakari Ailus | 06f8152 | 2017-06-20 09:14:43 -0400 | [diff] [blame] | 260 | vep->nr_of_link_frequencies = rval; |
| 261 | |
| 262 | rval = fwnode_property_read_u64_array( |
| 263 | fwnode, "link-frequencies", vep->link_frequencies, |
| 264 | vep->nr_of_link_frequencies); |
| 265 | if (rval < 0) |
| 266 | goto out_err; |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 267 | } |
| 268 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 269 | return vep; |
| 270 | |
| 271 | out_err: |
| 272 | v4l2_fwnode_endpoint_free(vep); |
| 273 | return ERR_PTR(rval); |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse); |
| 276 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 277 | int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode, |
| 278 | struct v4l2_fwnode_link *link) |
| 279 | { |
| 280 | const char *port_prop = is_of_node(__fwnode) ? "reg" : "port"; |
| 281 | struct fwnode_handle *fwnode; |
| 282 | |
| 283 | memset(link, 0, sizeof(*link)); |
| 284 | |
| 285 | fwnode = fwnode_get_parent(__fwnode); |
| 286 | fwnode_property_read_u32(fwnode, port_prop, &link->local_port); |
| 287 | fwnode = fwnode_get_next_parent(fwnode); |
| 288 | if (is_of_node(fwnode) && |
| 289 | of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) |
| 290 | fwnode = fwnode_get_next_parent(fwnode); |
| 291 | link->local_node = fwnode; |
| 292 | |
| 293 | fwnode = fwnode_graph_get_remote_endpoint(__fwnode); |
| 294 | if (!fwnode) { |
| 295 | fwnode_handle_put(fwnode); |
| 296 | return -ENOLINK; |
| 297 | } |
| 298 | |
| 299 | fwnode = fwnode_get_parent(fwnode); |
| 300 | fwnode_property_read_u32(fwnode, port_prop, &link->remote_port); |
| 301 | fwnode = fwnode_get_next_parent(fwnode); |
| 302 | if (is_of_node(fwnode) && |
| 303 | of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) |
| 304 | fwnode = fwnode_get_next_parent(fwnode); |
| 305 | link->remote_node = fwnode; |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link); |
| 310 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 311 | void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link) |
| 312 | { |
| 313 | fwnode_handle_put(link->local_node); |
| 314 | fwnode_handle_put(link->remote_node); |
| 315 | } |
| 316 | EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link); |
| 317 | |
Sakari Ailus | 9ca4653 | 2017-08-17 11:28:21 -0400 | [diff] [blame] | 318 | static int v4l2_async_notifier_realloc(struct v4l2_async_notifier *notifier, |
| 319 | unsigned int max_subdevs) |
| 320 | { |
| 321 | struct v4l2_async_subdev **subdevs; |
| 322 | |
| 323 | if (max_subdevs <= notifier->max_subdevs) |
| 324 | return 0; |
| 325 | |
| 326 | subdevs = kvmalloc_array( |
| 327 | max_subdevs, sizeof(*notifier->subdevs), |
| 328 | GFP_KERNEL | __GFP_ZERO); |
| 329 | if (!subdevs) |
| 330 | return -ENOMEM; |
| 331 | |
| 332 | if (notifier->subdevs) { |
| 333 | memcpy(subdevs, notifier->subdevs, |
| 334 | sizeof(*subdevs) * notifier->num_subdevs); |
| 335 | |
| 336 | kvfree(notifier->subdevs); |
| 337 | } |
| 338 | |
| 339 | notifier->subdevs = subdevs; |
| 340 | notifier->max_subdevs = max_subdevs; |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int v4l2_async_notifier_fwnode_parse_endpoint( |
| 346 | struct device *dev, struct v4l2_async_notifier *notifier, |
| 347 | struct fwnode_handle *endpoint, unsigned int asd_struct_size, |
| 348 | int (*parse_endpoint)(struct device *dev, |
| 349 | struct v4l2_fwnode_endpoint *vep, |
| 350 | struct v4l2_async_subdev *asd)) |
| 351 | { |
| 352 | struct v4l2_async_subdev *asd; |
| 353 | struct v4l2_fwnode_endpoint *vep; |
| 354 | int ret = 0; |
| 355 | |
| 356 | asd = kzalloc(asd_struct_size, GFP_KERNEL); |
| 357 | if (!asd) |
| 358 | return -ENOMEM; |
| 359 | |
| 360 | asd->match_type = V4L2_ASYNC_MATCH_FWNODE; |
| 361 | asd->match.fwnode.fwnode = |
| 362 | fwnode_graph_get_remote_port_parent(endpoint); |
| 363 | if (!asd->match.fwnode.fwnode) { |
| 364 | dev_warn(dev, "bad remote port parent\n"); |
| 365 | ret = -EINVAL; |
| 366 | goto out_err; |
| 367 | } |
| 368 | |
| 369 | vep = v4l2_fwnode_endpoint_alloc_parse(endpoint); |
| 370 | if (IS_ERR(vep)) { |
| 371 | ret = PTR_ERR(vep); |
| 372 | dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n", |
| 373 | ret); |
| 374 | goto out_err; |
| 375 | } |
| 376 | |
| 377 | ret = parse_endpoint ? parse_endpoint(dev, vep, asd) : 0; |
| 378 | if (ret == -ENOTCONN) |
| 379 | dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep->base.port, |
| 380 | vep->base.id); |
| 381 | else if (ret < 0) |
| 382 | dev_warn(dev, |
| 383 | "driver could not parse port@%u/endpoint@%u (%d)\n", |
| 384 | vep->base.port, vep->base.id, ret); |
| 385 | v4l2_fwnode_endpoint_free(vep); |
| 386 | if (ret < 0) |
| 387 | goto out_err; |
| 388 | |
| 389 | notifier->subdevs[notifier->num_subdevs] = asd; |
| 390 | notifier->num_subdevs++; |
| 391 | |
| 392 | return 0; |
| 393 | |
| 394 | out_err: |
| 395 | fwnode_handle_put(asd->match.fwnode.fwnode); |
| 396 | kfree(asd); |
| 397 | |
| 398 | return ret == -ENOTCONN ? 0 : ret; |
| 399 | } |
| 400 | |
| 401 | static int __v4l2_async_notifier_parse_fwnode_endpoints( |
| 402 | struct device *dev, struct v4l2_async_notifier *notifier, |
| 403 | size_t asd_struct_size, unsigned int port, bool has_port, |
| 404 | int (*parse_endpoint)(struct device *dev, |
| 405 | struct v4l2_fwnode_endpoint *vep, |
| 406 | struct v4l2_async_subdev *asd)) |
| 407 | { |
| 408 | struct fwnode_handle *fwnode; |
| 409 | unsigned int max_subdevs = notifier->max_subdevs; |
| 410 | int ret; |
| 411 | |
| 412 | if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev))) |
| 413 | return -EINVAL; |
| 414 | |
| 415 | for (fwnode = NULL; (fwnode = fwnode_graph_get_next_endpoint( |
| 416 | dev_fwnode(dev), fwnode)); ) { |
| 417 | struct fwnode_handle *dev_fwnode; |
| 418 | bool is_available; |
| 419 | |
| 420 | dev_fwnode = fwnode_graph_get_port_parent(fwnode); |
| 421 | is_available = fwnode_device_is_available(dev_fwnode); |
| 422 | fwnode_handle_put(dev_fwnode); |
| 423 | if (!is_available) |
| 424 | continue; |
| 425 | |
| 426 | if (has_port) { |
| 427 | struct fwnode_endpoint ep; |
| 428 | |
| 429 | ret = fwnode_graph_parse_endpoint(fwnode, &ep); |
| 430 | if (ret) { |
| 431 | fwnode_handle_put(fwnode); |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | if (ep.port != port) |
| 436 | continue; |
| 437 | } |
| 438 | max_subdevs++; |
| 439 | } |
| 440 | |
| 441 | /* No subdevs to add? Return here. */ |
| 442 | if (max_subdevs == notifier->max_subdevs) |
| 443 | return 0; |
| 444 | |
| 445 | ret = v4l2_async_notifier_realloc(notifier, max_subdevs); |
| 446 | if (ret) |
| 447 | return ret; |
| 448 | |
| 449 | for (fwnode = NULL; (fwnode = fwnode_graph_get_next_endpoint( |
| 450 | dev_fwnode(dev), fwnode)); ) { |
| 451 | struct fwnode_handle *dev_fwnode; |
| 452 | bool is_available; |
| 453 | |
| 454 | dev_fwnode = fwnode_graph_get_port_parent(fwnode); |
| 455 | is_available = fwnode_device_is_available(dev_fwnode); |
| 456 | fwnode_handle_put(dev_fwnode); |
| 457 | |
| 458 | if (!fwnode_device_is_available(dev_fwnode)) |
| 459 | continue; |
| 460 | |
| 461 | if (WARN_ON(notifier->num_subdevs >= notifier->max_subdevs)) { |
| 462 | ret = -EINVAL; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | if (has_port) { |
| 467 | struct fwnode_endpoint ep; |
| 468 | |
| 469 | ret = fwnode_graph_parse_endpoint(fwnode, &ep); |
| 470 | if (ret) |
| 471 | break; |
| 472 | |
| 473 | if (ep.port != port) |
| 474 | continue; |
| 475 | } |
| 476 | |
| 477 | ret = v4l2_async_notifier_fwnode_parse_endpoint( |
| 478 | dev, notifier, fwnode, asd_struct_size, parse_endpoint); |
| 479 | if (ret < 0) |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | fwnode_handle_put(fwnode); |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | int v4l2_async_notifier_parse_fwnode_endpoints( |
| 489 | struct device *dev, struct v4l2_async_notifier *notifier, |
| 490 | size_t asd_struct_size, |
| 491 | int (*parse_endpoint)(struct device *dev, |
| 492 | struct v4l2_fwnode_endpoint *vep, |
| 493 | struct v4l2_async_subdev *asd)) |
| 494 | { |
| 495 | return __v4l2_async_notifier_parse_fwnode_endpoints( |
| 496 | dev, notifier, asd_struct_size, 0, false, parse_endpoint); |
| 497 | } |
| 498 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints); |
| 499 | |
| 500 | int v4l2_async_notifier_parse_fwnode_endpoints_by_port( |
| 501 | struct device *dev, struct v4l2_async_notifier *notifier, |
| 502 | size_t asd_struct_size, unsigned int port, |
| 503 | int (*parse_endpoint)(struct device *dev, |
| 504 | struct v4l2_fwnode_endpoint *vep, |
| 505 | struct v4l2_async_subdev *asd)) |
| 506 | { |
| 507 | return __v4l2_async_notifier_parse_fwnode_endpoints( |
| 508 | dev, notifier, asd_struct_size, port, true, parse_endpoint); |
| 509 | } |
| 510 | EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port); |
| 511 | |
Sakari Ailus | d842853 | 2017-06-20 07:47:24 -0400 | [diff] [blame^] | 512 | /* |
| 513 | * v4l2_fwnode_reference_parse - parse references for async sub-devices |
| 514 | * @dev: the device node the properties of which are parsed for references |
| 515 | * @notifier: the async notifier where the async subdevs will be added |
| 516 | * @prop: the name of the property |
| 517 | * |
| 518 | * Return: 0 on success |
| 519 | * -ENOENT if no entries were found |
| 520 | * -ENOMEM if memory allocation failed |
| 521 | * -EINVAL if property parsing failed |
| 522 | */ |
| 523 | static int v4l2_fwnode_reference_parse( |
| 524 | struct device *dev, struct v4l2_async_notifier *notifier, |
| 525 | const char *prop) |
| 526 | { |
| 527 | struct fwnode_reference_args args; |
| 528 | unsigned int index; |
| 529 | int ret; |
| 530 | |
| 531 | for (index = 0; |
| 532 | !(ret = fwnode_property_get_reference_args( |
| 533 | dev_fwnode(dev), prop, NULL, 0, index, &args)); |
| 534 | index++) |
| 535 | fwnode_handle_put(args.fwnode); |
| 536 | |
| 537 | if (!index) |
| 538 | return -ENOENT; |
| 539 | |
| 540 | /* |
| 541 | * Note that right now both -ENODATA and -ENOENT may signal |
| 542 | * out-of-bounds access. Return the error in cases other than that. |
| 543 | */ |
| 544 | if (ret != -ENOENT && ret != -ENODATA) |
| 545 | return ret; |
| 546 | |
| 547 | ret = v4l2_async_notifier_realloc(notifier, |
| 548 | notifier->num_subdevs + index); |
| 549 | if (ret) |
| 550 | return ret; |
| 551 | |
| 552 | for (index = 0; !fwnode_property_get_reference_args( |
| 553 | dev_fwnode(dev), prop, NULL, 0, index, &args); |
| 554 | index++) { |
| 555 | struct v4l2_async_subdev *asd; |
| 556 | |
| 557 | if (WARN_ON(notifier->num_subdevs >= notifier->max_subdevs)) { |
| 558 | ret = -EINVAL; |
| 559 | goto error; |
| 560 | } |
| 561 | |
| 562 | asd = kzalloc(sizeof(*asd), GFP_KERNEL); |
| 563 | if (!asd) { |
| 564 | ret = -ENOMEM; |
| 565 | goto error; |
| 566 | } |
| 567 | |
| 568 | notifier->subdevs[notifier->num_subdevs] = asd; |
| 569 | asd->match.fwnode.fwnode = args.fwnode; |
| 570 | asd->match_type = V4L2_ASYNC_MATCH_FWNODE; |
| 571 | notifier->num_subdevs++; |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | |
| 576 | error: |
| 577 | fwnode_handle_put(args.fwnode); |
| 578 | return ret; |
| 579 | } |
| 580 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 581 | MODULE_LICENSE("GPL"); |
| 582 | MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>"); |
| 583 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 584 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |