pxafb.c 64 KB

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