Linux Audio

Check our new training course

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