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