sh_mobile_lcdcfb.c 41 KB

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