drm_of.c 17 KB

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