sh_mobile_lcdcfb.c 18 KB

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