sh_mobile_lcdcfb.c 17 KB

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