2 * Copyright (C) 2012 Texas Instruments
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 /* LCDC DRM driver, based on da8xx-fb */
20 #include <linux/component.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/suspend.h>
23 #include <drm/drm_atomic.h>
24 #include <drm/drm_atomic_helper.h>
25 #include <drm/drm_fb_helper.h>
26 #include <drm/drm_gem_framebuffer_helper.h>
28 #include "tilcdc_drv.h"
29 #include "tilcdc_regs.h"
30 #include "tilcdc_tfp410.h"
31 #include "tilcdc_panel.h"
32 #include "tilcdc_external.h"
34 static LIST_HEAD(module_list);
36 static const u32 tilcdc_rev1_formats[] = { DRM_FORMAT_RGB565 };
38 static const u32 tilcdc_straight_formats[] = { DRM_FORMAT_RGB565,
40 DRM_FORMAT_XBGR8888 };
42 static const u32 tilcdc_crossed_formats[] = { DRM_FORMAT_BGR565,
44 DRM_FORMAT_XRGB8888 };
46 static const u32 tilcdc_legacy_formats[] = { DRM_FORMAT_RGB565,
48 DRM_FORMAT_XRGB8888 };
50 void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
51 const struct tilcdc_module_ops *funcs)
55 INIT_LIST_HEAD(&mod->list);
56 list_add(&mod->list, &module_list);
59 void tilcdc_module_cleanup(struct tilcdc_module *mod)
64 static struct of_device_id tilcdc_of_match[];
66 static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
67 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
69 return drm_gem_fb_create(dev, file_priv, mode_cmd);
72 static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
74 struct tilcdc_drm_private *priv = dev->dev_private;
75 drm_fbdev_cma_hotplug_event(priv->fbdev);
78 static int tilcdc_atomic_check(struct drm_device *dev,
79 struct drm_atomic_state *state)
83 ret = drm_atomic_helper_check_modeset(dev, state);
87 ret = drm_atomic_helper_check_planes(dev, state);
92 * tilcdc ->atomic_check can update ->mode_changed if pixel format
93 * changes, hence will we check modeset changes again.
95 ret = drm_atomic_helper_check_modeset(dev, state);
102 static int tilcdc_commit(struct drm_device *dev,
103 struct drm_atomic_state *state,
108 ret = drm_atomic_helper_prepare_planes(dev, state);
112 ret = drm_atomic_helper_swap_state(state, true);
114 drm_atomic_helper_cleanup_planes(dev, state);
119 * Everything below can be run asynchronously without the need to grab
120 * any modeset locks at all under one condition: It must be guaranteed
121 * that the asynchronous work has either been cancelled (if the driver
122 * supports it, which at least requires that the framebuffers get
123 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
124 * before the new state gets committed on the software side with
125 * drm_atomic_helper_swap_state().
127 * This scheme allows new atomic state updates to be prepared and
128 * checked in parallel to the asynchronous completion of the previous
129 * update. Which is important since compositors need to figure out the
130 * composition of the next frame right after having submitted the
134 drm_atomic_helper_commit_modeset_disables(dev, state);
136 drm_atomic_helper_commit_planes(dev, state, 0);
138 drm_atomic_helper_commit_modeset_enables(dev, state);
140 drm_atomic_helper_wait_for_vblanks(dev, state);
142 drm_atomic_helper_cleanup_planes(dev, state);
147 static const struct drm_mode_config_funcs mode_config_funcs = {
148 .fb_create = tilcdc_fb_create,
149 .output_poll_changed = tilcdc_fb_output_poll_changed,
150 .atomic_check = tilcdc_atomic_check,
151 .atomic_commit = tilcdc_commit,
154 static void modeset_init(struct drm_device *dev)
156 struct tilcdc_drm_private *priv = dev->dev_private;
157 struct tilcdc_module *mod;
159 list_for_each_entry(mod, &module_list, list) {
160 DBG("loading module: %s", mod->name);
161 mod->funcs->modeset_init(mod, dev);
164 dev->mode_config.min_width = 0;
165 dev->mode_config.min_height = 0;
166 dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
167 dev->mode_config.max_height = 2048;
168 dev->mode_config.funcs = &mode_config_funcs;
171 #ifdef CONFIG_CPU_FREQ
172 static int cpufreq_transition(struct notifier_block *nb,
173 unsigned long val, void *data)
175 struct tilcdc_drm_private *priv = container_of(nb,
176 struct tilcdc_drm_private, freq_transition);
178 if (val == CPUFREQ_POSTCHANGE)
179 tilcdc_crtc_update_clk(priv->crtc);
189 static void tilcdc_fini(struct drm_device *dev)
191 struct tilcdc_drm_private *priv = dev->dev_private;
194 tilcdc_crtc_shutdown(priv->crtc);
196 if (priv->is_registered)
197 drm_dev_unregister(dev);
199 drm_kms_helper_poll_fini(dev);
202 drm_fbdev_cma_fini(priv->fbdev);
204 drm_irq_uninstall(dev);
205 drm_mode_config_cleanup(dev);
206 tilcdc_remove_external_device(dev);
208 #ifdef CONFIG_CPU_FREQ
209 if (priv->freq_transition.notifier_call)
210 cpufreq_unregister_notifier(&priv->freq_transition,
211 CPUFREQ_TRANSITION_NOTIFIER);
221 flush_workqueue(priv->wq);
222 destroy_workqueue(priv->wq);
225 dev->dev_private = NULL;
227 pm_runtime_disable(dev->dev);
232 static int tilcdc_init(struct drm_driver *ddrv, struct device *dev)
234 struct drm_device *ddev;
235 struct platform_device *pdev = to_platform_device(dev);
236 struct device_node *node = dev->of_node;
237 struct tilcdc_drm_private *priv;
238 struct resource *res;
242 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
244 dev_err(dev, "failed to allocate private data\n");
248 ddev = drm_dev_alloc(ddrv, dev);
250 return PTR_ERR(ddev);
252 ddev->dev_private = priv;
253 platform_set_drvdata(pdev, ddev);
254 drm_mode_config_init(ddev);
256 priv->is_componentized =
257 tilcdc_get_external_components(dev, NULL) > 0;
259 priv->wq = alloc_ordered_workqueue("tilcdc", 0);
265 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267 dev_err(dev, "failed to get memory resource\n");
272 priv->mmio = ioremap_nocache(res->start, resource_size(res));
274 dev_err(dev, "failed to ioremap\n");
279 priv->clk = clk_get(dev, "fck");
280 if (IS_ERR(priv->clk)) {
281 dev_err(dev, "failed to get functional clock\n");
286 #ifdef CONFIG_CPU_FREQ
287 priv->freq_transition.notifier_call = cpufreq_transition;
288 ret = cpufreq_register_notifier(&priv->freq_transition,
289 CPUFREQ_TRANSITION_NOTIFIER);
291 dev_err(dev, "failed to register cpufreq notifier\n");
292 priv->freq_transition.notifier_call = NULL;
297 if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
298 priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH;
300 DBG("Maximum Bandwidth Value %d", priv->max_bandwidth);
302 if (of_property_read_u32(node, "max-width", &priv->max_width))
303 priv->max_width = TILCDC_DEFAULT_MAX_WIDTH;
305 DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width);
307 if (of_property_read_u32(node, "max-pixelclock",
308 &priv->max_pixelclock))
309 priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK;
311 DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock);
313 pm_runtime_enable(dev);
315 /* Determine LCD IP Version */
316 pm_runtime_get_sync(dev);
317 switch (tilcdc_read(ddev, LCDC_PID_REG)) {
326 dev_warn(dev, "Unknown PID Reg value 0x%08x, "
327 "defaulting to LCD revision 1\n",
328 tilcdc_read(ddev, LCDC_PID_REG));
333 pm_runtime_put_sync(dev);
335 if (priv->rev == 1) {
336 DBG("Revision 1 LCDC supports only RGB565 format");
337 priv->pixelformats = tilcdc_rev1_formats;
338 priv->num_pixelformats = ARRAY_SIZE(tilcdc_rev1_formats);
341 const char *str = "\0";
343 of_property_read_string(node, "blue-and-red-wiring", &str);
344 if (0 == strcmp(str, "crossed")) {
345 DBG("Configured for crossed blue and red wires");
346 priv->pixelformats = tilcdc_crossed_formats;
347 priv->num_pixelformats =
348 ARRAY_SIZE(tilcdc_crossed_formats);
349 bpp = 32; /* Choose bpp with RGB support for fbdef */
350 } else if (0 == strcmp(str, "straight")) {
351 DBG("Configured for straight blue and red wires");
352 priv->pixelformats = tilcdc_straight_formats;
353 priv->num_pixelformats =
354 ARRAY_SIZE(tilcdc_straight_formats);
355 bpp = 16; /* Choose bpp with RGB support for fbdef */
357 DBG("Blue and red wiring '%s' unknown, use legacy mode",
359 priv->pixelformats = tilcdc_legacy_formats;
360 priv->num_pixelformats =
361 ARRAY_SIZE(tilcdc_legacy_formats);
362 bpp = 16; /* This is just a guess */
366 ret = tilcdc_crtc_create(ddev);
368 dev_err(dev, "failed to create crtc\n");
373 if (priv->is_componentized) {
374 ret = component_bind_all(dev, ddev);
378 ret = tilcdc_add_component_encoder(ddev);
382 ret = tilcdc_attach_external_device(ddev);
387 if (!priv->external_connector &&
388 ((priv->num_encoders == 0) || (priv->num_connectors == 0))) {
389 dev_err(dev, "no encoders/connectors found\n");
394 ret = drm_vblank_init(ddev, 1);
396 dev_err(dev, "failed to initialize vblank\n");
400 ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
402 dev_err(dev, "failed to install IRQ handler\n");
406 drm_mode_config_reset(ddev);
408 priv->fbdev = drm_fbdev_cma_init(ddev, bpp,
409 ddev->mode_config.num_connector);
410 if (IS_ERR(priv->fbdev)) {
411 ret = PTR_ERR(priv->fbdev);
415 drm_kms_helper_poll_init(ddev);
417 ret = drm_dev_register(ddev, 0);
421 priv->is_registered = true;
430 static void tilcdc_lastclose(struct drm_device *dev)
432 struct tilcdc_drm_private *priv = dev->dev_private;
433 drm_fbdev_cma_restore_mode(priv->fbdev);
436 static irqreturn_t tilcdc_irq(int irq, void *arg)
438 struct drm_device *dev = arg;
439 struct tilcdc_drm_private *priv = dev->dev_private;
440 return tilcdc_crtc_irq(priv->crtc);
443 #if defined(CONFIG_DEBUG_FS)
444 static const struct {
450 #define REG(rev, save, reg) { #reg, rev, save, reg }
451 /* exists in revision 1: */
452 REG(1, false, LCDC_PID_REG),
453 REG(1, true, LCDC_CTRL_REG),
454 REG(1, false, LCDC_STAT_REG),
455 REG(1, true, LCDC_RASTER_CTRL_REG),
456 REG(1, true, LCDC_RASTER_TIMING_0_REG),
457 REG(1, true, LCDC_RASTER_TIMING_1_REG),
458 REG(1, true, LCDC_RASTER_TIMING_2_REG),
459 REG(1, true, LCDC_DMA_CTRL_REG),
460 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
461 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
462 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
463 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
464 /* new in revision 2: */
465 REG(2, false, LCDC_RAW_STAT_REG),
466 REG(2, false, LCDC_MASKED_STAT_REG),
467 REG(2, true, LCDC_INT_ENABLE_SET_REG),
468 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
469 REG(2, false, LCDC_END_OF_INT_IND_REG),
470 REG(2, true, LCDC_CLK_ENABLE_REG),
476 #ifdef CONFIG_DEBUG_FS
477 static int tilcdc_regs_show(struct seq_file *m, void *arg)
479 struct drm_info_node *node = (struct drm_info_node *) m->private;
480 struct drm_device *dev = node->minor->dev;
481 struct tilcdc_drm_private *priv = dev->dev_private;
484 pm_runtime_get_sync(dev->dev);
486 seq_printf(m, "revision: %d\n", priv->rev);
488 for (i = 0; i < ARRAY_SIZE(registers); i++)
489 if (priv->rev >= registers[i].rev)
490 seq_printf(m, "%s:\t %08x\n", registers[i].name,
491 tilcdc_read(dev, registers[i].reg));
493 pm_runtime_put_sync(dev->dev);
498 static int tilcdc_mm_show(struct seq_file *m, void *arg)
500 struct drm_info_node *node = (struct drm_info_node *) m->private;
501 struct drm_device *dev = node->minor->dev;
502 struct drm_printer p = drm_seq_file_printer(m);
503 drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
507 static struct drm_info_list tilcdc_debugfs_list[] = {
508 { "regs", tilcdc_regs_show, 0 },
509 { "mm", tilcdc_mm_show, 0 },
510 { "fb", drm_fb_cma_debugfs_show, 0 },
513 static int tilcdc_debugfs_init(struct drm_minor *minor)
515 struct drm_device *dev = minor->dev;
516 struct tilcdc_module *mod;
519 ret = drm_debugfs_create_files(tilcdc_debugfs_list,
520 ARRAY_SIZE(tilcdc_debugfs_list),
521 minor->debugfs_root, minor);
523 list_for_each_entry(mod, &module_list, list)
524 if (mod->funcs->debugfs_init)
525 mod->funcs->debugfs_init(mod, minor);
528 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
536 DEFINE_DRM_GEM_CMA_FOPS(fops);
538 static struct drm_driver tilcdc_driver = {
539 .driver_features = (DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET |
540 DRIVER_PRIME | DRIVER_ATOMIC),
541 .lastclose = tilcdc_lastclose,
542 .irq_handler = tilcdc_irq,
543 .gem_free_object_unlocked = drm_gem_cma_free_object,
544 .gem_vm_ops = &drm_gem_cma_vm_ops,
545 .dumb_create = drm_gem_cma_dumb_create,
547 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
548 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
549 .gem_prime_import = drm_gem_prime_import,
550 .gem_prime_export = drm_gem_prime_export,
551 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
552 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
553 .gem_prime_vmap = drm_gem_cma_prime_vmap,
554 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
555 .gem_prime_mmap = drm_gem_cma_prime_mmap,
556 #ifdef CONFIG_DEBUG_FS
557 .debugfs_init = tilcdc_debugfs_init,
561 .desc = "TI LCD Controller DRM",
571 #ifdef CONFIG_PM_SLEEP
572 static int tilcdc_pm_suspend(struct device *dev)
574 struct drm_device *ddev = dev_get_drvdata(dev);
575 struct tilcdc_drm_private *priv = ddev->dev_private;
577 priv->saved_state = drm_atomic_helper_suspend(ddev);
579 /* Select sleep pin state */
580 pinctrl_pm_select_sleep_state(dev);
585 static int tilcdc_pm_resume(struct device *dev)
587 struct drm_device *ddev = dev_get_drvdata(dev);
588 struct tilcdc_drm_private *priv = ddev->dev_private;
591 /* Select default pin state */
592 pinctrl_pm_select_default_state(dev);
594 if (priv->saved_state)
595 ret = drm_atomic_helper_resume(ddev, priv->saved_state);
601 static const struct dev_pm_ops tilcdc_pm_ops = {
602 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
608 static int tilcdc_bind(struct device *dev)
610 return tilcdc_init(&tilcdc_driver, dev);
613 static void tilcdc_unbind(struct device *dev)
615 struct drm_device *ddev = dev_get_drvdata(dev);
617 /* Check if a subcomponent has already triggered the unloading. */
618 if (!ddev->dev_private)
621 tilcdc_fini(dev_get_drvdata(dev));
624 static const struct component_master_ops tilcdc_comp_ops = {
626 .unbind = tilcdc_unbind,
629 static int tilcdc_pdev_probe(struct platform_device *pdev)
631 struct component_match *match = NULL;
634 /* bail out early if no DT data: */
635 if (!pdev->dev.of_node) {
636 dev_err(&pdev->dev, "device-tree data is missing\n");
640 ret = tilcdc_get_external_components(&pdev->dev, &match);
644 return tilcdc_init(&tilcdc_driver, &pdev->dev);
646 return component_master_add_with_match(&pdev->dev,
651 static int tilcdc_pdev_remove(struct platform_device *pdev)
655 ret = tilcdc_get_external_components(&pdev->dev, NULL);
659 tilcdc_fini(platform_get_drvdata(pdev));
661 component_master_del(&pdev->dev, &tilcdc_comp_ops);
666 static struct of_device_id tilcdc_of_match[] = {
667 { .compatible = "ti,am33xx-tilcdc", },
668 { .compatible = "ti,da850-tilcdc", },
671 MODULE_DEVICE_TABLE(of, tilcdc_of_match);
673 static struct platform_driver tilcdc_platform_driver = {
674 .probe = tilcdc_pdev_probe,
675 .remove = tilcdc_pdev_remove,
678 .pm = &tilcdc_pm_ops,
679 .of_match_table = tilcdc_of_match,
683 static int __init tilcdc_drm_init(void)
686 tilcdc_tfp410_init();
688 return platform_driver_register(&tilcdc_platform_driver);
691 static void __exit tilcdc_drm_fini(void)
694 platform_driver_unregister(&tilcdc_platform_driver);
696 tilcdc_tfp410_fini();
699 module_init(tilcdc_drm_init);
700 module_exit(tilcdc_drm_fini);
703 MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
704 MODULE_LICENSE("GPL");