pxafb.c 51 KB

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