sh_mobile_lcdcfb.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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->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. atomic_set(&priv->hw_usecnt, -1);
  601. /* Runtime PM support involves two step for this driver:
  602. * 1) Enable Runtime PM
  603. * 2) Force Runtime PM Resume since hardware is accessed from probe()
  604. */
  605. priv->dev = &pdev->dev;
  606. pm_runtime_enable(priv->dev);
  607. pm_runtime_resume(priv->dev);
  608. return 0;
  609. }
  610. static int sh_mobile_lcdc_setcolreg(u_int regno,
  611. u_int red, u_int green, u_int blue,
  612. u_int transp, struct fb_info *info)
  613. {
  614. u32 *palette = info->pseudo_palette;
  615. if (regno >= PALETTE_NR)
  616. return -EINVAL;
  617. /* only FB_VISUAL_TRUECOLOR supported */
  618. red >>= 16 - info->var.red.length;
  619. green >>= 16 - info->var.green.length;
  620. blue >>= 16 - info->var.blue.length;
  621. transp >>= 16 - info->var.transp.length;
  622. palette[regno] = (red << info->var.red.offset) |
  623. (green << info->var.green.offset) |
  624. (blue << info->var.blue.offset) |
  625. (transp << info->var.transp.offset);
  626. return 0;
  627. }
  628. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  629. .id = "SH Mobile LCDC",
  630. .type = FB_TYPE_PACKED_PIXELS,
  631. .visual = FB_VISUAL_TRUECOLOR,
  632. .accel = FB_ACCEL_NONE,
  633. .xpanstep = 0,
  634. .ypanstep = 1,
  635. .ywrapstep = 0,
  636. };
  637. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  638. const struct fb_fillrect *rect)
  639. {
  640. sys_fillrect(info, rect);
  641. sh_mobile_lcdc_deferred_io_touch(info);
  642. }
  643. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  644. const struct fb_copyarea *area)
  645. {
  646. sys_copyarea(info, area);
  647. sh_mobile_lcdc_deferred_io_touch(info);
  648. }
  649. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  650. const struct fb_image *image)
  651. {
  652. sys_imageblit(info, image);
  653. sh_mobile_lcdc_deferred_io_touch(info);
  654. }
  655. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  656. struct fb_info *info)
  657. {
  658. struct sh_mobile_lcdc_chan *ch = info->par;
  659. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  660. unsigned long ldrcntr;
  661. unsigned long new_pan_offset;
  662. new_pan_offset = (var->yoffset * info->fix.line_length) +
  663. (var->xoffset * (info->var.bits_per_pixel / 8));
  664. if (new_pan_offset == ch->pan_offset)
  665. return 0; /* No change, do nothing */
  666. ldrcntr = lcdc_read(priv, _LDRCNTR);
  667. /* Set the source address for the next refresh */
  668. lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle + new_pan_offset);
  669. if (lcdc_chan_is_sublcd(ch))
  670. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  671. else
  672. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  673. ch->pan_offset = new_pan_offset;
  674. sh_mobile_lcdc_deferred_io_touch(info);
  675. return 0;
  676. }
  677. static int sh_mobile_wait_for_vsync(struct fb_info *info)
  678. {
  679. struct sh_mobile_lcdc_chan *ch = info->par;
  680. unsigned long ldintr;
  681. int ret;
  682. /* Enable VSync End interrupt */
  683. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  684. ldintr |= LDINTR_VEE;
  685. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  686. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  687. msecs_to_jiffies(100));
  688. if (!ret)
  689. return -ETIMEDOUT;
  690. return 0;
  691. }
  692. static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
  693. unsigned long arg)
  694. {
  695. int retval;
  696. switch (cmd) {
  697. case FBIO_WAITFORVSYNC:
  698. retval = sh_mobile_wait_for_vsync(info);
  699. break;
  700. default:
  701. retval = -ENOIOCTLCMD;
  702. break;
  703. }
  704. return retval;
  705. }
  706. static struct fb_ops sh_mobile_lcdc_ops = {
  707. .owner = THIS_MODULE,
  708. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  709. .fb_read = fb_sys_read,
  710. .fb_write = fb_sys_write,
  711. .fb_fillrect = sh_mobile_lcdc_fillrect,
  712. .fb_copyarea = sh_mobile_lcdc_copyarea,
  713. .fb_imageblit = sh_mobile_lcdc_imageblit,
  714. .fb_pan_display = sh_mobile_fb_pan_display,
  715. .fb_ioctl = sh_mobile_ioctl,
  716. };
  717. static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
  718. {
  719. switch (bpp) {
  720. case 16: /* PKF[4:0] = 00011 - RGB 565 */
  721. var->red.offset = 11;
  722. var->red.length = 5;
  723. var->green.offset = 5;
  724. var->green.length = 6;
  725. var->blue.offset = 0;
  726. var->blue.length = 5;
  727. var->transp.offset = 0;
  728. var->transp.length = 0;
  729. break;
  730. case 32: /* PKF[4:0] = 00000 - RGB 888
  731. * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
  732. * this may be because LDDDSR has word swap enabled..
  733. */
  734. var->red.offset = 0;
  735. var->red.length = 8;
  736. var->green.offset = 24;
  737. var->green.length = 8;
  738. var->blue.offset = 16;
  739. var->blue.length = 8;
  740. var->transp.offset = 0;
  741. var->transp.length = 0;
  742. break;
  743. default:
  744. return -EINVAL;
  745. }
  746. var->bits_per_pixel = bpp;
  747. var->red.msb_right = 0;
  748. var->green.msb_right = 0;
  749. var->blue.msb_right = 0;
  750. var->transp.msb_right = 0;
  751. return 0;
  752. }
  753. static int sh_mobile_lcdc_suspend(struct device *dev)
  754. {
  755. struct platform_device *pdev = to_platform_device(dev);
  756. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  757. return 0;
  758. }
  759. static int sh_mobile_lcdc_resume(struct device *dev)
  760. {
  761. struct platform_device *pdev = to_platform_device(dev);
  762. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  763. }
  764. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  765. {
  766. struct platform_device *pdev = to_platform_device(dev);
  767. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  768. struct sh_mobile_lcdc_chan *ch;
  769. int k, n;
  770. /* save per-channel registers */
  771. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  772. ch = &p->ch[k];
  773. if (!ch->enabled)
  774. continue;
  775. for (n = 0; n < NR_CH_REGS; n++)
  776. ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
  777. }
  778. /* save shared registers */
  779. for (n = 0; n < NR_SHARED_REGS; n++)
  780. p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
  781. /* turn off LCDC hardware */
  782. lcdc_write(p, _LDCNT1R, 0);
  783. return 0;
  784. }
  785. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  786. {
  787. struct platform_device *pdev = to_platform_device(dev);
  788. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  789. struct sh_mobile_lcdc_chan *ch;
  790. int k, n;
  791. /* restore per-channel registers */
  792. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  793. ch = &p->ch[k];
  794. if (!ch->enabled)
  795. continue;
  796. for (n = 0; n < NR_CH_REGS; n++)
  797. lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
  798. }
  799. /* restore shared registers */
  800. for (n = 0; n < NR_SHARED_REGS; n++)
  801. lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
  802. return 0;
  803. }
  804. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  805. .suspend = sh_mobile_lcdc_suspend,
  806. .resume = sh_mobile_lcdc_resume,
  807. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  808. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  809. };
  810. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  811. unsigned long action, void *data)
  812. {
  813. struct fb_event *event = data;
  814. struct fb_info *info = event->info;
  815. struct sh_mobile_lcdc_chan *ch = info->par;
  816. struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
  817. struct fb_var_screeninfo *var;
  818. if (&ch->lcdc->notifier != nb)
  819. return 0;
  820. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  821. __func__, action, event->data);
  822. switch(action) {
  823. case FB_EVENT_SUSPEND:
  824. if (board_cfg->display_off)
  825. board_cfg->display_off(board_cfg->board_data);
  826. pm_runtime_put(info->device);
  827. break;
  828. case FB_EVENT_RESUME:
  829. var = &info->var;
  830. /* HDMI must be enabled before LCDC configuration */
  831. if (board_cfg->display_on)
  832. board_cfg->display_on(board_cfg->board_data, ch->info);
  833. /* Check if the new display is not in our modelist */
  834. if (ch->info->modelist.next &&
  835. !fb_match_mode(var, &ch->info->modelist)) {
  836. struct fb_videomode mode;
  837. int ret;
  838. /* Can we handle this display? */
  839. if (var->xres > ch->cfg.lcd_cfg.xres ||
  840. var->yres > ch->cfg.lcd_cfg.yres)
  841. return -ENOMEM;
  842. /* Add to the modelist */
  843. fb_var_to_videomode(&mode, var);
  844. ret = fb_add_videomode(&mode, &ch->info->modelist);
  845. if (ret < 0)
  846. return ret;
  847. }
  848. pm_runtime_get_sync(info->device);
  849. sh_mobile_lcdc_geometry(ch);
  850. break;
  851. }
  852. return 0;
  853. }
  854. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  855. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  856. {
  857. struct fb_info *info;
  858. struct sh_mobile_lcdc_priv *priv;
  859. struct sh_mobile_lcdc_info *pdata;
  860. struct sh_mobile_lcdc_chan_cfg *cfg;
  861. struct resource *res;
  862. int error;
  863. void *buf;
  864. int i, j;
  865. if (!pdev->dev.platform_data) {
  866. dev_err(&pdev->dev, "no platform data defined\n");
  867. return -EINVAL;
  868. }
  869. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  870. i = platform_get_irq(pdev, 0);
  871. if (!res || i < 0) {
  872. dev_err(&pdev->dev, "cannot get platform resources\n");
  873. return -ENOENT;
  874. }
  875. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  876. if (!priv) {
  877. dev_err(&pdev->dev, "cannot allocate device data\n");
  878. return -ENOMEM;
  879. }
  880. platform_set_drvdata(pdev, priv);
  881. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  882. dev_name(&pdev->dev), priv);
  883. if (error) {
  884. dev_err(&pdev->dev, "unable to request irq\n");
  885. goto err1;
  886. }
  887. priv->irq = i;
  888. pdata = pdev->dev.platform_data;
  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. var->xres = var->xres_virtual = lcd_cfg->xres;
  942. var->yres = lcd_cfg->yres;
  943. /* Default Y virtual resolution is 2x panel size */
  944. var->yres_virtual = var->yres * 2;
  945. var->width = cfg->lcd_size_cfg.width;
  946. var->height = cfg->lcd_size_cfg.height;
  947. var->activate = FB_ACTIVATE_NOW;
  948. var->left_margin = lcd_cfg->left_margin;
  949. var->right_margin = lcd_cfg->right_margin;
  950. var->upper_margin = lcd_cfg->upper_margin;
  951. var->lower_margin = lcd_cfg->lower_margin;
  952. var->hsync_len = lcd_cfg->hsync_len;
  953. var->vsync_len = lcd_cfg->vsync_len;
  954. var->sync = lcd_cfg->sync;
  955. var->pixclock = lcd_cfg->pixclock;
  956. error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
  957. if (error)
  958. break;
  959. info->fix = sh_mobile_lcdc_fix;
  960. info->fix.line_length = lcd_cfg->xres * (cfg->bpp / 8);
  961. info->fix.smem_len = info->fix.line_length *
  962. var->yres_virtual;
  963. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  964. &priv->ch[i].dma_handle, GFP_KERNEL);
  965. if (!buf) {
  966. dev_err(&pdev->dev, "unable to allocate buffer\n");
  967. error = -ENOMEM;
  968. break;
  969. }
  970. info->pseudo_palette = &priv->ch[i].pseudo_palette;
  971. info->flags = FBINFO_FLAG_DEFAULT;
  972. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  973. if (error < 0) {
  974. dev_err(&pdev->dev, "unable to allocate cmap\n");
  975. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  976. buf, priv->ch[i].dma_handle);
  977. break;
  978. }
  979. memset(buf, 0, info->fix.smem_len);
  980. info->fix.smem_start = priv->ch[i].dma_handle;
  981. info->screen_base = buf;
  982. info->device = &pdev->dev;
  983. info->par = &priv->ch[i];
  984. }
  985. if (error)
  986. goto err1;
  987. error = sh_mobile_lcdc_start(priv);
  988. if (error) {
  989. dev_err(&pdev->dev, "unable to start hardware\n");
  990. goto err1;
  991. }
  992. for (i = 0; i < j; i++) {
  993. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  994. info = ch->info;
  995. if (info->fbdefio) {
  996. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  997. info->fix.smem_len >> PAGE_SHIFT);
  998. if (!ch->sglist) {
  999. dev_err(&pdev->dev, "cannot allocate sglist\n");
  1000. goto err1;
  1001. }
  1002. }
  1003. error = register_framebuffer(info);
  1004. if (error < 0)
  1005. goto err1;
  1006. dev_info(info->dev,
  1007. "registered %s/%s as %dx%d %dbpp.\n",
  1008. pdev->name,
  1009. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  1010. "mainlcd" : "sublcd",
  1011. (int) ch->cfg.lcd_cfg.xres,
  1012. (int) ch->cfg.lcd_cfg.yres,
  1013. ch->cfg.bpp);
  1014. /* deferred io mode: disable clock to save power */
  1015. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1016. sh_mobile_lcdc_clk_off(priv);
  1017. }
  1018. /* Failure ignored */
  1019. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  1020. fb_register_client(&priv->notifier);
  1021. return 0;
  1022. err1:
  1023. sh_mobile_lcdc_remove(pdev);
  1024. return error;
  1025. }
  1026. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1027. {
  1028. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1029. struct fb_info *info;
  1030. int i;
  1031. fb_unregister_client(&priv->notifier);
  1032. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1033. if (priv->ch[i].info && priv->ch[i].info->dev)
  1034. unregister_framebuffer(priv->ch[i].info);
  1035. sh_mobile_lcdc_stop(priv);
  1036. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1037. info = priv->ch[i].info;
  1038. if (!info || !info->device)
  1039. continue;
  1040. if (priv->ch[i].sglist)
  1041. vfree(priv->ch[i].sglist);
  1042. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  1043. info->screen_base, priv->ch[i].dma_handle);
  1044. fb_dealloc_cmap(&info->cmap);
  1045. framebuffer_release(info);
  1046. }
  1047. if (priv->dot_clk)
  1048. clk_put(priv->dot_clk);
  1049. if (priv->dev)
  1050. pm_runtime_disable(priv->dev);
  1051. if (priv->base)
  1052. iounmap(priv->base);
  1053. if (priv->irq)
  1054. free_irq(priv->irq, priv);
  1055. kfree(priv);
  1056. return 0;
  1057. }
  1058. static struct platform_driver sh_mobile_lcdc_driver = {
  1059. .driver = {
  1060. .name = "sh_mobile_lcdc_fb",
  1061. .owner = THIS_MODULE,
  1062. .pm = &sh_mobile_lcdc_dev_pm_ops,
  1063. },
  1064. .probe = sh_mobile_lcdc_probe,
  1065. .remove = sh_mobile_lcdc_remove,
  1066. };
  1067. static int __init sh_mobile_lcdc_init(void)
  1068. {
  1069. return platform_driver_register(&sh_mobile_lcdc_driver);
  1070. }
  1071. static void __exit sh_mobile_lcdc_exit(void)
  1072. {
  1073. platform_driver_unregister(&sh_mobile_lcdc_driver);
  1074. }
  1075. module_init(sh_mobile_lcdc_init);
  1076. module_exit(sh_mobile_lcdc_exit);
  1077. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  1078. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  1079. MODULE_LICENSE("GPL v2");