pxafb.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. */
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/errno.h>
  29. #include <linux/string.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/slab.h>
  32. #include <linux/mm.h>
  33. #include <linux/fb.h>
  34. #include <linux/delay.h>
  35. #include <linux/init.h>
  36. #include <linux/ioport.h>
  37. #include <linux/cpufreq.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/clk.h>
  41. #include <linux/err.h>
  42. #include <linux/completion.h>
  43. #include <linux/mutex.h>
  44. #include <linux/kthread.h>
  45. #include <linux/freezer.h>
  46. #include <mach/hardware.h>
  47. #include <asm/io.h>
  48. #include <asm/irq.h>
  49. #include <asm/div64.h>
  50. #include <mach/pxa-regs.h>
  51. #include <mach/bitfield.h>
  52. #include <mach/pxafb.h>
  53. /*
  54. * Complain if VAR is out of range.
  55. */
  56. #define DEBUG_VAR 1
  57. #include "pxafb.h"
  58. /* Bits which should not be set in machine configuration structures */
  59. #define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
  60. LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
  61. LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
  62. #define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
  63. LCCR3_PCD | LCCR3_BPP(0xf))
  64. static int pxafb_activate_var(struct fb_var_screeninfo *var,
  65. struct pxafb_info *);
  66. static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
  67. static void setup_base_frame(struct pxafb_info *fbi, int branch);
  68. static unsigned long video_mem_size = 0;
  69. static inline unsigned long
  70. lcd_readl(struct pxafb_info *fbi, unsigned int off)
  71. {
  72. return __raw_readl(fbi->mmio_base + off);
  73. }
  74. static inline void
  75. lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
  76. {
  77. __raw_writel(val, fbi->mmio_base + off);
  78. }
  79. static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
  80. {
  81. unsigned long flags;
  82. local_irq_save(flags);
  83. /*
  84. * We need to handle two requests being made at the same time.
  85. * There are two important cases:
  86. * 1. When we are changing VT (C_REENABLE) while unblanking
  87. * (C_ENABLE) We must perform the unblanking, which will
  88. * do our REENABLE for us.
  89. * 2. When we are blanking, but immediately unblank before
  90. * we have blanked. We do the "REENABLE" thing here as
  91. * well, just to be sure.
  92. */
  93. if (fbi->task_state == C_ENABLE && state == C_REENABLE)
  94. state = (u_int) -1;
  95. if (fbi->task_state == C_DISABLE && state == C_ENABLE)
  96. state = C_REENABLE;
  97. if (state != (u_int)-1) {
  98. fbi->task_state = state;
  99. schedule_work(&fbi->task);
  100. }
  101. local_irq_restore(flags);
  102. }
  103. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  104. {
  105. chan &= 0xffff;
  106. chan >>= 16 - bf->length;
  107. return chan << bf->offset;
  108. }
  109. static int
  110. pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
  111. u_int trans, struct fb_info *info)
  112. {
  113. struct pxafb_info *fbi = (struct pxafb_info *)info;
  114. u_int val;
  115. if (regno >= fbi->palette_size)
  116. return 1;
  117. if (fbi->fb.var.grayscale) {
  118. fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
  119. return 0;
  120. }
  121. switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
  122. case LCCR4_PAL_FOR_0:
  123. val = ((red >> 0) & 0xf800);
  124. val |= ((green >> 5) & 0x07e0);
  125. val |= ((blue >> 11) & 0x001f);
  126. fbi->palette_cpu[regno] = val;
  127. break;
  128. case LCCR4_PAL_FOR_1:
  129. val = ((red << 8) & 0x00f80000);
  130. val |= ((green >> 0) & 0x0000fc00);
  131. val |= ((blue >> 8) & 0x000000f8);
  132. ((u32 *)(fbi->palette_cpu))[regno] = val;
  133. break;
  134. case LCCR4_PAL_FOR_2:
  135. val = ((red << 8) & 0x00fc0000);
  136. val |= ((green >> 0) & 0x0000fc00);
  137. val |= ((blue >> 8) & 0x000000fc);
  138. ((u32 *)(fbi->palette_cpu))[regno] = val;
  139. break;
  140. case LCCR4_PAL_FOR_3:
  141. val = ((red << 8) & 0x00ff0000);
  142. val |= ((green >> 0) & 0x0000ff00);
  143. val |= ((blue >> 8) & 0x000000ff);
  144. ((u32 *)(fbi->palette_cpu))[regno] = val;
  145. break;
  146. }
  147. return 0;
  148. }
  149. static int
  150. pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  151. u_int trans, struct fb_info *info)
  152. {
  153. struct pxafb_info *fbi = (struct pxafb_info *)info;
  154. unsigned int val;
  155. int ret = 1;
  156. /*
  157. * If inverse mode was selected, invert all the colours
  158. * rather than the register number. The register number
  159. * is what you poke into the framebuffer to produce the
  160. * colour you requested.
  161. */
  162. if (fbi->cmap_inverse) {
  163. red = 0xffff - red;
  164. green = 0xffff - green;
  165. blue = 0xffff - blue;
  166. }
  167. /*
  168. * If greyscale is true, then we convert the RGB value
  169. * to greyscale no matter what visual we are using.
  170. */
  171. if (fbi->fb.var.grayscale)
  172. red = green = blue = (19595 * red + 38470 * green +
  173. 7471 * blue) >> 16;
  174. switch (fbi->fb.fix.visual) {
  175. case FB_VISUAL_TRUECOLOR:
  176. /*
  177. * 16-bit True Colour. We encode the RGB value
  178. * according to the RGB bitfield information.
  179. */
  180. if (regno < 16) {
  181. u32 *pal = fbi->fb.pseudo_palette;
  182. val = chan_to_field(red, &fbi->fb.var.red);
  183. val |= chan_to_field(green, &fbi->fb.var.green);
  184. val |= chan_to_field(blue, &fbi->fb.var.blue);
  185. pal[regno] = val;
  186. ret = 0;
  187. }
  188. break;
  189. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  190. case FB_VISUAL_PSEUDOCOLOR:
  191. ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
  192. break;
  193. }
  194. return ret;
  195. }
  196. /* calculate pixel depth, transparency bit included, >=16bpp formats _only_ */
  197. static inline int var_to_depth(struct fb_var_screeninfo *var)
  198. {
  199. return var->red.length + var->green.length +
  200. var->blue.length + var->transp.length;
  201. }
  202. /* calculate 4-bit BPP value for LCCR3 and OVLxC1 */
  203. static int pxafb_var_to_bpp(struct fb_var_screeninfo *var)
  204. {
  205. int bpp = -EINVAL;
  206. switch (var->bits_per_pixel) {
  207. case 1: bpp = 0; break;
  208. case 2: bpp = 1; break;
  209. case 4: bpp = 2; break;
  210. case 8: bpp = 3; break;
  211. case 16: bpp = 4; break;
  212. case 24:
  213. switch (var_to_depth(var)) {
  214. case 18: bpp = 6; break; /* 18-bits/pixel packed */
  215. case 19: bpp = 8; break; /* 19-bits/pixel packed */
  216. case 24: bpp = 9; break;
  217. }
  218. break;
  219. case 32:
  220. switch (var_to_depth(var)) {
  221. case 18: bpp = 5; break; /* 18-bits/pixel unpacked */
  222. case 19: bpp = 7; break; /* 19-bits/pixel unpacked */
  223. case 25: bpp = 10; break;
  224. }
  225. break;
  226. }
  227. return bpp;
  228. }
  229. /*
  230. * pxafb_var_to_lccr3():
  231. * Convert a bits per pixel value to the correct bit pattern for LCCR3
  232. *
  233. * NOTE: for PXA27x with overlays support, the LCCR3_PDFOR_x bits have an
  234. * implication of the acutal use of transparency bit, which we handle it
  235. * here separatedly. See PXA27x Developer's Manual, Section <<7.4.6 Pixel
  236. * Formats>> for the valid combination of PDFOR, PAL_FOR for various BPP.
  237. *
  238. * Transparency for palette pixel formats is not supported at the moment.
  239. */
  240. static uint32_t pxafb_var_to_lccr3(struct fb_var_screeninfo *var)
  241. {
  242. int bpp = pxafb_var_to_bpp(var);
  243. uint32_t lccr3;
  244. if (bpp < 0)
  245. return 0;
  246. lccr3 = LCCR3_BPP(bpp);
  247. switch (var_to_depth(var)) {
  248. case 16: lccr3 |= var->transp.length ? LCCR3_PDFOR_3 : 0; break;
  249. case 18: lccr3 |= LCCR3_PDFOR_3; break;
  250. case 24: lccr3 |= var->transp.length ? LCCR3_PDFOR_2 : LCCR3_PDFOR_3;
  251. break;
  252. case 19:
  253. case 25: lccr3 |= LCCR3_PDFOR_0; break;
  254. }
  255. return lccr3;
  256. }
  257. #define SET_PIXFMT(v, r, g, b, t) \
  258. ({ \
  259. (v)->transp.offset = (t) ? (r) + (g) + (b) : 0; \
  260. (v)->transp.length = (t) ? (t) : 0; \
  261. (v)->blue.length = (b); (v)->blue.offset = 0; \
  262. (v)->green.length = (g); (v)->green.offset = (b); \
  263. (v)->red.length = (r); (v)->red.offset = (b) + (g); \
  264. })
  265. /* set the RGBT bitfields of fb_var_screeninf according to
  266. * var->bits_per_pixel and given depth
  267. */
  268. static void pxafb_set_pixfmt(struct fb_var_screeninfo *var, int depth)
  269. {
  270. if (depth == 0)
  271. depth = var->bits_per_pixel;
  272. if (var->bits_per_pixel < 16) {
  273. /* indexed pixel formats */
  274. var->red.offset = 0; var->red.length = 8;
  275. var->green.offset = 0; var->green.length = 8;
  276. var->blue.offset = 0; var->blue.length = 8;
  277. var->transp.offset = 0; var->transp.length = 8;
  278. }
  279. switch (depth) {
  280. case 16: var->transp.length ?
  281. SET_PIXFMT(var, 5, 5, 5, 1) : /* RGBT555 */
  282. SET_PIXFMT(var, 5, 6, 5, 0); break; /* RGB565 */
  283. case 18: SET_PIXFMT(var, 6, 6, 6, 0); break; /* RGB666 */
  284. case 19: SET_PIXFMT(var, 6, 6, 6, 1); break; /* RGBT666 */
  285. case 24: var->transp.length ?
  286. SET_PIXFMT(var, 8, 8, 7, 1) : /* RGBT887 */
  287. SET_PIXFMT(var, 8, 8, 8, 0); break; /* RGB888 */
  288. case 25: SET_PIXFMT(var, 8, 8, 8, 1); break; /* RGBT888 */
  289. }
  290. }
  291. #ifdef CONFIG_CPU_FREQ
  292. /*
  293. * pxafb_display_dma_period()
  294. * Calculate the minimum period (in picoseconds) between two DMA
  295. * requests for the LCD controller. If we hit this, it means we're
  296. * doing nothing but LCD DMA.
  297. */
  298. static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
  299. {
  300. /*
  301. * Period = pixclock * bits_per_byte * bytes_per_transfer
  302. * / memory_bits_per_pixel;
  303. */
  304. return var->pixclock * 8 * 16 / var->bits_per_pixel;
  305. }
  306. #endif
  307. /*
  308. * Select the smallest mode that allows the desired resolution to be
  309. * displayed. If desired parameters can be rounded up.
  310. */
  311. static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
  312. struct fb_var_screeninfo *var)
  313. {
  314. struct pxafb_mode_info *mode = NULL;
  315. struct pxafb_mode_info *modelist = mach->modes;
  316. unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
  317. unsigned int i;
  318. for (i = 0; i < mach->num_modes; i++) {
  319. if (modelist[i].xres >= var->xres &&
  320. modelist[i].yres >= var->yres &&
  321. modelist[i].xres < best_x &&
  322. modelist[i].yres < best_y &&
  323. modelist[i].bpp >= var->bits_per_pixel) {
  324. best_x = modelist[i].xres;
  325. best_y = modelist[i].yres;
  326. mode = &modelist[i];
  327. }
  328. }
  329. return mode;
  330. }
  331. static void pxafb_setmode(struct fb_var_screeninfo *var,
  332. struct pxafb_mode_info *mode)
  333. {
  334. var->xres = mode->xres;
  335. var->yres = mode->yres;
  336. var->bits_per_pixel = mode->bpp;
  337. var->pixclock = mode->pixclock;
  338. var->hsync_len = mode->hsync_len;
  339. var->left_margin = mode->left_margin;
  340. var->right_margin = mode->right_margin;
  341. var->vsync_len = mode->vsync_len;
  342. var->upper_margin = mode->upper_margin;
  343. var->lower_margin = mode->lower_margin;
  344. var->sync = mode->sync;
  345. var->grayscale = mode->cmap_greyscale;
  346. /* set the initial RGBA bitfields */
  347. pxafb_set_pixfmt(var, mode->depth);
  348. }
  349. /*
  350. * pxafb_check_var():
  351. * Get the video params out of 'var'. If a value doesn't fit, round it up,
  352. * if it's too big, return -EINVAL.
  353. *
  354. * Round up in the following order: bits_per_pixel, xres,
  355. * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
  356. * bitfields, horizontal timing, vertical timing.
  357. */
  358. static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  359. {
  360. struct pxafb_info *fbi = (struct pxafb_info *)info;
  361. struct pxafb_mach_info *inf = fbi->dev->platform_data;
  362. int err;
  363. if (var->xres < MIN_XRES)
  364. var->xres = MIN_XRES;
  365. if (var->yres < MIN_YRES)
  366. var->yres = MIN_YRES;
  367. if (inf->fixed_modes) {
  368. struct pxafb_mode_info *mode;
  369. mode = pxafb_getmode(inf, var);
  370. if (!mode)
  371. return -EINVAL;
  372. pxafb_setmode(var, mode);
  373. } else {
  374. if (var->xres > inf->modes->xres)
  375. return -EINVAL;
  376. if (var->yres > inf->modes->yres)
  377. return -EINVAL;
  378. if (var->bits_per_pixel > inf->modes->bpp)
  379. return -EINVAL;
  380. }
  381. /* we don't support xpan, force xres_virtual to be equal to xres */
  382. var->xres_virtual = var->xres;
  383. if (var->accel_flags & FB_ACCELF_TEXT)
  384. var->yres_virtual = fbi->fb.fix.smem_len /
  385. (var->xres_virtual * var->bits_per_pixel / 8);
  386. else
  387. var->yres_virtual = max(var->yres_virtual, var->yres);
  388. /* do a test conversion to BPP fields to check the color formats */
  389. err = pxafb_var_to_bpp(var);
  390. if (err < 0)
  391. return err;
  392. pxafb_set_pixfmt(var, var_to_depth(var));
  393. #ifdef CONFIG_CPU_FREQ
  394. pr_debug("pxafb: dma period = %d ps\n",
  395. pxafb_display_dma_period(var));
  396. #endif
  397. return 0;
  398. }
  399. /*
  400. * pxafb_set_par():
  401. * Set the user defined part of the display for the specified console
  402. */
  403. static int pxafb_set_par(struct fb_info *info)
  404. {
  405. struct pxafb_info *fbi = (struct pxafb_info *)info;
  406. struct fb_var_screeninfo *var = &info->var;
  407. if (var->bits_per_pixel >= 16)
  408. fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  409. else if (!fbi->cmap_static)
  410. fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  411. else {
  412. /*
  413. * Some people have weird ideas about wanting static
  414. * pseudocolor maps. I suspect their user space
  415. * applications are broken.
  416. */
  417. fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
  418. }
  419. fbi->fb.fix.line_length = var->xres_virtual *
  420. var->bits_per_pixel / 8;
  421. if (var->bits_per_pixel >= 16)
  422. fbi->palette_size = 0;
  423. else
  424. fbi->palette_size = var->bits_per_pixel == 1 ?
  425. 4 : 1 << var->bits_per_pixel;
  426. fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
  427. if (fbi->fb.var.bits_per_pixel >= 16)
  428. fb_dealloc_cmap(&fbi->fb.cmap);
  429. else
  430. fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
  431. pxafb_activate_var(var, fbi);
  432. return 0;
  433. }
  434. static int pxafb_pan_display(struct fb_var_screeninfo *var,
  435. struct fb_info *info)
  436. {
  437. struct pxafb_info *fbi = (struct pxafb_info *)info;
  438. int dma = DMA_MAX + DMA_BASE;
  439. if (fbi->state != C_ENABLE)
  440. return 0;
  441. setup_base_frame(fbi, 1);
  442. if (fbi->lccr0 & LCCR0_SDS)
  443. lcd_writel(fbi, FBR1, fbi->fdadr[dma + 1] | 0x1);
  444. lcd_writel(fbi, FBR0, fbi->fdadr[dma] | 0x1);
  445. return 0;
  446. }
  447. /*
  448. * pxafb_blank():
  449. * Blank the display by setting all palette values to zero. Note, the
  450. * 16 bpp mode does not really use the palette, so this will not
  451. * blank the display in all modes.
  452. */
  453. static int pxafb_blank(int blank, struct fb_info *info)
  454. {
  455. struct pxafb_info *fbi = (struct pxafb_info *)info;
  456. int i;
  457. switch (blank) {
  458. case FB_BLANK_POWERDOWN:
  459. case FB_BLANK_VSYNC_SUSPEND:
  460. case FB_BLANK_HSYNC_SUSPEND:
  461. case FB_BLANK_NORMAL:
  462. if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  463. fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  464. for (i = 0; i < fbi->palette_size; i++)
  465. pxafb_setpalettereg(i, 0, 0, 0, 0, info);
  466. pxafb_schedule_work(fbi, C_DISABLE);
  467. /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
  468. break;
  469. case FB_BLANK_UNBLANK:
  470. /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
  471. if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  472. fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  473. fb_set_cmap(&fbi->fb.cmap, info);
  474. pxafb_schedule_work(fbi, C_ENABLE);
  475. }
  476. return 0;
  477. }
  478. static struct fb_ops pxafb_ops = {
  479. .owner = THIS_MODULE,
  480. .fb_check_var = pxafb_check_var,
  481. .fb_set_par = pxafb_set_par,
  482. .fb_pan_display = pxafb_pan_display,
  483. .fb_setcolreg = pxafb_setcolreg,
  484. .fb_fillrect = cfb_fillrect,
  485. .fb_copyarea = cfb_copyarea,
  486. .fb_imageblit = cfb_imageblit,
  487. .fb_blank = pxafb_blank,
  488. };
  489. /*
  490. * Calculate the PCD value from the clock rate (in picoseconds).
  491. * We take account of the PPCR clock setting.
  492. * From PXA Developer's Manual:
  493. *
  494. * PixelClock = LCLK
  495. * -------------
  496. * 2 ( PCD + 1 )
  497. *
  498. * PCD = LCLK
  499. * ------------- - 1
  500. * 2(PixelClock)
  501. *
  502. * Where:
  503. * LCLK = LCD/Memory Clock
  504. * PCD = LCCR3[7:0]
  505. *
  506. * PixelClock here is in Hz while the pixclock argument given is the
  507. * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
  508. *
  509. * The function get_lclk_frequency_10khz returns LCLK in units of
  510. * 10khz. Calling the result of this function lclk gives us the
  511. * following
  512. *
  513. * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
  514. * -------------------------------------- - 1
  515. * 2
  516. *
  517. * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
  518. */
  519. static inline unsigned int get_pcd(struct pxafb_info *fbi,
  520. unsigned int pixclock)
  521. {
  522. unsigned long long pcd;
  523. /* FIXME: Need to take into account Double Pixel Clock mode
  524. * (DPC) bit? or perhaps set it based on the various clock
  525. * speeds */
  526. pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
  527. pcd *= pixclock;
  528. do_div(pcd, 100000000 * 2);
  529. /* no need for this, since we should subtract 1 anyway. they cancel */
  530. /* pcd += 1; */ /* make up for integer math truncations */
  531. return (unsigned int)pcd;
  532. }
  533. /*
  534. * Some touchscreens need hsync information from the video driver to
  535. * function correctly. We export it here. Note that 'hsync_time' and
  536. * the value returned from pxafb_get_hsync_time() is the *reciprocal*
  537. * of the hsync period in seconds.
  538. */
  539. static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
  540. {
  541. unsigned long htime;
  542. if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
  543. fbi->hsync_time = 0;
  544. return;
  545. }
  546. htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
  547. fbi->hsync_time = htime;
  548. }
  549. unsigned long pxafb_get_hsync_time(struct device *dev)
  550. {
  551. struct pxafb_info *fbi = dev_get_drvdata(dev);
  552. /* If display is blanked/suspended, hsync isn't active */
  553. if (!fbi || (fbi->state != C_ENABLE))
  554. return 0;
  555. return fbi->hsync_time;
  556. }
  557. EXPORT_SYMBOL(pxafb_get_hsync_time);
  558. static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
  559. unsigned int offset, size_t size)
  560. {
  561. struct pxafb_dma_descriptor *dma_desc, *pal_desc;
  562. unsigned int dma_desc_off, pal_desc_off;
  563. if (dma < 0 || dma >= DMA_MAX * 2)
  564. return -EINVAL;
  565. dma_desc = &fbi->dma_buff->dma_desc[dma];
  566. dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
  567. dma_desc->fsadr = fbi->video_mem_phys + offset;
  568. dma_desc->fidr = 0;
  569. dma_desc->ldcmd = size;
  570. if (pal < 0 || pal >= PAL_MAX * 2) {
  571. dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  572. fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
  573. } else {
  574. pal_desc = &fbi->dma_buff->pal_desc[pal];
  575. pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
  576. pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
  577. pal_desc->fidr = 0;
  578. if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
  579. pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
  580. else
  581. pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
  582. pal_desc->ldcmd |= LDCMD_PAL;
  583. /* flip back and forth between palette and frame buffer */
  584. pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  585. dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
  586. fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
  587. }
  588. return 0;
  589. }
  590. static void setup_base_frame(struct pxafb_info *fbi, int branch)
  591. {
  592. struct fb_var_screeninfo *var = &fbi->fb.var;
  593. struct fb_fix_screeninfo *fix = &fbi->fb.fix;
  594. unsigned int nbytes, offset;
  595. int dma, pal, bpp = var->bits_per_pixel;
  596. dma = DMA_BASE + (branch ? DMA_MAX : 0);
  597. pal = (bpp >= 16) ? PAL_NONE : PAL_BASE + (branch ? PAL_MAX : 0);
  598. nbytes = fix->line_length * var->yres;
  599. offset = fix->line_length * var->yoffset;
  600. if (fbi->lccr0 & LCCR0_SDS) {
  601. nbytes = nbytes / 2;
  602. setup_frame_dma(fbi, dma + 1, PAL_NONE, offset + nbytes, nbytes);
  603. }
  604. setup_frame_dma(fbi, dma, pal, offset, nbytes);
  605. }
  606. #ifdef CONFIG_FB_PXA_SMARTPANEL
  607. static int setup_smart_dma(struct pxafb_info *fbi)
  608. {
  609. struct pxafb_dma_descriptor *dma_desc;
  610. unsigned long dma_desc_off, cmd_buff_off;
  611. dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
  612. dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
  613. cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
  614. dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  615. dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
  616. dma_desc->fidr = 0;
  617. dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
  618. fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
  619. return 0;
  620. }
  621. int pxafb_smart_flush(struct fb_info *info)
  622. {
  623. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  624. uint32_t prsr;
  625. int ret = 0;
  626. /* disable controller until all registers are set up */
  627. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  628. /* 1. make it an even number of commands to align on 32-bit boundary
  629. * 2. add the interrupt command to the end of the chain so we can
  630. * keep track of the end of the transfer
  631. */
  632. while (fbi->n_smart_cmds & 1)
  633. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
  634. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
  635. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
  636. setup_smart_dma(fbi);
  637. /* continue to execute next command */
  638. prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
  639. lcd_writel(fbi, PRSR, prsr);
  640. /* stop the processor in case it executed "wait for sync" cmd */
  641. lcd_writel(fbi, CMDCR, 0x0001);
  642. /* don't send interrupts for fifo underruns on channel 6 */
  643. lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
  644. lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
  645. lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
  646. lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
  647. lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
  648. lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
  649. lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
  650. /* begin sending */
  651. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
  652. if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
  653. pr_warning("%s: timeout waiting for command done\n",
  654. __func__);
  655. ret = -ETIMEDOUT;
  656. }
  657. /* quick disable */
  658. prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
  659. lcd_writel(fbi, PRSR, prsr);
  660. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  661. lcd_writel(fbi, FDADR6, 0);
  662. fbi->n_smart_cmds = 0;
  663. return ret;
  664. }
  665. int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
  666. {
  667. int i;
  668. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  669. for (i = 0; i < n_cmds; i++, cmds++) {
  670. /* if it is a software delay, flush and delay */
  671. if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
  672. pxafb_smart_flush(info);
  673. mdelay(*cmds & 0xff);
  674. continue;
  675. }
  676. /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
  677. if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
  678. pxafb_smart_flush(info);
  679. fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
  680. }
  681. return 0;
  682. }
  683. static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
  684. {
  685. unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
  686. return (t == 0) ? 1 : t;
  687. }
  688. static void setup_smart_timing(struct pxafb_info *fbi,
  689. struct fb_var_screeninfo *var)
  690. {
  691. struct pxafb_mach_info *inf = fbi->dev->platform_data;
  692. struct pxafb_mode_info *mode = &inf->modes[0];
  693. unsigned long lclk = clk_get_rate(fbi->clk);
  694. unsigned t1, t2, t3, t4;
  695. t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
  696. t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
  697. t3 = mode->op_hold_time;
  698. t4 = mode->cmd_inh_time;
  699. fbi->reg_lccr1 =
  700. LCCR1_DisWdth(var->xres) |
  701. LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
  702. LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
  703. LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
  704. fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
  705. fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
  706. fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
  707. fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
  708. /* FIXME: make this configurable */
  709. fbi->reg_cmdcr = 1;
  710. }
  711. static int pxafb_smart_thread(void *arg)
  712. {
  713. struct pxafb_info *fbi = arg;
  714. struct pxafb_mach_info *inf = fbi->dev->platform_data;
  715. if (!fbi || !inf->smart_update) {
  716. pr_err("%s: not properly initialized, thread terminated\n",
  717. __func__);
  718. return -EINVAL;
  719. }
  720. pr_debug("%s(): task starting\n", __func__);
  721. set_freezable();
  722. while (!kthread_should_stop()) {
  723. if (try_to_freeze())
  724. continue;
  725. mutex_lock(&fbi->ctrlr_lock);
  726. if (fbi->state == C_ENABLE) {
  727. inf->smart_update(&fbi->fb);
  728. complete(&fbi->refresh_done);
  729. }
  730. mutex_unlock(&fbi->ctrlr_lock);
  731. set_current_state(TASK_INTERRUPTIBLE);
  732. schedule_timeout(30 * HZ / 1000);
  733. }
  734. pr_debug("%s(): task ending\n", __func__);
  735. return 0;
  736. }
  737. static int pxafb_smart_init(struct pxafb_info *fbi)
  738. {
  739. if (!(fbi->lccr0 & LCCR0_LCDT))
  740. return 0;
  741. fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
  742. fbi->n_smart_cmds = 0;
  743. init_completion(&fbi->command_done);
  744. init_completion(&fbi->refresh_done);
  745. fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
  746. "lcd_refresh");
  747. if (IS_ERR(fbi->smart_thread)) {
  748. pr_err("%s: unable to create kernel thread\n", __func__);
  749. return PTR_ERR(fbi->smart_thread);
  750. }
  751. return 0;
  752. }
  753. #else
  754. int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
  755. {
  756. return 0;
  757. }
  758. int pxafb_smart_flush(struct fb_info *info)
  759. {
  760. return 0;
  761. }
  762. static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
  763. #endif /* CONFIG_FB_PXA_SMARTPANEL */
  764. static void setup_parallel_timing(struct pxafb_info *fbi,
  765. struct fb_var_screeninfo *var)
  766. {
  767. unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
  768. fbi->reg_lccr1 =
  769. LCCR1_DisWdth(var->xres) +
  770. LCCR1_HorSnchWdth(var->hsync_len) +
  771. LCCR1_BegLnDel(var->left_margin) +
  772. LCCR1_EndLnDel(var->right_margin);
  773. /*
  774. * If we have a dual scan LCD, we need to halve
  775. * the YRES parameter.
  776. */
  777. lines_per_panel = var->yres;
  778. if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
  779. lines_per_panel /= 2;
  780. fbi->reg_lccr2 =
  781. LCCR2_DisHght(lines_per_panel) +
  782. LCCR2_VrtSnchWdth(var->vsync_len) +
  783. LCCR2_BegFrmDel(var->upper_margin) +
  784. LCCR2_EndFrmDel(var->lower_margin);
  785. fbi->reg_lccr3 = fbi->lccr3 |
  786. (var->sync & FB_SYNC_HOR_HIGH_ACT ?
  787. LCCR3_HorSnchH : LCCR3_HorSnchL) |
  788. (var->sync & FB_SYNC_VERT_HIGH_ACT ?
  789. LCCR3_VrtSnchH : LCCR3_VrtSnchL);
  790. if (pcd) {
  791. fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
  792. set_hsync_time(fbi, pcd);
  793. }
  794. }
  795. /*
  796. * pxafb_activate_var():
  797. * Configures LCD Controller based on entries in var parameter.
  798. * Settings are only written to the controller if changes were made.
  799. */
  800. static int pxafb_activate_var(struct fb_var_screeninfo *var,
  801. struct pxafb_info *fbi)
  802. {
  803. u_long flags;
  804. #if DEBUG_VAR
  805. if (!(fbi->lccr0 & LCCR0_LCDT)) {
  806. if (var->xres < 16 || var->xres > 1024)
  807. printk(KERN_ERR "%s: invalid xres %d\n",
  808. fbi->fb.fix.id, var->xres);
  809. switch (var->bits_per_pixel) {
  810. case 1:
  811. case 2:
  812. case 4:
  813. case 8:
  814. case 16:
  815. case 24:
  816. case 32:
  817. break;
  818. default:
  819. printk(KERN_ERR "%s: invalid bit depth %d\n",
  820. fbi->fb.fix.id, var->bits_per_pixel);
  821. break;
  822. }
  823. if (var->hsync_len < 1 || var->hsync_len > 64)
  824. printk(KERN_ERR "%s: invalid hsync_len %d\n",
  825. fbi->fb.fix.id, var->hsync_len);
  826. if (var->left_margin < 1 || var->left_margin > 255)
  827. printk(KERN_ERR "%s: invalid left_margin %d\n",
  828. fbi->fb.fix.id, var->left_margin);
  829. if (var->right_margin < 1 || var->right_margin > 255)
  830. printk(KERN_ERR "%s: invalid right_margin %d\n",
  831. fbi->fb.fix.id, var->right_margin);
  832. if (var->yres < 1 || var->yres > 1024)
  833. printk(KERN_ERR "%s: invalid yres %d\n",
  834. fbi->fb.fix.id, var->yres);
  835. if (var->vsync_len < 1 || var->vsync_len > 64)
  836. printk(KERN_ERR "%s: invalid vsync_len %d\n",
  837. fbi->fb.fix.id, var->vsync_len);
  838. if (var->upper_margin < 0 || var->upper_margin > 255)
  839. printk(KERN_ERR "%s: invalid upper_margin %d\n",
  840. fbi->fb.fix.id, var->upper_margin);
  841. if (var->lower_margin < 0 || var->lower_margin > 255)
  842. printk(KERN_ERR "%s: invalid lower_margin %d\n",
  843. fbi->fb.fix.id, var->lower_margin);
  844. }
  845. #endif
  846. /* Update shadow copy atomically */
  847. local_irq_save(flags);
  848. #ifdef CONFIG_FB_PXA_SMARTPANEL
  849. if (fbi->lccr0 & LCCR0_LCDT)
  850. setup_smart_timing(fbi, var);
  851. else
  852. #endif
  853. setup_parallel_timing(fbi, var);
  854. setup_base_frame(fbi, 0);
  855. fbi->reg_lccr0 = fbi->lccr0 |
  856. (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
  857. LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
  858. fbi->reg_lccr3 |= pxafb_var_to_lccr3(var);
  859. fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
  860. fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
  861. local_irq_restore(flags);
  862. /*
  863. * Only update the registers if the controller is enabled
  864. * and something has changed.
  865. */
  866. if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
  867. (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
  868. (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
  869. (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
  870. (lcd_readl(fbi, LCCR4) != fbi->reg_lccr4) ||
  871. (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
  872. (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
  873. pxafb_schedule_work(fbi, C_REENABLE);
  874. return 0;
  875. }
  876. /*
  877. * NOTE! The following functions are purely helpers for set_ctrlr_state.
  878. * Do not call them directly; set_ctrlr_state does the correct serialisation
  879. * to ensure that things happen in the right way 100% of time time.
  880. * -- rmk
  881. */
  882. static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
  883. {
  884. pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
  885. if (fbi->backlight_power)
  886. fbi->backlight_power(on);
  887. }
  888. static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
  889. {
  890. pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
  891. if (fbi->lcd_power)
  892. fbi->lcd_power(on, &fbi->fb.var);
  893. }
  894. static void pxafb_enable_controller(struct pxafb_info *fbi)
  895. {
  896. pr_debug("pxafb: Enabling LCD controller\n");
  897. pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
  898. pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
  899. pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
  900. pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
  901. pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
  902. pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
  903. /* enable LCD controller clock */
  904. clk_enable(fbi->clk);
  905. if (fbi->lccr0 & LCCR0_LCDT)
  906. return;
  907. /* Sequence from 11.7.10 */
  908. lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
  909. lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
  910. lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
  911. lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
  912. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  913. lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
  914. lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
  915. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
  916. }
  917. static void pxafb_disable_controller(struct pxafb_info *fbi)
  918. {
  919. uint32_t lccr0;
  920. #ifdef CONFIG_FB_PXA_SMARTPANEL
  921. if (fbi->lccr0 & LCCR0_LCDT) {
  922. wait_for_completion_timeout(&fbi->refresh_done,
  923. 200 * HZ / 1000);
  924. return;
  925. }
  926. #endif
  927. /* Clear LCD Status Register */
  928. lcd_writel(fbi, LCSR, 0xffffffff);
  929. lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
  930. lcd_writel(fbi, LCCR0, lccr0);
  931. lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
  932. wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
  933. /* disable LCD controller clock */
  934. clk_disable(fbi->clk);
  935. }
  936. /*
  937. * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
  938. */
  939. static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
  940. {
  941. struct pxafb_info *fbi = dev_id;
  942. unsigned int lccr0, lcsr = lcd_readl(fbi, LCSR);
  943. if (lcsr & LCSR_LDD) {
  944. lccr0 = lcd_readl(fbi, LCCR0);
  945. lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
  946. complete(&fbi->disable_done);
  947. }
  948. #ifdef CONFIG_FB_PXA_SMARTPANEL
  949. if (lcsr & LCSR_CMD_INT)
  950. complete(&fbi->command_done);
  951. #endif
  952. lcd_writel(fbi, LCSR, lcsr);
  953. return IRQ_HANDLED;
  954. }
  955. /*
  956. * This function must be called from task context only, since it will
  957. * sleep when disabling the LCD controller, or if we get two contending
  958. * processes trying to alter state.
  959. */
  960. static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
  961. {
  962. u_int old_state;
  963. mutex_lock(&fbi->ctrlr_lock);
  964. old_state = fbi->state;
  965. /*
  966. * Hack around fbcon initialisation.
  967. */
  968. if (old_state == C_STARTUP && state == C_REENABLE)
  969. state = C_ENABLE;
  970. switch (state) {
  971. case C_DISABLE_CLKCHANGE:
  972. /*
  973. * Disable controller for clock change. If the
  974. * controller is already disabled, then do nothing.
  975. */
  976. if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
  977. fbi->state = state;
  978. /* TODO __pxafb_lcd_power(fbi, 0); */
  979. pxafb_disable_controller(fbi);
  980. }
  981. break;
  982. case C_DISABLE_PM:
  983. case C_DISABLE:
  984. /*
  985. * Disable controller
  986. */
  987. if (old_state != C_DISABLE) {
  988. fbi->state = state;
  989. __pxafb_backlight_power(fbi, 0);
  990. __pxafb_lcd_power(fbi, 0);
  991. if (old_state != C_DISABLE_CLKCHANGE)
  992. pxafb_disable_controller(fbi);
  993. }
  994. break;
  995. case C_ENABLE_CLKCHANGE:
  996. /*
  997. * Enable the controller after clock change. Only
  998. * do this if we were disabled for the clock change.
  999. */
  1000. if (old_state == C_DISABLE_CLKCHANGE) {
  1001. fbi->state = C_ENABLE;
  1002. pxafb_enable_controller(fbi);
  1003. /* TODO __pxafb_lcd_power(fbi, 1); */
  1004. }
  1005. break;
  1006. case C_REENABLE:
  1007. /*
  1008. * Re-enable the controller only if it was already
  1009. * enabled. This is so we reprogram the control
  1010. * registers.
  1011. */
  1012. if (old_state == C_ENABLE) {
  1013. __pxafb_lcd_power(fbi, 0);
  1014. pxafb_disable_controller(fbi);
  1015. pxafb_enable_controller(fbi);
  1016. __pxafb_lcd_power(fbi, 1);
  1017. }
  1018. break;
  1019. case C_ENABLE_PM:
  1020. /*
  1021. * Re-enable the controller after PM. This is not
  1022. * perfect - think about the case where we were doing
  1023. * a clock change, and we suspended half-way through.
  1024. */
  1025. if (old_state != C_DISABLE_PM)
  1026. break;
  1027. /* fall through */
  1028. case C_ENABLE:
  1029. /*
  1030. * Power up the LCD screen, enable controller, and
  1031. * turn on the backlight.
  1032. */
  1033. if (old_state != C_ENABLE) {
  1034. fbi->state = C_ENABLE;
  1035. pxafb_enable_controller(fbi);
  1036. __pxafb_lcd_power(fbi, 1);
  1037. __pxafb_backlight_power(fbi, 1);
  1038. }
  1039. break;
  1040. }
  1041. mutex_unlock(&fbi->ctrlr_lock);
  1042. }
  1043. /*
  1044. * Our LCD controller task (which is called when we blank or unblank)
  1045. * via keventd.
  1046. */
  1047. static void pxafb_task(struct work_struct *work)
  1048. {
  1049. struct pxafb_info *fbi =
  1050. container_of(work, struct pxafb_info, task);
  1051. u_int state = xchg(&fbi->task_state, -1);
  1052. set_ctrlr_state(fbi, state);
  1053. }
  1054. #ifdef CONFIG_CPU_FREQ
  1055. /*
  1056. * CPU clock speed change handler. We need to adjust the LCD timing
  1057. * parameters when the CPU clock is adjusted by the power management
  1058. * subsystem.
  1059. *
  1060. * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
  1061. */
  1062. static int
  1063. pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
  1064. {
  1065. struct pxafb_info *fbi = TO_INF(nb, freq_transition);
  1066. /* TODO struct cpufreq_freqs *f = data; */
  1067. u_int pcd;
  1068. switch (val) {
  1069. case CPUFREQ_PRECHANGE:
  1070. set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
  1071. break;
  1072. case CPUFREQ_POSTCHANGE:
  1073. pcd = get_pcd(fbi, fbi->fb.var.pixclock);
  1074. set_hsync_time(fbi, pcd);
  1075. fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
  1076. LCCR3_PixClkDiv(pcd);
  1077. set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
  1078. break;
  1079. }
  1080. return 0;
  1081. }
  1082. static int
  1083. pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
  1084. {
  1085. struct pxafb_info *fbi = TO_INF(nb, freq_policy);
  1086. struct fb_var_screeninfo *var = &fbi->fb.var;
  1087. struct cpufreq_policy *policy = data;
  1088. switch (val) {
  1089. case CPUFREQ_ADJUST:
  1090. case CPUFREQ_INCOMPATIBLE:
  1091. pr_debug("min dma period: %d ps, "
  1092. "new clock %d kHz\n", pxafb_display_dma_period(var),
  1093. policy->max);
  1094. /* TODO: fill in min/max values */
  1095. break;
  1096. }
  1097. return 0;
  1098. }
  1099. #endif
  1100. #ifdef CONFIG_PM
  1101. /*
  1102. * Power management hooks. Note that we won't be called from IRQ context,
  1103. * unlike the blank functions above, so we may sleep.
  1104. */
  1105. static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
  1106. {
  1107. struct pxafb_info *fbi = platform_get_drvdata(dev);
  1108. set_ctrlr_state(fbi, C_DISABLE_PM);
  1109. return 0;
  1110. }
  1111. static int pxafb_resume(struct platform_device *dev)
  1112. {
  1113. struct pxafb_info *fbi = platform_get_drvdata(dev);
  1114. set_ctrlr_state(fbi, C_ENABLE_PM);
  1115. return 0;
  1116. }
  1117. #else
  1118. #define pxafb_suspend NULL
  1119. #define pxafb_resume NULL
  1120. #endif
  1121. static int __devinit pxafb_init_video_memory(struct pxafb_info *fbi)
  1122. {
  1123. int size = PAGE_ALIGN(fbi->video_mem_size);
  1124. fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  1125. if (fbi->video_mem == NULL)
  1126. return -ENOMEM;
  1127. fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
  1128. fbi->video_mem_size = size;
  1129. fbi->fb.fix.smem_start = fbi->video_mem_phys;
  1130. fbi->fb.fix.smem_len = fbi->video_mem_size;
  1131. fbi->fb.screen_base = fbi->video_mem;
  1132. return fbi->video_mem ? 0 : -ENOMEM;
  1133. }
  1134. static void pxafb_decode_mach_info(struct pxafb_info *fbi,
  1135. struct pxafb_mach_info *inf)
  1136. {
  1137. unsigned int lcd_conn = inf->lcd_conn;
  1138. struct pxafb_mode_info *m;
  1139. int i;
  1140. fbi->cmap_inverse = inf->cmap_inverse;
  1141. fbi->cmap_static = inf->cmap_static;
  1142. fbi->lccr4 = inf->lccr4;
  1143. switch (lcd_conn & LCD_TYPE_MASK) {
  1144. case LCD_TYPE_MONO_STN:
  1145. fbi->lccr0 = LCCR0_CMS;
  1146. break;
  1147. case LCD_TYPE_MONO_DSTN:
  1148. fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
  1149. break;
  1150. case LCD_TYPE_COLOR_STN:
  1151. fbi->lccr0 = 0;
  1152. break;
  1153. case LCD_TYPE_COLOR_DSTN:
  1154. fbi->lccr0 = LCCR0_SDS;
  1155. break;
  1156. case LCD_TYPE_COLOR_TFT:
  1157. fbi->lccr0 = LCCR0_PAS;
  1158. break;
  1159. case LCD_TYPE_SMART_PANEL:
  1160. fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
  1161. break;
  1162. default:
  1163. /* fall back to backward compatibility way */
  1164. fbi->lccr0 = inf->lccr0;
  1165. fbi->lccr3 = inf->lccr3;
  1166. goto decode_mode;
  1167. }
  1168. if (lcd_conn == LCD_MONO_STN_8BPP)
  1169. fbi->lccr0 |= LCCR0_DPD;
  1170. fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
  1171. fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
  1172. fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
  1173. fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
  1174. decode_mode:
  1175. pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
  1176. /* decide video memory size as follows:
  1177. * 1. default to mode of maximum resolution
  1178. * 2. allow platform to override
  1179. * 3. allow module parameter to override
  1180. */
  1181. for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
  1182. fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
  1183. m->xres * m->yres * m->bpp / 8);
  1184. if (inf->video_mem_size > fbi->video_mem_size)
  1185. fbi->video_mem_size = inf->video_mem_size;
  1186. if (video_mem_size > fbi->video_mem_size)
  1187. fbi->video_mem_size = video_mem_size;
  1188. }
  1189. static struct pxafb_info * __devinit pxafb_init_fbinfo(struct device *dev)
  1190. {
  1191. struct pxafb_info *fbi;
  1192. void *addr;
  1193. struct pxafb_mach_info *inf = dev->platform_data;
  1194. /* Alloc the pxafb_info and pseudo_palette in one step */
  1195. fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
  1196. if (!fbi)
  1197. return NULL;
  1198. memset(fbi, 0, sizeof(struct pxafb_info));
  1199. fbi->dev = dev;
  1200. fbi->clk = clk_get(dev, "LCDCLK");
  1201. if (IS_ERR(fbi->clk)) {
  1202. kfree(fbi);
  1203. return NULL;
  1204. }
  1205. strcpy(fbi->fb.fix.id, PXA_NAME);
  1206. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  1207. fbi->fb.fix.type_aux = 0;
  1208. fbi->fb.fix.xpanstep = 0;
  1209. fbi->fb.fix.ypanstep = 1;
  1210. fbi->fb.fix.ywrapstep = 0;
  1211. fbi->fb.fix.accel = FB_ACCEL_NONE;
  1212. fbi->fb.var.nonstd = 0;
  1213. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  1214. fbi->fb.var.height = -1;
  1215. fbi->fb.var.width = -1;
  1216. fbi->fb.var.accel_flags = FB_ACCELF_TEXT;
  1217. fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
  1218. fbi->fb.fbops = &pxafb_ops;
  1219. fbi->fb.flags = FBINFO_DEFAULT;
  1220. fbi->fb.node = -1;
  1221. addr = fbi;
  1222. addr = addr + sizeof(struct pxafb_info);
  1223. fbi->fb.pseudo_palette = addr;
  1224. fbi->state = C_STARTUP;
  1225. fbi->task_state = (u_char)-1;
  1226. pxafb_decode_mach_info(fbi, inf);
  1227. init_waitqueue_head(&fbi->ctrlr_wait);
  1228. INIT_WORK(&fbi->task, pxafb_task);
  1229. mutex_init(&fbi->ctrlr_lock);
  1230. init_completion(&fbi->disable_done);
  1231. return fbi;
  1232. }
  1233. #ifdef CONFIG_FB_PXA_PARAMETERS
  1234. static int __devinit parse_opt_mode(struct device *dev, const char *this_opt)
  1235. {
  1236. struct pxafb_mach_info *inf = dev->platform_data;
  1237. const char *name = this_opt+5;
  1238. unsigned int namelen = strlen(name);
  1239. int res_specified = 0, bpp_specified = 0;
  1240. unsigned int xres = 0, yres = 0, bpp = 0;
  1241. int yres_specified = 0;
  1242. int i;
  1243. for (i = namelen-1; i >= 0; i--) {
  1244. switch (name[i]) {
  1245. case '-':
  1246. namelen = i;
  1247. if (!bpp_specified && !yres_specified) {
  1248. bpp = simple_strtoul(&name[i+1], NULL, 0);
  1249. bpp_specified = 1;
  1250. } else
  1251. goto done;
  1252. break;
  1253. case 'x':
  1254. if (!yres_specified) {
  1255. yres = simple_strtoul(&name[i+1], NULL, 0);
  1256. yres_specified = 1;
  1257. } else
  1258. goto done;
  1259. break;
  1260. case '0' ... '9':
  1261. break;
  1262. default:
  1263. goto done;
  1264. }
  1265. }
  1266. if (i < 0 && yres_specified) {
  1267. xres = simple_strtoul(name, NULL, 0);
  1268. res_specified = 1;
  1269. }
  1270. done:
  1271. if (res_specified) {
  1272. dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
  1273. inf->modes[0].xres = xres; inf->modes[0].yres = yres;
  1274. }
  1275. if (bpp_specified)
  1276. switch (bpp) {
  1277. case 1:
  1278. case 2:
  1279. case 4:
  1280. case 8:
  1281. case 16:
  1282. inf->modes[0].bpp = bpp;
  1283. dev_info(dev, "overriding bit depth: %d\n", bpp);
  1284. break;
  1285. default:
  1286. dev_err(dev, "Depth %d is not valid\n", bpp);
  1287. return -EINVAL;
  1288. }
  1289. return 0;
  1290. }
  1291. static int __devinit parse_opt(struct device *dev, char *this_opt)
  1292. {
  1293. struct pxafb_mach_info *inf = dev->platform_data;
  1294. struct pxafb_mode_info *mode = &inf->modes[0];
  1295. char s[64];
  1296. s[0] = '\0';
  1297. if (!strncmp(this_opt, "vmem:", 5)) {
  1298. video_mem_size = memparse(this_opt + 5, NULL);
  1299. } else if (!strncmp(this_opt, "mode:", 5)) {
  1300. return parse_opt_mode(dev, this_opt);
  1301. } else if (!strncmp(this_opt, "pixclock:", 9)) {
  1302. mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
  1303. sprintf(s, "pixclock: %ld\n", mode->pixclock);
  1304. } else if (!strncmp(this_opt, "left:", 5)) {
  1305. mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
  1306. sprintf(s, "left: %u\n", mode->left_margin);
  1307. } else if (!strncmp(this_opt, "right:", 6)) {
  1308. mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
  1309. sprintf(s, "right: %u\n", mode->right_margin);
  1310. } else if (!strncmp(this_opt, "upper:", 6)) {
  1311. mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
  1312. sprintf(s, "upper: %u\n", mode->upper_margin);
  1313. } else if (!strncmp(this_opt, "lower:", 6)) {
  1314. mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
  1315. sprintf(s, "lower: %u\n", mode->lower_margin);
  1316. } else if (!strncmp(this_opt, "hsynclen:", 9)) {
  1317. mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
  1318. sprintf(s, "hsynclen: %u\n", mode->hsync_len);
  1319. } else if (!strncmp(this_opt, "vsynclen:", 9)) {
  1320. mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
  1321. sprintf(s, "vsynclen: %u\n", mode->vsync_len);
  1322. } else if (!strncmp(this_opt, "hsync:", 6)) {
  1323. if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
  1324. sprintf(s, "hsync: Active Low\n");
  1325. mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
  1326. } else {
  1327. sprintf(s, "hsync: Active High\n");
  1328. mode->sync |= FB_SYNC_HOR_HIGH_ACT;
  1329. }
  1330. } else if (!strncmp(this_opt, "vsync:", 6)) {
  1331. if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
  1332. sprintf(s, "vsync: Active Low\n");
  1333. mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
  1334. } else {
  1335. sprintf(s, "vsync: Active High\n");
  1336. mode->sync |= FB_SYNC_VERT_HIGH_ACT;
  1337. }
  1338. } else if (!strncmp(this_opt, "dpc:", 4)) {
  1339. if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
  1340. sprintf(s, "double pixel clock: false\n");
  1341. inf->lccr3 &= ~LCCR3_DPC;
  1342. } else {
  1343. sprintf(s, "double pixel clock: true\n");
  1344. inf->lccr3 |= LCCR3_DPC;
  1345. }
  1346. } else if (!strncmp(this_opt, "outputen:", 9)) {
  1347. if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
  1348. sprintf(s, "output enable: active low\n");
  1349. inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
  1350. } else {
  1351. sprintf(s, "output enable: active high\n");
  1352. inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
  1353. }
  1354. } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
  1355. if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
  1356. sprintf(s, "pixel clock polarity: falling edge\n");
  1357. inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
  1358. } else {
  1359. sprintf(s, "pixel clock polarity: rising edge\n");
  1360. inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
  1361. }
  1362. } else if (!strncmp(this_opt, "color", 5)) {
  1363. inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
  1364. } else if (!strncmp(this_opt, "mono", 4)) {
  1365. inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
  1366. } else if (!strncmp(this_opt, "active", 6)) {
  1367. inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
  1368. } else if (!strncmp(this_opt, "passive", 7)) {
  1369. inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
  1370. } else if (!strncmp(this_opt, "single", 6)) {
  1371. inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
  1372. } else if (!strncmp(this_opt, "dual", 4)) {
  1373. inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
  1374. } else if (!strncmp(this_opt, "4pix", 4)) {
  1375. inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
  1376. } else if (!strncmp(this_opt, "8pix", 4)) {
  1377. inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
  1378. } else {
  1379. dev_err(dev, "unknown option: %s\n", this_opt);
  1380. return -EINVAL;
  1381. }
  1382. if (s[0] != '\0')
  1383. dev_info(dev, "override %s", s);
  1384. return 0;
  1385. }
  1386. static int __devinit pxafb_parse_options(struct device *dev, char *options)
  1387. {
  1388. char *this_opt;
  1389. int ret;
  1390. if (!options || !*options)
  1391. return 0;
  1392. dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
  1393. /* could be made table driven or similar?... */
  1394. while ((this_opt = strsep(&options, ",")) != NULL) {
  1395. ret = parse_opt(dev, this_opt);
  1396. if (ret)
  1397. return ret;
  1398. }
  1399. return 0;
  1400. }
  1401. static char g_options[256] __devinitdata = "";
  1402. #ifndef MODULE
  1403. static int __init pxafb_setup_options(void)
  1404. {
  1405. char *options = NULL;
  1406. if (fb_get_options("pxafb", &options))
  1407. return -ENODEV;
  1408. if (options)
  1409. strlcpy(g_options, options, sizeof(g_options));
  1410. return 0;
  1411. }
  1412. #else
  1413. #define pxafb_setup_options() (0)
  1414. module_param_string(options, g_options, sizeof(g_options), 0);
  1415. MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
  1416. #endif
  1417. #else
  1418. #define pxafb_parse_options(...) (0)
  1419. #define pxafb_setup_options() (0)
  1420. #endif
  1421. #ifdef DEBUG_VAR
  1422. /* Check for various illegal bit-combinations. Currently only
  1423. * a warning is given. */
  1424. static void __devinit pxafb_check_options(struct device *dev,
  1425. struct pxafb_mach_info *inf)
  1426. {
  1427. if (inf->lcd_conn)
  1428. return;
  1429. if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
  1430. dev_warn(dev, "machine LCCR0 setting contains "
  1431. "illegal bits: %08x\n",
  1432. inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
  1433. if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
  1434. dev_warn(dev, "machine LCCR3 setting contains "
  1435. "illegal bits: %08x\n",
  1436. inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
  1437. if (inf->lccr0 & LCCR0_DPD &&
  1438. ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
  1439. (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
  1440. (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
  1441. dev_warn(dev, "Double Pixel Data (DPD) mode is "
  1442. "only valid in passive mono"
  1443. " single panel mode\n");
  1444. if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
  1445. (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
  1446. dev_warn(dev, "Dual panel only valid in passive mode\n");
  1447. if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
  1448. (inf->modes->upper_margin || inf->modes->lower_margin))
  1449. dev_warn(dev, "Upper and lower margins must be 0 in "
  1450. "passive mode\n");
  1451. }
  1452. #else
  1453. #define pxafb_check_options(...) do {} while (0)
  1454. #endif
  1455. static int __devinit pxafb_probe(struct platform_device *dev)
  1456. {
  1457. struct pxafb_info *fbi;
  1458. struct pxafb_mach_info *inf;
  1459. struct resource *r;
  1460. int irq, ret;
  1461. dev_dbg(&dev->dev, "pxafb_probe\n");
  1462. inf = dev->dev.platform_data;
  1463. ret = -ENOMEM;
  1464. fbi = NULL;
  1465. if (!inf)
  1466. goto failed;
  1467. ret = pxafb_parse_options(&dev->dev, g_options);
  1468. if (ret < 0)
  1469. goto failed;
  1470. pxafb_check_options(&dev->dev, inf);
  1471. dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
  1472. inf->modes->xres,
  1473. inf->modes->yres,
  1474. inf->modes->bpp);
  1475. if (inf->modes->xres == 0 ||
  1476. inf->modes->yres == 0 ||
  1477. inf->modes->bpp == 0) {
  1478. dev_err(&dev->dev, "Invalid resolution or bit depth\n");
  1479. ret = -EINVAL;
  1480. goto failed;
  1481. }
  1482. fbi = pxafb_init_fbinfo(&dev->dev);
  1483. if (!fbi) {
  1484. /* only reason for pxafb_init_fbinfo to fail is kmalloc */
  1485. dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
  1486. ret = -ENOMEM;
  1487. goto failed;
  1488. }
  1489. fbi->backlight_power = inf->pxafb_backlight_power;
  1490. fbi->lcd_power = inf->pxafb_lcd_power;
  1491. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1492. if (r == NULL) {
  1493. dev_err(&dev->dev, "no I/O memory resource defined\n");
  1494. ret = -ENODEV;
  1495. goto failed_fbi;
  1496. }
  1497. r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
  1498. if (r == NULL) {
  1499. dev_err(&dev->dev, "failed to request I/O memory\n");
  1500. ret = -EBUSY;
  1501. goto failed_fbi;
  1502. }
  1503. fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
  1504. if (fbi->mmio_base == NULL) {
  1505. dev_err(&dev->dev, "failed to map I/O memory\n");
  1506. ret = -EBUSY;
  1507. goto failed_free_res;
  1508. }
  1509. fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
  1510. fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
  1511. &fbi->dma_buff_phys, GFP_KERNEL);
  1512. if (fbi->dma_buff == NULL) {
  1513. dev_err(&dev->dev, "failed to allocate memory for DMA\n");
  1514. ret = -ENOMEM;
  1515. goto failed_free_io;
  1516. }
  1517. ret = pxafb_init_video_memory(fbi);
  1518. if (ret) {
  1519. dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
  1520. ret = -ENOMEM;
  1521. goto failed_free_dma;
  1522. }
  1523. irq = platform_get_irq(dev, 0);
  1524. if (irq < 0) {
  1525. dev_err(&dev->dev, "no IRQ defined\n");
  1526. ret = -ENODEV;
  1527. goto failed_free_mem;
  1528. }
  1529. ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
  1530. if (ret) {
  1531. dev_err(&dev->dev, "request_irq failed: %d\n", ret);
  1532. ret = -EBUSY;
  1533. goto failed_free_mem;
  1534. }
  1535. ret = pxafb_smart_init(fbi);
  1536. if (ret) {
  1537. dev_err(&dev->dev, "failed to initialize smartpanel\n");
  1538. goto failed_free_irq;
  1539. }
  1540. /*
  1541. * This makes sure that our colour bitfield
  1542. * descriptors are correctly initialised.
  1543. */
  1544. ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
  1545. if (ret) {
  1546. dev_err(&dev->dev, "failed to get suitable mode\n");
  1547. goto failed_free_irq;
  1548. }
  1549. ret = pxafb_set_par(&fbi->fb);
  1550. if (ret) {
  1551. dev_err(&dev->dev, "Failed to set parameters\n");
  1552. goto failed_free_irq;
  1553. }
  1554. platform_set_drvdata(dev, fbi);
  1555. ret = register_framebuffer(&fbi->fb);
  1556. if (ret < 0) {
  1557. dev_err(&dev->dev,
  1558. "Failed to register framebuffer device: %d\n", ret);
  1559. goto failed_free_cmap;
  1560. }
  1561. #ifdef CONFIG_CPU_FREQ
  1562. fbi->freq_transition.notifier_call = pxafb_freq_transition;
  1563. fbi->freq_policy.notifier_call = pxafb_freq_policy;
  1564. cpufreq_register_notifier(&fbi->freq_transition,
  1565. CPUFREQ_TRANSITION_NOTIFIER);
  1566. cpufreq_register_notifier(&fbi->freq_policy,
  1567. CPUFREQ_POLICY_NOTIFIER);
  1568. #endif
  1569. /*
  1570. * Ok, now enable the LCD controller
  1571. */
  1572. set_ctrlr_state(fbi, C_ENABLE);
  1573. return 0;
  1574. failed_free_cmap:
  1575. if (fbi->fb.cmap.len)
  1576. fb_dealloc_cmap(&fbi->fb.cmap);
  1577. failed_free_irq:
  1578. free_irq(irq, fbi);
  1579. failed_free_mem:
  1580. free_pages_exact(fbi->video_mem, fbi->video_mem_size);
  1581. failed_free_dma:
  1582. dma_free_coherent(&dev->dev, fbi->dma_buff_size,
  1583. fbi->dma_buff, fbi->dma_buff_phys);
  1584. failed_free_io:
  1585. iounmap(fbi->mmio_base);
  1586. failed_free_res:
  1587. release_mem_region(r->start, r->end - r->start + 1);
  1588. failed_fbi:
  1589. clk_put(fbi->clk);
  1590. platform_set_drvdata(dev, NULL);
  1591. kfree(fbi);
  1592. failed:
  1593. return ret;
  1594. }
  1595. static int __devexit pxafb_remove(struct platform_device *dev)
  1596. {
  1597. struct pxafb_info *fbi = platform_get_drvdata(dev);
  1598. struct resource *r;
  1599. int irq;
  1600. struct fb_info *info;
  1601. if (!fbi)
  1602. return 0;
  1603. info = &fbi->fb;
  1604. unregister_framebuffer(info);
  1605. pxafb_disable_controller(fbi);
  1606. if (fbi->fb.cmap.len)
  1607. fb_dealloc_cmap(&fbi->fb.cmap);
  1608. irq = platform_get_irq(dev, 0);
  1609. free_irq(irq, fbi);
  1610. free_pages_exact(fbi->video_mem, fbi->video_mem_size);
  1611. dma_free_writecombine(&dev->dev, fbi->dma_buff_size,
  1612. fbi->dma_buff, fbi->dma_buff_phys);
  1613. iounmap(fbi->mmio_base);
  1614. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1615. release_mem_region(r->start, r->end - r->start + 1);
  1616. clk_put(fbi->clk);
  1617. kfree(fbi);
  1618. return 0;
  1619. }
  1620. static struct platform_driver pxafb_driver = {
  1621. .probe = pxafb_probe,
  1622. .remove = pxafb_remove,
  1623. .suspend = pxafb_suspend,
  1624. .resume = pxafb_resume,
  1625. .driver = {
  1626. .owner = THIS_MODULE,
  1627. .name = "pxa2xx-fb",
  1628. },
  1629. };
  1630. static int __init pxafb_init(void)
  1631. {
  1632. if (pxafb_setup_options())
  1633. return -EINVAL;
  1634. return platform_driver_register(&pxafb_driver);
  1635. }
  1636. static void __exit pxafb_exit(void)
  1637. {
  1638. platform_driver_unregister(&pxafb_driver);
  1639. }
  1640. module_init(pxafb_init);
  1641. module_exit(pxafb_exit);
  1642. MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
  1643. MODULE_LICENSE("GPL");