sh_mobile_lcdcfb.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. unsigned int nr_pages;
  252. /* enable clocks before accessing hardware */
  253. sh_mobile_lcdc_clk_on(ch->lcdc);
  254. nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  255. dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  256. /* trigger panel update */
  257. lcdc_write_chan(ch, LDSM2R, 1);
  258. dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  259. }
  260. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  261. {
  262. struct fb_deferred_io *fbdefio = info->fbdefio;
  263. if (fbdefio)
  264. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  265. }
  266. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  267. {
  268. struct sh_mobile_lcdc_priv *priv = data;
  269. struct sh_mobile_lcdc_chan *ch;
  270. unsigned long tmp;
  271. unsigned long ldintr;
  272. int is_sub;
  273. int k;
  274. /* acknowledge interrupt */
  275. ldintr = tmp = lcdc_read(priv, _LDINTR);
  276. /*
  277. * disable further VSYNC End IRQs, preserve all other enabled IRQs,
  278. * write 0 to bits 0-6 to ack all triggered IRQs.
  279. */
  280. tmp &= 0xffffff00 & ~LDINTR_VEE;
  281. lcdc_write(priv, _LDINTR, tmp);
  282. /* figure out if this interrupt is for main or sub lcd */
  283. is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
  284. /* wake up channel and disable clocks */
  285. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  286. ch = &priv->ch[k];
  287. if (!ch->enabled)
  288. continue;
  289. /* Frame Start */
  290. if (ldintr & LDINTR_FS) {
  291. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  292. ch->frame_end = 1;
  293. wake_up(&ch->frame_end_wait);
  294. sh_mobile_lcdc_clk_off(priv);
  295. }
  296. }
  297. /* VSYNC End */
  298. if (ldintr & LDINTR_VES) {
  299. unsigned long ldrcntr = lcdc_read(priv, _LDRCNTR);
  300. /* Set the source address for the next refresh */
  301. lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle +
  302. ch->new_pan_offset);
  303. if (lcdc_chan_is_sublcd(ch))
  304. lcdc_write(ch->lcdc, _LDRCNTR,
  305. ldrcntr ^ LDRCNTR_SRS);
  306. else
  307. lcdc_write(ch->lcdc, _LDRCNTR,
  308. ldrcntr ^ LDRCNTR_MRS);
  309. ch->pan_offset = ch->new_pan_offset;
  310. }
  311. }
  312. return IRQ_HANDLED;
  313. }
  314. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  315. int start)
  316. {
  317. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  318. int k;
  319. /* start or stop the lcdc */
  320. if (start)
  321. lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
  322. else
  323. lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
  324. /* wait until power is applied/stopped on all channels */
  325. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  326. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  327. while (1) {
  328. tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
  329. if (start && tmp == 3)
  330. break;
  331. if (!start && tmp == 0)
  332. break;
  333. cpu_relax();
  334. }
  335. if (!start)
  336. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  337. }
  338. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  339. {
  340. struct sh_mobile_lcdc_chan *ch;
  341. struct fb_videomode *lcd_cfg;
  342. struct sh_mobile_lcdc_board_cfg *board_cfg;
  343. unsigned long tmp;
  344. int k, m;
  345. int ret = 0;
  346. /* enable clocks before accessing the hardware */
  347. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  348. if (priv->ch[k].enabled)
  349. sh_mobile_lcdc_clk_on(priv);
  350. /* reset */
  351. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
  352. lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
  353. /* enable LCDC channels */
  354. tmp = lcdc_read(priv, _LDCNT2R);
  355. tmp |= priv->ch[0].enabled;
  356. tmp |= priv->ch[1].enabled;
  357. lcdc_write(priv, _LDCNT2R, tmp);
  358. /* read data from external memory, avoid using the BEU for now */
  359. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
  360. /* stop the lcdc first */
  361. sh_mobile_lcdc_start_stop(priv, 0);
  362. /* configure clocks */
  363. tmp = priv->lddckr;
  364. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  365. ch = &priv->ch[k];
  366. if (!priv->ch[k].enabled)
  367. continue;
  368. m = ch->cfg.clock_divider;
  369. if (!m)
  370. continue;
  371. if (m == 1)
  372. m = 1 << 6;
  373. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  374. lcdc_write_chan(ch, LDDCKPAT1R, 0x00000000);
  375. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  376. }
  377. lcdc_write(priv, _LDDCKR, tmp);
  378. /* start dotclock again */
  379. lcdc_write(priv, _LDDCKSTPR, 0);
  380. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  381. /* interrupts are disabled to begin with */
  382. lcdc_write(priv, _LDINTR, 0);
  383. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  384. ch = &priv->ch[k];
  385. lcd_cfg = &ch->cfg.lcd_cfg;
  386. if (!ch->enabled)
  387. continue;
  388. tmp = ch->ldmt1r_value;
  389. tmp |= (lcd_cfg->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
  390. tmp |= (lcd_cfg->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
  391. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
  392. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
  393. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
  394. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
  395. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
  396. lcdc_write_chan(ch, LDMT1R, tmp);
  397. /* setup SYS bus */
  398. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  399. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  400. /* horizontal configuration */
  401. tmp = lcd_cfg->xres + lcd_cfg->hsync_len;
  402. tmp += lcd_cfg->left_margin;
  403. tmp += lcd_cfg->right_margin;
  404. tmp /= 8; /* HTCN */
  405. tmp |= (lcd_cfg->xres / 8) << 16; /* HDCN */
  406. lcdc_write_chan(ch, LDHCNR, tmp);
  407. tmp = lcd_cfg->xres;
  408. tmp += lcd_cfg->right_margin;
  409. tmp /= 8; /* HSYNP */
  410. tmp |= (lcd_cfg->hsync_len / 8) << 16; /* HSYNW */
  411. lcdc_write_chan(ch, LDHSYNR, tmp);
  412. /* power supply */
  413. lcdc_write_chan(ch, LDPMR, 0);
  414. /* vertical configuration */
  415. tmp = lcd_cfg->yres + lcd_cfg->vsync_len;
  416. tmp += lcd_cfg->upper_margin;
  417. tmp += lcd_cfg->lower_margin; /* VTLN */
  418. tmp |= lcd_cfg->yres << 16; /* VDLN */
  419. lcdc_write_chan(ch, LDVLNR, tmp);
  420. tmp = lcd_cfg->yres;
  421. tmp += lcd_cfg->lower_margin; /* VSYNP */
  422. tmp |= lcd_cfg->vsync_len << 16; /* VSYNW */
  423. lcdc_write_chan(ch, LDVSYNR, tmp);
  424. board_cfg = &ch->cfg.board_cfg;
  425. if (board_cfg->setup_sys)
  426. ret = board_cfg->setup_sys(board_cfg->board_data, ch,
  427. &sh_mobile_lcdc_sys_bus_ops);
  428. if (ret)
  429. return ret;
  430. }
  431. /* word and long word swap */
  432. lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
  433. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  434. ch = &priv->ch[k];
  435. if (!priv->ch[k].enabled)
  436. continue;
  437. /* set bpp format in PKF[4:0] */
  438. tmp = lcdc_read_chan(ch, LDDFR);
  439. tmp &= ~(0x0001001f);
  440. tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
  441. lcdc_write_chan(ch, LDDFR, tmp);
  442. /* point out our frame buffer */
  443. lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
  444. /* set line size */
  445. lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
  446. /* setup deferred io if SYS bus */
  447. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  448. if (ch->ldmt1r_value & (1 << 12) && tmp) {
  449. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  450. ch->defio.delay = msecs_to_jiffies(tmp);
  451. ch->info->fbdefio = &ch->defio;
  452. fb_deferred_io_init(ch->info);
  453. /* one-shot mode */
  454. lcdc_write_chan(ch, LDSM1R, 1);
  455. /* enable "Frame End Interrupt Enable" bit */
  456. lcdc_write(priv, _LDINTR, LDINTR_FE);
  457. } else {
  458. /* continuous read mode */
  459. lcdc_write_chan(ch, LDSM1R, 0);
  460. }
  461. }
  462. /* display output */
  463. lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
  464. /* start the lcdc */
  465. sh_mobile_lcdc_start_stop(priv, 1);
  466. priv->started = 1;
  467. /* tell the board code to enable the panel */
  468. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  469. ch = &priv->ch[k];
  470. if (!ch->enabled)
  471. continue;
  472. board_cfg = &ch->cfg.board_cfg;
  473. if (board_cfg->display_on)
  474. board_cfg->display_on(board_cfg->board_data);
  475. }
  476. return 0;
  477. }
  478. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  479. {
  480. struct sh_mobile_lcdc_chan *ch;
  481. struct sh_mobile_lcdc_board_cfg *board_cfg;
  482. int k;
  483. /* clean up deferred io and ask board code to disable panel */
  484. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  485. ch = &priv->ch[k];
  486. if (!ch->enabled)
  487. continue;
  488. /* deferred io mode:
  489. * flush frame, and wait for frame end interrupt
  490. * clean up deferred io and enable clock
  491. */
  492. if (ch->info->fbdefio) {
  493. ch->frame_end = 0;
  494. schedule_delayed_work(&ch->info->deferred_work, 0);
  495. wait_event(ch->frame_end_wait, ch->frame_end);
  496. fb_deferred_io_cleanup(ch->info);
  497. ch->info->fbdefio = NULL;
  498. sh_mobile_lcdc_clk_on(priv);
  499. }
  500. board_cfg = &ch->cfg.board_cfg;
  501. if (board_cfg->display_off)
  502. board_cfg->display_off(board_cfg->board_data);
  503. }
  504. /* stop the lcdc */
  505. if (priv->started) {
  506. sh_mobile_lcdc_start_stop(priv, 0);
  507. priv->started = 0;
  508. }
  509. /* stop clocks */
  510. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  511. if (priv->ch[k].enabled)
  512. sh_mobile_lcdc_clk_off(priv);
  513. }
  514. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  515. {
  516. int ifm, miftyp;
  517. switch (ch->cfg.interface_type) {
  518. case RGB8: ifm = 0; miftyp = 0; break;
  519. case RGB9: ifm = 0; miftyp = 4; break;
  520. case RGB12A: ifm = 0; miftyp = 5; break;
  521. case RGB12B: ifm = 0; miftyp = 6; break;
  522. case RGB16: ifm = 0; miftyp = 7; break;
  523. case RGB18: ifm = 0; miftyp = 10; break;
  524. case RGB24: ifm = 0; miftyp = 11; break;
  525. case SYS8A: ifm = 1; miftyp = 0; break;
  526. case SYS8B: ifm = 1; miftyp = 1; break;
  527. case SYS8C: ifm = 1; miftyp = 2; break;
  528. case SYS8D: ifm = 1; miftyp = 3; break;
  529. case SYS9: ifm = 1; miftyp = 4; break;
  530. case SYS12: ifm = 1; miftyp = 5; break;
  531. case SYS16A: ifm = 1; miftyp = 7; break;
  532. case SYS16B: ifm = 1; miftyp = 8; break;
  533. case SYS16C: ifm = 1; miftyp = 9; break;
  534. case SYS18: ifm = 1; miftyp = 10; break;
  535. case SYS24: ifm = 1; miftyp = 11; break;
  536. default: goto bad;
  537. }
  538. /* SUBLCD only supports SYS interface */
  539. if (lcdc_chan_is_sublcd(ch)) {
  540. if (ifm == 0)
  541. goto bad;
  542. else
  543. ifm = 0;
  544. }
  545. ch->ldmt1r_value = (ifm << 12) | miftyp;
  546. return 0;
  547. bad:
  548. return -EINVAL;
  549. }
  550. static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
  551. int clock_source,
  552. struct sh_mobile_lcdc_priv *priv)
  553. {
  554. char *str;
  555. int icksel;
  556. switch (clock_source) {
  557. case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
  558. case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
  559. case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
  560. default:
  561. return -EINVAL;
  562. }
  563. priv->lddckr = icksel << 16;
  564. if (str) {
  565. priv->dot_clk = clk_get(&pdev->dev, str);
  566. if (IS_ERR(priv->dot_clk)) {
  567. dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
  568. return PTR_ERR(priv->dot_clk);
  569. }
  570. }
  571. atomic_set(&priv->hw_usecnt, -1);
  572. /* Runtime PM support involves two step for this driver:
  573. * 1) Enable Runtime PM
  574. * 2) Force Runtime PM Resume since hardware is accessed from probe()
  575. */
  576. pm_runtime_enable(priv->dev);
  577. pm_runtime_resume(priv->dev);
  578. return 0;
  579. }
  580. static int sh_mobile_lcdc_setcolreg(u_int regno,
  581. u_int red, u_int green, u_int blue,
  582. u_int transp, struct fb_info *info)
  583. {
  584. u32 *palette = info->pseudo_palette;
  585. if (regno >= PALETTE_NR)
  586. return -EINVAL;
  587. /* only FB_VISUAL_TRUECOLOR supported */
  588. red >>= 16 - info->var.red.length;
  589. green >>= 16 - info->var.green.length;
  590. blue >>= 16 - info->var.blue.length;
  591. transp >>= 16 - info->var.transp.length;
  592. palette[regno] = (red << info->var.red.offset) |
  593. (green << info->var.green.offset) |
  594. (blue << info->var.blue.offset) |
  595. (transp << info->var.transp.offset);
  596. return 0;
  597. }
  598. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  599. .id = "SH Mobile LCDC",
  600. .type = FB_TYPE_PACKED_PIXELS,
  601. .visual = FB_VISUAL_TRUECOLOR,
  602. .accel = FB_ACCEL_NONE,
  603. .xpanstep = 0,
  604. .ypanstep = 1,
  605. .ywrapstep = 0,
  606. };
  607. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  608. const struct fb_fillrect *rect)
  609. {
  610. sys_fillrect(info, rect);
  611. sh_mobile_lcdc_deferred_io_touch(info);
  612. }
  613. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  614. const struct fb_copyarea *area)
  615. {
  616. sys_copyarea(info, area);
  617. sh_mobile_lcdc_deferred_io_touch(info);
  618. }
  619. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  620. const struct fb_image *image)
  621. {
  622. sys_imageblit(info, image);
  623. sh_mobile_lcdc_deferred_io_touch(info);
  624. }
  625. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  626. struct fb_info *info)
  627. {
  628. struct sh_mobile_lcdc_chan *ch = info->par;
  629. if (info->var.xoffset == var->xoffset &&
  630. info->var.yoffset == var->yoffset)
  631. return 0; /* No change, do nothing */
  632. ch->new_pan_offset = (var->yoffset * info->fix.line_length) +
  633. (var->xoffset * (info->var.bits_per_pixel / 8));
  634. if (ch->new_pan_offset != ch->pan_offset) {
  635. unsigned long ldintr;
  636. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  637. ldintr |= LDINTR_VEE;
  638. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  639. sh_mobile_lcdc_deferred_io_touch(info);
  640. }
  641. return 0;
  642. }
  643. static struct fb_ops sh_mobile_lcdc_ops = {
  644. .owner = THIS_MODULE,
  645. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  646. .fb_read = fb_sys_read,
  647. .fb_write = fb_sys_write,
  648. .fb_fillrect = sh_mobile_lcdc_fillrect,
  649. .fb_copyarea = sh_mobile_lcdc_copyarea,
  650. .fb_imageblit = sh_mobile_lcdc_imageblit,
  651. .fb_pan_display = sh_mobile_fb_pan_display,
  652. };
  653. static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
  654. {
  655. switch (bpp) {
  656. case 16: /* PKF[4:0] = 00011 - RGB 565 */
  657. var->red.offset = 11;
  658. var->red.length = 5;
  659. var->green.offset = 5;
  660. var->green.length = 6;
  661. var->blue.offset = 0;
  662. var->blue.length = 5;
  663. var->transp.offset = 0;
  664. var->transp.length = 0;
  665. break;
  666. case 32: /* PKF[4:0] = 00000 - RGB 888
  667. * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
  668. * this may be because LDDDSR has word swap enabled..
  669. */
  670. var->red.offset = 0;
  671. var->red.length = 8;
  672. var->green.offset = 24;
  673. var->green.length = 8;
  674. var->blue.offset = 16;
  675. var->blue.length = 8;
  676. var->transp.offset = 0;
  677. var->transp.length = 0;
  678. break;
  679. default:
  680. return -EINVAL;
  681. }
  682. var->bits_per_pixel = bpp;
  683. var->red.msb_right = 0;
  684. var->green.msb_right = 0;
  685. var->blue.msb_right = 0;
  686. var->transp.msb_right = 0;
  687. return 0;
  688. }
  689. static int sh_mobile_lcdc_suspend(struct device *dev)
  690. {
  691. struct platform_device *pdev = to_platform_device(dev);
  692. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  693. return 0;
  694. }
  695. static int sh_mobile_lcdc_resume(struct device *dev)
  696. {
  697. struct platform_device *pdev = to_platform_device(dev);
  698. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  699. }
  700. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  701. {
  702. struct platform_device *pdev = to_platform_device(dev);
  703. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  704. struct sh_mobile_lcdc_chan *ch;
  705. int k, n;
  706. /* save per-channel registers */
  707. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  708. ch = &p->ch[k];
  709. if (!ch->enabled)
  710. continue;
  711. for (n = 0; n < NR_CH_REGS; n++)
  712. ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
  713. }
  714. /* save shared registers */
  715. for (n = 0; n < NR_SHARED_REGS; n++)
  716. p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
  717. /* turn off LCDC hardware */
  718. lcdc_write(p, _LDCNT1R, 0);
  719. return 0;
  720. }
  721. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  722. {
  723. struct platform_device *pdev = to_platform_device(dev);
  724. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  725. struct sh_mobile_lcdc_chan *ch;
  726. int k, n;
  727. /* restore per-channel registers */
  728. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  729. ch = &p->ch[k];
  730. if (!ch->enabled)
  731. continue;
  732. for (n = 0; n < NR_CH_REGS; n++)
  733. lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
  734. }
  735. /* restore shared registers */
  736. for (n = 0; n < NR_SHARED_REGS; n++)
  737. lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
  738. return 0;
  739. }
  740. static struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  741. .suspend = sh_mobile_lcdc_suspend,
  742. .resume = sh_mobile_lcdc_resume,
  743. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  744. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  745. };
  746. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  747. static int __init sh_mobile_lcdc_probe(struct platform_device *pdev)
  748. {
  749. struct fb_info *info;
  750. struct sh_mobile_lcdc_priv *priv;
  751. struct sh_mobile_lcdc_info *pdata;
  752. struct sh_mobile_lcdc_chan_cfg *cfg;
  753. struct resource *res;
  754. int error;
  755. void *buf;
  756. int i, j;
  757. if (!pdev->dev.platform_data) {
  758. dev_err(&pdev->dev, "no platform data defined\n");
  759. error = -EINVAL;
  760. goto err0;
  761. }
  762. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  763. i = platform_get_irq(pdev, 0);
  764. if (!res || i < 0) {
  765. dev_err(&pdev->dev, "cannot get platform resources\n");
  766. error = -ENOENT;
  767. goto err0;
  768. }
  769. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  770. if (!priv) {
  771. dev_err(&pdev->dev, "cannot allocate device data\n");
  772. error = -ENOMEM;
  773. goto err0;
  774. }
  775. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  776. dev_name(&pdev->dev), priv);
  777. if (error) {
  778. dev_err(&pdev->dev, "unable to request irq\n");
  779. goto err1;
  780. }
  781. priv->irq = i;
  782. priv->dev = &pdev->dev;
  783. platform_set_drvdata(pdev, priv);
  784. pdata = pdev->dev.platform_data;
  785. j = 0;
  786. for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  787. priv->ch[j].lcdc = priv;
  788. memcpy(&priv->ch[j].cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  789. error = sh_mobile_lcdc_check_interface(&priv->ch[i]);
  790. if (error) {
  791. dev_err(&pdev->dev, "unsupported interface type\n");
  792. goto err1;
  793. }
  794. init_waitqueue_head(&priv->ch[i].frame_end_wait);
  795. priv->ch[j].pan_offset = 0;
  796. priv->ch[j].new_pan_offset = 0;
  797. switch (pdata->ch[i].chan) {
  798. case LCDC_CHAN_MAINLCD:
  799. priv->ch[j].enabled = 1 << 1;
  800. priv->ch[j].reg_offs = lcdc_offs_mainlcd;
  801. j++;
  802. break;
  803. case LCDC_CHAN_SUBLCD:
  804. priv->ch[j].enabled = 1 << 2;
  805. priv->ch[j].reg_offs = lcdc_offs_sublcd;
  806. j++;
  807. break;
  808. }
  809. }
  810. if (!j) {
  811. dev_err(&pdev->dev, "no channels defined\n");
  812. error = -EINVAL;
  813. goto err1;
  814. }
  815. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  816. if (error) {
  817. dev_err(&pdev->dev, "unable to setup clocks\n");
  818. goto err1;
  819. }
  820. priv->base = ioremap_nocache(res->start, (res->end - res->start) + 1);
  821. for (i = 0; i < j; i++) {
  822. cfg = &priv->ch[i].cfg;
  823. priv->ch[i].info = framebuffer_alloc(0, &pdev->dev);
  824. if (!priv->ch[i].info) {
  825. dev_err(&pdev->dev, "unable to allocate fb_info\n");
  826. error = -ENOMEM;
  827. break;
  828. }
  829. info = priv->ch[i].info;
  830. info->fbops = &sh_mobile_lcdc_ops;
  831. info->var.xres = info->var.xres_virtual = cfg->lcd_cfg.xres;
  832. info->var.yres = cfg->lcd_cfg.yres;
  833. /* Default Y virtual resolution is 2x panel size */
  834. info->var.yres_virtual = info->var.yres * 2;
  835. info->var.width = cfg->lcd_size_cfg.width;
  836. info->var.height = cfg->lcd_size_cfg.height;
  837. info->var.activate = FB_ACTIVATE_NOW;
  838. error = sh_mobile_lcdc_set_bpp(&info->var, cfg->bpp);
  839. if (error)
  840. break;
  841. info->fix = sh_mobile_lcdc_fix;
  842. info->fix.line_length = cfg->lcd_cfg.xres * (cfg->bpp / 8);
  843. info->fix.smem_len = info->fix.line_length *
  844. info->var.yres_virtual;
  845. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  846. &priv->ch[i].dma_handle, GFP_KERNEL);
  847. if (!buf) {
  848. dev_err(&pdev->dev, "unable to allocate buffer\n");
  849. error = -ENOMEM;
  850. break;
  851. }
  852. info->pseudo_palette = &priv->ch[i].pseudo_palette;
  853. info->flags = FBINFO_FLAG_DEFAULT;
  854. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  855. if (error < 0) {
  856. dev_err(&pdev->dev, "unable to allocate cmap\n");
  857. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  858. buf, priv->ch[i].dma_handle);
  859. break;
  860. }
  861. memset(buf, 0, info->fix.smem_len);
  862. info->fix.smem_start = priv->ch[i].dma_handle;
  863. info->screen_base = buf;
  864. info->device = &pdev->dev;
  865. info->par = &priv->ch[i];
  866. }
  867. if (error)
  868. goto err1;
  869. error = sh_mobile_lcdc_start(priv);
  870. if (error) {
  871. dev_err(&pdev->dev, "unable to start hardware\n");
  872. goto err1;
  873. }
  874. for (i = 0; i < j; i++) {
  875. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  876. info = ch->info;
  877. if (info->fbdefio) {
  878. priv->ch->sglist = vmalloc(sizeof(struct scatterlist) *
  879. info->fix.smem_len >> PAGE_SHIFT);
  880. if (!priv->ch->sglist) {
  881. dev_err(&pdev->dev, "cannot allocate sglist\n");
  882. goto err1;
  883. }
  884. }
  885. error = register_framebuffer(info);
  886. if (error < 0)
  887. goto err1;
  888. dev_info(info->dev,
  889. "registered %s/%s as %dx%d %dbpp.\n",
  890. pdev->name,
  891. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  892. "mainlcd" : "sublcd",
  893. (int) ch->cfg.lcd_cfg.xres,
  894. (int) ch->cfg.lcd_cfg.yres,
  895. ch->cfg.bpp);
  896. /* deferred io mode: disable clock to save power */
  897. if (info->fbdefio)
  898. sh_mobile_lcdc_clk_off(priv);
  899. }
  900. return 0;
  901. err1:
  902. sh_mobile_lcdc_remove(pdev);
  903. err0:
  904. return error;
  905. }
  906. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  907. {
  908. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  909. struct fb_info *info;
  910. int i;
  911. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  912. if (priv->ch[i].info->dev)
  913. unregister_framebuffer(priv->ch[i].info);
  914. sh_mobile_lcdc_stop(priv);
  915. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  916. info = priv->ch[i].info;
  917. if (!info || !info->device)
  918. continue;
  919. if (priv->ch[i].sglist)
  920. vfree(priv->ch[i].sglist);
  921. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  922. info->screen_base, priv->ch[i].dma_handle);
  923. fb_dealloc_cmap(&info->cmap);
  924. framebuffer_release(info);
  925. }
  926. if (priv->dot_clk)
  927. clk_put(priv->dot_clk);
  928. pm_runtime_disable(priv->dev);
  929. if (priv->base)
  930. iounmap(priv->base);
  931. if (priv->irq)
  932. free_irq(priv->irq, priv);
  933. kfree(priv);
  934. return 0;
  935. }
  936. static struct platform_driver sh_mobile_lcdc_driver = {
  937. .driver = {
  938. .name = "sh_mobile_lcdc_fb",
  939. .owner = THIS_MODULE,
  940. .pm = &sh_mobile_lcdc_dev_pm_ops,
  941. },
  942. .probe = sh_mobile_lcdc_probe,
  943. .remove = sh_mobile_lcdc_remove,
  944. };
  945. static int __init sh_mobile_lcdc_init(void)
  946. {
  947. return platform_driver_register(&sh_mobile_lcdc_driver);
  948. }
  949. static void __exit sh_mobile_lcdc_exit(void)
  950. {
  951. platform_driver_unregister(&sh_mobile_lcdc_driver);
  952. }
  953. module_init(sh_mobile_lcdc_init);
  954. module_exit(sh_mobile_lcdc_exit);
  955. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  956. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  957. MODULE_LICENSE("GPL v2");