omapfb-main.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  1. /*
  2. * linux/drivers/video/omap2/omapfb-main.c
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * Some code and ideas taken from drivers/video/omap/ driver
  8. * by Imre Deak.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/fb.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/device.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/omapfb.h>
  31. #include <plat/display.h>
  32. #include <plat/vram.h>
  33. #include <plat/vrfb.h>
  34. #include "omapfb.h"
  35. #define MODULE_NAME "omapfb"
  36. #define OMAPFB_PLANE_XRES_MIN 8
  37. #define OMAPFB_PLANE_YRES_MIN 8
  38. static char *def_mode;
  39. static char *def_vram;
  40. static int def_vrfb;
  41. static int def_rotate;
  42. static int def_mirror;
  43. #ifdef DEBUG
  44. unsigned int omapfb_debug;
  45. module_param_named(debug, omapfb_debug, bool, 0644);
  46. static unsigned int omapfb_test_pattern;
  47. module_param_named(test, omapfb_test_pattern, bool, 0644);
  48. #endif
  49. static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi);
  50. static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
  51. struct omap_dss_device *dssdev);
  52. #ifdef DEBUG
  53. static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
  54. {
  55. struct fb_var_screeninfo *var = &fbi->var;
  56. struct fb_fix_screeninfo *fix = &fbi->fix;
  57. void __iomem *addr = fbi->screen_base;
  58. const unsigned bytespp = var->bits_per_pixel >> 3;
  59. const unsigned line_len = fix->line_length / bytespp;
  60. int r = (color >> 16) & 0xff;
  61. int g = (color >> 8) & 0xff;
  62. int b = (color >> 0) & 0xff;
  63. if (var->bits_per_pixel == 16) {
  64. u16 __iomem *p = (u16 __iomem *)addr;
  65. p += y * line_len + x;
  66. r = r * 32 / 256;
  67. g = g * 64 / 256;
  68. b = b * 32 / 256;
  69. __raw_writew((r << 11) | (g << 5) | (b << 0), p);
  70. } else if (var->bits_per_pixel == 24) {
  71. u8 __iomem *p = (u8 __iomem *)addr;
  72. p += (y * line_len + x) * 3;
  73. __raw_writeb(b, p + 0);
  74. __raw_writeb(g, p + 1);
  75. __raw_writeb(r, p + 2);
  76. } else if (var->bits_per_pixel == 32) {
  77. u32 __iomem *p = (u32 __iomem *)addr;
  78. p += y * line_len + x;
  79. __raw_writel(color, p);
  80. }
  81. }
  82. static void fill_fb(struct fb_info *fbi)
  83. {
  84. struct fb_var_screeninfo *var = &fbi->var;
  85. const short w = var->xres_virtual;
  86. const short h = var->yres_virtual;
  87. void __iomem *addr = fbi->screen_base;
  88. int y, x;
  89. if (!addr)
  90. return;
  91. DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
  92. for (y = 0; y < h; y++) {
  93. for (x = 0; x < w; x++) {
  94. if (x < 20 && y < 20)
  95. draw_pixel(fbi, x, y, 0xffffff);
  96. else if (x < 20 && (y > 20 && y < h - 20))
  97. draw_pixel(fbi, x, y, 0xff);
  98. else if (y < 20 && (x > 20 && x < w - 20))
  99. draw_pixel(fbi, x, y, 0xff00);
  100. else if (x > w - 20 && (y > 20 && y < h - 20))
  101. draw_pixel(fbi, x, y, 0xff0000);
  102. else if (y > h - 20 && (x > 20 && x < w - 20))
  103. draw_pixel(fbi, x, y, 0xffff00);
  104. else if (x == 20 || x == w - 20 ||
  105. y == 20 || y == h - 20)
  106. draw_pixel(fbi, x, y, 0xffffff);
  107. else if (x == y || w - x == h - y)
  108. draw_pixel(fbi, x, y, 0xff00ff);
  109. else if (w - x == y || x == h - y)
  110. draw_pixel(fbi, x, y, 0x00ffff);
  111. else if (x > 20 && y > 20 && x < w - 20 && y < h - 20) {
  112. int t = x * 3 / w;
  113. unsigned r = 0, g = 0, b = 0;
  114. unsigned c;
  115. if (var->bits_per_pixel == 16) {
  116. if (t == 0)
  117. b = (y % 32) * 256 / 32;
  118. else if (t == 1)
  119. g = (y % 64) * 256 / 64;
  120. else if (t == 2)
  121. r = (y % 32) * 256 / 32;
  122. } else {
  123. if (t == 0)
  124. b = (y % 256);
  125. else if (t == 1)
  126. g = (y % 256);
  127. else if (t == 2)
  128. r = (y % 256);
  129. }
  130. c = (r << 16) | (g << 8) | (b << 0);
  131. draw_pixel(fbi, x, y, c);
  132. } else {
  133. draw_pixel(fbi, x, y, 0);
  134. }
  135. }
  136. }
  137. }
  138. #endif
  139. static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
  140. {
  141. const struct vrfb *vrfb = &ofbi->region->vrfb;
  142. unsigned offset;
  143. switch (rot) {
  144. case FB_ROTATE_UR:
  145. offset = 0;
  146. break;
  147. case FB_ROTATE_CW:
  148. offset = vrfb->yoffset;
  149. break;
  150. case FB_ROTATE_UD:
  151. offset = vrfb->yoffset * OMAP_VRFB_LINE_LEN + vrfb->xoffset;
  152. break;
  153. case FB_ROTATE_CCW:
  154. offset = vrfb->xoffset * OMAP_VRFB_LINE_LEN;
  155. break;
  156. default:
  157. BUG();
  158. }
  159. offset *= vrfb->bytespp;
  160. return offset;
  161. }
  162. static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
  163. {
  164. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  165. return ofbi->region->vrfb.paddr[rot]
  166. + omapfb_get_vrfb_offset(ofbi, rot);
  167. } else {
  168. return ofbi->region->paddr;
  169. }
  170. }
  171. static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
  172. {
  173. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  174. return ofbi->region->vrfb.paddr[0];
  175. else
  176. return ofbi->region->paddr;
  177. }
  178. static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
  179. {
  180. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  181. return ofbi->region->vrfb.vaddr[0];
  182. else
  183. return ofbi->region->vaddr;
  184. }
  185. static struct omapfb_colormode omapfb_colormodes[] = {
  186. {
  187. .dssmode = OMAP_DSS_COLOR_UYVY,
  188. .bits_per_pixel = 16,
  189. .nonstd = OMAPFB_COLOR_YUV422,
  190. }, {
  191. .dssmode = OMAP_DSS_COLOR_YUV2,
  192. .bits_per_pixel = 16,
  193. .nonstd = OMAPFB_COLOR_YUY422,
  194. }, {
  195. .dssmode = OMAP_DSS_COLOR_ARGB16,
  196. .bits_per_pixel = 16,
  197. .red = { .length = 4, .offset = 8, .msb_right = 0 },
  198. .green = { .length = 4, .offset = 4, .msb_right = 0 },
  199. .blue = { .length = 4, .offset = 0, .msb_right = 0 },
  200. .transp = { .length = 4, .offset = 12, .msb_right = 0 },
  201. }, {
  202. .dssmode = OMAP_DSS_COLOR_RGB16,
  203. .bits_per_pixel = 16,
  204. .red = { .length = 5, .offset = 11, .msb_right = 0 },
  205. .green = { .length = 6, .offset = 5, .msb_right = 0 },
  206. .blue = { .length = 5, .offset = 0, .msb_right = 0 },
  207. .transp = { .length = 0, .offset = 0, .msb_right = 0 },
  208. }, {
  209. .dssmode = OMAP_DSS_COLOR_RGB24P,
  210. .bits_per_pixel = 24,
  211. .red = { .length = 8, .offset = 16, .msb_right = 0 },
  212. .green = { .length = 8, .offset = 8, .msb_right = 0 },
  213. .blue = { .length = 8, .offset = 0, .msb_right = 0 },
  214. .transp = { .length = 0, .offset = 0, .msb_right = 0 },
  215. }, {
  216. .dssmode = OMAP_DSS_COLOR_RGB24U,
  217. .bits_per_pixel = 32,
  218. .red = { .length = 8, .offset = 16, .msb_right = 0 },
  219. .green = { .length = 8, .offset = 8, .msb_right = 0 },
  220. .blue = { .length = 8, .offset = 0, .msb_right = 0 },
  221. .transp = { .length = 0, .offset = 0, .msb_right = 0 },
  222. }, {
  223. .dssmode = OMAP_DSS_COLOR_ARGB32,
  224. .bits_per_pixel = 32,
  225. .red = { .length = 8, .offset = 16, .msb_right = 0 },
  226. .green = { .length = 8, .offset = 8, .msb_right = 0 },
  227. .blue = { .length = 8, .offset = 0, .msb_right = 0 },
  228. .transp = { .length = 8, .offset = 24, .msb_right = 0 },
  229. }, {
  230. .dssmode = OMAP_DSS_COLOR_RGBA32,
  231. .bits_per_pixel = 32,
  232. .red = { .length = 8, .offset = 24, .msb_right = 0 },
  233. .green = { .length = 8, .offset = 16, .msb_right = 0 },
  234. .blue = { .length = 8, .offset = 8, .msb_right = 0 },
  235. .transp = { .length = 8, .offset = 0, .msb_right = 0 },
  236. }, {
  237. .dssmode = OMAP_DSS_COLOR_RGBX32,
  238. .bits_per_pixel = 32,
  239. .red = { .length = 8, .offset = 24, .msb_right = 0 },
  240. .green = { .length = 8, .offset = 16, .msb_right = 0 },
  241. .blue = { .length = 8, .offset = 8, .msb_right = 0 },
  242. .transp = { .length = 0, .offset = 0, .msb_right = 0 },
  243. },
  244. };
  245. static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
  246. struct omapfb_colormode *color)
  247. {
  248. bool cmp_component(struct fb_bitfield *f1, struct fb_bitfield *f2)
  249. {
  250. return f1->length == f2->length &&
  251. f1->offset == f2->offset &&
  252. f1->msb_right == f2->msb_right;
  253. }
  254. if (var->bits_per_pixel == 0 ||
  255. var->red.length == 0 ||
  256. var->blue.length == 0 ||
  257. var->green.length == 0)
  258. return 0;
  259. return var->bits_per_pixel == color->bits_per_pixel &&
  260. cmp_component(&var->red, &color->red) &&
  261. cmp_component(&var->green, &color->green) &&
  262. cmp_component(&var->blue, &color->blue) &&
  263. cmp_component(&var->transp, &color->transp);
  264. }
  265. static void assign_colormode_to_var(struct fb_var_screeninfo *var,
  266. struct omapfb_colormode *color)
  267. {
  268. var->bits_per_pixel = color->bits_per_pixel;
  269. var->nonstd = color->nonstd;
  270. var->red = color->red;
  271. var->green = color->green;
  272. var->blue = color->blue;
  273. var->transp = color->transp;
  274. }
  275. static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
  276. enum omap_color_mode *mode)
  277. {
  278. enum omap_color_mode dssmode;
  279. int i;
  280. /* first match with nonstd field */
  281. if (var->nonstd) {
  282. for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
  283. struct omapfb_colormode *m = &omapfb_colormodes[i];
  284. if (var->nonstd == m->nonstd) {
  285. assign_colormode_to_var(var, m);
  286. *mode = m->dssmode;
  287. return 0;
  288. }
  289. }
  290. return -EINVAL;
  291. }
  292. /* then try exact match of bpp and colors */
  293. for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
  294. struct omapfb_colormode *m = &omapfb_colormodes[i];
  295. if (cmp_var_to_colormode(var, m)) {
  296. assign_colormode_to_var(var, m);
  297. *mode = m->dssmode;
  298. return 0;
  299. }
  300. }
  301. /* match with bpp if user has not filled color fields
  302. * properly */
  303. switch (var->bits_per_pixel) {
  304. case 1:
  305. dssmode = OMAP_DSS_COLOR_CLUT1;
  306. break;
  307. case 2:
  308. dssmode = OMAP_DSS_COLOR_CLUT2;
  309. break;
  310. case 4:
  311. dssmode = OMAP_DSS_COLOR_CLUT4;
  312. break;
  313. case 8:
  314. dssmode = OMAP_DSS_COLOR_CLUT8;
  315. break;
  316. case 12:
  317. dssmode = OMAP_DSS_COLOR_RGB12U;
  318. break;
  319. case 16:
  320. dssmode = OMAP_DSS_COLOR_RGB16;
  321. break;
  322. case 24:
  323. dssmode = OMAP_DSS_COLOR_RGB24P;
  324. break;
  325. case 32:
  326. dssmode = OMAP_DSS_COLOR_RGB24U;
  327. break;
  328. default:
  329. return -EINVAL;
  330. }
  331. for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
  332. struct omapfb_colormode *m = &omapfb_colormodes[i];
  333. if (dssmode == m->dssmode) {
  334. assign_colormode_to_var(var, m);
  335. *mode = m->dssmode;
  336. return 0;
  337. }
  338. }
  339. return -EINVAL;
  340. }
  341. static int check_fb_res_bounds(struct fb_var_screeninfo *var)
  342. {
  343. int xres_min = OMAPFB_PLANE_XRES_MIN;
  344. int xres_max = 2048;
  345. int yres_min = OMAPFB_PLANE_YRES_MIN;
  346. int yres_max = 2048;
  347. /* XXX: some applications seem to set virtual res to 0. */
  348. if (var->xres_virtual == 0)
  349. var->xres_virtual = var->xres;
  350. if (var->yres_virtual == 0)
  351. var->yres_virtual = var->yres;
  352. if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
  353. return -EINVAL;
  354. if (var->xres < xres_min)
  355. var->xres = xres_min;
  356. if (var->yres < yres_min)
  357. var->yres = yres_min;
  358. if (var->xres > xres_max)
  359. var->xres = xres_max;
  360. if (var->yres > yres_max)
  361. var->yres = yres_max;
  362. if (var->xres > var->xres_virtual)
  363. var->xres = var->xres_virtual;
  364. if (var->yres > var->yres_virtual)
  365. var->yres = var->yres_virtual;
  366. return 0;
  367. }
  368. static void shrink_height(unsigned long max_frame_size,
  369. struct fb_var_screeninfo *var)
  370. {
  371. DBG("can't fit FB into memory, reducing y\n");
  372. var->yres_virtual = max_frame_size /
  373. (var->xres_virtual * var->bits_per_pixel >> 3);
  374. if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
  375. var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
  376. if (var->yres > var->yres_virtual)
  377. var->yres = var->yres_virtual;
  378. }
  379. static void shrink_width(unsigned long max_frame_size,
  380. struct fb_var_screeninfo *var)
  381. {
  382. DBG("can't fit FB into memory, reducing x\n");
  383. var->xres_virtual = max_frame_size / var->yres_virtual /
  384. (var->bits_per_pixel >> 3);
  385. if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
  386. var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
  387. if (var->xres > var->xres_virtual)
  388. var->xres = var->xres_virtual;
  389. }
  390. static int check_vrfb_fb_size(unsigned long region_size,
  391. const struct fb_var_screeninfo *var)
  392. {
  393. unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
  394. var->yres_virtual, var->bits_per_pixel >> 3);
  395. return min_phys_size > region_size ? -EINVAL : 0;
  396. }
  397. static int check_fb_size(const struct omapfb_info *ofbi,
  398. struct fb_var_screeninfo *var)
  399. {
  400. unsigned long max_frame_size = ofbi->region->size;
  401. int bytespp = var->bits_per_pixel >> 3;
  402. unsigned long line_size = var->xres_virtual * bytespp;
  403. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  404. /* One needs to check for both VRFB and OMAPFB limitations. */
  405. if (check_vrfb_fb_size(max_frame_size, var))
  406. shrink_height(omap_vrfb_max_height(
  407. max_frame_size, var->xres_virtual, bytespp) *
  408. line_size, var);
  409. if (check_vrfb_fb_size(max_frame_size, var)) {
  410. DBG("cannot fit FB to memory\n");
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
  416. if (line_size * var->yres_virtual > max_frame_size)
  417. shrink_height(max_frame_size, var);
  418. if (line_size * var->yres_virtual > max_frame_size) {
  419. shrink_width(max_frame_size, var);
  420. line_size = var->xres_virtual * bytespp;
  421. }
  422. if (line_size * var->yres_virtual > max_frame_size) {
  423. DBG("cannot fit FB to memory\n");
  424. return -EINVAL;
  425. }
  426. return 0;
  427. }
  428. /*
  429. * Consider if VRFB assisted rotation is in use and if the virtual space for
  430. * the zero degree view needs to be mapped. The need for mapping also acts as
  431. * the trigger for setting up the hardware on the context in question. This
  432. * ensures that one does not attempt to access the virtual view before the
  433. * hardware is serving the address translations.
  434. */
  435. static int setup_vrfb_rotation(struct fb_info *fbi)
  436. {
  437. struct omapfb_info *ofbi = FB2OFB(fbi);
  438. struct omapfb2_mem_region *rg = ofbi->region;
  439. struct vrfb *vrfb = &rg->vrfb;
  440. struct fb_var_screeninfo *var = &fbi->var;
  441. struct fb_fix_screeninfo *fix = &fbi->fix;
  442. unsigned bytespp;
  443. bool yuv_mode;
  444. enum omap_color_mode mode;
  445. int r;
  446. bool reconf;
  447. if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
  448. return 0;
  449. DBG("setup_vrfb_rotation\n");
  450. r = fb_mode_to_dss_mode(var, &mode);
  451. if (r)
  452. return r;
  453. bytespp = var->bits_per_pixel >> 3;
  454. yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
  455. /* We need to reconfigure VRFB if the resolution changes, if yuv mode
  456. * is enabled/disabled, or if bytes per pixel changes */
  457. /* XXX we shouldn't allow this when framebuffer is mmapped */
  458. reconf = false;
  459. if (yuv_mode != vrfb->yuv_mode)
  460. reconf = true;
  461. else if (bytespp != vrfb->bytespp)
  462. reconf = true;
  463. else if (vrfb->xres != var->xres_virtual ||
  464. vrfb->yres != var->yres_virtual)
  465. reconf = true;
  466. if (vrfb->vaddr[0] && reconf) {
  467. fbi->screen_base = NULL;
  468. fix->smem_start = 0;
  469. fix->smem_len = 0;
  470. iounmap(vrfb->vaddr[0]);
  471. vrfb->vaddr[0] = NULL;
  472. DBG("setup_vrfb_rotation: reset fb\n");
  473. }
  474. if (vrfb->vaddr[0])
  475. return 0;
  476. omap_vrfb_setup(&rg->vrfb, rg->paddr,
  477. var->xres_virtual,
  478. var->yres_virtual,
  479. bytespp, yuv_mode);
  480. /* Now one can ioremap the 0 angle view */
  481. r = omap_vrfb_map_angle(vrfb, var->yres_virtual, 0);
  482. if (r)
  483. return r;
  484. /* used by open/write in fbmem.c */
  485. fbi->screen_base = ofbi->region->vrfb.vaddr[0];
  486. fix->smem_start = ofbi->region->vrfb.paddr[0];
  487. switch (var->nonstd) {
  488. case OMAPFB_COLOR_YUV422:
  489. case OMAPFB_COLOR_YUY422:
  490. fix->line_length =
  491. (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
  492. break;
  493. default:
  494. fix->line_length =
  495. (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
  496. break;
  497. }
  498. fix->smem_len = var->yres_virtual * fix->line_length;
  499. return 0;
  500. }
  501. int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
  502. struct fb_var_screeninfo *var)
  503. {
  504. int i;
  505. for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
  506. struct omapfb_colormode *mode = &omapfb_colormodes[i];
  507. if (dssmode == mode->dssmode) {
  508. assign_colormode_to_var(var, mode);
  509. return 0;
  510. }
  511. }
  512. return -ENOENT;
  513. }
  514. void set_fb_fix(struct fb_info *fbi)
  515. {
  516. struct fb_fix_screeninfo *fix = &fbi->fix;
  517. struct fb_var_screeninfo *var = &fbi->var;
  518. struct omapfb_info *ofbi = FB2OFB(fbi);
  519. struct omapfb2_mem_region *rg = ofbi->region;
  520. DBG("set_fb_fix\n");
  521. /* used by open/write in fbmem.c */
  522. fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
  523. /* used by mmap in fbmem.c */
  524. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  525. switch (var->nonstd) {
  526. case OMAPFB_COLOR_YUV422:
  527. case OMAPFB_COLOR_YUY422:
  528. fix->line_length =
  529. (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
  530. break;
  531. default:
  532. fix->line_length =
  533. (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
  534. break;
  535. }
  536. fix->smem_len = var->yres_virtual * fix->line_length;
  537. } else {
  538. fix->line_length =
  539. (var->xres_virtual * var->bits_per_pixel) >> 3;
  540. fix->smem_len = rg->size;
  541. }
  542. fix->smem_start = omapfb_get_region_paddr(ofbi);
  543. fix->type = FB_TYPE_PACKED_PIXELS;
  544. if (var->nonstd)
  545. fix->visual = FB_VISUAL_PSEUDOCOLOR;
  546. else {
  547. switch (var->bits_per_pixel) {
  548. case 32:
  549. case 24:
  550. case 16:
  551. case 12:
  552. fix->visual = FB_VISUAL_TRUECOLOR;
  553. /* 12bpp is stored in 16 bits */
  554. break;
  555. case 1:
  556. case 2:
  557. case 4:
  558. case 8:
  559. fix->visual = FB_VISUAL_PSEUDOCOLOR;
  560. break;
  561. }
  562. }
  563. fix->accel = FB_ACCEL_NONE;
  564. fix->xpanstep = 1;
  565. fix->ypanstep = 1;
  566. }
  567. /* check new var and possibly modify it to be ok */
  568. int check_fb_var(struct fb_info *fbi, struct fb_var_screeninfo *var)
  569. {
  570. struct omapfb_info *ofbi = FB2OFB(fbi);
  571. struct omap_dss_device *display = fb2display(fbi);
  572. enum omap_color_mode mode = 0;
  573. int i;
  574. int r;
  575. DBG("check_fb_var %d\n", ofbi->id);
  576. WARN_ON(!atomic_read(&ofbi->region->lock_count));
  577. r = fb_mode_to_dss_mode(var, &mode);
  578. if (r) {
  579. DBG("cannot convert var to omap dss mode\n");
  580. return r;
  581. }
  582. for (i = 0; i < ofbi->num_overlays; ++i) {
  583. if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
  584. DBG("invalid mode\n");
  585. return -EINVAL;
  586. }
  587. }
  588. if (var->rotate > 3)
  589. return -EINVAL;
  590. if (check_fb_res_bounds(var))
  591. return -EINVAL;
  592. /* When no memory is allocated ignore the size check */
  593. if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
  594. return -EINVAL;
  595. if (var->xres + var->xoffset > var->xres_virtual)
  596. var->xoffset = var->xres_virtual - var->xres;
  597. if (var->yres + var->yoffset > var->yres_virtual)
  598. var->yoffset = var->yres_virtual - var->yres;
  599. DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
  600. var->xres, var->yres,
  601. var->xres_virtual, var->yres_virtual);
  602. var->height = -1;
  603. var->width = -1;
  604. var->grayscale = 0;
  605. if (display && display->driver->get_timings) {
  606. struct omap_video_timings timings;
  607. display->driver->get_timings(display, &timings);
  608. /* pixclock in ps, the rest in pixclock */
  609. var->pixclock = timings.pixel_clock != 0 ?
  610. KHZ2PICOS(timings.pixel_clock) :
  611. 0;
  612. var->left_margin = timings.hbp;
  613. var->right_margin = timings.hfp;
  614. var->upper_margin = timings.vbp;
  615. var->lower_margin = timings.vfp;
  616. var->hsync_len = timings.hsw;
  617. var->vsync_len = timings.vsw;
  618. } else {
  619. var->pixclock = 0;
  620. var->left_margin = 0;
  621. var->right_margin = 0;
  622. var->upper_margin = 0;
  623. var->lower_margin = 0;
  624. var->hsync_len = 0;
  625. var->vsync_len = 0;
  626. }
  627. /* TODO: get these from panel->config */
  628. var->vmode = FB_VMODE_NONINTERLACED;
  629. var->sync = 0;
  630. return 0;
  631. }
  632. /*
  633. * ---------------------------------------------------------------------------
  634. * fbdev framework callbacks
  635. * ---------------------------------------------------------------------------
  636. */
  637. static int omapfb_open(struct fb_info *fbi, int user)
  638. {
  639. return 0;
  640. }
  641. static int omapfb_release(struct fb_info *fbi, int user)
  642. {
  643. #if 0
  644. struct omapfb_info *ofbi = FB2OFB(fbi);
  645. struct omapfb2_device *fbdev = ofbi->fbdev;
  646. struct omap_dss_device *display = fb2display(fbi);
  647. DBG("Closing fb with plane index %d\n", ofbi->id);
  648. omapfb_lock(fbdev);
  649. if (display && display->get_update_mode && display->update) {
  650. /* XXX this update should be removed, I think. But it's
  651. * good for debugging */
  652. if (display->get_update_mode(display) ==
  653. OMAP_DSS_UPDATE_MANUAL) {
  654. u16 w, h;
  655. if (display->sync)
  656. display->sync(display);
  657. display->get_resolution(display, &w, &h);
  658. display->update(display, 0, 0, w, h);
  659. }
  660. }
  661. if (display && display->sync)
  662. display->sync(display);
  663. omapfb_unlock(fbdev);
  664. #endif
  665. return 0;
  666. }
  667. static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
  668. const struct fb_fix_screeninfo *fix, int rotation)
  669. {
  670. unsigned offset;
  671. offset = var->yoffset * fix->line_length +
  672. var->xoffset * (var->bits_per_pixel >> 3);
  673. return offset;
  674. }
  675. static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
  676. const struct fb_fix_screeninfo *fix, int rotation)
  677. {
  678. unsigned offset;
  679. if (rotation == FB_ROTATE_UD)
  680. offset = (var->yres_virtual - var->yres) *
  681. fix->line_length;
  682. else if (rotation == FB_ROTATE_CW)
  683. offset = (var->yres_virtual - var->yres) *
  684. (var->bits_per_pixel >> 3);
  685. else
  686. offset = 0;
  687. if (rotation == FB_ROTATE_UR)
  688. offset += var->yoffset * fix->line_length +
  689. var->xoffset * (var->bits_per_pixel >> 3);
  690. else if (rotation == FB_ROTATE_UD)
  691. offset -= var->yoffset * fix->line_length +
  692. var->xoffset * (var->bits_per_pixel >> 3);
  693. else if (rotation == FB_ROTATE_CW)
  694. offset -= var->xoffset * fix->line_length +
  695. var->yoffset * (var->bits_per_pixel >> 3);
  696. else if (rotation == FB_ROTATE_CCW)
  697. offset += var->xoffset * fix->line_length +
  698. var->yoffset * (var->bits_per_pixel >> 3);
  699. return offset;
  700. }
  701. static void omapfb_calc_addr(const struct omapfb_info *ofbi,
  702. const struct fb_var_screeninfo *var,
  703. const struct fb_fix_screeninfo *fix,
  704. int rotation, u32 *paddr, void __iomem **vaddr)
  705. {
  706. u32 data_start_p;
  707. void __iomem *data_start_v;
  708. int offset;
  709. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  710. data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
  711. data_start_v = NULL;
  712. } else {
  713. data_start_p = omapfb_get_region_paddr(ofbi);
  714. data_start_v = omapfb_get_region_vaddr(ofbi);
  715. }
  716. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  717. offset = calc_rotation_offset_vrfb(var, fix, rotation);
  718. else
  719. offset = calc_rotation_offset_dma(var, fix, rotation);
  720. data_start_p += offset;
  721. data_start_v += offset;
  722. if (offset)
  723. DBG("offset %d, %d = %d\n",
  724. var->xoffset, var->yoffset, offset);
  725. DBG("paddr %x, vaddr %p\n", data_start_p, data_start_v);
  726. *paddr = data_start_p;
  727. *vaddr = data_start_v;
  728. }
  729. /* setup overlay according to the fb */
  730. int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
  731. u16 posx, u16 posy, u16 outw, u16 outh)
  732. {
  733. int r = 0;
  734. struct omapfb_info *ofbi = FB2OFB(fbi);
  735. struct fb_var_screeninfo *var = &fbi->var;
  736. struct fb_fix_screeninfo *fix = &fbi->fix;
  737. enum omap_color_mode mode = 0;
  738. u32 data_start_p = 0;
  739. void __iomem *data_start_v = NULL;
  740. struct omap_overlay_info info;
  741. int xres, yres;
  742. int screen_width;
  743. int mirror;
  744. int rotation = var->rotate;
  745. int i;
  746. WARN_ON(!atomic_read(&ofbi->region->lock_count));
  747. for (i = 0; i < ofbi->num_overlays; i++) {
  748. if (ovl != ofbi->overlays[i])
  749. continue;
  750. rotation = (rotation + ofbi->rotation[i]) % 4;
  751. break;
  752. }
  753. DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
  754. posx, posy, outw, outh);
  755. if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
  756. xres = var->yres;
  757. yres = var->xres;
  758. } else {
  759. xres = var->xres;
  760. yres = var->yres;
  761. }
  762. if (ofbi->region->size)
  763. omapfb_calc_addr(ofbi, var, fix, rotation,
  764. &data_start_p, &data_start_v);
  765. r = fb_mode_to_dss_mode(var, &mode);
  766. if (r) {
  767. DBG("fb_mode_to_dss_mode failed");
  768. goto err;
  769. }
  770. switch (var->nonstd) {
  771. case OMAPFB_COLOR_YUV422:
  772. case OMAPFB_COLOR_YUY422:
  773. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  774. screen_width = fix->line_length
  775. / (var->bits_per_pixel >> 2);
  776. break;
  777. }
  778. default:
  779. screen_width = fix->line_length / (var->bits_per_pixel >> 3);
  780. break;
  781. }
  782. ovl->get_overlay_info(ovl, &info);
  783. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  784. mirror = 0;
  785. else
  786. mirror = ofbi->mirror;
  787. info.paddr = data_start_p;
  788. info.vaddr = data_start_v;
  789. info.screen_width = screen_width;
  790. info.width = xres;
  791. info.height = yres;
  792. info.color_mode = mode;
  793. info.rotation_type = ofbi->rotation_type;
  794. info.rotation = rotation;
  795. info.mirror = mirror;
  796. info.pos_x = posx;
  797. info.pos_y = posy;
  798. info.out_width = outw;
  799. info.out_height = outh;
  800. r = ovl->set_overlay_info(ovl, &info);
  801. if (r) {
  802. DBG("ovl->setup_overlay_info failed\n");
  803. goto err;
  804. }
  805. return 0;
  806. err:
  807. DBG("setup_overlay failed\n");
  808. return r;
  809. }
  810. /* apply var to the overlay */
  811. int omapfb_apply_changes(struct fb_info *fbi, int init)
  812. {
  813. int r = 0;
  814. struct omapfb_info *ofbi = FB2OFB(fbi);
  815. struct fb_var_screeninfo *var = &fbi->var;
  816. struct omap_overlay *ovl;
  817. u16 posx, posy;
  818. u16 outw, outh;
  819. int i;
  820. #ifdef DEBUG
  821. if (omapfb_test_pattern)
  822. fill_fb(fbi);
  823. #endif
  824. WARN_ON(!atomic_read(&ofbi->region->lock_count));
  825. for (i = 0; i < ofbi->num_overlays; i++) {
  826. ovl = ofbi->overlays[i];
  827. DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
  828. if (ofbi->region->size == 0) {
  829. /* the fb is not available. disable the overlay */
  830. omapfb_overlay_enable(ovl, 0);
  831. if (!init && ovl->manager)
  832. ovl->manager->apply(ovl->manager);
  833. continue;
  834. }
  835. if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
  836. int rotation = (var->rotate + ofbi->rotation[i]) % 4;
  837. if (rotation == FB_ROTATE_CW ||
  838. rotation == FB_ROTATE_CCW) {
  839. outw = var->yres;
  840. outh = var->xres;
  841. } else {
  842. outw = var->xres;
  843. outh = var->yres;
  844. }
  845. } else {
  846. outw = ovl->info.out_width;
  847. outh = ovl->info.out_height;
  848. }
  849. if (init) {
  850. posx = 0;
  851. posy = 0;
  852. } else {
  853. posx = ovl->info.pos_x;
  854. posy = ovl->info.pos_y;
  855. }
  856. r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
  857. if (r)
  858. goto err;
  859. if (!init && ovl->manager)
  860. ovl->manager->apply(ovl->manager);
  861. }
  862. return 0;
  863. err:
  864. DBG("apply_changes failed\n");
  865. return r;
  866. }
  867. /* checks var and eventually tweaks it to something supported,
  868. * DO NOT MODIFY PAR */
  869. static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
  870. {
  871. struct omapfb_info *ofbi = FB2OFB(fbi);
  872. int r;
  873. DBG("check_var(%d)\n", FB2OFB(fbi)->id);
  874. omapfb_get_mem_region(ofbi->region);
  875. r = check_fb_var(fbi, var);
  876. omapfb_put_mem_region(ofbi->region);
  877. return r;
  878. }
  879. /* set the video mode according to info->var */
  880. static int omapfb_set_par(struct fb_info *fbi)
  881. {
  882. struct omapfb_info *ofbi = FB2OFB(fbi);
  883. int r;
  884. DBG("set_par(%d)\n", FB2OFB(fbi)->id);
  885. omapfb_get_mem_region(ofbi->region);
  886. set_fb_fix(fbi);
  887. r = setup_vrfb_rotation(fbi);
  888. if (r)
  889. goto out;
  890. r = omapfb_apply_changes(fbi, 0);
  891. out:
  892. omapfb_put_mem_region(ofbi->region);
  893. return r;
  894. }
  895. static int omapfb_pan_display(struct fb_var_screeninfo *var,
  896. struct fb_info *fbi)
  897. {
  898. struct omapfb_info *ofbi = FB2OFB(fbi);
  899. struct fb_var_screeninfo new_var;
  900. int r;
  901. DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
  902. if (var->xoffset == fbi->var.xoffset &&
  903. var->yoffset == fbi->var.yoffset)
  904. return 0;
  905. new_var = fbi->var;
  906. new_var.xoffset = var->xoffset;
  907. new_var.yoffset = var->yoffset;
  908. fbi->var = new_var;
  909. omapfb_get_mem_region(ofbi->region);
  910. r = omapfb_apply_changes(fbi, 0);
  911. omapfb_put_mem_region(ofbi->region);
  912. return r;
  913. }
  914. static void mmap_user_open(struct vm_area_struct *vma)
  915. {
  916. struct omapfb2_mem_region *rg = vma->vm_private_data;
  917. omapfb_get_mem_region(rg);
  918. atomic_inc(&rg->map_count);
  919. omapfb_put_mem_region(rg);
  920. }
  921. static void mmap_user_close(struct vm_area_struct *vma)
  922. {
  923. struct omapfb2_mem_region *rg = vma->vm_private_data;
  924. omapfb_get_mem_region(rg);
  925. atomic_dec(&rg->map_count);
  926. omapfb_put_mem_region(rg);
  927. }
  928. static struct vm_operations_struct mmap_user_ops = {
  929. .open = mmap_user_open,
  930. .close = mmap_user_close,
  931. };
  932. static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
  933. {
  934. struct omapfb_info *ofbi = FB2OFB(fbi);
  935. struct fb_fix_screeninfo *fix = &fbi->fix;
  936. struct omapfb2_mem_region *rg;
  937. unsigned long off;
  938. unsigned long start;
  939. u32 len;
  940. int r = -EINVAL;
  941. if (vma->vm_end - vma->vm_start == 0)
  942. return 0;
  943. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  944. return -EINVAL;
  945. off = vma->vm_pgoff << PAGE_SHIFT;
  946. rg = omapfb_get_mem_region(ofbi->region);
  947. start = omapfb_get_region_paddr(ofbi);
  948. len = fix->smem_len;
  949. if (off >= len)
  950. goto error;
  951. if ((vma->vm_end - vma->vm_start + off) > len)
  952. goto error;
  953. off += start;
  954. DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
  955. vma->vm_pgoff = off >> PAGE_SHIFT;
  956. vma->vm_flags |= VM_IO | VM_RESERVED;
  957. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  958. vma->vm_ops = &mmap_user_ops;
  959. vma->vm_private_data = rg;
  960. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  961. vma->vm_end - vma->vm_start,
  962. vma->vm_page_prot)) {
  963. r = -EAGAIN;
  964. goto error;
  965. }
  966. /* vm_ops.open won't be called for mmap itself. */
  967. atomic_inc(&rg->map_count);
  968. omapfb_put_mem_region(rg);
  969. return 0;
  970. error:
  971. omapfb_put_mem_region(ofbi->region);
  972. return r;
  973. }
  974. /* Store a single color palette entry into a pseudo palette or the hardware
  975. * palette if one is available. For now we support only 16bpp and thus store
  976. * the entry only to the pseudo palette.
  977. */
  978. static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
  979. u_int blue, u_int transp, int update_hw_pal)
  980. {
  981. /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
  982. /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
  983. struct fb_var_screeninfo *var = &fbi->var;
  984. int r = 0;
  985. enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
  986. /*switch (plane->color_mode) {*/
  987. switch (mode) {
  988. case OMAPFB_COLOR_YUV422:
  989. case OMAPFB_COLOR_YUV420:
  990. case OMAPFB_COLOR_YUY422:
  991. r = -EINVAL;
  992. break;
  993. case OMAPFB_COLOR_CLUT_8BPP:
  994. case OMAPFB_COLOR_CLUT_4BPP:
  995. case OMAPFB_COLOR_CLUT_2BPP:
  996. case OMAPFB_COLOR_CLUT_1BPP:
  997. /*
  998. if (fbdev->ctrl->setcolreg)
  999. r = fbdev->ctrl->setcolreg(regno, red, green, blue,
  1000. transp, update_hw_pal);
  1001. */
  1002. /* Fallthrough */
  1003. r = -EINVAL;
  1004. break;
  1005. case OMAPFB_COLOR_RGB565:
  1006. case OMAPFB_COLOR_RGB444:
  1007. case OMAPFB_COLOR_RGB24P:
  1008. case OMAPFB_COLOR_RGB24U:
  1009. if (r != 0)
  1010. break;
  1011. if (regno < 16) {
  1012. u16 pal;
  1013. pal = ((red >> (16 - var->red.length)) <<
  1014. var->red.offset) |
  1015. ((green >> (16 - var->green.length)) <<
  1016. var->green.offset) |
  1017. (blue >> (16 - var->blue.length));
  1018. ((u32 *)(fbi->pseudo_palette))[regno] = pal;
  1019. }
  1020. break;
  1021. default:
  1022. BUG();
  1023. }
  1024. return r;
  1025. }
  1026. static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  1027. u_int transp, struct fb_info *info)
  1028. {
  1029. DBG("setcolreg\n");
  1030. return _setcolreg(info, regno, red, green, blue, transp, 1);
  1031. }
  1032. static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1033. {
  1034. int count, index, r;
  1035. u16 *red, *green, *blue, *transp;
  1036. u16 trans = 0xffff;
  1037. DBG("setcmap\n");
  1038. red = cmap->red;
  1039. green = cmap->green;
  1040. blue = cmap->blue;
  1041. transp = cmap->transp;
  1042. index = cmap->start;
  1043. for (count = 0; count < cmap->len; count++) {
  1044. if (transp)
  1045. trans = *transp++;
  1046. r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
  1047. count == cmap->len - 1);
  1048. if (r != 0)
  1049. return r;
  1050. }
  1051. return 0;
  1052. }
  1053. static int omapfb_blank(int blank, struct fb_info *fbi)
  1054. {
  1055. struct omapfb_info *ofbi = FB2OFB(fbi);
  1056. struct omapfb2_device *fbdev = ofbi->fbdev;
  1057. struct omap_dss_device *display = fb2display(fbi);
  1058. int do_update = 0;
  1059. int r = 0;
  1060. if (!display)
  1061. return -EINVAL;
  1062. omapfb_lock(fbdev);
  1063. switch (blank) {
  1064. case FB_BLANK_UNBLANK:
  1065. if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
  1066. goto exit;
  1067. if (display->driver->resume)
  1068. r = display->driver->resume(display);
  1069. if (r == 0 && display->driver->get_update_mode &&
  1070. display->driver->get_update_mode(display) ==
  1071. OMAP_DSS_UPDATE_MANUAL)
  1072. do_update = 1;
  1073. break;
  1074. case FB_BLANK_NORMAL:
  1075. /* FB_BLANK_NORMAL could be implemented.
  1076. * Needs DSS additions. */
  1077. case FB_BLANK_VSYNC_SUSPEND:
  1078. case FB_BLANK_HSYNC_SUSPEND:
  1079. case FB_BLANK_POWERDOWN:
  1080. if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
  1081. goto exit;
  1082. if (display->driver->suspend)
  1083. r = display->driver->suspend(display);
  1084. break;
  1085. default:
  1086. r = -EINVAL;
  1087. }
  1088. exit:
  1089. omapfb_unlock(fbdev);
  1090. if (r == 0 && do_update && display->driver->update) {
  1091. u16 w, h;
  1092. display->driver->get_resolution(display, &w, &h);
  1093. r = display->driver->update(display, 0, 0, w, h);
  1094. }
  1095. return r;
  1096. }
  1097. #if 0
  1098. /* XXX fb_read and fb_write are needed for VRFB */
  1099. ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
  1100. size_t count, loff_t *ppos)
  1101. {
  1102. DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
  1103. /* XXX needed for VRFB */
  1104. return count;
  1105. }
  1106. #endif
  1107. static struct fb_ops omapfb_ops = {
  1108. .owner = THIS_MODULE,
  1109. .fb_open = omapfb_open,
  1110. .fb_release = omapfb_release,
  1111. .fb_fillrect = cfb_fillrect,
  1112. .fb_copyarea = cfb_copyarea,
  1113. .fb_imageblit = cfb_imageblit,
  1114. .fb_blank = omapfb_blank,
  1115. .fb_ioctl = omapfb_ioctl,
  1116. .fb_check_var = omapfb_check_var,
  1117. .fb_set_par = omapfb_set_par,
  1118. .fb_pan_display = omapfb_pan_display,
  1119. .fb_mmap = omapfb_mmap,
  1120. .fb_setcolreg = omapfb_setcolreg,
  1121. .fb_setcmap = omapfb_setcmap,
  1122. /*.fb_write = omapfb_write,*/
  1123. };
  1124. static void omapfb_free_fbmem(struct fb_info *fbi)
  1125. {
  1126. struct omapfb_info *ofbi = FB2OFB(fbi);
  1127. struct omapfb2_device *fbdev = ofbi->fbdev;
  1128. struct omapfb2_mem_region *rg;
  1129. rg = ofbi->region;
  1130. WARN_ON(atomic_read(&rg->map_count));
  1131. if (rg->paddr)
  1132. if (omap_vram_free(rg->paddr, rg->size))
  1133. dev_err(fbdev->dev, "VRAM FREE failed\n");
  1134. if (rg->vaddr)
  1135. iounmap(rg->vaddr);
  1136. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  1137. /* unmap the 0 angle rotation */
  1138. if (rg->vrfb.vaddr[0]) {
  1139. iounmap(rg->vrfb.vaddr[0]);
  1140. omap_vrfb_release_ctx(&rg->vrfb);
  1141. rg->vrfb.vaddr[0] = NULL;
  1142. }
  1143. }
  1144. rg->vaddr = NULL;
  1145. rg->paddr = 0;
  1146. rg->alloc = 0;
  1147. rg->size = 0;
  1148. }
  1149. static void clear_fb_info(struct fb_info *fbi)
  1150. {
  1151. memset(&fbi->var, 0, sizeof(fbi->var));
  1152. memset(&fbi->fix, 0, sizeof(fbi->fix));
  1153. strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
  1154. }
  1155. static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
  1156. {
  1157. int i;
  1158. DBG("free all fbmem\n");
  1159. for (i = 0; i < fbdev->num_fbs; i++) {
  1160. struct fb_info *fbi = fbdev->fbs[i];
  1161. omapfb_free_fbmem(fbi);
  1162. clear_fb_info(fbi);
  1163. }
  1164. return 0;
  1165. }
  1166. static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
  1167. unsigned long paddr)
  1168. {
  1169. struct omapfb_info *ofbi = FB2OFB(fbi);
  1170. struct omapfb2_device *fbdev = ofbi->fbdev;
  1171. struct omapfb2_mem_region *rg;
  1172. void __iomem *vaddr;
  1173. int r;
  1174. rg = ofbi->region;
  1175. rg->paddr = 0;
  1176. rg->vaddr = NULL;
  1177. memset(&rg->vrfb, 0, sizeof rg->vrfb);
  1178. rg->size = 0;
  1179. rg->type = 0;
  1180. rg->alloc = false;
  1181. rg->map = false;
  1182. size = PAGE_ALIGN(size);
  1183. if (!paddr) {
  1184. DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
  1185. r = omap_vram_alloc(OMAP_VRAM_MEMTYPE_SDRAM, size, &paddr);
  1186. } else {
  1187. DBG("reserving %lu bytes at %lx for fb %d\n", size, paddr,
  1188. ofbi->id);
  1189. r = omap_vram_reserve(paddr, size);
  1190. }
  1191. if (r) {
  1192. dev_err(fbdev->dev, "failed to allocate framebuffer\n");
  1193. return -ENOMEM;
  1194. }
  1195. if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
  1196. vaddr = ioremap_wc(paddr, size);
  1197. if (!vaddr) {
  1198. dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
  1199. omap_vram_free(paddr, size);
  1200. return -ENOMEM;
  1201. }
  1202. DBG("allocated VRAM paddr %lx, vaddr %p\n", paddr, vaddr);
  1203. } else {
  1204. r = omap_vrfb_request_ctx(&rg->vrfb);
  1205. if (r) {
  1206. dev_err(fbdev->dev, "vrfb create ctx failed\n");
  1207. return r;
  1208. }
  1209. vaddr = NULL;
  1210. }
  1211. rg->paddr = paddr;
  1212. rg->vaddr = vaddr;
  1213. rg->size = size;
  1214. rg->alloc = 1;
  1215. return 0;
  1216. }
  1217. /* allocate fbmem using display resolution as reference */
  1218. static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
  1219. unsigned long paddr)
  1220. {
  1221. struct omapfb_info *ofbi = FB2OFB(fbi);
  1222. struct omapfb2_device *fbdev = ofbi->fbdev;
  1223. struct omap_dss_device *display;
  1224. int bytespp;
  1225. display = fb2display(fbi);
  1226. if (!display)
  1227. return 0;
  1228. switch (omapfb_get_recommended_bpp(fbdev, display)) {
  1229. case 16:
  1230. bytespp = 2;
  1231. break;
  1232. case 24:
  1233. bytespp = 4;
  1234. break;
  1235. default:
  1236. bytespp = 4;
  1237. break;
  1238. }
  1239. if (!size) {
  1240. u16 w, h;
  1241. display->driver->get_resolution(display, &w, &h);
  1242. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  1243. size = max(omap_vrfb_min_phys_size(w, h, bytespp),
  1244. omap_vrfb_min_phys_size(h, w, bytespp));
  1245. DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
  1246. w * h * bytespp, size);
  1247. } else {
  1248. size = w * h * bytespp;
  1249. }
  1250. }
  1251. if (!size)
  1252. return 0;
  1253. return omapfb_alloc_fbmem(fbi, size, paddr);
  1254. }
  1255. static enum omap_color_mode fb_format_to_dss_mode(enum omapfb_color_format fmt)
  1256. {
  1257. enum omap_color_mode mode;
  1258. switch (fmt) {
  1259. case OMAPFB_COLOR_RGB565:
  1260. mode = OMAP_DSS_COLOR_RGB16;
  1261. break;
  1262. case OMAPFB_COLOR_YUV422:
  1263. mode = OMAP_DSS_COLOR_YUV2;
  1264. break;
  1265. case OMAPFB_COLOR_CLUT_8BPP:
  1266. mode = OMAP_DSS_COLOR_CLUT8;
  1267. break;
  1268. case OMAPFB_COLOR_CLUT_4BPP:
  1269. mode = OMAP_DSS_COLOR_CLUT4;
  1270. break;
  1271. case OMAPFB_COLOR_CLUT_2BPP:
  1272. mode = OMAP_DSS_COLOR_CLUT2;
  1273. break;
  1274. case OMAPFB_COLOR_CLUT_1BPP:
  1275. mode = OMAP_DSS_COLOR_CLUT1;
  1276. break;
  1277. case OMAPFB_COLOR_RGB444:
  1278. mode = OMAP_DSS_COLOR_RGB12U;
  1279. break;
  1280. case OMAPFB_COLOR_YUY422:
  1281. mode = OMAP_DSS_COLOR_UYVY;
  1282. break;
  1283. case OMAPFB_COLOR_ARGB16:
  1284. mode = OMAP_DSS_COLOR_ARGB16;
  1285. break;
  1286. case OMAPFB_COLOR_RGB24U:
  1287. mode = OMAP_DSS_COLOR_RGB24U;
  1288. break;
  1289. case OMAPFB_COLOR_RGB24P:
  1290. mode = OMAP_DSS_COLOR_RGB24P;
  1291. break;
  1292. case OMAPFB_COLOR_ARGB32:
  1293. mode = OMAP_DSS_COLOR_ARGB32;
  1294. break;
  1295. case OMAPFB_COLOR_RGBA32:
  1296. mode = OMAP_DSS_COLOR_RGBA32;
  1297. break;
  1298. case OMAPFB_COLOR_RGBX32:
  1299. mode = OMAP_DSS_COLOR_RGBX32;
  1300. break;
  1301. default:
  1302. mode = -EINVAL;
  1303. }
  1304. return mode;
  1305. }
  1306. static int omapfb_parse_vram_param(const char *param, int max_entries,
  1307. unsigned long *sizes, unsigned long *paddrs)
  1308. {
  1309. int fbnum;
  1310. unsigned long size;
  1311. unsigned long paddr = 0;
  1312. char *p, *start;
  1313. start = (char *)param;
  1314. while (1) {
  1315. p = start;
  1316. fbnum = simple_strtoul(p, &p, 10);
  1317. if (p == param)
  1318. return -EINVAL;
  1319. if (*p != ':')
  1320. return -EINVAL;
  1321. if (fbnum >= max_entries)
  1322. return -EINVAL;
  1323. size = memparse(p + 1, &p);
  1324. if (!size)
  1325. return -EINVAL;
  1326. paddr = 0;
  1327. if (*p == '@') {
  1328. paddr = simple_strtoul(p + 1, &p, 16);
  1329. if (!paddr)
  1330. return -EINVAL;
  1331. }
  1332. paddrs[fbnum] = paddr;
  1333. sizes[fbnum] = size;
  1334. if (*p == 0)
  1335. break;
  1336. if (*p != ',')
  1337. return -EINVAL;
  1338. ++p;
  1339. start = p;
  1340. }
  1341. return 0;
  1342. }
  1343. static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
  1344. {
  1345. int i, r;
  1346. unsigned long vram_sizes[10];
  1347. unsigned long vram_paddrs[10];
  1348. memset(&vram_sizes, 0, sizeof(vram_sizes));
  1349. memset(&vram_paddrs, 0, sizeof(vram_paddrs));
  1350. if (def_vram && omapfb_parse_vram_param(def_vram, 10,
  1351. vram_sizes, vram_paddrs)) {
  1352. dev_err(fbdev->dev, "failed to parse vram parameter\n");
  1353. memset(&vram_sizes, 0, sizeof(vram_sizes));
  1354. memset(&vram_paddrs, 0, sizeof(vram_paddrs));
  1355. }
  1356. if (fbdev->dev->platform_data) {
  1357. struct omapfb_platform_data *opd;
  1358. opd = fbdev->dev->platform_data;
  1359. for (i = 0; i < opd->mem_desc.region_cnt; ++i) {
  1360. if (!vram_sizes[i]) {
  1361. unsigned long size;
  1362. unsigned long paddr;
  1363. size = opd->mem_desc.region[i].size;
  1364. paddr = opd->mem_desc.region[i].paddr;
  1365. vram_sizes[i] = size;
  1366. vram_paddrs[i] = paddr;
  1367. }
  1368. }
  1369. }
  1370. for (i = 0; i < fbdev->num_fbs; i++) {
  1371. /* allocate memory automatically only for fb0, or if
  1372. * excplicitly defined with vram or plat data option */
  1373. if (i == 0 || vram_sizes[i] != 0) {
  1374. r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
  1375. vram_sizes[i], vram_paddrs[i]);
  1376. if (r)
  1377. return r;
  1378. }
  1379. }
  1380. for (i = 0; i < fbdev->num_fbs; i++) {
  1381. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  1382. struct omapfb2_mem_region *rg;
  1383. rg = ofbi->region;
  1384. DBG("region%d phys %08x virt %p size=%lu\n",
  1385. i,
  1386. rg->paddr,
  1387. rg->vaddr,
  1388. rg->size);
  1389. }
  1390. return 0;
  1391. }
  1392. int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
  1393. {
  1394. struct omapfb_info *ofbi = FB2OFB(fbi);
  1395. struct omapfb2_device *fbdev = ofbi->fbdev;
  1396. struct omap_dss_device *display = fb2display(fbi);
  1397. struct omapfb2_mem_region *rg = ofbi->region;
  1398. unsigned long old_size = rg->size;
  1399. unsigned long old_paddr = rg->paddr;
  1400. int old_type = rg->type;
  1401. int r;
  1402. if (type > OMAPFB_MEMTYPE_MAX)
  1403. return -EINVAL;
  1404. size = PAGE_ALIGN(size);
  1405. if (old_size == size && old_type == type)
  1406. return 0;
  1407. if (display && display->driver->sync)
  1408. display->driver->sync(display);
  1409. omapfb_free_fbmem(fbi);
  1410. if (size == 0) {
  1411. clear_fb_info(fbi);
  1412. return 0;
  1413. }
  1414. r = omapfb_alloc_fbmem(fbi, size, 0);
  1415. if (r) {
  1416. if (old_size)
  1417. omapfb_alloc_fbmem(fbi, old_size, old_paddr);
  1418. if (rg->size == 0)
  1419. clear_fb_info(fbi);
  1420. return r;
  1421. }
  1422. if (old_size == size)
  1423. return 0;
  1424. if (old_size == 0) {
  1425. DBG("initializing fb %d\n", ofbi->id);
  1426. r = omapfb_fb_init(fbdev, fbi);
  1427. if (r) {
  1428. DBG("omapfb_fb_init failed\n");
  1429. goto err;
  1430. }
  1431. r = omapfb_apply_changes(fbi, 1);
  1432. if (r) {
  1433. DBG("omapfb_apply_changes failed\n");
  1434. goto err;
  1435. }
  1436. } else {
  1437. struct fb_var_screeninfo new_var;
  1438. memcpy(&new_var, &fbi->var, sizeof(new_var));
  1439. r = check_fb_var(fbi, &new_var);
  1440. if (r)
  1441. goto err;
  1442. memcpy(&fbi->var, &new_var, sizeof(fbi->var));
  1443. set_fb_fix(fbi);
  1444. r = setup_vrfb_rotation(fbi);
  1445. if (r)
  1446. goto err;
  1447. }
  1448. return 0;
  1449. err:
  1450. omapfb_free_fbmem(fbi);
  1451. clear_fb_info(fbi);
  1452. return r;
  1453. }
  1454. /* initialize fb_info, var, fix to something sane based on the display */
  1455. static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
  1456. {
  1457. struct fb_var_screeninfo *var = &fbi->var;
  1458. struct omap_dss_device *display = fb2display(fbi);
  1459. struct omapfb_info *ofbi = FB2OFB(fbi);
  1460. int r = 0;
  1461. fbi->fbops = &omapfb_ops;
  1462. fbi->flags = FBINFO_FLAG_DEFAULT;
  1463. fbi->pseudo_palette = fbdev->pseudo_palette;
  1464. if (ofbi->region->size == 0) {
  1465. clear_fb_info(fbi);
  1466. return 0;
  1467. }
  1468. var->nonstd = 0;
  1469. var->bits_per_pixel = 0;
  1470. var->rotate = def_rotate;
  1471. /*
  1472. * Check if there is a default color format set in the board file,
  1473. * and use this format instead the default deducted from the
  1474. * display bpp.
  1475. */
  1476. if (fbdev->dev->platform_data) {
  1477. struct omapfb_platform_data *opd;
  1478. int id = ofbi->id;
  1479. opd = fbdev->dev->platform_data;
  1480. if (opd->mem_desc.region[id].format_used) {
  1481. enum omap_color_mode mode;
  1482. enum omapfb_color_format format;
  1483. format = opd->mem_desc.region[id].format;
  1484. mode = fb_format_to_dss_mode(format);
  1485. if (mode < 0) {
  1486. r = mode;
  1487. goto err;
  1488. }
  1489. r = dss_mode_to_fb_mode(mode, var);
  1490. if (r < 0)
  1491. goto err;
  1492. }
  1493. }
  1494. if (display) {
  1495. u16 w, h;
  1496. int rotation = (var->rotate + ofbi->rotation[0]) % 4;
  1497. display->driver->get_resolution(display, &w, &h);
  1498. if (rotation == FB_ROTATE_CW ||
  1499. rotation == FB_ROTATE_CCW) {
  1500. var->xres = h;
  1501. var->yres = w;
  1502. } else {
  1503. var->xres = w;
  1504. var->yres = h;
  1505. }
  1506. var->xres_virtual = var->xres;
  1507. var->yres_virtual = var->yres;
  1508. if (!var->bits_per_pixel) {
  1509. switch (omapfb_get_recommended_bpp(fbdev, display)) {
  1510. case 16:
  1511. var->bits_per_pixel = 16;
  1512. break;
  1513. case 24:
  1514. var->bits_per_pixel = 32;
  1515. break;
  1516. default:
  1517. dev_err(fbdev->dev, "illegal display "
  1518. "bpp\n");
  1519. return -EINVAL;
  1520. }
  1521. }
  1522. } else {
  1523. /* if there's no display, let's just guess some basic values */
  1524. var->xres = 320;
  1525. var->yres = 240;
  1526. var->xres_virtual = var->xres;
  1527. var->yres_virtual = var->yres;
  1528. if (!var->bits_per_pixel)
  1529. var->bits_per_pixel = 16;
  1530. }
  1531. r = check_fb_var(fbi, var);
  1532. if (r)
  1533. goto err;
  1534. set_fb_fix(fbi);
  1535. r = setup_vrfb_rotation(fbi);
  1536. if (r)
  1537. goto err;
  1538. r = fb_alloc_cmap(&fbi->cmap, 256, 0);
  1539. if (r)
  1540. dev_err(fbdev->dev, "unable to allocate color map memory\n");
  1541. err:
  1542. return r;
  1543. }
  1544. static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
  1545. {
  1546. fb_dealloc_cmap(&fbi->cmap);
  1547. }
  1548. static void omapfb_free_resources(struct omapfb2_device *fbdev)
  1549. {
  1550. int i;
  1551. DBG("free_resources\n");
  1552. if (fbdev == NULL)
  1553. return;
  1554. for (i = 0; i < fbdev->num_fbs; i++)
  1555. unregister_framebuffer(fbdev->fbs[i]);
  1556. /* free the reserved fbmem */
  1557. omapfb_free_all_fbmem(fbdev);
  1558. for (i = 0; i < fbdev->num_fbs; i++) {
  1559. fbinfo_cleanup(fbdev, fbdev->fbs[i]);
  1560. framebuffer_release(fbdev->fbs[i]);
  1561. }
  1562. for (i = 0; i < fbdev->num_displays; i++) {
  1563. if (fbdev->displays[i]->state != OMAP_DSS_DISPLAY_DISABLED)
  1564. fbdev->displays[i]->driver->disable(fbdev->displays[i]);
  1565. omap_dss_put_device(fbdev->displays[i]);
  1566. }
  1567. dev_set_drvdata(fbdev->dev, NULL);
  1568. kfree(fbdev);
  1569. }
  1570. static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
  1571. {
  1572. int r, i;
  1573. fbdev->num_fbs = 0;
  1574. DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
  1575. /* allocate fb_infos */
  1576. for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
  1577. struct fb_info *fbi;
  1578. struct omapfb_info *ofbi;
  1579. fbi = framebuffer_alloc(sizeof(struct omapfb_info),
  1580. fbdev->dev);
  1581. if (fbi == NULL) {
  1582. dev_err(fbdev->dev,
  1583. "unable to allocate memory for plane info\n");
  1584. return -ENOMEM;
  1585. }
  1586. clear_fb_info(fbi);
  1587. fbdev->fbs[i] = fbi;
  1588. ofbi = FB2OFB(fbi);
  1589. ofbi->fbdev = fbdev;
  1590. ofbi->id = i;
  1591. ofbi->region = &fbdev->regions[i];
  1592. ofbi->region->id = i;
  1593. init_rwsem(&ofbi->region->lock);
  1594. /* assign these early, so that fb alloc can use them */
  1595. ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
  1596. OMAP_DSS_ROT_DMA;
  1597. ofbi->mirror = def_mirror;
  1598. fbdev->num_fbs++;
  1599. }
  1600. DBG("fb_infos allocated\n");
  1601. /* assign overlays for the fbs */
  1602. for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
  1603. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  1604. ofbi->overlays[0] = fbdev->overlays[i];
  1605. ofbi->num_overlays = 1;
  1606. }
  1607. /* allocate fb memories */
  1608. r = omapfb_allocate_all_fbs(fbdev);
  1609. if (r) {
  1610. dev_err(fbdev->dev, "failed to allocate fbmem\n");
  1611. return r;
  1612. }
  1613. DBG("fbmems allocated\n");
  1614. /* setup fb_infos */
  1615. for (i = 0; i < fbdev->num_fbs; i++) {
  1616. struct fb_info *fbi = fbdev->fbs[i];
  1617. struct omapfb_info *ofbi = FB2OFB(fbi);
  1618. omapfb_get_mem_region(ofbi->region);
  1619. r = omapfb_fb_init(fbdev, fbi);
  1620. omapfb_put_mem_region(ofbi->region);
  1621. if (r) {
  1622. dev_err(fbdev->dev, "failed to setup fb_info\n");
  1623. return r;
  1624. }
  1625. }
  1626. DBG("fb_infos initialized\n");
  1627. for (i = 0; i < fbdev->num_fbs; i++) {
  1628. r = register_framebuffer(fbdev->fbs[i]);
  1629. if (r != 0) {
  1630. dev_err(fbdev->dev,
  1631. "registering framebuffer %d failed\n", i);
  1632. return r;
  1633. }
  1634. }
  1635. DBG("framebuffers registered\n");
  1636. for (i = 0; i < fbdev->num_fbs; i++) {
  1637. struct fb_info *fbi = fbdev->fbs[i];
  1638. struct omapfb_info *ofbi = FB2OFB(fbi);
  1639. omapfb_get_mem_region(ofbi->region);
  1640. r = omapfb_apply_changes(fbi, 1);
  1641. omapfb_put_mem_region(ofbi->region);
  1642. if (r) {
  1643. dev_err(fbdev->dev, "failed to change mode\n");
  1644. return r;
  1645. }
  1646. }
  1647. /* Enable fb0 */
  1648. if (fbdev->num_fbs > 0) {
  1649. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
  1650. if (ofbi->num_overlays > 0) {
  1651. struct omap_overlay *ovl = ofbi->overlays[0];
  1652. r = omapfb_overlay_enable(ovl, 1);
  1653. if (r) {
  1654. dev_err(fbdev->dev,
  1655. "failed to enable overlay\n");
  1656. return r;
  1657. }
  1658. }
  1659. }
  1660. DBG("create_framebuffers done\n");
  1661. return 0;
  1662. }
  1663. static int omapfb_mode_to_timings(const char *mode_str,
  1664. struct omap_video_timings *timings, u8 *bpp)
  1665. {
  1666. struct fb_info fbi;
  1667. struct fb_var_screeninfo var;
  1668. struct fb_ops fbops;
  1669. int r;
  1670. #ifdef CONFIG_OMAP2_DSS_VENC
  1671. if (strcmp(mode_str, "pal") == 0) {
  1672. *timings = omap_dss_pal_timings;
  1673. *bpp = 24;
  1674. return 0;
  1675. } else if (strcmp(mode_str, "ntsc") == 0) {
  1676. *timings = omap_dss_ntsc_timings;
  1677. *bpp = 24;
  1678. return 0;
  1679. }
  1680. #endif
  1681. /* this is quite a hack, but I wanted to use the modedb and for
  1682. * that we need fb_info and var, so we create dummy ones */
  1683. memset(&fbi, 0, sizeof(fbi));
  1684. memset(&var, 0, sizeof(var));
  1685. memset(&fbops, 0, sizeof(fbops));
  1686. fbi.fbops = &fbops;
  1687. r = fb_find_mode(&var, &fbi, mode_str, NULL, 0, NULL, 24);
  1688. if (r != 0) {
  1689. timings->pixel_clock = PICOS2KHZ(var.pixclock);
  1690. timings->hbp = var.left_margin;
  1691. timings->hfp = var.right_margin;
  1692. timings->vbp = var.upper_margin;
  1693. timings->vfp = var.lower_margin;
  1694. timings->hsw = var.hsync_len;
  1695. timings->vsw = var.vsync_len;
  1696. timings->x_res = var.xres;
  1697. timings->y_res = var.yres;
  1698. switch (var.bits_per_pixel) {
  1699. case 16:
  1700. *bpp = 16;
  1701. break;
  1702. case 24:
  1703. case 32:
  1704. default:
  1705. *bpp = 24;
  1706. break;
  1707. }
  1708. return 0;
  1709. } else {
  1710. return -EINVAL;
  1711. }
  1712. }
  1713. static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
  1714. struct omap_dss_device *display, char *mode_str)
  1715. {
  1716. int r;
  1717. u8 bpp;
  1718. struct omap_video_timings timings, temp_timings;
  1719. r = omapfb_mode_to_timings(mode_str, &timings, &bpp);
  1720. if (r)
  1721. return r;
  1722. fbdev->bpp_overrides[fbdev->num_bpp_overrides].dssdev = display;
  1723. fbdev->bpp_overrides[fbdev->num_bpp_overrides].bpp = bpp;
  1724. ++fbdev->num_bpp_overrides;
  1725. if (display->driver->check_timings) {
  1726. r = display->driver->check_timings(display, &timings);
  1727. if (r)
  1728. return r;
  1729. } else {
  1730. /* If check_timings is not present compare xres and yres */
  1731. if (display->driver->get_timings) {
  1732. display->driver->get_timings(display, &temp_timings);
  1733. if (temp_timings.x_res != timings.x_res ||
  1734. temp_timings.y_res != timings.y_res)
  1735. return -EINVAL;
  1736. }
  1737. }
  1738. if (display->driver->set_timings)
  1739. display->driver->set_timings(display, &timings);
  1740. return 0;
  1741. }
  1742. static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
  1743. struct omap_dss_device *dssdev)
  1744. {
  1745. int i;
  1746. BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
  1747. for (i = 0; i < fbdev->num_bpp_overrides; ++i) {
  1748. if (dssdev == fbdev->bpp_overrides[i].dssdev)
  1749. return fbdev->bpp_overrides[i].bpp;
  1750. }
  1751. return dssdev->driver->get_recommended_bpp(dssdev);
  1752. }
  1753. static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
  1754. {
  1755. char *str, *options, *this_opt;
  1756. int r = 0;
  1757. str = kstrdup(def_mode, GFP_KERNEL);
  1758. if (!str)
  1759. return -ENOMEM;
  1760. options = str;
  1761. while (!r && (this_opt = strsep(&options, ",")) != NULL) {
  1762. char *p, *display_str, *mode_str;
  1763. struct omap_dss_device *display;
  1764. int i;
  1765. p = strchr(this_opt, ':');
  1766. if (!p) {
  1767. r = -EINVAL;
  1768. break;
  1769. }
  1770. *p = 0;
  1771. display_str = this_opt;
  1772. mode_str = p + 1;
  1773. display = NULL;
  1774. for (i = 0; i < fbdev->num_displays; ++i) {
  1775. if (strcmp(fbdev->displays[i]->name,
  1776. display_str) == 0) {
  1777. display = fbdev->displays[i];
  1778. break;
  1779. }
  1780. }
  1781. if (!display) {
  1782. r = -EINVAL;
  1783. break;
  1784. }
  1785. r = omapfb_set_def_mode(fbdev, display, mode_str);
  1786. if (r)
  1787. break;
  1788. }
  1789. kfree(str);
  1790. return r;
  1791. }
  1792. static int omapfb_probe(struct platform_device *pdev)
  1793. {
  1794. struct omapfb2_device *fbdev = NULL;
  1795. int r = 0;
  1796. int i;
  1797. struct omap_overlay *ovl;
  1798. struct omap_dss_device *def_display;
  1799. struct omap_dss_device *dssdev;
  1800. DBG("omapfb_probe\n");
  1801. if (pdev->num_resources != 0) {
  1802. dev_err(&pdev->dev, "probed for an unknown device\n");
  1803. r = -ENODEV;
  1804. goto err0;
  1805. }
  1806. fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
  1807. if (fbdev == NULL) {
  1808. r = -ENOMEM;
  1809. goto err0;
  1810. }
  1811. /* TODO : Replace cpu check with omap_has_vrfb once HAS_FEATURE
  1812. * available for OMAP2 and OMAP3
  1813. */
  1814. if (def_vrfb && !cpu_is_omap24xx() && !cpu_is_omap34xx()) {
  1815. def_vrfb = 0;
  1816. dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
  1817. "ignoring the module parameter vrfb=y\n");
  1818. }
  1819. mutex_init(&fbdev->mtx);
  1820. fbdev->dev = &pdev->dev;
  1821. platform_set_drvdata(pdev, fbdev);
  1822. r = 0;
  1823. fbdev->num_displays = 0;
  1824. dssdev = NULL;
  1825. for_each_dss_dev(dssdev) {
  1826. omap_dss_get_device(dssdev);
  1827. if (!dssdev->driver) {
  1828. dev_err(&pdev->dev, "no driver for display\n");
  1829. r = -ENODEV;
  1830. }
  1831. fbdev->displays[fbdev->num_displays++] = dssdev;
  1832. }
  1833. if (r)
  1834. goto cleanup;
  1835. if (fbdev->num_displays == 0) {
  1836. dev_err(&pdev->dev, "no displays\n");
  1837. r = -EINVAL;
  1838. goto cleanup;
  1839. }
  1840. fbdev->num_overlays = omap_dss_get_num_overlays();
  1841. for (i = 0; i < fbdev->num_overlays; i++)
  1842. fbdev->overlays[i] = omap_dss_get_overlay(i);
  1843. fbdev->num_managers = omap_dss_get_num_overlay_managers();
  1844. for (i = 0; i < fbdev->num_managers; i++)
  1845. fbdev->managers[i] = omap_dss_get_overlay_manager(i);
  1846. if (def_mode && strlen(def_mode) > 0) {
  1847. if (omapfb_parse_def_modes(fbdev))
  1848. dev_warn(&pdev->dev, "cannot parse default modes\n");
  1849. }
  1850. r = omapfb_create_framebuffers(fbdev);
  1851. if (r)
  1852. goto cleanup;
  1853. for (i = 0; i < fbdev->num_managers; i++) {
  1854. struct omap_overlay_manager *mgr;
  1855. mgr = fbdev->managers[i];
  1856. r = mgr->apply(mgr);
  1857. if (r)
  1858. dev_warn(fbdev->dev, "failed to apply dispc config\n");
  1859. }
  1860. DBG("mgr->apply'ed\n");
  1861. /* gfx overlay should be the default one. find a display
  1862. * connected to that, and use it as default display */
  1863. ovl = omap_dss_get_overlay(0);
  1864. if (ovl->manager && ovl->manager->device) {
  1865. def_display = ovl->manager->device;
  1866. } else {
  1867. dev_warn(&pdev->dev, "cannot find default display\n");
  1868. def_display = NULL;
  1869. }
  1870. if (def_display) {
  1871. struct omap_dss_driver *dssdrv = def_display->driver;
  1872. r = def_display->driver->enable(def_display);
  1873. if (r) {
  1874. dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
  1875. def_display->name);
  1876. goto cleanup;
  1877. }
  1878. if (def_display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
  1879. u16 w, h;
  1880. if (dssdrv->enable_te)
  1881. dssdrv->enable_te(def_display, 1);
  1882. if (dssdrv->set_update_mode)
  1883. dssdrv->set_update_mode(def_display,
  1884. OMAP_DSS_UPDATE_MANUAL);
  1885. dssdrv->get_resolution(def_display, &w, &h);
  1886. def_display->driver->update(def_display, 0, 0, w, h);
  1887. } else {
  1888. if (dssdrv->set_update_mode)
  1889. dssdrv->set_update_mode(def_display,
  1890. OMAP_DSS_UPDATE_AUTO);
  1891. }
  1892. }
  1893. DBG("create sysfs for fbs\n");
  1894. r = omapfb_create_sysfs(fbdev);
  1895. if (r) {
  1896. dev_err(fbdev->dev, "failed to create sysfs entries\n");
  1897. goto cleanup;
  1898. }
  1899. return 0;
  1900. cleanup:
  1901. omapfb_free_resources(fbdev);
  1902. err0:
  1903. dev_err(&pdev->dev, "failed to setup omapfb\n");
  1904. return r;
  1905. }
  1906. static int omapfb_remove(struct platform_device *pdev)
  1907. {
  1908. struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
  1909. /* FIXME: wait till completion of pending events */
  1910. omapfb_remove_sysfs(fbdev);
  1911. omapfb_free_resources(fbdev);
  1912. return 0;
  1913. }
  1914. static struct platform_driver omapfb_driver = {
  1915. .probe = omapfb_probe,
  1916. .remove = omapfb_remove,
  1917. .driver = {
  1918. .name = "omapfb",
  1919. .owner = THIS_MODULE,
  1920. },
  1921. };
  1922. static int __init omapfb_init(void)
  1923. {
  1924. DBG("omapfb_init\n");
  1925. if (platform_driver_register(&omapfb_driver)) {
  1926. printk(KERN_ERR "failed to register omapfb driver\n");
  1927. return -ENODEV;
  1928. }
  1929. return 0;
  1930. }
  1931. static void __exit omapfb_exit(void)
  1932. {
  1933. DBG("omapfb_exit\n");
  1934. platform_driver_unregister(&omapfb_driver);
  1935. }
  1936. module_param_named(mode, def_mode, charp, 0);
  1937. module_param_named(vram, def_vram, charp, 0);
  1938. module_param_named(rotate, def_rotate, int, 0);
  1939. module_param_named(vrfb, def_vrfb, bool, 0);
  1940. module_param_named(mirror, def_mirror, bool, 0);
  1941. /* late_initcall to let panel/ctrl drivers loaded first.
  1942. * I guess better option would be a more dynamic approach,
  1943. * so that omapfb reacts to new panels when they are loaded */
  1944. late_initcall(omapfb_init);
  1945. /*module_init(omapfb_init);*/
  1946. module_exit(omapfb_exit);
  1947. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
  1948. MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
  1949. MODULE_LICENSE("GPL v2");