pxafb.c 45 KB

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