pxafb.c 42 KB

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