sh_mobile_lcdcfb.c 23 KB

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