pxafb.c 65 KB

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