sh_mobile_lcdcfb.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * SuperH Mobile LCDC Framebuffer
  3. *
  4. * Copyright (c) 2008 Magnus Damm
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/mm.h>
  14. #include <linux/fb.h>
  15. #include <linux/clk.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/vmalloc.h>
  21. #include <video/sh_mobile_lcdc.h>
  22. #include <asm/atomic.h>
  23. #define PALETTE_NR 16
  24. #define SIDE_B_OFFSET 0x1000
  25. #define MIRROR_OFFSET 0x2000
  26. /* shared registers */
  27. #define _LDDCKR 0x410
  28. #define _LDDCKSTPR 0x414
  29. #define _LDINTR 0x468
  30. #define _LDSR 0x46c
  31. #define _LDCNT1R 0x470
  32. #define _LDCNT2R 0x474
  33. #define _LDRCNTR 0x478
  34. #define _LDDDSR 0x47c
  35. #define _LDDWD0R 0x800
  36. #define _LDDRDR 0x840
  37. #define _LDDWAR 0x900
  38. #define _LDDRAR 0x904
  39. /* shared registers and their order for context save/restore */
  40. static int lcdc_shared_regs[] = {
  41. _LDDCKR,
  42. _LDDCKSTPR,
  43. _LDINTR,
  44. _LDDDSR,
  45. _LDCNT1R,
  46. _LDCNT2R,
  47. };
  48. #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
  49. /* per-channel registers */
  50. enum { LDDCKPAT1R, LDDCKPAT2R, LDMT1R, LDMT2R, LDMT3R, LDDFR, LDSM1R,
  51. LDSM2R, LDSA1R, LDMLSR, LDHCNR, LDHSYNR, LDVLNR, LDVSYNR, LDPMR,
  52. NR_CH_REGS };
  53. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  54. [LDDCKPAT1R] = 0x400,
  55. [LDDCKPAT2R] = 0x404,
  56. [LDMT1R] = 0x418,
  57. [LDMT2R] = 0x41c,
  58. [LDMT3R] = 0x420,
  59. [LDDFR] = 0x424,
  60. [LDSM1R] = 0x428,
  61. [LDSM2R] = 0x42c,
  62. [LDSA1R] = 0x430,
  63. [LDMLSR] = 0x438,
  64. [LDHCNR] = 0x448,
  65. [LDHSYNR] = 0x44c,
  66. [LDVLNR] = 0x450,
  67. [LDVSYNR] = 0x454,
  68. [LDPMR] = 0x460,
  69. };
  70. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  71. [LDDCKPAT1R] = 0x408,
  72. [LDDCKPAT2R] = 0x40c,
  73. [LDMT1R] = 0x600,
  74. [LDMT2R] = 0x604,
  75. [LDMT3R] = 0x608,
  76. [LDDFR] = 0x60c,
  77. [LDSM1R] = 0x610,
  78. [LDSM2R] = 0x614,
  79. [LDSA1R] = 0x618,
  80. [LDMLSR] = 0x620,
  81. [LDHCNR] = 0x624,
  82. [LDHSYNR] = 0x628,
  83. [LDVLNR] = 0x62c,
  84. [LDVSYNR] = 0x630,
  85. [LDPMR] = 0x63c,
  86. };
  87. #define START_LCDC 0x00000001
  88. #define LCDC_RESET 0x00000100
  89. #define DISPLAY_BEU 0x00000008
  90. #define LCDC_ENABLE 0x00000001
  91. #define LDINTR_FE 0x00000400
  92. #define LDINTR_VSE 0x00000200
  93. #define LDINTR_VEE 0x00000100
  94. #define LDINTR_FS 0x00000004
  95. #define LDINTR_VSS 0x00000002
  96. #define LDINTR_VES 0x00000001
  97. #define LDRCNTR_SRS 0x00020000
  98. #define LDRCNTR_SRC 0x00010000
  99. #define LDRCNTR_MRS 0x00000002
  100. #define LDRCNTR_MRC 0x00000001
  101. struct sh_mobile_lcdc_priv;
  102. struct sh_mobile_lcdc_chan {
  103. struct sh_mobile_lcdc_priv *lcdc;
  104. unsigned long *reg_offs;
  105. unsigned long ldmt1r_value;
  106. unsigned long enabled; /* ME and SE in LDCNT2R */
  107. struct sh_mobile_lcdc_chan_cfg cfg;
  108. u32 pseudo_palette[PALETTE_NR];
  109. unsigned long saved_ch_regs[NR_CH_REGS];
  110. struct fb_info *info;
  111. dma_addr_t dma_handle;
  112. struct fb_deferred_io defio;
  113. struct scatterlist *sglist;
  114. unsigned long frame_end;
  115. unsigned long pan_offset;
  116. unsigned long new_pan_offset;
  117. wait_queue_head_t frame_end_wait;
  118. };
  119. struct sh_mobile_lcdc_priv {
  120. void __iomem *base;
  121. int irq;
  122. atomic_t hw_usecnt;
  123. struct device *dev;
  124. struct clk *dot_clk;
  125. unsigned long lddckr;
  126. struct sh_mobile_lcdc_chan ch[2];
  127. unsigned long saved_shared_regs[NR_SHARED_REGS];
  128. int started;
  129. };
  130. static bool banked(int reg_nr)
  131. {
  132. switch (reg_nr) {
  133. case LDMT1R:
  134. case LDMT2R:
  135. case LDMT3R:
  136. case LDDFR:
  137. case LDSM1R:
  138. case LDSA1R:
  139. case LDMLSR:
  140. case LDHCNR:
  141. case LDHSYNR:
  142. case LDVLNR:
  143. case LDVSYNR:
  144. return true;
  145. }
  146. return false;
  147. }
  148. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  149. int reg_nr, unsigned long data)
  150. {
  151. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  152. if (banked(reg_nr))
  153. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  154. SIDE_B_OFFSET);
  155. }
  156. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  157. int reg_nr, unsigned long data)
  158. {
  159. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  160. MIRROR_OFFSET);
  161. }
  162. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  163. int reg_nr)
  164. {
  165. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  166. }
  167. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  168. unsigned long reg_offs, unsigned long data)
  169. {
  170. iowrite32(data, priv->base + reg_offs);
  171. }
  172. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  173. unsigned long reg_offs)
  174. {
  175. return ioread32(priv->base + reg_offs);
  176. }
  177. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  178. unsigned long reg_offs,
  179. unsigned long mask, unsigned long until)
  180. {
  181. while ((lcdc_read(priv, reg_offs) & mask) != until)
  182. cpu_relax();
  183. }
  184. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  185. {
  186. return chan->cfg.chan == LCDC_CHAN_SUBLCD;
  187. }
  188. static void lcdc_sys_write_index(void *handle, unsigned long data)
  189. {
  190. struct sh_mobile_lcdc_chan *ch = handle;
  191. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
  192. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  193. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  194. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  195. }
  196. static void lcdc_sys_write_data(void *handle, unsigned long data)
  197. {
  198. struct sh_mobile_lcdc_chan *ch = handle;
  199. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
  200. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  201. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  202. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  203. }
  204. static unsigned long lcdc_sys_read_data(void *handle)
  205. {
  206. struct sh_mobile_lcdc_chan *ch = handle;
  207. lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
  208. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  209. lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  210. udelay(1);
  211. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  212. return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
  213. }
  214. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  215. lcdc_sys_write_index,
  216. lcdc_sys_write_data,
  217. lcdc_sys_read_data,
  218. };
  219. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  220. {
  221. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  222. pm_runtime_get_sync(priv->dev);
  223. if (priv->dot_clk)
  224. clk_enable(priv->dot_clk);
  225. }
  226. }
  227. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  228. {
  229. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  230. if (priv->dot_clk)
  231. clk_disable(priv->dot_clk);
  232. pm_runtime_put(priv->dev);
  233. }
  234. }
  235. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  236. struct list_head *pagelist)
  237. {
  238. struct sh_mobile_lcdc_chan *ch = info->par;
  239. unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
  240. struct page *page;
  241. int nr_pages = 0;
  242. sg_init_table(ch->sglist, nr_pages_max);
  243. list_for_each_entry(page, pagelist, lru)
  244. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  245. return nr_pages;
  246. }
  247. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  248. struct list_head *pagelist)
  249. {
  250. struct sh_mobile_lcdc_chan *ch = info->par;
  251. /* enable clocks before accessing hardware */
  252. sh_mobile_lcdc_clk_on(ch->lcdc);
  253. /*
  254. * It's possible to get here without anything on the pagelist via
  255. * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
  256. * invocation. In the former case, the acceleration routines are
  257. * stepped in to when using the framebuffer console causing the
  258. * workqueue to be scheduled without any dirty pages on the list.
  259. *
  260. * Despite this, a panel update is still needed given that the
  261. * acceleration routines have their own methods for writing in
  262. * that still need to be updated.
  263. *
  264. * The fsync() and empty pagelist case could be optimized for,
  265. * but we don't bother, as any application exhibiting such
  266. * behaviour is fundamentally broken anyways.
  267. */
  268. if (!list_empty(pagelist)) {
  269. unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  270. /* trigger panel update */
  271. dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  272. lcdc_write_chan(ch, LDSM2R, 1);
  273. dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  274. } else
  275. lcdc_write_chan(ch, LDSM2R, 1);
  276. }
  277. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  278. {
  279. struct fb_deferred_io *fbdefio = info->fbdefio;
  280. if (fbdefio)
  281. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  282. }
  283. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  284. {
  285. struct sh_mobile_lcdc_priv *priv = data;
  286. struct sh_mobile_lcdc_chan *ch;
  287. unsigned long tmp;
  288. unsigned long ldintr;
  289. int is_sub;
  290. int k;
  291. /* acknowledge interrupt */
  292. ldintr = tmp = lcdc_read(priv, _LDINTR);
  293. /*
  294. * disable further VSYNC End IRQs, preserve all other enabled IRQs,
  295. * write 0 to bits 0-6 to ack all triggered IRQs.
  296. */
  297. tmp &= 0xffffff00 & ~LDINTR_VEE;
  298. lcdc_write(priv, _LDINTR, tmp);
  299. /* figure out if this interrupt is for main or sub lcd */
  300. is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
  301. /* wake up channel and disable clocks */
  302. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  303. ch = &priv->ch[k];
  304. if (!ch->enabled)
  305. continue;
  306. /* Frame Start */
  307. if (ldintr & LDINTR_FS) {
  308. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  309. ch->frame_end = 1;
  310. wake_up(&ch->frame_end_wait);
  311. sh_mobile_lcdc_clk_off(priv);
  312. }
  313. }
  314. /* VSYNC End */
  315. if (ldintr & LDINTR_VES) {
  316. unsigned long ldrcntr = lcdc_read(priv, _LDRCNTR);
  317. /* Set the source address for the next refresh */
  318. lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle +
  319. ch->new_pan_offset);
  320. if (lcdc_chan_is_sublcd(ch))
  321. lcdc_write(ch->lcdc, _LDRCNTR,
  322. ldrcntr ^ LDRCNTR_SRS);
  323. else
  324. lcdc_write(ch->lcdc, _LDRCNTR,
  325. ldrcntr ^ LDRCNTR_MRS);
  326. ch->pan_offset = ch->new_pan_offset;
  327. }
  328. }
  329. return IRQ_HANDLED;
  330. }
  331. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  332. int start)
  333. {
  334. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  335. int k;
  336. /* start or stop the lcdc */
  337. if (start)
  338. lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
  339. else
  340. lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
  341. /* wait until power is applied/stopped on all channels */
  342. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  343. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  344. while (1) {
  345. tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
  346. if (start && tmp == 3)
  347. break;
  348. if (!start && tmp == 0)
  349. break;
  350. cpu_relax();
  351. }
  352. if (!start)
  353. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  354. }
  355. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  356. {
  357. struct sh_mobile_lcdc_chan *ch;
  358. struct fb_videomode *lcd_cfg;
  359. struct sh_mobile_lcdc_board_cfg *board_cfg;
  360. unsigned long tmp;
  361. int k, m;
  362. int ret = 0;
  363. /* enable clocks before accessing the hardware */
  364. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  365. if (priv->ch[k].enabled)
  366. sh_mobile_lcdc_clk_on(priv);
  367. /* reset */
  368. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
  369. lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
  370. /* enable LCDC channels */
  371. tmp = lcdc_read(priv, _LDCNT2R);
  372. tmp |= priv->ch[0].enabled;
  373. tmp |= priv->ch[1].enabled;
  374. lcdc_write(priv, _LDCNT2R, tmp);
  375. /* read data from external memory, avoid using the BEU for now */
  376. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
  377. /* stop the lcdc first */
  378. sh_mobile_lcdc_start_stop(priv, 0);
  379. /* configure clocks */
  380. tmp = priv->lddckr;
  381. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  382. ch = &priv->ch[k];
  383. if (!priv->ch[k].enabled)
  384. continue;
  385. m = ch->cfg.clock_divider;
  386. if (!m)
  387. continue;
  388. if (m == 1)
  389. m = 1 << 6;
  390. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  391. lcdc_write_chan(ch, LDDCKPAT1R, 0x00000000);
  392. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  393. }
  394. lcdc_write(priv, _LDDCKR, tmp);
  395. /* start dotclock again */
  396. lcdc_write(priv, _LDDCKSTPR, 0);
  397. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  398. /* interrupts are disabled to begin with */
  399. lcdc_write(priv, _LDINTR, 0);
  400. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  401. ch = &priv->ch[k];
  402. lcd_cfg = &ch->cfg.lcd_cfg;
  403. if (!ch->enabled)
  404. continue;
  405. tmp = ch->ldmt1r_value;
  406. tmp |= (lcd_cfg->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
  407. tmp |= (lcd_cfg->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
  408. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
  409. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
  410. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
  411. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
  412. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
  413. lcdc_write_chan(ch, LDMT1R, tmp);
  414. /* setup SYS bus */
  415. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  416. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  417. /* horizontal configuration */
  418. tmp = lcd_cfg->xres + lcd_cfg->hsync_len;
  419. tmp += lcd_cfg->left_margin;
  420. tmp += lcd_cfg->right_margin;
  421. tmp /= 8; /* HTCN */
  422. tmp |= (lcd_cfg->xres / 8) << 16; /* HDCN */
  423. lcdc_write_chan(ch, LDHCNR, tmp);
  424. tmp = lcd_cfg->xres;
  425. tmp += lcd_cfg->right_margin;
  426. tmp /= 8; /* HSYNP */
  427. tmp |= (lcd_cfg->hsync_len / 8) << 16; /* HSYNW */
  428. lcdc_write_chan(ch, LDHSYNR, tmp);
  429. /* power supply */
  430. lcdc_write_chan(ch, LDPMR, 0);
  431. /* vertical configuration */
  432. tmp = lcd_cfg->yres + lcd_cfg->vsync_len;
  433. tmp += lcd_cfg->upper_margin;
  434. tmp += lcd_cfg->lower_margin; /* VTLN */
  435. tmp |= lcd_cfg->yres << 16; /* VDLN */
  436. lcdc_write_chan(ch, LDVLNR, tmp);
  437. tmp = lcd_cfg->yres;
  438. tmp += lcd_cfg->lower_margin; /* VSYNP */
  439. tmp |= lcd_cfg->vsync_len << 16; /* VSYNW */
  440. lcdc_write_chan(ch, LDVSYNR, tmp);
  441. board_cfg = &ch->cfg.board_cfg;
  442. if (board_cfg->setup_sys)
  443. ret = board_cfg->setup_sys(board_cfg->board_data, ch,
  444. &sh_mobile_lcdc_sys_bus_ops);
  445. if (ret)
  446. return ret;
  447. }
  448. /* word and long word swap */
  449. lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
  450. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  451. ch = &priv->ch[k];
  452. if (!priv->ch[k].enabled)
  453. continue;
  454. /* set bpp format in PKF[4:0] */
  455. tmp = lcdc_read_chan(ch, LDDFR);
  456. tmp &= ~(0x0001001f);
  457. tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
  458. lcdc_write_chan(ch, LDDFR, tmp);
  459. /* point out our frame buffer */
  460. lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
  461. /* set line size */
  462. lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
  463. /* setup deferred io if SYS bus */
  464. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  465. if (ch->ldmt1r_value & (1 << 12) && tmp) {
  466. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  467. ch->defio.delay = msecs_to_jiffies(tmp);
  468. ch->info->fbdefio = &ch->defio;
  469. fb_deferred_io_init(ch->info);
  470. /* one-shot mode */
  471. lcdc_write_chan(ch, LDSM1R, 1);
  472. /* enable "Frame End Interrupt Enable" bit */
  473. lcdc_write(priv, _LDINTR, LDINTR_FE);
  474. } else {
  475. /* continuous read mode */
  476. lcdc_write_chan(ch, LDSM1R, 0);
  477. }
  478. }
  479. /* display output */
  480. lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
  481. /* start the lcdc */
  482. sh_mobile_lcdc_start_stop(priv, 1);
  483. priv->started = 1;
  484. /* tell the board code to enable the panel */
  485. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  486. ch = &priv->ch[k];
  487. if (!ch->enabled)
  488. continue;
  489. board_cfg = &ch->cfg.board_cfg;
  490. if (board_cfg->display_on)
  491. board_cfg->display_on(board_cfg->board_data);
  492. }
  493. return 0;
  494. }
  495. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  496. {
  497. struct sh_mobile_lcdc_chan *ch;
  498. struct sh_mobile_lcdc_board_cfg *board_cfg;
  499. int k;
  500. /* clean up deferred io and ask board code to disable panel */
  501. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  502. ch = &priv->ch[k];
  503. if (!ch->enabled)
  504. continue;
  505. /* deferred io mode:
  506. * flush frame, and wait for frame end interrupt
  507. * clean up deferred io and enable clock
  508. */
  509. if (ch->info->fbdefio) {
  510. ch->frame_end = 0;
  511. schedule_delayed_work(&ch->info->deferred_work, 0);
  512. wait_event(ch->frame_end_wait, ch->frame_end);
  513. fb_deferred_io_cleanup(ch->info);
  514. ch->info->fbdefio = NULL;
  515. sh_mobile_lcdc_clk_on(priv);
  516. }
  517. board_cfg = &ch->cfg.board_cfg;
  518. if (board_cfg->display_off)
  519. board_cfg->display_off(board_cfg->board_data);
  520. }
  521. /* stop the lcdc */
  522. if (priv->started) {
  523. sh_mobile_lcdc_start_stop(priv, 0);
  524. priv->started = 0;
  525. }
  526. /* stop clocks */
  527. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  528. if (priv->ch[k].enabled)
  529. sh_mobile_lcdc_clk_off(priv);
  530. }
  531. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  532. {
  533. int ifm, miftyp;
  534. switch (ch->cfg.interface_type) {
  535. case RGB8: ifm = 0; miftyp = 0; break;
  536. case RGB9: ifm = 0; miftyp = 4; break;
  537. case RGB12A: ifm = 0; miftyp = 5; break;
  538. case RGB12B: ifm = 0; miftyp = 6; break;
  539. case RGB16: ifm = 0; miftyp = 7; break;
  540. case RGB18: ifm = 0; miftyp = 10; break;
  541. case RGB24: ifm = 0; miftyp = 11; break;
  542. case SYS8A: ifm = 1; miftyp = 0; break;
  543. case SYS8B: ifm = 1; miftyp = 1; break;
  544. case SYS8C: ifm = 1; miftyp = 2; break;
  545. case SYS8D: ifm = 1; miftyp = 3; break;
  546. case SYS9: ifm = 1; miftyp = 4; break;
  547. case SYS12: ifm = 1; miftyp = 5; break;
  548. case SYS16A: ifm = 1; miftyp = 7; break;
  549. case SYS16B: ifm = 1; miftyp = 8; break;
  550. case SYS16C: ifm = 1; miftyp = 9; break;
  551. case SYS18: ifm = 1; miftyp = 10; break;
  552. case SYS24: ifm = 1; miftyp = 11; break;
  553. default: goto bad;
  554. }
  555. /* SUBLCD only supports SYS interface */
  556. if (lcdc_chan_is_sublcd(ch)) {
  557. if (ifm == 0)
  558. goto bad;
  559. else
  560. ifm = 0;
  561. }
  562. ch->ldmt1r_value = (ifm << 12) | miftyp;
  563. return 0;
  564. bad:
  565. return -EINVAL;
  566. }
  567. static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
  568. int clock_source,
  569. struct sh_mobile_lcdc_priv *priv)
  570. {
  571. char *str;
  572. int icksel;
  573. switch (clock_source) {
  574. case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
  575. case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
  576. case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
  577. default:
  578. return -EINVAL;
  579. }
  580. priv->lddckr = icksel << 16;
  581. if (str) {
  582. priv->dot_clk = clk_get(&pdev->dev, str);
  583. if (IS_ERR(priv->dot_clk)) {
  584. dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
  585. return PTR_ERR(priv->dot_clk);
  586. }
  587. }
  588. atomic_set(&priv->hw_usecnt, -1);
  589. /* Runtime PM support involves two step for this driver:
  590. * 1) Enable Runtime PM
  591. * 2) Force Runtime PM Resume since hardware is accessed from probe()
  592. */
  593. pm_runtime_enable(priv->dev);
  594. pm_runtime_resume(priv->dev);
  595. return 0;
  596. }
  597. static int sh_mobile_lcdc_setcolreg(u_int regno,
  598. u_int red, u_int green, u_int blue,
  599. u_int transp, struct fb_info *info)
  600. {
  601. u32 *palette = info->pseudo_palette;
  602. if (regno >= PALETTE_NR)
  603. return -EINVAL;
  604. /* only FB_VISUAL_TRUECOLOR supported */
  605. red >>= 16 - info->var.red.length;
  606. green >>= 16 - info->var.green.length;
  607. blue >>= 16 - info->var.blue.length;
  608. transp >>= 16 - info->var.transp.length;
  609. palette[regno] = (red << info->var.red.offset) |
  610. (green << info->var.green.offset) |
  611. (blue << info->var.blue.offset) |
  612. (transp << info->var.transp.offset);
  613. return 0;
  614. }
  615. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  616. .id = "SH Mobile LCDC",
  617. .type = FB_TYPE_PACKED_PIXELS,
  618. .visual = FB_VISUAL_TRUECOLOR,
  619. .accel = FB_ACCEL_NONE,
  620. .xpanstep = 0,
  621. .ypanstep = 1,
  622. .ywrapstep = 0,
  623. };
  624. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  625. const struct fb_fillrect *rect)
  626. {
  627. sys_fillrect(info, rect);
  628. sh_mobile_lcdc_deferred_io_touch(info);
  629. }
  630. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  631. const struct fb_copyarea *area)
  632. {
  633. sys_copyarea(info, area);
  634. sh_mobile_lcdc_deferred_io_touch(info);
  635. }
  636. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  637. const struct fb_image *image)
  638. {
  639. sys_imageblit(info, image);
  640. sh_mobile_lcdc_deferred_io_touch(info);
  641. }
  642. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  643. struct fb_info *info)
  644. {
  645. struct sh_mobile_lcdc_chan *ch = info->par;
  646. if (info->var.xoffset == var->xoffset &&
  647. info->var.yoffset == var->yoffset)
  648. return 0; /* No change, do nothing */
  649. ch->new_pan_offset = (var->yoffset * info->fix.line_length) +
  650. (var->xoffset * (info->var.bits_per_pixel / 8));
  651. if (ch->new_pan_offset != ch->pan_offset) {
  652. unsigned long ldintr;
  653. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  654. ldintr |= LDINTR_VEE;
  655. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  656. sh_mobile_lcdc_deferred_io_touch(info);
  657. }
  658. return 0;
  659. }
  660. static struct fb_ops sh_mobile_lcdc_ops = {
  661. .owner = THIS_MODULE,
  662. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  663. .fb_read = fb_sys_read,
  664. .fb_write = fb_sys_write,
  665. .fb_fillrect = sh_mobile_lcdc_fillrect,
  666. .fb_copyarea = sh_mobile_lcdc_copyarea,
  667. .fb_imageblit = sh_mobile_lcdc_imageblit,
  668. .fb_pan_display = sh_mobile_fb_pan_display,
  669. };
  670. static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
  671. {
  672. switch (bpp) {
  673. case 16: /* PKF[4:0] = 00011 - RGB 565 */
  674. var->red.offset = 11;
  675. var->red.length = 5;
  676. var->green.offset = 5;
  677. var->green.length = 6;
  678. var->blue.offset = 0;
  679. var->blue.length = 5;
  680. var->transp.offset = 0;
  681. var->transp.length = 0;
  682. break;
  683. case 32: /* PKF[4:0] = 00000 - RGB 888
  684. * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
  685. * this may be because LDDDSR has word swap enabled..
  686. */
  687. var->red.offset = 0;
  688. var->red.length = 8;
  689. var->green.offset = 24;
  690. var->green.length = 8;
  691. var->blue.offset = 16;
  692. var->blue.length = 8;
  693. var->transp.offset = 0;
  694. var->transp.length = 0;
  695. break;
  696. default:
  697. return -EINVAL;
  698. }
  699. var->bits_per_pixel = bpp;
  700. var->red.msb_right = 0;
  701. var->green.msb_right = 0;
  702. var->blue.msb_right = 0;
  703. var->transp.msb_right = 0;
  704. return 0;
  705. }
  706. static int sh_mobile_lcdc_suspend(struct device *dev)
  707. {
  708. struct platform_device *pdev = to_platform_device(dev);
  709. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  710. return 0;
  711. }
  712. static int sh_mobile_lcdc_resume(struct device *dev)
  713. {
  714. struct platform_device *pdev = to_platform_device(dev);
  715. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  716. }
  717. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  718. {
  719. struct platform_device *pdev = to_platform_device(dev);
  720. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  721. struct sh_mobile_lcdc_chan *ch;
  722. int k, n;
  723. /* save per-channel registers */
  724. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  725. ch = &p->ch[k];
  726. if (!ch->enabled)
  727. continue;
  728. for (n = 0; n < NR_CH_REGS; n++)
  729. ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
  730. }
  731. /* save shared registers */
  732. for (n = 0; n < NR_SHARED_REGS; n++)
  733. p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
  734. /* turn off LCDC hardware */
  735. lcdc_write(p, _LDCNT1R, 0);
  736. return 0;
  737. }
  738. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  739. {
  740. struct platform_device *pdev = to_platform_device(dev);
  741. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  742. struct sh_mobile_lcdc_chan *ch;
  743. int k, n;
  744. /* restore per-channel registers */
  745. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  746. ch = &p->ch[k];
  747. if (!ch->enabled)
  748. continue;
  749. for (n = 0; n < NR_CH_REGS; n++)
  750. lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
  751. }
  752. /* restore shared registers */
  753. for (n = 0; n < NR_SHARED_REGS; n++)
  754. lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
  755. return 0;
  756. }
  757. static struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  758. .suspend = sh_mobile_lcdc_suspend,
  759. .resume = sh_mobile_lcdc_resume,
  760. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  761. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  762. };
  763. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  764. static int __init sh_mobile_lcdc_probe(struct platform_device *pdev)
  765. {
  766. struct fb_info *info;
  767. struct sh_mobile_lcdc_priv *priv;
  768. struct sh_mobile_lcdc_info *pdata;
  769. struct sh_mobile_lcdc_chan_cfg *cfg;
  770. struct resource *res;
  771. int error;
  772. void *buf;
  773. int i, j;
  774. if (!pdev->dev.platform_data) {
  775. dev_err(&pdev->dev, "no platform data defined\n");
  776. error = -EINVAL;
  777. goto err0;
  778. }
  779. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  780. i = platform_get_irq(pdev, 0);
  781. if (!res || i < 0) {
  782. dev_err(&pdev->dev, "cannot get platform resources\n");
  783. error = -ENOENT;
  784. goto err0;
  785. }
  786. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  787. if (!priv) {
  788. dev_err(&pdev->dev, "cannot allocate device data\n");
  789. error = -ENOMEM;
  790. goto err0;
  791. }
  792. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  793. dev_name(&pdev->dev), priv);
  794. if (error) {
  795. dev_err(&pdev->dev, "unable to request irq\n");
  796. goto err1;
  797. }
  798. priv->irq = i;
  799. priv->dev = &pdev->dev;
  800. platform_set_drvdata(pdev, priv);
  801. pdata = pdev->dev.platform_data;
  802. j = 0;
  803. for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  804. priv->ch[j].lcdc = priv;
  805. memcpy(&priv->ch[j].cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  806. error = sh_mobile_lcdc_check_interface(&priv->ch[i]);
  807. if (error) {
  808. dev_err(&pdev->dev, "unsupported interface type\n");
  809. goto err1;
  810. }
  811. init_waitqueue_head(&priv->ch[i].frame_end_wait);
  812. priv->ch[j].pan_offset = 0;
  813. priv->ch[j].new_pan_offset = 0;
  814. switch (pdata->ch[i].chan) {
  815. case LCDC_CHAN_MAINLCD:
  816. priv->ch[j].enabled = 1 << 1;
  817. priv->ch[j].reg_offs = lcdc_offs_mainlcd;
  818. j++;
  819. break;
  820. case LCDC_CHAN_SUBLCD:
  821. priv->ch[j].enabled = 1 << 2;
  822. priv->ch[j].reg_offs = lcdc_offs_sublcd;
  823. j++;
  824. break;
  825. }
  826. }
  827. if (!j) {
  828. dev_err(&pdev->dev, "no channels defined\n");
  829. error = -EINVAL;
  830. goto err1;
  831. }
  832. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  833. if (error) {
  834. dev_err(&pdev->dev, "unable to setup clocks\n");
  835. goto err1;
  836. }
  837. priv->base = ioremap_nocache(res->start, (res->end - res->start) + 1);
  838. for (i = 0; i < j; i++) {
  839. cfg = &priv->ch[i].cfg;
  840. priv->ch[i].info = framebuffer_alloc(0, &pdev->dev);
  841. if (!priv->ch[i].info) {
  842. dev_err(&pdev->dev, "unable to allocate fb_info\n");
  843. error = -ENOMEM;
  844. break;
  845. }
  846. info = priv->ch[i].info;
  847. info->fbops = &sh_mobile_lcdc_ops;
  848. info->var.xres = info->var.xres_virtual = cfg->lcd_cfg.xres;
  849. info->var.yres = cfg->lcd_cfg.yres;
  850. /* Default Y virtual resolution is 2x panel size */
  851. info->var.yres_virtual = info->var.yres * 2;
  852. info->var.width = cfg->lcd_size_cfg.width;
  853. info->var.height = cfg->lcd_size_cfg.height;
  854. info->var.activate = FB_ACTIVATE_NOW;
  855. error = sh_mobile_lcdc_set_bpp(&info->var, cfg->bpp);
  856. if (error)
  857. break;
  858. info->fix = sh_mobile_lcdc_fix;
  859. info->fix.line_length = cfg->lcd_cfg.xres * (cfg->bpp / 8);
  860. info->fix.smem_len = info->fix.line_length *
  861. info->var.yres_virtual;
  862. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  863. &priv->ch[i].dma_handle, GFP_KERNEL);
  864. if (!buf) {
  865. dev_err(&pdev->dev, "unable to allocate buffer\n");
  866. error = -ENOMEM;
  867. break;
  868. }
  869. info->pseudo_palette = &priv->ch[i].pseudo_palette;
  870. info->flags = FBINFO_FLAG_DEFAULT;
  871. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  872. if (error < 0) {
  873. dev_err(&pdev->dev, "unable to allocate cmap\n");
  874. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  875. buf, priv->ch[i].dma_handle);
  876. break;
  877. }
  878. memset(buf, 0, info->fix.smem_len);
  879. info->fix.smem_start = priv->ch[i].dma_handle;
  880. info->screen_base = buf;
  881. info->device = &pdev->dev;
  882. info->par = &priv->ch[i];
  883. }
  884. if (error)
  885. goto err1;
  886. error = sh_mobile_lcdc_start(priv);
  887. if (error) {
  888. dev_err(&pdev->dev, "unable to start hardware\n");
  889. goto err1;
  890. }
  891. for (i = 0; i < j; i++) {
  892. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  893. info = ch->info;
  894. if (info->fbdefio) {
  895. priv->ch->sglist = vmalloc(sizeof(struct scatterlist) *
  896. info->fix.smem_len >> PAGE_SHIFT);
  897. if (!priv->ch->sglist) {
  898. dev_err(&pdev->dev, "cannot allocate sglist\n");
  899. goto err1;
  900. }
  901. }
  902. error = register_framebuffer(info);
  903. if (error < 0)
  904. goto err1;
  905. dev_info(info->dev,
  906. "registered %s/%s as %dx%d %dbpp.\n",
  907. pdev->name,
  908. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  909. "mainlcd" : "sublcd",
  910. (int) ch->cfg.lcd_cfg.xres,
  911. (int) ch->cfg.lcd_cfg.yres,
  912. ch->cfg.bpp);
  913. /* deferred io mode: disable clock to save power */
  914. if (info->fbdefio)
  915. sh_mobile_lcdc_clk_off(priv);
  916. }
  917. return 0;
  918. err1:
  919. sh_mobile_lcdc_remove(pdev);
  920. err0:
  921. return error;
  922. }
  923. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  924. {
  925. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  926. struct fb_info *info;
  927. int i;
  928. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  929. if (priv->ch[i].info->dev)
  930. unregister_framebuffer(priv->ch[i].info);
  931. sh_mobile_lcdc_stop(priv);
  932. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  933. info = priv->ch[i].info;
  934. if (!info || !info->device)
  935. continue;
  936. if (priv->ch[i].sglist)
  937. vfree(priv->ch[i].sglist);
  938. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  939. info->screen_base, priv->ch[i].dma_handle);
  940. fb_dealloc_cmap(&info->cmap);
  941. framebuffer_release(info);
  942. }
  943. if (priv->dot_clk)
  944. clk_put(priv->dot_clk);
  945. pm_runtime_disable(priv->dev);
  946. if (priv->base)
  947. iounmap(priv->base);
  948. if (priv->irq)
  949. free_irq(priv->irq, priv);
  950. kfree(priv);
  951. return 0;
  952. }
  953. static struct platform_driver sh_mobile_lcdc_driver = {
  954. .driver = {
  955. .name = "sh_mobile_lcdc_fb",
  956. .owner = THIS_MODULE,
  957. .pm = &sh_mobile_lcdc_dev_pm_ops,
  958. },
  959. .probe = sh_mobile_lcdc_probe,
  960. .remove = sh_mobile_lcdc_remove,
  961. };
  962. static int __init sh_mobile_lcdc_init(void)
  963. {
  964. return platform_driver_register(&sh_mobile_lcdc_driver);
  965. }
  966. static void __exit sh_mobile_lcdc_exit(void)
  967. {
  968. platform_driver_unregister(&sh_mobile_lcdc_driver);
  969. }
  970. module_init(sh_mobile_lcdc_init);
  971. module_exit(sh_mobile_lcdc_exit);
  972. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  973. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  974. MODULE_LICENSE("GPL v2");