1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Texas Instruments
4 * Author: Rob Clark <robdclark@gmail.com>
7 /* LCDC DRM driver, based on da8xx-fb */
9 #include <linux/component.h>
10 #include <linux/pinctrl/consumer.h>
11 #include <linux/suspend.h>
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_probe_helper.h>
18 #include "tilcdc_drv.h"
19 #include "tilcdc_regs.h"
20 #include "tilcdc_tfp410.h"
21 #include "tilcdc_panel.h"
22 #include "tilcdc_external.h"
24 static LIST_HEAD(module_list);
26 static const u32 tilcdc_rev1_formats[] = { DRM_FORMAT_RGB565 };
28 static const u32 tilcdc_straight_formats[] = { DRM_FORMAT_RGB565,
30 DRM_FORMAT_XBGR8888 };
32 static const u32 tilcdc_crossed_formats[] = { DRM_FORMAT_BGR565,
34 DRM_FORMAT_XRGB8888 };
36 static const u32 tilcdc_legacy_formats[] = { DRM_FORMAT_RGB565,
38 DRM_FORMAT_XRGB8888 };
40 void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
41 const struct tilcdc_module_ops *funcs)
45 INIT_LIST_HEAD(&mod->list);
46 list_add(&mod->list, &module_list);
49 void tilcdc_module_cleanup(struct tilcdc_module *mod)
54 static struct of_device_id tilcdc_of_match[];
56 static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
57 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
59 return drm_gem_fb_create(dev, file_priv, mode_cmd);
62 static int tilcdc_atomic_check(struct drm_device *dev,
63 struct drm_atomic_state *state)
67 ret = drm_atomic_helper_check_modeset(dev, state);
71 ret = drm_atomic_helper_check_planes(dev, state);
76 * tilcdc ->atomic_check can update ->mode_changed if pixel format
77 * changes, hence will we check modeset changes again.
79 ret = drm_atomic_helper_check_modeset(dev, state);
86 static int tilcdc_commit(struct drm_device *dev,
87 struct drm_atomic_state *state,
92 ret = drm_atomic_helper_prepare_planes(dev, state);
96 ret = drm_atomic_helper_swap_state(state, true);
98 drm_atomic_helper_cleanup_planes(dev, state);
103 * Everything below can be run asynchronously without the need to grab
104 * any modeset locks at all under one condition: It must be guaranteed
105 * that the asynchronous work has either been cancelled (if the driver
106 * supports it, which at least requires that the framebuffers get
107 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
108 * before the new state gets committed on the software side with
109 * drm_atomic_helper_swap_state().
111 * This scheme allows new atomic state updates to be prepared and
112 * checked in parallel to the asynchronous completion of the previous
113 * update. Which is important since compositors need to figure out the
114 * composition of the next frame right after having submitted the
118 drm_atomic_helper_commit_modeset_disables(dev, state);
120 drm_atomic_helper_commit_planes(dev, state, 0);
122 drm_atomic_helper_commit_modeset_enables(dev, state);
124 drm_atomic_helper_wait_for_vblanks(dev, state);
126 drm_atomic_helper_cleanup_planes(dev, state);
131 static const struct drm_mode_config_funcs mode_config_funcs = {
132 .fb_create = tilcdc_fb_create,
133 .atomic_check = tilcdc_atomic_check,
134 .atomic_commit = tilcdc_commit,
137 static void modeset_init(struct drm_device *dev)
139 struct tilcdc_drm_private *priv = dev->dev_private;
140 struct tilcdc_module *mod;
142 list_for_each_entry(mod, &module_list, list) {
143 DBG("loading module: %s", mod->name);
144 mod->funcs->modeset_init(mod, dev);
147 dev->mode_config.min_width = 0;
148 dev->mode_config.min_height = 0;
149 dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
150 dev->mode_config.max_height = 2048;
151 dev->mode_config.funcs = &mode_config_funcs;
154 #ifdef CONFIG_CPU_FREQ
155 static int cpufreq_transition(struct notifier_block *nb,
156 unsigned long val, void *data)
158 struct tilcdc_drm_private *priv = container_of(nb,
159 struct tilcdc_drm_private, freq_transition);
161 if (val == CPUFREQ_POSTCHANGE)
162 tilcdc_crtc_update_clk(priv->crtc);
172 static void tilcdc_fini(struct drm_device *dev)
174 struct tilcdc_drm_private *priv = dev->dev_private;
176 #ifdef CONFIG_CPU_FREQ
177 if (priv->freq_transition.notifier_call)
178 cpufreq_unregister_notifier(&priv->freq_transition,
179 CPUFREQ_TRANSITION_NOTIFIER);
183 tilcdc_crtc_shutdown(priv->crtc);
185 if (priv->is_registered)
186 drm_dev_unregister(dev);
188 drm_kms_helper_poll_fini(dev);
189 drm_irq_uninstall(dev);
190 drm_mode_config_cleanup(dev);
191 tilcdc_remove_external_device(dev);
200 flush_workqueue(priv->wq);
201 destroy_workqueue(priv->wq);
204 dev->dev_private = NULL;
206 pm_runtime_disable(dev->dev);
211 static int tilcdc_init(struct drm_driver *ddrv, struct device *dev)
213 struct drm_device *ddev;
214 struct platform_device *pdev = to_platform_device(dev);
215 struct device_node *node = dev->of_node;
216 struct tilcdc_drm_private *priv;
217 struct resource *res;
221 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
225 ddev = drm_dev_alloc(ddrv, dev);
227 return PTR_ERR(ddev);
229 ddev->dev_private = priv;
230 platform_set_drvdata(pdev, ddev);
231 drm_mode_config_init(ddev);
233 priv->is_componentized =
234 tilcdc_get_external_components(dev, NULL) > 0;
236 priv->wq = alloc_ordered_workqueue("tilcdc", 0);
242 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
244 dev_err(dev, "failed to get memory resource\n");
249 priv->mmio = ioremap_nocache(res->start, resource_size(res));
251 dev_err(dev, "failed to ioremap\n");
256 priv->clk = clk_get(dev, "fck");
257 if (IS_ERR(priv->clk)) {
258 dev_err(dev, "failed to get functional clock\n");
263 if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
264 priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH;
266 DBG("Maximum Bandwidth Value %d", priv->max_bandwidth);
268 if (of_property_read_u32(node, "max-width", &priv->max_width))
269 priv->max_width = TILCDC_DEFAULT_MAX_WIDTH;
271 DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width);
273 if (of_property_read_u32(node, "max-pixelclock",
274 &priv->max_pixelclock))
275 priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK;
277 DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock);
279 pm_runtime_enable(dev);
281 /* Determine LCD IP Version */
282 pm_runtime_get_sync(dev);
283 switch (tilcdc_read(ddev, LCDC_PID_REG)) {
292 dev_warn(dev, "Unknown PID Reg value 0x%08x, "
293 "defaulting to LCD revision 1\n",
294 tilcdc_read(ddev, LCDC_PID_REG));
299 pm_runtime_put_sync(dev);
301 if (priv->rev == 1) {
302 DBG("Revision 1 LCDC supports only RGB565 format");
303 priv->pixelformats = tilcdc_rev1_formats;
304 priv->num_pixelformats = ARRAY_SIZE(tilcdc_rev1_formats);
307 const char *str = "\0";
309 of_property_read_string(node, "blue-and-red-wiring", &str);
310 if (0 == strcmp(str, "crossed")) {
311 DBG("Configured for crossed blue and red wires");
312 priv->pixelformats = tilcdc_crossed_formats;
313 priv->num_pixelformats =
314 ARRAY_SIZE(tilcdc_crossed_formats);
315 bpp = 32; /* Choose bpp with RGB support for fbdef */
316 } else if (0 == strcmp(str, "straight")) {
317 DBG("Configured for straight blue and red wires");
318 priv->pixelformats = tilcdc_straight_formats;
319 priv->num_pixelformats =
320 ARRAY_SIZE(tilcdc_straight_formats);
321 bpp = 16; /* Choose bpp with RGB support for fbdef */
323 DBG("Blue and red wiring '%s' unknown, use legacy mode",
325 priv->pixelformats = tilcdc_legacy_formats;
326 priv->num_pixelformats =
327 ARRAY_SIZE(tilcdc_legacy_formats);
328 bpp = 16; /* This is just a guess */
332 ret = tilcdc_crtc_create(ddev);
334 dev_err(dev, "failed to create crtc\n");
339 #ifdef CONFIG_CPU_FREQ
340 priv->freq_transition.notifier_call = cpufreq_transition;
341 ret = cpufreq_register_notifier(&priv->freq_transition,
342 CPUFREQ_TRANSITION_NOTIFIER);
344 dev_err(dev, "failed to register cpufreq notifier\n");
345 priv->freq_transition.notifier_call = NULL;
350 if (priv->is_componentized) {
351 ret = component_bind_all(dev, ddev);
355 ret = tilcdc_add_component_encoder(ddev);
359 ret = tilcdc_attach_external_device(ddev);
364 if (!priv->external_connector &&
365 ((priv->num_encoders == 0) || (priv->num_connectors == 0))) {
366 dev_err(dev, "no encoders/connectors found\n");
371 ret = drm_vblank_init(ddev, 1);
373 dev_err(dev, "failed to initialize vblank\n");
377 ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
379 dev_err(dev, "failed to install IRQ handler\n");
383 drm_mode_config_reset(ddev);
385 drm_kms_helper_poll_init(ddev);
387 ret = drm_dev_register(ddev, 0);
391 drm_fbdev_generic_setup(ddev, bpp);
393 priv->is_registered = true;
402 static irqreturn_t tilcdc_irq(int irq, void *arg)
404 struct drm_device *dev = arg;
405 struct tilcdc_drm_private *priv = dev->dev_private;
406 return tilcdc_crtc_irq(priv->crtc);
409 #if defined(CONFIG_DEBUG_FS)
410 static const struct {
416 #define REG(rev, save, reg) { #reg, rev, save, reg }
417 /* exists in revision 1: */
418 REG(1, false, LCDC_PID_REG),
419 REG(1, true, LCDC_CTRL_REG),
420 REG(1, false, LCDC_STAT_REG),
421 REG(1, true, LCDC_RASTER_CTRL_REG),
422 REG(1, true, LCDC_RASTER_TIMING_0_REG),
423 REG(1, true, LCDC_RASTER_TIMING_1_REG),
424 REG(1, true, LCDC_RASTER_TIMING_2_REG),
425 REG(1, true, LCDC_DMA_CTRL_REG),
426 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
427 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
428 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
429 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
430 /* new in revision 2: */
431 REG(2, false, LCDC_RAW_STAT_REG),
432 REG(2, false, LCDC_MASKED_STAT_REG),
433 REG(2, true, LCDC_INT_ENABLE_SET_REG),
434 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
435 REG(2, false, LCDC_END_OF_INT_IND_REG),
436 REG(2, true, LCDC_CLK_ENABLE_REG),
442 #ifdef CONFIG_DEBUG_FS
443 static int tilcdc_regs_show(struct seq_file *m, void *arg)
445 struct drm_info_node *node = (struct drm_info_node *) m->private;
446 struct drm_device *dev = node->minor->dev;
447 struct tilcdc_drm_private *priv = dev->dev_private;
450 pm_runtime_get_sync(dev->dev);
452 seq_printf(m, "revision: %d\n", priv->rev);
454 for (i = 0; i < ARRAY_SIZE(registers); i++)
455 if (priv->rev >= registers[i].rev)
456 seq_printf(m, "%s:\t %08x\n", registers[i].name,
457 tilcdc_read(dev, registers[i].reg));
459 pm_runtime_put_sync(dev->dev);
464 static int tilcdc_mm_show(struct seq_file *m, void *arg)
466 struct drm_info_node *node = (struct drm_info_node *) m->private;
467 struct drm_device *dev = node->minor->dev;
468 struct drm_printer p = drm_seq_file_printer(m);
469 drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
473 static struct drm_info_list tilcdc_debugfs_list[] = {
474 { "regs", tilcdc_regs_show, 0 },
475 { "mm", tilcdc_mm_show, 0 },
478 static int tilcdc_debugfs_init(struct drm_minor *minor)
480 struct drm_device *dev = minor->dev;
481 struct tilcdc_module *mod;
484 ret = drm_debugfs_create_files(tilcdc_debugfs_list,
485 ARRAY_SIZE(tilcdc_debugfs_list),
486 minor->debugfs_root, minor);
488 list_for_each_entry(mod, &module_list, list)
489 if (mod->funcs->debugfs_init)
490 mod->funcs->debugfs_init(mod, minor);
493 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
501 DEFINE_DRM_GEM_CMA_FOPS(fops);
503 static struct drm_driver tilcdc_driver = {
504 .driver_features = (DRIVER_GEM | DRIVER_MODESET |
505 DRIVER_PRIME | DRIVER_ATOMIC),
506 .irq_handler = tilcdc_irq,
507 .gem_free_object_unlocked = drm_gem_cma_free_object,
508 .gem_print_info = drm_gem_cma_print_info,
509 .gem_vm_ops = &drm_gem_cma_vm_ops,
510 .dumb_create = drm_gem_cma_dumb_create,
512 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
513 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
514 .gem_prime_import = drm_gem_prime_import,
515 .gem_prime_export = drm_gem_prime_export,
516 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
517 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
518 .gem_prime_vmap = drm_gem_cma_prime_vmap,
519 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
520 .gem_prime_mmap = drm_gem_cma_prime_mmap,
521 #ifdef CONFIG_DEBUG_FS
522 .debugfs_init = tilcdc_debugfs_init,
526 .desc = "TI LCD Controller DRM",
536 #ifdef CONFIG_PM_SLEEP
537 static int tilcdc_pm_suspend(struct device *dev)
539 struct drm_device *ddev = dev_get_drvdata(dev);
542 ret = drm_mode_config_helper_suspend(ddev);
544 /* Select sleep pin state */
545 pinctrl_pm_select_sleep_state(dev);
550 static int tilcdc_pm_resume(struct device *dev)
552 struct drm_device *ddev = dev_get_drvdata(dev);
554 /* Select default pin state */
555 pinctrl_pm_select_default_state(dev);
556 return drm_mode_config_helper_resume(ddev);
560 static const struct dev_pm_ops tilcdc_pm_ops = {
561 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
567 static int tilcdc_bind(struct device *dev)
569 return tilcdc_init(&tilcdc_driver, dev);
572 static void tilcdc_unbind(struct device *dev)
574 struct drm_device *ddev = dev_get_drvdata(dev);
576 /* Check if a subcomponent has already triggered the unloading. */
577 if (!ddev->dev_private)
580 tilcdc_fini(dev_get_drvdata(dev));
583 static const struct component_master_ops tilcdc_comp_ops = {
585 .unbind = tilcdc_unbind,
588 static int tilcdc_pdev_probe(struct platform_device *pdev)
590 struct component_match *match = NULL;
593 /* bail out early if no DT data: */
594 if (!pdev->dev.of_node) {
595 dev_err(&pdev->dev, "device-tree data is missing\n");
599 ret = tilcdc_get_external_components(&pdev->dev, &match);
603 return tilcdc_init(&tilcdc_driver, &pdev->dev);
605 return component_master_add_with_match(&pdev->dev,
610 static int tilcdc_pdev_remove(struct platform_device *pdev)
614 ret = tilcdc_get_external_components(&pdev->dev, NULL);
618 tilcdc_fini(platform_get_drvdata(pdev));
620 component_master_del(&pdev->dev, &tilcdc_comp_ops);
625 static struct of_device_id tilcdc_of_match[] = {
626 { .compatible = "ti,am33xx-tilcdc", },
627 { .compatible = "ti,da850-tilcdc", },
630 MODULE_DEVICE_TABLE(of, tilcdc_of_match);
632 static struct platform_driver tilcdc_platform_driver = {
633 .probe = tilcdc_pdev_probe,
634 .remove = tilcdc_pdev_remove,
637 .pm = &tilcdc_pm_ops,
638 .of_match_table = tilcdc_of_match,
642 static int __init tilcdc_drm_init(void)
645 tilcdc_tfp410_init();
647 return platform_driver_register(&tilcdc_platform_driver);
650 static void __exit tilcdc_drm_fini(void)
653 platform_driver_unregister(&tilcdc_platform_driver);
655 tilcdc_tfp410_fini();
658 module_init(tilcdc_drm_init);
659 module_exit(tilcdc_drm_fini);
661 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
662 MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
663 MODULE_LICENSE("GPL");