drm_of.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/component.h>
  3. #include <linux/export.h>
  4. #include <linux/list.h>
  5. #include <linux/media-bus-format.h>
  6. #include <linux/of.h>
  7. #include <linux/of_graph.h>
  8. #include <drm/drm_bridge.h>
  9. #include <drm/drm_crtc.h>
  10. #include <drm/drm_device.h>
  11. #include <drm/drm_encoder.h>
  12. #include <drm/drm_of.h>
  13. #include <drm/drm_panel.h>
  14. /**
  15. * DOC: overview
  16. *
  17. * A set of helper functions to aid DRM drivers in parsing standard DT
  18. * properties.
  19. */
  20. /**
  21. * drm_of_crtc_port_mask - find the mask of a registered CRTC by port OF node
  22. * @dev: DRM device
  23. * @port: port OF node
  24. *
  25. * Given a port OF node, return the possible mask of the corresponding
  26. * CRTC within a device's list of CRTCs. Returns zero if not found.
  27. */
  28. uint32_t drm_of_crtc_port_mask(struct drm_device *dev,
  29. struct device_node *port)
  30. {
  31. unsigned int index = 0;
  32. struct drm_crtc *tmp;
  33. drm_for_each_crtc(tmp, dev) {
  34. if (tmp->port == port)
  35. return 1 << index;
  36. index++;
  37. }
  38. return 0;
  39. }
  40. EXPORT_SYMBOL(drm_of_crtc_port_mask);
  41. /**
  42. * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
  43. * @dev: DRM device
  44. * @port: encoder port to scan for endpoints
  45. *
  46. * Scan all endpoints attached to a port, locate their attached CRTCs,
  47. * and generate the DRM mask of CRTCs which may be attached to this
  48. * encoder.
  49. *
  50. * See Documentation/devicetree/bindings/graph.txt for the bindings.
  51. */
  52. uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
  53. struct device_node *port)
  54. {
  55. struct device_node *remote_port, *ep;
  56. uint32_t possible_crtcs = 0;
  57. for_each_endpoint_of_node(port, ep) {
  58. remote_port = of_graph_get_remote_port(ep);
  59. if (!remote_port) {
  60. of_node_put(ep);
  61. return 0;
  62. }
  63. possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
  64. of_node_put(remote_port);
  65. }
  66. return possible_crtcs;
  67. }
  68. EXPORT_SYMBOL(drm_of_find_possible_crtcs);
  69. /**
  70. * drm_of_component_match_add - Add a component helper OF node match rule
  71. * @master: master device
  72. * @matchptr: component match pointer
  73. * @compare: compare function used for matching component
  74. * @node: of_node
  75. */
  76. void drm_of_component_match_add(struct device *master,
  77. struct component_match **matchptr,
  78. int (*compare)(struct device *, void *),
  79. struct device_node *node)
  80. {
  81. of_node_get(node);
  82. component_match_add_release(master, matchptr, component_release_of,
  83. compare, node);
  84. }
  85. EXPORT_SYMBOL_GPL(drm_of_component_match_add);
  86. /**
  87. * drm_of_component_probe - Generic probe function for a component based master
  88. * @dev: master device containing the OF node
  89. * @compare_of: compare function used for matching components
  90. * @m_ops: component master ops to be used
  91. *
  92. * Parse the platform device OF node and bind all the components associated
  93. * with the master. Interface ports are added before the encoders in order to
  94. * satisfy their .bind requirements
  95. * See Documentation/devicetree/bindings/graph.txt for the bindings.
  96. *
  97. * Returns zero if successful, or one of the standard error codes if it fails.
  98. */
  99. int drm_of_component_probe(struct device *dev,
  100. int (*compare_of)(struct device *, void *),
  101. const struct component_master_ops *m_ops)
  102. {
  103. struct device_node *ep, *port, *remote;
  104. struct component_match *match = NULL;
  105. int i;
  106. if (!dev->of_node)
  107. return -EINVAL;
  108. /*
  109. * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
  110. * called from encoder's .bind callbacks works as expected
  111. */
  112. for (i = 0; ; i++) {
  113. port = of_parse_phandle(dev->of_node, "ports", i);
  114. if (!port)
  115. break;
  116. if (of_device_is_available(port->parent))
  117. drm_of_component_match_add(dev, &match, compare_of,
  118. port);
  119. of_node_put(port);
  120. }
  121. if (i == 0) {
  122. dev_err(dev, "missing 'ports' property\n");
  123. return -ENODEV;
  124. }
  125. if (!match) {
  126. dev_err(dev, "no available port\n");
  127. return -ENODEV;
  128. }
  129. /*
  130. * For bound crtcs, bind the encoders attached to their remote endpoint
  131. */
  132. for (i = 0; ; i++) {
  133. port = of_parse_phandle(dev->of_node, "ports", i);
  134. if (!port)
  135. break;
  136. if (!of_device_is_available(port->parent)) {
  137. of_node_put(port);
  138. continue;
  139. }
  140. for_each_child_of_node(port, ep) {
  141. remote = of_graph_get_remote_port_parent(ep);
  142. if (!remote || !of_device_is_available(remote)) {
  143. of_node_put(remote);
  144. continue;
  145. } else if (!of_device_is_available(remote->parent)) {
  146. dev_warn(dev, "parent device of %pOF is not available\n",
  147. remote);
  148. of_node_put(remote);
  149. continue;
  150. }
  151. drm_of_component_match_add(dev, &match, compare_of,
  152. remote);
  153. of_node_put(remote);
  154. }
  155. of_node_put(port);
  156. }
  157. return component_master_add_with_match(dev, m_ops, match);
  158. }
  159. EXPORT_SYMBOL(drm_of_component_probe);
  160. /*
  161. * drm_of_encoder_active_endpoint - return the active encoder endpoint
  162. * @node: device tree node containing encoder input ports
  163. * @encoder: drm_encoder
  164. *
  165. * Given an encoder device node and a drm_encoder with a connected crtc,
  166. * parse the encoder endpoint connecting to the crtc port.
  167. */
  168. int drm_of_encoder_active_endpoint(struct device_node *node,
  169. struct drm_encoder *encoder,
  170. struct of_endpoint *endpoint)
  171. {
  172. struct device_node *ep;
  173. struct drm_crtc *crtc = encoder->crtc;
  174. struct device_node *port;
  175. int ret;
  176. if (!node || !crtc)
  177. return -EINVAL;
  178. for_each_endpoint_of_node(node, ep) {
  179. port = of_graph_get_remote_port(ep);
  180. of_node_put(port);
  181. if (port == crtc->port) {
  182. ret = of_graph_parse_endpoint(ep, endpoint);
  183. of_node_put(ep);
  184. return ret;
  185. }
  186. }
  187. return -EINVAL;
  188. }
  189. EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);
  190. /**
  191. * drm_of_find_panel_or_bridge - return connected panel or bridge device
  192. * @np: device tree node containing encoder output ports
  193. * @port: port in the device tree node
  194. * @endpoint: endpoint in the device tree node
  195. * @panel: pointer to hold returned drm_panel
  196. * @bridge: pointer to hold returned drm_bridge
  197. *
  198. * Given a DT node's port and endpoint number, find the connected node and
  199. * return either the associated struct drm_panel or drm_bridge device. Either
  200. * @panel or @bridge must not be NULL.
  201. *
  202. * This function is deprecated and should not be used in new drivers. Use
  203. * devm_drm_of_get_bridge() instead.
  204. *
  205. * Returns zero if successful, or one of the standard error codes if it fails.
  206. */
  207. int drm_of_find_panel_or_bridge(const struct device_node *np,
  208. int port, int endpoint,
  209. struct drm_panel **panel,
  210. struct drm_bridge **bridge)
  211. {
  212. int ret = -EPROBE_DEFER;
  213. struct device_node *remote;
  214. if (!panel && !bridge)
  215. return -EINVAL;
  216. if (panel)
  217. *panel = NULL;
  218. /*
  219. * of_graph_get_remote_node() produces a noisy error message if port
  220. * node isn't found and the absence of the port is a legit case here,
  221. * so at first we silently check whether graph presents in the
  222. * device-tree node.
  223. */
  224. if (!of_graph_is_present(np))
  225. return -ENODEV;
  226. remote = of_graph_get_remote_node(np, port, endpoint);
  227. if (!remote)
  228. return -ENODEV;
  229. if (panel) {
  230. *panel = of_drm_find_panel(remote);
  231. if (!IS_ERR(*panel))
  232. ret = 0;
  233. else
  234. *panel = NULL;
  235. }
  236. /* No panel found yet, check for a bridge next. */
  237. if (bridge) {
  238. if (ret) {
  239. *bridge = of_drm_find_bridge(remote);
  240. if (*bridge)
  241. ret = 0;
  242. } else {
  243. *bridge = NULL;
  244. }
  245. }
  246. of_node_put(remote);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
  250. enum drm_of_lvds_pixels {
  251. DRM_OF_LVDS_EVEN = BIT(0),
  252. DRM_OF_LVDS_ODD = BIT(1),
  253. };
  254. static int drm_of_lvds_get_port_pixels_type(struct device_node *port_node)
  255. {
  256. bool even_pixels =
  257. of_property_read_bool(port_node, "dual-lvds-even-pixels");
  258. bool odd_pixels =
  259. of_property_read_bool(port_node, "dual-lvds-odd-pixels");
  260. return (even_pixels ? DRM_OF_LVDS_EVEN : 0) |
  261. (odd_pixels ? DRM_OF_LVDS_ODD : 0);
  262. }
  263. static int drm_of_lvds_get_remote_pixels_type(
  264. const struct device_node *port_node)
  265. {
  266. struct device_node *endpoint = NULL;
  267. int pixels_type = -EPIPE;
  268. for_each_child_of_node(port_node, endpoint) {
  269. struct device_node *remote_port;
  270. int current_pt;
  271. if (!of_node_name_eq(endpoint, "endpoint"))
  272. continue;
  273. remote_port = of_graph_get_remote_port(endpoint);
  274. if (!remote_port) {
  275. of_node_put(endpoint);
  276. return -EPIPE;
  277. }
  278. current_pt = drm_of_lvds_get_port_pixels_type(remote_port);
  279. of_node_put(remote_port);
  280. if (pixels_type < 0)
  281. pixels_type = current_pt;
  282. /*
  283. * Sanity check, ensure that all remote endpoints have the same
  284. * pixel type. We may lift this restriction later if we need to
  285. * support multiple sinks with different dual-link
  286. * configurations by passing the endpoints explicitly to
  287. * drm_of_lvds_get_dual_link_pixel_order().
  288. */
  289. if (!current_pt || pixels_type != current_pt) {
  290. of_node_put(endpoint);
  291. return -EINVAL;
  292. }
  293. }
  294. return pixels_type;
  295. }
  296. /**
  297. * drm_of_lvds_get_dual_link_pixel_order - Get LVDS dual-link pixel order
  298. * @port1: First DT port node of the Dual-link LVDS source
  299. * @port2: Second DT port node of the Dual-link LVDS source
  300. *
  301. * An LVDS dual-link connection is made of two links, with even pixels
  302. * transitting on one link, and odd pixels on the other link. This function
  303. * returns, for two ports of an LVDS dual-link source, which port shall transmit
  304. * the even and odd pixels, based on the requirements of the connected sink.
  305. *
  306. * The pixel order is determined from the dual-lvds-even-pixels and
  307. * dual-lvds-odd-pixels properties in the sink's DT port nodes. If those
  308. * properties are not present, or if their usage is not valid, this function
  309. * returns -EINVAL.
  310. *
  311. * If either port is not connected, this function returns -EPIPE.
  312. *
  313. * @port1 and @port2 are typically DT sibling nodes, but may have different
  314. * parents when, for instance, two separate LVDS encoders carry the even and odd
  315. * pixels.
  316. *
  317. * Return:
  318. * * DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS - @port1 carries even pixels and @port2
  319. * carries odd pixels
  320. * * DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS - @port1 carries odd pixels and @port2
  321. * carries even pixels
  322. * * -EINVAL - @port1 and @port2 are not connected to a dual-link LVDS sink, or
  323. * the sink configuration is invalid
  324. * * -EPIPE - when @port1 or @port2 are not connected
  325. */
  326. int drm_of_lvds_get_dual_link_pixel_order(const struct device_node *port1,
  327. const struct device_node *port2)
  328. {
  329. int remote_p1_pt, remote_p2_pt;
  330. if (!port1 || !port2)
  331. return -EINVAL;
  332. remote_p1_pt = drm_of_lvds_get_remote_pixels_type(port1);
  333. if (remote_p1_pt < 0)
  334. return remote_p1_pt;
  335. remote_p2_pt = drm_of_lvds_get_remote_pixels_type(port2);
  336. if (remote_p2_pt < 0)
  337. return remote_p2_pt;
  338. /*
  339. * A valid dual-lVDS bus is found when one remote port is marked with
  340. * "dual-lvds-even-pixels", and the other remote port is marked with
  341. * "dual-lvds-odd-pixels", bail out if the markers are not right.
  342. */
  343. if (remote_p1_pt + remote_p2_pt != DRM_OF_LVDS_EVEN + DRM_OF_LVDS_ODD)
  344. return -EINVAL;
  345. return remote_p1_pt == DRM_OF_LVDS_EVEN ?
  346. DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS :
  347. DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS;
  348. }
  349. EXPORT_SYMBOL_GPL(drm_of_lvds_get_dual_link_pixel_order);
  350. /**
  351. * drm_of_lvds_get_data_mapping - Get LVDS data mapping
  352. * @port: DT port node of the LVDS source or sink
  353. *
  354. * Convert DT "data-mapping" property string value into media bus format value.
  355. *
  356. * Return:
  357. * * MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - data-mapping is "jeida-18"
  358. * * MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA - data-mapping is "jeida-24"
  359. * * MEDIA_BUS_FMT_RGB888_1X7X4_SPWG - data-mapping is "vesa-24"
  360. * * -EINVAL - the "data-mapping" property is unsupported
  361. * * -ENODEV - the "data-mapping" property is missing
  362. */
  363. int drm_of_lvds_get_data_mapping(const struct device_node *port)
  364. {
  365. const char *mapping;
  366. int ret;
  367. ret = of_property_read_string(port, "data-mapping", &mapping);
  368. if (ret < 0)
  369. return -ENODEV;
  370. if (!strcmp(mapping, "jeida-18"))
  371. return MEDIA_BUS_FMT_RGB666_1X7X3_SPWG;
  372. if (!strcmp(mapping, "jeida-24"))
  373. return MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA;
  374. if (!strcmp(mapping, "vesa-24"))
  375. return MEDIA_BUS_FMT_RGB888_1X7X4_SPWG;
  376. return -EINVAL;
  377. }
  378. EXPORT_SYMBOL_GPL(drm_of_lvds_get_data_mapping);
  379. /**
  380. * drm_of_get_data_lanes_count - Get DSI/(e)DP data lane count
  381. * @endpoint: DT endpoint node of the DSI/(e)DP source or sink
  382. * @min: minimum supported number of data lanes
  383. * @max: maximum supported number of data lanes
  384. *
  385. * Count DT "data-lanes" property elements and check for validity.
  386. *
  387. * Return:
  388. * * min..max - positive integer count of "data-lanes" elements
  389. * * -ve - the "data-lanes" property is missing or invalid
  390. * * -EINVAL - the "data-lanes" property is unsupported
  391. */
  392. int drm_of_get_data_lanes_count(const struct device_node *endpoint,
  393. const unsigned int min, const unsigned int max)
  394. {
  395. int ret;
  396. ret = of_property_count_u32_elems(endpoint, "data-lanes");
  397. if (ret < 0)
  398. return ret;
  399. if (ret < min || ret > max)
  400. return -EINVAL;
  401. return ret;
  402. }
  403. EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count);
  404. /**
  405. * drm_of_get_data_lanes_count_ep - Get DSI/(e)DP data lane count by endpoint
  406. * @port: DT port node of the DSI/(e)DP source or sink
  407. * @port_reg: identifier (value of reg property) of the parent port node
  408. * @reg: identifier (value of reg property) of the endpoint node
  409. * @min: minimum supported number of data lanes
  410. * @max: maximum supported number of data lanes
  411. *
  412. * Count DT "data-lanes" property elements and check for validity.
  413. * This variant uses endpoint specifier.
  414. *
  415. * Return:
  416. * * min..max - positive integer count of "data-lanes" elements
  417. * * -EINVAL - the "data-mapping" property is unsupported
  418. * * -ENODEV - the "data-mapping" property is missing
  419. */
  420. int drm_of_get_data_lanes_count_ep(const struct device_node *port,
  421. int port_reg, int reg,
  422. const unsigned int min,
  423. const unsigned int max)
  424. {
  425. struct device_node *endpoint;
  426. int ret;
  427. endpoint = of_graph_get_endpoint_by_regs(port, port_reg, reg);
  428. ret = drm_of_get_data_lanes_count(endpoint, min, max);
  429. of_node_put(endpoint);
  430. return ret;
  431. }
  432. EXPORT_SYMBOL_GPL(drm_of_get_data_lanes_count_ep);