2 * linux/drivers/video/omap2/dss/dss.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 "DSS"
25 #include <linux/kernel.h>
27 #include <linux/export.h>
28 #include <linux/err.h>
29 #include <linux/delay.h>
30 #include <linux/seq_file.h>
31 #include <linux/clk.h>
32 #include <linux/platform_device.h>
33 #include <linux/pm_runtime.h>
35 #include <video/omapdss.h>
38 #include <plat/clock.h>
41 #include "dss_features.h"
43 #define DSS_SZ_REGS SZ_512
49 #define DSS_REG(idx) ((const struct dss_reg) { idx })
51 #define DSS_REVISION DSS_REG(0x0000)
52 #define DSS_SYSCONFIG DSS_REG(0x0010)
53 #define DSS_SYSSTATUS DSS_REG(0x0014)
54 #define DSS_CONTROL DSS_REG(0x0040)
55 #define DSS_SDI_CONTROL DSS_REG(0x0044)
56 #define DSS_PLL_CONTROL DSS_REG(0x0048)
57 #define DSS_SDI_STATUS DSS_REG(0x005C)
59 #define REG_GET(idx, start, end) \
60 FLD_GET(dss_read_reg(idx), start, end)
62 #define REG_FLD_MOD(idx, val, start, end) \
63 dss_write_reg(idx, FLD_MOD(dss_read_reg(idx), val, start, end))
66 struct platform_device *pdev;
69 struct clk *dpll4_m4_ck;
72 unsigned long cache_req_pck;
73 unsigned long cache_prate;
74 struct dss_clock_info cache_dss_cinfo;
75 struct dispc_clock_info cache_dispc_cinfo;
77 enum omap_dss_clk_source dsi_clk_source[MAX_NUM_DSI];
78 enum omap_dss_clk_source dispc_clk_source;
79 enum omap_dss_clk_source lcd_clk_source[MAX_DSS_LCD_MANAGERS];
82 u32 ctx[DSS_SZ_REGS / sizeof(u32)];
85 static const char * const dss_generic_clk_source_names[] = {
86 [OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC] = "DSI_PLL_HSDIV_DISPC",
87 [OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI] = "DSI_PLL_HSDIV_DSI",
88 [OMAP_DSS_CLK_SRC_FCK] = "DSS_FCK",
91 static inline void dss_write_reg(const struct dss_reg idx, u32 val)
93 __raw_writel(val, dss.base + idx.idx);
96 static inline u32 dss_read_reg(const struct dss_reg idx)
98 return __raw_readl(dss.base + idx.idx);
102 dss.ctx[(DSS_##reg).idx / sizeof(u32)] = dss_read_reg(DSS_##reg)
104 dss_write_reg(DSS_##reg, dss.ctx[(DSS_##reg).idx / sizeof(u32)])
106 static void dss_save_context(void)
108 DSSDBG("dss_save_context\n");
112 if (dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_LCD) &
113 OMAP_DISPLAY_TYPE_SDI) {
118 dss.ctx_valid = true;
120 DSSDBG("context saved\n");
123 static void dss_restore_context(void)
125 DSSDBG("dss_restore_context\n");
132 if (dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_LCD) &
133 OMAP_DISPLAY_TYPE_SDI) {
138 DSSDBG("context restored\n");
144 void dss_sdi_init(u8 datapairs)
148 BUG_ON(datapairs > 3 || datapairs < 1);
150 l = dss_read_reg(DSS_SDI_CONTROL);
151 l = FLD_MOD(l, 0xf, 19, 15); /* SDI_PDIV */
152 l = FLD_MOD(l, datapairs-1, 3, 2); /* SDI_PRSEL */
153 l = FLD_MOD(l, 2, 1, 0); /* SDI_BWSEL */
154 dss_write_reg(DSS_SDI_CONTROL, l);
156 l = dss_read_reg(DSS_PLL_CONTROL);
157 l = FLD_MOD(l, 0x7, 25, 22); /* SDI_PLL_FREQSEL */
158 l = FLD_MOD(l, 0xb, 16, 11); /* SDI_PLL_REGN */
159 l = FLD_MOD(l, 0xb4, 10, 1); /* SDI_PLL_REGM */
160 dss_write_reg(DSS_PLL_CONTROL, l);
163 int dss_sdi_enable(void)
165 unsigned long timeout;
167 dispc_pck_free_enable(1);
170 REG_FLD_MOD(DSS_PLL_CONTROL, 1, 18, 18); /* SDI_PLL_SYSRESET */
171 udelay(1); /* wait 2x PCLK */
174 REG_FLD_MOD(DSS_PLL_CONTROL, 1, 28, 28); /* SDI_PLL_GOBIT */
176 /* Waiting for PLL lock request to complete */
177 timeout = jiffies + msecs_to_jiffies(500);
178 while (dss_read_reg(DSS_SDI_STATUS) & (1 << 6)) {
179 if (time_after_eq(jiffies, timeout)) {
180 DSSERR("PLL lock request timed out\n");
185 /* Clearing PLL_GO bit */
186 REG_FLD_MOD(DSS_PLL_CONTROL, 0, 28, 28);
188 /* Waiting for PLL to lock */
189 timeout = jiffies + msecs_to_jiffies(500);
190 while (!(dss_read_reg(DSS_SDI_STATUS) & (1 << 5))) {
191 if (time_after_eq(jiffies, timeout)) {
192 DSSERR("PLL lock timed out\n");
197 dispc_lcd_enable_signal(1);
199 /* Waiting for SDI reset to complete */
200 timeout = jiffies + msecs_to_jiffies(500);
201 while (!(dss_read_reg(DSS_SDI_STATUS) & (1 << 2))) {
202 if (time_after_eq(jiffies, timeout)) {
203 DSSERR("SDI reset timed out\n");
211 dispc_lcd_enable_signal(0);
214 REG_FLD_MOD(DSS_PLL_CONTROL, 0, 18, 18); /* SDI_PLL_SYSRESET */
216 dispc_pck_free_enable(0);
221 void dss_sdi_disable(void)
223 dispc_lcd_enable_signal(0);
225 dispc_pck_free_enable(0);
228 REG_FLD_MOD(DSS_PLL_CONTROL, 0, 18, 18); /* SDI_PLL_SYSRESET */
231 const char *dss_get_generic_clk_source_name(enum omap_dss_clk_source clk_src)
233 return dss_generic_clk_source_names[clk_src];
237 void dss_dump_clocks(struct seq_file *s)
239 unsigned long dpll4_ck_rate;
240 unsigned long dpll4_m4_ck_rate;
241 const char *fclk_name, *fclk_real_name;
242 unsigned long fclk_rate;
244 if (dss_runtime_get())
247 seq_printf(s, "- DSS -\n");
249 fclk_name = dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_FCK);
250 fclk_real_name = dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_FCK);
251 fclk_rate = clk_get_rate(dss.dss_clk);
253 if (dss.dpll4_m4_ck) {
254 dpll4_ck_rate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
255 dpll4_m4_ck_rate = clk_get_rate(dss.dpll4_m4_ck);
257 seq_printf(s, "dpll4_ck %lu\n", dpll4_ck_rate);
259 if (cpu_is_omap3630() || cpu_is_omap44xx())
260 seq_printf(s, "%s (%s) = %lu / %lu = %lu\n",
261 fclk_name, fclk_real_name,
263 dpll4_ck_rate / dpll4_m4_ck_rate,
266 seq_printf(s, "%s (%s) = %lu / %lu * 2 = %lu\n",
267 fclk_name, fclk_real_name,
269 dpll4_ck_rate / dpll4_m4_ck_rate,
272 seq_printf(s, "%s (%s) = %lu\n",
273 fclk_name, fclk_real_name,
280 void dss_dump_regs(struct seq_file *s)
282 #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, dss_read_reg(r))
284 if (dss_runtime_get())
287 DUMPREG(DSS_REVISION);
288 DUMPREG(DSS_SYSCONFIG);
289 DUMPREG(DSS_SYSSTATUS);
290 DUMPREG(DSS_CONTROL);
292 if (dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_LCD) &
293 OMAP_DISPLAY_TYPE_SDI) {
294 DUMPREG(DSS_SDI_CONTROL);
295 DUMPREG(DSS_PLL_CONTROL);
296 DUMPREG(DSS_SDI_STATUS);
303 void dss_select_dispc_clk_source(enum omap_dss_clk_source clk_src)
305 struct platform_device *dsidev;
310 case OMAP_DSS_CLK_SRC_FCK:
313 case OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC:
315 dsidev = dsi_get_dsidev_from_id(0);
316 dsi_wait_pll_hsdiv_dispc_active(dsidev);
318 case OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC:
320 dsidev = dsi_get_dsidev_from_id(1);
321 dsi_wait_pll_hsdiv_dispc_active(dsidev);
327 dss_feat_get_reg_field(FEAT_REG_DISPC_CLK_SWITCH, &start, &end);
329 REG_FLD_MOD(DSS_CONTROL, b, start, end); /* DISPC_CLK_SWITCH */
331 dss.dispc_clk_source = clk_src;
334 void dss_select_dsi_clk_source(int dsi_module,
335 enum omap_dss_clk_source clk_src)
337 struct platform_device *dsidev;
341 case OMAP_DSS_CLK_SRC_FCK:
344 case OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI:
345 BUG_ON(dsi_module != 0);
347 dsidev = dsi_get_dsidev_from_id(0);
348 dsi_wait_pll_hsdiv_dsi_active(dsidev);
350 case OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DSI:
351 BUG_ON(dsi_module != 1);
353 dsidev = dsi_get_dsidev_from_id(1);
354 dsi_wait_pll_hsdiv_dsi_active(dsidev);
360 pos = dsi_module == 0 ? 1 : 10;
361 REG_FLD_MOD(DSS_CONTROL, b, pos, pos); /* DSIx_CLK_SWITCH */
363 dss.dsi_clk_source[dsi_module] = clk_src;
366 void dss_select_lcd_clk_source(enum omap_channel channel,
367 enum omap_dss_clk_source clk_src)
369 struct platform_device *dsidev;
372 if (!dss_has_feature(FEAT_LCD_CLK_SRC))
376 case OMAP_DSS_CLK_SRC_FCK:
379 case OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC:
380 BUG_ON(channel != OMAP_DSS_CHANNEL_LCD);
382 dsidev = dsi_get_dsidev_from_id(0);
383 dsi_wait_pll_hsdiv_dispc_active(dsidev);
385 case OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC:
386 BUG_ON(channel != OMAP_DSS_CHANNEL_LCD2);
388 dsidev = dsi_get_dsidev_from_id(1);
389 dsi_wait_pll_hsdiv_dispc_active(dsidev);
395 pos = channel == OMAP_DSS_CHANNEL_LCD ? 0 : 12;
396 REG_FLD_MOD(DSS_CONTROL, b, pos, pos); /* LCDx_CLK_SWITCH */
398 ix = channel == OMAP_DSS_CHANNEL_LCD ? 0 : 1;
399 dss.lcd_clk_source[ix] = clk_src;
402 enum omap_dss_clk_source dss_get_dispc_clk_source(void)
404 return dss.dispc_clk_source;
407 enum omap_dss_clk_source dss_get_dsi_clk_source(int dsi_module)
409 return dss.dsi_clk_source[dsi_module];
412 enum omap_dss_clk_source dss_get_lcd_clk_source(enum omap_channel channel)
414 if (dss_has_feature(FEAT_LCD_CLK_SRC)) {
415 int ix = channel == OMAP_DSS_CHANNEL_LCD ? 0 : 1;
416 return dss.lcd_clk_source[ix];
418 /* LCD_CLK source is the same as DISPC_FCLK source for
420 return dss.dispc_clk_source;
424 /* calculate clock rates using dividers in cinfo */
425 int dss_calc_clock_rates(struct dss_clock_info *cinfo)
427 if (dss.dpll4_m4_ck) {
429 u16 fck_div_max = 16;
431 if (cpu_is_omap3630() || cpu_is_omap44xx())
434 if (cinfo->fck_div > fck_div_max || cinfo->fck_div == 0)
437 prate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
439 cinfo->fck = prate / cinfo->fck_div;
441 if (cinfo->fck_div != 0)
443 cinfo->fck = clk_get_rate(dss.dss_clk);
449 int dss_set_clock_div(struct dss_clock_info *cinfo)
451 if (dss.dpll4_m4_ck) {
455 prate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
456 DSSDBG("dpll4_m4 = %ld\n", prate);
458 r = clk_set_rate(dss.dpll4_m4_ck, prate / cinfo->fck_div);
462 if (cinfo->fck_div != 0)
466 DSSDBG("fck = %ld (%d)\n", cinfo->fck, cinfo->fck_div);
471 int dss_get_clock_div(struct dss_clock_info *cinfo)
473 cinfo->fck = clk_get_rate(dss.dss_clk);
475 if (dss.dpll4_m4_ck) {
478 prate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
480 if (cpu_is_omap3630() || cpu_is_omap44xx())
481 cinfo->fck_div = prate / (cinfo->fck);
483 cinfo->fck_div = prate / (cinfo->fck / 2);
491 unsigned long dss_get_dpll4_rate(void)
494 return clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
499 int dss_calc_clock_div(bool is_tft, unsigned long req_pck,
500 struct dss_clock_info *dss_cinfo,
501 struct dispc_clock_info *dispc_cinfo)
504 struct dss_clock_info best_dss;
505 struct dispc_clock_info best_dispc;
507 unsigned long fck, max_dss_fck;
509 u16 fck_div, fck_div_max = 16;
514 prate = dss_get_dpll4_rate();
516 max_dss_fck = dss_feat_get_param_max(FEAT_PARAM_DSS_FCK);
518 fck = clk_get_rate(dss.dss_clk);
519 if (req_pck == dss.cache_req_pck &&
520 ((cpu_is_omap34xx() && prate == dss.cache_prate) ||
521 dss.cache_dss_cinfo.fck == fck)) {
522 DSSDBG("dispc clock info found from cache.\n");
523 *dss_cinfo = dss.cache_dss_cinfo;
524 *dispc_cinfo = dss.cache_dispc_cinfo;
528 min_fck_per_pck = CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK;
530 if (min_fck_per_pck &&
531 req_pck * min_fck_per_pck > max_dss_fck) {
532 DSSERR("Requested pixel clock not possible with the current "
533 "OMAP2_DSS_MIN_FCK_PER_PCK setting. Turning "
534 "the constraint off.\n");
539 memset(&best_dss, 0, sizeof(best_dss));
540 memset(&best_dispc, 0, sizeof(best_dispc));
542 if (dss.dpll4_m4_ck == NULL) {
543 struct dispc_clock_info cur_dispc;
544 /* XXX can we change the clock on omap2? */
545 fck = clk_get_rate(dss.dss_clk);
548 dispc_find_clk_divs(is_tft, req_pck, fck, &cur_dispc);
552 best_dss.fck_div = fck_div;
554 best_dispc = cur_dispc;
558 if (cpu_is_omap3630() || cpu_is_omap44xx())
561 for (fck_div = fck_div_max; fck_div > 0; --fck_div) {
562 struct dispc_clock_info cur_dispc;
564 if (fck_div_max == 32)
565 fck = prate / fck_div;
567 fck = prate / fck_div * 2;
569 if (fck > max_dss_fck)
572 if (min_fck_per_pck &&
573 fck < req_pck * min_fck_per_pck)
578 dispc_find_clk_divs(is_tft, req_pck, fck, &cur_dispc);
580 if (abs(cur_dispc.pck - req_pck) <
581 abs(best_dispc.pck - req_pck)) {
584 best_dss.fck_div = fck_div;
586 best_dispc = cur_dispc;
588 if (cur_dispc.pck == req_pck)
596 if (min_fck_per_pck) {
597 DSSERR("Could not find suitable clock settings.\n"
598 "Turning FCK/PCK constraint off and"
604 DSSERR("Could not find suitable clock settings.\n");
610 *dss_cinfo = best_dss;
612 *dispc_cinfo = best_dispc;
614 dss.cache_req_pck = req_pck;
615 dss.cache_prate = prate;
616 dss.cache_dss_cinfo = best_dss;
617 dss.cache_dispc_cinfo = best_dispc;
622 void dss_set_venc_output(enum omap_dss_venc_type type)
626 if (type == OMAP_DSS_VENC_TYPE_COMPOSITE)
628 else if (type == OMAP_DSS_VENC_TYPE_SVIDEO)
633 /* venc out selection. 0 = comp, 1 = svideo */
634 REG_FLD_MOD(DSS_CONTROL, l, 6, 6);
637 void dss_set_dac_pwrdn_bgz(bool enable)
639 REG_FLD_MOD(DSS_CONTROL, enable, 5, 5); /* DAC Power-Down Control */
642 void dss_select_hdmi_venc_clk_source(enum dss_hdmi_venc_clk_source_select hdmi)
644 REG_FLD_MOD(DSS_CONTROL, hdmi, 15, 15); /* VENC_HDMI_SWITCH */
647 enum dss_hdmi_venc_clk_source_select dss_get_hdmi_venc_clk_source(void)
649 enum omap_display_type displays;
651 displays = dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_DIGIT);
652 if ((displays & OMAP_DISPLAY_TYPE_HDMI) == 0)
653 return DSS_VENC_TV_CLK;
655 return REG_GET(DSS_CONTROL, 15, 15);
658 static int dss_get_clocks(void)
663 clk = clk_get(&dss.pdev->dev, "fck");
665 DSSERR("can't get clock fck\n");
672 if (cpu_is_omap34xx()) {
673 clk = clk_get(NULL, "dpll4_m4_ck");
675 DSSERR("Failed to get dpll4_m4_ck\n");
679 } else if (cpu_is_omap44xx()) {
680 clk = clk_get(NULL, "dpll_per_m5x2_ck");
682 DSSERR("Failed to get dpll_per_m5x2_ck\n");
686 } else { /* omap24xx */
690 dss.dpll4_m4_ck = clk;
696 clk_put(dss.dss_clk);
698 clk_put(dss.dpll4_m4_ck);
703 static void dss_put_clocks(void)
706 clk_put(dss.dpll4_m4_ck);
707 clk_put(dss.dss_clk);
710 int dss_runtime_get(void)
714 DSSDBG("dss_runtime_get\n");
716 r = pm_runtime_get_sync(&dss.pdev->dev);
718 return r < 0 ? r : 0;
721 void dss_runtime_put(void)
725 DSSDBG("dss_runtime_put\n");
727 r = pm_runtime_put_sync(&dss.pdev->dev);
732 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_OMAP2_DSS_DEBUG_SUPPORT)
733 void dss_debug_dump_clocks(struct seq_file *s)
736 dispc_dump_clocks(s);
737 #ifdef CONFIG_OMAP2_DSS_DSI
743 /* DSS HW IP initialisation */
744 static int omap_dsshw_probe(struct platform_device *pdev)
746 struct resource *dss_mem;
752 dss_mem = platform_get_resource(dss.pdev, IORESOURCE_MEM, 0);
754 DSSERR("can't get IORESOURCE_MEM DSS\n");
758 dss.base = devm_ioremap(&pdev->dev, dss_mem->start,
759 resource_size(dss_mem));
761 DSSERR("can't ioremap DSS\n");
765 r = dss_get_clocks();
769 pm_runtime_enable(&pdev->dev);
771 r = dss_runtime_get();
773 goto err_runtime_get;
776 REG_FLD_MOD(DSS_CONTROL, 0, 0, 0);
778 #ifdef CONFIG_OMAP2_DSS_VENC
779 REG_FLD_MOD(DSS_CONTROL, 1, 4, 4); /* venc dac demen */
780 REG_FLD_MOD(DSS_CONTROL, 1, 3, 3); /* venc clock 4x enable */
781 REG_FLD_MOD(DSS_CONTROL, 0, 2, 2); /* venc clock mode = normal */
783 dss.dsi_clk_source[0] = OMAP_DSS_CLK_SRC_FCK;
784 dss.dsi_clk_source[1] = OMAP_DSS_CLK_SRC_FCK;
785 dss.dispc_clk_source = OMAP_DSS_CLK_SRC_FCK;
786 dss.lcd_clk_source[0] = OMAP_DSS_CLK_SRC_FCK;
787 dss.lcd_clk_source[1] = OMAP_DSS_CLK_SRC_FCK;
789 rev = dss_read_reg(DSS_REVISION);
790 printk(KERN_INFO "OMAP DSS rev %d.%d\n",
791 FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
798 pm_runtime_disable(&pdev->dev);
803 static int omap_dsshw_remove(struct platform_device *pdev)
805 pm_runtime_disable(&pdev->dev);
812 static int dss_runtime_suspend(struct device *dev)
815 dss_set_min_bus_tput(dev, 0);
819 static int dss_runtime_resume(struct device *dev)
823 * Set an arbitrarily high tput request to ensure OPP100.
824 * What we should really do is to make a request to stay in OPP100,
825 * without any tput requirements, but that is not currently possible
829 r = dss_set_min_bus_tput(dev, 1000000000);
833 dss_restore_context();
837 static const struct dev_pm_ops dss_pm_ops = {
838 .runtime_suspend = dss_runtime_suspend,
839 .runtime_resume = dss_runtime_resume,
842 static struct platform_driver omap_dsshw_driver = {
843 .remove = omap_dsshw_remove,
845 .name = "omapdss_dss",
846 .owner = THIS_MODULE,
851 int dss_init_platform_driver(void)
853 return platform_driver_probe(&omap_dsshw_driver, omap_dsshw_probe);
856 void dss_uninit_platform_driver(void)
858 platform_driver_unregister(&omap_dsshw_driver);