xref: /openbmc/linux/drivers/gpu/drm/mcde/mcde_drv.c (revision 14474950)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 Linus Walleij <linus.walleij@linaro.org>
4  * Parts of this file were based on the MCDE driver by Marcus Lorentzon
5  * (C) ST-Ericsson SA 2013
6  */
7 
8 /**
9  * DOC: ST-Ericsson MCDE Driver
10  *
11  * The MCDE (short for multi-channel display engine) is a graphics
12  * controller found in the Ux500 chipsets, such as NovaThor U8500.
13  * It was initially conceptualized by ST Microelectronics for the
14  * successor of the Nomadik line, STn8500 but productified in the
15  * ST-Ericsson U8500 where is was used for mass-market deployments
16  * in Android phones from Samsung and Sony Ericsson.
17  *
18  * It can do 1080p30 on SDTV CCIR656, DPI-2, DBI-2 or DSI for
19  * panels with or without frame buffering and can convert most
20  * input formats including most variants of RGB and YUV.
21  *
22  * The hardware has four display pipes, and the layout is a little
23  * bit like this::
24  *
25  *   Memory     -> Overlay -> Channel -> FIFO -> 5 formatters -> DSI/DPI
26  *   External      0..5       0..3       A,B,    3 x DSI         bridge
27  *   source 0..9                         C0,C1   2 x DPI
28  *
29  * FIFOs A and B are for LCD and HDMI while FIFO CO/C1 are for
30  * panels with embedded buffer.
31  * 3 of the formatters are for DSI.
32  * 2 of the formatters are for DPI.
33  *
34  * Behind the formatters are the DSI or DPI ports that route to
35  * the external pins of the chip. As there are 3 DSI ports and one
36  * DPI port, it is possible to configure up to 4 display pipelines
37  * (effectively using channels 0..3) for concurrent use.
38  *
39  * In the current DRM/KMS setup, we use one external source, one overlay,
40  * one FIFO and one formatter which we connect to the simple CMA framebuffer
41  * helpers. We then provide a bridge to the DSI port, and on the DSI port
42  * bridge we connect hang a panel bridge or other bridge. This may be subject
43  * to change as we exploit more of the hardware capabilities.
44  *
45  * TODO:
46  *
47  * - Enabled damaged rectangles using drm_plane_enable_fb_damage_clips()
48  *   so we can selectively just transmit the damaged area to a
49  *   command-only display.
50  * - Enable mixing of more planes, possibly at the cost of moving away
51  *   from using the simple framebuffer pipeline.
52  * - Enable output to bridges such as the AV8100 HDMI encoder from
53  *   the DSI bridge.
54  */
55 
56 #include <linux/clk.h>
57 #include <linux/component.h>
58 #include <linux/dma-buf.h>
59 #include <linux/irq.h>
60 #include <linux/io.h>
61 #include <linux/module.h>
62 #include <linux/of_platform.h>
63 #include <linux/platform_device.h>
64 #include <linux/regulator/consumer.h>
65 #include <linux/slab.h>
66 
67 #include <drm/drm_atomic_helper.h>
68 #include <drm/drm_bridge.h>
69 #include <drm/drm_drv.h>
70 #include <drm/drm_fb_cma_helper.h>
71 #include <drm/drm_fb_helper.h>
72 #include <drm/drm_gem.h>
73 #include <drm/drm_gem_cma_helper.h>
74 #include <drm/drm_gem_framebuffer_helper.h>
75 #include <drm/drm_managed.h>
76 #include <drm/drm_of.h>
77 #include <drm/drm_probe_helper.h>
78 #include <drm/drm_panel.h>
79 #include <drm/drm_vblank.h>
80 
81 #include "mcde_drm.h"
82 
83 #define DRIVER_DESC	"DRM module for MCDE"
84 
85 #define MCDE_CR 0x00000000
86 #define MCDE_CR_IFIFOEMPTYLINECOUNT_V422_SHIFT 0
87 #define MCDE_CR_IFIFOEMPTYLINECOUNT_V422_MASK 0x0000003F
88 #define MCDE_CR_IFIFOCTRLEN BIT(15)
89 #define MCDE_CR_UFRECOVERY_MODE_V422 BIT(16)
90 #define MCDE_CR_WRAP_MODE_V422_SHIFT BIT(17)
91 #define MCDE_CR_AUTOCLKG_EN BIT(30)
92 #define MCDE_CR_MCDEEN BIT(31)
93 
94 #define MCDE_CONF0 0x00000004
95 #define MCDE_CONF0_SYNCMUX0 BIT(0)
96 #define MCDE_CONF0_SYNCMUX1 BIT(1)
97 #define MCDE_CONF0_SYNCMUX2 BIT(2)
98 #define MCDE_CONF0_SYNCMUX3 BIT(3)
99 #define MCDE_CONF0_SYNCMUX4 BIT(4)
100 #define MCDE_CONF0_SYNCMUX5 BIT(5)
101 #define MCDE_CONF0_SYNCMUX6 BIT(6)
102 #define MCDE_CONF0_SYNCMUX7 BIT(7)
103 #define MCDE_CONF0_IFIFOCTRLWTRMRKLVL_SHIFT 12
104 #define MCDE_CONF0_IFIFOCTRLWTRMRKLVL_MASK 0x00007000
105 #define MCDE_CONF0_OUTMUX0_SHIFT 16
106 #define MCDE_CONF0_OUTMUX0_MASK 0x00070000
107 #define MCDE_CONF0_OUTMUX1_SHIFT 19
108 #define MCDE_CONF0_OUTMUX1_MASK 0x00380000
109 #define MCDE_CONF0_OUTMUX2_SHIFT 22
110 #define MCDE_CONF0_OUTMUX2_MASK 0x01C00000
111 #define MCDE_CONF0_OUTMUX3_SHIFT 25
112 #define MCDE_CONF0_OUTMUX3_MASK 0x0E000000
113 #define MCDE_CONF0_OUTMUX4_SHIFT 28
114 #define MCDE_CONF0_OUTMUX4_MASK 0x70000000
115 
116 #define MCDE_SSP 0x00000008
117 #define MCDE_AIS 0x00000100
118 #define MCDE_IMSCERR 0x00000110
119 #define MCDE_RISERR 0x00000120
120 #define MCDE_MISERR 0x00000130
121 #define MCDE_SISERR 0x00000140
122 
123 #define MCDE_PID 0x000001FC
124 #define MCDE_PID_METALFIX_VERSION_SHIFT 0
125 #define MCDE_PID_METALFIX_VERSION_MASK 0x000000FF
126 #define MCDE_PID_DEVELOPMENT_VERSION_SHIFT 8
127 #define MCDE_PID_DEVELOPMENT_VERSION_MASK 0x0000FF00
128 #define MCDE_PID_MINOR_VERSION_SHIFT 16
129 #define MCDE_PID_MINOR_VERSION_MASK 0x00FF0000
130 #define MCDE_PID_MAJOR_VERSION_SHIFT 24
131 #define MCDE_PID_MAJOR_VERSION_MASK 0xFF000000
132 
133 static const struct drm_mode_config_funcs mcde_mode_config_funcs = {
134 	.fb_create = drm_gem_fb_create_with_dirty,
135 	.atomic_check = drm_atomic_helper_check,
136 	.atomic_commit = drm_atomic_helper_commit,
137 };
138 
139 static const struct drm_mode_config_helper_funcs mcde_mode_config_helpers = {
140 	/*
141 	 * Using this function is necessary to commit atomic updates
142 	 * that need the CRTC to be enabled before a commit, as is
143 	 * the case with e.g. DSI displays.
144 	 */
145 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
146 };
147 
148 static irqreturn_t mcde_irq(int irq, void *data)
149 {
150 	struct mcde *mcde = data;
151 	u32 val;
152 
153 	val = readl(mcde->regs + MCDE_MISERR);
154 
155 	mcde_display_irq(mcde);
156 
157 	if (val)
158 		dev_info(mcde->dev, "some error IRQ\n");
159 	writel(val, mcde->regs + MCDE_RISERR);
160 
161 	return IRQ_HANDLED;
162 }
163 
164 static int mcde_modeset_init(struct drm_device *drm)
165 {
166 	struct drm_mode_config *mode_config;
167 	struct mcde *mcde = to_mcde(drm);
168 	int ret;
169 
170 	if (!mcde->bridge) {
171 		dev_err(drm->dev, "no display output bridge yet\n");
172 		return -EPROBE_DEFER;
173 	}
174 
175 	mode_config = &drm->mode_config;
176 	mode_config->funcs = &mcde_mode_config_funcs;
177 	mode_config->helper_private = &mcde_mode_config_helpers;
178 	/* This hardware can do 1080p */
179 	mode_config->min_width = 1;
180 	mode_config->max_width = 1920;
181 	mode_config->min_height = 1;
182 	mode_config->max_height = 1080;
183 
184 	ret = drm_vblank_init(drm, 1);
185 	if (ret) {
186 		dev_err(drm->dev, "failed to init vblank\n");
187 		return ret;
188 	}
189 
190 	ret = mcde_display_init(drm);
191 	if (ret) {
192 		dev_err(drm->dev, "failed to init display\n");
193 		return ret;
194 	}
195 
196 	/*
197 	 * Attach the DSI bridge
198 	 *
199 	 * TODO: when adding support for the DPI bridge or several DSI bridges,
200 	 * we selectively connect the bridge(s) here instead of this simple
201 	 * attachment.
202 	 */
203 	ret = drm_simple_display_pipe_attach_bridge(&mcde->pipe,
204 						    mcde->bridge);
205 	if (ret) {
206 		dev_err(drm->dev, "failed to attach display output bridge\n");
207 		return ret;
208 	}
209 
210 	drm_mode_config_reset(drm);
211 	drm_kms_helper_poll_init(drm);
212 	drm_fbdev_generic_setup(drm, 32);
213 
214 	return 0;
215 }
216 
217 DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
218 
219 static struct drm_driver mcde_drm_driver = {
220 	.driver_features =
221 		DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
222 	.lastclose = drm_fb_helper_lastclose,
223 	.ioctls = NULL,
224 	.fops = &drm_fops,
225 	.name = "mcde",
226 	.desc = DRIVER_DESC,
227 	.date = "20180529",
228 	.major = 1,
229 	.minor = 0,
230 	.patchlevel = 0,
231 	.dumb_create = drm_gem_cma_dumb_create,
232 	.gem_free_object_unlocked = drm_gem_cma_free_object,
233 	.gem_vm_ops = &drm_gem_cma_vm_ops,
234 
235 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
236 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
237 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
238 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
239 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
240 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
241 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
242 };
243 
244 static int mcde_drm_bind(struct device *dev)
245 {
246 	struct drm_device *drm = dev_get_drvdata(dev);
247 	int ret;
248 
249 	ret = drmm_mode_config_init(drm);
250 	if (ret)
251 		return ret;
252 
253 	ret = component_bind_all(drm->dev, drm);
254 	if (ret) {
255 		dev_err(dev, "can't bind component devices\n");
256 		return ret;
257 	}
258 
259 	ret = mcde_modeset_init(drm);
260 	if (ret)
261 		goto unbind;
262 
263 	ret = drm_dev_register(drm, 0);
264 	if (ret < 0)
265 		goto unbind;
266 
267 	return 0;
268 
269 unbind:
270 	component_unbind_all(drm->dev, drm);
271 	return ret;
272 }
273 
274 static void mcde_drm_unbind(struct device *dev)
275 {
276 	struct drm_device *drm = dev_get_drvdata(dev);
277 
278 	drm_dev_unregister(drm);
279 	drm_atomic_helper_shutdown(drm);
280 	component_unbind_all(drm->dev, drm);
281 }
282 
283 static const struct component_master_ops mcde_drm_comp_ops = {
284 	.bind = mcde_drm_bind,
285 	.unbind = mcde_drm_unbind,
286 };
287 
288 static struct platform_driver *const mcde_component_drivers[] = {
289 	&mcde_dsi_driver,
290 };
291 
292 static int mcde_compare_dev(struct device *dev, void *data)
293 {
294 	return dev == data;
295 }
296 
297 static int mcde_probe(struct platform_device *pdev)
298 {
299 	struct device *dev = &pdev->dev;
300 	struct drm_device *drm;
301 	struct mcde *mcde;
302 	struct component_match *match = NULL;
303 	struct resource *res;
304 	u32 pid;
305 	u32 val;
306 	int irq;
307 	int ret;
308 	int i;
309 
310 	mcde = devm_drm_dev_alloc(dev, &mcde_drm_driver, struct mcde, drm);
311 	if (IS_ERR(mcde))
312 		return PTR_ERR(mcde);
313 	drm = &mcde->drm;
314 	mcde->dev = dev;
315 	platform_set_drvdata(pdev, drm);
316 
317 	/* Enable continuous updates: this is what Linux' framebuffer expects */
318 	mcde->oneshot_mode = false;
319 
320 	/* First obtain and turn on the main power */
321 	mcde->epod = devm_regulator_get(dev, "epod");
322 	if (IS_ERR(mcde->epod)) {
323 		ret = PTR_ERR(mcde->epod);
324 		dev_err(dev, "can't get EPOD regulator\n");
325 		return ret;
326 	}
327 	ret = regulator_enable(mcde->epod);
328 	if (ret) {
329 		dev_err(dev, "can't enable EPOD regulator\n");
330 		return ret;
331 	}
332 	mcde->vana = devm_regulator_get(dev, "vana");
333 	if (IS_ERR(mcde->vana)) {
334 		ret = PTR_ERR(mcde->vana);
335 		dev_err(dev, "can't get VANA regulator\n");
336 		goto regulator_epod_off;
337 	}
338 	ret = regulator_enable(mcde->vana);
339 	if (ret) {
340 		dev_err(dev, "can't enable VANA regulator\n");
341 		goto regulator_epod_off;
342 	}
343 	/*
344 	 * The vendor code uses ESRAM (onchip RAM) and need to activate
345 	 * the v-esram34 regulator, but we don't use that yet
346 	 */
347 
348 	/* Clock the silicon so we can access the registers */
349 	mcde->mcde_clk = devm_clk_get(dev, "mcde");
350 	if (IS_ERR(mcde->mcde_clk)) {
351 		dev_err(dev, "unable to get MCDE main clock\n");
352 		ret = PTR_ERR(mcde->mcde_clk);
353 		goto regulator_off;
354 	}
355 	ret = clk_prepare_enable(mcde->mcde_clk);
356 	if (ret) {
357 		dev_err(dev, "failed to enable MCDE main clock\n");
358 		goto regulator_off;
359 	}
360 	dev_info(dev, "MCDE clk rate %lu Hz\n", clk_get_rate(mcde->mcde_clk));
361 
362 	mcde->lcd_clk = devm_clk_get(dev, "lcd");
363 	if (IS_ERR(mcde->lcd_clk)) {
364 		dev_err(dev, "unable to get LCD clock\n");
365 		ret = PTR_ERR(mcde->lcd_clk);
366 		goto clk_disable;
367 	}
368 	mcde->hdmi_clk = devm_clk_get(dev, "hdmi");
369 	if (IS_ERR(mcde->hdmi_clk)) {
370 		dev_err(dev, "unable to get HDMI clock\n");
371 		ret = PTR_ERR(mcde->hdmi_clk);
372 		goto clk_disable;
373 	}
374 
375 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
376 	mcde->regs = devm_ioremap_resource(dev, res);
377 	if (IS_ERR(mcde->regs)) {
378 		dev_err(dev, "no MCDE regs\n");
379 		ret = -EINVAL;
380 		goto clk_disable;
381 	}
382 
383 	irq = platform_get_irq(pdev, 0);
384 	if (!irq) {
385 		ret = -EINVAL;
386 		goto clk_disable;
387 	}
388 
389 	ret = devm_request_irq(dev, irq, mcde_irq, 0, "mcde", mcde);
390 	if (ret) {
391 		dev_err(dev, "failed to request irq %d\n", ret);
392 		goto clk_disable;
393 	}
394 
395 	/*
396 	 * Check hardware revision, we only support U8500v2 version
397 	 * as this was the only version used for mass market deployment,
398 	 * but surely you can add more versions if you have them and
399 	 * need them.
400 	 */
401 	pid = readl(mcde->regs + MCDE_PID);
402 	dev_info(dev, "found MCDE HW revision %d.%d (dev %d, metal fix %d)\n",
403 		 (pid & MCDE_PID_MAJOR_VERSION_MASK)
404 		 >> MCDE_PID_MAJOR_VERSION_SHIFT,
405 		 (pid & MCDE_PID_MINOR_VERSION_MASK)
406 		 >> MCDE_PID_MINOR_VERSION_SHIFT,
407 		 (pid & MCDE_PID_DEVELOPMENT_VERSION_MASK)
408 		 >> MCDE_PID_DEVELOPMENT_VERSION_SHIFT,
409 		 (pid & MCDE_PID_METALFIX_VERSION_MASK)
410 		 >> MCDE_PID_METALFIX_VERSION_SHIFT);
411 	if (pid != 0x03000800) {
412 		dev_err(dev, "unsupported hardware revision\n");
413 		ret = -ENODEV;
414 		goto clk_disable;
415 	}
416 
417 	/* Set up the main control, watermark level at 7 */
418 	val = 7 << MCDE_CONF0_IFIFOCTRLWTRMRKLVL_SHIFT;
419 	/* 24 bits DPI: connect LSB Ch B to D[0:7] */
420 	val |= 3 << MCDE_CONF0_OUTMUX0_SHIFT;
421 	/* TV out: connect LSB Ch B to D[8:15] */
422 	val |= 3 << MCDE_CONF0_OUTMUX1_SHIFT;
423 	/* Don't care about this muxing */
424 	val |= 0 << MCDE_CONF0_OUTMUX2_SHIFT;
425 	/* 24 bits DPI: connect MID Ch B to D[24:31] */
426 	val |= 4 << MCDE_CONF0_OUTMUX3_SHIFT;
427 	/* 5: 24 bits DPI: connect MSB Ch B to D[32:39] */
428 	val |= 5 << MCDE_CONF0_OUTMUX4_SHIFT;
429 	/* Syncmux bits zero: DPI channel A and B on output pins A and B resp */
430 	writel(val, mcde->regs + MCDE_CONF0);
431 
432 	/* Enable automatic clock gating */
433 	val = readl(mcde->regs + MCDE_CR);
434 	val |= MCDE_CR_MCDEEN | MCDE_CR_AUTOCLKG_EN;
435 	writel(val, mcde->regs + MCDE_CR);
436 
437 	/* Clear any pending interrupts */
438 	mcde_display_disable_irqs(mcde);
439 	writel(0, mcde->regs + MCDE_IMSCERR);
440 	writel(0xFFFFFFFF, mcde->regs + MCDE_RISERR);
441 
442 	/* Spawn child devices for the DSI ports */
443 	devm_of_platform_populate(dev);
444 
445 	/* Create something that will match the subdrivers when we bind */
446 	for (i = 0; i < ARRAY_SIZE(mcde_component_drivers); i++) {
447 		struct device_driver *drv = &mcde_component_drivers[i]->driver;
448 		struct device *p = NULL, *d;
449 
450 		while ((d = platform_find_device_by_driver(p, drv))) {
451 			put_device(p);
452 			component_match_add(dev, &match, mcde_compare_dev, d);
453 			p = d;
454 		}
455 		put_device(p);
456 	}
457 	if (!match) {
458 		dev_err(dev, "no matching components\n");
459 		ret = -ENODEV;
460 		goto clk_disable;
461 	}
462 	if (IS_ERR(match)) {
463 		dev_err(dev, "could not create component match\n");
464 		ret = PTR_ERR(match);
465 		goto clk_disable;
466 	}
467 	ret = component_master_add_with_match(&pdev->dev, &mcde_drm_comp_ops,
468 					      match);
469 	if (ret) {
470 		dev_err(dev, "failed to add component master\n");
471 		goto clk_disable;
472 	}
473 	return 0;
474 
475 clk_disable:
476 	clk_disable_unprepare(mcde->mcde_clk);
477 regulator_off:
478 	regulator_disable(mcde->vana);
479 regulator_epod_off:
480 	regulator_disable(mcde->epod);
481 	return ret;
482 
483 }
484 
485 static int mcde_remove(struct platform_device *pdev)
486 {
487 	struct drm_device *drm = platform_get_drvdata(pdev);
488 	struct mcde *mcde = to_mcde(drm);
489 
490 	component_master_del(&pdev->dev, &mcde_drm_comp_ops);
491 	clk_disable_unprepare(mcde->mcde_clk);
492 	regulator_disable(mcde->vana);
493 	regulator_disable(mcde->epod);
494 
495 	return 0;
496 }
497 
498 static const struct of_device_id mcde_of_match[] = {
499 	{
500 		.compatible = "ste,mcde",
501 	},
502 	{},
503 };
504 
505 static struct platform_driver mcde_driver = {
506 	.driver = {
507 		.name           = "mcde",
508 		.of_match_table = of_match_ptr(mcde_of_match),
509 	},
510 	.probe = mcde_probe,
511 	.remove = mcde_remove,
512 };
513 
514 static struct platform_driver *const component_drivers[] = {
515 	&mcde_dsi_driver,
516 };
517 
518 static int __init mcde_drm_register(void)
519 {
520 	int ret;
521 
522 	ret = platform_register_drivers(component_drivers,
523 					ARRAY_SIZE(component_drivers));
524 	if (ret)
525 		return ret;
526 
527 	return platform_driver_register(&mcde_driver);
528 }
529 
530 static void __exit mcde_drm_unregister(void)
531 {
532 	platform_unregister_drivers(component_drivers,
533 				    ARRAY_SIZE(component_drivers));
534 	platform_driver_unregister(&mcde_driver);
535 }
536 
537 module_init(mcde_drm_register);
538 module_exit(mcde_drm_unregister);
539 
540 MODULE_ALIAS("platform:mcde-drm");
541 MODULE_DESCRIPTION(DRIVER_DESC);
542 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
543 MODULE_LICENSE("GPL");
544