sh_mobile_lcdcfb.c 22 KB

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