pxafb.c 45 KB

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