sh_mobile_lcdcfb.c 23 KB

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