drm_of.c 15 KB

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