pxafb.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  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");