v4l2-fwnode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/mm.h>
  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. #include <media/v4l2-async.h>
  30. #include <media/v4l2-fwnode.h>
  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. static int v4l2_fwnode_endpoint_parse_csi2_bus(struct fwnode_handle *fwnode,
  39. struct v4l2_fwnode_endpoint *vep)
  40. {
  41. struct v4l2_fwnode_bus_mipi_csi2 *bus = &vep->bus.mipi_csi2;
  42. bool have_clk_lane = false;
  43. unsigned int flags = 0, lanes_used = 0;
  44. unsigned int i;
  45. u32 v;
  46. int rval;
  47. rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
  48. if (rval > 0) {
  49. u32 array[1 + V4L2_FWNODE_CSI2_MAX_DATA_LANES];
  50. bus->num_data_lanes =
  51. min_t(int, V4L2_FWNODE_CSI2_MAX_DATA_LANES, rval);
  52. fwnode_property_read_u32_array(fwnode, "data-lanes", array,
  53. bus->num_data_lanes);
  54. for (i = 0; i < bus->num_data_lanes; i++) {
  55. if (lanes_used & BIT(array[i]))
  56. pr_warn("duplicated lane %u in data-lanes\n",
  57. array[i]);
  58. lanes_used |= BIT(array[i]);
  59. bus->data_lanes[i] = array[i];
  60. }
  61. rval = fwnode_property_read_u32_array(fwnode,
  62. "lane-polarities", NULL,
  63. 0);
  64. if (rval > 0) {
  65. if (rval != 1 + bus->num_data_lanes /* clock+data */) {
  66. pr_warn("invalid number of lane-polarities entries (need %u, got %u)\n",
  67. 1 + bus->num_data_lanes, rval);
  68. return -EINVAL;
  69. }
  70. fwnode_property_read_u32_array(fwnode,
  71. "lane-polarities", array,
  72. 1 + bus->num_data_lanes);
  73. for (i = 0; i < 1 + bus->num_data_lanes; i++)
  74. bus->lane_polarities[i] = array[i];
  75. }
  76. }
  77. if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v)) {
  78. if (lanes_used & BIT(v))
  79. pr_warn("duplicated lane %u in clock-lanes\n", v);
  80. lanes_used |= BIT(v);
  81. bus->clock_lane = v;
  82. have_clk_lane = true;
  83. }
  84. if (fwnode_property_present(fwnode, "clock-noncontinuous"))
  85. flags |= V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  86. else if (have_clk_lane || bus->num_data_lanes > 0)
  87. flags |= V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  88. bus->flags = flags;
  89. vep->bus_type = V4L2_MBUS_CSI2;
  90. return 0;
  91. }
  92. static void v4l2_fwnode_endpoint_parse_parallel_bus(
  93. struct fwnode_handle *fwnode, struct v4l2_fwnode_endpoint *vep)
  94. {
  95. struct v4l2_fwnode_bus_parallel *bus = &vep->bus.parallel;
  96. unsigned int flags = 0;
  97. u32 v;
  98. if (!fwnode_property_read_u32(fwnode, "hsync-active", &v))
  99. flags |= v ? V4L2_MBUS_HSYNC_ACTIVE_HIGH :
  100. V4L2_MBUS_HSYNC_ACTIVE_LOW;
  101. if (!fwnode_property_read_u32(fwnode, "vsync-active", &v))
  102. flags |= v ? V4L2_MBUS_VSYNC_ACTIVE_HIGH :
  103. V4L2_MBUS_VSYNC_ACTIVE_LOW;
  104. if (!fwnode_property_read_u32(fwnode, "field-even-active", &v))
  105. flags |= v ? V4L2_MBUS_FIELD_EVEN_HIGH :
  106. V4L2_MBUS_FIELD_EVEN_LOW;
  107. if (flags)
  108. vep->bus_type = V4L2_MBUS_PARALLEL;
  109. else
  110. vep->bus_type = V4L2_MBUS_BT656;
  111. if (!fwnode_property_read_u32(fwnode, "pclk-sample", &v))
  112. flags |= v ? V4L2_MBUS_PCLK_SAMPLE_RISING :
  113. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  114. if (!fwnode_property_read_u32(fwnode, "data-active", &v))
  115. flags |= v ? V4L2_MBUS_DATA_ACTIVE_HIGH :
  116. V4L2_MBUS_DATA_ACTIVE_LOW;
  117. if (fwnode_property_present(fwnode, "slave-mode"))
  118. flags |= V4L2_MBUS_SLAVE;
  119. else
  120. flags |= V4L2_MBUS_MASTER;
  121. if (!fwnode_property_read_u32(fwnode, "bus-width", &v))
  122. bus->bus_width = v;
  123. if (!fwnode_property_read_u32(fwnode, "data-shift", &v))
  124. bus->data_shift = v;
  125. if (!fwnode_property_read_u32(fwnode, "sync-on-green-active", &v))
  126. flags |= v ? V4L2_MBUS_VIDEO_SOG_ACTIVE_HIGH :
  127. V4L2_MBUS_VIDEO_SOG_ACTIVE_LOW;
  128. bus->flags = flags;
  129. }
  130. static void
  131. v4l2_fwnode_endpoint_parse_csi1_bus(struct fwnode_handle *fwnode,
  132. struct v4l2_fwnode_endpoint *vep,
  133. u32 bus_type)
  134. {
  135. struct v4l2_fwnode_bus_mipi_csi1 *bus = &vep->bus.mipi_csi1;
  136. u32 v;
  137. if (!fwnode_property_read_u32(fwnode, "clock-inv", &v))
  138. bus->clock_inv = v;
  139. if (!fwnode_property_read_u32(fwnode, "strobe", &v))
  140. bus->strobe = v;
  141. if (!fwnode_property_read_u32(fwnode, "data-lanes", &v))
  142. bus->data_lane = v;
  143. if (!fwnode_property_read_u32(fwnode, "clock-lanes", &v))
  144. bus->clock_lane = v;
  145. if (bus_type == V4L2_FWNODE_BUS_TYPE_CCP2)
  146. vep->bus_type = V4L2_MBUS_CCP2;
  147. else
  148. vep->bus_type = V4L2_MBUS_CSI1;
  149. }
  150. int v4l2_fwnode_endpoint_parse(struct fwnode_handle *fwnode,
  151. struct v4l2_fwnode_endpoint *vep)
  152. {
  153. u32 bus_type = 0;
  154. int rval;
  155. fwnode_graph_parse_endpoint(fwnode, &vep->base);
  156. /* Zero fields from bus_type to until the end */
  157. memset(&vep->bus_type, 0, sizeof(*vep) -
  158. offsetof(typeof(*vep), bus_type));
  159. fwnode_property_read_u32(fwnode, "bus-type", &bus_type);
  160. switch (bus_type) {
  161. case V4L2_FWNODE_BUS_TYPE_GUESS:
  162. rval = v4l2_fwnode_endpoint_parse_csi2_bus(fwnode, vep);
  163. if (rval)
  164. return rval;
  165. /*
  166. * Parse the parallel video bus properties only if none
  167. * of the MIPI CSI-2 specific properties were found.
  168. */
  169. if (vep->bus.mipi_csi2.flags == 0)
  170. v4l2_fwnode_endpoint_parse_parallel_bus(fwnode, vep);
  171. return 0;
  172. case V4L2_FWNODE_BUS_TYPE_CCP2:
  173. case V4L2_FWNODE_BUS_TYPE_CSI1:
  174. v4l2_fwnode_endpoint_parse_csi1_bus(fwnode, vep, bus_type);
  175. return 0;
  176. default:
  177. pr_warn("unsupported bus type %u\n", bus_type);
  178. return -EINVAL;
  179. }
  180. }
  181. EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_parse);
  182. void v4l2_fwnode_endpoint_free(struct v4l2_fwnode_endpoint *vep)
  183. {
  184. if (IS_ERR_OR_NULL(vep))
  185. return;
  186. kfree(vep->link_frequencies);
  187. kfree(vep);
  188. }
  189. EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_free);
  190. struct v4l2_fwnode_endpoint *v4l2_fwnode_endpoint_alloc_parse(
  191. struct fwnode_handle *fwnode)
  192. {
  193. struct v4l2_fwnode_endpoint *vep;
  194. int rval;
  195. vep = kzalloc(sizeof(*vep), GFP_KERNEL);
  196. if (!vep)
  197. return ERR_PTR(-ENOMEM);
  198. rval = v4l2_fwnode_endpoint_parse(fwnode, vep);
  199. if (rval < 0)
  200. goto out_err;
  201. rval = fwnode_property_read_u64_array(fwnode, "link-frequencies",
  202. NULL, 0);
  203. if (rval > 0) {
  204. vep->link_frequencies =
  205. kmalloc_array(rval, sizeof(*vep->link_frequencies),
  206. GFP_KERNEL);
  207. if (!vep->link_frequencies) {
  208. rval = -ENOMEM;
  209. goto out_err;
  210. }
  211. vep->nr_of_link_frequencies = rval;
  212. rval = fwnode_property_read_u64_array(
  213. fwnode, "link-frequencies", vep->link_frequencies,
  214. vep->nr_of_link_frequencies);
  215. if (rval < 0)
  216. goto out_err;
  217. }
  218. return vep;
  219. out_err:
  220. v4l2_fwnode_endpoint_free(vep);
  221. return ERR_PTR(rval);
  222. }
  223. EXPORT_SYMBOL_GPL(v4l2_fwnode_endpoint_alloc_parse);
  224. int v4l2_fwnode_parse_link(struct fwnode_handle *__fwnode,
  225. struct v4l2_fwnode_link *link)
  226. {
  227. const char *port_prop = is_of_node(__fwnode) ? "reg" : "port";
  228. struct fwnode_handle *fwnode;
  229. memset(link, 0, sizeof(*link));
  230. fwnode = fwnode_get_parent(__fwnode);
  231. fwnode_property_read_u32(fwnode, port_prop, &link->local_port);
  232. fwnode = fwnode_get_next_parent(fwnode);
  233. if (is_of_node(fwnode) &&
  234. of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
  235. fwnode = fwnode_get_next_parent(fwnode);
  236. link->local_node = fwnode;
  237. fwnode = fwnode_graph_get_remote_endpoint(__fwnode);
  238. if (!fwnode) {
  239. fwnode_handle_put(fwnode);
  240. return -ENOLINK;
  241. }
  242. fwnode = fwnode_get_parent(fwnode);
  243. fwnode_property_read_u32(fwnode, port_prop, &link->remote_port);
  244. fwnode = fwnode_get_next_parent(fwnode);
  245. if (is_of_node(fwnode) &&
  246. of_node_cmp(to_of_node(fwnode)->name, "ports") == 0)
  247. fwnode = fwnode_get_next_parent(fwnode);
  248. link->remote_node = fwnode;
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(v4l2_fwnode_parse_link);
  252. void v4l2_fwnode_put_link(struct v4l2_fwnode_link *link)
  253. {
  254. fwnode_handle_put(link->local_node);
  255. fwnode_handle_put(link->remote_node);
  256. }
  257. EXPORT_SYMBOL_GPL(v4l2_fwnode_put_link);
  258. static int v4l2_async_notifier_realloc(struct v4l2_async_notifier *notifier,
  259. unsigned int max_subdevs)
  260. {
  261. struct v4l2_async_subdev **subdevs;
  262. if (max_subdevs <= notifier->max_subdevs)
  263. return 0;
  264. subdevs = kvmalloc_array(
  265. max_subdevs, sizeof(*notifier->subdevs),
  266. GFP_KERNEL | __GFP_ZERO);
  267. if (!subdevs)
  268. return -ENOMEM;
  269. if (notifier->subdevs) {
  270. memcpy(subdevs, notifier->subdevs,
  271. sizeof(*subdevs) * notifier->num_subdevs);
  272. kvfree(notifier->subdevs);
  273. }
  274. notifier->subdevs = subdevs;
  275. notifier->max_subdevs = max_subdevs;
  276. return 0;
  277. }
  278. static int v4l2_async_notifier_fwnode_parse_endpoint(
  279. struct device *dev, struct v4l2_async_notifier *notifier,
  280. struct fwnode_handle *endpoint, unsigned int asd_struct_size,
  281. int (*parse_endpoint)(struct device *dev,
  282. struct v4l2_fwnode_endpoint *vep,
  283. struct v4l2_async_subdev *asd))
  284. {
  285. struct v4l2_async_subdev *asd;
  286. struct v4l2_fwnode_endpoint *vep;
  287. int ret = 0;
  288. asd = kzalloc(asd_struct_size, GFP_KERNEL);
  289. if (!asd)
  290. return -ENOMEM;
  291. asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
  292. asd->match.fwnode.fwnode =
  293. fwnode_graph_get_remote_port_parent(endpoint);
  294. if (!asd->match.fwnode.fwnode) {
  295. dev_warn(dev, "bad remote port parent\n");
  296. ret = -EINVAL;
  297. goto out_err;
  298. }
  299. vep = v4l2_fwnode_endpoint_alloc_parse(endpoint);
  300. if (IS_ERR(vep)) {
  301. ret = PTR_ERR(vep);
  302. dev_warn(dev, "unable to parse V4L2 fwnode endpoint (%d)\n",
  303. ret);
  304. goto out_err;
  305. }
  306. ret = parse_endpoint ? parse_endpoint(dev, vep, asd) : 0;
  307. if (ret == -ENOTCONN)
  308. dev_dbg(dev, "ignoring port@%u/endpoint@%u\n", vep->base.port,
  309. vep->base.id);
  310. else if (ret < 0)
  311. dev_warn(dev,
  312. "driver could not parse port@%u/endpoint@%u (%d)\n",
  313. vep->base.port, vep->base.id, ret);
  314. v4l2_fwnode_endpoint_free(vep);
  315. if (ret < 0)
  316. goto out_err;
  317. notifier->subdevs[notifier->num_subdevs] = asd;
  318. notifier->num_subdevs++;
  319. return 0;
  320. out_err:
  321. fwnode_handle_put(asd->match.fwnode.fwnode);
  322. kfree(asd);
  323. return ret == -ENOTCONN ? 0 : ret;
  324. }
  325. static int __v4l2_async_notifier_parse_fwnode_endpoints(
  326. struct device *dev, struct v4l2_async_notifier *notifier,
  327. size_t asd_struct_size, unsigned int port, bool has_port,
  328. int (*parse_endpoint)(struct device *dev,
  329. struct v4l2_fwnode_endpoint *vep,
  330. struct v4l2_async_subdev *asd))
  331. {
  332. struct fwnode_handle *fwnode;
  333. unsigned int max_subdevs = notifier->max_subdevs;
  334. int ret;
  335. if (WARN_ON(asd_struct_size < sizeof(struct v4l2_async_subdev)))
  336. return -EINVAL;
  337. for (fwnode = NULL; (fwnode = fwnode_graph_get_next_endpoint(
  338. dev_fwnode(dev), fwnode)); ) {
  339. struct fwnode_handle *dev_fwnode;
  340. bool is_available;
  341. dev_fwnode = fwnode_graph_get_port_parent(fwnode);
  342. is_available = fwnode_device_is_available(dev_fwnode);
  343. fwnode_handle_put(dev_fwnode);
  344. if (!is_available)
  345. continue;
  346. if (has_port) {
  347. struct fwnode_endpoint ep;
  348. ret = fwnode_graph_parse_endpoint(fwnode, &ep);
  349. if (ret) {
  350. fwnode_handle_put(fwnode);
  351. return ret;
  352. }
  353. if (ep.port != port)
  354. continue;
  355. }
  356. max_subdevs++;
  357. }
  358. /* No subdevs to add? Return here. */
  359. if (max_subdevs == notifier->max_subdevs)
  360. return 0;
  361. ret = v4l2_async_notifier_realloc(notifier, max_subdevs);
  362. if (ret)
  363. return ret;
  364. for (fwnode = NULL; (fwnode = fwnode_graph_get_next_endpoint(
  365. dev_fwnode(dev), fwnode)); ) {
  366. struct fwnode_handle *dev_fwnode;
  367. bool is_available;
  368. dev_fwnode = fwnode_graph_get_port_parent(fwnode);
  369. is_available = fwnode_device_is_available(dev_fwnode);
  370. fwnode_handle_put(dev_fwnode);
  371. if (!fwnode_device_is_available(dev_fwnode))
  372. continue;
  373. if (WARN_ON(notifier->num_subdevs >= notifier->max_subdevs)) {
  374. ret = -EINVAL;
  375. break;
  376. }
  377. if (has_port) {
  378. struct fwnode_endpoint ep;
  379. ret = fwnode_graph_parse_endpoint(fwnode, &ep);
  380. if (ret)
  381. break;
  382. if (ep.port != port)
  383. continue;
  384. }
  385. ret = v4l2_async_notifier_fwnode_parse_endpoint(
  386. dev, notifier, fwnode, asd_struct_size, parse_endpoint);
  387. if (ret < 0)
  388. break;
  389. }
  390. fwnode_handle_put(fwnode);
  391. return ret;
  392. }
  393. int v4l2_async_notifier_parse_fwnode_endpoints(
  394. struct device *dev, struct v4l2_async_notifier *notifier,
  395. size_t asd_struct_size,
  396. int (*parse_endpoint)(struct device *dev,
  397. struct v4l2_fwnode_endpoint *vep,
  398. struct v4l2_async_subdev *asd))
  399. {
  400. return __v4l2_async_notifier_parse_fwnode_endpoints(
  401. dev, notifier, asd_struct_size, 0, false, parse_endpoint);
  402. }
  403. EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints);
  404. int v4l2_async_notifier_parse_fwnode_endpoints_by_port(
  405. struct device *dev, struct v4l2_async_notifier *notifier,
  406. size_t asd_struct_size, unsigned int port,
  407. int (*parse_endpoint)(struct device *dev,
  408. struct v4l2_fwnode_endpoint *vep,
  409. struct v4l2_async_subdev *asd))
  410. {
  411. return __v4l2_async_notifier_parse_fwnode_endpoints(
  412. dev, notifier, asd_struct_size, port, true, parse_endpoint);
  413. }
  414. EXPORT_SYMBOL_GPL(v4l2_async_notifier_parse_fwnode_endpoints_by_port);
  415. MODULE_LICENSE("GPL");
  416. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
  417. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  418. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");