2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
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>
26 #include "tilcdc_drv.h"
27 #include "tilcdc_regs.h"
28 #include "tilcdc_tfp410.h"
29 #include "tilcdc_panel.h"
30 #include "tilcdc_external.h"
32 #include "drm_fb_helper.h"
34 static LIST_HEAD(module_list);
36 void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
37 const struct tilcdc_module_ops *funcs)
41 INIT_LIST_HEAD(&mod->list);
42 list_add(&mod->list, &module_list);
45 void tilcdc_module_cleanup(struct tilcdc_module *mod)
50 static struct of_device_id tilcdc_of_match[];
52 static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
53 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
55 return drm_fb_cma_create(dev, file_priv, mode_cmd);
58 static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
60 struct tilcdc_drm_private *priv = dev->dev_private;
61 drm_fbdev_cma_hotplug_event(priv->fbdev);
64 int tilcdc_atomic_check(struct drm_device *dev,
65 struct drm_atomic_state *state)
69 ret = drm_atomic_helper_check_modeset(dev, state);
73 ret = drm_atomic_helper_check_planes(dev, state);
78 * tilcdc ->atomic_check can update ->mode_changed if pixel format
79 * changes, hence will we check modeset changes again.
81 ret = drm_atomic_helper_check_modeset(dev, state);
88 static int tilcdc_commit(struct drm_device *dev,
89 struct drm_atomic_state *state,
94 ret = drm_atomic_helper_prepare_planes(dev, state);
98 drm_atomic_helper_swap_state(state, true);
101 * Everything below can be run asynchronously without the need to grab
102 * any modeset locks at all under one condition: It must be guaranteed
103 * that the asynchronous work has either been cancelled (if the driver
104 * supports it, which at least requires that the framebuffers get
105 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
106 * before the new state gets committed on the software side with
107 * drm_atomic_helper_swap_state().
109 * This scheme allows new atomic state updates to be prepared and
110 * checked in parallel to the asynchronous completion of the previous
111 * update. Which is important since compositors need to figure out the
112 * composition of the next frame right after having submitted the
116 drm_atomic_helper_commit_modeset_disables(dev, state);
118 drm_atomic_helper_commit_planes(dev, state, false);
120 drm_atomic_helper_commit_modeset_enables(dev, state);
122 drm_atomic_helper_wait_for_vblanks(dev, state);
124 drm_atomic_helper_cleanup_planes(dev, state);
126 drm_atomic_state_free(state);
131 static const struct drm_mode_config_funcs mode_config_funcs = {
132 .fb_create = tilcdc_fb_create,
133 .output_poll_changed = tilcdc_fb_output_poll_changed,
134 .atomic_check = tilcdc_atomic_check,
135 .atomic_commit = tilcdc_commit,
138 static int modeset_init(struct drm_device *dev)
140 struct tilcdc_drm_private *priv = dev->dev_private;
141 struct tilcdc_module *mod;
143 drm_mode_config_init(dev);
145 priv->crtc = tilcdc_crtc_create(dev);
147 list_for_each_entry(mod, &module_list, list) {
148 DBG("loading module: %s", mod->name);
149 mod->funcs->modeset_init(mod, dev);
152 dev->mode_config.min_width = 0;
153 dev->mode_config.min_height = 0;
154 dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
155 dev->mode_config.max_height = 2048;
156 dev->mode_config.funcs = &mode_config_funcs;
161 #ifdef CONFIG_CPU_FREQ
162 static int cpufreq_transition(struct notifier_block *nb,
163 unsigned long val, void *data)
165 struct tilcdc_drm_private *priv = container_of(nb,
166 struct tilcdc_drm_private, freq_transition);
167 if (val == CPUFREQ_POSTCHANGE) {
168 if (priv->lcd_fck_rate != clk_get_rate(priv->clk)) {
169 priv->lcd_fck_rate = clk_get_rate(priv->clk);
170 tilcdc_crtc_update_clk(priv->crtc);
182 static int tilcdc_unload(struct drm_device *dev)
184 struct tilcdc_drm_private *priv = dev->dev_private;
186 tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF);
188 tilcdc_remove_external_encoders(dev);
190 drm_fbdev_cma_fini(priv->fbdev);
191 drm_kms_helper_poll_fini(dev);
192 drm_mode_config_cleanup(dev);
193 drm_vblank_cleanup(dev);
195 drm_irq_uninstall(dev);
197 #ifdef CONFIG_CPU_FREQ
198 cpufreq_unregister_notifier(&priv->freq_transition,
199 CPUFREQ_TRANSITION_NOTIFIER);
208 flush_workqueue(priv->wq);
209 destroy_workqueue(priv->wq);
211 dev->dev_private = NULL;
213 pm_runtime_disable(dev->dev);
218 static int tilcdc_load(struct drm_device *dev, unsigned long flags)
220 struct platform_device *pdev = dev->platformdev;
221 struct device_node *node = pdev->dev.of_node;
222 struct tilcdc_drm_private *priv;
223 struct tilcdc_module *mod;
224 struct resource *res;
228 priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
230 dev_err(dev->dev, "failed to allocate private data\n");
234 dev->dev_private = priv;
236 priv->is_componentized =
237 tilcdc_get_external_components(dev->dev, NULL) > 0;
239 priv->wq = alloc_ordered_workqueue("tilcdc", 0);
242 goto fail_unset_priv;
245 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
247 dev_err(dev->dev, "failed to get memory resource\n");
252 priv->mmio = ioremap_nocache(res->start, resource_size(res));
254 dev_err(dev->dev, "failed to ioremap\n");
259 priv->clk = clk_get(dev->dev, "fck");
260 if (IS_ERR(priv->clk)) {
261 dev_err(dev->dev, "failed to get functional clock\n");
266 #ifdef CONFIG_CPU_FREQ
267 priv->lcd_fck_rate = clk_get_rate(priv->clk);
268 priv->freq_transition.notifier_call = cpufreq_transition;
269 ret = cpufreq_register_notifier(&priv->freq_transition,
270 CPUFREQ_TRANSITION_NOTIFIER);
272 dev_err(dev->dev, "failed to register cpufreq notifier\n");
277 if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
278 priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH;
280 DBG("Maximum Bandwidth Value %d", priv->max_bandwidth);
282 if (of_property_read_u32(node, "ti,max-width", &priv->max_width))
283 priv->max_width = TILCDC_DEFAULT_MAX_WIDTH;
285 DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width);
287 if (of_property_read_u32(node, "ti,max-pixelclock",
288 &priv->max_pixelclock))
289 priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK;
291 DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock);
293 pm_runtime_enable(dev->dev);
295 /* Determine LCD IP Version */
296 pm_runtime_get_sync(dev->dev);
297 switch (tilcdc_read(dev, LCDC_PID_REG)) {
306 dev_warn(dev->dev, "Unknown PID Reg value 0x%08x, "
307 "defaulting to LCD revision 1\n",
308 tilcdc_read(dev, LCDC_PID_REG));
313 pm_runtime_put_sync(dev->dev);
315 ret = modeset_init(dev);
317 dev_err(dev->dev, "failed to initialize mode setting\n");
318 goto fail_cpufreq_unregister;
321 platform_set_drvdata(pdev, dev);
323 if (priv->is_componentized) {
324 ret = component_bind_all(dev->dev, dev);
326 goto fail_mode_config_cleanup;
328 ret = tilcdc_add_external_encoders(dev, &bpp);
330 goto fail_component_cleanup;
333 if ((priv->num_encoders == 0) || (priv->num_connectors == 0)) {
334 dev_err(dev->dev, "no encoders/connectors found\n");
336 goto fail_external_cleanup;
339 ret = drm_vblank_init(dev, 1);
341 dev_err(dev->dev, "failed to initialize vblank\n");
342 goto fail_external_cleanup;
345 ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
347 dev_err(dev->dev, "failed to install IRQ handler\n");
348 goto fail_vblank_cleanup;
351 list_for_each_entry(mod, &module_list, list) {
352 DBG("%s: preferred_bpp: %d", mod->name, mod->preferred_bpp);
353 bpp = mod->preferred_bpp;
358 drm_helper_disable_unused_functions(dev);
360 drm_mode_config_reset(dev);
362 priv->fbdev = drm_fbdev_cma_init(dev, bpp,
363 dev->mode_config.num_crtc,
364 dev->mode_config.num_connector);
365 if (IS_ERR(priv->fbdev)) {
366 ret = PTR_ERR(priv->fbdev);
367 goto fail_irq_uninstall;
370 drm_kms_helper_poll_init(dev);
375 drm_irq_uninstall(dev);
378 drm_vblank_cleanup(dev);
380 fail_mode_config_cleanup:
381 drm_mode_config_cleanup(dev);
383 fail_component_cleanup:
384 if (priv->is_componentized)
385 component_unbind_all(dev->dev, dev);
387 fail_external_cleanup:
388 tilcdc_remove_external_encoders(dev);
390 fail_cpufreq_unregister:
391 pm_runtime_disable(dev->dev);
392 #ifdef CONFIG_CPU_FREQ
393 cpufreq_unregister_notifier(&priv->freq_transition,
394 CPUFREQ_TRANSITION_NOTIFIER);
404 flush_workqueue(priv->wq);
405 destroy_workqueue(priv->wq);
408 dev->dev_private = NULL;
413 static void tilcdc_lastclose(struct drm_device *dev)
415 struct tilcdc_drm_private *priv = dev->dev_private;
416 drm_fbdev_cma_restore_mode(priv->fbdev);
419 static irqreturn_t tilcdc_irq(int irq, void *arg)
421 struct drm_device *dev = arg;
422 struct tilcdc_drm_private *priv = dev->dev_private;
423 return tilcdc_crtc_irq(priv->crtc);
426 static int tilcdc_enable_vblank(struct drm_device *dev, unsigned int pipe)
431 static void tilcdc_disable_vblank(struct drm_device *dev, unsigned int pipe)
436 #if defined(CONFIG_DEBUG_FS)
437 static const struct {
443 #define REG(rev, save, reg) { #reg, rev, save, reg }
444 /* exists in revision 1: */
445 REG(1, false, LCDC_PID_REG),
446 REG(1, true, LCDC_CTRL_REG),
447 REG(1, false, LCDC_STAT_REG),
448 REG(1, true, LCDC_RASTER_CTRL_REG),
449 REG(1, true, LCDC_RASTER_TIMING_0_REG),
450 REG(1, true, LCDC_RASTER_TIMING_1_REG),
451 REG(1, true, LCDC_RASTER_TIMING_2_REG),
452 REG(1, true, LCDC_DMA_CTRL_REG),
453 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
454 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
455 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
456 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
457 /* new in revision 2: */
458 REG(2, false, LCDC_RAW_STAT_REG),
459 REG(2, false, LCDC_MASKED_STAT_REG),
460 REG(2, true, LCDC_INT_ENABLE_SET_REG),
461 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
462 REG(2, false, LCDC_END_OF_INT_IND_REG),
463 REG(2, true, LCDC_CLK_ENABLE_REG),
469 #ifdef CONFIG_DEBUG_FS
470 static int tilcdc_regs_show(struct seq_file *m, void *arg)
472 struct drm_info_node *node = (struct drm_info_node *) m->private;
473 struct drm_device *dev = node->minor->dev;
474 struct tilcdc_drm_private *priv = dev->dev_private;
477 pm_runtime_get_sync(dev->dev);
479 seq_printf(m, "revision: %d\n", priv->rev);
481 for (i = 0; i < ARRAY_SIZE(registers); i++)
482 if (priv->rev >= registers[i].rev)
483 seq_printf(m, "%s:\t %08x\n", registers[i].name,
484 tilcdc_read(dev, registers[i].reg));
486 pm_runtime_put_sync(dev->dev);
491 static int tilcdc_mm_show(struct seq_file *m, void *arg)
493 struct drm_info_node *node = (struct drm_info_node *) m->private;
494 struct drm_device *dev = node->minor->dev;
495 return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
498 static struct drm_info_list tilcdc_debugfs_list[] = {
499 { "regs", tilcdc_regs_show, 0 },
500 { "mm", tilcdc_mm_show, 0 },
501 { "fb", drm_fb_cma_debugfs_show, 0 },
504 static int tilcdc_debugfs_init(struct drm_minor *minor)
506 struct drm_device *dev = minor->dev;
507 struct tilcdc_module *mod;
510 ret = drm_debugfs_create_files(tilcdc_debugfs_list,
511 ARRAY_SIZE(tilcdc_debugfs_list),
512 minor->debugfs_root, minor);
514 list_for_each_entry(mod, &module_list, list)
515 if (mod->funcs->debugfs_init)
516 mod->funcs->debugfs_init(mod, minor);
519 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
526 static void tilcdc_debugfs_cleanup(struct drm_minor *minor)
528 struct tilcdc_module *mod;
529 drm_debugfs_remove_files(tilcdc_debugfs_list,
530 ARRAY_SIZE(tilcdc_debugfs_list), minor);
532 list_for_each_entry(mod, &module_list, list)
533 if (mod->funcs->debugfs_cleanup)
534 mod->funcs->debugfs_cleanup(mod, minor);
538 static const struct file_operations fops = {
539 .owner = THIS_MODULE,
541 .release = drm_release,
542 .unlocked_ioctl = drm_ioctl,
544 .compat_ioctl = drm_compat_ioctl,
549 .mmap = drm_gem_cma_mmap,
552 static struct drm_driver tilcdc_driver = {
553 .driver_features = (DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET |
554 DRIVER_PRIME | DRIVER_ATOMIC),
556 .unload = tilcdc_unload,
557 .lastclose = tilcdc_lastclose,
558 .irq_handler = tilcdc_irq,
559 .get_vblank_counter = drm_vblank_no_hw_counter,
560 .enable_vblank = tilcdc_enable_vblank,
561 .disable_vblank = tilcdc_disable_vblank,
562 .gem_free_object_unlocked = drm_gem_cma_free_object,
563 .gem_vm_ops = &drm_gem_cma_vm_ops,
564 .dumb_create = drm_gem_cma_dumb_create,
565 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
566 .dumb_destroy = drm_gem_dumb_destroy,
568 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
569 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
570 .gem_prime_import = drm_gem_prime_import,
571 .gem_prime_export = drm_gem_prime_export,
572 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
573 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
574 .gem_prime_vmap = drm_gem_cma_prime_vmap,
575 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
576 .gem_prime_mmap = drm_gem_cma_prime_mmap,
577 #ifdef CONFIG_DEBUG_FS
578 .debugfs_init = tilcdc_debugfs_init,
579 .debugfs_cleanup = tilcdc_debugfs_cleanup,
583 .desc = "TI LCD Controller DRM",
593 #ifdef CONFIG_PM_SLEEP
594 static int tilcdc_pm_suspend(struct device *dev)
596 struct drm_device *ddev = dev_get_drvdata(dev);
597 struct tilcdc_drm_private *priv = ddev->dev_private;
599 priv->saved_state = drm_atomic_helper_suspend(ddev);
601 /* Select sleep pin state */
602 pinctrl_pm_select_sleep_state(dev);
607 static int tilcdc_pm_resume(struct device *dev)
609 struct drm_device *ddev = dev_get_drvdata(dev);
610 struct tilcdc_drm_private *priv = ddev->dev_private;
613 /* Select default pin state */
614 pinctrl_pm_select_default_state(dev);
616 if (priv->saved_state)
617 ret = drm_atomic_helper_resume(ddev, priv->saved_state);
623 static const struct dev_pm_ops tilcdc_pm_ops = {
624 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
631 static int tilcdc_bind(struct device *dev)
633 return drm_platform_init(&tilcdc_driver, to_platform_device(dev));
636 static void tilcdc_unbind(struct device *dev)
638 struct drm_device *ddev = dev_get_drvdata(dev);
640 /* Check if a subcomponent has already triggered the unloading. */
641 if (!ddev->dev_private)
644 drm_put_dev(dev_get_drvdata(dev));
647 static const struct component_master_ops tilcdc_comp_ops = {
649 .unbind = tilcdc_unbind,
652 static int tilcdc_pdev_probe(struct platform_device *pdev)
654 struct component_match *match = NULL;
657 /* bail out early if no DT data: */
658 if (!pdev->dev.of_node) {
659 dev_err(&pdev->dev, "device-tree data is missing\n");
663 ret = tilcdc_get_external_components(&pdev->dev, &match);
667 return drm_platform_init(&tilcdc_driver, pdev);
669 return component_master_add_with_match(&pdev->dev,
674 static int tilcdc_pdev_remove(struct platform_device *pdev)
678 ret = tilcdc_get_external_components(&pdev->dev, NULL);
682 drm_put_dev(platform_get_drvdata(pdev));
684 component_master_del(&pdev->dev, &tilcdc_comp_ops);
689 static struct of_device_id tilcdc_of_match[] = {
690 { .compatible = "ti,am33xx-tilcdc", },
693 MODULE_DEVICE_TABLE(of, tilcdc_of_match);
695 static struct platform_driver tilcdc_platform_driver = {
696 .probe = tilcdc_pdev_probe,
697 .remove = tilcdc_pdev_remove,
700 .pm = &tilcdc_pm_ops,
701 .of_match_table = tilcdc_of_match,
705 static int __init tilcdc_drm_init(void)
708 tilcdc_tfp410_init();
710 return platform_driver_register(&tilcdc_platform_driver);
713 static void __exit tilcdc_drm_fini(void)
716 platform_driver_unregister(&tilcdc_platform_driver);
718 tilcdc_tfp410_fini();
721 module_init(tilcdc_drm_init);
722 module_exit(tilcdc_drm_fini);
724 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
725 MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
726 MODULE_LICENSE("GPL");