xref: /linux/drivers/gpu/drm/tilcdc/tilcdc_external.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Texas Instruments
4  * Author: Jyri Sarha <jsarha@ti.com>
5  */
6 
7 #include <linux/component.h>
8 #include <linux/of_graph.h>
9 
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/drm_of.h>
13 #include <drm/drm_simple_kms_helper.h>
14 
15 #include "tilcdc_drv.h"
16 #include "tilcdc_external.h"
17 
18 static const struct tilcdc_panel_info panel_info_tda998x = {
19 		.ac_bias                = 255,
20 		.ac_bias_intrpt         = 0,
21 		.dma_burst_sz           = 16,
22 		.bpp                    = 16,
23 		.fdd                    = 0x80,
24 		.tft_alt_mode           = 0,
25 		.invert_pxl_clk		= 1,
26 		.sync_edge              = 1,
27 		.sync_ctrl              = 1,
28 		.raster_order           = 0,
29 };
30 
31 static const struct tilcdc_panel_info panel_info_default = {
32 		.ac_bias                = 255,
33 		.ac_bias_intrpt         = 0,
34 		.dma_burst_sz           = 16,
35 		.bpp                    = 16,
36 		.fdd                    = 0x80,
37 		.tft_alt_mode           = 0,
38 		.sync_edge              = 0,
39 		.sync_ctrl              = 1,
40 		.raster_order           = 0,
41 };
42 
43 static
44 struct drm_connector *tilcdc_encoder_find_connector(struct drm_device *ddev,
45 						    struct drm_encoder *encoder)
46 {
47 	struct drm_connector *connector;
48 
49 	list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
50 		if (drm_connector_has_possible_encoder(connector, encoder))
51 			return connector;
52 	}
53 
54 	dev_err(ddev->dev, "No connector found for %s encoder (id %d)\n",
55 		encoder->name, encoder->base.id);
56 
57 	return NULL;
58 }
59 
60 int tilcdc_add_component_encoder(struct drm_device *ddev)
61 {
62 	struct tilcdc_drm_private *priv = ddev->dev_private;
63 	struct drm_encoder *encoder = NULL, *iter;
64 
65 	list_for_each_entry(iter, &ddev->mode_config.encoder_list, head)
66 		if (iter->possible_crtcs & (1 << priv->crtc->index)) {
67 			encoder = iter;
68 			break;
69 		}
70 
71 	if (!encoder) {
72 		dev_err(ddev->dev, "%s: No suitable encoder found\n", __func__);
73 		return -ENODEV;
74 	}
75 
76 	priv->external_connector =
77 		tilcdc_encoder_find_connector(ddev, encoder);
78 
79 	if (!priv->external_connector)
80 		return -ENODEV;
81 
82 	/* Only tda998x is supported at the moment. */
83 	tilcdc_crtc_set_simulate_vesa_sync(priv->crtc, true);
84 	tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_tda998x);
85 
86 	return 0;
87 }
88 
89 static
90 int tilcdc_attach_bridge(struct drm_device *ddev, struct drm_bridge *bridge)
91 {
92 	struct tilcdc_drm_private *priv = ddev->dev_private;
93 	int ret;
94 
95 	priv->external_encoder->possible_crtcs = BIT(0);
96 
97 	ret = drm_bridge_attach(priv->external_encoder, bridge, NULL, 0);
98 	if (ret)
99 		return ret;
100 
101 	tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_default);
102 
103 	priv->external_connector =
104 		tilcdc_encoder_find_connector(ddev, priv->external_encoder);
105 	if (!priv->external_connector)
106 		return -ENODEV;
107 
108 	return 0;
109 }
110 
111 int tilcdc_attach_external_device(struct drm_device *ddev)
112 {
113 	struct tilcdc_drm_private *priv = ddev->dev_private;
114 	struct drm_bridge *bridge;
115 	struct drm_panel *panel;
116 	int ret;
117 
118 	ret = drm_of_find_panel_or_bridge(ddev->dev->of_node, 0, 0,
119 					  &panel, &bridge);
120 	if (ret == -ENODEV)
121 		return 0;
122 	else if (ret)
123 		return ret;
124 
125 	priv->external_encoder = devm_kzalloc(ddev->dev,
126 					      sizeof(*priv->external_encoder),
127 					      GFP_KERNEL);
128 	if (!priv->external_encoder)
129 		return -ENOMEM;
130 
131 	ret = drm_simple_encoder_init(ddev, priv->external_encoder,
132 				      DRM_MODE_ENCODER_NONE);
133 	if (ret) {
134 		dev_err(ddev->dev, "drm_encoder_init() failed %d\n", ret);
135 		return ret;
136 	}
137 
138 	if (panel) {
139 		bridge = devm_drm_panel_bridge_add_typed(ddev->dev, panel,
140 							 DRM_MODE_CONNECTOR_DPI);
141 		if (IS_ERR(bridge)) {
142 			ret = PTR_ERR(bridge);
143 			goto err_encoder_cleanup;
144 		}
145 	}
146 
147 	ret = tilcdc_attach_bridge(ddev, bridge);
148 	if (ret)
149 		goto err_encoder_cleanup;
150 
151 	return 0;
152 
153 err_encoder_cleanup:
154 	drm_encoder_cleanup(priv->external_encoder);
155 	return ret;
156 }
157 
158 static int dev_match_of(struct device *dev, void *data)
159 {
160 	return dev->of_node == data;
161 }
162 
163 int tilcdc_get_external_components(struct device *dev,
164 				   struct component_match **match)
165 {
166 	struct device_node *node;
167 
168 	node = of_graph_get_remote_node(dev->of_node, 0, 0);
169 
170 	if (!of_device_is_compatible(node, "nxp,tda998x")) {
171 		of_node_put(node);
172 		return 0;
173 	}
174 
175 	if (match)
176 		drm_of_component_match_add(dev, match, dev_match_of, node);
177 	of_node_put(node);
178 	return 1;
179 }
180