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 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 39 static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode, 40 struct v4l2_fwnode_endpoint *vep) 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) { 51 u32 array[MAX_DATA_LANES + 1]; 52 53 bus->num_data_lanes = min_t(int, MAX_DATA_LANES, rval); 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 } 66 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 } 76 77 78 for (i = 0; i < 1 + bus->num_data_lanes; i++) 79 bus->lane_polarities[i] = array[i]; 80 } 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 153 static void 154 v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode, 155 struct v4l2_fwnode_endpoint *vep, 156 u32 bus_type) 157 { 158 struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1; 159 u32 v; 160 161 if (!fwnode_property_read_u32(fwnode, "clock-inv", &v)) 162 bus->clock_inv = v; 163 164 if (!fwnode_property_read_u32(fwnode, "strobe", &v)) 165 bus->strobe = v; 166 167 if (!fwnode_property_read_u32(fwnode, "data-lanes", &v)) 168 bus->data_lane = v; 169 170 if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) 171 bus->clock_lane = v; 172 173 if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2) 174 vep->bus_type = V4L2_MBUS_CCP2; 175 else 176 vep->bus_type = V4L2_MBUS_CSI1; 177 } 178 179 /** 180 * v4l2_fwnode_endpoint_parse() - parse all fwnode node properties 181 * @fwnode: pointer to the endpoint's fwnode handle 182 * @vep: pointer to the V4L2 fwnode data structure 183 * 184 * All properties are optional. If none are found, we don't set any flags. This 185 * means the port has a static configuration and no properties have to be 186 * specified explicitly. If any properties that identify the bus as parallel 187 * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if 188 * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we 189 * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a 190 * reference to @fwnode. 191 * 192 * NOTE: This function does not parse properties the size of which is variable 193 * without a low fixed limit. Please use v4l2_fwnode_endpoint_alloc_parse() in 194 * new drivers instead. 195 * 196 * Return: 0 on success or a negative error code on failure. 197 */ 198 int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode, 199 struct v4l2_fwnode_endpoint *vep) 200 { 201 u32 bus_type = 0; 202 int rval; 203 204 fwnode_graph_parse_endpoint(fwnode, &vep->base); 205 206 /* Zero fields from bus_type to until the end */ 207 memset(&vep->bus_type, 0, sizeof(*vep) - 208 offsetof(typeof(*vep), bus_type)); 209 210 fwnode_property_read_u32(fwnode, "bus-type", &bus_type); 211 212 switch (bus_type) { 213 case V4L2_FWNODE_BUS_TYPE_GUESS: 214 rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep); 215 if (rval) 216 return rval; 217 /* 218 * Parse the parallel video bus properties only if none 219 * of the MIPI CSI-2 specific properties were found. 220 */ 221 if (vep->bus.mipi_csi2.flags == 0) 222 v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep); 223 224 return 0; 225 case V4L2_FWNODE_BUS_TYPE_CCP2: 226 case V4L2_FWNODE_BUS_TYPE_CSI1: 227 v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type); 228 229 return 0; 230 default: 231 pr_warn("unsupported bus type %u\n", bus_type); 232 return -EINVAL; 233 } 234 } 235 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse); 236 237 /* 238 * v4l2_fwnode_endpoint_free() - free the V4L2 fwnode acquired by 239 * v4l2_fwnode_endpoint_alloc_parse() 240 * @vep - the V4L2 fwnode the resources of which are to be released 241 * 242 * It is safe to call this function with NULL argument or on a V4L2 fwnode the 243 * parsing of which failed. 244 */ 245 void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep) 246 { 247 if (IS_ERR_OR_NULL(vep)) 248 return; 249 250 kfree(vep->link_frequencies); 251 kfree(vep); 252 } 253 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free); 254 255 /** 256 * v4l2_fwnode_endpoint_alloc_parse() - parse all fwnode node properties 257 * @fwnode: pointer to the endpoint's fwnode handle 258 * 259 * All properties are optional. If none are found, we don't set any flags. This 260 * means the port has a static configuration and no properties have to be 261 * specified explicitly. If any properties that identify the bus as parallel 262 * are found and slave-mode isn't set, we set V4L2_MBUS_MASTER. Similarly, if 263 * we recognise the bus as serial CSI-2 and clock-noncontinuous isn't set, we 264 * set the V4L2_MBUS_CSI2_CONTINUOUS_CLOCK flag. The caller should hold a 265 * reference to @fwnode. 266 * 267 * v4l2_fwnode_endpoint_alloc_parse() has two important differences to 268 * v4l2_fwnode_endpoint_parse(): 269 * 270 * 1. It also parses variable size data. 271 * 272 * 2. The memory it has allocated to store the variable size data must be freed 273 * using v4l2_fwnode_endpoint_free() when no longer needed. 274 * 275 * Return: Pointer to v4l2_fwnode_endpoint if successful, on an error pointer 276 * on error. 277 */ 278 struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse( 279 struct fwnode_handle *fwnode) 280 { 281 struct v4l2_fwnode_endpoint *vep; 282 int rval; 283 284 vep = kzalloc(sizeof(*vep), GFP_KERNEL); 285 if (!vep) 286 return ERR_PTR(-ENOMEM); 287 288 rval = v4l2_fwnode_endpoint_parse(fwnode, vep); 289 if (rval < 0) 290 goto out_err; 291 292 rval = fwnode_property_read_u64_array(fwnode, "link-frequencies", 293 NULL, 0); 294 if (rval > 0) { 295 vep->link_frequencies = 296 kmalloc_array(rval, sizeof(*vep->link_frequencies), 297 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( 306 fwnode, "link-frequencies", vep->link_frequencies, 307 vep->nr_of_link_frequencies); 308 if (rval < 0) 309 goto out_err; 310 } 311 312 return vep; 313 314 out_err: 315 v4l2_fwnode_endpoint_free(vep); 316 return ERR_PTR(rval); 317 } 318 EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse); 319 320 /** 321 * v4l2_fwnode_endpoint_parse_link() - parse a link between two endpoints 322 * @__fwnode: pointer to the endpoint's fwnode at the local end of the link 323 * @link: pointer to the V4L2 fwnode link data structure 324 * 325 * Fill the link structure with the local and remote nodes and port numbers. 326 * The local_node and remote_node fields are set to point to the local and 327 * remote port's parent nodes respectively (the port parent node being the 328 * parent node of the port node if that node isn't a 'ports' node, or the 329 * grand-parent node of the port node otherwise). 330 * 331 * A reference is taken to both the local and remote nodes, the caller must use 332 * v4l2_fwnode_endpoint_put_link() to drop the references when done with the 333 * link. 334 * 335 * Return: 0 on success, or -ENOLINK if the remote endpoint fwnode can't be 336 * found. 337 */ 338 int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode, 339 struct v4l2_fwnode_link *link) 340 { 341 const char *port_prop = is_of_node(__fwnode) ? "reg" : "port"; 342 struct fwnode_handle *fwnode; 343 344 memset(link, 0, sizeof(*link)); 345 346 fwnode = fwnode_get_parent(__fwnode); 347 fwnode_property_read_u32(fwnode, port_prop, &link->local_port); 348 fwnode = fwnode_get_next_parent(fwnode); 349 if (is_of_node(fwnode) && 350 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) 351 fwnode = fwnode_get_next_parent(fwnode); 352 link->local_node = fwnode; 353 354 fwnode = fwnode_graph_get_remote_endpoint(__fwnode); 355 if (!fwnode) { 356 fwnode_handle_put(fwnode); 357 return -ENOLINK; 358 } 359 360 fwnode = fwnode_get_parent(fwnode); 361 fwnode_property_read_u32(fwnode, port_prop, &link->remote_port); 362 fwnode = fwnode_get_next_parent(fwnode); 363 if (is_of_node(fwnode) && 364 of_node_cmp(to_of_node(fwnode)->name, "ports") == 0) 365 fwnode = fwnode_get_next_parent(fwnode); 366 link->remote_node = fwnode; 367 368 return 0; 369 } 370 EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link); 371 372 /** 373 * v4l2_fwnode_put_link() - drop references to nodes in a link 374 * @link: pointer to the V4L2 fwnode link data structure 375 * 376 * Drop references to the local and remote nodes in the link. This function 377 * must be called on every link parsed with v4l2_fwnode_parse_link(). 378 */ 379 void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link) 380 { 381 fwnode_handle_put(link->local_node); 382 fwnode_handle_put(link->remote_node); 383 } 384 EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link); 385 386 MODULE_LICENSE("GPL"); 387 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>"); 388 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>"); 389 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); 390