sh_mobile_lcdcfb.c 42 KB

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