sh_mobile_lcdcfb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. atomic_t clk_usecnt;
  42. struct clk *dot_clk;
  43. struct clk *clk;
  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. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  141. }
  142. static void lcdc_sys_write_data(void *handle, unsigned long data)
  143. {
  144. struct sh_mobile_lcdc_chan *ch = handle;
  145. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
  146. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  147. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  148. lcdc_wait_bit(ch->lcdc, _LDSR, 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. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  158. return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
  159. }
  160. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  161. lcdc_sys_write_index,
  162. lcdc_sys_write_data,
  163. lcdc_sys_read_data,
  164. };
  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. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  182. struct list_head *pagelist)
  183. {
  184. struct sh_mobile_lcdc_chan *ch = info->par;
  185. unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
  186. struct page *page;
  187. int nr_pages = 0;
  188. sg_init_table(ch->sglist, nr_pages_max);
  189. list_for_each_entry(page, pagelist, lru)
  190. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  191. return nr_pages;
  192. }
  193. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  194. struct list_head *pagelist)
  195. {
  196. struct sh_mobile_lcdc_chan *ch = info->par;
  197. unsigned int nr_pages;
  198. /* enable clocks before accessing hardware */
  199. sh_mobile_lcdc_clk_on(ch->lcdc);
  200. nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  201. dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  202. /* trigger panel update */
  203. lcdc_write_chan(ch, LDSM2R, 1);
  204. dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  205. }
  206. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  207. {
  208. struct fb_deferred_io *fbdefio = info->fbdefio;
  209. if (fbdefio)
  210. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  211. }
  212. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  213. {
  214. struct sh_mobile_lcdc_priv *priv = data;
  215. struct sh_mobile_lcdc_chan *ch;
  216. unsigned long tmp;
  217. int is_sub;
  218. int k;
  219. /* acknowledge interrupt */
  220. tmp = lcdc_read(priv, _LDINTR);
  221. tmp &= 0xffffff00; /* mask in high 24 bits */
  222. tmp |= 0x000000ff ^ LDINTR_FS; /* status in low 8 */
  223. lcdc_write(priv, _LDINTR, tmp);
  224. /* figure out if this interrupt is for main or sub lcd */
  225. is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
  226. /* wake up channel and disable clocks*/
  227. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  228. ch = &priv->ch[k];
  229. if (!ch->enabled)
  230. continue;
  231. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  232. ch->frame_end = 1;
  233. wake_up(&ch->frame_end_wait);
  234. sh_mobile_lcdc_clk_off(priv);
  235. }
  236. }
  237. return IRQ_HANDLED;
  238. }
  239. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  240. int start)
  241. {
  242. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  243. int k;
  244. /* start or stop the lcdc */
  245. if (start)
  246. lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
  247. else
  248. lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
  249. /* wait until power is applied/stopped on all channels */
  250. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  251. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  252. while (1) {
  253. tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
  254. if (start && tmp == 3)
  255. break;
  256. if (!start && tmp == 0)
  257. break;
  258. cpu_relax();
  259. }
  260. if (!start)
  261. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  262. }
  263. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  264. {
  265. struct sh_mobile_lcdc_chan *ch;
  266. struct fb_videomode *lcd_cfg;
  267. struct sh_mobile_lcdc_board_cfg *board_cfg;
  268. unsigned long tmp;
  269. int k, m;
  270. int ret = 0;
  271. /* enable clocks before accessing the hardware */
  272. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  273. if (priv->ch[k].enabled)
  274. sh_mobile_lcdc_clk_on(priv);
  275. /* reset */
  276. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
  277. lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
  278. /* enable LCDC channels */
  279. tmp = lcdc_read(priv, _LDCNT2R);
  280. tmp |= priv->ch[0].enabled;
  281. tmp |= priv->ch[1].enabled;
  282. lcdc_write(priv, _LDCNT2R, tmp);
  283. /* read data from external memory, avoid using the BEU for now */
  284. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
  285. /* stop the lcdc first */
  286. sh_mobile_lcdc_start_stop(priv, 0);
  287. /* configure clocks */
  288. tmp = priv->lddckr;
  289. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  290. ch = &priv->ch[k];
  291. if (!priv->ch[k].enabled)
  292. continue;
  293. m = ch->cfg.clock_divider;
  294. if (!m)
  295. continue;
  296. if (m == 1)
  297. m = 1 << 6;
  298. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  299. lcdc_write_chan(ch, LDDCKPAT1R, 0x00000000);
  300. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  301. }
  302. lcdc_write(priv, _LDDCKR, tmp);
  303. /* start dotclock again */
  304. lcdc_write(priv, _LDDCKSTPR, 0);
  305. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  306. /* interrupts are disabled to begin with */
  307. lcdc_write(priv, _LDINTR, 0);
  308. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  309. ch = &priv->ch[k];
  310. lcd_cfg = &ch->cfg.lcd_cfg;
  311. if (!ch->enabled)
  312. continue;
  313. tmp = ch->ldmt1r_value;
  314. tmp |= (lcd_cfg->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
  315. tmp |= (lcd_cfg->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
  316. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
  317. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
  318. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
  319. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
  320. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
  321. lcdc_write_chan(ch, LDMT1R, tmp);
  322. /* setup SYS bus */
  323. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  324. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  325. /* horizontal configuration */
  326. tmp = lcd_cfg->xres + lcd_cfg->hsync_len;
  327. tmp += lcd_cfg->left_margin;
  328. tmp += lcd_cfg->right_margin;
  329. tmp /= 8; /* HTCN */
  330. tmp |= (lcd_cfg->xres / 8) << 16; /* HDCN */
  331. lcdc_write_chan(ch, LDHCNR, tmp);
  332. tmp = lcd_cfg->xres;
  333. tmp += lcd_cfg->right_margin;
  334. tmp /= 8; /* HSYNP */
  335. tmp |= (lcd_cfg->hsync_len / 8) << 16; /* HSYNW */
  336. lcdc_write_chan(ch, LDHSYNR, tmp);
  337. /* power supply */
  338. lcdc_write_chan(ch, LDPMR, 0);
  339. /* vertical configuration */
  340. tmp = lcd_cfg->yres + lcd_cfg->vsync_len;
  341. tmp += lcd_cfg->upper_margin;
  342. tmp += lcd_cfg->lower_margin; /* VTLN */
  343. tmp |= lcd_cfg->yres << 16; /* VDLN */
  344. lcdc_write_chan(ch, LDVLNR, tmp);
  345. tmp = lcd_cfg->yres;
  346. tmp += lcd_cfg->lower_margin; /* VSYNP */
  347. tmp |= lcd_cfg->vsync_len << 16; /* VSYNW */
  348. lcdc_write_chan(ch, LDVSYNR, tmp);
  349. board_cfg = &ch->cfg.board_cfg;
  350. if (board_cfg->setup_sys)
  351. ret = board_cfg->setup_sys(board_cfg->board_data, ch,
  352. &sh_mobile_lcdc_sys_bus_ops);
  353. if (ret)
  354. return ret;
  355. }
  356. /* word and long word swap */
  357. lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
  358. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  359. ch = &priv->ch[k];
  360. if (!priv->ch[k].enabled)
  361. continue;
  362. /* set bpp format in PKF[4:0] */
  363. tmp = lcdc_read_chan(ch, LDDFR);
  364. tmp &= ~(0x0001001f);
  365. tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
  366. lcdc_write_chan(ch, LDDFR, tmp);
  367. /* point out our frame buffer */
  368. lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
  369. /* set line size */
  370. lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
  371. /* setup deferred io if SYS bus */
  372. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  373. if (ch->ldmt1r_value & (1 << 12) && tmp) {
  374. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  375. ch->defio.delay = msecs_to_jiffies(tmp);
  376. ch->info->fbdefio = &ch->defio;
  377. fb_deferred_io_init(ch->info);
  378. /* one-shot mode */
  379. lcdc_write_chan(ch, LDSM1R, 1);
  380. /* enable "Frame End Interrupt Enable" bit */
  381. lcdc_write(priv, _LDINTR, LDINTR_FE);
  382. } else {
  383. /* continuous read mode */
  384. lcdc_write_chan(ch, LDSM1R, 0);
  385. }
  386. }
  387. /* display output */
  388. lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
  389. /* start the lcdc */
  390. sh_mobile_lcdc_start_stop(priv, 1);
  391. priv->started = 1;
  392. /* tell the board code to enable the panel */
  393. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  394. ch = &priv->ch[k];
  395. if (!ch->enabled)
  396. continue;
  397. board_cfg = &ch->cfg.board_cfg;
  398. if (board_cfg->display_on)
  399. board_cfg->display_on(board_cfg->board_data);
  400. }
  401. return 0;
  402. }
  403. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  404. {
  405. struct sh_mobile_lcdc_chan *ch;
  406. struct sh_mobile_lcdc_board_cfg *board_cfg;
  407. int k;
  408. /* clean up deferred io and ask board code to disable panel */
  409. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  410. ch = &priv->ch[k];
  411. if (!ch->enabled)
  412. continue;
  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. char clk_name[8];
  480. char *str;
  481. int icksel;
  482. switch (clock_source) {
  483. case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
  484. case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
  485. case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
  486. default:
  487. return -EINVAL;
  488. }
  489. priv->lddckr = icksel << 16;
  490. atomic_set(&priv->clk_usecnt, -1);
  491. snprintf(clk_name, sizeof(clk_name), "lcdc%d", pdev->id);
  492. priv->clk = clk_get(&pdev->dev, clk_name);
  493. if (IS_ERR(priv->clk)) {
  494. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  495. return PTR_ERR(priv->clk);
  496. }
  497. if (str) {
  498. priv->dot_clk = clk_get(&pdev->dev, str);
  499. if (IS_ERR(priv->dot_clk)) {
  500. dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
  501. clk_put(priv->clk);
  502. return PTR_ERR(priv->dot_clk);
  503. }
  504. }
  505. return 0;
  506. }
  507. static int sh_mobile_lcdc_setcolreg(u_int regno,
  508. u_int red, u_int green, u_int blue,
  509. u_int transp, struct fb_info *info)
  510. {
  511. u32 *palette = info->pseudo_palette;
  512. if (regno >= PALETTE_NR)
  513. return -EINVAL;
  514. /* only FB_VISUAL_TRUECOLOR supported */
  515. red >>= 16 - info->var.red.length;
  516. green >>= 16 - info->var.green.length;
  517. blue >>= 16 - info->var.blue.length;
  518. transp >>= 16 - info->var.transp.length;
  519. palette[regno] = (red << info->var.red.offset) |
  520. (green << info->var.green.offset) |
  521. (blue << info->var.blue.offset) |
  522. (transp << info->var.transp.offset);
  523. return 0;
  524. }
  525. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  526. .id = "SH Mobile LCDC",
  527. .type = FB_TYPE_PACKED_PIXELS,
  528. .visual = FB_VISUAL_TRUECOLOR,
  529. .accel = FB_ACCEL_NONE,
  530. };
  531. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  532. const struct fb_fillrect *rect)
  533. {
  534. sys_fillrect(info, rect);
  535. sh_mobile_lcdc_deferred_io_touch(info);
  536. }
  537. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  538. const struct fb_copyarea *area)
  539. {
  540. sys_copyarea(info, area);
  541. sh_mobile_lcdc_deferred_io_touch(info);
  542. }
  543. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  544. const struct fb_image *image)
  545. {
  546. sys_imageblit(info, image);
  547. sh_mobile_lcdc_deferred_io_touch(info);
  548. }
  549. static struct fb_ops sh_mobile_lcdc_ops = {
  550. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  551. .fb_read = fb_sys_read,
  552. .fb_write = fb_sys_write,
  553. .fb_fillrect = sh_mobile_lcdc_fillrect,
  554. .fb_copyarea = sh_mobile_lcdc_copyarea,
  555. .fb_imageblit = sh_mobile_lcdc_imageblit,
  556. };
  557. static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
  558. {
  559. switch (bpp) {
  560. case 16: /* PKF[4:0] = 00011 - RGB 565 */
  561. var->red.offset = 11;
  562. var->red.length = 5;
  563. var->green.offset = 5;
  564. var->green.length = 6;
  565. var->blue.offset = 0;
  566. var->blue.length = 5;
  567. var->transp.offset = 0;
  568. var->transp.length = 0;
  569. break;
  570. case 32: /* PKF[4:0] = 00000 - RGB 888
  571. * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
  572. * this may be because LDDDSR has word swap enabled..
  573. */
  574. var->red.offset = 0;
  575. var->red.length = 8;
  576. var->green.offset = 24;
  577. var->green.length = 8;
  578. var->blue.offset = 16;
  579. var->blue.length = 8;
  580. var->transp.offset = 0;
  581. var->transp.length = 0;
  582. break;
  583. default:
  584. return -EINVAL;
  585. }
  586. var->bits_per_pixel = bpp;
  587. var->red.msb_right = 0;
  588. var->green.msb_right = 0;
  589. var->blue.msb_right = 0;
  590. var->transp.msb_right = 0;
  591. return 0;
  592. }
  593. static int sh_mobile_lcdc_suspend(struct device *dev)
  594. {
  595. struct platform_device *pdev = to_platform_device(dev);
  596. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  597. return 0;
  598. }
  599. static int sh_mobile_lcdc_resume(struct device *dev)
  600. {
  601. struct platform_device *pdev = to_platform_device(dev);
  602. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  603. }
  604. static struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  605. .suspend = sh_mobile_lcdc_suspend,
  606. .resume = sh_mobile_lcdc_resume,
  607. };
  608. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  609. static int __init sh_mobile_lcdc_probe(struct platform_device *pdev)
  610. {
  611. struct fb_info *info;
  612. struct sh_mobile_lcdc_priv *priv;
  613. struct sh_mobile_lcdc_info *pdata;
  614. struct sh_mobile_lcdc_chan_cfg *cfg;
  615. struct resource *res;
  616. int error;
  617. void *buf;
  618. int i, j;
  619. if (!pdev->dev.platform_data) {
  620. dev_err(&pdev->dev, "no platform data defined\n");
  621. error = -EINVAL;
  622. goto err0;
  623. }
  624. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  625. i = platform_get_irq(pdev, 0);
  626. if (!res || i < 0) {
  627. dev_err(&pdev->dev, "cannot get platform resources\n");
  628. error = -ENOENT;
  629. goto err0;
  630. }
  631. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  632. if (!priv) {
  633. dev_err(&pdev->dev, "cannot allocate device data\n");
  634. error = -ENOMEM;
  635. goto err0;
  636. }
  637. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  638. dev_name(&pdev->dev), priv);
  639. if (error) {
  640. dev_err(&pdev->dev, "unable to request irq\n");
  641. goto err1;
  642. }
  643. priv->irq = i;
  644. platform_set_drvdata(pdev, priv);
  645. pdata = pdev->dev.platform_data;
  646. j = 0;
  647. for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  648. priv->ch[j].lcdc = priv;
  649. memcpy(&priv->ch[j].cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  650. error = sh_mobile_lcdc_check_interface(&priv->ch[i]);
  651. if (error) {
  652. dev_err(&pdev->dev, "unsupported interface type\n");
  653. goto err1;
  654. }
  655. init_waitqueue_head(&priv->ch[i].frame_end_wait);
  656. switch (pdata->ch[i].chan) {
  657. case LCDC_CHAN_MAINLCD:
  658. priv->ch[j].enabled = 1 << 1;
  659. priv->ch[j].reg_offs = lcdc_offs_mainlcd;
  660. j++;
  661. break;
  662. case LCDC_CHAN_SUBLCD:
  663. priv->ch[j].enabled = 1 << 2;
  664. priv->ch[j].reg_offs = lcdc_offs_sublcd;
  665. j++;
  666. break;
  667. }
  668. }
  669. if (!j) {
  670. dev_err(&pdev->dev, "no channels defined\n");
  671. error = -EINVAL;
  672. goto err1;
  673. }
  674. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  675. if (error) {
  676. dev_err(&pdev->dev, "unable to setup clocks\n");
  677. goto err1;
  678. }
  679. priv->base = ioremap_nocache(res->start, (res->end - res->start) + 1);
  680. for (i = 0; i < j; i++) {
  681. cfg = &priv->ch[i].cfg;
  682. priv->ch[i].info = framebuffer_alloc(0, &pdev->dev);
  683. if (!priv->ch[i].info) {
  684. dev_err(&pdev->dev, "unable to allocate fb_info\n");
  685. error = -ENOMEM;
  686. break;
  687. }
  688. info = priv->ch[i].info;
  689. info->fbops = &sh_mobile_lcdc_ops;
  690. info->var.xres = info->var.xres_virtual = cfg->lcd_cfg.xres;
  691. info->var.yres = info->var.yres_virtual = cfg->lcd_cfg.yres;
  692. info->var.width = cfg->lcd_size_cfg.width;
  693. info->var.height = cfg->lcd_size_cfg.height;
  694. info->var.activate = FB_ACTIVATE_NOW;
  695. error = sh_mobile_lcdc_set_bpp(&info->var, cfg->bpp);
  696. if (error)
  697. break;
  698. info->fix = sh_mobile_lcdc_fix;
  699. info->fix.line_length = cfg->lcd_cfg.xres * (cfg->bpp / 8);
  700. info->fix.smem_len = info->fix.line_length * cfg->lcd_cfg.yres;
  701. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  702. &priv->ch[i].dma_handle, GFP_KERNEL);
  703. if (!buf) {
  704. dev_err(&pdev->dev, "unable to allocate buffer\n");
  705. error = -ENOMEM;
  706. break;
  707. }
  708. info->pseudo_palette = &priv->ch[i].pseudo_palette;
  709. info->flags = FBINFO_FLAG_DEFAULT;
  710. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  711. if (error < 0) {
  712. dev_err(&pdev->dev, "unable to allocate cmap\n");
  713. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  714. buf, priv->ch[i].dma_handle);
  715. break;
  716. }
  717. memset(buf, 0, info->fix.smem_len);
  718. info->fix.smem_start = priv->ch[i].dma_handle;
  719. info->screen_base = buf;
  720. info->device = &pdev->dev;
  721. info->par = &priv->ch[i];
  722. }
  723. if (error)
  724. goto err1;
  725. error = sh_mobile_lcdc_start(priv);
  726. if (error) {
  727. dev_err(&pdev->dev, "unable to start hardware\n");
  728. goto err1;
  729. }
  730. for (i = 0; i < j; i++) {
  731. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  732. info = ch->info;
  733. if (info->fbdefio) {
  734. priv->ch->sglist = vmalloc(sizeof(struct scatterlist) *
  735. info->fix.smem_len >> PAGE_SHIFT);
  736. if (!priv->ch->sglist) {
  737. dev_err(&pdev->dev, "cannot allocate sglist\n");
  738. goto err1;
  739. }
  740. }
  741. error = register_framebuffer(info);
  742. if (error < 0)
  743. goto err1;
  744. dev_info(info->dev,
  745. "registered %s/%s as %dx%d %dbpp.\n",
  746. pdev->name,
  747. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  748. "mainlcd" : "sublcd",
  749. (int) ch->cfg.lcd_cfg.xres,
  750. (int) ch->cfg.lcd_cfg.yres,
  751. ch->cfg.bpp);
  752. /* deferred io mode: disable clock to save power */
  753. if (info->fbdefio)
  754. sh_mobile_lcdc_clk_off(priv);
  755. }
  756. return 0;
  757. err1:
  758. sh_mobile_lcdc_remove(pdev);
  759. err0:
  760. return error;
  761. }
  762. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  763. {
  764. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  765. struct fb_info *info;
  766. int i;
  767. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  768. if (priv->ch[i].info->dev)
  769. unregister_framebuffer(priv->ch[i].info);
  770. sh_mobile_lcdc_stop(priv);
  771. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  772. info = priv->ch[i].info;
  773. if (!info || !info->device)
  774. continue;
  775. if (priv->ch[i].sglist)
  776. vfree(priv->ch[i].sglist);
  777. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  778. info->screen_base, priv->ch[i].dma_handle);
  779. fb_dealloc_cmap(&info->cmap);
  780. framebuffer_release(info);
  781. }
  782. if (priv->dot_clk)
  783. clk_put(priv->dot_clk);
  784. clk_put(priv->clk);
  785. if (priv->base)
  786. iounmap(priv->base);
  787. if (priv->irq)
  788. free_irq(priv->irq, priv);
  789. kfree(priv);
  790. return 0;
  791. }
  792. static struct platform_driver sh_mobile_lcdc_driver = {
  793. .driver = {
  794. .name = "sh_mobile_lcdc_fb",
  795. .owner = THIS_MODULE,
  796. .pm = &sh_mobile_lcdc_dev_pm_ops,
  797. },
  798. .probe = sh_mobile_lcdc_probe,
  799. .remove = sh_mobile_lcdc_remove,
  800. };
  801. static int __init sh_mobile_lcdc_init(void)
  802. {
  803. return platform_driver_register(&sh_mobile_lcdc_driver);
  804. }
  805. static void __exit sh_mobile_lcdc_exit(void)
  806. {
  807. platform_driver_unregister(&sh_mobile_lcdc_driver);
  808. }
  809. module_init(sh_mobile_lcdc_init);
  810. module_exit(sh_mobile_lcdc_exit);
  811. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  812. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  813. MODULE_LICENSE("GPL v2");