sh_mobile_lcdcfb.c 35 KB

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