pxafb.c 65 KB

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