sh_mobile_lcdcfb.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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 <linux/backlight.h>
  24. #include <linux/gpio.h>
  25. #include <linux/module.h>
  26. #include <video/sh_mobile_lcdc.h>
  27. #include <video/sh_mobile_meram.h>
  28. #include <linux/atomic.h>
  29. #include "sh_mobile_lcdcfb.h"
  30. #define SIDE_B_OFFSET 0x1000
  31. #define MIRROR_OFFSET 0x2000
  32. #define MAX_XRES 1920
  33. #define MAX_YRES 1080
  34. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  35. [LDDCKPAT1R] = 0x400,
  36. [LDDCKPAT2R] = 0x404,
  37. [LDMT1R] = 0x418,
  38. [LDMT2R] = 0x41c,
  39. [LDMT3R] = 0x420,
  40. [LDDFR] = 0x424,
  41. [LDSM1R] = 0x428,
  42. [LDSM2R] = 0x42c,
  43. [LDSA1R] = 0x430,
  44. [LDSA2R] = 0x434,
  45. [LDMLSR] = 0x438,
  46. [LDHCNR] = 0x448,
  47. [LDHSYNR] = 0x44c,
  48. [LDVLNR] = 0x450,
  49. [LDVSYNR] = 0x454,
  50. [LDPMR] = 0x460,
  51. [LDHAJR] = 0x4a0,
  52. };
  53. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  54. [LDDCKPAT1R] = 0x408,
  55. [LDDCKPAT2R] = 0x40c,
  56. [LDMT1R] = 0x600,
  57. [LDMT2R] = 0x604,
  58. [LDMT3R] = 0x608,
  59. [LDDFR] = 0x60c,
  60. [LDSM1R] = 0x610,
  61. [LDSM2R] = 0x614,
  62. [LDSA1R] = 0x618,
  63. [LDMLSR] = 0x620,
  64. [LDHCNR] = 0x624,
  65. [LDHSYNR] = 0x628,
  66. [LDVLNR] = 0x62c,
  67. [LDVSYNR] = 0x630,
  68. [LDPMR] = 0x63c,
  69. };
  70. static const struct fb_videomode default_720p = {
  71. .name = "HDMI 720p",
  72. .xres = 1280,
  73. .yres = 720,
  74. .left_margin = 220,
  75. .right_margin = 110,
  76. .hsync_len = 40,
  77. .upper_margin = 20,
  78. .lower_margin = 5,
  79. .vsync_len = 5,
  80. .pixclock = 13468,
  81. .refresh = 60,
  82. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  83. };
  84. struct sh_mobile_lcdc_priv {
  85. void __iomem *base;
  86. int irq;
  87. atomic_t hw_usecnt;
  88. struct device *dev;
  89. struct clk *dot_clk;
  90. unsigned long lddckr;
  91. struct sh_mobile_lcdc_chan ch[2];
  92. struct notifier_block notifier;
  93. int started;
  94. int forced_bpp; /* 2 channel LCDC must share bpp setting */
  95. struct sh_mobile_meram_info *meram_dev;
  96. };
  97. static bool banked(int reg_nr)
  98. {
  99. switch (reg_nr) {
  100. case LDMT1R:
  101. case LDMT2R:
  102. case LDMT3R:
  103. case LDDFR:
  104. case LDSM1R:
  105. case LDSA1R:
  106. case LDSA2R:
  107. case LDMLSR:
  108. case LDHCNR:
  109. case LDHSYNR:
  110. case LDVLNR:
  111. case LDVSYNR:
  112. return true;
  113. }
  114. return false;
  115. }
  116. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  117. int reg_nr, unsigned long data)
  118. {
  119. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  120. if (banked(reg_nr))
  121. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  122. SIDE_B_OFFSET);
  123. }
  124. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  125. int reg_nr, unsigned long data)
  126. {
  127. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  128. MIRROR_OFFSET);
  129. }
  130. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  131. int reg_nr)
  132. {
  133. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  134. }
  135. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  136. unsigned long reg_offs, unsigned long data)
  137. {
  138. iowrite32(data, priv->base + reg_offs);
  139. }
  140. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  141. unsigned long reg_offs)
  142. {
  143. return ioread32(priv->base + reg_offs);
  144. }
  145. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  146. unsigned long reg_offs,
  147. unsigned long mask, unsigned long until)
  148. {
  149. while ((lcdc_read(priv, reg_offs) & mask) != until)
  150. cpu_relax();
  151. }
  152. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  153. {
  154. return chan->cfg.chan == LCDC_CHAN_SUBLCD;
  155. }
  156. static void lcdc_sys_write_index(void *handle, unsigned long data)
  157. {
  158. struct sh_mobile_lcdc_chan *ch = handle;
  159. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
  160. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  161. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  162. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  163. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  164. }
  165. static void lcdc_sys_write_data(void *handle, unsigned long data)
  166. {
  167. struct sh_mobile_lcdc_chan *ch = handle;
  168. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
  169. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  170. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  171. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  172. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  173. }
  174. static unsigned long lcdc_sys_read_data(void *handle)
  175. {
  176. struct sh_mobile_lcdc_chan *ch = handle;
  177. lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
  178. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  179. lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
  180. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  181. udelay(1);
  182. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  183. return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
  184. }
  185. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  186. lcdc_sys_write_index,
  187. lcdc_sys_write_data,
  188. lcdc_sys_read_data,
  189. };
  190. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  191. {
  192. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  193. if (priv->dot_clk)
  194. clk_enable(priv->dot_clk);
  195. pm_runtime_get_sync(priv->dev);
  196. if (priv->meram_dev && priv->meram_dev->pdev)
  197. pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
  198. }
  199. }
  200. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  201. {
  202. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  203. if (priv->meram_dev && priv->meram_dev->pdev)
  204. pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
  205. pm_runtime_put(priv->dev);
  206. if (priv->dot_clk)
  207. clk_disable(priv->dot_clk);
  208. }
  209. }
  210. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  211. struct list_head *pagelist)
  212. {
  213. struct sh_mobile_lcdc_chan *ch = info->par;
  214. unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
  215. struct page *page;
  216. int nr_pages = 0;
  217. sg_init_table(ch->sglist, nr_pages_max);
  218. list_for_each_entry(page, pagelist, lru)
  219. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  220. return nr_pages;
  221. }
  222. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  223. struct list_head *pagelist)
  224. {
  225. struct sh_mobile_lcdc_chan *ch = info->par;
  226. struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
  227. /* enable clocks before accessing hardware */
  228. sh_mobile_lcdc_clk_on(ch->lcdc);
  229. /*
  230. * It's possible to get here without anything on the pagelist via
  231. * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
  232. * invocation. In the former case, the acceleration routines are
  233. * stepped in to when using the framebuffer console causing the
  234. * workqueue to be scheduled without any dirty pages on the list.
  235. *
  236. * Despite this, a panel update is still needed given that the
  237. * acceleration routines have their own methods for writing in
  238. * that still need to be updated.
  239. *
  240. * The fsync() and empty pagelist case could be optimized for,
  241. * but we don't bother, as any application exhibiting such
  242. * behaviour is fundamentally broken anyways.
  243. */
  244. if (!list_empty(pagelist)) {
  245. unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  246. /* trigger panel update */
  247. dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  248. if (bcfg->start_transfer)
  249. bcfg->start_transfer(bcfg->board_data, ch,
  250. &sh_mobile_lcdc_sys_bus_ops);
  251. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  252. dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  253. } else {
  254. if (bcfg->start_transfer)
  255. bcfg->start_transfer(bcfg->board_data, ch,
  256. &sh_mobile_lcdc_sys_bus_ops);
  257. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  258. }
  259. }
  260. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  261. {
  262. struct fb_deferred_io *fbdefio = info->fbdefio;
  263. if (fbdefio)
  264. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  265. }
  266. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  267. {
  268. struct sh_mobile_lcdc_priv *priv = data;
  269. struct sh_mobile_lcdc_chan *ch;
  270. unsigned long ldintr;
  271. int is_sub;
  272. int k;
  273. /* Acknowledge interrupts and disable further VSYNC End IRQs. */
  274. ldintr = lcdc_read(priv, _LDINTR);
  275. lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
  276. /* figure out if this interrupt is for main or sub lcd */
  277. is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
  278. /* wake up channel and disable clocks */
  279. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  280. ch = &priv->ch[k];
  281. if (!ch->enabled)
  282. continue;
  283. /* Frame End */
  284. if (ldintr & LDINTR_FS) {
  285. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  286. ch->frame_end = 1;
  287. wake_up(&ch->frame_end_wait);
  288. sh_mobile_lcdc_clk_off(priv);
  289. }
  290. }
  291. /* VSYNC End */
  292. if (ldintr & LDINTR_VES)
  293. complete(&ch->vsync_completion);
  294. }
  295. return IRQ_HANDLED;
  296. }
  297. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  298. int start)
  299. {
  300. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  301. int k;
  302. /* start or stop the lcdc */
  303. if (start)
  304. lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
  305. else
  306. lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
  307. /* wait until power is applied/stopped on all channels */
  308. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  309. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  310. while (1) {
  311. tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
  312. & LDPMR_LPS;
  313. if (start && tmp == LDPMR_LPS)
  314. break;
  315. if (!start && tmp == 0)
  316. break;
  317. cpu_relax();
  318. }
  319. if (!start)
  320. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  321. }
  322. static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
  323. {
  324. struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
  325. unsigned long h_total, hsync_pos, display_h_total;
  326. u32 tmp;
  327. tmp = ch->ldmt1r_value;
  328. tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
  329. tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
  330. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
  331. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
  332. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
  333. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
  334. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
  335. lcdc_write_chan(ch, LDMT1R, tmp);
  336. /* setup SYS bus */
  337. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  338. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  339. /* horizontal configuration */
  340. h_total = display_var->xres + display_var->hsync_len +
  341. display_var->left_margin + display_var->right_margin;
  342. tmp = h_total / 8; /* HTCN */
  343. tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
  344. lcdc_write_chan(ch, LDHCNR, tmp);
  345. hsync_pos = display_var->xres + display_var->right_margin;
  346. tmp = hsync_pos / 8; /* HSYNP */
  347. tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
  348. lcdc_write_chan(ch, LDHSYNR, tmp);
  349. /* vertical configuration */
  350. tmp = display_var->yres + display_var->vsync_len +
  351. display_var->upper_margin + display_var->lower_margin; /* VTLN */
  352. tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
  353. lcdc_write_chan(ch, LDVLNR, tmp);
  354. tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
  355. tmp |= display_var->vsync_len << 16; /* VSYNW */
  356. lcdc_write_chan(ch, LDVSYNR, tmp);
  357. /* Adjust horizontal synchronisation for HDMI */
  358. display_h_total = display_var->xres + display_var->hsync_len +
  359. display_var->left_margin + display_var->right_margin;
  360. tmp = ((display_var->xres & 7) << 24) |
  361. ((display_h_total & 7) << 16) |
  362. ((display_var->hsync_len & 7) << 8) |
  363. hsync_pos;
  364. lcdc_write_chan(ch, LDHAJR, tmp);
  365. }
  366. /*
  367. * __sh_mobile_lcdc_start - Configure and tart the LCDC
  368. * @priv: LCDC device
  369. *
  370. * Configure all enabled channels and start the LCDC device. All external
  371. * devices (clocks, MERAM, panels, ...) are not touched by this function.
  372. */
  373. static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  374. {
  375. struct sh_mobile_lcdc_chan *ch;
  376. unsigned long tmp;
  377. int bpp = 0;
  378. int k, m;
  379. /* Enable LCDC channels. Read data from external memory, avoid using the
  380. * BEU for now.
  381. */
  382. lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
  383. /* Stop the LCDC first and disable all interrupts. */
  384. sh_mobile_lcdc_start_stop(priv, 0);
  385. lcdc_write(priv, _LDINTR, 0);
  386. /* Configure power supply, dot clocks and start them. */
  387. tmp = priv->lddckr;
  388. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  389. ch = &priv->ch[k];
  390. if (!ch->enabled)
  391. continue;
  392. if (!bpp)
  393. bpp = ch->info->var.bits_per_pixel;
  394. /* Power supply */
  395. lcdc_write_chan(ch, LDPMR, 0);
  396. m = ch->cfg.clock_divider;
  397. if (!m)
  398. continue;
  399. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
  400. * denominator.
  401. */
  402. lcdc_write_chan(ch, LDDCKPAT1R, 0);
  403. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  404. if (m == 1)
  405. m = LDDCKR_MOSEL;
  406. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  407. }
  408. lcdc_write(priv, _LDDCKR, tmp);
  409. lcdc_write(priv, _LDDCKSTPR, 0);
  410. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  411. /* Setup geometry, format, frame buffer memory and operation mode. */
  412. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  413. ch = &priv->ch[k];
  414. if (!ch->enabled)
  415. continue;
  416. sh_mobile_lcdc_geometry(ch);
  417. if (ch->info->var.nonstd) {
  418. tmp = (ch->info->var.nonstd << 16);
  419. switch (ch->info->var.bits_per_pixel) {
  420. case 12:
  421. tmp |= LDDFR_YF_420;
  422. break;
  423. case 16:
  424. tmp |= LDDFR_YF_422;
  425. break;
  426. case 24:
  427. default:
  428. tmp |= LDDFR_YF_444;
  429. break;
  430. }
  431. } else {
  432. switch (ch->info->var.bits_per_pixel) {
  433. case 16:
  434. tmp = LDDFR_PKF_RGB16;
  435. break;
  436. case 24:
  437. tmp = LDDFR_PKF_RGB24;
  438. break;
  439. case 32:
  440. default:
  441. tmp = LDDFR_PKF_ARGB32;
  442. break;
  443. }
  444. }
  445. lcdc_write_chan(ch, LDDFR, tmp);
  446. lcdc_write_chan(ch, LDMLSR, ch->pitch);
  447. lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
  448. if (ch->info->var.nonstd)
  449. lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
  450. /* When using deferred I/O mode, configure the LCDC for one-shot
  451. * operation and enable the frame end interrupt. Otherwise use
  452. * continuous read mode.
  453. */
  454. if (ch->ldmt1r_value & LDMT1R_IFM &&
  455. ch->cfg.sys_bus_cfg.deferred_io_msec) {
  456. lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
  457. lcdc_write(priv, _LDINTR, LDINTR_FE);
  458. } else {
  459. lcdc_write_chan(ch, LDSM1R, 0);
  460. }
  461. }
  462. /* Word and long word swap. */
  463. if (priv->ch[0].info->var.nonstd)
  464. tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
  465. else {
  466. switch (bpp) {
  467. case 16:
  468. tmp = LDDDSR_LS | LDDDSR_WS;
  469. break;
  470. case 24:
  471. tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
  472. break;
  473. case 32:
  474. default:
  475. tmp = LDDDSR_LS;
  476. break;
  477. }
  478. }
  479. lcdc_write(priv, _LDDDSR, tmp);
  480. /* Enable the display output. */
  481. lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
  482. sh_mobile_lcdc_start_stop(priv, 1);
  483. priv->started = 1;
  484. }
  485. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  486. {
  487. struct sh_mobile_meram_info *mdev = priv->meram_dev;
  488. struct sh_mobile_lcdc_board_cfg *board_cfg;
  489. struct sh_mobile_lcdc_chan *ch;
  490. unsigned long tmp;
  491. int ret;
  492. int k;
  493. /* enable clocks before accessing the hardware */
  494. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  495. if (priv->ch[k].enabled)
  496. sh_mobile_lcdc_clk_on(priv);
  497. }
  498. /* reset */
  499. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
  500. lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
  501. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  502. ch = &priv->ch[k];
  503. if (!ch->enabled)
  504. continue;
  505. board_cfg = &ch->cfg.board_cfg;
  506. if (board_cfg->setup_sys) {
  507. ret = board_cfg->setup_sys(board_cfg->board_data, ch,
  508. &sh_mobile_lcdc_sys_bus_ops);
  509. if (ret)
  510. return ret;
  511. }
  512. }
  513. /* Compute frame buffer base address and pitch for each channel. */
  514. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  515. struct sh_mobile_meram_cfg *cfg;
  516. int pixelformat;
  517. ch = &priv->ch[k];
  518. if (!ch->enabled)
  519. continue;
  520. ch->base_addr_y = ch->info->fix.smem_start;
  521. ch->base_addr_c = ch->base_addr_y
  522. + ch->info->var.xres
  523. * ch->info->var.yres_virtual;
  524. ch->pitch = ch->info->fix.line_length;
  525. /* Enable MERAM if possible. */
  526. cfg = ch->cfg.meram_cfg;
  527. if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
  528. continue;
  529. /* we need to de-init configured ICBs before we can
  530. * re-initialize them.
  531. */
  532. if (ch->meram_enabled) {
  533. mdev->ops->meram_unregister(mdev, cfg);
  534. ch->meram_enabled = 0;
  535. }
  536. if (!ch->info->var.nonstd)
  537. pixelformat = SH_MOBILE_MERAM_PF_RGB;
  538. else if (ch->info->var.bits_per_pixel == 24)
  539. pixelformat = SH_MOBILE_MERAM_PF_NV24;
  540. else
  541. pixelformat = SH_MOBILE_MERAM_PF_NV;
  542. ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
  543. ch->info->var.yres, pixelformat,
  544. ch->base_addr_y, ch->base_addr_c,
  545. &ch->base_addr_y, &ch->base_addr_c,
  546. &ch->pitch);
  547. if (!ret)
  548. ch->meram_enabled = 1;
  549. }
  550. /* Start the LCDC. */
  551. __sh_mobile_lcdc_start(priv);
  552. /* Setup deferred I/O, tell the board code to enable the panels, and
  553. * turn backlight on.
  554. */
  555. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  556. ch = &priv->ch[k];
  557. if (!ch->enabled)
  558. continue;
  559. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  560. if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
  561. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  562. ch->defio.delay = msecs_to_jiffies(tmp);
  563. ch->info->fbdefio = &ch->defio;
  564. fb_deferred_io_init(ch->info);
  565. }
  566. board_cfg = &ch->cfg.board_cfg;
  567. if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
  568. board_cfg->display_on(board_cfg->board_data, ch->info);
  569. module_put(board_cfg->owner);
  570. }
  571. if (ch->bl) {
  572. ch->bl->props.power = FB_BLANK_UNBLANK;
  573. backlight_update_status(ch->bl);
  574. }
  575. }
  576. return 0;
  577. }
  578. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  579. {
  580. struct sh_mobile_lcdc_chan *ch;
  581. struct sh_mobile_lcdc_board_cfg *board_cfg;
  582. int k;
  583. /* clean up deferred io and ask board code to disable panel */
  584. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  585. ch = &priv->ch[k];
  586. if (!ch->enabled)
  587. continue;
  588. /* deferred io mode:
  589. * flush frame, and wait for frame end interrupt
  590. * clean up deferred io and enable clock
  591. */
  592. if (ch->info && ch->info->fbdefio) {
  593. ch->frame_end = 0;
  594. schedule_delayed_work(&ch->info->deferred_work, 0);
  595. wait_event(ch->frame_end_wait, ch->frame_end);
  596. fb_deferred_io_cleanup(ch->info);
  597. ch->info->fbdefio = NULL;
  598. sh_mobile_lcdc_clk_on(priv);
  599. }
  600. if (ch->bl) {
  601. ch->bl->props.power = FB_BLANK_POWERDOWN;
  602. backlight_update_status(ch->bl);
  603. }
  604. board_cfg = &ch->cfg.board_cfg;
  605. if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
  606. board_cfg->display_off(board_cfg->board_data);
  607. module_put(board_cfg->owner);
  608. }
  609. /* disable the meram */
  610. if (ch->meram_enabled) {
  611. struct sh_mobile_meram_cfg *cfg;
  612. struct sh_mobile_meram_info *mdev;
  613. cfg = ch->cfg.meram_cfg;
  614. mdev = priv->meram_dev;
  615. mdev->ops->meram_unregister(mdev, cfg);
  616. ch->meram_enabled = 0;
  617. }
  618. }
  619. /* stop the lcdc */
  620. if (priv->started) {
  621. sh_mobile_lcdc_start_stop(priv, 0);
  622. priv->started = 0;
  623. }
  624. /* stop clocks */
  625. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  626. if (priv->ch[k].enabled)
  627. sh_mobile_lcdc_clk_off(priv);
  628. }
  629. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  630. {
  631. int interface_type = ch->cfg.interface_type;
  632. switch (interface_type) {
  633. case RGB8:
  634. case RGB9:
  635. case RGB12A:
  636. case RGB12B:
  637. case RGB16:
  638. case RGB18:
  639. case RGB24:
  640. case SYS8A:
  641. case SYS8B:
  642. case SYS8C:
  643. case SYS8D:
  644. case SYS9:
  645. case SYS12:
  646. case SYS16A:
  647. case SYS16B:
  648. case SYS16C:
  649. case SYS18:
  650. case SYS24:
  651. break;
  652. default:
  653. return -EINVAL;
  654. }
  655. /* SUBLCD only supports SYS interface */
  656. if (lcdc_chan_is_sublcd(ch)) {
  657. if (!(interface_type & LDMT1R_IFM))
  658. return -EINVAL;
  659. interface_type &= ~LDMT1R_IFM;
  660. }
  661. ch->ldmt1r_value = interface_type;
  662. return 0;
  663. }
  664. static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
  665. int clock_source,
  666. struct sh_mobile_lcdc_priv *priv)
  667. {
  668. char *str;
  669. switch (clock_source) {
  670. case LCDC_CLK_BUS:
  671. str = "bus_clk";
  672. priv->lddckr = LDDCKR_ICKSEL_BUS;
  673. break;
  674. case LCDC_CLK_PERIPHERAL:
  675. str = "peripheral_clk";
  676. priv->lddckr = LDDCKR_ICKSEL_MIPI;
  677. break;
  678. case LCDC_CLK_EXTERNAL:
  679. str = NULL;
  680. priv->lddckr = LDDCKR_ICKSEL_HDMI;
  681. break;
  682. default:
  683. return -EINVAL;
  684. }
  685. if (str) {
  686. priv->dot_clk = clk_get(&pdev->dev, str);
  687. if (IS_ERR(priv->dot_clk)) {
  688. dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
  689. return PTR_ERR(priv->dot_clk);
  690. }
  691. }
  692. /* Runtime PM support involves two step for this driver:
  693. * 1) Enable Runtime PM
  694. * 2) Force Runtime PM Resume since hardware is accessed from probe()
  695. */
  696. priv->dev = &pdev->dev;
  697. pm_runtime_enable(priv->dev);
  698. pm_runtime_resume(priv->dev);
  699. return 0;
  700. }
  701. static int sh_mobile_lcdc_setcolreg(u_int regno,
  702. u_int red, u_int green, u_int blue,
  703. u_int transp, struct fb_info *info)
  704. {
  705. u32 *palette = info->pseudo_palette;
  706. if (regno >= PALETTE_NR)
  707. return -EINVAL;
  708. /* only FB_VISUAL_TRUECOLOR supported */
  709. red >>= 16 - info->var.red.length;
  710. green >>= 16 - info->var.green.length;
  711. blue >>= 16 - info->var.blue.length;
  712. transp >>= 16 - info->var.transp.length;
  713. palette[regno] = (red << info->var.red.offset) |
  714. (green << info->var.green.offset) |
  715. (blue << info->var.blue.offset) |
  716. (transp << info->var.transp.offset);
  717. return 0;
  718. }
  719. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  720. .id = "SH Mobile LCDC",
  721. .type = FB_TYPE_PACKED_PIXELS,
  722. .visual = FB_VISUAL_TRUECOLOR,
  723. .accel = FB_ACCEL_NONE,
  724. .xpanstep = 0,
  725. .ypanstep = 1,
  726. .ywrapstep = 0,
  727. };
  728. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  729. const struct fb_fillrect *rect)
  730. {
  731. sys_fillrect(info, rect);
  732. sh_mobile_lcdc_deferred_io_touch(info);
  733. }
  734. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  735. const struct fb_copyarea *area)
  736. {
  737. sys_copyarea(info, area);
  738. sh_mobile_lcdc_deferred_io_touch(info);
  739. }
  740. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  741. const struct fb_image *image)
  742. {
  743. sys_imageblit(info, image);
  744. sh_mobile_lcdc_deferred_io_touch(info);
  745. }
  746. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  747. struct fb_info *info)
  748. {
  749. struct sh_mobile_lcdc_chan *ch = info->par;
  750. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  751. unsigned long ldrcntr;
  752. unsigned long new_pan_offset;
  753. unsigned long base_addr_y, base_addr_c;
  754. unsigned long c_offset;
  755. if (!info->var.nonstd)
  756. new_pan_offset = var->yoffset * info->fix.line_length
  757. + var->xoffset * (info->var.bits_per_pixel / 8);
  758. else
  759. new_pan_offset = var->yoffset * info->fix.line_length
  760. + var->xoffset;
  761. if (new_pan_offset == ch->pan_offset)
  762. return 0; /* No change, do nothing */
  763. ldrcntr = lcdc_read(priv, _LDRCNTR);
  764. /* Set the source address for the next refresh */
  765. base_addr_y = ch->dma_handle + new_pan_offset;
  766. if (info->var.nonstd) {
  767. /* Set y offset */
  768. c_offset = var->yoffset * info->fix.line_length
  769. * (info->var.bits_per_pixel - 8) / 8;
  770. base_addr_c = ch->dma_handle
  771. + info->var.xres * info->var.yres_virtual
  772. + c_offset;
  773. /* Set x offset */
  774. if (info->var.bits_per_pixel == 24)
  775. base_addr_c += 2 * var->xoffset;
  776. else
  777. base_addr_c += var->xoffset;
  778. }
  779. if (ch->meram_enabled) {
  780. struct sh_mobile_meram_cfg *cfg;
  781. struct sh_mobile_meram_info *mdev;
  782. int ret;
  783. cfg = ch->cfg.meram_cfg;
  784. mdev = priv->meram_dev;
  785. ret = mdev->ops->meram_update(mdev, cfg,
  786. base_addr_y, base_addr_c,
  787. &base_addr_y, &base_addr_c);
  788. if (ret)
  789. return ret;
  790. }
  791. ch->base_addr_y = base_addr_y;
  792. ch->base_addr_c = base_addr_c;
  793. lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
  794. if (info->var.nonstd)
  795. lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
  796. if (lcdc_chan_is_sublcd(ch))
  797. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  798. else
  799. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  800. ch->pan_offset = new_pan_offset;
  801. sh_mobile_lcdc_deferred_io_touch(info);
  802. return 0;
  803. }
  804. static int sh_mobile_wait_for_vsync(struct fb_info *info)
  805. {
  806. struct sh_mobile_lcdc_chan *ch = info->par;
  807. unsigned long ldintr;
  808. int ret;
  809. /* Enable VSync End interrupt and be careful not to acknowledge any
  810. * pending interrupt.
  811. */
  812. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  813. ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
  814. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  815. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  816. msecs_to_jiffies(100));
  817. if (!ret)
  818. return -ETIMEDOUT;
  819. return 0;
  820. }
  821. static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
  822. unsigned long arg)
  823. {
  824. int retval;
  825. switch (cmd) {
  826. case FBIO_WAITFORVSYNC:
  827. retval = sh_mobile_wait_for_vsync(info);
  828. break;
  829. default:
  830. retval = -ENOIOCTLCMD;
  831. break;
  832. }
  833. return retval;
  834. }
  835. static void sh_mobile_fb_reconfig(struct fb_info *info)
  836. {
  837. struct sh_mobile_lcdc_chan *ch = info->par;
  838. struct fb_videomode mode1, mode2;
  839. struct fb_event event;
  840. int evnt = FB_EVENT_MODE_CHANGE_ALL;
  841. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  842. /* More framebuffer users are active */
  843. return;
  844. fb_var_to_videomode(&mode1, &ch->display_var);
  845. fb_var_to_videomode(&mode2, &info->var);
  846. if (fb_mode_is_equal(&mode1, &mode2))
  847. return;
  848. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  849. if (fb_set_var(info, &ch->display_var) < 0)
  850. /* Couldn't reconfigure, hopefully, can continue as before */
  851. return;
  852. /*
  853. * fb_set_var() calls the notifier change internally, only if
  854. * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
  855. * user event, we have to call the chain ourselves.
  856. */
  857. event.info = info;
  858. event.data = &mode1;
  859. fb_notifier_call_chain(evnt, &event);
  860. }
  861. /*
  862. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  863. * user == 1, or with console sem held, if user == 0.
  864. */
  865. static int sh_mobile_release(struct fb_info *info, int user)
  866. {
  867. struct sh_mobile_lcdc_chan *ch = info->par;
  868. mutex_lock(&ch->open_lock);
  869. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  870. ch->use_count--;
  871. /* Nothing to reconfigure, when called from fbcon */
  872. if (user) {
  873. console_lock();
  874. sh_mobile_fb_reconfig(info);
  875. console_unlock();
  876. }
  877. mutex_unlock(&ch->open_lock);
  878. return 0;
  879. }
  880. static int sh_mobile_open(struct fb_info *info, int user)
  881. {
  882. struct sh_mobile_lcdc_chan *ch = info->par;
  883. mutex_lock(&ch->open_lock);
  884. ch->use_count++;
  885. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  886. mutex_unlock(&ch->open_lock);
  887. return 0;
  888. }
  889. static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  890. {
  891. struct sh_mobile_lcdc_chan *ch = info->par;
  892. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  893. unsigned int best_dist = (unsigned int)-1;
  894. unsigned int best_xres = 0;
  895. unsigned int best_yres = 0;
  896. unsigned int i;
  897. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  898. return -EINVAL;
  899. /* If board code provides us with a list of available modes, make sure
  900. * we use one of them. Find the mode closest to the requested one. The
  901. * distance between two modes is defined as the size of the
  902. * non-overlapping parts of the two rectangles.
  903. */
  904. for (i = 0; i < ch->cfg.num_cfg; ++i) {
  905. const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
  906. unsigned int dist;
  907. /* We can only round up. */
  908. if (var->xres > mode->xres || var->yres > mode->yres)
  909. continue;
  910. dist = var->xres * var->yres + mode->xres * mode->yres
  911. - 2 * min(var->xres, mode->xres)
  912. * min(var->yres, mode->yres);
  913. if (dist < best_dist) {
  914. best_xres = mode->xres;
  915. best_yres = mode->yres;
  916. best_dist = dist;
  917. }
  918. }
  919. /* If no available mode can be used, return an error. */
  920. if (ch->cfg.num_cfg != 0) {
  921. if (best_dist == (unsigned int)-1)
  922. return -EINVAL;
  923. var->xres = best_xres;
  924. var->yres = best_yres;
  925. }
  926. /* Make sure the virtual resolution is at least as big as the visible
  927. * resolution.
  928. */
  929. if (var->xres_virtual < var->xres)
  930. var->xres_virtual = var->xres;
  931. if (var->yres_virtual < var->yres)
  932. var->yres_virtual = var->yres;
  933. if (var->bits_per_pixel <= 16) { /* RGB 565 */
  934. var->bits_per_pixel = 16;
  935. var->red.offset = 11;
  936. var->red.length = 5;
  937. var->green.offset = 5;
  938. var->green.length = 6;
  939. var->blue.offset = 0;
  940. var->blue.length = 5;
  941. var->transp.offset = 0;
  942. var->transp.length = 0;
  943. } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
  944. var->bits_per_pixel = 24;
  945. var->red.offset = 16;
  946. var->red.length = 8;
  947. var->green.offset = 8;
  948. var->green.length = 8;
  949. var->blue.offset = 0;
  950. var->blue.length = 8;
  951. var->transp.offset = 0;
  952. var->transp.length = 0;
  953. } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
  954. var->bits_per_pixel = 32;
  955. var->red.offset = 16;
  956. var->red.length = 8;
  957. var->green.offset = 8;
  958. var->green.length = 8;
  959. var->blue.offset = 0;
  960. var->blue.length = 8;
  961. var->transp.offset = 24;
  962. var->transp.length = 8;
  963. } else
  964. return -EINVAL;
  965. var->red.msb_right = 0;
  966. var->green.msb_right = 0;
  967. var->blue.msb_right = 0;
  968. var->transp.msb_right = 0;
  969. /* Make sure we don't exceed our allocated memory. */
  970. if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
  971. info->fix.smem_len)
  972. return -EINVAL;
  973. /* only accept the forced_bpp for dual channel configurations */
  974. if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
  975. return -EINVAL;
  976. return 0;
  977. }
  978. static int sh_mobile_set_par(struct fb_info *info)
  979. {
  980. struct sh_mobile_lcdc_chan *ch = info->par;
  981. u32 line_length = info->fix.line_length;
  982. int ret;
  983. sh_mobile_lcdc_stop(ch->lcdc);
  984. if (info->var.nonstd)
  985. info->fix.line_length = info->var.xres;
  986. else
  987. info->fix.line_length = info->var.xres
  988. * info->var.bits_per_pixel / 8;
  989. ret = sh_mobile_lcdc_start(ch->lcdc);
  990. if (ret < 0) {
  991. dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
  992. info->fix.line_length = line_length;
  993. }
  994. return ret;
  995. }
  996. /*
  997. * Screen blanking. Behavior is as follows:
  998. * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
  999. * FB_BLANK_NORMAL: screen blanked, clocks enabled
  1000. * FB_BLANK_VSYNC,
  1001. * FB_BLANK_HSYNC,
  1002. * FB_BLANK_POWEROFF: screen blanked, clocks disabled
  1003. */
  1004. static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
  1005. {
  1006. struct sh_mobile_lcdc_chan *ch = info->par;
  1007. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1008. /* blank the screen? */
  1009. if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
  1010. struct fb_fillrect rect = {
  1011. .width = info->var.xres,
  1012. .height = info->var.yres,
  1013. };
  1014. sh_mobile_lcdc_fillrect(info, &rect);
  1015. }
  1016. /* turn clocks on? */
  1017. if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
  1018. sh_mobile_lcdc_clk_on(p);
  1019. }
  1020. /* turn clocks off? */
  1021. if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
  1022. /* make sure the screen is updated with the black fill before
  1023. * switching the clocks off. one vsync is not enough since
  1024. * blanking may occur in the middle of a refresh. deferred io
  1025. * mode will reenable the clocks and update the screen in time,
  1026. * so it does not need this. */
  1027. if (!info->fbdefio) {
  1028. sh_mobile_wait_for_vsync(info);
  1029. sh_mobile_wait_for_vsync(info);
  1030. }
  1031. sh_mobile_lcdc_clk_off(p);
  1032. }
  1033. ch->blank_status = blank;
  1034. return 0;
  1035. }
  1036. static struct fb_ops sh_mobile_lcdc_ops = {
  1037. .owner = THIS_MODULE,
  1038. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  1039. .fb_read = fb_sys_read,
  1040. .fb_write = fb_sys_write,
  1041. .fb_fillrect = sh_mobile_lcdc_fillrect,
  1042. .fb_copyarea = sh_mobile_lcdc_copyarea,
  1043. .fb_imageblit = sh_mobile_lcdc_imageblit,
  1044. .fb_blank = sh_mobile_lcdc_blank,
  1045. .fb_pan_display = sh_mobile_fb_pan_display,
  1046. .fb_ioctl = sh_mobile_ioctl,
  1047. .fb_open = sh_mobile_open,
  1048. .fb_release = sh_mobile_release,
  1049. .fb_check_var = sh_mobile_check_var,
  1050. .fb_set_par = sh_mobile_set_par,
  1051. };
  1052. static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
  1053. {
  1054. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1055. struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
  1056. int brightness = bdev->props.brightness;
  1057. if (bdev->props.power != FB_BLANK_UNBLANK ||
  1058. bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  1059. brightness = 0;
  1060. return cfg->set_brightness(cfg->board_data, brightness);
  1061. }
  1062. static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
  1063. {
  1064. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1065. struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
  1066. return cfg->get_brightness(cfg->board_data);
  1067. }
  1068. static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
  1069. struct fb_info *info)
  1070. {
  1071. return (info->bl_dev == bdev);
  1072. }
  1073. static struct backlight_ops sh_mobile_lcdc_bl_ops = {
  1074. .options = BL_CORE_SUSPENDRESUME,
  1075. .update_status = sh_mobile_lcdc_update_bl,
  1076. .get_brightness = sh_mobile_lcdc_get_brightness,
  1077. .check_fb = sh_mobile_lcdc_check_fb,
  1078. };
  1079. static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
  1080. struct sh_mobile_lcdc_chan *ch)
  1081. {
  1082. struct backlight_device *bl;
  1083. bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
  1084. &sh_mobile_lcdc_bl_ops, NULL);
  1085. if (IS_ERR(bl)) {
  1086. dev_err(parent, "unable to register backlight device: %ld\n",
  1087. PTR_ERR(bl));
  1088. return NULL;
  1089. }
  1090. bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
  1091. bl->props.brightness = bl->props.max_brightness;
  1092. backlight_update_status(bl);
  1093. return bl;
  1094. }
  1095. static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
  1096. {
  1097. backlight_device_unregister(bdev);
  1098. }
  1099. static int sh_mobile_lcdc_suspend(struct device *dev)
  1100. {
  1101. struct platform_device *pdev = to_platform_device(dev);
  1102. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  1103. return 0;
  1104. }
  1105. static int sh_mobile_lcdc_resume(struct device *dev)
  1106. {
  1107. struct platform_device *pdev = to_platform_device(dev);
  1108. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  1109. }
  1110. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  1111. {
  1112. struct platform_device *pdev = to_platform_device(dev);
  1113. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1114. /* turn off LCDC hardware */
  1115. lcdc_write(priv, _LDCNT1R, 0);
  1116. return 0;
  1117. }
  1118. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  1119. {
  1120. struct platform_device *pdev = to_platform_device(dev);
  1121. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1122. __sh_mobile_lcdc_start(priv);
  1123. return 0;
  1124. }
  1125. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  1126. .suspend = sh_mobile_lcdc_suspend,
  1127. .resume = sh_mobile_lcdc_resume,
  1128. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  1129. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  1130. };
  1131. /* locking: called with info->lock held */
  1132. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  1133. unsigned long action, void *data)
  1134. {
  1135. struct fb_event *event = data;
  1136. struct fb_info *info = event->info;
  1137. struct sh_mobile_lcdc_chan *ch = info->par;
  1138. struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
  1139. if (&ch->lcdc->notifier != nb)
  1140. return NOTIFY_DONE;
  1141. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  1142. __func__, action, event->data);
  1143. switch(action) {
  1144. case FB_EVENT_SUSPEND:
  1145. if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
  1146. board_cfg->display_off(board_cfg->board_data);
  1147. module_put(board_cfg->owner);
  1148. }
  1149. sh_mobile_lcdc_stop(ch->lcdc);
  1150. break;
  1151. case FB_EVENT_RESUME:
  1152. mutex_lock(&ch->open_lock);
  1153. sh_mobile_fb_reconfig(info);
  1154. mutex_unlock(&ch->open_lock);
  1155. /* HDMI must be enabled before LCDC configuration */
  1156. if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
  1157. board_cfg->display_on(board_cfg->board_data, info);
  1158. module_put(board_cfg->owner);
  1159. }
  1160. sh_mobile_lcdc_start(ch->lcdc);
  1161. }
  1162. return NOTIFY_OK;
  1163. }
  1164. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1165. {
  1166. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1167. struct fb_info *info;
  1168. int i;
  1169. fb_unregister_client(&priv->notifier);
  1170. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1171. if (priv->ch[i].info && priv->ch[i].info->dev)
  1172. unregister_framebuffer(priv->ch[i].info);
  1173. sh_mobile_lcdc_stop(priv);
  1174. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1175. info = priv->ch[i].info;
  1176. if (!info || !info->device)
  1177. continue;
  1178. if (priv->ch[i].sglist)
  1179. vfree(priv->ch[i].sglist);
  1180. if (info->screen_base)
  1181. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  1182. info->screen_base,
  1183. priv->ch[i].dma_handle);
  1184. fb_dealloc_cmap(&info->cmap);
  1185. framebuffer_release(info);
  1186. }
  1187. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1188. if (priv->ch[i].bl)
  1189. sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
  1190. }
  1191. if (priv->dot_clk)
  1192. clk_put(priv->dot_clk);
  1193. if (priv->dev)
  1194. pm_runtime_disable(priv->dev);
  1195. if (priv->base)
  1196. iounmap(priv->base);
  1197. if (priv->irq)
  1198. free_irq(priv->irq, priv);
  1199. kfree(priv);
  1200. return 0;
  1201. }
  1202. static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
  1203. struct device *dev)
  1204. {
  1205. struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
  1206. const struct fb_videomode *max_mode;
  1207. const struct fb_videomode *mode;
  1208. struct fb_var_screeninfo *var;
  1209. struct fb_info *info;
  1210. unsigned int max_size;
  1211. int num_cfg;
  1212. void *buf;
  1213. int ret;
  1214. int i;
  1215. mutex_init(&ch->open_lock);
  1216. /* Allocate the frame buffer device. */
  1217. ch->info = framebuffer_alloc(0, dev);
  1218. if (!ch->info) {
  1219. dev_err(dev, "unable to allocate fb_info\n");
  1220. return -ENOMEM;
  1221. }
  1222. info = ch->info;
  1223. info->fbops = &sh_mobile_lcdc_ops;
  1224. info->par = ch;
  1225. info->pseudo_palette = &ch->pseudo_palette;
  1226. info->flags = FBINFO_FLAG_DEFAULT;
  1227. /* Iterate through the modes to validate them and find the highest
  1228. * resolution.
  1229. */
  1230. max_mode = NULL;
  1231. max_size = 0;
  1232. for (i = 0, mode = cfg->lcd_cfg; i < cfg->num_cfg; i++, mode++) {
  1233. unsigned int size = mode->yres * mode->xres;
  1234. /* NV12 buffers must have even number of lines */
  1235. if ((cfg->nonstd) && cfg->bpp == 12 &&
  1236. (mode->yres & 0x1)) {
  1237. dev_err(dev, "yres must be multiple of 2 for YCbCr420 "
  1238. "mode.\n");
  1239. return -EINVAL;
  1240. }
  1241. if (size > max_size) {
  1242. max_mode = mode;
  1243. max_size = size;
  1244. }
  1245. }
  1246. if (!max_size)
  1247. max_size = MAX_XRES * MAX_YRES;
  1248. else
  1249. dev_dbg(dev, "Found largest videomode %ux%u\n",
  1250. max_mode->xres, max_mode->yres);
  1251. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1252. * for NV12.
  1253. */
  1254. info->fix = sh_mobile_lcdc_fix;
  1255. info->fix.smem_len = max_size * 2 * cfg->bpp / 8;
  1256. if (cfg->nonstd && cfg->bpp == 12)
  1257. info->fix.ypanstep = 2;
  1258. /* Create the mode list. */
  1259. if (cfg->lcd_cfg == NULL) {
  1260. mode = &default_720p;
  1261. num_cfg = 1;
  1262. } else {
  1263. mode = cfg->lcd_cfg;
  1264. num_cfg = cfg->num_cfg;
  1265. }
  1266. fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
  1267. /* Initialize variable screen information using the first mode as
  1268. * default. The default Y virtual resolution is twice the panel size to
  1269. * allow for double-buffering.
  1270. */
  1271. var = &info->var;
  1272. fb_videomode_to_var(var, mode);
  1273. var->bits_per_pixel = cfg->bpp;
  1274. var->width = cfg->lcd_size_cfg.width;
  1275. var->height = cfg->lcd_size_cfg.height;
  1276. var->yres_virtual = var->yres * 2;
  1277. var->activate = FB_ACTIVATE_NOW;
  1278. ret = sh_mobile_check_var(var, info);
  1279. if (ret)
  1280. return ret;
  1281. /* Allocate frame buffer memory and color map. */
  1282. buf = dma_alloc_coherent(dev, info->fix.smem_len, &ch->dma_handle,
  1283. GFP_KERNEL);
  1284. if (!buf) {
  1285. dev_err(dev, "unable to allocate buffer\n");
  1286. return -ENOMEM;
  1287. }
  1288. ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1289. if (ret < 0) {
  1290. dev_err(dev, "unable to allocate cmap\n");
  1291. dma_free_coherent(dev, info->fix.smem_len,
  1292. buf, ch->dma_handle);
  1293. return ret;
  1294. }
  1295. info->fix.smem_start = ch->dma_handle;
  1296. if (var->nonstd)
  1297. info->fix.line_length = var->xres;
  1298. else
  1299. info->fix.line_length = var->xres * (cfg->bpp / 8);
  1300. info->screen_base = buf;
  1301. info->device = dev;
  1302. ch->display_var = *var;
  1303. return 0;
  1304. }
  1305. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  1306. {
  1307. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  1308. struct sh_mobile_lcdc_priv *priv;
  1309. struct resource *res;
  1310. int num_channels;
  1311. int error;
  1312. int i;
  1313. if (!pdata) {
  1314. dev_err(&pdev->dev, "no platform data defined\n");
  1315. return -EINVAL;
  1316. }
  1317. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1318. i = platform_get_irq(pdev, 0);
  1319. if (!res || i < 0) {
  1320. dev_err(&pdev->dev, "cannot get platform resources\n");
  1321. return -ENOENT;
  1322. }
  1323. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1324. if (!priv) {
  1325. dev_err(&pdev->dev, "cannot allocate device data\n");
  1326. return -ENOMEM;
  1327. }
  1328. platform_set_drvdata(pdev, priv);
  1329. error = request_irq(i, sh_mobile_lcdc_irq, 0,
  1330. dev_name(&pdev->dev), priv);
  1331. if (error) {
  1332. dev_err(&pdev->dev, "unable to request irq\n");
  1333. goto err1;
  1334. }
  1335. priv->irq = i;
  1336. atomic_set(&priv->hw_usecnt, -1);
  1337. for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  1338. struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
  1339. ch->lcdc = priv;
  1340. memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  1341. error = sh_mobile_lcdc_check_interface(ch);
  1342. if (error) {
  1343. dev_err(&pdev->dev, "unsupported interface type\n");
  1344. goto err1;
  1345. }
  1346. init_waitqueue_head(&ch->frame_end_wait);
  1347. init_completion(&ch->vsync_completion);
  1348. ch->pan_offset = 0;
  1349. /* probe the backlight is there is one defined */
  1350. if (ch->cfg.bl_info.max_brightness)
  1351. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  1352. switch (pdata->ch[i].chan) {
  1353. case LCDC_CHAN_MAINLCD:
  1354. ch->enabled = LDCNT2R_ME;
  1355. ch->reg_offs = lcdc_offs_mainlcd;
  1356. num_channels++;
  1357. break;
  1358. case LCDC_CHAN_SUBLCD:
  1359. ch->enabled = LDCNT2R_SE;
  1360. ch->reg_offs = lcdc_offs_sublcd;
  1361. num_channels++;
  1362. break;
  1363. }
  1364. }
  1365. if (!num_channels) {
  1366. dev_err(&pdev->dev, "no channels defined\n");
  1367. error = -EINVAL;
  1368. goto err1;
  1369. }
  1370. /* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
  1371. if (num_channels == 2)
  1372. priv->forced_bpp = pdata->ch[0].bpp;
  1373. priv->base = ioremap_nocache(res->start, resource_size(res));
  1374. if (!priv->base)
  1375. goto err1;
  1376. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  1377. if (error) {
  1378. dev_err(&pdev->dev, "unable to setup clocks\n");
  1379. goto err1;
  1380. }
  1381. priv->meram_dev = pdata->meram_dev;
  1382. for (i = 0; i < num_channels; i++) {
  1383. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1384. error = sh_mobile_lcdc_channel_init(ch, &pdev->dev);
  1385. if (error)
  1386. goto err1;
  1387. }
  1388. error = sh_mobile_lcdc_start(priv);
  1389. if (error) {
  1390. dev_err(&pdev->dev, "unable to start hardware\n");
  1391. goto err1;
  1392. }
  1393. for (i = 0; i < num_channels; i++) {
  1394. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1395. struct fb_info *info = ch->info;
  1396. if (info->fbdefio) {
  1397. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1398. info->fix.smem_len >> PAGE_SHIFT);
  1399. if (!ch->sglist) {
  1400. dev_err(&pdev->dev, "cannot allocate sglist\n");
  1401. goto err1;
  1402. }
  1403. }
  1404. info->bl_dev = ch->bl;
  1405. error = register_framebuffer(info);
  1406. if (error < 0)
  1407. goto err1;
  1408. dev_info(info->dev,
  1409. "registered %s/%s as %dx%d %dbpp.\n",
  1410. pdev->name,
  1411. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  1412. "mainlcd" : "sublcd",
  1413. info->var.xres, info->var.yres,
  1414. ch->cfg.bpp);
  1415. /* deferred io mode: disable clock to save power */
  1416. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1417. sh_mobile_lcdc_clk_off(priv);
  1418. }
  1419. /* Failure ignored */
  1420. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  1421. fb_register_client(&priv->notifier);
  1422. return 0;
  1423. err1:
  1424. sh_mobile_lcdc_remove(pdev);
  1425. return error;
  1426. }
  1427. static struct platform_driver sh_mobile_lcdc_driver = {
  1428. .driver = {
  1429. .name = "sh_mobile_lcdc_fb",
  1430. .owner = THIS_MODULE,
  1431. .pm = &sh_mobile_lcdc_dev_pm_ops,
  1432. },
  1433. .probe = sh_mobile_lcdc_probe,
  1434. .remove = sh_mobile_lcdc_remove,
  1435. };
  1436. static int __init sh_mobile_lcdc_init(void)
  1437. {
  1438. return platform_driver_register(&sh_mobile_lcdc_driver);
  1439. }
  1440. static void __exit sh_mobile_lcdc_exit(void)
  1441. {
  1442. platform_driver_unregister(&sh_mobile_lcdc_driver);
  1443. }
  1444. module_init(sh_mobile_lcdc_init);
  1445. module_exit(sh_mobile_lcdc_exit);
  1446. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  1447. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  1448. MODULE_LICENSE("GPL v2");