pxafb.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  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 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. if (!(fbi->lccr0 | LCCR0_LCDT))
  693. return 0;
  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 (fbi->backlight_power)
  843. fbi->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 (fbi->lcd_power)
  849. fbi->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 & LCD_TYPE_MASK) {
  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. fbi = pxafb_init_fbinfo(&dev->dev);
  1513. if (!fbi) {
  1514. /* only reason for pxafb_init_fbinfo to fail is kmalloc */
  1515. dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
  1516. ret = -ENOMEM;
  1517. goto failed;
  1518. }
  1519. fbi->backlight_power = inf->pxafb_backlight_power;
  1520. fbi->lcd_power = inf->pxafb_lcd_power;
  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");