2 * linux/drivers/video/omap2/dss/core.c
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
7 * Some code and ideas taken from drivers/video/omap/ driver
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
23 #define DSS_SUBSYS_NAME "CORE"
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/clk.h>
28 #include <linux/err.h>
29 #include <linux/platform_device.h>
30 #include <linux/seq_file.h>
31 #include <linux/debugfs.h>
33 #include <linux/device.h>
35 #include <plat/display.h>
36 #include <plat/clock.h>
41 struct platform_device *pdev;
47 struct clk *dss_54m_fck;
48 struct clk *dss_96m_fck;
49 unsigned num_clks_enabled;
52 static void dss_clk_enable_all_no_ctx(void);
53 static void dss_clk_disable_all_no_ctx(void);
54 static void dss_clk_enable_no_ctx(enum dss_clock clks);
55 static void dss_clk_disable_no_ctx(enum dss_clock clks);
57 static char *def_disp_name;
58 module_param_named(def_disp, def_disp_name, charp, 0);
59 MODULE_PARM_DESC(def_disp_name, "default display name");
62 unsigned int dss_debug;
63 module_param_named(debug, dss_debug, bool, 0644);
67 static int dss_get_ctx_id(void)
69 struct omap_dss_board_info *pdata = core.pdev->dev.platform_data;
72 if (!pdata->get_last_off_on_transaction_id)
74 r = pdata->get_last_off_on_transaction_id(&core.pdev->dev);
76 dev_err(&core.pdev->dev, "getting transaction ID failed, "
77 "will force context restore\n");
83 int dss_need_ctx_restore(void)
85 int id = dss_get_ctx_id();
87 if (id < 0 || id != core.ctx_id) {
88 DSSDBG("ctx id %d -> id %d\n",
97 static void save_all_ctx(void)
99 DSSDBG("save context\n");
101 dss_clk_enable_no_ctx(DSS_CLK_ICK | DSS_CLK_FCK1);
104 dispc_save_context();
105 #ifdef CONFIG_OMAP2_DSS_DSI
109 dss_clk_disable_no_ctx(DSS_CLK_ICK | DSS_CLK_FCK1);
112 static void restore_all_ctx(void)
114 DSSDBG("restore context\n");
116 dss_clk_enable_all_no_ctx();
118 dss_restore_context();
119 dispc_restore_context();
120 #ifdef CONFIG_OMAP2_DSS_DSI
121 dsi_restore_context();
124 dss_clk_disable_all_no_ctx();
127 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_OMAP2_DSS_DEBUG_SUPPORT)
129 static void core_dump_clocks(struct seq_file *s)
132 struct clk *clocks[5] = {
140 seq_printf(s, "- CORE -\n");
142 seq_printf(s, "internal clk count\t\t%u\n", core.num_clks_enabled);
144 for (i = 0; i < 5; i++) {
147 seq_printf(s, "%-15s\t%lu\t%d\n",
149 clk_get_rate(clocks[i]),
150 clocks[i]->usecount);
153 #endif /* defined(CONFIG_DEBUG_FS) && defined(CONFIG_OMAP2_DSS_DEBUG_SUPPORT) */
155 static int dss_get_clock(struct clk **clock, const char *clk_name)
159 clk = clk_get(&core.pdev->dev, clk_name);
162 DSSERR("can't get clock %s", clk_name);
168 DSSDBG("clk %s, rate %ld\n", clk_name, clk_get_rate(clk));
173 static int dss_get_clocks(void)
178 core.dss1_fck = NULL;
179 core.dss2_fck = NULL;
180 core.dss_54m_fck = NULL;
181 core.dss_96m_fck = NULL;
183 r = dss_get_clock(&core.dss_ick, "ick");
187 r = dss_get_clock(&core.dss1_fck, "dss1_fck");
191 r = dss_get_clock(&core.dss2_fck, "dss2_fck");
195 r = dss_get_clock(&core.dss_54m_fck, "tv_fck");
199 r = dss_get_clock(&core.dss_96m_fck, "video_fck");
207 clk_put(core.dss_ick);
209 clk_put(core.dss1_fck);
211 clk_put(core.dss2_fck);
212 if (core.dss_54m_fck)
213 clk_put(core.dss_54m_fck);
214 if (core.dss_96m_fck)
215 clk_put(core.dss_96m_fck);
220 static void dss_put_clocks(void)
222 if (core.dss_96m_fck)
223 clk_put(core.dss_96m_fck);
224 clk_put(core.dss_54m_fck);
225 clk_put(core.dss1_fck);
226 clk_put(core.dss2_fck);
227 clk_put(core.dss_ick);
230 unsigned long dss_clk_get_rate(enum dss_clock clk)
234 return clk_get_rate(core.dss_ick);
236 return clk_get_rate(core.dss1_fck);
238 return clk_get_rate(core.dss2_fck);
240 return clk_get_rate(core.dss_54m_fck);
242 return clk_get_rate(core.dss_96m_fck);
249 static unsigned count_clk_bits(enum dss_clock clks)
251 unsigned num_clks = 0;
253 if (clks & DSS_CLK_ICK)
255 if (clks & DSS_CLK_FCK1)
257 if (clks & DSS_CLK_FCK2)
259 if (clks & DSS_CLK_54M)
261 if (clks & DSS_CLK_96M)
267 static void dss_clk_enable_no_ctx(enum dss_clock clks)
269 unsigned num_clks = count_clk_bits(clks);
271 if (clks & DSS_CLK_ICK)
272 clk_enable(core.dss_ick);
273 if (clks & DSS_CLK_FCK1)
274 clk_enable(core.dss1_fck);
275 if (clks & DSS_CLK_FCK2)
276 clk_enable(core.dss2_fck);
277 if (clks & DSS_CLK_54M)
278 clk_enable(core.dss_54m_fck);
279 if (clks & DSS_CLK_96M)
280 clk_enable(core.dss_96m_fck);
282 core.num_clks_enabled += num_clks;
285 void dss_clk_enable(enum dss_clock clks)
287 dss_clk_enable_no_ctx(clks);
289 if (cpu_is_omap34xx() && dss_need_ctx_restore())
293 static void dss_clk_disable_no_ctx(enum dss_clock clks)
295 unsigned num_clks = count_clk_bits(clks);
297 if (clks & DSS_CLK_ICK)
298 clk_disable(core.dss_ick);
299 if (clks & DSS_CLK_FCK1)
300 clk_disable(core.dss1_fck);
301 if (clks & DSS_CLK_FCK2)
302 clk_disable(core.dss2_fck);
303 if (clks & DSS_CLK_54M)
304 clk_disable(core.dss_54m_fck);
305 if (clks & DSS_CLK_96M)
306 clk_disable(core.dss_96m_fck);
308 core.num_clks_enabled -= num_clks;
311 void dss_clk_disable(enum dss_clock clks)
313 if (cpu_is_omap34xx()) {
314 unsigned num_clks = count_clk_bits(clks);
316 BUG_ON(core.num_clks_enabled < num_clks);
318 if (core.num_clks_enabled == num_clks)
322 dss_clk_disable_no_ctx(clks);
325 static void dss_clk_enable_all_no_ctx(void)
329 clks = DSS_CLK_ICK | DSS_CLK_FCK1 | DSS_CLK_FCK2 | DSS_CLK_54M;
330 if (cpu_is_omap34xx())
332 dss_clk_enable_no_ctx(clks);
335 static void dss_clk_disable_all_no_ctx(void)
339 clks = DSS_CLK_ICK | DSS_CLK_FCK1 | DSS_CLK_FCK2 | DSS_CLK_54M;
340 if (cpu_is_omap34xx())
342 dss_clk_disable_no_ctx(clks);
345 static void dss_clk_disable_all(void)
349 clks = DSS_CLK_ICK | DSS_CLK_FCK1 | DSS_CLK_FCK2 | DSS_CLK_54M;
350 if (cpu_is_omap34xx())
352 dss_clk_disable(clks);
356 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_OMAP2_DSS_DEBUG_SUPPORT)
357 static void dss_debug_dump_clocks(struct seq_file *s)
361 dispc_dump_clocks(s);
362 #ifdef CONFIG_OMAP2_DSS_DSI
367 static int dss_debug_show(struct seq_file *s, void *unused)
369 void (*func)(struct seq_file *) = s->private;
374 static int dss_debug_open(struct inode *inode, struct file *file)
376 return single_open(file, dss_debug_show, inode->i_private);
379 static const struct file_operations dss_debug_fops = {
380 .open = dss_debug_open,
383 .release = single_release,
386 static struct dentry *dss_debugfs_dir;
388 static int dss_initialize_debugfs(void)
390 dss_debugfs_dir = debugfs_create_dir("omapdss", NULL);
391 if (IS_ERR(dss_debugfs_dir)) {
392 int err = PTR_ERR(dss_debugfs_dir);
393 dss_debugfs_dir = NULL;
397 debugfs_create_file("clk", S_IRUGO, dss_debugfs_dir,
398 &dss_debug_dump_clocks, &dss_debug_fops);
400 debugfs_create_file("dispc_irq", S_IRUGO, dss_debugfs_dir,
401 &dispc_dump_irqs, &dss_debug_fops);
403 #ifdef CONFIG_OMAP2_DSS_DSI
404 debugfs_create_file("dsi_irq", S_IRUGO, dss_debugfs_dir,
405 &dsi_dump_irqs, &dss_debug_fops);
408 debugfs_create_file("dss", S_IRUGO, dss_debugfs_dir,
409 &dss_dump_regs, &dss_debug_fops);
410 debugfs_create_file("dispc", S_IRUGO, dss_debugfs_dir,
411 &dispc_dump_regs, &dss_debug_fops);
412 #ifdef CONFIG_OMAP2_DSS_RFBI
413 debugfs_create_file("rfbi", S_IRUGO, dss_debugfs_dir,
414 &rfbi_dump_regs, &dss_debug_fops);
416 #ifdef CONFIG_OMAP2_DSS_DSI
417 debugfs_create_file("dsi", S_IRUGO, dss_debugfs_dir,
418 &dsi_dump_regs, &dss_debug_fops);
420 #ifdef CONFIG_OMAP2_DSS_VENC
421 debugfs_create_file("venc", S_IRUGO, dss_debugfs_dir,
422 &venc_dump_regs, &dss_debug_fops);
427 static void dss_uninitialize_debugfs(void)
430 debugfs_remove_recursive(dss_debugfs_dir);
432 #endif /* CONFIG_DEBUG_FS && CONFIG_OMAP2_DSS_DEBUG_SUPPORT */
434 /* PLATFORM DEVICE */
435 static int omap_dss_probe(struct platform_device *pdev)
437 struct omap_dss_board_info *pdata = pdev->dev.platform_data;
444 dss_init_overlay_managers(pdev);
445 dss_init_overlays(pdev);
447 r = dss_get_clocks();
451 dss_clk_enable_all_no_ctx();
453 core.ctx_id = dss_get_ctx_id();
454 DSSDBG("initial ctx id %u\n", core.ctx_id);
456 #ifdef CONFIG_FB_OMAP_BOOTLOADER_INIT
458 if (omap_readl(0x48050440) & 1) /* LCD enabled? */
462 r = dss_init(skip_init);
464 DSSERR("Failed to initialize DSS\n");
468 #ifdef CONFIG_OMAP2_DSS_RFBI
471 DSSERR("Failed to initialize rfbi\n");
478 DSSERR("Failed to initialize dpi\n");
484 DSSERR("Failed to initialize dispc\n");
487 #ifdef CONFIG_OMAP2_DSS_VENC
490 DSSERR("Failed to initialize venc\n");
494 if (cpu_is_omap34xx()) {
495 #ifdef CONFIG_OMAP2_DSS_SDI
496 r = sdi_init(skip_init);
498 DSSERR("Failed to initialize SDI\n");
502 #ifdef CONFIG_OMAP2_DSS_DSI
505 DSSERR("Failed to initialize DSI\n");
511 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_OMAP2_DSS_DEBUG_SUPPORT)
512 r = dss_initialize_debugfs();
517 for (i = 0; i < pdata->num_devices; ++i) {
518 struct omap_dss_device *dssdev = pdata->devices[i];
520 r = omap_dss_register_device(dssdev);
522 DSSERR("device reg failed %d\n", i);
524 if (def_disp_name && strcmp(def_disp_name, dssdev->name) == 0)
525 pdata->default_device = dssdev;
528 dss_clk_disable_all();
532 /* XXX fail correctly */
537 static int omap_dss_remove(struct platform_device *pdev)
539 struct omap_dss_board_info *pdata = pdev->dev.platform_data;
543 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_OMAP2_DSS_DEBUG_SUPPORT)
544 dss_uninitialize_debugfs();
547 #ifdef CONFIG_OMAP2_DSS_VENC
552 #ifdef CONFIG_OMAP2_DSS_RFBI
555 if (cpu_is_omap34xx()) {
556 #ifdef CONFIG_OMAP2_DSS_DSI
559 #ifdef CONFIG_OMAP2_DSS_SDI
566 /* these should be removed at some point */
567 c = core.dss_ick->usecount;
569 DSSERR("warning: dss_ick usecount %d, disabling\n", c);
571 clk_disable(core.dss_ick);
574 c = core.dss1_fck->usecount;
576 DSSERR("warning: dss1_fck usecount %d, disabling\n", c);
578 clk_disable(core.dss1_fck);
581 c = core.dss2_fck->usecount;
583 DSSERR("warning: dss2_fck usecount %d, disabling\n", c);
585 clk_disable(core.dss2_fck);
588 c = core.dss_54m_fck->usecount;
590 DSSERR("warning: dss_54m_fck usecount %d, disabling\n", c);
592 clk_disable(core.dss_54m_fck);
595 if (core.dss_96m_fck) {
596 c = core.dss_96m_fck->usecount;
598 DSSERR("warning: dss_96m_fck usecount %d, disabling\n",
601 clk_disable(core.dss_96m_fck);
607 dss_uninit_overlays(pdev);
608 dss_uninit_overlay_managers(pdev);
610 for (i = 0; i < pdata->num_devices; ++i)
611 omap_dss_unregister_device(pdata->devices[i]);
616 static void omap_dss_shutdown(struct platform_device *pdev)
618 DSSDBG("shutdown\n");
619 dss_disable_all_devices();
622 static int omap_dss_suspend(struct platform_device *pdev, pm_message_t state)
624 DSSDBG("suspend %d\n", state.event);
626 return dss_suspend_all_devices();
629 static int omap_dss_resume(struct platform_device *pdev)
633 return dss_resume_all_devices();
636 static struct platform_driver omap_dss_driver = {
637 .probe = omap_dss_probe,
638 .remove = omap_dss_remove,
639 .shutdown = omap_dss_shutdown,
640 .suspend = omap_dss_suspend,
641 .resume = omap_dss_resume,
644 .owner = THIS_MODULE,
649 static int dss_bus_match(struct device *dev, struct device_driver *driver)
651 struct omap_dss_device *dssdev = to_dss_device(dev);
653 DSSDBG("bus_match. dev %s/%s, drv %s\n",
654 dev_name(dev), dssdev->driver_name, driver->name);
656 return strcmp(dssdev->driver_name, driver->name) == 0;
659 static ssize_t device_name_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct omap_dss_device *dssdev = to_dss_device(dev);
663 return snprintf(buf, PAGE_SIZE, "%s\n",
668 static struct device_attribute default_dev_attrs[] = {
669 __ATTR(name, S_IRUGO, device_name_show, NULL),
673 static ssize_t driver_name_show(struct device_driver *drv, char *buf)
675 struct omap_dss_driver *dssdrv = to_dss_driver(drv);
676 return snprintf(buf, PAGE_SIZE, "%s\n",
677 dssdrv->driver.name ?
678 dssdrv->driver.name : "");
680 static struct driver_attribute default_drv_attrs[] = {
681 __ATTR(name, S_IRUGO, driver_name_show, NULL),
685 static struct bus_type dss_bus_type = {
687 .match = dss_bus_match,
688 .dev_attrs = default_dev_attrs,
689 .drv_attrs = default_drv_attrs,
692 static void dss_bus_release(struct device *dev)
694 DSSDBG("bus_release\n");
697 static struct device dss_bus = {
698 .release = dss_bus_release,
701 struct bus_type *dss_get_bus(void)
703 return &dss_bus_type;
707 static int dss_driver_probe(struct device *dev)
710 struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
711 struct omap_dss_device *dssdev = to_dss_device(dev);
712 struct omap_dss_board_info *pdata = core.pdev->dev.platform_data;
715 DSSDBG("driver_probe: dev %s/%s, drv %s\n",
716 dev_name(dev), dssdev->driver_name,
717 dssdrv->driver.name);
719 dss_init_device(core.pdev, dssdev);
721 /* skip this if the device is behind a ctrl */
722 if (!dssdev->panel.ctrl) {
723 force = pdata->default_device == dssdev;
724 dss_recheck_connections(dssdev, force);
727 r = dssdrv->probe(dssdev);
730 DSSERR("driver probe failed: %d\n", r);
734 DSSDBG("probe done for device %s\n", dev_name(dev));
736 dssdev->driver = dssdrv;
741 static int dss_driver_remove(struct device *dev)
743 struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
744 struct omap_dss_device *dssdev = to_dss_device(dev);
746 DSSDBG("driver_remove: dev %s/%s\n", dev_name(dev),
747 dssdev->driver_name);
749 dssdrv->remove(dssdev);
751 dss_uninit_device(core.pdev, dssdev);
753 dssdev->driver = NULL;
758 int omap_dss_register_driver(struct omap_dss_driver *dssdriver)
760 dssdriver->driver.bus = &dss_bus_type;
761 dssdriver->driver.probe = dss_driver_probe;
762 dssdriver->driver.remove = dss_driver_remove;
763 return driver_register(&dssdriver->driver);
765 EXPORT_SYMBOL(omap_dss_register_driver);
767 void omap_dss_unregister_driver(struct omap_dss_driver *dssdriver)
769 driver_unregister(&dssdriver->driver);
771 EXPORT_SYMBOL(omap_dss_unregister_driver);
774 static void reset_device(struct device *dev, int check)
776 u8 *dev_p = (u8 *)dev;
777 u8 *dev_end = dev_p + sizeof(*dev);
780 saved_pdata = dev->platform_data;
783 * Check if there is any other setting than platform_data
784 * in struct device; warn that these will be reset by our
787 dev->platform_data = NULL;
788 while (dev_p < dev_end) {
790 WARN("%s: struct device fields will be "
798 memset(dev, 0, sizeof(*dev));
799 dev->platform_data = saved_pdata;
803 static void omap_dss_dev_release(struct device *dev)
805 reset_device(dev, 0);
808 int omap_dss_register_device(struct omap_dss_device *dssdev)
811 static int panel_num;
814 WARN_ON(!dssdev->driver_name);
816 reset_device(&dssdev->dev, 1);
817 dssdev->dev.bus = &dss_bus_type;
818 dssdev->dev.parent = &dss_bus;
819 dssdev->dev.release = omap_dss_dev_release;
820 dev_set_name(&dssdev->dev, "display%d", dev_num++);
821 r = device_register(&dssdev->dev);
825 if (dssdev->ctrl.panel) {
826 struct omap_dss_device *panel = dssdev->ctrl.panel;
828 panel->panel.ctrl = dssdev;
830 reset_device(&panel->dev, 1);
831 panel->dev.bus = &dss_bus_type;
832 panel->dev.parent = &dssdev->dev;
833 panel->dev.release = omap_dss_dev_release;
834 dev_set_name(&panel->dev, "panel%d", panel_num++);
835 r = device_register(&panel->dev);
843 void omap_dss_unregister_device(struct omap_dss_device *dssdev)
845 device_unregister(&dssdev->dev);
847 if (dssdev->ctrl.panel) {
848 struct omap_dss_device *panel = dssdev->ctrl.panel;
849 device_unregister(&panel->dev);
854 static int omap_dss_bus_register(void)
858 r = bus_register(&dss_bus_type);
860 DSSERR("bus register failed\n");
864 dev_set_name(&dss_bus, "omapdss");
865 r = device_register(&dss_bus);
867 DSSERR("bus driver register failed\n");
868 bus_unregister(&dss_bus_type);
877 #ifdef CONFIG_OMAP2_DSS_MODULE
878 static void omap_dss_bus_unregister(void)
880 device_unregister(&dss_bus);
882 bus_unregister(&dss_bus_type);
885 static int __init omap_dss_init(void)
889 r = omap_dss_bus_register();
893 r = platform_driver_register(&omap_dss_driver);
895 omap_dss_bus_unregister();
902 static void __exit omap_dss_exit(void)
904 platform_driver_unregister(&omap_dss_driver);
906 omap_dss_bus_unregister();
909 module_init(omap_dss_init);
910 module_exit(omap_dss_exit);
912 static int __init omap_dss_init(void)
914 return omap_dss_bus_register();
917 static int __init omap_dss_init2(void)
919 return platform_driver_register(&omap_dss_driver);
922 core_initcall(omap_dss_init);
923 device_initcall(omap_dss_init2);
926 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
927 MODULE_DESCRIPTION("OMAP2/3 Display Subsystem");
928 MODULE_LICENSE("GPL v2");