sh_mobile_lcdcfb.c 24 KB

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