sh_mobile_lcdcfb.c 43 KB

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