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