pxafb.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  1. /*
  2. * linux/drivers/video/pxafb.c
  3. *
  4. * Copyright (C) 1999 Eric A. Thomas.
  5. * Copyright (C) 2004 Jean-Frederic Clere.
  6. * Copyright (C) 2004 Ian Campbell.
  7. * Copyright (C) 2004 Jeff Lackey.
  8. * Based on sa1100fb.c Copyright (C) 1999 Eric A. Thomas
  9. * which in turn is
  10. * Based on acornfb.c Copyright (C) Russell King.
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file COPYING in the main directory of this archive for
  14. * more details.
  15. *
  16. * Intel PXA250/210 LCD Controller Frame Buffer Driver
  17. *
  18. * Please direct your questions and comments on this driver to the following
  19. * email address:
  20. *
  21. * linux-arm-kernel@lists.arm.linux.org.uk
  22. *
  23. * Add support for overlay1 and overlay2 based on pxafb_overlay.c:
  24. *
  25. * Copyright (C) 2004, Intel Corporation
  26. *
  27. * 2003/08/27: <yu.tang@intel.com>
  28. * 2004/03/10: <stanley.cai@intel.com>
  29. * 2004/10/28: <yan.yin@intel.com>
  30. *
  31. * Copyright (C) 2006-2008 Marvell International Ltd.
  32. * All Rights Reserved
  33. */
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/kernel.h>
  37. #include <linux/sched.h>
  38. #include <linux/errno.h>
  39. #include <linux/string.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/slab.h>
  42. #include <linux/mm.h>
  43. #include <linux/fb.h>
  44. #include <linux/delay.h>
  45. #include <linux/init.h>
  46. #include <linux/ioport.h>
  47. #include <linux/cpufreq.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/clk.h>
  51. #include <linux/err.h>
  52. #include <linux/completion.h>
  53. #include <linux/mutex.h>
  54. #include <linux/kthread.h>
  55. #include <linux/freezer.h>
  56. #include <linux/console.h>
  57. #include <linux/of_graph.h>
  58. #include <linux/regulator/consumer.h>
  59. #include <video/of_display_timing.h>
  60. #include <video/videomode.h>
  61. #include <mach/hardware.h>
  62. #include <asm/io.h>
  63. #include <asm/irq.h>
  64. #include <asm/div64.h>
  65. #include <mach/bitfield.h>
  66. #include <linux/platform_data/video-pxafb.h>
  67. /*
  68. * Complain if VAR is out of range.
  69. */
  70. #define DEBUG_VAR 1
  71. #include "pxafb.h"
  72. /* Bits which should not be set in machine configuration structures */
  73. #define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
  74. LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
  75. LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
  76. #define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
  77. LCCR3_PCD | LCCR3_BPP(0xf))
  78. static int pxafb_activate_var(struct fb_var_screeninfo *var,
  79. struct pxafb_info *);
  80. static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
  81. static void setup_base_frame(struct pxafb_info *fbi,
  82. struct fb_var_screeninfo *var, int branch);
  83. static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
  84. unsigned long offset, size_t size);
  85. static unsigned long video_mem_size = 0;
  86. static inline unsigned long
  87. lcd_readl(struct pxafb_info *fbi, unsigned int off)
  88. {
  89. return __raw_readl(fbi->mmio_base + off);
  90. }
  91. static inline void
  92. lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
  93. {
  94. __raw_writel(val, fbi->mmio_base + off);
  95. }
  96. static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
  97. {
  98. unsigned long flags;
  99. local_irq_save(flags);
  100. /*
  101. * We need to handle two requests being made at the same time.
  102. * There are two important cases:
  103. * 1. When we are changing VT (C_REENABLE) while unblanking
  104. * (C_ENABLE) We must perform the unblanking, which will
  105. * do our REENABLE for us.
  106. * 2. When we are blanking, but immediately unblank before
  107. * we have blanked. We do the "REENABLE" thing here as
  108. * well, just to be sure.
  109. */
  110. if (fbi->task_state == C_ENABLE && state == C_REENABLE)
  111. state = (u_int) -1;
  112. if (fbi->task_state == C_DISABLE && state == C_ENABLE)
  113. state = C_REENABLE;
  114. if (state != (u_int)-1) {
  115. fbi->task_state = state;
  116. schedule_work(&fbi->task);
  117. }
  118. local_irq_restore(flags);
  119. }
  120. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  121. {
  122. chan &= 0xffff;
  123. chan >>= 16 - bf->length;
  124. return chan << bf->offset;
  125. }
  126. static int
  127. pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
  128. u_int trans, struct fb_info *info)
  129. {
  130. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  131. u_int val;
  132. if (regno >= fbi->palette_size)
  133. return 1;
  134. if (fbi->fb.var.grayscale) {
  135. fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
  136. return 0;
  137. }
  138. switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
  139. case LCCR4_PAL_FOR_0:
  140. val = ((red >> 0) & 0xf800);
  141. val |= ((green >> 5) & 0x07e0);
  142. val |= ((blue >> 11) & 0x001f);
  143. fbi->palette_cpu[regno] = val;
  144. break;
  145. case LCCR4_PAL_FOR_1:
  146. val = ((red << 8) & 0x00f80000);
  147. val |= ((green >> 0) & 0x0000fc00);
  148. val |= ((blue >> 8) & 0x000000f8);
  149. ((u32 *)(fbi->palette_cpu))[regno] = val;
  150. break;
  151. case LCCR4_PAL_FOR_2:
  152. val = ((red << 8) & 0x00fc0000);
  153. val |= ((green >> 0) & 0x0000fc00);
  154. val |= ((blue >> 8) & 0x000000fc);
  155. ((u32 *)(fbi->palette_cpu))[regno] = val;
  156. break;
  157. case LCCR4_PAL_FOR_3:
  158. val = ((red << 8) & 0x00ff0000);
  159. val |= ((green >> 0) & 0x0000ff00);
  160. val |= ((blue >> 8) & 0x000000ff);
  161. ((u32 *)(fbi->palette_cpu))[regno] = val;
  162. break;
  163. }
  164. return 0;
  165. }
  166. static int
  167. pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  168. u_int trans, struct fb_info *info)
  169. {
  170. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  171. unsigned int val;
  172. int ret = 1;
  173. /*
  174. * If inverse mode was selected, invert all the colours
  175. * rather than the register number. The register number
  176. * is what you poke into the framebuffer to produce the
  177. * colour you requested.
  178. */
  179. if (fbi->cmap_inverse) {
  180. red = 0xffff - red;
  181. green = 0xffff - green;
  182. blue = 0xffff - blue;
  183. }
  184. /*
  185. * If greyscale is true, then we convert the RGB value
  186. * to greyscale no matter what visual we are using.
  187. */
  188. if (fbi->fb.var.grayscale)
  189. red = green = blue = (19595 * red + 38470 * green +
  190. 7471 * blue) >> 16;
  191. switch (fbi->fb.fix.visual) {
  192. case FB_VISUAL_TRUECOLOR:
  193. /*
  194. * 16-bit True Colour. We encode the RGB value
  195. * according to the RGB bitfield information.
  196. */
  197. if (regno < 16) {
  198. u32 *pal = fbi->fb.pseudo_palette;
  199. val = chan_to_field(red, &fbi->fb.var.red);
  200. val |= chan_to_field(green, &fbi->fb.var.green);
  201. val |= chan_to_field(blue, &fbi->fb.var.blue);
  202. pal[regno] = val;
  203. ret = 0;
  204. }
  205. break;
  206. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  207. case FB_VISUAL_PSEUDOCOLOR:
  208. ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
  209. break;
  210. }
  211. return ret;
  212. }
  213. /* calculate pixel depth, transparency bit included, >=16bpp formats _only_ */
  214. static inline int var_to_depth(struct fb_var_screeninfo *var)
  215. {
  216. return var->red.length + var->green.length +
  217. var->blue.length + var->transp.length;
  218. }
  219. /* calculate 4-bit BPP value for LCCR3 and OVLxC1 */
  220. static int pxafb_var_to_bpp(struct fb_var_screeninfo *var)
  221. {
  222. int bpp = -EINVAL;
  223. switch (var->bits_per_pixel) {
  224. case 1: bpp = 0; break;
  225. case 2: bpp = 1; break;
  226. case 4: bpp = 2; break;
  227. case 8: bpp = 3; break;
  228. case 16: bpp = 4; break;
  229. case 24:
  230. switch (var_to_depth(var)) {
  231. case 18: bpp = 6; break; /* 18-bits/pixel packed */
  232. case 19: bpp = 8; break; /* 19-bits/pixel packed */
  233. case 24: bpp = 9; break;
  234. }
  235. break;
  236. case 32:
  237. switch (var_to_depth(var)) {
  238. case 18: bpp = 5; break; /* 18-bits/pixel unpacked */
  239. case 19: bpp = 7; break; /* 19-bits/pixel unpacked */
  240. case 25: bpp = 10; break;
  241. }
  242. break;
  243. }
  244. return bpp;
  245. }
  246. /*
  247. * pxafb_var_to_lccr3():
  248. * Convert a bits per pixel value to the correct bit pattern for LCCR3
  249. *
  250. * NOTE: for PXA27x with overlays support, the LCCR3_PDFOR_x bits have an
  251. * implication of the acutal use of transparency bit, which we handle it
  252. * here separatedly. See PXA27x Developer's Manual, Section <<7.4.6 Pixel
  253. * Formats>> for the valid combination of PDFOR, PAL_FOR for various BPP.
  254. *
  255. * Transparency for palette pixel formats is not supported at the moment.
  256. */
  257. static uint32_t pxafb_var_to_lccr3(struct fb_var_screeninfo *var)
  258. {
  259. int bpp = pxafb_var_to_bpp(var);
  260. uint32_t lccr3;
  261. if (bpp < 0)
  262. return 0;
  263. lccr3 = LCCR3_BPP(bpp);
  264. switch (var_to_depth(var)) {
  265. case 16: lccr3 |= var->transp.length ? LCCR3_PDFOR_3 : 0; break;
  266. case 18: lccr3 |= LCCR3_PDFOR_3; break;
  267. case 24: lccr3 |= var->transp.length ? LCCR3_PDFOR_2 : LCCR3_PDFOR_3;
  268. break;
  269. case 19:
  270. case 25: lccr3 |= LCCR3_PDFOR_0; break;
  271. }
  272. return lccr3;
  273. }
  274. #define SET_PIXFMT(v, r, g, b, t) \
  275. ({ \
  276. (v)->transp.offset = (t) ? (r) + (g) + (b) : 0; \
  277. (v)->transp.length = (t) ? (t) : 0; \
  278. (v)->blue.length = (b); (v)->blue.offset = 0; \
  279. (v)->green.length = (g); (v)->green.offset = (b); \
  280. (v)->red.length = (r); (v)->red.offset = (b) + (g); \
  281. })
  282. /* set the RGBT bitfields of fb_var_screeninf according to
  283. * var->bits_per_pixel and given depth
  284. */
  285. static void pxafb_set_pixfmt(struct fb_var_screeninfo *var, int depth)
  286. {
  287. if (depth == 0)
  288. depth = var->bits_per_pixel;
  289. if (var->bits_per_pixel < 16) {
  290. /* indexed pixel formats */
  291. var->red.offset = 0; var->red.length = 8;
  292. var->green.offset = 0; var->green.length = 8;
  293. var->blue.offset = 0; var->blue.length = 8;
  294. var->transp.offset = 0; var->transp.length = 8;
  295. }
  296. switch (depth) {
  297. case 16: var->transp.length ?
  298. SET_PIXFMT(var, 5, 5, 5, 1) : /* RGBT555 */
  299. SET_PIXFMT(var, 5, 6, 5, 0); break; /* RGB565 */
  300. case 18: SET_PIXFMT(var, 6, 6, 6, 0); break; /* RGB666 */
  301. case 19: SET_PIXFMT(var, 6, 6, 6, 1); break; /* RGBT666 */
  302. case 24: var->transp.length ?
  303. SET_PIXFMT(var, 8, 8, 7, 1) : /* RGBT887 */
  304. SET_PIXFMT(var, 8, 8, 8, 0); break; /* RGB888 */
  305. case 25: SET_PIXFMT(var, 8, 8, 8, 1); break; /* RGBT888 */
  306. }
  307. }
  308. #ifdef CONFIG_CPU_FREQ
  309. /*
  310. * pxafb_display_dma_period()
  311. * Calculate the minimum period (in picoseconds) between two DMA
  312. * requests for the LCD controller. If we hit this, it means we're
  313. * doing nothing but LCD DMA.
  314. */
  315. static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
  316. {
  317. /*
  318. * Period = pixclock * bits_per_byte * bytes_per_transfer
  319. * / memory_bits_per_pixel;
  320. */
  321. return var->pixclock * 8 * 16 / var->bits_per_pixel;
  322. }
  323. #endif
  324. /*
  325. * Select the smallest mode that allows the desired resolution to be
  326. * displayed. If desired parameters can be rounded up.
  327. */
  328. static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
  329. struct fb_var_screeninfo *var)
  330. {
  331. struct pxafb_mode_info *mode = NULL;
  332. struct pxafb_mode_info *modelist = mach->modes;
  333. unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
  334. unsigned int i;
  335. for (i = 0; i < mach->num_modes; i++) {
  336. if (modelist[i].xres >= var->xres &&
  337. modelist[i].yres >= var->yres &&
  338. modelist[i].xres < best_x &&
  339. modelist[i].yres < best_y &&
  340. modelist[i].bpp >= var->bits_per_pixel) {
  341. best_x = modelist[i].xres;
  342. best_y = modelist[i].yres;
  343. mode = &modelist[i];
  344. }
  345. }
  346. return mode;
  347. }
  348. static void pxafb_setmode(struct fb_var_screeninfo *var,
  349. struct pxafb_mode_info *mode)
  350. {
  351. var->xres = mode->xres;
  352. var->yres = mode->yres;
  353. var->bits_per_pixel = mode->bpp;
  354. var->pixclock = mode->pixclock;
  355. var->hsync_len = mode->hsync_len;
  356. var->left_margin = mode->left_margin;
  357. var->right_margin = mode->right_margin;
  358. var->vsync_len = mode->vsync_len;
  359. var->upper_margin = mode->upper_margin;
  360. var->lower_margin = mode->lower_margin;
  361. var->sync = mode->sync;
  362. var->grayscale = mode->cmap_greyscale;
  363. var->transp.length = mode->transparency;
  364. /* set the initial RGBA bitfields */
  365. pxafb_set_pixfmt(var, mode->depth);
  366. }
  367. static int pxafb_adjust_timing(struct pxafb_info *fbi,
  368. struct fb_var_screeninfo *var)
  369. {
  370. int line_length;
  371. var->xres = max_t(int, var->xres, MIN_XRES);
  372. var->yres = max_t(int, var->yres, MIN_YRES);
  373. if (!(fbi->lccr0 & LCCR0_LCDT)) {
  374. clamp_val(var->hsync_len, 1, 64);
  375. clamp_val(var->vsync_len, 1, 64);
  376. clamp_val(var->left_margin, 1, 255);
  377. clamp_val(var->right_margin, 1, 255);
  378. clamp_val(var->upper_margin, 1, 255);
  379. clamp_val(var->lower_margin, 1, 255);
  380. }
  381. /* make sure each line is aligned on word boundary */
  382. line_length = var->xres * var->bits_per_pixel / 8;
  383. line_length = ALIGN(line_length, 4);
  384. var->xres = line_length * 8 / var->bits_per_pixel;
  385. /* we don't support xpan, force xres_virtual to be equal to xres */
  386. var->xres_virtual = var->xres;
  387. if (var->accel_flags & FB_ACCELF_TEXT)
  388. var->yres_virtual = fbi->fb.fix.smem_len / line_length;
  389. else
  390. var->yres_virtual = max(var->yres_virtual, var->yres);
  391. /* check for limits */
  392. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  393. return -EINVAL;
  394. if (var->yres > var->yres_virtual)
  395. return -EINVAL;
  396. return 0;
  397. }
  398. /*
  399. * pxafb_check_var():
  400. * Get the video params out of 'var'. If a value doesn't fit, round it up,
  401. * if it's too big, return -EINVAL.
  402. *
  403. * Round up in the following order: bits_per_pixel, xres,
  404. * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
  405. * bitfields, horizontal timing, vertical timing.
  406. */
  407. static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  408. {
  409. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  410. struct pxafb_mach_info *inf = fbi->inf;
  411. int err;
  412. if (inf->fixed_modes) {
  413. struct pxafb_mode_info *mode;
  414. mode = pxafb_getmode(inf, var);
  415. if (!mode)
  416. return -EINVAL;
  417. pxafb_setmode(var, mode);
  418. }
  419. /* do a test conversion to BPP fields to check the color formats */
  420. err = pxafb_var_to_bpp(var);
  421. if (err < 0)
  422. return err;
  423. pxafb_set_pixfmt(var, var_to_depth(var));
  424. err = pxafb_adjust_timing(fbi, var);
  425. if (err)
  426. return err;
  427. #ifdef CONFIG_CPU_FREQ
  428. pr_debug("pxafb: dma period = %d ps\n",
  429. pxafb_display_dma_period(var));
  430. #endif
  431. return 0;
  432. }
  433. /*
  434. * pxafb_set_par():
  435. * Set the user defined part of the display for the specified console
  436. */
  437. static int pxafb_set_par(struct fb_info *info)
  438. {
  439. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  440. struct fb_var_screeninfo *var = &info->var;
  441. if (var->bits_per_pixel >= 16)
  442. fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  443. else if (!fbi->cmap_static)
  444. fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  445. else {
  446. /*
  447. * Some people have weird ideas about wanting static
  448. * pseudocolor maps. I suspect their user space
  449. * applications are broken.
  450. */
  451. fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
  452. }
  453. fbi->fb.fix.line_length = var->xres_virtual *
  454. var->bits_per_pixel / 8;
  455. if (var->bits_per_pixel >= 16)
  456. fbi->palette_size = 0;
  457. else
  458. fbi->palette_size = var->bits_per_pixel == 1 ?
  459. 4 : 1 << var->bits_per_pixel;
  460. fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
  461. if (fbi->fb.var.bits_per_pixel >= 16)
  462. fb_dealloc_cmap(&fbi->fb.cmap);
  463. else
  464. fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
  465. pxafb_activate_var(var, fbi);
  466. return 0;
  467. }
  468. static int pxafb_pan_display(struct fb_var_screeninfo *var,
  469. struct fb_info *info)
  470. {
  471. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  472. struct fb_var_screeninfo newvar;
  473. int dma = DMA_MAX + DMA_BASE;
  474. if (fbi->state != C_ENABLE)
  475. return 0;
  476. /* Only take .xoffset, .yoffset and .vmode & FB_VMODE_YWRAP from what
  477. * was passed in and copy the rest from the old screeninfo.
  478. */
  479. memcpy(&newvar, &fbi->fb.var, sizeof(newvar));
  480. newvar.xoffset = var->xoffset;
  481. newvar.yoffset = var->yoffset;
  482. newvar.vmode &= ~FB_VMODE_YWRAP;
  483. newvar.vmode |= var->vmode & FB_VMODE_YWRAP;
  484. setup_base_frame(fbi, &newvar, 1);
  485. if (fbi->lccr0 & LCCR0_SDS)
  486. lcd_writel(fbi, FBR1, fbi->fdadr[dma + 1] | 0x1);
  487. lcd_writel(fbi, FBR0, fbi->fdadr[dma] | 0x1);
  488. return 0;
  489. }
  490. /*
  491. * pxafb_blank():
  492. * Blank the display by setting all palette values to zero. Note, the
  493. * 16 bpp mode does not really use the palette, so this will not
  494. * blank the display in all modes.
  495. */
  496. static int pxafb_blank(int blank, struct fb_info *info)
  497. {
  498. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  499. int i;
  500. switch (blank) {
  501. case FB_BLANK_POWERDOWN:
  502. case FB_BLANK_VSYNC_SUSPEND:
  503. case FB_BLANK_HSYNC_SUSPEND:
  504. case FB_BLANK_NORMAL:
  505. if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  506. fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  507. for (i = 0; i < fbi->palette_size; i++)
  508. pxafb_setpalettereg(i, 0, 0, 0, 0, info);
  509. pxafb_schedule_work(fbi, C_DISABLE);
  510. /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
  511. break;
  512. case FB_BLANK_UNBLANK:
  513. /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
  514. if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  515. fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  516. fb_set_cmap(&fbi->fb.cmap, info);
  517. pxafb_schedule_work(fbi, C_ENABLE);
  518. }
  519. return 0;
  520. }
  521. static struct fb_ops pxafb_ops = {
  522. .owner = THIS_MODULE,
  523. .fb_check_var = pxafb_check_var,
  524. .fb_set_par = pxafb_set_par,
  525. .fb_pan_display = pxafb_pan_display,
  526. .fb_setcolreg = pxafb_setcolreg,
  527. .fb_fillrect = cfb_fillrect,
  528. .fb_copyarea = cfb_copyarea,
  529. .fb_imageblit = cfb_imageblit,
  530. .fb_blank = pxafb_blank,
  531. };
  532. #ifdef CONFIG_FB_PXA_OVERLAY
  533. static void overlay1fb_setup(struct pxafb_layer *ofb)
  534. {
  535. int size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
  536. unsigned long start = ofb->video_mem_phys;
  537. setup_frame_dma(ofb->fbi, DMA_OV1, PAL_NONE, start, size);
  538. }
  539. /* Depending on the enable status of overlay1/2, the DMA should be
  540. * updated from FDADRx (when disabled) or FBRx (when enabled).
  541. */
  542. static void overlay1fb_enable(struct pxafb_layer *ofb)
  543. {
  544. int enabled = lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN;
  545. uint32_t fdadr1 = ofb->fbi->fdadr[DMA_OV1] | (enabled ? 0x1 : 0);
  546. lcd_writel(ofb->fbi, enabled ? FBR1 : FDADR1, fdadr1);
  547. lcd_writel(ofb->fbi, OVL1C2, ofb->control[1]);
  548. lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] | OVLxC1_OEN);
  549. }
  550. static void overlay1fb_disable(struct pxafb_layer *ofb)
  551. {
  552. uint32_t lccr5;
  553. if (!(lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN))
  554. return;
  555. lccr5 = lcd_readl(ofb->fbi, LCCR5);
  556. lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] & ~OVLxC1_OEN);
  557. lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(1));
  558. lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(1));
  559. lcd_writel(ofb->fbi, FBR1, ofb->fbi->fdadr[DMA_OV1] | 0x3);
  560. if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
  561. pr_warn("%s: timeout disabling overlay1\n", __func__);
  562. lcd_writel(ofb->fbi, LCCR5, lccr5);
  563. }
  564. static void overlay2fb_setup(struct pxafb_layer *ofb)
  565. {
  566. int size, div = 1, pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
  567. unsigned long start[3] = { ofb->video_mem_phys, 0, 0 };
  568. if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED) {
  569. size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
  570. setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
  571. } else {
  572. size = ofb->fb.var.xres_virtual * ofb->fb.var.yres_virtual;
  573. switch (pfor) {
  574. case OVERLAY_FORMAT_YUV444_PLANAR: div = 1; break;
  575. case OVERLAY_FORMAT_YUV422_PLANAR: div = 2; break;
  576. case OVERLAY_FORMAT_YUV420_PLANAR: div = 4; break;
  577. }
  578. start[1] = start[0] + size;
  579. start[2] = start[1] + size / div;
  580. setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
  581. setup_frame_dma(ofb->fbi, DMA_OV2_Cb, -1, start[1], size / div);
  582. setup_frame_dma(ofb->fbi, DMA_OV2_Cr, -1, start[2], size / div);
  583. }
  584. }
  585. static void overlay2fb_enable(struct pxafb_layer *ofb)
  586. {
  587. int pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
  588. int enabled = lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN;
  589. uint32_t fdadr2 = ofb->fbi->fdadr[DMA_OV2_Y] | (enabled ? 0x1 : 0);
  590. uint32_t fdadr3 = ofb->fbi->fdadr[DMA_OV2_Cb] | (enabled ? 0x1 : 0);
  591. uint32_t fdadr4 = ofb->fbi->fdadr[DMA_OV2_Cr] | (enabled ? 0x1 : 0);
  592. if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED)
  593. lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
  594. else {
  595. lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
  596. lcd_writel(ofb->fbi, enabled ? FBR3 : FDADR3, fdadr3);
  597. lcd_writel(ofb->fbi, enabled ? FBR4 : FDADR4, fdadr4);
  598. }
  599. lcd_writel(ofb->fbi, OVL2C2, ofb->control[1]);
  600. lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] | OVLxC1_OEN);
  601. }
  602. static void overlay2fb_disable(struct pxafb_layer *ofb)
  603. {
  604. uint32_t lccr5;
  605. if (!(lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN))
  606. return;
  607. lccr5 = lcd_readl(ofb->fbi, LCCR5);
  608. lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] & ~OVLxC1_OEN);
  609. lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(2));
  610. lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(2));
  611. lcd_writel(ofb->fbi, FBR2, ofb->fbi->fdadr[DMA_OV2_Y] | 0x3);
  612. lcd_writel(ofb->fbi, FBR3, ofb->fbi->fdadr[DMA_OV2_Cb] | 0x3);
  613. lcd_writel(ofb->fbi, FBR4, ofb->fbi->fdadr[DMA_OV2_Cr] | 0x3);
  614. if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
  615. pr_warn("%s: timeout disabling overlay2\n", __func__);
  616. }
  617. static struct pxafb_layer_ops ofb_ops[] = {
  618. [0] = {
  619. .enable = overlay1fb_enable,
  620. .disable = overlay1fb_disable,
  621. .setup = overlay1fb_setup,
  622. },
  623. [1] = {
  624. .enable = overlay2fb_enable,
  625. .disable = overlay2fb_disable,
  626. .setup = overlay2fb_setup,
  627. },
  628. };
  629. static int overlayfb_open(struct fb_info *info, int user)
  630. {
  631. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  632. /* no support for framebuffer console on overlay */
  633. if (user == 0)
  634. return -ENODEV;
  635. if (ofb->usage++ == 0) {
  636. /* unblank the base framebuffer */
  637. console_lock();
  638. fb_blank(&ofb->fbi->fb, FB_BLANK_UNBLANK);
  639. console_unlock();
  640. }
  641. return 0;
  642. }
  643. static int overlayfb_release(struct fb_info *info, int user)
  644. {
  645. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  646. if (ofb->usage == 1) {
  647. ofb->ops->disable(ofb);
  648. ofb->fb.var.height = -1;
  649. ofb->fb.var.width = -1;
  650. ofb->fb.var.xres = ofb->fb.var.xres_virtual = 0;
  651. ofb->fb.var.yres = ofb->fb.var.yres_virtual = 0;
  652. ofb->usage--;
  653. }
  654. return 0;
  655. }
  656. static int overlayfb_check_var(struct fb_var_screeninfo *var,
  657. struct fb_info *info)
  658. {
  659. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  660. struct fb_var_screeninfo *base_var = &ofb->fbi->fb.var;
  661. int xpos, ypos, pfor, bpp;
  662. xpos = NONSTD_TO_XPOS(var->nonstd);
  663. ypos = NONSTD_TO_YPOS(var->nonstd);
  664. pfor = NONSTD_TO_PFOR(var->nonstd);
  665. bpp = pxafb_var_to_bpp(var);
  666. if (bpp < 0)
  667. return -EINVAL;
  668. /* no support for YUV format on overlay1 */
  669. if (ofb->id == OVERLAY1 && pfor != 0)
  670. return -EINVAL;
  671. /* for YUV packed formats, bpp = 'minimum bpp of YUV components' */
  672. switch (pfor) {
  673. case OVERLAY_FORMAT_RGB:
  674. bpp = pxafb_var_to_bpp(var);
  675. if (bpp < 0)
  676. return -EINVAL;
  677. pxafb_set_pixfmt(var, var_to_depth(var));
  678. break;
  679. case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
  680. case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 8; break;
  681. case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 4; break;
  682. case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 2; break;
  683. default:
  684. return -EINVAL;
  685. }
  686. /* each line must start at a 32-bit word boundary */
  687. if ((xpos * bpp) % 32)
  688. return -EINVAL;
  689. /* xres must align on 32-bit word boundary */
  690. var->xres = roundup(var->xres * bpp, 32) / bpp;
  691. if ((xpos + var->xres > base_var->xres) ||
  692. (ypos + var->yres > base_var->yres))
  693. return -EINVAL;
  694. var->xres_virtual = var->xres;
  695. var->yres_virtual = max(var->yres, var->yres_virtual);
  696. return 0;
  697. }
  698. static int overlayfb_check_video_memory(struct pxafb_layer *ofb)
  699. {
  700. struct fb_var_screeninfo *var = &ofb->fb.var;
  701. int pfor = NONSTD_TO_PFOR(var->nonstd);
  702. int size, bpp = 0;
  703. switch (pfor) {
  704. case OVERLAY_FORMAT_RGB: bpp = var->bits_per_pixel; break;
  705. case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
  706. case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 24; break;
  707. case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 16; break;
  708. case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 12; break;
  709. }
  710. ofb->fb.fix.line_length = var->xres_virtual * bpp / 8;
  711. size = PAGE_ALIGN(ofb->fb.fix.line_length * var->yres_virtual);
  712. if (ofb->video_mem) {
  713. if (ofb->video_mem_size >= size)
  714. return 0;
  715. }
  716. return -EINVAL;
  717. }
  718. static int overlayfb_set_par(struct fb_info *info)
  719. {
  720. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  721. struct fb_var_screeninfo *var = &info->var;
  722. int xpos, ypos, pfor, bpp, ret;
  723. ret = overlayfb_check_video_memory(ofb);
  724. if (ret)
  725. return ret;
  726. bpp = pxafb_var_to_bpp(var);
  727. xpos = NONSTD_TO_XPOS(var->nonstd);
  728. ypos = NONSTD_TO_YPOS(var->nonstd);
  729. pfor = NONSTD_TO_PFOR(var->nonstd);
  730. ofb->control[0] = OVLxC1_PPL(var->xres) | OVLxC1_LPO(var->yres) |
  731. OVLxC1_BPP(bpp);
  732. ofb->control[1] = OVLxC2_XPOS(xpos) | OVLxC2_YPOS(ypos);
  733. if (ofb->id == OVERLAY2)
  734. ofb->control[1] |= OVL2C2_PFOR(pfor);
  735. ofb->ops->setup(ofb);
  736. ofb->ops->enable(ofb);
  737. return 0;
  738. }
  739. static struct fb_ops overlay_fb_ops = {
  740. .owner = THIS_MODULE,
  741. .fb_open = overlayfb_open,
  742. .fb_release = overlayfb_release,
  743. .fb_check_var = overlayfb_check_var,
  744. .fb_set_par = overlayfb_set_par,
  745. };
  746. static void init_pxafb_overlay(struct pxafb_info *fbi, struct pxafb_layer *ofb,
  747. int id)
  748. {
  749. sprintf(ofb->fb.fix.id, "overlay%d", id + 1);
  750. ofb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  751. ofb->fb.fix.xpanstep = 0;
  752. ofb->fb.fix.ypanstep = 1;
  753. ofb->fb.var.activate = FB_ACTIVATE_NOW;
  754. ofb->fb.var.height = -1;
  755. ofb->fb.var.width = -1;
  756. ofb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  757. ofb->fb.fbops = &overlay_fb_ops;
  758. ofb->fb.flags = FBINFO_FLAG_DEFAULT;
  759. ofb->fb.node = -1;
  760. ofb->fb.pseudo_palette = NULL;
  761. ofb->id = id;
  762. ofb->ops = &ofb_ops[id];
  763. ofb->usage = 0;
  764. ofb->fbi = fbi;
  765. init_completion(&ofb->branch_done);
  766. }
  767. static inline int pxafb_overlay_supported(void)
  768. {
  769. if (cpu_is_pxa27x() || cpu_is_pxa3xx())
  770. return 1;
  771. return 0;
  772. }
  773. static int pxafb_overlay_map_video_memory(struct pxafb_info *pxafb,
  774. struct pxafb_layer *ofb)
  775. {
  776. /* We assume that user will use at most video_mem_size for overlay fb,
  777. * anyway, it's useless to use 16bpp main plane and 24bpp overlay
  778. */
  779. ofb->video_mem = alloc_pages_exact(PAGE_ALIGN(pxafb->video_mem_size),
  780. GFP_KERNEL | __GFP_ZERO);
  781. if (ofb->video_mem == NULL)
  782. return -ENOMEM;
  783. ofb->video_mem_phys = virt_to_phys(ofb->video_mem);
  784. ofb->video_mem_size = PAGE_ALIGN(pxafb->video_mem_size);
  785. mutex_lock(&ofb->fb.mm_lock);
  786. ofb->fb.fix.smem_start = ofb->video_mem_phys;
  787. ofb->fb.fix.smem_len = pxafb->video_mem_size;
  788. mutex_unlock(&ofb->fb.mm_lock);
  789. ofb->fb.screen_base = ofb->video_mem;
  790. return 0;
  791. }
  792. static void pxafb_overlay_init(struct pxafb_info *fbi)
  793. {
  794. int i, ret;
  795. if (!pxafb_overlay_supported())
  796. return;
  797. for (i = 0; i < 2; i++) {
  798. struct pxafb_layer *ofb = &fbi->overlay[i];
  799. init_pxafb_overlay(fbi, ofb, i);
  800. ret = register_framebuffer(&ofb->fb);
  801. if (ret) {
  802. dev_err(fbi->dev, "failed to register overlay %d\n", i);
  803. continue;
  804. }
  805. ret = pxafb_overlay_map_video_memory(fbi, ofb);
  806. if (ret) {
  807. dev_err(fbi->dev,
  808. "failed to map video memory for overlay %d\n",
  809. i);
  810. unregister_framebuffer(&ofb->fb);
  811. continue;
  812. }
  813. ofb->registered = 1;
  814. }
  815. /* mask all IU/BS/EOF/SOF interrupts */
  816. lcd_writel(fbi, LCCR5, ~0);
  817. pr_info("PXA Overlay driver loaded successfully!\n");
  818. }
  819. static void pxafb_overlay_exit(struct pxafb_info *fbi)
  820. {
  821. int i;
  822. if (!pxafb_overlay_supported())
  823. return;
  824. for (i = 0; i < 2; i++) {
  825. struct pxafb_layer *ofb = &fbi->overlay[i];
  826. if (ofb->registered) {
  827. if (ofb->video_mem)
  828. free_pages_exact(ofb->video_mem,
  829. ofb->video_mem_size);
  830. unregister_framebuffer(&ofb->fb);
  831. }
  832. }
  833. }
  834. #else
  835. static inline void pxafb_overlay_init(struct pxafb_info *fbi) {}
  836. static inline void pxafb_overlay_exit(struct pxafb_info *fbi) {}
  837. #endif /* CONFIG_FB_PXA_OVERLAY */
  838. /*
  839. * Calculate the PCD value from the clock rate (in picoseconds).
  840. * We take account of the PPCR clock setting.
  841. * From PXA Developer's Manual:
  842. *
  843. * PixelClock = LCLK
  844. * -------------
  845. * 2 ( PCD + 1 )
  846. *
  847. * PCD = LCLK
  848. * ------------- - 1
  849. * 2(PixelClock)
  850. *
  851. * Where:
  852. * LCLK = LCD/Memory Clock
  853. * PCD = LCCR3[7:0]
  854. *
  855. * PixelClock here is in Hz while the pixclock argument given is the
  856. * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
  857. *
  858. * The function get_lclk_frequency_10khz returns LCLK in units of
  859. * 10khz. Calling the result of this function lclk gives us the
  860. * following
  861. *
  862. * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
  863. * -------------------------------------- - 1
  864. * 2
  865. *
  866. * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
  867. */
  868. static inline unsigned int get_pcd(struct pxafb_info *fbi,
  869. unsigned int pixclock)
  870. {
  871. unsigned long long pcd;
  872. /* FIXME: Need to take into account Double Pixel Clock mode
  873. * (DPC) bit? or perhaps set it based on the various clock
  874. * speeds */
  875. pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
  876. pcd *= pixclock;
  877. do_div(pcd, 100000000 * 2);
  878. /* no need for this, since we should subtract 1 anyway. they cancel */
  879. /* pcd += 1; */ /* make up for integer math truncations */
  880. return (unsigned int)pcd;
  881. }
  882. /*
  883. * Some touchscreens need hsync information from the video driver to
  884. * function correctly. We export it here. Note that 'hsync_time' and
  885. * the value returned from pxafb_get_hsync_time() is the *reciprocal*
  886. * of the hsync period in seconds.
  887. */
  888. static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
  889. {
  890. unsigned long htime;
  891. if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
  892. fbi->hsync_time = 0;
  893. return;
  894. }
  895. htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
  896. fbi->hsync_time = htime;
  897. }
  898. unsigned long pxafb_get_hsync_time(struct device *dev)
  899. {
  900. struct pxafb_info *fbi = dev_get_drvdata(dev);
  901. /* If display is blanked/suspended, hsync isn't active */
  902. if (!fbi || (fbi->state != C_ENABLE))
  903. return 0;
  904. return fbi->hsync_time;
  905. }
  906. EXPORT_SYMBOL(pxafb_get_hsync_time);
  907. static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
  908. unsigned long start, size_t size)
  909. {
  910. struct pxafb_dma_descriptor *dma_desc, *pal_desc;
  911. unsigned int dma_desc_off, pal_desc_off;
  912. if (dma < 0 || dma >= DMA_MAX * 2)
  913. return -EINVAL;
  914. dma_desc = &fbi->dma_buff->dma_desc[dma];
  915. dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
  916. dma_desc->fsadr = start;
  917. dma_desc->fidr = 0;
  918. dma_desc->ldcmd = size;
  919. if (pal < 0 || pal >= PAL_MAX * 2) {
  920. dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  921. fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
  922. } else {
  923. pal_desc = &fbi->dma_buff->pal_desc[pal];
  924. pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
  925. pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
  926. pal_desc->fidr = 0;
  927. if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
  928. pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
  929. else
  930. pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
  931. pal_desc->ldcmd |= LDCMD_PAL;
  932. /* flip back and forth between palette and frame buffer */
  933. pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  934. dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
  935. fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
  936. }
  937. return 0;
  938. }
  939. static void setup_base_frame(struct pxafb_info *fbi,
  940. struct fb_var_screeninfo *var,
  941. int branch)
  942. {
  943. struct fb_fix_screeninfo *fix = &fbi->fb.fix;
  944. int nbytes, dma, pal, bpp = var->bits_per_pixel;
  945. unsigned long offset;
  946. dma = DMA_BASE + (branch ? DMA_MAX : 0);
  947. pal = (bpp >= 16) ? PAL_NONE : PAL_BASE + (branch ? PAL_MAX : 0);
  948. nbytes = fix->line_length * var->yres;
  949. offset = fix->line_length * var->yoffset + fbi->video_mem_phys;
  950. if (fbi->lccr0 & LCCR0_SDS) {
  951. nbytes = nbytes / 2;
  952. setup_frame_dma(fbi, dma + 1, PAL_NONE, offset + nbytes, nbytes);
  953. }
  954. setup_frame_dma(fbi, dma, pal, offset, nbytes);
  955. }
  956. #ifdef CONFIG_FB_PXA_SMARTPANEL
  957. static int setup_smart_dma(struct pxafb_info *fbi)
  958. {
  959. struct pxafb_dma_descriptor *dma_desc;
  960. unsigned long dma_desc_off, cmd_buff_off;
  961. dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
  962. dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
  963. cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
  964. dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  965. dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
  966. dma_desc->fidr = 0;
  967. dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
  968. fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
  969. return 0;
  970. }
  971. int pxafb_smart_flush(struct fb_info *info)
  972. {
  973. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  974. uint32_t prsr;
  975. int ret = 0;
  976. /* disable controller until all registers are set up */
  977. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  978. /* 1. make it an even number of commands to align on 32-bit boundary
  979. * 2. add the interrupt command to the end of the chain so we can
  980. * keep track of the end of the transfer
  981. */
  982. while (fbi->n_smart_cmds & 1)
  983. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
  984. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
  985. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
  986. setup_smart_dma(fbi);
  987. /* continue to execute next command */
  988. prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
  989. lcd_writel(fbi, PRSR, prsr);
  990. /* stop the processor in case it executed "wait for sync" cmd */
  991. lcd_writel(fbi, CMDCR, 0x0001);
  992. /* don't send interrupts for fifo underruns on channel 6 */
  993. lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
  994. lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
  995. lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
  996. lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
  997. lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
  998. lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
  999. lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
  1000. /* begin sending */
  1001. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
  1002. if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
  1003. pr_warn("%s: timeout waiting for command done\n", __func__);
  1004. ret = -ETIMEDOUT;
  1005. }
  1006. /* quick disable */
  1007. prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
  1008. lcd_writel(fbi, PRSR, prsr);
  1009. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  1010. lcd_writel(fbi, FDADR6, 0);
  1011. fbi->n_smart_cmds = 0;
  1012. return ret;
  1013. }
  1014. int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
  1015. {
  1016. int i;
  1017. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  1018. for (i = 0; i < n_cmds; i++, cmds++) {
  1019. /* if it is a software delay, flush and delay */
  1020. if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
  1021. pxafb_smart_flush(info);
  1022. mdelay(*cmds & 0xff);
  1023. continue;
  1024. }
  1025. /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
  1026. if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
  1027. pxafb_smart_flush(info);
  1028. fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
  1029. }
  1030. return 0;
  1031. }
  1032. static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
  1033. {
  1034. unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
  1035. return (t == 0) ? 1 : t;
  1036. }
  1037. static void setup_smart_timing(struct pxafb_info *fbi,
  1038. struct fb_var_screeninfo *var)
  1039. {
  1040. struct pxafb_mach_info *inf = fbi->inf;
  1041. struct pxafb_mode_info *mode = &inf->modes[0];
  1042. unsigned long lclk = clk_get_rate(fbi->clk);
  1043. unsigned t1, t2, t3, t4;
  1044. t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
  1045. t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
  1046. t3 = mode->op_hold_time;
  1047. t4 = mode->cmd_inh_time;
  1048. fbi->reg_lccr1 =
  1049. LCCR1_DisWdth(var->xres) |
  1050. LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
  1051. LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
  1052. LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
  1053. fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
  1054. fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
  1055. fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
  1056. fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
  1057. /* FIXME: make this configurable */
  1058. fbi->reg_cmdcr = 1;
  1059. }
  1060. static int pxafb_smart_thread(void *arg)
  1061. {
  1062. struct pxafb_info *fbi = arg;
  1063. struct pxafb_mach_info *inf = fbi->inf;
  1064. if (!inf->smart_update) {
  1065. pr_err("%s: not properly initialized, thread terminated\n",
  1066. __func__);
  1067. return -EINVAL;
  1068. }
  1069. pr_debug("%s(): task starting\n", __func__);
  1070. set_freezable();
  1071. while (!kthread_should_stop()) {
  1072. if (try_to_freeze())
  1073. continue;
  1074. mutex_lock(&fbi->ctrlr_lock);
  1075. if (fbi->state == C_ENABLE) {
  1076. inf->smart_update(&fbi->fb);
  1077. complete(&fbi->refresh_done);
  1078. }
  1079. mutex_unlock(&fbi->ctrlr_lock);
  1080. set_current_state(TASK_INTERRUPTIBLE);
  1081. schedule_timeout(msecs_to_jiffies(30));
  1082. }
  1083. pr_debug("%s(): task ending\n", __func__);
  1084. return 0;
  1085. }
  1086. static int pxafb_smart_init(struct pxafb_info *fbi)
  1087. {
  1088. if (!(fbi->lccr0 & LCCR0_LCDT))
  1089. return 0;
  1090. fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
  1091. fbi->n_smart_cmds = 0;
  1092. init_completion(&fbi->command_done);
  1093. init_completion(&fbi->refresh_done);
  1094. fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
  1095. "lcd_refresh");
  1096. if (IS_ERR(fbi->smart_thread)) {
  1097. pr_err("%s: unable to create kernel thread\n", __func__);
  1098. return PTR_ERR(fbi->smart_thread);
  1099. }
  1100. return 0;
  1101. }
  1102. #else
  1103. static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
  1104. #endif /* CONFIG_FB_PXA_SMARTPANEL */
  1105. static void setup_parallel_timing(struct pxafb_info *fbi,
  1106. struct fb_var_screeninfo *var)
  1107. {
  1108. unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
  1109. fbi->reg_lccr1 =
  1110. LCCR1_DisWdth(var->xres) +
  1111. LCCR1_HorSnchWdth(var->hsync_len) +
  1112. LCCR1_BegLnDel(var->left_margin) +
  1113. LCCR1_EndLnDel(var->right_margin);
  1114. /*
  1115. * If we have a dual scan LCD, we need to halve
  1116. * the YRES parameter.
  1117. */
  1118. lines_per_panel = var->yres;
  1119. if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
  1120. lines_per_panel /= 2;
  1121. fbi->reg_lccr2 =
  1122. LCCR2_DisHght(lines_per_panel) +
  1123. LCCR2_VrtSnchWdth(var->vsync_len) +
  1124. LCCR2_BegFrmDel(var->upper_margin) +
  1125. LCCR2_EndFrmDel(var->lower_margin);
  1126. fbi->reg_lccr3 = fbi->lccr3 |
  1127. (var->sync & FB_SYNC_HOR_HIGH_ACT ?
  1128. LCCR3_HorSnchH : LCCR3_HorSnchL) |
  1129. (var->sync & FB_SYNC_VERT_HIGH_ACT ?
  1130. LCCR3_VrtSnchH : LCCR3_VrtSnchL);
  1131. if (pcd) {
  1132. fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
  1133. set_hsync_time(fbi, pcd);
  1134. }
  1135. }
  1136. /*
  1137. * pxafb_activate_var():
  1138. * Configures LCD Controller based on entries in var parameter.
  1139. * Settings are only written to the controller if changes were made.
  1140. */
  1141. static int pxafb_activate_var(struct fb_var_screeninfo *var,
  1142. struct pxafb_info *fbi)
  1143. {
  1144. u_long flags;
  1145. /* Update shadow copy atomically */
  1146. local_irq_save(flags);
  1147. #ifdef CONFIG_FB_PXA_SMARTPANEL
  1148. if (fbi->lccr0 & LCCR0_LCDT)
  1149. setup_smart_timing(fbi, var);
  1150. else
  1151. #endif
  1152. setup_parallel_timing(fbi, var);
  1153. setup_base_frame(fbi, var, 0);
  1154. fbi->reg_lccr0 = fbi->lccr0 |
  1155. (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
  1156. LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
  1157. fbi->reg_lccr3 |= pxafb_var_to_lccr3(var);
  1158. fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
  1159. fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
  1160. local_irq_restore(flags);
  1161. /*
  1162. * Only update the registers if the controller is enabled
  1163. * and something has changed.
  1164. */
  1165. if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
  1166. (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
  1167. (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
  1168. (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
  1169. (lcd_readl(fbi, LCCR4) != fbi->reg_lccr4) ||
  1170. (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
  1171. ((fbi->lccr0 & LCCR0_SDS) &&
  1172. (lcd_readl(fbi, FDADR1) != fbi->fdadr[1])))
  1173. pxafb_schedule_work(fbi, C_REENABLE);
  1174. return 0;
  1175. }
  1176. /*
  1177. * NOTE! The following functions are purely helpers for set_ctrlr_state.
  1178. * Do not call them directly; set_ctrlr_state does the correct serialisation
  1179. * to ensure that things happen in the right way 100% of time time.
  1180. * -- rmk
  1181. */
  1182. static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
  1183. {
  1184. pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
  1185. if (fbi->backlight_power)
  1186. fbi->backlight_power(on);
  1187. }
  1188. static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
  1189. {
  1190. pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
  1191. if (fbi->lcd_power)
  1192. fbi->lcd_power(on, &fbi->fb.var);
  1193. if (fbi->lcd_supply && fbi->lcd_supply_enabled != on) {
  1194. int ret;
  1195. if (on)
  1196. ret = regulator_enable(fbi->lcd_supply);
  1197. else
  1198. ret = regulator_disable(fbi->lcd_supply);
  1199. if (ret < 0)
  1200. pr_warn("Unable to %s LCD supply regulator: %d\n",
  1201. on ? "enable" : "disable", ret);
  1202. else
  1203. fbi->lcd_supply_enabled = on;
  1204. }
  1205. }
  1206. static void pxafb_enable_controller(struct pxafb_info *fbi)
  1207. {
  1208. pr_debug("pxafb: Enabling LCD controller\n");
  1209. pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
  1210. pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
  1211. pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
  1212. pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
  1213. pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
  1214. pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
  1215. /* enable LCD controller clock */
  1216. if (clk_prepare_enable(fbi->clk)) {
  1217. pr_err("%s: Failed to prepare clock\n", __func__);
  1218. return;
  1219. }
  1220. if (fbi->lccr0 & LCCR0_LCDT)
  1221. return;
  1222. /* Sequence from 11.7.10 */
  1223. lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
  1224. lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
  1225. lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
  1226. lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
  1227. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  1228. lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
  1229. if (fbi->lccr0 & LCCR0_SDS)
  1230. lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
  1231. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
  1232. }
  1233. static void pxafb_disable_controller(struct pxafb_info *fbi)
  1234. {
  1235. uint32_t lccr0;
  1236. #ifdef CONFIG_FB_PXA_SMARTPANEL
  1237. if (fbi->lccr0 & LCCR0_LCDT) {
  1238. wait_for_completion_timeout(&fbi->refresh_done,
  1239. msecs_to_jiffies(200));
  1240. return;
  1241. }
  1242. #endif
  1243. /* Clear LCD Status Register */
  1244. lcd_writel(fbi, LCSR, 0xffffffff);
  1245. lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
  1246. lcd_writel(fbi, LCCR0, lccr0);
  1247. lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
  1248. wait_for_completion_timeout(&fbi->disable_done, msecs_to_jiffies(200));
  1249. /* disable LCD controller clock */
  1250. clk_disable_unprepare(fbi->clk);
  1251. }
  1252. /*
  1253. * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
  1254. */
  1255. static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
  1256. {
  1257. struct pxafb_info *fbi = dev_id;
  1258. unsigned int lccr0, lcsr;
  1259. lcsr = lcd_readl(fbi, LCSR);
  1260. if (lcsr & LCSR_LDD) {
  1261. lccr0 = lcd_readl(fbi, LCCR0);
  1262. lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
  1263. complete(&fbi->disable_done);
  1264. }
  1265. #ifdef CONFIG_FB_PXA_SMARTPANEL
  1266. if (lcsr & LCSR_CMD_INT)
  1267. complete(&fbi->command_done);
  1268. #endif
  1269. lcd_writel(fbi, LCSR, lcsr);
  1270. #ifdef CONFIG_FB_PXA_OVERLAY
  1271. {
  1272. unsigned int lcsr1 = lcd_readl(fbi, LCSR1);
  1273. if (lcsr1 & LCSR1_BS(1))
  1274. complete(&fbi->overlay[0].branch_done);
  1275. if (lcsr1 & LCSR1_BS(2))
  1276. complete(&fbi->overlay[1].branch_done);
  1277. lcd_writel(fbi, LCSR1, lcsr1);
  1278. }
  1279. #endif
  1280. return IRQ_HANDLED;
  1281. }
  1282. /*
  1283. * This function must be called from task context only, since it will
  1284. * sleep when disabling the LCD controller, or if we get two contending
  1285. * processes trying to alter state.
  1286. */
  1287. static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
  1288. {
  1289. u_int old_state;
  1290. mutex_lock(&fbi->ctrlr_lock);
  1291. old_state = fbi->state;
  1292. /*
  1293. * Hack around fbcon initialisation.
  1294. */
  1295. if (old_state == C_STARTUP && state == C_REENABLE)
  1296. state = C_ENABLE;
  1297. switch (state) {
  1298. case C_DISABLE_CLKCHANGE:
  1299. /*
  1300. * Disable controller for clock change. If the
  1301. * controller is already disabled, then do nothing.
  1302. */
  1303. if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
  1304. fbi->state = state;
  1305. /* TODO __pxafb_lcd_power(fbi, 0); */
  1306. pxafb_disable_controller(fbi);
  1307. }
  1308. break;
  1309. case C_DISABLE_PM:
  1310. case C_DISABLE:
  1311. /*
  1312. * Disable controller
  1313. */
  1314. if (old_state != C_DISABLE) {
  1315. fbi->state = state;
  1316. __pxafb_backlight_power(fbi, 0);
  1317. __pxafb_lcd_power(fbi, 0);
  1318. if (old_state != C_DISABLE_CLKCHANGE)
  1319. pxafb_disable_controller(fbi);
  1320. }
  1321. break;
  1322. case C_ENABLE_CLKCHANGE:
  1323. /*
  1324. * Enable the controller after clock change. Only
  1325. * do this if we were disabled for the clock change.
  1326. */
  1327. if (old_state == C_DISABLE_CLKCHANGE) {
  1328. fbi->state = C_ENABLE;
  1329. pxafb_enable_controller(fbi);
  1330. /* TODO __pxafb_lcd_power(fbi, 1); */
  1331. }
  1332. break;
  1333. case C_REENABLE:
  1334. /*
  1335. * Re-enable the controller only if it was already
  1336. * enabled. This is so we reprogram the control
  1337. * registers.
  1338. */
  1339. if (old_state == C_ENABLE) {
  1340. __pxafb_lcd_power(fbi, 0);
  1341. pxafb_disable_controller(fbi);
  1342. pxafb_enable_controller(fbi);
  1343. __pxafb_lcd_power(fbi, 1);
  1344. }
  1345. break;
  1346. case C_ENABLE_PM:
  1347. /*
  1348. * Re-enable the controller after PM. This is not
  1349. * perfect - think about the case where we were doing
  1350. * a clock change, and we suspended half-way through.
  1351. */
  1352. if (old_state != C_DISABLE_PM)
  1353. break;
  1354. /* fall through */
  1355. case C_ENABLE:
  1356. /*
  1357. * Power up the LCD screen, enable controller, and
  1358. * turn on the backlight.
  1359. */
  1360. if (old_state != C_ENABLE) {
  1361. fbi->state = C_ENABLE;
  1362. pxafb_enable_controller(fbi);
  1363. __pxafb_lcd_power(fbi, 1);
  1364. __pxafb_backlight_power(fbi, 1);
  1365. }
  1366. break;
  1367. }
  1368. mutex_unlock(&fbi->ctrlr_lock);
  1369. }
  1370. /*
  1371. * Our LCD controller task (which is called when we blank or unblank)
  1372. * via keventd.
  1373. */
  1374. static void pxafb_task(struct work_struct *work)
  1375. {
  1376. struct pxafb_info *fbi =
  1377. container_of(work, struct pxafb_info, task);
  1378. u_int state = xchg(&fbi->task_state, -1);
  1379. set_ctrlr_state(fbi, state);
  1380. }
  1381. #ifdef CONFIG_CPU_FREQ
  1382. /*
  1383. * CPU clock speed change handler. We need to adjust the LCD timing
  1384. * parameters when the CPU clock is adjusted by the power management
  1385. * subsystem.
  1386. *
  1387. * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
  1388. */
  1389. static int
  1390. pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
  1391. {
  1392. struct pxafb_info *fbi = TO_INF(nb, freq_transition);
  1393. /* TODO struct cpufreq_freqs *f = data; */
  1394. u_int pcd;
  1395. switch (val) {
  1396. case CPUFREQ_PRECHANGE:
  1397. #ifdef CONFIG_FB_PXA_OVERLAY
  1398. if (!(fbi->overlay[0].usage || fbi->overlay[1].usage))
  1399. #endif
  1400. set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
  1401. break;
  1402. case CPUFREQ_POSTCHANGE:
  1403. pcd = get_pcd(fbi, fbi->fb.var.pixclock);
  1404. set_hsync_time(fbi, pcd);
  1405. fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
  1406. LCCR3_PixClkDiv(pcd);
  1407. set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
  1408. break;
  1409. }
  1410. return 0;
  1411. }
  1412. static int
  1413. pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
  1414. {
  1415. struct pxafb_info *fbi = TO_INF(nb, freq_policy);
  1416. struct fb_var_screeninfo *var = &fbi->fb.var;
  1417. struct cpufreq_policy *policy = data;
  1418. switch (val) {
  1419. case CPUFREQ_ADJUST:
  1420. pr_debug("min dma period: %d ps, "
  1421. "new clock %d kHz\n", pxafb_display_dma_period(var),
  1422. policy->max);
  1423. /* TODO: fill in min/max values */
  1424. break;
  1425. }
  1426. return 0;
  1427. }
  1428. #endif
  1429. #ifdef CONFIG_PM
  1430. /*
  1431. * Power management hooks. Note that we won't be called from IRQ context,
  1432. * unlike the blank functions above, so we may sleep.
  1433. */
  1434. static int pxafb_suspend(struct device *dev)
  1435. {
  1436. struct pxafb_info *fbi = dev_get_drvdata(dev);
  1437. set_ctrlr_state(fbi, C_DISABLE_PM);
  1438. return 0;
  1439. }
  1440. static int pxafb_resume(struct device *dev)
  1441. {
  1442. struct pxafb_info *fbi = dev_get_drvdata(dev);
  1443. set_ctrlr_state(fbi, C_ENABLE_PM);
  1444. return 0;
  1445. }
  1446. static const struct dev_pm_ops pxafb_pm_ops = {
  1447. .suspend = pxafb_suspend,
  1448. .resume = pxafb_resume,
  1449. };
  1450. #endif
  1451. static int pxafb_init_video_memory(struct pxafb_info *fbi)
  1452. {
  1453. int size = PAGE_ALIGN(fbi->video_mem_size);
  1454. fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  1455. if (fbi->video_mem == NULL)
  1456. return -ENOMEM;
  1457. fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
  1458. fbi->video_mem_size = size;
  1459. fbi->fb.fix.smem_start = fbi->video_mem_phys;
  1460. fbi->fb.fix.smem_len = fbi->video_mem_size;
  1461. fbi->fb.screen_base = fbi->video_mem;
  1462. return fbi->video_mem ? 0 : -ENOMEM;
  1463. }
  1464. static void pxafb_decode_mach_info(struct pxafb_info *fbi,
  1465. struct pxafb_mach_info *inf)
  1466. {
  1467. unsigned int lcd_conn = inf->lcd_conn;
  1468. struct pxafb_mode_info *m;
  1469. int i;
  1470. fbi->cmap_inverse = inf->cmap_inverse;
  1471. fbi->cmap_static = inf->cmap_static;
  1472. fbi->lccr4 = inf->lccr4;
  1473. switch (lcd_conn & LCD_TYPE_MASK) {
  1474. case LCD_TYPE_MONO_STN:
  1475. fbi->lccr0 = LCCR0_CMS;
  1476. break;
  1477. case LCD_TYPE_MONO_DSTN:
  1478. fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
  1479. break;
  1480. case LCD_TYPE_COLOR_STN:
  1481. fbi->lccr0 = 0;
  1482. break;
  1483. case LCD_TYPE_COLOR_DSTN:
  1484. fbi->lccr0 = LCCR0_SDS;
  1485. break;
  1486. case LCD_TYPE_COLOR_TFT:
  1487. fbi->lccr0 = LCCR0_PAS;
  1488. break;
  1489. case LCD_TYPE_SMART_PANEL:
  1490. fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
  1491. break;
  1492. default:
  1493. /* fall back to backward compatibility way */
  1494. fbi->lccr0 = inf->lccr0;
  1495. fbi->lccr3 = inf->lccr3;
  1496. goto decode_mode;
  1497. }
  1498. if (lcd_conn == LCD_MONO_STN_8BPP)
  1499. fbi->lccr0 |= LCCR0_DPD;
  1500. fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
  1501. fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
  1502. fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
  1503. fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
  1504. decode_mode:
  1505. pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
  1506. /* decide video memory size as follows:
  1507. * 1. default to mode of maximum resolution
  1508. * 2. allow platform to override
  1509. * 3. allow module parameter to override
  1510. */
  1511. for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
  1512. fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
  1513. m->xres * m->yres * m->bpp / 8);
  1514. if (inf->video_mem_size > fbi->video_mem_size)
  1515. fbi->video_mem_size = inf->video_mem_size;
  1516. if (video_mem_size > fbi->video_mem_size)
  1517. fbi->video_mem_size = video_mem_size;
  1518. }
  1519. static struct pxafb_info *pxafb_init_fbinfo(struct device *dev,
  1520. struct pxafb_mach_info *inf)
  1521. {
  1522. struct pxafb_info *fbi;
  1523. void *addr;
  1524. /* Alloc the pxafb_info and pseudo_palette in one step */
  1525. fbi = devm_kzalloc(dev, sizeof(struct pxafb_info) + sizeof(u32) * 16,
  1526. GFP_KERNEL);
  1527. if (!fbi)
  1528. return ERR_PTR(-ENOMEM);
  1529. fbi->dev = dev;
  1530. fbi->inf = inf;
  1531. fbi->clk = devm_clk_get(dev, NULL);
  1532. if (IS_ERR(fbi->clk))
  1533. return ERR_CAST(fbi->clk);
  1534. strcpy(fbi->fb.fix.id, PXA_NAME);
  1535. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  1536. fbi->fb.fix.type_aux = 0;
  1537. fbi->fb.fix.xpanstep = 0;
  1538. fbi->fb.fix.ypanstep = 1;
  1539. fbi->fb.fix.ywrapstep = 0;
  1540. fbi->fb.fix.accel = FB_ACCEL_NONE;
  1541. fbi->fb.var.nonstd = 0;
  1542. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  1543. fbi->fb.var.height = -1;
  1544. fbi->fb.var.width = -1;
  1545. fbi->fb.var.accel_flags = FB_ACCELF_TEXT;
  1546. fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
  1547. fbi->fb.fbops = &pxafb_ops;
  1548. fbi->fb.flags = FBINFO_DEFAULT;
  1549. fbi->fb.node = -1;
  1550. addr = fbi;
  1551. addr = addr + sizeof(struct pxafb_info);
  1552. fbi->fb.pseudo_palette = addr;
  1553. fbi->state = C_STARTUP;
  1554. fbi->task_state = (u_char)-1;
  1555. pxafb_decode_mach_info(fbi, inf);
  1556. #ifdef CONFIG_FB_PXA_OVERLAY
  1557. /* place overlay(s) on top of base */
  1558. if (pxafb_overlay_supported())
  1559. fbi->lccr0 |= LCCR0_OUC;
  1560. #endif
  1561. init_waitqueue_head(&fbi->ctrlr_wait);
  1562. INIT_WORK(&fbi->task, pxafb_task);
  1563. mutex_init(&fbi->ctrlr_lock);
  1564. init_completion(&fbi->disable_done);
  1565. return fbi;
  1566. }
  1567. #ifdef CONFIG_FB_PXA_PARAMETERS
  1568. static int parse_opt_mode(struct device *dev, const char *this_opt,
  1569. struct pxafb_mach_info *inf)
  1570. {
  1571. const char *name = this_opt+5;
  1572. unsigned int namelen = strlen(name);
  1573. int res_specified = 0, bpp_specified = 0;
  1574. unsigned int xres = 0, yres = 0, bpp = 0;
  1575. int yres_specified = 0;
  1576. int i;
  1577. for (i = namelen-1; i >= 0; i--) {
  1578. switch (name[i]) {
  1579. case '-':
  1580. namelen = i;
  1581. if (!bpp_specified && !yres_specified) {
  1582. bpp = simple_strtoul(&name[i+1], NULL, 0);
  1583. bpp_specified = 1;
  1584. } else
  1585. goto done;
  1586. break;
  1587. case 'x':
  1588. if (!yres_specified) {
  1589. yres = simple_strtoul(&name[i+1], NULL, 0);
  1590. yres_specified = 1;
  1591. } else
  1592. goto done;
  1593. break;
  1594. case '0' ... '9':
  1595. break;
  1596. default:
  1597. goto done;
  1598. }
  1599. }
  1600. if (i < 0 && yres_specified) {
  1601. xres = simple_strtoul(name, NULL, 0);
  1602. res_specified = 1;
  1603. }
  1604. done:
  1605. if (res_specified) {
  1606. dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
  1607. inf->modes[0].xres = xres; inf->modes[0].yres = yres;
  1608. }
  1609. if (bpp_specified)
  1610. switch (bpp) {
  1611. case 1:
  1612. case 2:
  1613. case 4:
  1614. case 8:
  1615. case 16:
  1616. inf->modes[0].bpp = bpp;
  1617. dev_info(dev, "overriding bit depth: %d\n", bpp);
  1618. break;
  1619. default:
  1620. dev_err(dev, "Depth %d is not valid\n", bpp);
  1621. return -EINVAL;
  1622. }
  1623. return 0;
  1624. }
  1625. static int parse_opt(struct device *dev, char *this_opt,
  1626. struct pxafb_mach_info *inf)
  1627. {
  1628. struct pxafb_mode_info *mode = &inf->modes[0];
  1629. char s[64];
  1630. s[0] = '\0';
  1631. if (!strncmp(this_opt, "vmem:", 5)) {
  1632. video_mem_size = memparse(this_opt + 5, NULL);
  1633. } else if (!strncmp(this_opt, "mode:", 5)) {
  1634. return parse_opt_mode(dev, this_opt, inf);
  1635. } else if (!strncmp(this_opt, "pixclock:", 9)) {
  1636. mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
  1637. sprintf(s, "pixclock: %ld\n", mode->pixclock);
  1638. } else if (!strncmp(this_opt, "left:", 5)) {
  1639. mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
  1640. sprintf(s, "left: %u\n", mode->left_margin);
  1641. } else if (!strncmp(this_opt, "right:", 6)) {
  1642. mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
  1643. sprintf(s, "right: %u\n", mode->right_margin);
  1644. } else if (!strncmp(this_opt, "upper:", 6)) {
  1645. mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
  1646. sprintf(s, "upper: %u\n", mode->upper_margin);
  1647. } else if (!strncmp(this_opt, "lower:", 6)) {
  1648. mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
  1649. sprintf(s, "lower: %u\n", mode->lower_margin);
  1650. } else if (!strncmp(this_opt, "hsynclen:", 9)) {
  1651. mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
  1652. sprintf(s, "hsynclen: %u\n", mode->hsync_len);
  1653. } else if (!strncmp(this_opt, "vsynclen:", 9)) {
  1654. mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
  1655. sprintf(s, "vsynclen: %u\n", mode->vsync_len);
  1656. } else if (!strncmp(this_opt, "hsync:", 6)) {
  1657. if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
  1658. sprintf(s, "hsync: Active Low\n");
  1659. mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
  1660. } else {
  1661. sprintf(s, "hsync: Active High\n");
  1662. mode->sync |= FB_SYNC_HOR_HIGH_ACT;
  1663. }
  1664. } else if (!strncmp(this_opt, "vsync:", 6)) {
  1665. if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
  1666. sprintf(s, "vsync: Active Low\n");
  1667. mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
  1668. } else {
  1669. sprintf(s, "vsync: Active High\n");
  1670. mode->sync |= FB_SYNC_VERT_HIGH_ACT;
  1671. }
  1672. } else if (!strncmp(this_opt, "dpc:", 4)) {
  1673. if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
  1674. sprintf(s, "double pixel clock: false\n");
  1675. inf->lccr3 &= ~LCCR3_DPC;
  1676. } else {
  1677. sprintf(s, "double pixel clock: true\n");
  1678. inf->lccr3 |= LCCR3_DPC;
  1679. }
  1680. } else if (!strncmp(this_opt, "outputen:", 9)) {
  1681. if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
  1682. sprintf(s, "output enable: active low\n");
  1683. inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
  1684. } else {
  1685. sprintf(s, "output enable: active high\n");
  1686. inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
  1687. }
  1688. } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
  1689. if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
  1690. sprintf(s, "pixel clock polarity: falling edge\n");
  1691. inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
  1692. } else {
  1693. sprintf(s, "pixel clock polarity: rising edge\n");
  1694. inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
  1695. }
  1696. } else if (!strncmp(this_opt, "color", 5)) {
  1697. inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
  1698. } else if (!strncmp(this_opt, "mono", 4)) {
  1699. inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
  1700. } else if (!strncmp(this_opt, "active", 6)) {
  1701. inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
  1702. } else if (!strncmp(this_opt, "passive", 7)) {
  1703. inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
  1704. } else if (!strncmp(this_opt, "single", 6)) {
  1705. inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
  1706. } else if (!strncmp(this_opt, "dual", 4)) {
  1707. inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
  1708. } else if (!strncmp(this_opt, "4pix", 4)) {
  1709. inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
  1710. } else if (!strncmp(this_opt, "8pix", 4)) {
  1711. inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
  1712. } else {
  1713. dev_err(dev, "unknown option: %s\n", this_opt);
  1714. return -EINVAL;
  1715. }
  1716. if (s[0] != '\0')
  1717. dev_info(dev, "override %s", s);
  1718. return 0;
  1719. }
  1720. static int pxafb_parse_options(struct device *dev, char *options,
  1721. struct pxafb_mach_info *inf)
  1722. {
  1723. char *this_opt;
  1724. int ret;
  1725. if (!options || !*options)
  1726. return 0;
  1727. dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
  1728. /* could be made table driven or similar?... */
  1729. while ((this_opt = strsep(&options, ",")) != NULL) {
  1730. ret = parse_opt(dev, this_opt, inf);
  1731. if (ret)
  1732. return ret;
  1733. }
  1734. return 0;
  1735. }
  1736. static char g_options[256] = "";
  1737. #ifndef MODULE
  1738. static int __init pxafb_setup_options(void)
  1739. {
  1740. char *options = NULL;
  1741. if (fb_get_options("pxafb", &options))
  1742. return -ENODEV;
  1743. if (options)
  1744. strlcpy(g_options, options, sizeof(g_options));
  1745. return 0;
  1746. }
  1747. #else
  1748. #define pxafb_setup_options() (0)
  1749. module_param_string(options, g_options, sizeof(g_options), 0);
  1750. MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
  1751. #endif
  1752. #else
  1753. #define pxafb_parse_options(...) (0)
  1754. #define pxafb_setup_options() (0)
  1755. #endif
  1756. #ifdef DEBUG_VAR
  1757. /* Check for various illegal bit-combinations. Currently only
  1758. * a warning is given. */
  1759. static void pxafb_check_options(struct device *dev, struct pxafb_mach_info *inf)
  1760. {
  1761. if (inf->lcd_conn)
  1762. return;
  1763. if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
  1764. dev_warn(dev, "machine LCCR0 setting contains "
  1765. "illegal bits: %08x\n",
  1766. inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
  1767. if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
  1768. dev_warn(dev, "machine LCCR3 setting contains "
  1769. "illegal bits: %08x\n",
  1770. inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
  1771. if (inf->lccr0 & LCCR0_DPD &&
  1772. ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
  1773. (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
  1774. (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
  1775. dev_warn(dev, "Double Pixel Data (DPD) mode is "
  1776. "only valid in passive mono"
  1777. " single panel mode\n");
  1778. if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
  1779. (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
  1780. dev_warn(dev, "Dual panel only valid in passive mode\n");
  1781. if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
  1782. (inf->modes->upper_margin || inf->modes->lower_margin))
  1783. dev_warn(dev, "Upper and lower margins must be 0 in "
  1784. "passive mode\n");
  1785. }
  1786. #else
  1787. #define pxafb_check_options(...) do {} while (0)
  1788. #endif
  1789. #if defined(CONFIG_OF)
  1790. static const char * const lcd_types[] = {
  1791. "unknown", "mono-stn", "mono-dstn", "color-stn", "color-dstn",
  1792. "color-tft", "smart-panel", NULL
  1793. };
  1794. static int of_get_pxafb_display(struct device *dev, struct device_node *disp,
  1795. struct pxafb_mach_info *info, u32 bus_width)
  1796. {
  1797. struct display_timings *timings;
  1798. struct videomode vm;
  1799. int i, ret = -EINVAL;
  1800. const char *s;
  1801. ret = of_property_read_string(disp, "lcd-type", &s);
  1802. if (ret)
  1803. s = "color-tft";
  1804. i = match_string(lcd_types, -1, s);
  1805. if (i < 0) {
  1806. dev_err(dev, "lcd-type %s is unknown\n", s);
  1807. return i;
  1808. }
  1809. info->lcd_conn |= LCD_CONN_TYPE(i);
  1810. info->lcd_conn |= LCD_CONN_WIDTH(bus_width);
  1811. timings = of_get_display_timings(disp);
  1812. if (!timings)
  1813. return -EINVAL;
  1814. ret = -ENOMEM;
  1815. info->modes = devm_kcalloc(dev, timings->num_timings,
  1816. sizeof(info->modes[0]),
  1817. GFP_KERNEL);
  1818. if (!info->modes)
  1819. goto out;
  1820. info->num_modes = timings->num_timings;
  1821. for (i = 0; i < timings->num_timings; i++) {
  1822. ret = videomode_from_timings(timings, &vm, i);
  1823. if (ret) {
  1824. dev_err(dev, "videomode_from_timings %d failed: %d\n",
  1825. i, ret);
  1826. goto out;
  1827. }
  1828. if (vm.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
  1829. info->lcd_conn |= LCD_PCLK_EDGE_RISE;
  1830. if (vm.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  1831. info->lcd_conn |= LCD_PCLK_EDGE_FALL;
  1832. if (vm.flags & DISPLAY_FLAGS_DE_HIGH)
  1833. info->lcd_conn |= LCD_BIAS_ACTIVE_HIGH;
  1834. if (vm.flags & DISPLAY_FLAGS_DE_LOW)
  1835. info->lcd_conn |= LCD_BIAS_ACTIVE_LOW;
  1836. if (vm.flags & DISPLAY_FLAGS_HSYNC_HIGH)
  1837. info->modes[i].sync |= FB_SYNC_HOR_HIGH_ACT;
  1838. if (vm.flags & DISPLAY_FLAGS_VSYNC_HIGH)
  1839. info->modes[i].sync |= FB_SYNC_VERT_HIGH_ACT;
  1840. info->modes[i].pixclock = 1000000000UL / (vm.pixelclock / 1000);
  1841. info->modes[i].xres = vm.hactive;
  1842. info->modes[i].yres = vm.vactive;
  1843. info->modes[i].hsync_len = vm.hsync_len;
  1844. info->modes[i].left_margin = vm.hback_porch;
  1845. info->modes[i].right_margin = vm.hfront_porch;
  1846. info->modes[i].vsync_len = vm.vsync_len;
  1847. info->modes[i].upper_margin = vm.vback_porch;
  1848. info->modes[i].lower_margin = vm.vfront_porch;
  1849. }
  1850. ret = 0;
  1851. out:
  1852. display_timings_release(timings);
  1853. return ret;
  1854. }
  1855. static int of_get_pxafb_mode_info(struct device *dev,
  1856. struct pxafb_mach_info *info)
  1857. {
  1858. struct device_node *display, *np;
  1859. u32 bus_width;
  1860. int ret, i;
  1861. np = of_graph_get_next_endpoint(dev->of_node, NULL);
  1862. if (!np) {
  1863. dev_err(dev, "could not find endpoint\n");
  1864. return -EINVAL;
  1865. }
  1866. ret = of_property_read_u32(np, "bus-width", &bus_width);
  1867. if (ret) {
  1868. dev_err(dev, "no bus-width specified: %d\n", ret);
  1869. of_node_put(np);
  1870. return ret;
  1871. }
  1872. display = of_graph_get_remote_port_parent(np);
  1873. of_node_put(np);
  1874. if (!display) {
  1875. dev_err(dev, "no display defined\n");
  1876. return -EINVAL;
  1877. }
  1878. ret = of_get_pxafb_display(dev, display, info, bus_width);
  1879. of_node_put(display);
  1880. if (ret)
  1881. return ret;
  1882. for (i = 0; i < info->num_modes; i++)
  1883. info->modes[i].bpp = bus_width;
  1884. return 0;
  1885. }
  1886. static struct pxafb_mach_info *of_pxafb_of_mach_info(struct device *dev)
  1887. {
  1888. int ret;
  1889. struct pxafb_mach_info *info;
  1890. if (!dev->of_node)
  1891. return NULL;
  1892. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  1893. if (!info)
  1894. return ERR_PTR(-ENOMEM);
  1895. ret = of_get_pxafb_mode_info(dev, info);
  1896. if (ret)
  1897. return ERR_PTR(ret);
  1898. /*
  1899. * On purpose, neither lccrX registers nor video memory size can be
  1900. * specified through device-tree, they are considered more a debug hack
  1901. * available through command line.
  1902. */
  1903. return info;
  1904. }
  1905. #else
  1906. static struct pxafb_mach_info *of_pxafb_of_mach_info(struct device *dev)
  1907. {
  1908. return NULL;
  1909. }
  1910. #endif
  1911. static int pxafb_probe(struct platform_device *dev)
  1912. {
  1913. struct pxafb_info *fbi;
  1914. struct pxafb_mach_info *inf, *pdata;
  1915. struct resource *r;
  1916. int i, irq, ret;
  1917. dev_dbg(&dev->dev, "pxafb_probe\n");
  1918. ret = -ENOMEM;
  1919. pdata = dev_get_platdata(&dev->dev);
  1920. inf = devm_kmalloc(&dev->dev, sizeof(*inf), GFP_KERNEL);
  1921. if (!inf)
  1922. goto failed;
  1923. if (pdata) {
  1924. *inf = *pdata;
  1925. inf->modes =
  1926. devm_kmalloc_array(&dev->dev, pdata->num_modes,
  1927. sizeof(inf->modes[0]), GFP_KERNEL);
  1928. if (!inf->modes)
  1929. goto failed;
  1930. for (i = 0; i < inf->num_modes; i++)
  1931. inf->modes[i] = pdata->modes[i];
  1932. }
  1933. if (!pdata)
  1934. inf = of_pxafb_of_mach_info(&dev->dev);
  1935. if (IS_ERR_OR_NULL(inf))
  1936. goto failed;
  1937. ret = pxafb_parse_options(&dev->dev, g_options, inf);
  1938. if (ret < 0)
  1939. goto failed;
  1940. pxafb_check_options(&dev->dev, inf);
  1941. dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
  1942. inf->modes->xres,
  1943. inf->modes->yres,
  1944. inf->modes->bpp);
  1945. if (inf->modes->xres == 0 ||
  1946. inf->modes->yres == 0 ||
  1947. inf->modes->bpp == 0) {
  1948. dev_err(&dev->dev, "Invalid resolution or bit depth\n");
  1949. ret = -EINVAL;
  1950. goto failed;
  1951. }
  1952. fbi = pxafb_init_fbinfo(&dev->dev, inf);
  1953. if (IS_ERR(fbi)) {
  1954. dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
  1955. ret = PTR_ERR(fbi);
  1956. goto failed;
  1957. }
  1958. if (cpu_is_pxa3xx() && inf->acceleration_enabled)
  1959. fbi->fb.fix.accel = FB_ACCEL_PXA3XX;
  1960. fbi->backlight_power = inf->pxafb_backlight_power;
  1961. fbi->lcd_power = inf->pxafb_lcd_power;
  1962. fbi->lcd_supply = devm_regulator_get_optional(&dev->dev, "lcd");
  1963. if (IS_ERR(fbi->lcd_supply)) {
  1964. if (PTR_ERR(fbi->lcd_supply) == -EPROBE_DEFER)
  1965. return -EPROBE_DEFER;
  1966. fbi->lcd_supply = NULL;
  1967. }
  1968. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1969. if (r == NULL) {
  1970. dev_err(&dev->dev, "no I/O memory resource defined\n");
  1971. ret = -ENODEV;
  1972. goto failed;
  1973. }
  1974. fbi->mmio_base = devm_ioremap_resource(&dev->dev, r);
  1975. if (IS_ERR(fbi->mmio_base)) {
  1976. dev_err(&dev->dev, "failed to get I/O memory\n");
  1977. ret = -EBUSY;
  1978. goto failed;
  1979. }
  1980. fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
  1981. fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
  1982. &fbi->dma_buff_phys, GFP_KERNEL);
  1983. if (fbi->dma_buff == NULL) {
  1984. dev_err(&dev->dev, "failed to allocate memory for DMA\n");
  1985. ret = -ENOMEM;
  1986. goto failed;
  1987. }
  1988. ret = pxafb_init_video_memory(fbi);
  1989. if (ret) {
  1990. dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
  1991. ret = -ENOMEM;
  1992. goto failed_free_dma;
  1993. }
  1994. irq = platform_get_irq(dev, 0);
  1995. if (irq < 0) {
  1996. dev_err(&dev->dev, "no IRQ defined\n");
  1997. ret = -ENODEV;
  1998. goto failed_free_mem;
  1999. }
  2000. ret = devm_request_irq(&dev->dev, irq, pxafb_handle_irq, 0, "LCD", fbi);
  2001. if (ret) {
  2002. dev_err(&dev->dev, "request_irq failed: %d\n", ret);
  2003. ret = -EBUSY;
  2004. goto failed_free_mem;
  2005. }
  2006. ret = pxafb_smart_init(fbi);
  2007. if (ret) {
  2008. dev_err(&dev->dev, "failed to initialize smartpanel\n");
  2009. goto failed_free_mem;
  2010. }
  2011. /*
  2012. * This makes sure that our colour bitfield
  2013. * descriptors are correctly initialised.
  2014. */
  2015. ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
  2016. if (ret) {
  2017. dev_err(&dev->dev, "failed to get suitable mode\n");
  2018. goto failed_free_mem;
  2019. }
  2020. ret = pxafb_set_par(&fbi->fb);
  2021. if (ret) {
  2022. dev_err(&dev->dev, "Failed to set parameters\n");
  2023. goto failed_free_mem;
  2024. }
  2025. platform_set_drvdata(dev, fbi);
  2026. ret = register_framebuffer(&fbi->fb);
  2027. if (ret < 0) {
  2028. dev_err(&dev->dev,
  2029. "Failed to register framebuffer device: %d\n", ret);
  2030. goto failed_free_cmap;
  2031. }
  2032. pxafb_overlay_init(fbi);
  2033. #ifdef CONFIG_CPU_FREQ
  2034. fbi->freq_transition.notifier_call = pxafb_freq_transition;
  2035. fbi->freq_policy.notifier_call = pxafb_freq_policy;
  2036. cpufreq_register_notifier(&fbi->freq_transition,
  2037. CPUFREQ_TRANSITION_NOTIFIER);
  2038. cpufreq_register_notifier(&fbi->freq_policy,
  2039. CPUFREQ_POLICY_NOTIFIER);
  2040. #endif
  2041. /*
  2042. * Ok, now enable the LCD controller
  2043. */
  2044. set_ctrlr_state(fbi, C_ENABLE);
  2045. return 0;
  2046. failed_free_cmap:
  2047. if (fbi->fb.cmap.len)
  2048. fb_dealloc_cmap(&fbi->fb.cmap);
  2049. failed_free_mem:
  2050. free_pages_exact(fbi->video_mem, fbi->video_mem_size);
  2051. failed_free_dma:
  2052. dma_free_coherent(&dev->dev, fbi->dma_buff_size,
  2053. fbi->dma_buff, fbi->dma_buff_phys);
  2054. failed:
  2055. return ret;
  2056. }
  2057. static int pxafb_remove(struct platform_device *dev)
  2058. {
  2059. struct pxafb_info *fbi = platform_get_drvdata(dev);
  2060. struct fb_info *info;
  2061. if (!fbi)
  2062. return 0;
  2063. info = &fbi->fb;
  2064. pxafb_overlay_exit(fbi);
  2065. unregister_framebuffer(info);
  2066. pxafb_disable_controller(fbi);
  2067. if (fbi->fb.cmap.len)
  2068. fb_dealloc_cmap(&fbi->fb.cmap);
  2069. free_pages_exact(fbi->video_mem, fbi->video_mem_size);
  2070. dma_free_coherent(&dev->dev, fbi->dma_buff_size, fbi->dma_buff,
  2071. fbi->dma_buff_phys);
  2072. return 0;
  2073. }
  2074. static const struct of_device_id pxafb_of_dev_id[] = {
  2075. { .compatible = "marvell,pxa270-lcdc", },
  2076. { .compatible = "marvell,pxa300-lcdc", },
  2077. { .compatible = "marvell,pxa2xx-lcdc", },
  2078. { /* sentinel */ }
  2079. };
  2080. MODULE_DEVICE_TABLE(of, pxafb_of_dev_id);
  2081. static struct platform_driver pxafb_driver = {
  2082. .probe = pxafb_probe,
  2083. .remove = pxafb_remove,
  2084. .driver = {
  2085. .name = "pxa2xx-fb",
  2086. .of_match_table = pxafb_of_dev_id,
  2087. #ifdef CONFIG_PM
  2088. .pm = &pxafb_pm_ops,
  2089. #endif
  2090. },
  2091. };
  2092. static int __init pxafb_init(void)
  2093. {
  2094. if (pxafb_setup_options())
  2095. return -EINVAL;
  2096. return platform_driver_register(&pxafb_driver);
  2097. }
  2098. static void __exit pxafb_exit(void)
  2099. {
  2100. platform_driver_unregister(&pxafb_driver);
  2101. }
  2102. module_init(pxafb_init);
  2103. module_exit(pxafb_exit);
  2104. MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
  2105. MODULE_LICENSE("GPL");