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>
22 #include "tilcdc_drv.h"
23 #include "tilcdc_regs.h"
24 #include "tilcdc_tfp410.h"
25 #include "tilcdc_panel.h"
26 #include "tilcdc_external.h"
28 #include "drm_fb_helper.h"
30 static LIST_HEAD(module_list
);
32 void tilcdc_module_init(struct tilcdc_module
*mod
, const char *name
,
33 const struct tilcdc_module_ops
*funcs
)
37 INIT_LIST_HEAD(&mod
->list
);
38 list_add(&mod
->list
, &module_list
);
41 void tilcdc_module_cleanup(struct tilcdc_module
*mod
)
46 static struct of_device_id tilcdc_of_match
[];
48 static struct drm_framebuffer
*tilcdc_fb_create(struct drm_device
*dev
,
49 struct drm_file
*file_priv
, const struct drm_mode_fb_cmd2
*mode_cmd
)
51 return drm_fb_cma_create(dev
, file_priv
, mode_cmd
);
54 static void tilcdc_fb_output_poll_changed(struct drm_device
*dev
)
56 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
57 drm_fbdev_cma_hotplug_event(priv
->fbdev
);
60 static const struct drm_mode_config_funcs mode_config_funcs
= {
61 .fb_create
= tilcdc_fb_create
,
62 .output_poll_changed
= tilcdc_fb_output_poll_changed
,
65 static int modeset_init(struct drm_device
*dev
)
67 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
68 struct tilcdc_module
*mod
;
70 drm_mode_config_init(dev
);
72 priv
->crtc
= tilcdc_crtc_create(dev
);
74 list_for_each_entry(mod
, &module_list
, list
) {
75 DBG("loading module: %s", mod
->name
);
76 mod
->funcs
->modeset_init(mod
, dev
);
79 dev
->mode_config
.min_width
= 0;
80 dev
->mode_config
.min_height
= 0;
81 dev
->mode_config
.max_width
= tilcdc_crtc_max_width(priv
->crtc
);
82 dev
->mode_config
.max_height
= 2048;
83 dev
->mode_config
.funcs
= &mode_config_funcs
;
88 #ifdef CONFIG_CPU_FREQ
89 static int cpufreq_transition(struct notifier_block
*nb
,
90 unsigned long val
, void *data
)
92 struct tilcdc_drm_private
*priv
= container_of(nb
,
93 struct tilcdc_drm_private
, freq_transition
);
94 if (val
== CPUFREQ_POSTCHANGE
) {
95 if (priv
->lcd_fck_rate
!= clk_get_rate(priv
->clk
)) {
96 priv
->lcd_fck_rate
= clk_get_rate(priv
->clk
);
97 tilcdc_crtc_update_clk(priv
->crtc
);
109 static int tilcdc_unload(struct drm_device
*dev
)
111 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
113 tilcdc_remove_external_encoders(dev
);
115 drm_fbdev_cma_fini(priv
->fbdev
);
116 drm_kms_helper_poll_fini(dev
);
117 drm_mode_config_cleanup(dev
);
118 drm_vblank_cleanup(dev
);
120 pm_runtime_get_sync(dev
->dev
);
121 drm_irq_uninstall(dev
);
122 pm_runtime_put_sync(dev
->dev
);
124 #ifdef CONFIG_CPU_FREQ
125 cpufreq_unregister_notifier(&priv
->freq_transition
,
126 CPUFREQ_TRANSITION_NOTIFIER
);
135 flush_workqueue(priv
->wq
);
136 destroy_workqueue(priv
->wq
);
138 dev
->dev_private
= NULL
;
140 pm_runtime_disable(dev
->dev
);
147 static int tilcdc_load(struct drm_device
*dev
, unsigned long flags
)
149 struct platform_device
*pdev
= dev
->platformdev
;
150 struct device_node
*node
= pdev
->dev
.of_node
;
151 struct tilcdc_drm_private
*priv
;
152 struct tilcdc_module
*mod
;
153 struct resource
*res
;
157 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
159 dev_err(dev
->dev
, "failed to allocate private data\n");
163 dev
->dev_private
= priv
;
165 priv
->is_componentized
=
166 tilcdc_get_external_components(dev
->dev
, NULL
) > 0;
168 priv
->wq
= alloc_ordered_workqueue("tilcdc", 0);
174 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
176 dev_err(dev
->dev
, "failed to get memory resource\n");
181 priv
->mmio
= ioremap_nocache(res
->start
, resource_size(res
));
183 dev_err(dev
->dev
, "failed to ioremap\n");
188 priv
->clk
= clk_get(dev
->dev
, "fck");
189 if (IS_ERR(priv
->clk
)) {
190 dev_err(dev
->dev
, "failed to get functional clock\n");
195 priv
->disp_clk
= clk_get(dev
->dev
, "dpll_disp_ck");
196 if (IS_ERR(priv
->clk
)) {
197 dev_err(dev
->dev
, "failed to get display clock\n");
202 #ifdef CONFIG_CPU_FREQ
203 priv
->lcd_fck_rate
= clk_get_rate(priv
->clk
);
204 priv
->freq_transition
.notifier_call
= cpufreq_transition
;
205 ret
= cpufreq_register_notifier(&priv
->freq_transition
,
206 CPUFREQ_TRANSITION_NOTIFIER
);
208 dev_err(dev
->dev
, "failed to register cpufreq notifier\n");
209 goto fail_put_disp_clk
;
213 if (of_property_read_u32(node
, "max-bandwidth", &priv
->max_bandwidth
))
214 priv
->max_bandwidth
= TILCDC_DEFAULT_MAX_BANDWIDTH
;
216 DBG("Maximum Bandwidth Value %d", priv
->max_bandwidth
);
218 if (of_property_read_u32(node
, "ti,max-width", &priv
->max_width
))
219 priv
->max_width
= TILCDC_DEFAULT_MAX_WIDTH
;
221 DBG("Maximum Horizontal Pixel Width Value %dpixels", priv
->max_width
);
223 if (of_property_read_u32(node
, "ti,max-pixelclock",
224 &priv
->max_pixelclock
))
225 priv
->max_pixelclock
= TILCDC_DEFAULT_MAX_PIXELCLOCK
;
227 DBG("Maximum Pixel Clock Value %dKHz", priv
->max_pixelclock
);
229 pm_runtime_enable(dev
->dev
);
230 pm_runtime_irq_safe(dev
->dev
);
232 /* Determine LCD IP Version */
233 pm_runtime_get_sync(dev
->dev
);
234 switch (tilcdc_read(dev
, LCDC_PID_REG
)) {
243 dev_warn(dev
->dev
, "Unknown PID Reg value 0x%08x, "
244 "defaulting to LCD revision 1\n",
245 tilcdc_read(dev
, LCDC_PID_REG
));
250 pm_runtime_put_sync(dev
->dev
);
252 ret
= modeset_init(dev
);
254 dev_err(dev
->dev
, "failed to initialize mode setting\n");
255 goto fail_cpufreq_unregister
;
258 platform_set_drvdata(pdev
, dev
);
260 if (priv
->is_componentized
) {
261 ret
= component_bind_all(dev
->dev
, dev
);
263 goto fail_mode_config_cleanup
;
265 ret
= tilcdc_add_external_encoders(dev
, &bpp
);
267 goto fail_component_cleanup
;
270 if ((priv
->num_encoders
== 0) || (priv
->num_connectors
== 0)) {
271 dev_err(dev
->dev
, "no encoders/connectors found\n");
273 goto fail_external_cleanup
;
276 ret
= drm_vblank_init(dev
, 1);
278 dev_err(dev
->dev
, "failed to initialize vblank\n");
279 goto fail_external_cleanup
;
282 pm_runtime_get_sync(dev
->dev
);
283 ret
= drm_irq_install(dev
, platform_get_irq(dev
->platformdev
, 0));
284 pm_runtime_put_sync(dev
->dev
);
286 dev_err(dev
->dev
, "failed to install IRQ handler\n");
287 goto fail_vblank_cleanup
;
290 list_for_each_entry(mod
, &module_list
, list
) {
291 DBG("%s: preferred_bpp: %d", mod
->name
, mod
->preferred_bpp
);
292 bpp
= mod
->preferred_bpp
;
297 drm_helper_disable_unused_functions(dev
);
298 priv
->fbdev
= drm_fbdev_cma_init(dev
, bpp
,
299 dev
->mode_config
.num_crtc
,
300 dev
->mode_config
.num_connector
);
301 if (IS_ERR(priv
->fbdev
)) {
302 ret
= PTR_ERR(priv
->fbdev
);
303 goto fail_irq_uninstall
;
306 drm_kms_helper_poll_init(dev
);
311 pm_runtime_get_sync(dev
->dev
);
312 drm_irq_uninstall(dev
);
313 pm_runtime_put_sync(dev
->dev
);
316 drm_vblank_cleanup(dev
);
318 fail_mode_config_cleanup
:
319 drm_mode_config_cleanup(dev
);
321 fail_component_cleanup
:
322 if (priv
->is_componentized
)
323 component_unbind_all(dev
->dev
, dev
);
325 fail_external_cleanup
:
326 tilcdc_remove_external_encoders(dev
);
328 fail_cpufreq_unregister
:
329 pm_runtime_disable(dev
->dev
);
330 #ifdef CONFIG_CPU_FREQ
331 cpufreq_unregister_notifier(&priv
->freq_transition
,
332 CPUFREQ_TRANSITION_NOTIFIER
);
334 clk_put(priv
->disp_clk
);
344 flush_workqueue(priv
->wq
);
345 destroy_workqueue(priv
->wq
);
348 dev
->dev_private
= NULL
;
353 static void tilcdc_preclose(struct drm_device
*dev
, struct drm_file
*file
)
355 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
357 tilcdc_crtc_cancel_page_flip(priv
->crtc
, file
);
360 static void tilcdc_lastclose(struct drm_device
*dev
)
362 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
363 drm_fbdev_cma_restore_mode(priv
->fbdev
);
366 static irqreturn_t
tilcdc_irq(int irq
, void *arg
)
368 struct drm_device
*dev
= arg
;
369 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
370 return tilcdc_crtc_irq(priv
->crtc
);
373 static void tilcdc_irq_preinstall(struct drm_device
*dev
)
375 tilcdc_clear_irqstatus(dev
, 0xffffffff);
378 static int tilcdc_irq_postinstall(struct drm_device
*dev
)
380 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
382 /* enable FIFO underflow irq: */
384 tilcdc_set(dev
, LCDC_RASTER_CTRL_REG
, LCDC_V1_UNDERFLOW_INT_ENA
);
386 tilcdc_set(dev
, LCDC_INT_ENABLE_SET_REG
, LCDC_V2_UNDERFLOW_INT_ENA
);
391 static void tilcdc_irq_uninstall(struct drm_device
*dev
)
393 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
395 /* disable irqs that we might have enabled: */
396 if (priv
->rev
== 1) {
397 tilcdc_clear(dev
, LCDC_RASTER_CTRL_REG
,
398 LCDC_V1_UNDERFLOW_INT_ENA
| LCDC_V1_PL_INT_ENA
);
399 tilcdc_clear(dev
, LCDC_DMA_CTRL_REG
, LCDC_V1_END_OF_FRAME_INT_ENA
);
401 tilcdc_clear(dev
, LCDC_INT_ENABLE_SET_REG
,
402 LCDC_V2_UNDERFLOW_INT_ENA
| LCDC_V2_PL_INT_ENA
|
403 LCDC_V2_END_OF_FRAME0_INT_ENA
| LCDC_V2_END_OF_FRAME1_INT_ENA
|
409 static void enable_vblank(struct drm_device
*dev
, bool enable
)
411 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
414 if (priv
->rev
== 1) {
415 reg
= LCDC_DMA_CTRL_REG
;
416 mask
= LCDC_V1_END_OF_FRAME_INT_ENA
;
418 reg
= LCDC_INT_ENABLE_SET_REG
;
419 mask
= LCDC_V2_END_OF_FRAME0_INT_ENA
|
420 LCDC_V2_END_OF_FRAME1_INT_ENA
| LCDC_FRAME_DONE
;
424 tilcdc_set(dev
, reg
, mask
);
426 tilcdc_clear(dev
, reg
, mask
);
429 static int tilcdc_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
431 enable_vblank(dev
, true);
435 static void tilcdc_disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
437 enable_vblank(dev
, false);
440 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP)
441 static const struct {
447 #define REG(rev, save, reg) { #reg, rev, save, reg }
448 /* exists in revision 1: */
449 REG(1, false, LCDC_PID_REG
),
450 REG(1, true, LCDC_CTRL_REG
),
451 REG(1, false, LCDC_STAT_REG
),
452 REG(1, true, LCDC_RASTER_CTRL_REG
),
453 REG(1, true, LCDC_RASTER_TIMING_0_REG
),
454 REG(1, true, LCDC_RASTER_TIMING_1_REG
),
455 REG(1, true, LCDC_RASTER_TIMING_2_REG
),
456 REG(1, true, LCDC_DMA_CTRL_REG
),
457 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG
),
458 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG
),
459 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG
),
460 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG
),
461 /* new in revision 2: */
462 REG(2, false, LCDC_RAW_STAT_REG
),
463 REG(2, false, LCDC_MASKED_STAT_REG
),
464 REG(2, false, LCDC_INT_ENABLE_SET_REG
),
465 REG(2, false, LCDC_INT_ENABLE_CLR_REG
),
466 REG(2, false, LCDC_END_OF_INT_IND_REG
),
467 REG(2, true, LCDC_CLK_ENABLE_REG
),
468 REG(2, true, LCDC_INT_ENABLE_SET_REG
),
473 #ifdef CONFIG_DEBUG_FS
474 static int tilcdc_regs_show(struct seq_file
*m
, void *arg
)
476 struct drm_info_node
*node
= (struct drm_info_node
*) m
->private;
477 struct drm_device
*dev
= node
->minor
->dev
;
478 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
481 pm_runtime_get_sync(dev
->dev
);
483 seq_printf(m
, "revision: %d\n", priv
->rev
);
485 for (i
= 0; i
< ARRAY_SIZE(registers
); i
++)
486 if (priv
->rev
>= registers
[i
].rev
)
487 seq_printf(m
, "%s:\t %08x\n", registers
[i
].name
,
488 tilcdc_read(dev
, registers
[i
].reg
));
490 pm_runtime_put_sync(dev
->dev
);
495 static int tilcdc_mm_show(struct seq_file
*m
, void *arg
)
497 struct drm_info_node
*node
= (struct drm_info_node
*) m
->private;
498 struct drm_device
*dev
= node
->minor
->dev
;
499 return drm_mm_dump_table(m
, &dev
->vma_offset_manager
->vm_addr_space_mm
);
502 static struct drm_info_list tilcdc_debugfs_list
[] = {
503 { "regs", tilcdc_regs_show
, 0 },
504 { "mm", tilcdc_mm_show
, 0 },
505 { "fb", drm_fb_cma_debugfs_show
, 0 },
508 static int tilcdc_debugfs_init(struct drm_minor
*minor
)
510 struct drm_device
*dev
= minor
->dev
;
511 struct tilcdc_module
*mod
;
514 ret
= drm_debugfs_create_files(tilcdc_debugfs_list
,
515 ARRAY_SIZE(tilcdc_debugfs_list
),
516 minor
->debugfs_root
, minor
);
518 list_for_each_entry(mod
, &module_list
, list
)
519 if (mod
->funcs
->debugfs_init
)
520 mod
->funcs
->debugfs_init(mod
, minor
);
523 dev_err(dev
->dev
, "could not install tilcdc_debugfs_list\n");
530 static void tilcdc_debugfs_cleanup(struct drm_minor
*minor
)
532 struct tilcdc_module
*mod
;
533 drm_debugfs_remove_files(tilcdc_debugfs_list
,
534 ARRAY_SIZE(tilcdc_debugfs_list
), minor
);
536 list_for_each_entry(mod
, &module_list
, list
)
537 if (mod
->funcs
->debugfs_cleanup
)
538 mod
->funcs
->debugfs_cleanup(mod
, minor
);
542 static const struct file_operations fops
= {
543 .owner
= THIS_MODULE
,
545 .release
= drm_release
,
546 .unlocked_ioctl
= drm_ioctl
,
548 .compat_ioctl
= drm_compat_ioctl
,
553 .mmap
= drm_gem_cma_mmap
,
556 static struct drm_driver tilcdc_driver
= {
557 .driver_features
= DRIVER_HAVE_IRQ
| DRIVER_GEM
| DRIVER_MODESET
,
559 .unload
= tilcdc_unload
,
560 .preclose
= tilcdc_preclose
,
561 .lastclose
= tilcdc_lastclose
,
562 .set_busid
= drm_platform_set_busid
,
563 .irq_handler
= tilcdc_irq
,
564 .irq_preinstall
= tilcdc_irq_preinstall
,
565 .irq_postinstall
= tilcdc_irq_postinstall
,
566 .irq_uninstall
= tilcdc_irq_uninstall
,
567 .get_vblank_counter
= drm_vblank_no_hw_counter
,
568 .enable_vblank
= tilcdc_enable_vblank
,
569 .disable_vblank
= tilcdc_disable_vblank
,
570 .gem_free_object
= drm_gem_cma_free_object
,
571 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
572 .dumb_create
= drm_gem_cma_dumb_create
,
573 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
574 .dumb_destroy
= drm_gem_dumb_destroy
,
575 #ifdef CONFIG_DEBUG_FS
576 .debugfs_init
= tilcdc_debugfs_init
,
577 .debugfs_cleanup
= tilcdc_debugfs_cleanup
,
581 .desc
= "TI LCD Controller DRM",
591 #ifdef CONFIG_PM_SLEEP
592 static int tilcdc_pm_suspend(struct device
*dev
)
594 struct drm_device
*ddev
= dev_get_drvdata(dev
);
595 struct tilcdc_drm_private
*priv
= ddev
->dev_private
;
598 drm_kms_helper_poll_disable(ddev
);
600 /* Save register state: */
601 for (i
= 0; i
< ARRAY_SIZE(registers
); i
++)
602 if (registers
[i
].save
&& (priv
->rev
>= registers
[i
].rev
))
603 priv
->saved_register
[n
++] = tilcdc_read(ddev
, registers
[i
].reg
);
608 static int tilcdc_pm_resume(struct device
*dev
)
610 struct drm_device
*ddev
= dev_get_drvdata(dev
);
611 struct tilcdc_drm_private
*priv
= ddev
->dev_private
;
614 /* Restore register state: */
615 for (i
= 0; i
< ARRAY_SIZE(registers
); i
++)
616 if (registers
[i
].save
&& (priv
->rev
>= registers
[i
].rev
))
617 tilcdc_write(ddev
, registers
[i
].reg
, priv
->saved_register
[n
++]);
619 drm_kms_helper_poll_enable(ddev
);
625 static const struct dev_pm_ops tilcdc_pm_ops
= {
626 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend
, tilcdc_pm_resume
)
633 static int tilcdc_bind(struct device
*dev
)
635 return drm_platform_init(&tilcdc_driver
, to_platform_device(dev
));
638 static void tilcdc_unbind(struct device
*dev
)
640 drm_put_dev(dev_get_drvdata(dev
));
643 static const struct component_master_ops tilcdc_comp_ops
= {
645 .unbind
= tilcdc_unbind
,
648 static int tilcdc_pdev_probe(struct platform_device
*pdev
)
650 struct component_match
*match
= NULL
;
653 /* bail out early if no DT data: */
654 if (!pdev
->dev
.of_node
) {
655 dev_err(&pdev
->dev
, "device-tree data is missing\n");
659 ret
= tilcdc_get_external_components(&pdev
->dev
, &match
);
663 return drm_platform_init(&tilcdc_driver
, pdev
);
665 return component_master_add_with_match(&pdev
->dev
,
670 static int tilcdc_pdev_remove(struct platform_device
*pdev
)
672 struct drm_device
*ddev
= dev_get_drvdata(&pdev
->dev
);
673 struct tilcdc_drm_private
*priv
= ddev
->dev_private
;
675 /* Check if a subcomponent has already triggered the unloading. */
679 if (priv
->is_componentized
)
680 component_master_del(&pdev
->dev
, &tilcdc_comp_ops
);
682 drm_put_dev(platform_get_drvdata(pdev
));
687 static struct of_device_id tilcdc_of_match
[] = {
688 { .compatible
= "ti,am33xx-tilcdc", },
691 MODULE_DEVICE_TABLE(of
, tilcdc_of_match
);
693 static struct platform_driver tilcdc_platform_driver
= {
694 .probe
= tilcdc_pdev_probe
,
695 .remove
= tilcdc_pdev_remove
,
698 .pm
= &tilcdc_pm_ops
,
699 .of_match_table
= tilcdc_of_match
,
703 static int __init
tilcdc_drm_init(void)
706 tilcdc_tfp410_init();
708 return platform_driver_register(&tilcdc_platform_driver
);
711 static void __exit
tilcdc_drm_fini(void)
714 platform_driver_unregister(&tilcdc_platform_driver
);
716 tilcdc_tfp410_fini();
719 module_init(tilcdc_drm_init
);
720 module_exit(tilcdc_drm_fini
);
722 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
723 MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
724 MODULE_LICENSE("GPL");