xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_dpi.c (revision 8e8e69d6)
1 /*
2  * Copyright (C) 2016 Broadcom Limited
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 /**
18  * DOC: VC4 DPI module
19  *
20  * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI
21  * signals.  On BCM2835, these can be routed out to GPIO0-27 with the
22  * ALT2 function.
23  */
24 
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_bridge.h>
27 #include <drm/drm_edid.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_panel.h>
30 #include <drm/drm_probe_helper.h>
31 #include <linux/clk.h>
32 #include <linux/component.h>
33 #include <linux/of_graph.h>
34 #include <linux/of_platform.h>
35 #include "vc4_drv.h"
36 #include "vc4_regs.h"
37 
38 #define DPI_C			0x00
39 # define DPI_OUTPUT_ENABLE_MODE		BIT(16)
40 
41 /* The order field takes the incoming 24 bit RGB from the pixel valve
42  * and shuffles the 3 channels.
43  */
44 # define DPI_ORDER_MASK			VC4_MASK(15, 14)
45 # define DPI_ORDER_SHIFT		14
46 # define DPI_ORDER_RGB			0
47 # define DPI_ORDER_BGR			1
48 # define DPI_ORDER_GRB			2
49 # define DPI_ORDER_BRG			3
50 
51 /* The format field takes the ORDER-shuffled pixel valve data and
52  * formats it onto the output lines.
53  */
54 # define DPI_FORMAT_MASK		VC4_MASK(13, 11)
55 # define DPI_FORMAT_SHIFT		11
56 /* This define is named in the hardware, but actually just outputs 0. */
57 # define DPI_FORMAT_9BIT_666_RGB	0
58 /* Outputs 00000000rrrrrggggggbbbbb */
59 # define DPI_FORMAT_16BIT_565_RGB_1	1
60 /* Outputs 000rrrrr00gggggg000bbbbb */
61 # define DPI_FORMAT_16BIT_565_RGB_2	2
62 /* Outputs 00rrrrr000gggggg00bbbbb0 */
63 # define DPI_FORMAT_16BIT_565_RGB_3	3
64 /* Outputs 000000rrrrrrggggggbbbbbb */
65 # define DPI_FORMAT_18BIT_666_RGB_1	4
66 /* Outputs 00rrrrrr00gggggg00bbbbbb */
67 # define DPI_FORMAT_18BIT_666_RGB_2	5
68 /* Outputs rrrrrrrrggggggggbbbbbbbb */
69 # define DPI_FORMAT_24BIT_888_RGB	6
70 
71 /* Reverses the polarity of the corresponding signal */
72 # define DPI_PIXEL_CLK_INVERT		BIT(10)
73 # define DPI_HSYNC_INVERT		BIT(9)
74 # define DPI_VSYNC_INVERT		BIT(8)
75 # define DPI_OUTPUT_ENABLE_INVERT	BIT(7)
76 
77 /* Outputs the signal the falling clock edge instead of rising. */
78 # define DPI_HSYNC_NEGATE		BIT(6)
79 # define DPI_VSYNC_NEGATE		BIT(5)
80 # define DPI_OUTPUT_ENABLE_NEGATE	BIT(4)
81 
82 /* Disables the signal */
83 # define DPI_HSYNC_DISABLE		BIT(3)
84 # define DPI_VSYNC_DISABLE		BIT(2)
85 # define DPI_OUTPUT_ENABLE_DISABLE	BIT(1)
86 
87 /* Power gate to the device, full reset at 0 -> 1 transition */
88 # define DPI_ENABLE			BIT(0)
89 
90 /* All other registers besides DPI_C return the ID */
91 #define DPI_ID			0x04
92 # define DPI_ID_VALUE		0x00647069
93 
94 /* General DPI hardware state. */
95 struct vc4_dpi {
96 	struct platform_device *pdev;
97 
98 	struct drm_encoder *encoder;
99 
100 	void __iomem *regs;
101 
102 	struct clk *pixel_clock;
103 	struct clk *core_clock;
104 
105 	struct debugfs_regset32 regset;
106 };
107 
108 #define DPI_READ(offset) readl(dpi->regs + (offset))
109 #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
110 
111 /* VC4 DPI encoder KMS struct */
112 struct vc4_dpi_encoder {
113 	struct vc4_encoder base;
114 	struct vc4_dpi *dpi;
115 };
116 
117 static inline struct vc4_dpi_encoder *
118 to_vc4_dpi_encoder(struct drm_encoder *encoder)
119 {
120 	return container_of(encoder, struct vc4_dpi_encoder, base.base);
121 }
122 
123 static const struct debugfs_reg32 dpi_regs[] = {
124 	VC4_REG32(DPI_C),
125 	VC4_REG32(DPI_ID),
126 };
127 
128 static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
129 	.destroy = drm_encoder_cleanup,
130 };
131 
132 static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
133 {
134 	struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
135 	struct vc4_dpi *dpi = vc4_encoder->dpi;
136 
137 	clk_disable_unprepare(dpi->pixel_clock);
138 }
139 
140 static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
141 {
142 	struct drm_device *dev = encoder->dev;
143 	struct drm_display_mode *mode = &encoder->crtc->mode;
144 	struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
145 	struct vc4_dpi *dpi = vc4_encoder->dpi;
146 	struct drm_connector_list_iter conn_iter;
147 	struct drm_connector *connector = NULL, *connector_scan;
148 	u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
149 	int ret;
150 
151 	/* Look up the connector attached to DPI so we can get the
152 	 * bus_format.  Ideally the bridge would tell us the
153 	 * bus_format we want, but it doesn't yet, so assume that it's
154 	 * uniform throughout the bridge chain.
155 	 */
156 	drm_connector_list_iter_begin(dev, &conn_iter);
157 	drm_for_each_connector_iter(connector_scan, &conn_iter) {
158 		if (connector_scan->encoder == encoder) {
159 			connector = connector_scan;
160 			break;
161 		}
162 	}
163 	drm_connector_list_iter_end(&conn_iter);
164 
165 	if (connector && connector->display_info.num_bus_formats) {
166 		u32 bus_format = connector->display_info.bus_formats[0];
167 
168 		switch (bus_format) {
169 		case MEDIA_BUS_FMT_RGB888_1X24:
170 			dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
171 					       DPI_FORMAT);
172 			break;
173 		case MEDIA_BUS_FMT_BGR888_1X24:
174 			dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
175 					       DPI_FORMAT);
176 			dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
177 			break;
178 		case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
179 			dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
180 					       DPI_FORMAT);
181 			break;
182 		case MEDIA_BUS_FMT_RGB666_1X18:
183 			dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
184 					       DPI_FORMAT);
185 			break;
186 		case MEDIA_BUS_FMT_RGB565_1X16:
187 			dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
188 					       DPI_FORMAT);
189 			break;
190 		default:
191 			DRM_ERROR("Unknown media bus format %d\n", bus_format);
192 			break;
193 		}
194 	} else {
195 		/* Default to 24bit if no connector found. */
196 		dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, DPI_FORMAT);
197 	}
198 
199 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
200 		dpi_c |= DPI_HSYNC_INVERT;
201 	else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
202 		dpi_c |= DPI_HSYNC_DISABLE;
203 
204 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
205 		dpi_c |= DPI_VSYNC_INVERT;
206 	else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
207 		dpi_c |= DPI_VSYNC_DISABLE;
208 
209 	DPI_WRITE(DPI_C, dpi_c);
210 
211 	ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
212 	if (ret)
213 		DRM_ERROR("Failed to set clock rate: %d\n", ret);
214 
215 	ret = clk_prepare_enable(dpi->pixel_clock);
216 	if (ret)
217 		DRM_ERROR("Failed to set clock rate: %d\n", ret);
218 }
219 
220 static enum drm_mode_status vc4_dpi_encoder_mode_valid(struct drm_encoder *encoder,
221 						       const struct drm_display_mode *mode)
222 {
223 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
224 		return MODE_NO_INTERLACE;
225 
226 	return MODE_OK;
227 }
228 
229 static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
230 	.disable = vc4_dpi_encoder_disable,
231 	.enable = vc4_dpi_encoder_enable,
232 	.mode_valid = vc4_dpi_encoder_mode_valid,
233 };
234 
235 static const struct of_device_id vc4_dpi_dt_match[] = {
236 	{ .compatible = "brcm,bcm2835-dpi", .data = NULL },
237 	{}
238 };
239 
240 /* Sets up the next link in the display chain, whether it's a panel or
241  * a bridge.
242  */
243 static int vc4_dpi_init_bridge(struct vc4_dpi *dpi)
244 {
245 	struct device *dev = &dpi->pdev->dev;
246 	struct drm_panel *panel;
247 	struct drm_bridge *bridge;
248 	int ret;
249 
250 	ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0,
251 					  &panel, &bridge);
252 	if (ret) {
253 		/* If nothing was connected in the DT, that's not an
254 		 * error.
255 		 */
256 		if (ret == -ENODEV)
257 			return 0;
258 		else
259 			return ret;
260 	}
261 
262 	if (panel)
263 		bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DPI);
264 
265 	return drm_bridge_attach(dpi->encoder, bridge, NULL);
266 }
267 
268 static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
269 {
270 	struct platform_device *pdev = to_platform_device(dev);
271 	struct drm_device *drm = dev_get_drvdata(master);
272 	struct vc4_dev *vc4 = to_vc4_dev(drm);
273 	struct vc4_dpi *dpi;
274 	struct vc4_dpi_encoder *vc4_dpi_encoder;
275 	int ret;
276 
277 	dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
278 	if (!dpi)
279 		return -ENOMEM;
280 
281 	vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
282 				       GFP_KERNEL);
283 	if (!vc4_dpi_encoder)
284 		return -ENOMEM;
285 	vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
286 	vc4_dpi_encoder->dpi = dpi;
287 	dpi->encoder = &vc4_dpi_encoder->base.base;
288 
289 	dpi->pdev = pdev;
290 	dpi->regs = vc4_ioremap_regs(pdev, 0);
291 	if (IS_ERR(dpi->regs))
292 		return PTR_ERR(dpi->regs);
293 	dpi->regset.base = dpi->regs;
294 	dpi->regset.regs = dpi_regs;
295 	dpi->regset.nregs = ARRAY_SIZE(dpi_regs);
296 
297 	if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
298 		dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
299 			DPI_READ(DPI_ID), DPI_ID_VALUE);
300 		return -ENODEV;
301 	}
302 
303 	dpi->core_clock = devm_clk_get(dev, "core");
304 	if (IS_ERR(dpi->core_clock)) {
305 		ret = PTR_ERR(dpi->core_clock);
306 		if (ret != -EPROBE_DEFER)
307 			DRM_ERROR("Failed to get core clock: %d\n", ret);
308 		return ret;
309 	}
310 	dpi->pixel_clock = devm_clk_get(dev, "pixel");
311 	if (IS_ERR(dpi->pixel_clock)) {
312 		ret = PTR_ERR(dpi->pixel_clock);
313 		if (ret != -EPROBE_DEFER)
314 			DRM_ERROR("Failed to get pixel clock: %d\n", ret);
315 		return ret;
316 	}
317 
318 	ret = clk_prepare_enable(dpi->core_clock);
319 	if (ret)
320 		DRM_ERROR("Failed to turn on core clock: %d\n", ret);
321 
322 	drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
323 			 DRM_MODE_ENCODER_DPI, NULL);
324 	drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
325 
326 	ret = vc4_dpi_init_bridge(dpi);
327 	if (ret)
328 		goto err_destroy_encoder;
329 
330 	dev_set_drvdata(dev, dpi);
331 
332 	vc4->dpi = dpi;
333 
334 	vc4_debugfs_add_regset32(drm, "dpi_regs", &dpi->regset);
335 
336 	return 0;
337 
338 err_destroy_encoder:
339 	drm_encoder_cleanup(dpi->encoder);
340 	clk_disable_unprepare(dpi->core_clock);
341 	return ret;
342 }
343 
344 static void vc4_dpi_unbind(struct device *dev, struct device *master,
345 			   void *data)
346 {
347 	struct drm_device *drm = dev_get_drvdata(master);
348 	struct vc4_dev *vc4 = to_vc4_dev(drm);
349 	struct vc4_dpi *dpi = dev_get_drvdata(dev);
350 
351 	drm_of_panel_bridge_remove(dev->of_node, 0, 0);
352 
353 	drm_encoder_cleanup(dpi->encoder);
354 
355 	clk_disable_unprepare(dpi->core_clock);
356 
357 	vc4->dpi = NULL;
358 }
359 
360 static const struct component_ops vc4_dpi_ops = {
361 	.bind   = vc4_dpi_bind,
362 	.unbind = vc4_dpi_unbind,
363 };
364 
365 static int vc4_dpi_dev_probe(struct platform_device *pdev)
366 {
367 	return component_add(&pdev->dev, &vc4_dpi_ops);
368 }
369 
370 static int vc4_dpi_dev_remove(struct platform_device *pdev)
371 {
372 	component_del(&pdev->dev, &vc4_dpi_ops);
373 	return 0;
374 }
375 
376 struct platform_driver vc4_dpi_driver = {
377 	.probe = vc4_dpi_dev_probe,
378 	.remove = vc4_dpi_dev_remove,
379 	.driver = {
380 		.name = "vc4_dpi",
381 		.of_match_table = vc4_dpi_dt_match,
382 	},
383 };
384