pxafb.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332
  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. * Add support for overlay1 and overlay2 based on pxafb_overlay.c:
  24. *
  25. * Copyright (C) 2004, Intel Corporation
  26. *
  27. * 2003/08/27: <yu.tang@intel.com>
  28. * 2004/03/10: <stanley.cai@intel.com>
  29. * 2004/10/28: <yan.yin@intel.com>
  30. *
  31. * Copyright (C) 2006-2008 Marvell International Ltd.
  32. * All Rights Reserved
  33. */
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/kernel.h>
  37. #include <linux/sched.h>
  38. #include <linux/errno.h>
  39. #include <linux/string.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/slab.h>
  42. #include <linux/mm.h>
  43. #include <linux/fb.h>
  44. #include <linux/delay.h>
  45. #include <linux/init.h>
  46. #include <linux/ioport.h>
  47. #include <linux/cpufreq.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/clk.h>
  51. #include <linux/err.h>
  52. #include <linux/completion.h>
  53. #include <linux/mutex.h>
  54. #include <linux/kthread.h>
  55. #include <linux/freezer.h>
  56. #include <linux/console.h>
  57. #include <mach/hardware.h>
  58. #include <asm/io.h>
  59. #include <asm/irq.h>
  60. #include <asm/div64.h>
  61. #include <mach/bitfield.h>
  62. #include <linux/platform_data/video-pxafb.h>
  63. /*
  64. * Complain if VAR is out of range.
  65. */
  66. #define DEBUG_VAR 1
  67. #include "pxafb.h"
  68. /* Bits which should not be set in machine configuration structures */
  69. #define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
  70. LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
  71. LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
  72. #define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
  73. LCCR3_PCD | LCCR3_BPP(0xf))
  74. static int pxafb_activate_var(struct fb_var_screeninfo *var,
  75. struct pxafb_info *);
  76. static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
  77. static void setup_base_frame(struct pxafb_info *fbi,
  78. struct fb_var_screeninfo *var, int branch);
  79. static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
  80. unsigned long offset, size_t size);
  81. static unsigned long video_mem_size = 0;
  82. static inline unsigned long
  83. lcd_readl(struct pxafb_info *fbi, unsigned int off)
  84. {
  85. return __raw_readl(fbi->mmio_base + off);
  86. }
  87. static inline void
  88. lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
  89. {
  90. __raw_writel(val, fbi->mmio_base + off);
  91. }
  92. static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
  93. {
  94. unsigned long flags;
  95. local_irq_save(flags);
  96. /*
  97. * We need to handle two requests being made at the same time.
  98. * There are two important cases:
  99. * 1. When we are changing VT (C_REENABLE) while unblanking
  100. * (C_ENABLE) We must perform the unblanking, which will
  101. * do our REENABLE for us.
  102. * 2. When we are blanking, but immediately unblank before
  103. * we have blanked. We do the "REENABLE" thing here as
  104. * well, just to be sure.
  105. */
  106. if (fbi->task_state == C_ENABLE && state == C_REENABLE)
  107. state = (u_int) -1;
  108. if (fbi->task_state == C_DISABLE && state == C_ENABLE)
  109. state = C_REENABLE;
  110. if (state != (u_int)-1) {
  111. fbi->task_state = state;
  112. schedule_work(&fbi->task);
  113. }
  114. local_irq_restore(flags);
  115. }
  116. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  117. {
  118. chan &= 0xffff;
  119. chan >>= 16 - bf->length;
  120. return chan << bf->offset;
  121. }
  122. static int
  123. pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
  124. u_int trans, struct fb_info *info)
  125. {
  126. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  127. u_int val;
  128. if (regno >= fbi->palette_size)
  129. return 1;
  130. if (fbi->fb.var.grayscale) {
  131. fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
  132. return 0;
  133. }
  134. switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
  135. case LCCR4_PAL_FOR_0:
  136. val = ((red >> 0) & 0xf800);
  137. val |= ((green >> 5) & 0x07e0);
  138. val |= ((blue >> 11) & 0x001f);
  139. fbi->palette_cpu[regno] = val;
  140. break;
  141. case LCCR4_PAL_FOR_1:
  142. val = ((red << 8) & 0x00f80000);
  143. val |= ((green >> 0) & 0x0000fc00);
  144. val |= ((blue >> 8) & 0x000000f8);
  145. ((u32 *)(fbi->palette_cpu))[regno] = val;
  146. break;
  147. case LCCR4_PAL_FOR_2:
  148. val = ((red << 8) & 0x00fc0000);
  149. val |= ((green >> 0) & 0x0000fc00);
  150. val |= ((blue >> 8) & 0x000000fc);
  151. ((u32 *)(fbi->palette_cpu))[regno] = val;
  152. break;
  153. case LCCR4_PAL_FOR_3:
  154. val = ((red << 8) & 0x00ff0000);
  155. val |= ((green >> 0) & 0x0000ff00);
  156. val |= ((blue >> 8) & 0x000000ff);
  157. ((u32 *)(fbi->palette_cpu))[regno] = val;
  158. break;
  159. }
  160. return 0;
  161. }
  162. static int
  163. pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  164. u_int trans, struct fb_info *info)
  165. {
  166. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  167. unsigned int val;
  168. int ret = 1;
  169. /*
  170. * If inverse mode was selected, invert all the colours
  171. * rather than the register number. The register number
  172. * is what you poke into the framebuffer to produce the
  173. * colour you requested.
  174. */
  175. if (fbi->cmap_inverse) {
  176. red = 0xffff - red;
  177. green = 0xffff - green;
  178. blue = 0xffff - blue;
  179. }
  180. /*
  181. * If greyscale is true, then we convert the RGB value
  182. * to greyscale no matter what visual we are using.
  183. */
  184. if (fbi->fb.var.grayscale)
  185. red = green = blue = (19595 * red + 38470 * green +
  186. 7471 * blue) >> 16;
  187. switch (fbi->fb.fix.visual) {
  188. case FB_VISUAL_TRUECOLOR:
  189. /*
  190. * 16-bit True Colour. We encode the RGB value
  191. * according to the RGB bitfield information.
  192. */
  193. if (regno < 16) {
  194. u32 *pal = fbi->fb.pseudo_palette;
  195. val = chan_to_field(red, &fbi->fb.var.red);
  196. val |= chan_to_field(green, &fbi->fb.var.green);
  197. val |= chan_to_field(blue, &fbi->fb.var.blue);
  198. pal[regno] = val;
  199. ret = 0;
  200. }
  201. break;
  202. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  203. case FB_VISUAL_PSEUDOCOLOR:
  204. ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
  205. break;
  206. }
  207. return ret;
  208. }
  209. /* calculate pixel depth, transparency bit included, >=16bpp formats _only_ */
  210. static inline int var_to_depth(struct fb_var_screeninfo *var)
  211. {
  212. return var->red.length + var->green.length +
  213. var->blue.length + var->transp.length;
  214. }
  215. /* calculate 4-bit BPP value for LCCR3 and OVLxC1 */
  216. static int pxafb_var_to_bpp(struct fb_var_screeninfo *var)
  217. {
  218. int bpp = -EINVAL;
  219. switch (var->bits_per_pixel) {
  220. case 1: bpp = 0; break;
  221. case 2: bpp = 1; break;
  222. case 4: bpp = 2; break;
  223. case 8: bpp = 3; break;
  224. case 16: bpp = 4; break;
  225. case 24:
  226. switch (var_to_depth(var)) {
  227. case 18: bpp = 6; break; /* 18-bits/pixel packed */
  228. case 19: bpp = 8; break; /* 19-bits/pixel packed */
  229. case 24: bpp = 9; break;
  230. }
  231. break;
  232. case 32:
  233. switch (var_to_depth(var)) {
  234. case 18: bpp = 5; break; /* 18-bits/pixel unpacked */
  235. case 19: bpp = 7; break; /* 19-bits/pixel unpacked */
  236. case 25: bpp = 10; break;
  237. }
  238. break;
  239. }
  240. return bpp;
  241. }
  242. /*
  243. * pxafb_var_to_lccr3():
  244. * Convert a bits per pixel value to the correct bit pattern for LCCR3
  245. *
  246. * NOTE: for PXA27x with overlays support, the LCCR3_PDFOR_x bits have an
  247. * implication of the acutal use of transparency bit, which we handle it
  248. * here separatedly. See PXA27x Developer's Manual, Section <<7.4.6 Pixel
  249. * Formats>> for the valid combination of PDFOR, PAL_FOR for various BPP.
  250. *
  251. * Transparency for palette pixel formats is not supported at the moment.
  252. */
  253. static uint32_t pxafb_var_to_lccr3(struct fb_var_screeninfo *var)
  254. {
  255. int bpp = pxafb_var_to_bpp(var);
  256. uint32_t lccr3;
  257. if (bpp < 0)
  258. return 0;
  259. lccr3 = LCCR3_BPP(bpp);
  260. switch (var_to_depth(var)) {
  261. case 16: lccr3 |= var->transp.length ? LCCR3_PDFOR_3 : 0; break;
  262. case 18: lccr3 |= LCCR3_PDFOR_3; break;
  263. case 24: lccr3 |= var->transp.length ? LCCR3_PDFOR_2 : LCCR3_PDFOR_3;
  264. break;
  265. case 19:
  266. case 25: lccr3 |= LCCR3_PDFOR_0; break;
  267. }
  268. return lccr3;
  269. }
  270. #define SET_PIXFMT(v, r, g, b, t) \
  271. ({ \
  272. (v)->transp.offset = (t) ? (r) + (g) + (b) : 0; \
  273. (v)->transp.length = (t) ? (t) : 0; \
  274. (v)->blue.length = (b); (v)->blue.offset = 0; \
  275. (v)->green.length = (g); (v)->green.offset = (b); \
  276. (v)->red.length = (r); (v)->red.offset = (b) + (g); \
  277. })
  278. /* set the RGBT bitfields of fb_var_screeninf according to
  279. * var->bits_per_pixel and given depth
  280. */
  281. static void pxafb_set_pixfmt(struct fb_var_screeninfo *var, int depth)
  282. {
  283. if (depth == 0)
  284. depth = var->bits_per_pixel;
  285. if (var->bits_per_pixel < 16) {
  286. /* indexed pixel formats */
  287. var->red.offset = 0; var->red.length = 8;
  288. var->green.offset = 0; var->green.length = 8;
  289. var->blue.offset = 0; var->blue.length = 8;
  290. var->transp.offset = 0; var->transp.length = 8;
  291. }
  292. switch (depth) {
  293. case 16: var->transp.length ?
  294. SET_PIXFMT(var, 5, 5, 5, 1) : /* RGBT555 */
  295. SET_PIXFMT(var, 5, 6, 5, 0); break; /* RGB565 */
  296. case 18: SET_PIXFMT(var, 6, 6, 6, 0); break; /* RGB666 */
  297. case 19: SET_PIXFMT(var, 6, 6, 6, 1); break; /* RGBT666 */
  298. case 24: var->transp.length ?
  299. SET_PIXFMT(var, 8, 8, 7, 1) : /* RGBT887 */
  300. SET_PIXFMT(var, 8, 8, 8, 0); break; /* RGB888 */
  301. case 25: SET_PIXFMT(var, 8, 8, 8, 1); break; /* RGBT888 */
  302. }
  303. }
  304. #ifdef CONFIG_CPU_FREQ
  305. /*
  306. * pxafb_display_dma_period()
  307. * Calculate the minimum period (in picoseconds) between two DMA
  308. * requests for the LCD controller. If we hit this, it means we're
  309. * doing nothing but LCD DMA.
  310. */
  311. static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
  312. {
  313. /*
  314. * Period = pixclock * bits_per_byte * bytes_per_transfer
  315. * / memory_bits_per_pixel;
  316. */
  317. return var->pixclock * 8 * 16 / var->bits_per_pixel;
  318. }
  319. #endif
  320. /*
  321. * Select the smallest mode that allows the desired resolution to be
  322. * displayed. If desired parameters can be rounded up.
  323. */
  324. static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
  325. struct fb_var_screeninfo *var)
  326. {
  327. struct pxafb_mode_info *mode = NULL;
  328. struct pxafb_mode_info *modelist = mach->modes;
  329. unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
  330. unsigned int i;
  331. for (i = 0; i < mach->num_modes; i++) {
  332. if (modelist[i].xres >= var->xres &&
  333. modelist[i].yres >= var->yres &&
  334. modelist[i].xres < best_x &&
  335. modelist[i].yres < best_y &&
  336. modelist[i].bpp >= var->bits_per_pixel) {
  337. best_x = modelist[i].xres;
  338. best_y = modelist[i].yres;
  339. mode = &modelist[i];
  340. }
  341. }
  342. return mode;
  343. }
  344. static void pxafb_setmode(struct fb_var_screeninfo *var,
  345. struct pxafb_mode_info *mode)
  346. {
  347. var->xres = mode->xres;
  348. var->yres = mode->yres;
  349. var->bits_per_pixel = mode->bpp;
  350. var->pixclock = mode->pixclock;
  351. var->hsync_len = mode->hsync_len;
  352. var->left_margin = mode->left_margin;
  353. var->right_margin = mode->right_margin;
  354. var->vsync_len = mode->vsync_len;
  355. var->upper_margin = mode->upper_margin;
  356. var->lower_margin = mode->lower_margin;
  357. var->sync = mode->sync;
  358. var->grayscale = mode->cmap_greyscale;
  359. var->transp.length = mode->transparency;
  360. /* set the initial RGBA bitfields */
  361. pxafb_set_pixfmt(var, mode->depth);
  362. }
  363. static int pxafb_adjust_timing(struct pxafb_info *fbi,
  364. struct fb_var_screeninfo *var)
  365. {
  366. int line_length;
  367. var->xres = max_t(int, var->xres, MIN_XRES);
  368. var->yres = max_t(int, var->yres, MIN_YRES);
  369. if (!(fbi->lccr0 & LCCR0_LCDT)) {
  370. clamp_val(var->hsync_len, 1, 64);
  371. clamp_val(var->vsync_len, 1, 64);
  372. clamp_val(var->left_margin, 1, 255);
  373. clamp_val(var->right_margin, 1, 255);
  374. clamp_val(var->upper_margin, 1, 255);
  375. clamp_val(var->lower_margin, 1, 255);
  376. }
  377. /* make sure each line is aligned on word boundary */
  378. line_length = var->xres * var->bits_per_pixel / 8;
  379. line_length = ALIGN(line_length, 4);
  380. var->xres = line_length * 8 / var->bits_per_pixel;
  381. /* we don't support xpan, force xres_virtual to be equal to xres */
  382. var->xres_virtual = var->xres;
  383. if (var->accel_flags & FB_ACCELF_TEXT)
  384. var->yres_virtual = fbi->fb.fix.smem_len / line_length;
  385. else
  386. var->yres_virtual = max(var->yres_virtual, var->yres);
  387. /* check for limits */
  388. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  389. return -EINVAL;
  390. if (var->yres > var->yres_virtual)
  391. return -EINVAL;
  392. return 0;
  393. }
  394. /*
  395. * pxafb_check_var():
  396. * Get the video params out of 'var'. If a value doesn't fit, round it up,
  397. * if it's too big, return -EINVAL.
  398. *
  399. * Round up in the following order: bits_per_pixel, xres,
  400. * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
  401. * bitfields, horizontal timing, vertical timing.
  402. */
  403. static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  404. {
  405. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  406. struct pxafb_mach_info *inf = dev_get_platdata(fbi->dev);
  407. int err;
  408. if (inf->fixed_modes) {
  409. struct pxafb_mode_info *mode;
  410. mode = pxafb_getmode(inf, var);
  411. if (!mode)
  412. return -EINVAL;
  413. pxafb_setmode(var, mode);
  414. }
  415. /* do a test conversion to BPP fields to check the color formats */
  416. err = pxafb_var_to_bpp(var);
  417. if (err < 0)
  418. return err;
  419. pxafb_set_pixfmt(var, var_to_depth(var));
  420. err = pxafb_adjust_timing(fbi, var);
  421. if (err)
  422. return err;
  423. #ifdef CONFIG_CPU_FREQ
  424. pr_debug("pxafb: dma period = %d ps\n",
  425. pxafb_display_dma_period(var));
  426. #endif
  427. return 0;
  428. }
  429. /*
  430. * pxafb_set_par():
  431. * Set the user defined part of the display for the specified console
  432. */
  433. static int pxafb_set_par(struct fb_info *info)
  434. {
  435. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  436. struct fb_var_screeninfo *var = &info->var;
  437. if (var->bits_per_pixel >= 16)
  438. fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  439. else if (!fbi->cmap_static)
  440. fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  441. else {
  442. /*
  443. * Some people have weird ideas about wanting static
  444. * pseudocolor maps. I suspect their user space
  445. * applications are broken.
  446. */
  447. fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
  448. }
  449. fbi->fb.fix.line_length = var->xres_virtual *
  450. var->bits_per_pixel / 8;
  451. if (var->bits_per_pixel >= 16)
  452. fbi->palette_size = 0;
  453. else
  454. fbi->palette_size = var->bits_per_pixel == 1 ?
  455. 4 : 1 << var->bits_per_pixel;
  456. fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
  457. if (fbi->fb.var.bits_per_pixel >= 16)
  458. fb_dealloc_cmap(&fbi->fb.cmap);
  459. else
  460. fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
  461. pxafb_activate_var(var, fbi);
  462. return 0;
  463. }
  464. static int pxafb_pan_display(struct fb_var_screeninfo *var,
  465. struct fb_info *info)
  466. {
  467. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  468. struct fb_var_screeninfo newvar;
  469. int dma = DMA_MAX + DMA_BASE;
  470. if (fbi->state != C_ENABLE)
  471. return 0;
  472. /* Only take .xoffset, .yoffset and .vmode & FB_VMODE_YWRAP from what
  473. * was passed in and copy the rest from the old screeninfo.
  474. */
  475. memcpy(&newvar, &fbi->fb.var, sizeof(newvar));
  476. newvar.xoffset = var->xoffset;
  477. newvar.yoffset = var->yoffset;
  478. newvar.vmode &= ~FB_VMODE_YWRAP;
  479. newvar.vmode |= var->vmode & FB_VMODE_YWRAP;
  480. setup_base_frame(fbi, &newvar, 1);
  481. if (fbi->lccr0 & LCCR0_SDS)
  482. lcd_writel(fbi, FBR1, fbi->fdadr[dma + 1] | 0x1);
  483. lcd_writel(fbi, FBR0, fbi->fdadr[dma] | 0x1);
  484. return 0;
  485. }
  486. /*
  487. * pxafb_blank():
  488. * Blank the display by setting all palette values to zero. Note, the
  489. * 16 bpp mode does not really use the palette, so this will not
  490. * blank the display in all modes.
  491. */
  492. static int pxafb_blank(int blank, struct fb_info *info)
  493. {
  494. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  495. int i;
  496. switch (blank) {
  497. case FB_BLANK_POWERDOWN:
  498. case FB_BLANK_VSYNC_SUSPEND:
  499. case FB_BLANK_HSYNC_SUSPEND:
  500. case FB_BLANK_NORMAL:
  501. if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  502. fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  503. for (i = 0; i < fbi->palette_size; i++)
  504. pxafb_setpalettereg(i, 0, 0, 0, 0, info);
  505. pxafb_schedule_work(fbi, C_DISABLE);
  506. /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
  507. break;
  508. case FB_BLANK_UNBLANK:
  509. /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
  510. if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
  511. fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
  512. fb_set_cmap(&fbi->fb.cmap, info);
  513. pxafb_schedule_work(fbi, C_ENABLE);
  514. }
  515. return 0;
  516. }
  517. static struct fb_ops pxafb_ops = {
  518. .owner = THIS_MODULE,
  519. .fb_check_var = pxafb_check_var,
  520. .fb_set_par = pxafb_set_par,
  521. .fb_pan_display = pxafb_pan_display,
  522. .fb_setcolreg = pxafb_setcolreg,
  523. .fb_fillrect = cfb_fillrect,
  524. .fb_copyarea = cfb_copyarea,
  525. .fb_imageblit = cfb_imageblit,
  526. .fb_blank = pxafb_blank,
  527. };
  528. #ifdef CONFIG_FB_PXA_OVERLAY
  529. static void overlay1fb_setup(struct pxafb_layer *ofb)
  530. {
  531. int size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
  532. unsigned long start = ofb->video_mem_phys;
  533. setup_frame_dma(ofb->fbi, DMA_OV1, PAL_NONE, start, size);
  534. }
  535. /* Depending on the enable status of overlay1/2, the DMA should be
  536. * updated from FDADRx (when disabled) or FBRx (when enabled).
  537. */
  538. static void overlay1fb_enable(struct pxafb_layer *ofb)
  539. {
  540. int enabled = lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN;
  541. uint32_t fdadr1 = ofb->fbi->fdadr[DMA_OV1] | (enabled ? 0x1 : 0);
  542. lcd_writel(ofb->fbi, enabled ? FBR1 : FDADR1, fdadr1);
  543. lcd_writel(ofb->fbi, OVL1C2, ofb->control[1]);
  544. lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] | OVLxC1_OEN);
  545. }
  546. static void overlay1fb_disable(struct pxafb_layer *ofb)
  547. {
  548. uint32_t lccr5;
  549. if (!(lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN))
  550. return;
  551. lccr5 = lcd_readl(ofb->fbi, LCCR5);
  552. lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] & ~OVLxC1_OEN);
  553. lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(1));
  554. lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(1));
  555. lcd_writel(ofb->fbi, FBR1, ofb->fbi->fdadr[DMA_OV1] | 0x3);
  556. if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
  557. pr_warning("%s: timeout disabling overlay1\n", __func__);
  558. lcd_writel(ofb->fbi, LCCR5, lccr5);
  559. }
  560. static void overlay2fb_setup(struct pxafb_layer *ofb)
  561. {
  562. int size, div = 1, pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
  563. unsigned long start[3] = { ofb->video_mem_phys, 0, 0 };
  564. if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED) {
  565. size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
  566. setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
  567. } else {
  568. size = ofb->fb.var.xres_virtual * ofb->fb.var.yres_virtual;
  569. switch (pfor) {
  570. case OVERLAY_FORMAT_YUV444_PLANAR: div = 1; break;
  571. case OVERLAY_FORMAT_YUV422_PLANAR: div = 2; break;
  572. case OVERLAY_FORMAT_YUV420_PLANAR: div = 4; break;
  573. }
  574. start[1] = start[0] + size;
  575. start[2] = start[1] + size / div;
  576. setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
  577. setup_frame_dma(ofb->fbi, DMA_OV2_Cb, -1, start[1], size / div);
  578. setup_frame_dma(ofb->fbi, DMA_OV2_Cr, -1, start[2], size / div);
  579. }
  580. }
  581. static void overlay2fb_enable(struct pxafb_layer *ofb)
  582. {
  583. int pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
  584. int enabled = lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN;
  585. uint32_t fdadr2 = ofb->fbi->fdadr[DMA_OV2_Y] | (enabled ? 0x1 : 0);
  586. uint32_t fdadr3 = ofb->fbi->fdadr[DMA_OV2_Cb] | (enabled ? 0x1 : 0);
  587. uint32_t fdadr4 = ofb->fbi->fdadr[DMA_OV2_Cr] | (enabled ? 0x1 : 0);
  588. if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED)
  589. lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
  590. else {
  591. lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
  592. lcd_writel(ofb->fbi, enabled ? FBR3 : FDADR3, fdadr3);
  593. lcd_writel(ofb->fbi, enabled ? FBR4 : FDADR4, fdadr4);
  594. }
  595. lcd_writel(ofb->fbi, OVL2C2, ofb->control[1]);
  596. lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] | OVLxC1_OEN);
  597. }
  598. static void overlay2fb_disable(struct pxafb_layer *ofb)
  599. {
  600. uint32_t lccr5;
  601. if (!(lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN))
  602. return;
  603. lccr5 = lcd_readl(ofb->fbi, LCCR5);
  604. lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] & ~OVLxC1_OEN);
  605. lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(2));
  606. lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(2));
  607. lcd_writel(ofb->fbi, FBR2, ofb->fbi->fdadr[DMA_OV2_Y] | 0x3);
  608. lcd_writel(ofb->fbi, FBR3, ofb->fbi->fdadr[DMA_OV2_Cb] | 0x3);
  609. lcd_writel(ofb->fbi, FBR4, ofb->fbi->fdadr[DMA_OV2_Cr] | 0x3);
  610. if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
  611. pr_warning("%s: timeout disabling overlay2\n", __func__);
  612. }
  613. static struct pxafb_layer_ops ofb_ops[] = {
  614. [0] = {
  615. .enable = overlay1fb_enable,
  616. .disable = overlay1fb_disable,
  617. .setup = overlay1fb_setup,
  618. },
  619. [1] = {
  620. .enable = overlay2fb_enable,
  621. .disable = overlay2fb_disable,
  622. .setup = overlay2fb_setup,
  623. },
  624. };
  625. static int overlayfb_open(struct fb_info *info, int user)
  626. {
  627. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  628. /* no support for framebuffer console on overlay */
  629. if (user == 0)
  630. return -ENODEV;
  631. if (ofb->usage++ == 0) {
  632. /* unblank the base framebuffer */
  633. console_lock();
  634. fb_blank(&ofb->fbi->fb, FB_BLANK_UNBLANK);
  635. console_unlock();
  636. }
  637. return 0;
  638. }
  639. static int overlayfb_release(struct fb_info *info, int user)
  640. {
  641. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  642. if (ofb->usage == 1) {
  643. ofb->ops->disable(ofb);
  644. ofb->fb.var.height = -1;
  645. ofb->fb.var.width = -1;
  646. ofb->fb.var.xres = ofb->fb.var.xres_virtual = 0;
  647. ofb->fb.var.yres = ofb->fb.var.yres_virtual = 0;
  648. ofb->usage--;
  649. }
  650. return 0;
  651. }
  652. static int overlayfb_check_var(struct fb_var_screeninfo *var,
  653. struct fb_info *info)
  654. {
  655. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  656. struct fb_var_screeninfo *base_var = &ofb->fbi->fb.var;
  657. int xpos, ypos, pfor, bpp;
  658. xpos = NONSTD_TO_XPOS(var->nonstd);
  659. ypos = NONSTD_TO_YPOS(var->nonstd);
  660. pfor = NONSTD_TO_PFOR(var->nonstd);
  661. bpp = pxafb_var_to_bpp(var);
  662. if (bpp < 0)
  663. return -EINVAL;
  664. /* no support for YUV format on overlay1 */
  665. if (ofb->id == OVERLAY1 && pfor != 0)
  666. return -EINVAL;
  667. /* for YUV packed formats, bpp = 'minimum bpp of YUV components' */
  668. switch (pfor) {
  669. case OVERLAY_FORMAT_RGB:
  670. bpp = pxafb_var_to_bpp(var);
  671. if (bpp < 0)
  672. return -EINVAL;
  673. pxafb_set_pixfmt(var, var_to_depth(var));
  674. break;
  675. case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
  676. case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 8; break;
  677. case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 4; break;
  678. case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 2; break;
  679. default:
  680. return -EINVAL;
  681. }
  682. /* each line must start at a 32-bit word boundary */
  683. if ((xpos * bpp) % 32)
  684. return -EINVAL;
  685. /* xres must align on 32-bit word boundary */
  686. var->xres = roundup(var->xres * bpp, 32) / bpp;
  687. if ((xpos + var->xres > base_var->xres) ||
  688. (ypos + var->yres > base_var->yres))
  689. return -EINVAL;
  690. var->xres_virtual = var->xres;
  691. var->yres_virtual = max(var->yres, var->yres_virtual);
  692. return 0;
  693. }
  694. static int overlayfb_check_video_memory(struct pxafb_layer *ofb)
  695. {
  696. struct fb_var_screeninfo *var = &ofb->fb.var;
  697. int pfor = NONSTD_TO_PFOR(var->nonstd);
  698. int size, bpp = 0;
  699. switch (pfor) {
  700. case OVERLAY_FORMAT_RGB: bpp = var->bits_per_pixel; break;
  701. case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
  702. case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 24; break;
  703. case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 16; break;
  704. case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 12; break;
  705. }
  706. ofb->fb.fix.line_length = var->xres_virtual * bpp / 8;
  707. size = PAGE_ALIGN(ofb->fb.fix.line_length * var->yres_virtual);
  708. if (ofb->video_mem) {
  709. if (ofb->video_mem_size >= size)
  710. return 0;
  711. }
  712. return -EINVAL;
  713. }
  714. static int overlayfb_set_par(struct fb_info *info)
  715. {
  716. struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
  717. struct fb_var_screeninfo *var = &info->var;
  718. int xpos, ypos, pfor, bpp, ret;
  719. ret = overlayfb_check_video_memory(ofb);
  720. if (ret)
  721. return ret;
  722. bpp = pxafb_var_to_bpp(var);
  723. xpos = NONSTD_TO_XPOS(var->nonstd);
  724. ypos = NONSTD_TO_YPOS(var->nonstd);
  725. pfor = NONSTD_TO_PFOR(var->nonstd);
  726. ofb->control[0] = OVLxC1_PPL(var->xres) | OVLxC1_LPO(var->yres) |
  727. OVLxC1_BPP(bpp);
  728. ofb->control[1] = OVLxC2_XPOS(xpos) | OVLxC2_YPOS(ypos);
  729. if (ofb->id == OVERLAY2)
  730. ofb->control[1] |= OVL2C2_PFOR(pfor);
  731. ofb->ops->setup(ofb);
  732. ofb->ops->enable(ofb);
  733. return 0;
  734. }
  735. static struct fb_ops overlay_fb_ops = {
  736. .owner = THIS_MODULE,
  737. .fb_open = overlayfb_open,
  738. .fb_release = overlayfb_release,
  739. .fb_check_var = overlayfb_check_var,
  740. .fb_set_par = overlayfb_set_par,
  741. };
  742. static void init_pxafb_overlay(struct pxafb_info *fbi, struct pxafb_layer *ofb,
  743. int id)
  744. {
  745. sprintf(ofb->fb.fix.id, "overlay%d", id + 1);
  746. ofb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  747. ofb->fb.fix.xpanstep = 0;
  748. ofb->fb.fix.ypanstep = 1;
  749. ofb->fb.var.activate = FB_ACTIVATE_NOW;
  750. ofb->fb.var.height = -1;
  751. ofb->fb.var.width = -1;
  752. ofb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  753. ofb->fb.fbops = &overlay_fb_ops;
  754. ofb->fb.flags = FBINFO_FLAG_DEFAULT;
  755. ofb->fb.node = -1;
  756. ofb->fb.pseudo_palette = NULL;
  757. ofb->id = id;
  758. ofb->ops = &ofb_ops[id];
  759. ofb->usage = 0;
  760. ofb->fbi = fbi;
  761. init_completion(&ofb->branch_done);
  762. }
  763. static inline int pxafb_overlay_supported(void)
  764. {
  765. if (cpu_is_pxa27x() || cpu_is_pxa3xx())
  766. return 1;
  767. return 0;
  768. }
  769. static int pxafb_overlay_map_video_memory(struct pxafb_info *pxafb,
  770. struct pxafb_layer *ofb)
  771. {
  772. /* We assume that user will use at most video_mem_size for overlay fb,
  773. * anyway, it's useless to use 16bpp main plane and 24bpp overlay
  774. */
  775. ofb->video_mem = alloc_pages_exact(PAGE_ALIGN(pxafb->video_mem_size),
  776. GFP_KERNEL | __GFP_ZERO);
  777. if (ofb->video_mem == NULL)
  778. return -ENOMEM;
  779. ofb->video_mem_phys = virt_to_phys(ofb->video_mem);
  780. ofb->video_mem_size = PAGE_ALIGN(pxafb->video_mem_size);
  781. mutex_lock(&ofb->fb.mm_lock);
  782. ofb->fb.fix.smem_start = ofb->video_mem_phys;
  783. ofb->fb.fix.smem_len = pxafb->video_mem_size;
  784. mutex_unlock(&ofb->fb.mm_lock);
  785. ofb->fb.screen_base = ofb->video_mem;
  786. return 0;
  787. }
  788. static void pxafb_overlay_init(struct pxafb_info *fbi)
  789. {
  790. int i, ret;
  791. if (!pxafb_overlay_supported())
  792. return;
  793. for (i = 0; i < 2; i++) {
  794. struct pxafb_layer *ofb = &fbi->overlay[i];
  795. init_pxafb_overlay(fbi, ofb, i);
  796. ret = register_framebuffer(&ofb->fb);
  797. if (ret) {
  798. dev_err(fbi->dev, "failed to register overlay %d\n", i);
  799. continue;
  800. }
  801. ret = pxafb_overlay_map_video_memory(fbi, ofb);
  802. if (ret) {
  803. dev_err(fbi->dev,
  804. "failed to map video memory for overlay %d\n",
  805. i);
  806. unregister_framebuffer(&ofb->fb);
  807. continue;
  808. }
  809. ofb->registered = 1;
  810. }
  811. /* mask all IU/BS/EOF/SOF interrupts */
  812. lcd_writel(fbi, LCCR5, ~0);
  813. pr_info("PXA Overlay driver loaded successfully!\n");
  814. }
  815. static void pxafb_overlay_exit(struct pxafb_info *fbi)
  816. {
  817. int i;
  818. if (!pxafb_overlay_supported())
  819. return;
  820. for (i = 0; i < 2; i++) {
  821. struct pxafb_layer *ofb = &fbi->overlay[i];
  822. if (ofb->registered) {
  823. if (ofb->video_mem)
  824. free_pages_exact(ofb->video_mem,
  825. ofb->video_mem_size);
  826. unregister_framebuffer(&ofb->fb);
  827. }
  828. }
  829. }
  830. #else
  831. static inline void pxafb_overlay_init(struct pxafb_info *fbi) {}
  832. static inline void pxafb_overlay_exit(struct pxafb_info *fbi) {}
  833. #endif /* CONFIG_FB_PXA_OVERLAY */
  834. /*
  835. * Calculate the PCD value from the clock rate (in picoseconds).
  836. * We take account of the PPCR clock setting.
  837. * From PXA Developer's Manual:
  838. *
  839. * PixelClock = LCLK
  840. * -------------
  841. * 2 ( PCD + 1 )
  842. *
  843. * PCD = LCLK
  844. * ------------- - 1
  845. * 2(PixelClock)
  846. *
  847. * Where:
  848. * LCLK = LCD/Memory Clock
  849. * PCD = LCCR3[7:0]
  850. *
  851. * PixelClock here is in Hz while the pixclock argument given is the
  852. * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
  853. *
  854. * The function get_lclk_frequency_10khz returns LCLK in units of
  855. * 10khz. Calling the result of this function lclk gives us the
  856. * following
  857. *
  858. * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
  859. * -------------------------------------- - 1
  860. * 2
  861. *
  862. * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
  863. */
  864. static inline unsigned int get_pcd(struct pxafb_info *fbi,
  865. unsigned int pixclock)
  866. {
  867. unsigned long long pcd;
  868. /* FIXME: Need to take into account Double Pixel Clock mode
  869. * (DPC) bit? or perhaps set it based on the various clock
  870. * speeds */
  871. pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
  872. pcd *= pixclock;
  873. do_div(pcd, 100000000 * 2);
  874. /* no need for this, since we should subtract 1 anyway. they cancel */
  875. /* pcd += 1; */ /* make up for integer math truncations */
  876. return (unsigned int)pcd;
  877. }
  878. /*
  879. * Some touchscreens need hsync information from the video driver to
  880. * function correctly. We export it here. Note that 'hsync_time' and
  881. * the value returned from pxafb_get_hsync_time() is the *reciprocal*
  882. * of the hsync period in seconds.
  883. */
  884. static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
  885. {
  886. unsigned long htime;
  887. if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
  888. fbi->hsync_time = 0;
  889. return;
  890. }
  891. htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
  892. fbi->hsync_time = htime;
  893. }
  894. unsigned long pxafb_get_hsync_time(struct device *dev)
  895. {
  896. struct pxafb_info *fbi = dev_get_drvdata(dev);
  897. /* If display is blanked/suspended, hsync isn't active */
  898. if (!fbi || (fbi->state != C_ENABLE))
  899. return 0;
  900. return fbi->hsync_time;
  901. }
  902. EXPORT_SYMBOL(pxafb_get_hsync_time);
  903. static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
  904. unsigned long start, size_t size)
  905. {
  906. struct pxafb_dma_descriptor *dma_desc, *pal_desc;
  907. unsigned int dma_desc_off, pal_desc_off;
  908. if (dma < 0 || dma >= DMA_MAX * 2)
  909. return -EINVAL;
  910. dma_desc = &fbi->dma_buff->dma_desc[dma];
  911. dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
  912. dma_desc->fsadr = start;
  913. dma_desc->fidr = 0;
  914. dma_desc->ldcmd = size;
  915. if (pal < 0 || pal >= PAL_MAX * 2) {
  916. dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  917. fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
  918. } else {
  919. pal_desc = &fbi->dma_buff->pal_desc[pal];
  920. pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
  921. pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
  922. pal_desc->fidr = 0;
  923. if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
  924. pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
  925. else
  926. pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
  927. pal_desc->ldcmd |= LDCMD_PAL;
  928. /* flip back and forth between palette and frame buffer */
  929. pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  930. dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
  931. fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
  932. }
  933. return 0;
  934. }
  935. static void setup_base_frame(struct pxafb_info *fbi,
  936. struct fb_var_screeninfo *var,
  937. int branch)
  938. {
  939. struct fb_fix_screeninfo *fix = &fbi->fb.fix;
  940. int nbytes, dma, pal, bpp = var->bits_per_pixel;
  941. unsigned long offset;
  942. dma = DMA_BASE + (branch ? DMA_MAX : 0);
  943. pal = (bpp >= 16) ? PAL_NONE : PAL_BASE + (branch ? PAL_MAX : 0);
  944. nbytes = fix->line_length * var->yres;
  945. offset = fix->line_length * var->yoffset + fbi->video_mem_phys;
  946. if (fbi->lccr0 & LCCR0_SDS) {
  947. nbytes = nbytes / 2;
  948. setup_frame_dma(fbi, dma + 1, PAL_NONE, offset + nbytes, nbytes);
  949. }
  950. setup_frame_dma(fbi, dma, pal, offset, nbytes);
  951. }
  952. #ifdef CONFIG_FB_PXA_SMARTPANEL
  953. static int setup_smart_dma(struct pxafb_info *fbi)
  954. {
  955. struct pxafb_dma_descriptor *dma_desc;
  956. unsigned long dma_desc_off, cmd_buff_off;
  957. dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
  958. dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
  959. cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
  960. dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
  961. dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
  962. dma_desc->fidr = 0;
  963. dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
  964. fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
  965. return 0;
  966. }
  967. int pxafb_smart_flush(struct fb_info *info)
  968. {
  969. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  970. uint32_t prsr;
  971. int ret = 0;
  972. /* disable controller until all registers are set up */
  973. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  974. /* 1. make it an even number of commands to align on 32-bit boundary
  975. * 2. add the interrupt command to the end of the chain so we can
  976. * keep track of the end of the transfer
  977. */
  978. while (fbi->n_smart_cmds & 1)
  979. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
  980. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
  981. fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
  982. setup_smart_dma(fbi);
  983. /* continue to execute next command */
  984. prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
  985. lcd_writel(fbi, PRSR, prsr);
  986. /* stop the processor in case it executed "wait for sync" cmd */
  987. lcd_writel(fbi, CMDCR, 0x0001);
  988. /* don't send interrupts for fifo underruns on channel 6 */
  989. lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
  990. lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
  991. lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
  992. lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
  993. lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
  994. lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
  995. lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
  996. /* begin sending */
  997. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
  998. if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
  999. pr_warning("%s: timeout waiting for command done\n",
  1000. __func__);
  1001. ret = -ETIMEDOUT;
  1002. }
  1003. /* quick disable */
  1004. prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
  1005. lcd_writel(fbi, PRSR, prsr);
  1006. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  1007. lcd_writel(fbi, FDADR6, 0);
  1008. fbi->n_smart_cmds = 0;
  1009. return ret;
  1010. }
  1011. int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
  1012. {
  1013. int i;
  1014. struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
  1015. for (i = 0; i < n_cmds; i++, cmds++) {
  1016. /* if it is a software delay, flush and delay */
  1017. if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
  1018. pxafb_smart_flush(info);
  1019. mdelay(*cmds & 0xff);
  1020. continue;
  1021. }
  1022. /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
  1023. if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
  1024. pxafb_smart_flush(info);
  1025. fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
  1026. }
  1027. return 0;
  1028. }
  1029. static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
  1030. {
  1031. unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
  1032. return (t == 0) ? 1 : t;
  1033. }
  1034. static void setup_smart_timing(struct pxafb_info *fbi,
  1035. struct fb_var_screeninfo *var)
  1036. {
  1037. struct pxafb_mach_info *inf = dev_get_platdata(fbi->dev);
  1038. struct pxafb_mode_info *mode = &inf->modes[0];
  1039. unsigned long lclk = clk_get_rate(fbi->clk);
  1040. unsigned t1, t2, t3, t4;
  1041. t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
  1042. t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
  1043. t3 = mode->op_hold_time;
  1044. t4 = mode->cmd_inh_time;
  1045. fbi->reg_lccr1 =
  1046. LCCR1_DisWdth(var->xres) |
  1047. LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
  1048. LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
  1049. LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
  1050. fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
  1051. fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
  1052. fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
  1053. fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
  1054. /* FIXME: make this configurable */
  1055. fbi->reg_cmdcr = 1;
  1056. }
  1057. static int pxafb_smart_thread(void *arg)
  1058. {
  1059. struct pxafb_info *fbi = arg;
  1060. struct pxafb_mach_info *inf = dev_get_platdata(fbi->dev);
  1061. if (!inf->smart_update) {
  1062. pr_err("%s: not properly initialized, thread terminated\n",
  1063. __func__);
  1064. return -EINVAL;
  1065. }
  1066. inf = dev_get_platdata(fbi->dev);
  1067. pr_debug("%s(): task starting\n", __func__);
  1068. set_freezable();
  1069. while (!kthread_should_stop()) {
  1070. if (try_to_freeze())
  1071. continue;
  1072. mutex_lock(&fbi->ctrlr_lock);
  1073. if (fbi->state == C_ENABLE) {
  1074. inf->smart_update(&fbi->fb);
  1075. complete(&fbi->refresh_done);
  1076. }
  1077. mutex_unlock(&fbi->ctrlr_lock);
  1078. set_current_state(TASK_INTERRUPTIBLE);
  1079. schedule_timeout(msecs_to_jiffies(30));
  1080. }
  1081. pr_debug("%s(): task ending\n", __func__);
  1082. return 0;
  1083. }
  1084. static int pxafb_smart_init(struct pxafb_info *fbi)
  1085. {
  1086. if (!(fbi->lccr0 & LCCR0_LCDT))
  1087. return 0;
  1088. fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
  1089. fbi->n_smart_cmds = 0;
  1090. init_completion(&fbi->command_done);
  1091. init_completion(&fbi->refresh_done);
  1092. fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
  1093. "lcd_refresh");
  1094. if (IS_ERR(fbi->smart_thread)) {
  1095. pr_err("%s: unable to create kernel thread\n", __func__);
  1096. return PTR_ERR(fbi->smart_thread);
  1097. }
  1098. return 0;
  1099. }
  1100. #else
  1101. static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
  1102. #endif /* CONFIG_FB_PXA_SMARTPANEL */
  1103. static void setup_parallel_timing(struct pxafb_info *fbi,
  1104. struct fb_var_screeninfo *var)
  1105. {
  1106. unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
  1107. fbi->reg_lccr1 =
  1108. LCCR1_DisWdth(var->xres) +
  1109. LCCR1_HorSnchWdth(var->hsync_len) +
  1110. LCCR1_BegLnDel(var->left_margin) +
  1111. LCCR1_EndLnDel(var->right_margin);
  1112. /*
  1113. * If we have a dual scan LCD, we need to halve
  1114. * the YRES parameter.
  1115. */
  1116. lines_per_panel = var->yres;
  1117. if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
  1118. lines_per_panel /= 2;
  1119. fbi->reg_lccr2 =
  1120. LCCR2_DisHght(lines_per_panel) +
  1121. LCCR2_VrtSnchWdth(var->vsync_len) +
  1122. LCCR2_BegFrmDel(var->upper_margin) +
  1123. LCCR2_EndFrmDel(var->lower_margin);
  1124. fbi->reg_lccr3 = fbi->lccr3 |
  1125. (var->sync & FB_SYNC_HOR_HIGH_ACT ?
  1126. LCCR3_HorSnchH : LCCR3_HorSnchL) |
  1127. (var->sync & FB_SYNC_VERT_HIGH_ACT ?
  1128. LCCR3_VrtSnchH : LCCR3_VrtSnchL);
  1129. if (pcd) {
  1130. fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
  1131. set_hsync_time(fbi, pcd);
  1132. }
  1133. }
  1134. /*
  1135. * pxafb_activate_var():
  1136. * Configures LCD Controller based on entries in var parameter.
  1137. * Settings are only written to the controller if changes were made.
  1138. */
  1139. static int pxafb_activate_var(struct fb_var_screeninfo *var,
  1140. struct pxafb_info *fbi)
  1141. {
  1142. u_long flags;
  1143. /* Update shadow copy atomically */
  1144. local_irq_save(flags);
  1145. #ifdef CONFIG_FB_PXA_SMARTPANEL
  1146. if (fbi->lccr0 & LCCR0_LCDT)
  1147. setup_smart_timing(fbi, var);
  1148. else
  1149. #endif
  1150. setup_parallel_timing(fbi, var);
  1151. setup_base_frame(fbi, var, 0);
  1152. fbi->reg_lccr0 = fbi->lccr0 |
  1153. (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
  1154. LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
  1155. fbi->reg_lccr3 |= pxafb_var_to_lccr3(var);
  1156. fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
  1157. fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
  1158. local_irq_restore(flags);
  1159. /*
  1160. * Only update the registers if the controller is enabled
  1161. * and something has changed.
  1162. */
  1163. if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
  1164. (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
  1165. (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
  1166. (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
  1167. (lcd_readl(fbi, LCCR4) != fbi->reg_lccr4) ||
  1168. (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
  1169. ((fbi->lccr0 & LCCR0_SDS) &&
  1170. (lcd_readl(fbi, FDADR1) != fbi->fdadr[1])))
  1171. pxafb_schedule_work(fbi, C_REENABLE);
  1172. return 0;
  1173. }
  1174. /*
  1175. * NOTE! The following functions are purely helpers for set_ctrlr_state.
  1176. * Do not call them directly; set_ctrlr_state does the correct serialisation
  1177. * to ensure that things happen in the right way 100% of time time.
  1178. * -- rmk
  1179. */
  1180. static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
  1181. {
  1182. pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
  1183. if (fbi->backlight_power)
  1184. fbi->backlight_power(on);
  1185. }
  1186. static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
  1187. {
  1188. pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
  1189. if (fbi->lcd_power)
  1190. fbi->lcd_power(on, &fbi->fb.var);
  1191. }
  1192. static void pxafb_enable_controller(struct pxafb_info *fbi)
  1193. {
  1194. pr_debug("pxafb: Enabling LCD controller\n");
  1195. pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
  1196. pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
  1197. pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
  1198. pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
  1199. pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
  1200. pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
  1201. /* enable LCD controller clock */
  1202. clk_prepare_enable(fbi->clk);
  1203. if (fbi->lccr0 & LCCR0_LCDT)
  1204. return;
  1205. /* Sequence from 11.7.10 */
  1206. lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
  1207. lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
  1208. lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
  1209. lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
  1210. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
  1211. lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
  1212. if (fbi->lccr0 & LCCR0_SDS)
  1213. lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
  1214. lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
  1215. }
  1216. static void pxafb_disable_controller(struct pxafb_info *fbi)
  1217. {
  1218. uint32_t lccr0;
  1219. #ifdef CONFIG_FB_PXA_SMARTPANEL
  1220. if (fbi->lccr0 & LCCR0_LCDT) {
  1221. wait_for_completion_timeout(&fbi->refresh_done,
  1222. msecs_to_jiffies(200));
  1223. return;
  1224. }
  1225. #endif
  1226. /* Clear LCD Status Register */
  1227. lcd_writel(fbi, LCSR, 0xffffffff);
  1228. lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
  1229. lcd_writel(fbi, LCCR0, lccr0);
  1230. lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
  1231. wait_for_completion_timeout(&fbi->disable_done, msecs_to_jiffies(200));
  1232. /* disable LCD controller clock */
  1233. clk_disable_unprepare(fbi->clk);
  1234. }
  1235. /*
  1236. * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
  1237. */
  1238. static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
  1239. {
  1240. struct pxafb_info *fbi = dev_id;
  1241. unsigned int lccr0, lcsr;
  1242. lcsr = lcd_readl(fbi, LCSR);
  1243. if (lcsr & LCSR_LDD) {
  1244. lccr0 = lcd_readl(fbi, LCCR0);
  1245. lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
  1246. complete(&fbi->disable_done);
  1247. }
  1248. #ifdef CONFIG_FB_PXA_SMARTPANEL
  1249. if (lcsr & LCSR_CMD_INT)
  1250. complete(&fbi->command_done);
  1251. #endif
  1252. lcd_writel(fbi, LCSR, lcsr);
  1253. #ifdef CONFIG_FB_PXA_OVERLAY
  1254. {
  1255. unsigned int lcsr1 = lcd_readl(fbi, LCSR1);
  1256. if (lcsr1 & LCSR1_BS(1))
  1257. complete(&fbi->overlay[0].branch_done);
  1258. if (lcsr1 & LCSR1_BS(2))
  1259. complete(&fbi->overlay[1].branch_done);
  1260. lcd_writel(fbi, LCSR1, lcsr1);
  1261. }
  1262. #endif
  1263. return IRQ_HANDLED;
  1264. }
  1265. /*
  1266. * This function must be called from task context only, since it will
  1267. * sleep when disabling the LCD controller, or if we get two contending
  1268. * processes trying to alter state.
  1269. */
  1270. static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
  1271. {
  1272. u_int old_state;
  1273. mutex_lock(&fbi->ctrlr_lock);
  1274. old_state = fbi->state;
  1275. /*
  1276. * Hack around fbcon initialisation.
  1277. */
  1278. if (old_state == C_STARTUP && state == C_REENABLE)
  1279. state = C_ENABLE;
  1280. switch (state) {
  1281. case C_DISABLE_CLKCHANGE:
  1282. /*
  1283. * Disable controller for clock change. If the
  1284. * controller is already disabled, then do nothing.
  1285. */
  1286. if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
  1287. fbi->state = state;
  1288. /* TODO __pxafb_lcd_power(fbi, 0); */
  1289. pxafb_disable_controller(fbi);
  1290. }
  1291. break;
  1292. case C_DISABLE_PM:
  1293. case C_DISABLE:
  1294. /*
  1295. * Disable controller
  1296. */
  1297. if (old_state != C_DISABLE) {
  1298. fbi->state = state;
  1299. __pxafb_backlight_power(fbi, 0);
  1300. __pxafb_lcd_power(fbi, 0);
  1301. if (old_state != C_DISABLE_CLKCHANGE)
  1302. pxafb_disable_controller(fbi);
  1303. }
  1304. break;
  1305. case C_ENABLE_CLKCHANGE:
  1306. /*
  1307. * Enable the controller after clock change. Only
  1308. * do this if we were disabled for the clock change.
  1309. */
  1310. if (old_state == C_DISABLE_CLKCHANGE) {
  1311. fbi->state = C_ENABLE;
  1312. pxafb_enable_controller(fbi);
  1313. /* TODO __pxafb_lcd_power(fbi, 1); */
  1314. }
  1315. break;
  1316. case C_REENABLE:
  1317. /*
  1318. * Re-enable the controller only if it was already
  1319. * enabled. This is so we reprogram the control
  1320. * registers.
  1321. */
  1322. if (old_state == C_ENABLE) {
  1323. __pxafb_lcd_power(fbi, 0);
  1324. pxafb_disable_controller(fbi);
  1325. pxafb_enable_controller(fbi);
  1326. __pxafb_lcd_power(fbi, 1);
  1327. }
  1328. break;
  1329. case C_ENABLE_PM:
  1330. /*
  1331. * Re-enable the controller after PM. This is not
  1332. * perfect - think about the case where we were doing
  1333. * a clock change, and we suspended half-way through.
  1334. */
  1335. if (old_state != C_DISABLE_PM)
  1336. break;
  1337. /* fall through */
  1338. case C_ENABLE:
  1339. /*
  1340. * Power up the LCD screen, enable controller, and
  1341. * turn on the backlight.
  1342. */
  1343. if (old_state != C_ENABLE) {
  1344. fbi->state = C_ENABLE;
  1345. pxafb_enable_controller(fbi);
  1346. __pxafb_lcd_power(fbi, 1);
  1347. __pxafb_backlight_power(fbi, 1);
  1348. }
  1349. break;
  1350. }
  1351. mutex_unlock(&fbi->ctrlr_lock);
  1352. }
  1353. /*
  1354. * Our LCD controller task (which is called when we blank or unblank)
  1355. * via keventd.
  1356. */
  1357. static void pxafb_task(struct work_struct *work)
  1358. {
  1359. struct pxafb_info *fbi =
  1360. container_of(work, struct pxafb_info, task);
  1361. u_int state = xchg(&fbi->task_state, -1);
  1362. set_ctrlr_state(fbi, state);
  1363. }
  1364. #ifdef CONFIG_CPU_FREQ
  1365. /*
  1366. * CPU clock speed change handler. We need to adjust the LCD timing
  1367. * parameters when the CPU clock is adjusted by the power management
  1368. * subsystem.
  1369. *
  1370. * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
  1371. */
  1372. static int
  1373. pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
  1374. {
  1375. struct pxafb_info *fbi = TO_INF(nb, freq_transition);
  1376. /* TODO struct cpufreq_freqs *f = data; */
  1377. u_int pcd;
  1378. switch (val) {
  1379. case CPUFREQ_PRECHANGE:
  1380. #ifdef CONFIG_FB_PXA_OVERLAY
  1381. if (!(fbi->overlay[0].usage || fbi->overlay[1].usage))
  1382. #endif
  1383. set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
  1384. break;
  1385. case CPUFREQ_POSTCHANGE:
  1386. pcd = get_pcd(fbi, fbi->fb.var.pixclock);
  1387. set_hsync_time(fbi, pcd);
  1388. fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
  1389. LCCR3_PixClkDiv(pcd);
  1390. set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
  1391. break;
  1392. }
  1393. return 0;
  1394. }
  1395. static int
  1396. pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
  1397. {
  1398. struct pxafb_info *fbi = TO_INF(nb, freq_policy);
  1399. struct fb_var_screeninfo *var = &fbi->fb.var;
  1400. struct cpufreq_policy *policy = data;
  1401. switch (val) {
  1402. case CPUFREQ_ADJUST:
  1403. case CPUFREQ_INCOMPATIBLE:
  1404. pr_debug("min dma period: %d ps, "
  1405. "new clock %d kHz\n", pxafb_display_dma_period(var),
  1406. policy->max);
  1407. /* TODO: fill in min/max values */
  1408. break;
  1409. }
  1410. return 0;
  1411. }
  1412. #endif
  1413. #ifdef CONFIG_PM
  1414. /*
  1415. * Power management hooks. Note that we won't be called from IRQ context,
  1416. * unlike the blank functions above, so we may sleep.
  1417. */
  1418. static int pxafb_suspend(struct device *dev)
  1419. {
  1420. struct pxafb_info *fbi = dev_get_drvdata(dev);
  1421. set_ctrlr_state(fbi, C_DISABLE_PM);
  1422. return 0;
  1423. }
  1424. static int pxafb_resume(struct device *dev)
  1425. {
  1426. struct pxafb_info *fbi = dev_get_drvdata(dev);
  1427. set_ctrlr_state(fbi, C_ENABLE_PM);
  1428. return 0;
  1429. }
  1430. static const struct dev_pm_ops pxafb_pm_ops = {
  1431. .suspend = pxafb_suspend,
  1432. .resume = pxafb_resume,
  1433. };
  1434. #endif
  1435. static int pxafb_init_video_memory(struct pxafb_info *fbi)
  1436. {
  1437. int size = PAGE_ALIGN(fbi->video_mem_size);
  1438. fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  1439. if (fbi->video_mem == NULL)
  1440. return -ENOMEM;
  1441. fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
  1442. fbi->video_mem_size = size;
  1443. fbi->fb.fix.smem_start = fbi->video_mem_phys;
  1444. fbi->fb.fix.smem_len = fbi->video_mem_size;
  1445. fbi->fb.screen_base = fbi->video_mem;
  1446. return fbi->video_mem ? 0 : -ENOMEM;
  1447. }
  1448. static void pxafb_decode_mach_info(struct pxafb_info *fbi,
  1449. struct pxafb_mach_info *inf)
  1450. {
  1451. unsigned int lcd_conn = inf->lcd_conn;
  1452. struct pxafb_mode_info *m;
  1453. int i;
  1454. fbi->cmap_inverse = inf->cmap_inverse;
  1455. fbi->cmap_static = inf->cmap_static;
  1456. fbi->lccr4 = inf->lccr4;
  1457. switch (lcd_conn & LCD_TYPE_MASK) {
  1458. case LCD_TYPE_MONO_STN:
  1459. fbi->lccr0 = LCCR0_CMS;
  1460. break;
  1461. case LCD_TYPE_MONO_DSTN:
  1462. fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
  1463. break;
  1464. case LCD_TYPE_COLOR_STN:
  1465. fbi->lccr0 = 0;
  1466. break;
  1467. case LCD_TYPE_COLOR_DSTN:
  1468. fbi->lccr0 = LCCR0_SDS;
  1469. break;
  1470. case LCD_TYPE_COLOR_TFT:
  1471. fbi->lccr0 = LCCR0_PAS;
  1472. break;
  1473. case LCD_TYPE_SMART_PANEL:
  1474. fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
  1475. break;
  1476. default:
  1477. /* fall back to backward compatibility way */
  1478. fbi->lccr0 = inf->lccr0;
  1479. fbi->lccr3 = inf->lccr3;
  1480. goto decode_mode;
  1481. }
  1482. if (lcd_conn == LCD_MONO_STN_8BPP)
  1483. fbi->lccr0 |= LCCR0_DPD;
  1484. fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
  1485. fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
  1486. fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
  1487. fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
  1488. decode_mode:
  1489. pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
  1490. /* decide video memory size as follows:
  1491. * 1. default to mode of maximum resolution
  1492. * 2. allow platform to override
  1493. * 3. allow module parameter to override
  1494. */
  1495. for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
  1496. fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
  1497. m->xres * m->yres * m->bpp / 8);
  1498. if (inf->video_mem_size > fbi->video_mem_size)
  1499. fbi->video_mem_size = inf->video_mem_size;
  1500. if (video_mem_size > fbi->video_mem_size)
  1501. fbi->video_mem_size = video_mem_size;
  1502. }
  1503. static struct pxafb_info *pxafb_init_fbinfo(struct device *dev)
  1504. {
  1505. struct pxafb_info *fbi;
  1506. void *addr;
  1507. struct pxafb_mach_info *inf = dev_get_platdata(dev);
  1508. /* Alloc the pxafb_info and pseudo_palette in one step */
  1509. fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
  1510. if (!fbi)
  1511. return NULL;
  1512. memset(fbi, 0, sizeof(struct pxafb_info));
  1513. fbi->dev = dev;
  1514. fbi->clk = clk_get(dev, NULL);
  1515. if (IS_ERR(fbi->clk)) {
  1516. kfree(fbi);
  1517. return NULL;
  1518. }
  1519. strcpy(fbi->fb.fix.id, PXA_NAME);
  1520. fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  1521. fbi->fb.fix.type_aux = 0;
  1522. fbi->fb.fix.xpanstep = 0;
  1523. fbi->fb.fix.ypanstep = 1;
  1524. fbi->fb.fix.ywrapstep = 0;
  1525. fbi->fb.fix.accel = FB_ACCEL_NONE;
  1526. fbi->fb.var.nonstd = 0;
  1527. fbi->fb.var.activate = FB_ACTIVATE_NOW;
  1528. fbi->fb.var.height = -1;
  1529. fbi->fb.var.width = -1;
  1530. fbi->fb.var.accel_flags = FB_ACCELF_TEXT;
  1531. fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
  1532. fbi->fb.fbops = &pxafb_ops;
  1533. fbi->fb.flags = FBINFO_DEFAULT;
  1534. fbi->fb.node = -1;
  1535. addr = fbi;
  1536. addr = addr + sizeof(struct pxafb_info);
  1537. fbi->fb.pseudo_palette = addr;
  1538. fbi->state = C_STARTUP;
  1539. fbi->task_state = (u_char)-1;
  1540. pxafb_decode_mach_info(fbi, inf);
  1541. #ifdef CONFIG_FB_PXA_OVERLAY
  1542. /* place overlay(s) on top of base */
  1543. if (pxafb_overlay_supported())
  1544. fbi->lccr0 |= LCCR0_OUC;
  1545. #endif
  1546. init_waitqueue_head(&fbi->ctrlr_wait);
  1547. INIT_WORK(&fbi->task, pxafb_task);
  1548. mutex_init(&fbi->ctrlr_lock);
  1549. init_completion(&fbi->disable_done);
  1550. return fbi;
  1551. }
  1552. #ifdef CONFIG_FB_PXA_PARAMETERS
  1553. static int parse_opt_mode(struct device *dev, const char *this_opt)
  1554. {
  1555. struct pxafb_mach_info *inf = dev_get_platdata(dev);
  1556. const char *name = this_opt+5;
  1557. unsigned int namelen = strlen(name);
  1558. int res_specified = 0, bpp_specified = 0;
  1559. unsigned int xres = 0, yres = 0, bpp = 0;
  1560. int yres_specified = 0;
  1561. int i;
  1562. for (i = namelen-1; i >= 0; i--) {
  1563. switch (name[i]) {
  1564. case '-':
  1565. namelen = i;
  1566. if (!bpp_specified && !yres_specified) {
  1567. bpp = simple_strtoul(&name[i+1], NULL, 0);
  1568. bpp_specified = 1;
  1569. } else
  1570. goto done;
  1571. break;
  1572. case 'x':
  1573. if (!yres_specified) {
  1574. yres = simple_strtoul(&name[i+1], NULL, 0);
  1575. yres_specified = 1;
  1576. } else
  1577. goto done;
  1578. break;
  1579. case '0' ... '9':
  1580. break;
  1581. default:
  1582. goto done;
  1583. }
  1584. }
  1585. if (i < 0 && yres_specified) {
  1586. xres = simple_strtoul(name, NULL, 0);
  1587. res_specified = 1;
  1588. }
  1589. done:
  1590. if (res_specified) {
  1591. dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
  1592. inf->modes[0].xres = xres; inf->modes[0].yres = yres;
  1593. }
  1594. if (bpp_specified)
  1595. switch (bpp) {
  1596. case 1:
  1597. case 2:
  1598. case 4:
  1599. case 8:
  1600. case 16:
  1601. inf->modes[0].bpp = bpp;
  1602. dev_info(dev, "overriding bit depth: %d\n", bpp);
  1603. break;
  1604. default:
  1605. dev_err(dev, "Depth %d is not valid\n", bpp);
  1606. return -EINVAL;
  1607. }
  1608. return 0;
  1609. }
  1610. static int parse_opt(struct device *dev, char *this_opt)
  1611. {
  1612. struct pxafb_mach_info *inf = dev_get_platdata(dev);
  1613. struct pxafb_mode_info *mode = &inf->modes[0];
  1614. char s[64];
  1615. s[0] = '\0';
  1616. if (!strncmp(this_opt, "vmem:", 5)) {
  1617. video_mem_size = memparse(this_opt + 5, NULL);
  1618. } else if (!strncmp(this_opt, "mode:", 5)) {
  1619. return parse_opt_mode(dev, this_opt);
  1620. } else if (!strncmp(this_opt, "pixclock:", 9)) {
  1621. mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
  1622. sprintf(s, "pixclock: %ld\n", mode->pixclock);
  1623. } else if (!strncmp(this_opt, "left:", 5)) {
  1624. mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
  1625. sprintf(s, "left: %u\n", mode->left_margin);
  1626. } else if (!strncmp(this_opt, "right:", 6)) {
  1627. mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
  1628. sprintf(s, "right: %u\n", mode->right_margin);
  1629. } else if (!strncmp(this_opt, "upper:", 6)) {
  1630. mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
  1631. sprintf(s, "upper: %u\n", mode->upper_margin);
  1632. } else if (!strncmp(this_opt, "lower:", 6)) {
  1633. mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
  1634. sprintf(s, "lower: %u\n", mode->lower_margin);
  1635. } else if (!strncmp(this_opt, "hsynclen:", 9)) {
  1636. mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
  1637. sprintf(s, "hsynclen: %u\n", mode->hsync_len);
  1638. } else if (!strncmp(this_opt, "vsynclen:", 9)) {
  1639. mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
  1640. sprintf(s, "vsynclen: %u\n", mode->vsync_len);
  1641. } else if (!strncmp(this_opt, "hsync:", 6)) {
  1642. if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
  1643. sprintf(s, "hsync: Active Low\n");
  1644. mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
  1645. } else {
  1646. sprintf(s, "hsync: Active High\n");
  1647. mode->sync |= FB_SYNC_HOR_HIGH_ACT;
  1648. }
  1649. } else if (!strncmp(this_opt, "vsync:", 6)) {
  1650. if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
  1651. sprintf(s, "vsync: Active Low\n");
  1652. mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
  1653. } else {
  1654. sprintf(s, "vsync: Active High\n");
  1655. mode->sync |= FB_SYNC_VERT_HIGH_ACT;
  1656. }
  1657. } else if (!strncmp(this_opt, "dpc:", 4)) {
  1658. if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
  1659. sprintf(s, "double pixel clock: false\n");
  1660. inf->lccr3 &= ~LCCR3_DPC;
  1661. } else {
  1662. sprintf(s, "double pixel clock: true\n");
  1663. inf->lccr3 |= LCCR3_DPC;
  1664. }
  1665. } else if (!strncmp(this_opt, "outputen:", 9)) {
  1666. if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
  1667. sprintf(s, "output enable: active low\n");
  1668. inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
  1669. } else {
  1670. sprintf(s, "output enable: active high\n");
  1671. inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
  1672. }
  1673. } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
  1674. if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
  1675. sprintf(s, "pixel clock polarity: falling edge\n");
  1676. inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
  1677. } else {
  1678. sprintf(s, "pixel clock polarity: rising edge\n");
  1679. inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
  1680. }
  1681. } else if (!strncmp(this_opt, "color", 5)) {
  1682. inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
  1683. } else if (!strncmp(this_opt, "mono", 4)) {
  1684. inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
  1685. } else if (!strncmp(this_opt, "active", 6)) {
  1686. inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
  1687. } else if (!strncmp(this_opt, "passive", 7)) {
  1688. inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
  1689. } else if (!strncmp(this_opt, "single", 6)) {
  1690. inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
  1691. } else if (!strncmp(this_opt, "dual", 4)) {
  1692. inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
  1693. } else if (!strncmp(this_opt, "4pix", 4)) {
  1694. inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
  1695. } else if (!strncmp(this_opt, "8pix", 4)) {
  1696. inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
  1697. } else {
  1698. dev_err(dev, "unknown option: %s\n", this_opt);
  1699. return -EINVAL;
  1700. }
  1701. if (s[0] != '\0')
  1702. dev_info(dev, "override %s", s);
  1703. return 0;
  1704. }
  1705. static int pxafb_parse_options(struct device *dev, char *options)
  1706. {
  1707. char *this_opt;
  1708. int ret;
  1709. if (!options || !*options)
  1710. return 0;
  1711. dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
  1712. /* could be made table driven or similar?... */
  1713. while ((this_opt = strsep(&options, ",")) != NULL) {
  1714. ret = parse_opt(dev, this_opt);
  1715. if (ret)
  1716. return ret;
  1717. }
  1718. return 0;
  1719. }
  1720. static char g_options[256] = "";
  1721. #ifndef MODULE
  1722. static int __init pxafb_setup_options(void)
  1723. {
  1724. char *options = NULL;
  1725. if (fb_get_options("pxafb", &options))
  1726. return -ENODEV;
  1727. if (options)
  1728. strlcpy(g_options, options, sizeof(g_options));
  1729. return 0;
  1730. }
  1731. #else
  1732. #define pxafb_setup_options() (0)
  1733. module_param_string(options, g_options, sizeof(g_options), 0);
  1734. MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
  1735. #endif
  1736. #else
  1737. #define pxafb_parse_options(...) (0)
  1738. #define pxafb_setup_options() (0)
  1739. #endif
  1740. #ifdef DEBUG_VAR
  1741. /* Check for various illegal bit-combinations. Currently only
  1742. * a warning is given. */
  1743. static void pxafb_check_options(struct device *dev, struct pxafb_mach_info *inf)
  1744. {
  1745. if (inf->lcd_conn)
  1746. return;
  1747. if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
  1748. dev_warn(dev, "machine LCCR0 setting contains "
  1749. "illegal bits: %08x\n",
  1750. inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
  1751. if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
  1752. dev_warn(dev, "machine LCCR3 setting contains "
  1753. "illegal bits: %08x\n",
  1754. inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
  1755. if (inf->lccr0 & LCCR0_DPD &&
  1756. ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
  1757. (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
  1758. (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
  1759. dev_warn(dev, "Double Pixel Data (DPD) mode is "
  1760. "only valid in passive mono"
  1761. " single panel mode\n");
  1762. if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
  1763. (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
  1764. dev_warn(dev, "Dual panel only valid in passive mode\n");
  1765. if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
  1766. (inf->modes->upper_margin || inf->modes->lower_margin))
  1767. dev_warn(dev, "Upper and lower margins must be 0 in "
  1768. "passive mode\n");
  1769. }
  1770. #else
  1771. #define pxafb_check_options(...) do {} while (0)
  1772. #endif
  1773. static int pxafb_probe(struct platform_device *dev)
  1774. {
  1775. struct pxafb_info *fbi;
  1776. struct pxafb_mach_info *inf;
  1777. struct resource *r;
  1778. int irq, ret;
  1779. dev_dbg(&dev->dev, "pxafb_probe\n");
  1780. inf = dev_get_platdata(&dev->dev);
  1781. ret = -ENOMEM;
  1782. fbi = NULL;
  1783. if (!inf)
  1784. goto failed;
  1785. ret = pxafb_parse_options(&dev->dev, g_options);
  1786. if (ret < 0)
  1787. goto failed;
  1788. pxafb_check_options(&dev->dev, inf);
  1789. dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
  1790. inf->modes->xres,
  1791. inf->modes->yres,
  1792. inf->modes->bpp);
  1793. if (inf->modes->xres == 0 ||
  1794. inf->modes->yres == 0 ||
  1795. inf->modes->bpp == 0) {
  1796. dev_err(&dev->dev, "Invalid resolution or bit depth\n");
  1797. ret = -EINVAL;
  1798. goto failed;
  1799. }
  1800. fbi = pxafb_init_fbinfo(&dev->dev);
  1801. if (!fbi) {
  1802. /* only reason for pxafb_init_fbinfo to fail is kmalloc */
  1803. dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
  1804. ret = -ENOMEM;
  1805. goto failed;
  1806. }
  1807. if (cpu_is_pxa3xx() && inf->acceleration_enabled)
  1808. fbi->fb.fix.accel = FB_ACCEL_PXA3XX;
  1809. fbi->backlight_power = inf->pxafb_backlight_power;
  1810. fbi->lcd_power = inf->pxafb_lcd_power;
  1811. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1812. if (r == NULL) {
  1813. dev_err(&dev->dev, "no I/O memory resource defined\n");
  1814. ret = -ENODEV;
  1815. goto failed_fbi;
  1816. }
  1817. r = request_mem_region(r->start, resource_size(r), dev->name);
  1818. if (r == NULL) {
  1819. dev_err(&dev->dev, "failed to request I/O memory\n");
  1820. ret = -EBUSY;
  1821. goto failed_fbi;
  1822. }
  1823. fbi->mmio_base = ioremap(r->start, resource_size(r));
  1824. if (fbi->mmio_base == NULL) {
  1825. dev_err(&dev->dev, "failed to map I/O memory\n");
  1826. ret = -EBUSY;
  1827. goto failed_free_res;
  1828. }
  1829. fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
  1830. fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
  1831. &fbi->dma_buff_phys, GFP_KERNEL);
  1832. if (fbi->dma_buff == NULL) {
  1833. dev_err(&dev->dev, "failed to allocate memory for DMA\n");
  1834. ret = -ENOMEM;
  1835. goto failed_free_io;
  1836. }
  1837. ret = pxafb_init_video_memory(fbi);
  1838. if (ret) {
  1839. dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
  1840. ret = -ENOMEM;
  1841. goto failed_free_dma;
  1842. }
  1843. irq = platform_get_irq(dev, 0);
  1844. if (irq < 0) {
  1845. dev_err(&dev->dev, "no IRQ defined\n");
  1846. ret = -ENODEV;
  1847. goto failed_free_mem;
  1848. }
  1849. ret = request_irq(irq, pxafb_handle_irq, 0, "LCD", fbi);
  1850. if (ret) {
  1851. dev_err(&dev->dev, "request_irq failed: %d\n", ret);
  1852. ret = -EBUSY;
  1853. goto failed_free_mem;
  1854. }
  1855. ret = pxafb_smart_init(fbi);
  1856. if (ret) {
  1857. dev_err(&dev->dev, "failed to initialize smartpanel\n");
  1858. goto failed_free_irq;
  1859. }
  1860. /*
  1861. * This makes sure that our colour bitfield
  1862. * descriptors are correctly initialised.
  1863. */
  1864. ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
  1865. if (ret) {
  1866. dev_err(&dev->dev, "failed to get suitable mode\n");
  1867. goto failed_free_irq;
  1868. }
  1869. ret = pxafb_set_par(&fbi->fb);
  1870. if (ret) {
  1871. dev_err(&dev->dev, "Failed to set parameters\n");
  1872. goto failed_free_irq;
  1873. }
  1874. platform_set_drvdata(dev, fbi);
  1875. ret = register_framebuffer(&fbi->fb);
  1876. if (ret < 0) {
  1877. dev_err(&dev->dev,
  1878. "Failed to register framebuffer device: %d\n", ret);
  1879. goto failed_free_cmap;
  1880. }
  1881. pxafb_overlay_init(fbi);
  1882. #ifdef CONFIG_CPU_FREQ
  1883. fbi->freq_transition.notifier_call = pxafb_freq_transition;
  1884. fbi->freq_policy.notifier_call = pxafb_freq_policy;
  1885. cpufreq_register_notifier(&fbi->freq_transition,
  1886. CPUFREQ_TRANSITION_NOTIFIER);
  1887. cpufreq_register_notifier(&fbi->freq_policy,
  1888. CPUFREQ_POLICY_NOTIFIER);
  1889. #endif
  1890. /*
  1891. * Ok, now enable the LCD controller
  1892. */
  1893. set_ctrlr_state(fbi, C_ENABLE);
  1894. return 0;
  1895. failed_free_cmap:
  1896. if (fbi->fb.cmap.len)
  1897. fb_dealloc_cmap(&fbi->fb.cmap);
  1898. failed_free_irq:
  1899. free_irq(irq, fbi);
  1900. failed_free_mem:
  1901. free_pages_exact(fbi->video_mem, fbi->video_mem_size);
  1902. failed_free_dma:
  1903. dma_free_coherent(&dev->dev, fbi->dma_buff_size,
  1904. fbi->dma_buff, fbi->dma_buff_phys);
  1905. failed_free_io:
  1906. iounmap(fbi->mmio_base);
  1907. failed_free_res:
  1908. release_mem_region(r->start, resource_size(r));
  1909. failed_fbi:
  1910. clk_put(fbi->clk);
  1911. kfree(fbi);
  1912. failed:
  1913. return ret;
  1914. }
  1915. static int pxafb_remove(struct platform_device *dev)
  1916. {
  1917. struct pxafb_info *fbi = platform_get_drvdata(dev);
  1918. struct resource *r;
  1919. int irq;
  1920. struct fb_info *info;
  1921. if (!fbi)
  1922. return 0;
  1923. info = &fbi->fb;
  1924. pxafb_overlay_exit(fbi);
  1925. unregister_framebuffer(info);
  1926. pxafb_disable_controller(fbi);
  1927. if (fbi->fb.cmap.len)
  1928. fb_dealloc_cmap(&fbi->fb.cmap);
  1929. irq = platform_get_irq(dev, 0);
  1930. free_irq(irq, fbi);
  1931. free_pages_exact(fbi->video_mem, fbi->video_mem_size);
  1932. dma_free_writecombine(&dev->dev, fbi->dma_buff_size,
  1933. fbi->dma_buff, fbi->dma_buff_phys);
  1934. iounmap(fbi->mmio_base);
  1935. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1936. release_mem_region(r->start, resource_size(r));
  1937. clk_put(fbi->clk);
  1938. kfree(fbi);
  1939. return 0;
  1940. }
  1941. static struct platform_driver pxafb_driver = {
  1942. .probe = pxafb_probe,
  1943. .remove = pxafb_remove,
  1944. .driver = {
  1945. .name = "pxa2xx-fb",
  1946. #ifdef CONFIG_PM
  1947. .pm = &pxafb_pm_ops,
  1948. #endif
  1949. },
  1950. };
  1951. static int __init pxafb_init(void)
  1952. {
  1953. if (pxafb_setup_options())
  1954. return -EINVAL;
  1955. return platform_driver_register(&pxafb_driver);
  1956. }
  1957. static void __exit pxafb_exit(void)
  1958. {
  1959. platform_driver_unregister(&pxafb_driver);
  1960. }
  1961. module_init(pxafb_init);
  1962. module_exit(pxafb_exit);
  1963. MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
  1964. MODULE_LICENSE("GPL");