pxafb.c 42 KB

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