sh_mobile_lcdcfb.c 23 KB

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