pxafb.c 49 KB

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