sh_mobile_lcdcfb.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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/pm_runtime.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/ioctl.h>
  22. #include <linux/slab.h>
  23. #include <video/sh_mobile_lcdc.h>
  24. #include <asm/atomic.h>
  25. #define PALETTE_NR 16
  26. #define SIDE_B_OFFSET 0x1000
  27. #define MIRROR_OFFSET 0x2000
  28. /* shared registers */
  29. #define _LDDCKR 0x410
  30. #define _LDDCKSTPR 0x414
  31. #define _LDINTR 0x468
  32. #define _LDSR 0x46c
  33. #define _LDCNT1R 0x470
  34. #define _LDCNT2R 0x474
  35. #define _LDRCNTR 0x478
  36. #define _LDDDSR 0x47c
  37. #define _LDDWD0R 0x800
  38. #define _LDDRDR 0x840
  39. #define _LDDWAR 0x900
  40. #define _LDDRAR 0x904
  41. /* shared registers and their order for context save/restore */
  42. static int lcdc_shared_regs[] = {
  43. _LDDCKR,
  44. _LDDCKSTPR,
  45. _LDINTR,
  46. _LDDDSR,
  47. _LDCNT1R,
  48. _LDCNT2R,
  49. };
  50. #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
  51. /* per-channel registers */
  52. enum { LDDCKPAT1R, LDDCKPAT2R, LDMT1R, LDMT2R, LDMT3R, LDDFR, LDSM1R,
  53. LDSM2R, LDSA1R, LDMLSR, LDHCNR, LDHSYNR, LDVLNR, LDVSYNR, LDPMR,
  54. LDHAJR,
  55. NR_CH_REGS };
  56. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  57. [LDDCKPAT1R] = 0x400,
  58. [LDDCKPAT2R] = 0x404,
  59. [LDMT1R] = 0x418,
  60. [LDMT2R] = 0x41c,
  61. [LDMT3R] = 0x420,
  62. [LDDFR] = 0x424,
  63. [LDSM1R] = 0x428,
  64. [LDSM2R] = 0x42c,
  65. [LDSA1R] = 0x430,
  66. [LDMLSR] = 0x438,
  67. [LDHCNR] = 0x448,
  68. [LDHSYNR] = 0x44c,
  69. [LDVLNR] = 0x450,
  70. [LDVSYNR] = 0x454,
  71. [LDPMR] = 0x460,
  72. [LDHAJR] = 0x4a0,
  73. };
  74. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  75. [LDDCKPAT1R] = 0x408,
  76. [LDDCKPAT2R] = 0x40c,
  77. [LDMT1R] = 0x600,
  78. [LDMT2R] = 0x604,
  79. [LDMT3R] = 0x608,
  80. [LDDFR] = 0x60c,
  81. [LDSM1R] = 0x610,
  82. [LDSM2R] = 0x614,
  83. [LDSA1R] = 0x618,
  84. [LDMLSR] = 0x620,
  85. [LDHCNR] = 0x624,
  86. [LDHSYNR] = 0x628,
  87. [LDVLNR] = 0x62c,
  88. [LDVSYNR] = 0x630,
  89. [LDPMR] = 0x63c,
  90. };
  91. #define START_LCDC 0x00000001
  92. #define LCDC_RESET 0x00000100
  93. #define DISPLAY_BEU 0x00000008
  94. #define LCDC_ENABLE 0x00000001
  95. #define LDINTR_FE 0x00000400
  96. #define LDINTR_VSE 0x00000200
  97. #define LDINTR_VEE 0x00000100
  98. #define LDINTR_FS 0x00000004
  99. #define LDINTR_VSS 0x00000002
  100. #define LDINTR_VES 0x00000001
  101. #define LDRCNTR_SRS 0x00020000
  102. #define LDRCNTR_SRC 0x00010000
  103. #define LDRCNTR_MRS 0x00000002
  104. #define LDRCNTR_MRC 0x00000001
  105. #define LDSR_MRS 0x00000100
  106. struct sh_mobile_lcdc_priv;
  107. struct sh_mobile_lcdc_chan {
  108. struct sh_mobile_lcdc_priv *lcdc;
  109. unsigned long *reg_offs;
  110. unsigned long ldmt1r_value;
  111. unsigned long enabled; /* ME and SE in LDCNT2R */
  112. struct sh_mobile_lcdc_chan_cfg cfg;
  113. u32 pseudo_palette[PALETTE_NR];
  114. unsigned long saved_ch_regs[NR_CH_REGS];
  115. struct fb_info *info;
  116. dma_addr_t dma_handle;
  117. struct fb_deferred_io defio;
  118. struct scatterlist *sglist;
  119. unsigned long frame_end;
  120. unsigned long pan_offset;
  121. wait_queue_head_t frame_end_wait;
  122. struct completion vsync_completion;
  123. };
  124. struct sh_mobile_lcdc_priv {
  125. void __iomem *base;
  126. int irq;
  127. atomic_t hw_usecnt;
  128. struct device *dev;
  129. struct clk *dot_clk;
  130. unsigned long lddckr;
  131. struct sh_mobile_lcdc_chan ch[2];
  132. struct notifier_block notifier;
  133. unsigned long saved_shared_regs[NR_SHARED_REGS];
  134. int started;
  135. };
  136. static bool banked(int reg_nr)
  137. {
  138. switch (reg_nr) {
  139. case LDMT1R:
  140. case LDMT2R:
  141. case LDMT3R:
  142. case LDDFR:
  143. case LDSM1R:
  144. case LDSA1R:
  145. case LDMLSR:
  146. case LDHCNR:
  147. case LDHSYNR:
  148. case LDVLNR:
  149. case LDVSYNR:
  150. return true;
  151. }
  152. return false;
  153. }
  154. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  155. int reg_nr, unsigned long data)
  156. {
  157. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  158. if (banked(reg_nr))
  159. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  160. SIDE_B_OFFSET);
  161. }
  162. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  163. int reg_nr, unsigned long data)
  164. {
  165. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  166. MIRROR_OFFSET);
  167. }
  168. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  169. int reg_nr)
  170. {
  171. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  172. }
  173. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  174. unsigned long reg_offs, unsigned long data)
  175. {
  176. iowrite32(data, priv->base + reg_offs);
  177. }
  178. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  179. unsigned long reg_offs)
  180. {
  181. return ioread32(priv->base + reg_offs);
  182. }
  183. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  184. unsigned long reg_offs,
  185. unsigned long mask, unsigned long until)
  186. {
  187. while ((lcdc_read(priv, reg_offs) & mask) != until)
  188. cpu_relax();
  189. }
  190. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  191. {
  192. return chan->cfg.chan == LCDC_CHAN_SUBLCD;
  193. }
  194. static void lcdc_sys_write_index(void *handle, unsigned long data)
  195. {
  196. struct sh_mobile_lcdc_chan *ch = handle;
  197. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
  198. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  199. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  200. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  201. }
  202. static void lcdc_sys_write_data(void *handle, unsigned long data)
  203. {
  204. struct sh_mobile_lcdc_chan *ch = handle;
  205. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
  206. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  207. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  208. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  209. }
  210. static unsigned long lcdc_sys_read_data(void *handle)
  211. {
  212. struct sh_mobile_lcdc_chan *ch = handle;
  213. lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
  214. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  215. lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  216. udelay(1);
  217. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  218. return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
  219. }
  220. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  221. lcdc_sys_write_index,
  222. lcdc_sys_write_data,
  223. lcdc_sys_read_data,
  224. };
  225. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  226. {
  227. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  228. pm_runtime_get_sync(priv->dev);
  229. if (priv->dot_clk)
  230. clk_enable(priv->dot_clk);
  231. }
  232. }
  233. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  234. {
  235. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  236. if (priv->dot_clk)
  237. clk_disable(priv->dot_clk);
  238. pm_runtime_put(priv->dev);
  239. }
  240. }
  241. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  242. struct list_head *pagelist)
  243. {
  244. struct sh_mobile_lcdc_chan *ch = info->par;
  245. unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
  246. struct page *page;
  247. int nr_pages = 0;
  248. sg_init_table(ch->sglist, nr_pages_max);
  249. list_for_each_entry(page, pagelist, lru)
  250. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  251. return nr_pages;
  252. }
  253. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  254. struct list_head *pagelist)
  255. {
  256. struct sh_mobile_lcdc_chan *ch = info->par;
  257. struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
  258. /* enable clocks before accessing hardware */
  259. sh_mobile_lcdc_clk_on(ch->lcdc);
  260. /*
  261. * It's possible to get here without anything on the pagelist via
  262. * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
  263. * invocation. In the former case, the acceleration routines are
  264. * stepped in to when using the framebuffer console causing the
  265. * workqueue to be scheduled without any dirty pages on the list.
  266. *
  267. * Despite this, a panel update is still needed given that the
  268. * acceleration routines have their own methods for writing in
  269. * that still need to be updated.
  270. *
  271. * The fsync() and empty pagelist case could be optimized for,
  272. * but we don't bother, as any application exhibiting such
  273. * behaviour is fundamentally broken anyways.
  274. */
  275. if (!list_empty(pagelist)) {
  276. unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  277. /* trigger panel update */
  278. dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  279. if (bcfg->start_transfer)
  280. bcfg->start_transfer(bcfg->board_data, ch,
  281. &sh_mobile_lcdc_sys_bus_ops);
  282. lcdc_write_chan(ch, LDSM2R, 1);
  283. dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  284. } else {
  285. if (bcfg->start_transfer)
  286. bcfg->start_transfer(bcfg->board_data, ch,
  287. &sh_mobile_lcdc_sys_bus_ops);
  288. lcdc_write_chan(ch, LDSM2R, 1);
  289. }
  290. }
  291. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  292. {
  293. struct fb_deferred_io *fbdefio = info->fbdefio;
  294. if (fbdefio)
  295. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  296. }
  297. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  298. {
  299. struct sh_mobile_lcdc_priv *priv = data;
  300. struct sh_mobile_lcdc_chan *ch;
  301. unsigned long tmp;
  302. unsigned long ldintr;
  303. int is_sub;
  304. int k;
  305. /* acknowledge interrupt */
  306. ldintr = tmp = lcdc_read(priv, _LDINTR);
  307. /*
  308. * disable further VSYNC End IRQs, preserve all other enabled IRQs,
  309. * write 0 to bits 0-6 to ack all triggered IRQs.
  310. */
  311. tmp &= 0xffffff00 & ~LDINTR_VEE;
  312. lcdc_write(priv, _LDINTR, tmp);
  313. /* figure out if this interrupt is for main or sub lcd */
  314. is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
  315. /* wake up channel and disable clocks */
  316. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  317. ch = &priv->ch[k];
  318. if (!ch->enabled)
  319. continue;
  320. /* Frame Start */
  321. if (ldintr & LDINTR_FS) {
  322. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  323. ch->frame_end = 1;
  324. wake_up(&ch->frame_end_wait);
  325. sh_mobile_lcdc_clk_off(priv);
  326. }
  327. }
  328. /* VSYNC End */
  329. if (ldintr & LDINTR_VES)
  330. complete(&ch->vsync_completion);
  331. }
  332. return IRQ_HANDLED;
  333. }
  334. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  335. int start)
  336. {
  337. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  338. int k;
  339. /* start or stop the lcdc */
  340. if (start)
  341. lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
  342. else
  343. lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
  344. /* wait until power is applied/stopped on all channels */
  345. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  346. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  347. while (1) {
  348. tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
  349. if (start && tmp == 3)
  350. break;
  351. if (!start && tmp == 0)
  352. break;
  353. cpu_relax();
  354. }
  355. if (!start)
  356. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  357. }
  358. static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
  359. {
  360. struct fb_var_screeninfo *var = &ch->info->var;
  361. unsigned long h_total, hsync_pos;
  362. u32 tmp;
  363. tmp = ch->ldmt1r_value;
  364. tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
  365. tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
  366. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
  367. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
  368. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
  369. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
  370. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
  371. lcdc_write_chan(ch, LDMT1R, tmp);
  372. /* setup SYS bus */
  373. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  374. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  375. /* horizontal configuration */
  376. h_total = var->xres + var->hsync_len +
  377. var->left_margin + var->right_margin;
  378. tmp = h_total / 8; /* HTCN */
  379. tmp |= (var->xres / 8) << 16; /* HDCN */
  380. lcdc_write_chan(ch, LDHCNR, tmp);
  381. hsync_pos = var->xres + var->right_margin;
  382. tmp = hsync_pos / 8; /* HSYNP */
  383. tmp |= (var->hsync_len / 8) << 16; /* HSYNW */
  384. lcdc_write_chan(ch, LDHSYNR, tmp);
  385. /* vertical configuration */
  386. tmp = var->yres + var->vsync_len +
  387. var->upper_margin + var->lower_margin; /* VTLN */
  388. tmp |= var->yres << 16; /* VDLN */
  389. lcdc_write_chan(ch, LDVLNR, tmp);
  390. tmp = var->yres + var->lower_margin; /* VSYNP */
  391. tmp |= var->vsync_len << 16; /* VSYNW */
  392. lcdc_write_chan(ch, LDVSYNR, tmp);
  393. /* Adjust horizontal synchronisation for HDMI */
  394. tmp = ((var->xres & 7) << 24) |
  395. ((h_total & 7) << 16) |
  396. ((var->hsync_len & 7) << 8) |
  397. hsync_pos;
  398. lcdc_write_chan(ch, LDHAJR, tmp);
  399. }
  400. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  401. {
  402. struct sh_mobile_lcdc_chan *ch;
  403. struct fb_videomode *lcd_cfg;
  404. struct sh_mobile_lcdc_board_cfg *board_cfg;
  405. unsigned long tmp;
  406. int k, m;
  407. int ret = 0;
  408. /* enable clocks before accessing the hardware */
  409. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  410. if (priv->ch[k].enabled)
  411. sh_mobile_lcdc_clk_on(priv);
  412. /* reset */
  413. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
  414. lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
  415. /* enable LCDC channels */
  416. tmp = lcdc_read(priv, _LDCNT2R);
  417. tmp |= priv->ch[0].enabled;
  418. tmp |= priv->ch[1].enabled;
  419. lcdc_write(priv, _LDCNT2R, tmp);
  420. /* read data from external memory, avoid using the BEU for now */
  421. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
  422. /* stop the lcdc first */
  423. sh_mobile_lcdc_start_stop(priv, 0);
  424. /* configure clocks */
  425. tmp = priv->lddckr;
  426. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  427. ch = &priv->ch[k];
  428. if (!priv->ch[k].enabled)
  429. continue;
  430. m = ch->cfg.clock_divider;
  431. if (!m)
  432. continue;
  433. if (m == 1)
  434. m = 1 << 6;
  435. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  436. lcdc_write_chan(ch, LDDCKPAT1R, 0x00000000);
  437. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  438. }
  439. lcdc_write(priv, _LDDCKR, tmp);
  440. /* start dotclock again */
  441. lcdc_write(priv, _LDDCKSTPR, 0);
  442. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  443. /* interrupts are disabled to begin with */
  444. lcdc_write(priv, _LDINTR, 0);
  445. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  446. ch = &priv->ch[k];
  447. lcd_cfg = &ch->cfg.lcd_cfg;
  448. if (!ch->enabled)
  449. continue;
  450. sh_mobile_lcdc_geometry(ch);
  451. /* power supply */
  452. lcdc_write_chan(ch, LDPMR, 0);
  453. board_cfg = &ch->cfg.board_cfg;
  454. if (board_cfg->setup_sys)
  455. ret = board_cfg->setup_sys(board_cfg->board_data, ch,
  456. &sh_mobile_lcdc_sys_bus_ops);
  457. if (ret)
  458. return ret;
  459. }
  460. /* word and long word swap */
  461. lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
  462. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  463. ch = &priv->ch[k];
  464. if (!priv->ch[k].enabled)
  465. continue;
  466. /* set bpp format in PKF[4:0] */
  467. tmp = lcdc_read_chan(ch, LDDFR);
  468. tmp &= ~(0x0001001f);
  469. tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
  470. lcdc_write_chan(ch, LDDFR, tmp);
  471. /* point out our frame buffer */
  472. lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
  473. /* set line size */
  474. lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
  475. /* setup deferred io if SYS bus */
  476. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  477. if (ch->ldmt1r_value & (1 << 12) && tmp) {
  478. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  479. ch->defio.delay = msecs_to_jiffies(tmp);
  480. ch->info->fbdefio = &ch->defio;
  481. fb_deferred_io_init(ch->info);
  482. /* one-shot mode */
  483. lcdc_write_chan(ch, LDSM1R, 1);
  484. /* enable "Frame End Interrupt Enable" bit */
  485. lcdc_write(priv, _LDINTR, LDINTR_FE);
  486. } else {
  487. /* continuous read mode */
  488. lcdc_write_chan(ch, LDSM1R, 0);
  489. }
  490. }
  491. /* display output */
  492. lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
  493. /* start the lcdc */
  494. sh_mobile_lcdc_start_stop(priv, 1);
  495. priv->started = 1;
  496. /* tell the board code to enable the panel */
  497. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  498. ch = &priv->ch[k];
  499. if (!ch->enabled)
  500. continue;
  501. board_cfg = &ch->cfg.board_cfg;
  502. if (board_cfg->display_on)
  503. board_cfg->display_on(board_cfg->board_data, ch->info);
  504. }
  505. return 0;
  506. }
  507. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  508. {
  509. struct sh_mobile_lcdc_chan *ch;
  510. struct sh_mobile_lcdc_board_cfg *board_cfg;
  511. int k;
  512. /* clean up deferred io and ask board code to disable panel */
  513. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  514. ch = &priv->ch[k];
  515. if (!ch->enabled)
  516. continue;
  517. /* deferred io mode:
  518. * flush frame, and wait for frame end interrupt
  519. * clean up deferred io and enable clock
  520. */
  521. if (ch->info && ch->info->fbdefio) {
  522. ch->frame_end = 0;
  523. schedule_delayed_work(&ch->info->deferred_work, 0);
  524. wait_event(ch->frame_end_wait, ch->frame_end);
  525. fb_deferred_io_cleanup(ch->info);
  526. ch->info->fbdefio = NULL;
  527. sh_mobile_lcdc_clk_on(priv);
  528. }
  529. board_cfg = &ch->cfg.board_cfg;
  530. if (board_cfg->display_off)
  531. board_cfg->display_off(board_cfg->board_data);
  532. }
  533. /* stop the lcdc */
  534. if (priv->started) {
  535. sh_mobile_lcdc_start_stop(priv, 0);
  536. priv->started = 0;
  537. }
  538. /* stop clocks */
  539. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  540. if (priv->ch[k].enabled)
  541. sh_mobile_lcdc_clk_off(priv);
  542. }
  543. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  544. {
  545. int ifm, miftyp;
  546. switch (ch->cfg.interface_type) {
  547. case RGB8: ifm = 0; miftyp = 0; break;
  548. case RGB9: ifm = 0; miftyp = 4; break;
  549. case RGB12A: ifm = 0; miftyp = 5; break;
  550. case RGB12B: ifm = 0; miftyp = 6; break;
  551. case RGB16: ifm = 0; miftyp = 7; break;
  552. case RGB18: ifm = 0; miftyp = 10; break;
  553. case RGB24: ifm = 0; miftyp = 11; break;
  554. case SYS8A: ifm = 1; miftyp = 0; break;
  555. case SYS8B: ifm = 1; miftyp = 1; break;
  556. case SYS8C: ifm = 1; miftyp = 2; break;
  557. case SYS8D: ifm = 1; miftyp = 3; break;
  558. case SYS9: ifm = 1; miftyp = 4; break;
  559. case SYS12: ifm = 1; miftyp = 5; break;
  560. case SYS16A: ifm = 1; miftyp = 7; break;
  561. case SYS16B: ifm = 1; miftyp = 8; break;
  562. case SYS16C: ifm = 1; miftyp = 9; break;
  563. case SYS18: ifm = 1; miftyp = 10; break;
  564. case SYS24: ifm = 1; miftyp = 11; break;
  565. default: goto bad;
  566. }
  567. /* SUBLCD only supports SYS interface */
  568. if (lcdc_chan_is_sublcd(ch)) {
  569. if (ifm == 0)
  570. goto bad;
  571. else
  572. ifm = 0;
  573. }
  574. ch->ldmt1r_value = (ifm << 12) | miftyp;
  575. return 0;
  576. bad:
  577. return -EINVAL;
  578. }
  579. static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
  580. int clock_source,
  581. struct sh_mobile_lcdc_priv *priv)
  582. {
  583. char *str;
  584. int icksel;
  585. switch (clock_source) {
  586. case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
  587. case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
  588. case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
  589. default:
  590. return -EINVAL;
  591. }
  592. priv->lddckr = icksel << 16;
  593. if (str) {
  594. priv->dot_clk = clk_get(&pdev->dev, str);
  595. if (IS_ERR(priv->dot_clk)) {
  596. dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
  597. return PTR_ERR(priv->dot_clk);
  598. }
  599. }
  600. /* Runtime PM support involves two step for this driver:
  601. * 1) Enable Runtime PM
  602. * 2) Force Runtime PM Resume since hardware is accessed from probe()
  603. */
  604. priv->dev = &pdev->dev;
  605. pm_runtime_enable(priv->dev);
  606. pm_runtime_resume(priv->dev);
  607. return 0;
  608. }
  609. static int sh_mobile_lcdc_setcolreg(u_int regno,
  610. u_int red, u_int green, u_int blue,
  611. u_int transp, struct fb_info *info)
  612. {
  613. u32 *palette = info->pseudo_palette;
  614. if (regno >= PALETTE_NR)
  615. return -EINVAL;
  616. /* only FB_VISUAL_TRUECOLOR supported */
  617. red >>= 16 - info->var.red.length;
  618. green >>= 16 - info->var.green.length;
  619. blue >>= 16 - info->var.blue.length;
  620. transp >>= 16 - info->var.transp.length;
  621. palette[regno] = (red << info->var.red.offset) |
  622. (green << info->var.green.offset) |
  623. (blue << info->var.blue.offset) |
  624. (transp << info->var.transp.offset);
  625. return 0;
  626. }
  627. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  628. .id = "SH Mobile LCDC",
  629. .type = FB_TYPE_PACKED_PIXELS,
  630. .visual = FB_VISUAL_TRUECOLOR,
  631. .accel = FB_ACCEL_NONE,
  632. .xpanstep = 0,
  633. .ypanstep = 1,
  634. .ywrapstep = 0,
  635. };
  636. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  637. const struct fb_fillrect *rect)
  638. {
  639. sys_fillrect(info, rect);
  640. sh_mobile_lcdc_deferred_io_touch(info);
  641. }
  642. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  643. const struct fb_copyarea *area)
  644. {
  645. sys_copyarea(info, area);
  646. sh_mobile_lcdc_deferred_io_touch(info);
  647. }
  648. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  649. const struct fb_image *image)
  650. {
  651. sys_imageblit(info, image);
  652. sh_mobile_lcdc_deferred_io_touch(info);
  653. }
  654. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  655. struct fb_info *info)
  656. {
  657. struct sh_mobile_lcdc_chan *ch = info->par;
  658. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  659. unsigned long ldrcntr;
  660. unsigned long new_pan_offset;
  661. new_pan_offset = (var->yoffset * info->fix.line_length) +
  662. (var->xoffset * (info->var.bits_per_pixel / 8));
  663. if (new_pan_offset == ch->pan_offset)
  664. return 0; /* No change, do nothing */
  665. ldrcntr = lcdc_read(priv, _LDRCNTR);
  666. /* Set the source address for the next refresh */
  667. lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle + new_pan_offset);
  668. if (lcdc_chan_is_sublcd(ch))
  669. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  670. else
  671. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  672. ch->pan_offset = new_pan_offset;
  673. sh_mobile_lcdc_deferred_io_touch(info);
  674. return 0;
  675. }
  676. static int sh_mobile_wait_for_vsync(struct fb_info *info)
  677. {
  678. struct sh_mobile_lcdc_chan *ch = info->par;
  679. unsigned long ldintr;
  680. int ret;
  681. /* Enable VSync End interrupt */
  682. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  683. ldintr |= LDINTR_VEE;
  684. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  685. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  686. msecs_to_jiffies(100));
  687. if (!ret)
  688. return -ETIMEDOUT;
  689. return 0;
  690. }
  691. static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
  692. unsigned long arg)
  693. {
  694. int retval;
  695. switch (cmd) {
  696. case FBIO_WAITFORVSYNC:
  697. retval = sh_mobile_wait_for_vsync(info);
  698. break;
  699. default:
  700. retval = -ENOIOCTLCMD;
  701. break;
  702. }
  703. return retval;
  704. }
  705. static struct fb_ops sh_mobile_lcdc_ops = {
  706. .owner = THIS_MODULE,
  707. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  708. .fb_read = fb_sys_read,
  709. .fb_write = fb_sys_write,
  710. .fb_fillrect = sh_mobile_lcdc_fillrect,
  711. .fb_copyarea = sh_mobile_lcdc_copyarea,
  712. .fb_imageblit = sh_mobile_lcdc_imageblit,
  713. .fb_pan_display = sh_mobile_fb_pan_display,
  714. .fb_ioctl = sh_mobile_ioctl,
  715. };
  716. static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
  717. {
  718. switch (bpp) {
  719. case 16: /* PKF[4:0] = 00011 - RGB 565 */
  720. var->red.offset = 11;
  721. var->red.length = 5;
  722. var->green.offset = 5;
  723. var->green.length = 6;
  724. var->blue.offset = 0;
  725. var->blue.length = 5;
  726. var->transp.offset = 0;
  727. var->transp.length = 0;
  728. break;
  729. case 32: /* PKF[4:0] = 00000 - RGB 888
  730. * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
  731. * this may be because LDDDSR has word swap enabled..
  732. */
  733. var->red.offset = 0;
  734. var->red.length = 8;
  735. var->green.offset = 24;
  736. var->green.length = 8;
  737. var->blue.offset = 16;
  738. var->blue.length = 8;
  739. var->transp.offset = 0;
  740. var->transp.length = 0;
  741. break;
  742. default:
  743. return -EINVAL;
  744. }
  745. var->bits_per_pixel = bpp;
  746. var->red.msb_right = 0;
  747. var->green.msb_right = 0;
  748. var->blue.msb_right = 0;
  749. var->transp.msb_right = 0;
  750. return 0;
  751. }
  752. static int sh_mobile_lcdc_suspend(struct device *dev)
  753. {
  754. struct platform_device *pdev = to_platform_device(dev);
  755. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  756. return 0;
  757. }
  758. static int sh_mobile_lcdc_resume(struct device *dev)
  759. {
  760. struct platform_device *pdev = to_platform_device(dev);
  761. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  762. }
  763. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  764. {
  765. struct platform_device *pdev = to_platform_device(dev);
  766. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  767. struct sh_mobile_lcdc_chan *ch;
  768. int k, n;
  769. /* save per-channel registers */
  770. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  771. ch = &p->ch[k];
  772. if (!ch->enabled)
  773. continue;
  774. for (n = 0; n < NR_CH_REGS; n++)
  775. ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
  776. }
  777. /* save shared registers */
  778. for (n = 0; n < NR_SHARED_REGS; n++)
  779. p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
  780. /* turn off LCDC hardware */
  781. lcdc_write(p, _LDCNT1R, 0);
  782. return 0;
  783. }
  784. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  785. {
  786. struct platform_device *pdev = to_platform_device(dev);
  787. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  788. struct sh_mobile_lcdc_chan *ch;
  789. int k, n;
  790. /* restore per-channel registers */
  791. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  792. ch = &p->ch[k];
  793. if (!ch->enabled)
  794. continue;
  795. for (n = 0; n < NR_CH_REGS; n++)
  796. lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
  797. }
  798. /* restore shared registers */
  799. for (n = 0; n < NR_SHARED_REGS; n++)
  800. lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
  801. return 0;
  802. }
  803. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  804. .suspend = sh_mobile_lcdc_suspend,
  805. .resume = sh_mobile_lcdc_resume,
  806. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  807. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  808. };
  809. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  810. unsigned long action, void *data)
  811. {
  812. struct fb_event *event = data;
  813. struct fb_info *info = event->info;
  814. struct sh_mobile_lcdc_chan *ch = info->par;
  815. struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
  816. struct fb_var_screeninfo *var;
  817. if (&ch->lcdc->notifier != nb)
  818. return 0;
  819. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  820. __func__, action, event->data);
  821. switch(action) {
  822. case FB_EVENT_SUSPEND:
  823. if (board_cfg->display_off)
  824. board_cfg->display_off(board_cfg->board_data);
  825. pm_runtime_put(info->device);
  826. break;
  827. case FB_EVENT_RESUME:
  828. var = &info->var;
  829. /* HDMI must be enabled before LCDC configuration */
  830. if (board_cfg->display_on)
  831. board_cfg->display_on(board_cfg->board_data, ch->info);
  832. /* Check if the new display is not in our modelist */
  833. if (ch->info->modelist.next &&
  834. !fb_match_mode(var, &ch->info->modelist)) {
  835. struct fb_videomode mode;
  836. int ret;
  837. /* Can we handle this display? */
  838. if (var->xres > ch->cfg.lcd_cfg.xres ||
  839. var->yres > ch->cfg.lcd_cfg.yres)
  840. return -ENOMEM;
  841. /* Add to the modelist */
  842. fb_var_to_videomode(&mode, var);
  843. ret = fb_add_videomode(&mode, &ch->info->modelist);
  844. if (ret < 0)
  845. return ret;
  846. }
  847. pm_runtime_get_sync(info->device);
  848. sh_mobile_lcdc_geometry(ch);
  849. break;
  850. }
  851. return 0;
  852. }
  853. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  854. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  855. {
  856. struct fb_info *info;
  857. struct sh_mobile_lcdc_priv *priv;
  858. struct sh_mobile_lcdc_info *pdata;
  859. struct sh_mobile_lcdc_chan_cfg *cfg;
  860. struct resource *res;
  861. int error;
  862. void *buf;
  863. int i, j;
  864. if (!pdev->dev.platform_data) {
  865. dev_err(&pdev->dev, "no platform data defined\n");
  866. return -EINVAL;
  867. }
  868. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  869. i = platform_get_irq(pdev, 0);
  870. if (!res || i < 0) {
  871. dev_err(&pdev->dev, "cannot get platform resources\n");
  872. return -ENOENT;
  873. }
  874. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  875. if (!priv) {
  876. dev_err(&pdev->dev, "cannot allocate device data\n");
  877. return -ENOMEM;
  878. }
  879. platform_set_drvdata(pdev, priv);
  880. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  881. dev_name(&pdev->dev), priv);
  882. if (error) {
  883. dev_err(&pdev->dev, "unable to request irq\n");
  884. goto err1;
  885. }
  886. priv->irq = i;
  887. pdata = pdev->dev.platform_data;
  888. atomic_set(&priv->hw_usecnt, -1);
  889. j = 0;
  890. for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  891. priv->ch[j].lcdc = priv;
  892. memcpy(&priv->ch[j].cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  893. error = sh_mobile_lcdc_check_interface(&priv->ch[j]);
  894. if (error) {
  895. dev_err(&pdev->dev, "unsupported interface type\n");
  896. goto err1;
  897. }
  898. init_waitqueue_head(&priv->ch[j].frame_end_wait);
  899. init_completion(&priv->ch[j].vsync_completion);
  900. priv->ch[j].pan_offset = 0;
  901. switch (pdata->ch[i].chan) {
  902. case LCDC_CHAN_MAINLCD:
  903. priv->ch[j].enabled = 1 << 1;
  904. priv->ch[j].reg_offs = lcdc_offs_mainlcd;
  905. j++;
  906. break;
  907. case LCDC_CHAN_SUBLCD:
  908. priv->ch[j].enabled = 1 << 2;
  909. priv->ch[j].reg_offs = lcdc_offs_sublcd;
  910. j++;
  911. break;
  912. }
  913. }
  914. if (!j) {
  915. dev_err(&pdev->dev, "no channels defined\n");
  916. error = -EINVAL;
  917. goto err1;
  918. }
  919. priv->base = ioremap_nocache(res->start, resource_size(res));
  920. if (!priv->base)
  921. goto err1;
  922. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  923. if (error) {
  924. dev_err(&pdev->dev, "unable to setup clocks\n");
  925. goto err1;
  926. }
  927. for (i = 0; i < j; i++) {
  928. struct fb_var_screeninfo *var;
  929. struct fb_videomode *lcd_cfg;
  930. cfg = &priv->ch[i].cfg;
  931. priv->ch[i].info = framebuffer_alloc(0, &pdev->dev);
  932. if (!priv->ch[i].info) {
  933. dev_err(&pdev->dev, "unable to allocate fb_info\n");
  934. error = -ENOMEM;
  935. break;
  936. }
  937. info = priv->ch[i].info;
  938. var = &info->var;
  939. lcd_cfg = &cfg->lcd_cfg;
  940. info->fbops = &sh_mobile_lcdc_ops;
  941. fb_videomode_to_var(var, lcd_cfg);
  942. /* Default Y virtual resolution is 2x panel size */
  943. var->yres_virtual = var->yres * 2;
  944. error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
  945. if (error)
  946. break;
  947. info->fix = sh_mobile_lcdc_fix;
  948. info->fix.line_length = lcd_cfg->xres * (cfg->bpp / 8);
  949. info->fix.smem_len = info->fix.line_length *
  950. var->yres_virtual;
  951. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  952. &priv->ch[i].dma_handle, GFP_KERNEL);
  953. if (!buf) {
  954. dev_err(&pdev->dev, "unable to allocate buffer\n");
  955. error = -ENOMEM;
  956. break;
  957. }
  958. info->pseudo_palette = &priv->ch[i].pseudo_palette;
  959. info->flags = FBINFO_FLAG_DEFAULT;
  960. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  961. if (error < 0) {
  962. dev_err(&pdev->dev, "unable to allocate cmap\n");
  963. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  964. buf, priv->ch[i].dma_handle);
  965. break;
  966. }
  967. info->fix.smem_start = priv->ch[i].dma_handle;
  968. info->screen_base = buf;
  969. info->device = &pdev->dev;
  970. info->par = &priv->ch[i];
  971. }
  972. if (error)
  973. goto err1;
  974. error = sh_mobile_lcdc_start(priv);
  975. if (error) {
  976. dev_err(&pdev->dev, "unable to start hardware\n");
  977. goto err1;
  978. }
  979. for (i = 0; i < j; i++) {
  980. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  981. info = ch->info;
  982. if (info->fbdefio) {
  983. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  984. info->fix.smem_len >> PAGE_SHIFT);
  985. if (!ch->sglist) {
  986. dev_err(&pdev->dev, "cannot allocate sglist\n");
  987. goto err1;
  988. }
  989. }
  990. error = register_framebuffer(info);
  991. if (error < 0)
  992. goto err1;
  993. dev_info(info->dev,
  994. "registered %s/%s as %dx%d %dbpp.\n",
  995. pdev->name,
  996. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  997. "mainlcd" : "sublcd",
  998. (int) ch->cfg.lcd_cfg.xres,
  999. (int) ch->cfg.lcd_cfg.yres,
  1000. ch->cfg.bpp);
  1001. /* deferred io mode: disable clock to save power */
  1002. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1003. sh_mobile_lcdc_clk_off(priv);
  1004. }
  1005. /* Failure ignored */
  1006. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  1007. fb_register_client(&priv->notifier);
  1008. return 0;
  1009. err1:
  1010. sh_mobile_lcdc_remove(pdev);
  1011. return error;
  1012. }
  1013. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1014. {
  1015. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1016. struct fb_info *info;
  1017. int i;
  1018. fb_unregister_client(&priv->notifier);
  1019. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1020. if (priv->ch[i].info && priv->ch[i].info->dev)
  1021. unregister_framebuffer(priv->ch[i].info);
  1022. sh_mobile_lcdc_stop(priv);
  1023. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1024. info = priv->ch[i].info;
  1025. if (!info || !info->device)
  1026. continue;
  1027. if (priv->ch[i].sglist)
  1028. vfree(priv->ch[i].sglist);
  1029. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  1030. info->screen_base, priv->ch[i].dma_handle);
  1031. fb_dealloc_cmap(&info->cmap);
  1032. framebuffer_release(info);
  1033. }
  1034. if (priv->dot_clk)
  1035. clk_put(priv->dot_clk);
  1036. if (priv->dev)
  1037. pm_runtime_disable(priv->dev);
  1038. if (priv->base)
  1039. iounmap(priv->base);
  1040. if (priv->irq)
  1041. free_irq(priv->irq, priv);
  1042. kfree(priv);
  1043. return 0;
  1044. }
  1045. static struct platform_driver sh_mobile_lcdc_driver = {
  1046. .driver = {
  1047. .name = "sh_mobile_lcdc_fb",
  1048. .owner = THIS_MODULE,
  1049. .pm = &sh_mobile_lcdc_dev_pm_ops,
  1050. },
  1051. .probe = sh_mobile_lcdc_probe,
  1052. .remove = sh_mobile_lcdc_remove,
  1053. };
  1054. static int __init sh_mobile_lcdc_init(void)
  1055. {
  1056. return platform_driver_register(&sh_mobile_lcdc_driver);
  1057. }
  1058. static void __exit sh_mobile_lcdc_exit(void)
  1059. {
  1060. platform_driver_unregister(&sh_mobile_lcdc_driver);
  1061. }
  1062. module_init(sh_mobile_lcdc_init);
  1063. module_exit(sh_mobile_lcdc_exit);
  1064. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  1065. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  1066. MODULE_LICENSE("GPL v2");