dss.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * linux/drivers/video/omap2/dss/dss.c
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * Some code and ideas taken from drivers/video/omap/ driver
  8. * by Imre Deak.
  9. *
  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.
  13. *
  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
  17. * more details.
  18. *
  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/>.
  21. */
  22. #define DSS_SUBSYS_NAME "DSS"
  23. #include <linux/kernel.h>
  24. #include <linux/io.h>
  25. #include <linux/err.h>
  26. #include <linux/delay.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/clk.h>
  30. #include <plat/display.h>
  31. #include "dss.h"
  32. #define DSS_BASE 0x48050000
  33. #define DSS_SZ_REGS SZ_512
  34. struct dss_reg {
  35. u16 idx;
  36. };
  37. #define DSS_REG(idx) ((const struct dss_reg) { idx })
  38. #define DSS_REVISION DSS_REG(0x0000)
  39. #define DSS_SYSCONFIG DSS_REG(0x0010)
  40. #define DSS_SYSSTATUS DSS_REG(0x0014)
  41. #define DSS_IRQSTATUS DSS_REG(0x0018)
  42. #define DSS_CONTROL DSS_REG(0x0040)
  43. #define DSS_SDI_CONTROL DSS_REG(0x0044)
  44. #define DSS_PLL_CONTROL DSS_REG(0x0048)
  45. #define DSS_SDI_STATUS DSS_REG(0x005C)
  46. #define REG_GET(idx, start, end) \
  47. FLD_GET(dss_read_reg(idx), start, end)
  48. #define REG_FLD_MOD(idx, val, start, end) \
  49. dss_write_reg(idx, FLD_MOD(dss_read_reg(idx), val, start, end))
  50. static struct {
  51. void __iomem *base;
  52. struct clk *dpll4_m4_ck;
  53. unsigned long cache_req_pck;
  54. unsigned long cache_prate;
  55. struct dss_clock_info cache_dss_cinfo;
  56. struct dispc_clock_info cache_dispc_cinfo;
  57. u32 ctx[DSS_SZ_REGS / sizeof(u32)];
  58. } dss;
  59. static int _omap_dss_wait_reset(void);
  60. static inline void dss_write_reg(const struct dss_reg idx, u32 val)
  61. {
  62. __raw_writel(val, dss.base + idx.idx);
  63. }
  64. static inline u32 dss_read_reg(const struct dss_reg idx)
  65. {
  66. return __raw_readl(dss.base + idx.idx);
  67. }
  68. #define SR(reg) \
  69. dss.ctx[(DSS_##reg).idx / sizeof(u32)] = dss_read_reg(DSS_##reg)
  70. #define RR(reg) \
  71. dss_write_reg(DSS_##reg, dss.ctx[(DSS_##reg).idx / sizeof(u32)])
  72. void dss_save_context(void)
  73. {
  74. if (cpu_is_omap24xx())
  75. return;
  76. SR(SYSCONFIG);
  77. SR(CONTROL);
  78. #ifdef CONFIG_OMAP2_DSS_SDI
  79. SR(SDI_CONTROL);
  80. SR(PLL_CONTROL);
  81. #endif
  82. }
  83. void dss_restore_context(void)
  84. {
  85. if (_omap_dss_wait_reset())
  86. DSSERR("DSS not coming out of reset after sleep\n");
  87. RR(SYSCONFIG);
  88. RR(CONTROL);
  89. #ifdef CONFIG_OMAP2_DSS_SDI
  90. RR(SDI_CONTROL);
  91. RR(PLL_CONTROL);
  92. #endif
  93. }
  94. #undef SR
  95. #undef RR
  96. void dss_sdi_init(u8 datapairs)
  97. {
  98. u32 l;
  99. BUG_ON(datapairs > 3 || datapairs < 1);
  100. l = dss_read_reg(DSS_SDI_CONTROL);
  101. l = FLD_MOD(l, 0xf, 19, 15); /* SDI_PDIV */
  102. l = FLD_MOD(l, datapairs-1, 3, 2); /* SDI_PRSEL */
  103. l = FLD_MOD(l, 2, 1, 0); /* SDI_BWSEL */
  104. dss_write_reg(DSS_SDI_CONTROL, l);
  105. l = dss_read_reg(DSS_PLL_CONTROL);
  106. l = FLD_MOD(l, 0x7, 25, 22); /* SDI_PLL_FREQSEL */
  107. l = FLD_MOD(l, 0xb, 16, 11); /* SDI_PLL_REGN */
  108. l = FLD_MOD(l, 0xb4, 10, 1); /* SDI_PLL_REGM */
  109. dss_write_reg(DSS_PLL_CONTROL, l);
  110. }
  111. int dss_sdi_enable(void)
  112. {
  113. unsigned long timeout;
  114. dispc_pck_free_enable(1);
  115. /* Reset SDI PLL */
  116. REG_FLD_MOD(DSS_PLL_CONTROL, 1, 18, 18); /* SDI_PLL_SYSRESET */
  117. udelay(1); /* wait 2x PCLK */
  118. /* Lock SDI PLL */
  119. REG_FLD_MOD(DSS_PLL_CONTROL, 1, 28, 28); /* SDI_PLL_GOBIT */
  120. /* Waiting for PLL lock request to complete */
  121. timeout = jiffies + msecs_to_jiffies(500);
  122. while (dss_read_reg(DSS_SDI_STATUS) & (1 << 6)) {
  123. if (time_after_eq(jiffies, timeout)) {
  124. DSSERR("PLL lock request timed out\n");
  125. goto err1;
  126. }
  127. }
  128. /* Clearing PLL_GO bit */
  129. REG_FLD_MOD(DSS_PLL_CONTROL, 0, 28, 28);
  130. /* Waiting for PLL to lock */
  131. timeout = jiffies + msecs_to_jiffies(500);
  132. while (!(dss_read_reg(DSS_SDI_STATUS) & (1 << 5))) {
  133. if (time_after_eq(jiffies, timeout)) {
  134. DSSERR("PLL lock timed out\n");
  135. goto err1;
  136. }
  137. }
  138. dispc_lcd_enable_signal(1);
  139. /* Waiting for SDI reset to complete */
  140. timeout = jiffies + msecs_to_jiffies(500);
  141. while (!(dss_read_reg(DSS_SDI_STATUS) & (1 << 2))) {
  142. if (time_after_eq(jiffies, timeout)) {
  143. DSSERR("SDI reset timed out\n");
  144. goto err2;
  145. }
  146. }
  147. return 0;
  148. err2:
  149. dispc_lcd_enable_signal(0);
  150. err1:
  151. /* Reset SDI PLL */
  152. REG_FLD_MOD(DSS_PLL_CONTROL, 0, 18, 18); /* SDI_PLL_SYSRESET */
  153. dispc_pck_free_enable(0);
  154. return -ETIMEDOUT;
  155. }
  156. void dss_sdi_disable(void)
  157. {
  158. dispc_lcd_enable_signal(0);
  159. dispc_pck_free_enable(0);
  160. /* Reset SDI PLL */
  161. REG_FLD_MOD(DSS_PLL_CONTROL, 0, 18, 18); /* SDI_PLL_SYSRESET */
  162. }
  163. void dss_dump_clocks(struct seq_file *s)
  164. {
  165. unsigned long dpll4_ck_rate;
  166. unsigned long dpll4_m4_ck_rate;
  167. dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK1);
  168. dpll4_ck_rate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
  169. dpll4_m4_ck_rate = clk_get_rate(dss.dpll4_m4_ck);
  170. seq_printf(s, "- DSS -\n");
  171. seq_printf(s, "dpll4_ck %lu\n", dpll4_ck_rate);
  172. seq_printf(s, "dss1_alwon_fclk = %lu / %lu * 2 = %lu\n",
  173. dpll4_ck_rate,
  174. dpll4_ck_rate / dpll4_m4_ck_rate,
  175. dss_clk_get_rate(DSS_CLK_FCK1));
  176. dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK1);
  177. }
  178. void dss_dump_regs(struct seq_file *s)
  179. {
  180. #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, dss_read_reg(r))
  181. dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK1);
  182. DUMPREG(DSS_REVISION);
  183. DUMPREG(DSS_SYSCONFIG);
  184. DUMPREG(DSS_SYSSTATUS);
  185. DUMPREG(DSS_IRQSTATUS);
  186. DUMPREG(DSS_CONTROL);
  187. DUMPREG(DSS_SDI_CONTROL);
  188. DUMPREG(DSS_PLL_CONTROL);
  189. DUMPREG(DSS_SDI_STATUS);
  190. dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK1);
  191. #undef DUMPREG
  192. }
  193. void dss_select_clk_source(bool dsi, bool dispc)
  194. {
  195. u32 r;
  196. r = dss_read_reg(DSS_CONTROL);
  197. r = FLD_MOD(r, dsi, 1, 1); /* DSI_CLK_SWITCH */
  198. r = FLD_MOD(r, dispc, 0, 0); /* DISPC_CLK_SWITCH */
  199. dss_write_reg(DSS_CONTROL, r);
  200. }
  201. int dss_get_dsi_clk_source(void)
  202. {
  203. return FLD_GET(dss_read_reg(DSS_CONTROL), 1, 1);
  204. }
  205. int dss_get_dispc_clk_source(void)
  206. {
  207. return FLD_GET(dss_read_reg(DSS_CONTROL), 0, 0);
  208. }
  209. /* calculate clock rates using dividers in cinfo */
  210. int dss_calc_clock_rates(struct dss_clock_info *cinfo)
  211. {
  212. unsigned long prate;
  213. if (cinfo->fck_div > 16 || cinfo->fck_div == 0)
  214. return -EINVAL;
  215. prate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
  216. cinfo->fck = prate / cinfo->fck_div;
  217. return 0;
  218. }
  219. int dss_set_clock_div(struct dss_clock_info *cinfo)
  220. {
  221. unsigned long prate;
  222. int r;
  223. if (cpu_is_omap34xx()) {
  224. prate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
  225. DSSDBG("dpll4_m4 = %ld\n", prate);
  226. r = clk_set_rate(dss.dpll4_m4_ck, prate / cinfo->fck_div);
  227. if (r)
  228. return r;
  229. }
  230. DSSDBG("fck = %ld (%d)\n", cinfo->fck, cinfo->fck_div);
  231. return 0;
  232. }
  233. int dss_get_clock_div(struct dss_clock_info *cinfo)
  234. {
  235. cinfo->fck = dss_clk_get_rate(DSS_CLK_FCK1);
  236. if (cpu_is_omap34xx()) {
  237. unsigned long prate;
  238. prate = clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
  239. cinfo->fck_div = prate / (cinfo->fck / 2);
  240. } else {
  241. cinfo->fck_div = 0;
  242. }
  243. return 0;
  244. }
  245. unsigned long dss_get_dpll4_rate(void)
  246. {
  247. if (cpu_is_omap34xx())
  248. return clk_get_rate(clk_get_parent(dss.dpll4_m4_ck));
  249. else
  250. return 0;
  251. }
  252. int dss_calc_clock_div(bool is_tft, unsigned long req_pck,
  253. struct dss_clock_info *dss_cinfo,
  254. struct dispc_clock_info *dispc_cinfo)
  255. {
  256. unsigned long prate;
  257. struct dss_clock_info best_dss;
  258. struct dispc_clock_info best_dispc;
  259. unsigned long fck;
  260. u16 fck_div;
  261. int match = 0;
  262. int min_fck_per_pck;
  263. prate = dss_get_dpll4_rate();
  264. fck = dss_clk_get_rate(DSS_CLK_FCK1);
  265. if (req_pck == dss.cache_req_pck &&
  266. ((cpu_is_omap34xx() && prate == dss.cache_prate) ||
  267. dss.cache_dss_cinfo.fck == fck)) {
  268. DSSDBG("dispc clock info found from cache.\n");
  269. *dss_cinfo = dss.cache_dss_cinfo;
  270. *dispc_cinfo = dss.cache_dispc_cinfo;
  271. return 0;
  272. }
  273. min_fck_per_pck = CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK;
  274. if (min_fck_per_pck &&
  275. req_pck * min_fck_per_pck > DISPC_MAX_FCK) {
  276. DSSERR("Requested pixel clock not possible with the current "
  277. "OMAP2_DSS_MIN_FCK_PER_PCK setting. Turning "
  278. "the constraint off.\n");
  279. min_fck_per_pck = 0;
  280. }
  281. retry:
  282. memset(&best_dss, 0, sizeof(best_dss));
  283. memset(&best_dispc, 0, sizeof(best_dispc));
  284. if (cpu_is_omap24xx()) {
  285. struct dispc_clock_info cur_dispc;
  286. /* XXX can we change the clock on omap2? */
  287. fck = dss_clk_get_rate(DSS_CLK_FCK1);
  288. fck_div = 1;
  289. dispc_find_clk_divs(is_tft, req_pck, fck, &cur_dispc);
  290. match = 1;
  291. best_dss.fck = fck;
  292. best_dss.fck_div = fck_div;
  293. best_dispc = cur_dispc;
  294. goto found;
  295. } else if (cpu_is_omap34xx()) {
  296. for (fck_div = 16; fck_div > 0; --fck_div) {
  297. struct dispc_clock_info cur_dispc;
  298. fck = prate / fck_div * 2;
  299. if (fck > DISPC_MAX_FCK)
  300. continue;
  301. if (min_fck_per_pck &&
  302. fck < req_pck * min_fck_per_pck)
  303. continue;
  304. match = 1;
  305. dispc_find_clk_divs(is_tft, req_pck, fck, &cur_dispc);
  306. if (abs(cur_dispc.pck - req_pck) <
  307. abs(best_dispc.pck - req_pck)) {
  308. best_dss.fck = fck;
  309. best_dss.fck_div = fck_div;
  310. best_dispc = cur_dispc;
  311. if (cur_dispc.pck == req_pck)
  312. goto found;
  313. }
  314. }
  315. } else {
  316. BUG();
  317. }
  318. found:
  319. if (!match) {
  320. if (min_fck_per_pck) {
  321. DSSERR("Could not find suitable clock settings.\n"
  322. "Turning FCK/PCK constraint off and"
  323. "trying again.\n");
  324. min_fck_per_pck = 0;
  325. goto retry;
  326. }
  327. DSSERR("Could not find suitable clock settings.\n");
  328. return -EINVAL;
  329. }
  330. if (dss_cinfo)
  331. *dss_cinfo = best_dss;
  332. if (dispc_cinfo)
  333. *dispc_cinfo = best_dispc;
  334. dss.cache_req_pck = req_pck;
  335. dss.cache_prate = prate;
  336. dss.cache_dss_cinfo = best_dss;
  337. dss.cache_dispc_cinfo = best_dispc;
  338. return 0;
  339. }
  340. static irqreturn_t dss_irq_handler_omap2(int irq, void *arg)
  341. {
  342. dispc_irq_handler();
  343. return IRQ_HANDLED;
  344. }
  345. static irqreturn_t dss_irq_handler_omap3(int irq, void *arg)
  346. {
  347. u32 irqstatus;
  348. irqstatus = dss_read_reg(DSS_IRQSTATUS);
  349. if (irqstatus & (1<<0)) /* DISPC_IRQ */
  350. dispc_irq_handler();
  351. #ifdef CONFIG_OMAP2_DSS_DSI
  352. if (irqstatus & (1<<1)) /* DSI_IRQ */
  353. dsi_irq_handler();
  354. #endif
  355. return IRQ_HANDLED;
  356. }
  357. static int _omap_dss_wait_reset(void)
  358. {
  359. unsigned timeout = 1000;
  360. while (REG_GET(DSS_SYSSTATUS, 0, 0) == 0) {
  361. udelay(1);
  362. if (!--timeout) {
  363. DSSERR("soft reset failed\n");
  364. return -ENODEV;
  365. }
  366. }
  367. return 0;
  368. }
  369. static int _omap_dss_reset(void)
  370. {
  371. /* Soft reset */
  372. REG_FLD_MOD(DSS_SYSCONFIG, 1, 1, 1);
  373. return _omap_dss_wait_reset();
  374. }
  375. void dss_set_venc_output(enum omap_dss_venc_type type)
  376. {
  377. int l = 0;
  378. if (type == OMAP_DSS_VENC_TYPE_COMPOSITE)
  379. l = 0;
  380. else if (type == OMAP_DSS_VENC_TYPE_SVIDEO)
  381. l = 1;
  382. else
  383. BUG();
  384. /* venc out selection. 0 = comp, 1 = svideo */
  385. REG_FLD_MOD(DSS_CONTROL, l, 6, 6);
  386. }
  387. void dss_set_dac_pwrdn_bgz(bool enable)
  388. {
  389. REG_FLD_MOD(DSS_CONTROL, enable, 5, 5); /* DAC Power-Down Control */
  390. }
  391. int dss_init(bool skip_init)
  392. {
  393. int r;
  394. u32 rev;
  395. dss.base = ioremap(DSS_BASE, DSS_SZ_REGS);
  396. if (!dss.base) {
  397. DSSERR("can't ioremap DSS\n");
  398. r = -ENOMEM;
  399. goto fail0;
  400. }
  401. if (!skip_init) {
  402. /* disable LCD and DIGIT output. This seems to fix the synclost
  403. * problem that we get, if the bootloader starts the DSS and
  404. * the kernel resets it */
  405. omap_writel(omap_readl(0x48050440) & ~0x3, 0x48050440);
  406. /* We need to wait here a bit, otherwise we sometimes start to
  407. * get synclost errors, and after that only power cycle will
  408. * restore DSS functionality. I have no idea why this happens.
  409. * And we have to wait _before_ resetting the DSS, but after
  410. * enabling clocks.
  411. */
  412. msleep(50);
  413. _omap_dss_reset();
  414. }
  415. /* autoidle */
  416. REG_FLD_MOD(DSS_SYSCONFIG, 1, 0, 0);
  417. /* Select DPLL */
  418. REG_FLD_MOD(DSS_CONTROL, 0, 0, 0);
  419. #ifdef CONFIG_OMAP2_DSS_VENC
  420. REG_FLD_MOD(DSS_CONTROL, 1, 4, 4); /* venc dac demen */
  421. REG_FLD_MOD(DSS_CONTROL, 1, 3, 3); /* venc clock 4x enable */
  422. REG_FLD_MOD(DSS_CONTROL, 0, 2, 2); /* venc clock mode = normal */
  423. #endif
  424. r = request_irq(INT_24XX_DSS_IRQ,
  425. cpu_is_omap24xx()
  426. ? dss_irq_handler_omap2
  427. : dss_irq_handler_omap3,
  428. 0, "OMAP DSS", NULL);
  429. if (r < 0) {
  430. DSSERR("omap2 dss: request_irq failed\n");
  431. goto fail1;
  432. }
  433. if (cpu_is_omap34xx()) {
  434. dss.dpll4_m4_ck = clk_get(NULL, "dpll4_m4_ck");
  435. if (IS_ERR(dss.dpll4_m4_ck)) {
  436. DSSERR("Failed to get dpll4_m4_ck\n");
  437. r = PTR_ERR(dss.dpll4_m4_ck);
  438. goto fail2;
  439. }
  440. }
  441. dss_save_context();
  442. rev = dss_read_reg(DSS_REVISION);
  443. printk(KERN_INFO "OMAP DSS rev %d.%d\n",
  444. FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
  445. return 0;
  446. fail2:
  447. free_irq(INT_24XX_DSS_IRQ, NULL);
  448. fail1:
  449. iounmap(dss.base);
  450. fail0:
  451. return r;
  452. }
  453. void dss_exit(void)
  454. {
  455. if (cpu_is_omap34xx())
  456. clk_put(dss.dpll4_m4_ck);
  457. free_irq(INT_24XX_DSS_IRQ, NULL);
  458. iounmap(dss.base);
  459. }