pxafb.c 44 KB

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