sh_mobile_lcdcfb.c 27 KB

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