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> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/property.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/types.h> |
| 28 | |
| 29 | #include <media/v4l2-fwnode.h> |
| 30 | |
Sakari Ailus | e07a41f | 2015-02-25 14:51:01 -0500 | [diff] [blame] | 31 | enum v4l2_fwnode_bus_type { |
| 32 | V4L2_FWNODE_BUS_TYPE_GUESS = 0, |
| 33 | V4L2_FWNODE_BUS_TYPE_CSI2_CPHY, |
| 34 | V4L2_FWNODE_BUS_TYPE_CSI1, |
| 35 | V4L2_FWNODE_BUS_TYPE_CCP2, |
| 36 | NR_OF_V4L2_FWNODE_BUS_TYPE, |
| 37 | }; |
| 38 | |
Sakari Ailus | f311273 | 2017-02-20 05:42:09 -0500 | [diff] [blame] | 39 | static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode, |
| 40 | struct v4l2_fwnode_endpoint *vep) |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 41 | { |
| 42 | struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2; |
| 43 | bool have_clk_lane = false; |
| 44 | unsigned int flags = 0, lanes_used = 0; |
| 45 | unsigned int i; |
| 46 | u32 v; |
| 47 | int rval; |
| 48 | |
| 49 | rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0); |
| 50 | if (rval > 0) { |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame^] | 51 | u32 array[MAX_DATA_LANES + 1]; |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 52 | |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame^] | 53 | bus->num_data_lanes = min_t(int, MAX_DATA_LANES, rval); |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 54 | |
| 55 | fwnode_property_read_u32_array(fwnode, "data-lanes", array, |
| 56 | bus->num_data_lanes); |
| 57 | |
| 58 | for (i = 0; i < bus->num_data_lanes; i++) { |
| 59 | if (lanes_used & BIT(array[i])) |
| 60 | pr_warn("duplicated lane %u in data-lanes\n", |
| 61 | array[i]); |
| 62 | lanes_used |= BIT(array[i]); |
| 63 | |
| 64 | bus->data_lanes[i] = array[i]; |
| 65 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 66 | |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame^] | 67 | rval = fwnode_property_read_u32_array(fwnode, |
| 68 | "lane-polarities", array, |
| 69 | 1 + bus->num_data_lanes); |
| 70 | if (rval > 0) { |
| 71 | if (rval != 1 + bus->num_data_lanes /* clock + data */) { |
| 72 | pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n", |
| 73 | 1 + bus->num_data_lanes, rval); |
| 74 | return -EINVAL; |
| 75 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 76 | |
Mauro Carvalho Chehab | 4ee2362 | 2017-06-26 14:07:54 -0400 | [diff] [blame^] | 77 | |
| 78 | for (i = 0; i < 1 + bus->num_data_lanes; i++) |
| 79 | bus->lane_polarities[i] = array[i]; |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 80 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) { |
| 84 | if (lanes_used & BIT(v)) |
| 85 | pr_warn("duplicated lane %u in clock-lanes\n", v); |
| 86 | lanes_used |= BIT(v); |
| 87 | |
| 88 | bus->clock_lane = v; |
| 89 | have_clk_lane = true; |
| 90 | } |
| 91 | |
| 92 | if (fwnode_property_present(fwnode, "clock-noncontinuous")) |
| 93 | flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK; |
| 94 | else if (have_clk_lane || bus->num_data_lanes > 0) |
| 95 | flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK; |
| 96 | |
| 97 | bus->flags = flags; |
| 98 | vep->bus_type = V4L2_MBUS_CSI2; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static void v4l2_fwnode_endpoint_parse_parallel_bus( |
| 104 | struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep) |
| 105 | { |
| 106 | struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel; |
| 107 | unsigned int flags = 0; |
| 108 | u32 v; |
| 109 | |
| 110 | if (!fwnode_property_read_u32(fwnode, "hsync-active", &v)) |
| 111 | flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH : |
| 112 | V4L2_MBUS_HSYNC_ACTIVE_LOW; |
| 113 | |
| 114 | if (!fwnode_property_read_u32(fwnode, "vsync-active", &v)) |
| 115 | flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH : |
| 116 | V4L2_MBUS_VSYNC_ACTIVE_LOW; |
| 117 | |
| 118 | if (!fwnode_property_read_u32(fwnode, "field-even-active", &v)) |
| 119 | flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH : |
| 120 | V4L2_MBUS_FIELD_EVEN_LOW; |
| 121 | if (flags) |
| 122 | vep->bus_type = V4L2_MBUS_PARALLEL; |
| 123 | else |
| 124 | vep->bus_type = V4L2_MBUS_BT656; |
| 125 | |
| 126 | if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v)) |
| 127 | flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING : |
| 128 | V4L2_MBUS_PCLK_SAMPLE_FALLING; |
| 129 | |
| 130 | if (!fwnode_property_read_u32(fwnode, "data-active", &v)) |
| 131 | flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH : |
| 132 | V4L2_MBUS_DATA_ACTIVE_LOW; |
| 133 | |
| 134 | if (fwnode_property_present(fwnode, "slave-mode")) |
| 135 | flags |= V4L2_MBUS_SLAVE; |
| 136 | else |
| 137 | flags |= V4L2_MBUS_MASTER; |
| 138 | |
| 139 | if (!fwnode_property_read_u32(fwnode, "bus-width", &v)) |
| 140 | bus->bus_width = v; |
| 141 | |
| 142 | if (!fwnode_property_read_u32(fwnode, "data-shift", &v)) |
| 143 | bus->data_shift = v; |
| 144 | |
| 145 | if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v)) |
| 146 | flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH : |
| 147 | V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW; |
| 148 | |
| 149 | bus->flags = flags; |
| 150 | |
| 151 | } |
| 152 | |
Sakari Ailus | 97bbdf0 | 2015-02-25 14:39:11 -0500 | [diff] [blame] | 153 | void v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode, |
| 154 | struct v4l2_fwnode_endpoint *vep, |
| 155 | u32 bus_type) |
| 156 | { |
| 157 | struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1; |
| 158 | u32 v; |
| 159 | |
| 160 | if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) |
| 161 | bus->clock_inv = v; |
| 162 | |
| 163 | if (!fwnode_property_read_u32(fwnode, "strobe", &v)) |
| 164 | bus->strobe = v; |
| 165 | |
| 166 | if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) |
| 167 | bus->data_lane = v; |
| 168 | |
| 169 | if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) |
| 170 | bus->clock_lane = v; |
| 171 | |
| 172 | if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2) |
| 173 | vep->bus_type = V4L2_MBUS_CCP2; |
| 174 | else |
| 175 | vep->bus_type = V4L2_MBUS_CSI1; |
| 176 | } |
| 177 | |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 178 | /** |
| 179 | * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties |
| 180 | * @fwnode: pointer to the endpoint's fwnode handle |
| 181 | * @vep: pointer to the V4L2 fwnode data structure |
| 182 | * |
| 183 | * All properties are optional. If none are found, we don't set any flags. This |
| 184 | * means the port has a static configuration and no properties have to be |
| 185 | * specified explicitly. If any properties that identify the bus as parallel |
| 186 | * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if |
| 187 | * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we |
| 188 | * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a |
| 189 | * reference to @fwnode. |
| 190 | * |
| 191 | * NOTE: This function does not parse properties the size of which is variable |
| 192 | * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in |
| 193 | * new drivers instead. |
| 194 | * |
| 195 | * Return: 0 on success or a negative error code on failure. |
| 196 | */ |
| 197 | int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode, |
| 198 | struct v4l2_fwnode_endpoint *vep) |
| 199 | { |
Sakari Ailus | e07a41f | 2015-02-25 14:51:01 -0500 | [diff] [blame] | 200 | u32 bus_type = 0; |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 201 | int rval; |
| 202 | |
| 203 | fwnode_graph_parse_endpoint(fwnode, &vep->base); |
| 204 | |
| 205 | /* Zero fields from bus_type to until the end */ |
| 206 | memset(&vep->bus_type, 0, sizeof(*vep) - |
| 207 | offsetof(typeof(*vep), bus_type)); |
| 208 | |
Sakari Ailus | e07a41f | 2015-02-25 14:51:01 -0500 | [diff] [blame] | 209 | fwnode_property_read_u32(fwnode, "bus-type", &bus_type); |
| 210 | |
Sakari Ailus | 97bbdf0 | 2015-02-25 14:39:11 -0500 | [diff] [blame] | 211 | switch (bus_type) { |
| 212 | case V4L2_FWNODE_BUS_TYPE_GUESS: |
| 213 | rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep); |
| 214 | if (rval) |
| 215 | return rval; |
| 216 | /* |
| 217 | * Parse the parallel video bus properties only if none |
| 218 | * of the MIPI CSI-2 specific properties were found. |
| 219 | */ |
| 220 | if (vep->bus.mipi_csi2.flags == 0) |
| 221 | v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep); |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 222 | |
Sakari Ailus | 97bbdf0 | 2015-02-25 14:39:11 -0500 | [diff] [blame] | 223 | return 0; |
| 224 | case V4L2_FWNODE_BUS_TYPE_CCP2: |
| 225 | case V4L2_FWNODE_BUS_TYPE_CSI1: |
| 226 | v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type); |
| 227 | |
| 228 | return 0; |
| 229 | default: |
| 230 | pr_warn("unsupported bus type %u\n", bus_type); |
| 231 | return -EINVAL; |
| 232 | } |
Sakari Ailus | ca50c19 | 2016-08-12 08:05:51 -0300 | [diff] [blame] | 233 | } |
| 234 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse); |
| 235 | |
| 236 | /* |
| 237 | * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by |
| 238 | * v4l2_fwnode_endpoint_alloc_parse() |
| 239 | * @vep - the V4L2 fwnode the resources of which are to be released |
| 240 | * |
| 241 | * It is safe to call this function with NULL argument or on a V4L2 fwnode the |
| 242 | * parsing of which failed. |
| 243 | */ |
| 244 | void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep) |
| 245 | { |
| 246 | if (IS_ERR_OR_NULL(vep)) |
| 247 | return; |
| 248 | |
| 249 | kfree(vep->link_frequencies); |
| 250 | kfree(vep); |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free); |
| 253 | |
| 254 | /** |
| 255 | * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties |
| 256 | * @fwnode: pointer to the endpoint's fwnode handle |
| 257 | * |
| 258 | * All properties are optional. If none are found, we don't set any flags. This |
| 259 | * means the port has a static configuration and no properties have to be |
| 260 | * specified explicitly. If any properties that identify the bus as parallel |
| 261 | * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if |
| 262 | * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we |
| 263 | * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a |
| 264 | * reference to @fwnode. |
| 265 | * |
| 266 | * v4l2_fwnode_endpoint_alloc_parse() has two important differences to |
| 267 | * v4l2_fwnode_endpoint_parse(): |
| 268 | * |
| 269 | * 1. It also parses variable size data. |
| 270 | * |
| 271 | * 2. The memory it has allocated to store the variable size data must be freed |
| 272 | * using v4l2_fwnode_endpoint_free() when no longer needed. |
| 273 | * |
| 274 | * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer |
| 275 | * on error. |
| 276 | */ |
| 277 | struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse( |
| 278 | struct fwnode_handle *fwnode) |
| 279 | { |
| 280 | struct v4l2_fwnode_endpoint *vep; |
| 281 | int rval; |
| 282 | |
| 283 | vep = kzalloc(sizeof(*vep), GFP_KERNEL); |
| 284 | if (!vep) |
| 285 | return ERR_PTR(-ENOMEM); |
| 286 | |
| 287 | rval = v4l2_fwnode_endpoint_parse(fwnode, vep); |
| 288 | if (rval < 0) |
| 289 | goto out_err; |
| 290 | |
| 291 | rval = fwnode_property_read_u64_array(fwnode, "link-frequencies", |
| 292 | NULL, 0); |
| 293 | if (rval < 0) |
| 294 | goto out_err; |
| 295 | |
| 296 | vep->link_frequencies = |
| 297 | kmalloc_array(rval, sizeof(*vep->link_frequencies), GFP_KERNEL); |
| 298 | if (!vep->link_frequencies) { |
| 299 | rval = -ENOMEM; |
| 300 | goto out_err; |
| 301 | } |
| 302 | |
| 303 | vep->nr_of_link_frequencies = rval; |
| 304 | |
| 305 | rval = fwnode_property_read_u64_array(fwnode, "link-frequencies", |
| 306 | vep->link_frequencies, |
| 307 | vep->nr_of_link_frequencies); |
| 308 | if (rval < 0) |
| 309 | goto out_err; |
| 310 | |
| 311 | return vep; |
| 312 | |
| 313 | out_err: |
| 314 | v4l2_fwnode_endpoint_free(vep); |
| 315 | return ERR_PTR(rval); |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse); |
| 318 | |
| 319 | /** |
| 320 | * v4l2_fwnode_endpoint_parse_link() - parse a link between two endpoints |
| 321 | * @__fwnode: pointer to the endpoint's fwnode at the local end of the link |
| 322 | * @link: pointer to the V4L2 fwnode link data structure |
| 323 | * |
| 324 | * Fill the link structure with the local and remote nodes and port numbers. |
| 325 | * The local_node and remote_node fields are set to point to the local and |
| 326 | * remote port's parent nodes respectively (the port parent node being the |
| 327 | * parent node of the port node if that node isn't a 'ports' node, or the |
| 328 | * grand-parent node of the port node otherwise). |
| 329 | * |
| 330 | * A reference is taken to both the local and remote nodes, the caller must use |
| 331 | * v4l2_fwnode_endpoint_put_link() to drop the references when done with the |
| 332 | * link. |
| 333 | * |
| 334 | * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be |
| 335 | * found. |
| 336 | */ |
| 337 | int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode, |
| 338 | struct v4l2_fwnode_link *link) |
| 339 | { |
| 340 | const char *port_prop = is_of_node(__fwnode) ? "reg" : "port"; |
| 341 | struct fwnode_handle *fwnode; |
| 342 | |
| 343 | memset(link, 0, sizeof(*link)); |
| 344 | |
| 345 | fwnode = fwnode_get_parent(__fwnode); |
| 346 | fwnode_property_read_u32(fwnode, port_prop, &link->local_port); |
| 347 | fwnode = fwnode_get_next_parent(fwnode); |
| 348 | if (is_of_node(fwnode) && |
| 349 | of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) |
| 350 | fwnode = fwnode_get_next_parent(fwnode); |
| 351 | link->local_node = fwnode; |
| 352 | |
| 353 | fwnode = fwnode_graph_get_remote_endpoint(__fwnode); |
| 354 | if (!fwnode) { |
| 355 | fwnode_handle_put(fwnode); |
| 356 | return -ENOLINK; |
| 357 | } |
| 358 | |
| 359 | fwnode = fwnode_get_parent(fwnode); |
| 360 | fwnode_property_read_u32(fwnode, port_prop, &link->remote_port); |
| 361 | fwnode = fwnode_get_next_parent(fwnode); |
| 362 | if (is_of_node(fwnode) && |
| 363 | of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) |
| 364 | fwnode = fwnode_get_next_parent(fwnode); |
| 365 | link->remote_node = fwnode; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link); |
| 370 | |
| 371 | /** |
| 372 | * v4l2_fwnode_put_link() - drop references to nodes in a link |
| 373 | * @link: pointer to the V4L2 fwnode link data structure |
| 374 | * |
| 375 | * Drop references to the local and remote nodes in the link. This function |
| 376 | * must be called on every link parsed with v4l2_fwnode_parse_link(). |
| 377 | */ |
| 378 | void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link) |
| 379 | { |
| 380 | fwnode_handle_put(link->local_node); |
| 381 | fwnode_handle_put(link->remote_node); |
| 382 | } |
| 383 | EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link); |
| 384 | |
| 385 | MODULE_LICENSE("GPL"); |
| 386 | MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>"); |
| 387 | MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); |
| 388 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |