pxafb.c 50 KB

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