omapfb-main.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360
  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. r = fb_mode_to_dss_mode(var, &mode);
  577. if (r) {
  578. DBG("cannot convert var to omap dss mode\n");
  579. return r;
  580. }
  581. for (i = 0; i < ofbi->num_overlays; ++i) {
  582. if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
  583. DBG("invalid mode\n");
  584. return -EINVAL;
  585. }
  586. }
  587. if (var->rotate < 0 || var->rotate > 3)
  588. return -EINVAL;
  589. if (check_fb_res_bounds(var))
  590. return -EINVAL;
  591. /* When no memory is allocated ignore the size check */
  592. if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
  593. return -EINVAL;
  594. if (var->xres + var->xoffset > var->xres_virtual)
  595. var->xoffset = var->xres_virtual - var->xres;
  596. if (var->yres + var->yoffset > var->yres_virtual)
  597. var->yoffset = var->yres_virtual - var->yres;
  598. DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
  599. var->xres, var->yres,
  600. var->xres_virtual, var->yres_virtual);
  601. var->height = -1;
  602. var->width = -1;
  603. var->grayscale = 0;
  604. if (display && display->driver->get_timings) {
  605. struct omap_video_timings timings;
  606. display->driver->get_timings(display, &timings);
  607. /* pixclock in ps, the rest in pixclock */
  608. var->pixclock = timings.pixel_clock != 0 ?
  609. KHZ2PICOS(timings.pixel_clock) :
  610. 0;
  611. var->left_margin = timings.hfp;
  612. var->right_margin = timings.hbp;
  613. var->upper_margin = timings.vfp;
  614. var->lower_margin = timings.vbp;
  615. var->hsync_len = timings.hsw;
  616. var->vsync_len = timings.vsw;
  617. } else {
  618. var->pixclock = 0;
  619. var->left_margin = 0;
  620. var->right_margin = 0;
  621. var->upper_margin = 0;
  622. var->lower_margin = 0;
  623. var->hsync_len = 0;
  624. var->vsync_len = 0;
  625. }
  626. /* TODO: get these from panel->config */
  627. var->vmode = FB_VMODE_NONINTERLACED;
  628. var->sync = 0;
  629. return 0;
  630. }
  631. /*
  632. * ---------------------------------------------------------------------------
  633. * fbdev framework callbacks
  634. * ---------------------------------------------------------------------------
  635. */
  636. static int omapfb_open(struct fb_info *fbi, int user)
  637. {
  638. return 0;
  639. }
  640. static int omapfb_release(struct fb_info *fbi, int user)
  641. {
  642. #if 0
  643. struct omapfb_info *ofbi = FB2OFB(fbi);
  644. struct omapfb2_device *fbdev = ofbi->fbdev;
  645. struct omap_dss_device *display = fb2display(fbi);
  646. DBG("Closing fb with plane index %d\n", ofbi->id);
  647. omapfb_lock(fbdev);
  648. if (display && display->get_update_mode && display->update) {
  649. /* XXX this update should be removed, I think. But it's
  650. * good for debugging */
  651. if (display->get_update_mode(display) ==
  652. OMAP_DSS_UPDATE_MANUAL) {
  653. u16 w, h;
  654. if (display->sync)
  655. display->sync(display);
  656. display->get_resolution(display, &w, &h);
  657. display->update(display, 0, 0, w, h);
  658. }
  659. }
  660. if (display && display->sync)
  661. display->sync(display);
  662. omapfb_unlock(fbdev);
  663. #endif
  664. return 0;
  665. }
  666. static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
  667. const struct fb_fix_screeninfo *fix, int rotation)
  668. {
  669. unsigned offset;
  670. offset = var->yoffset * fix->line_length +
  671. var->xoffset * (var->bits_per_pixel >> 3);
  672. return offset;
  673. }
  674. static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
  675. const struct fb_fix_screeninfo *fix, int rotation)
  676. {
  677. unsigned offset;
  678. if (rotation == FB_ROTATE_UD)
  679. offset = (var->yres_virtual - var->yres) *
  680. fix->line_length;
  681. else if (rotation == FB_ROTATE_CW)
  682. offset = (var->yres_virtual - var->yres) *
  683. (var->bits_per_pixel >> 3);
  684. else
  685. offset = 0;
  686. if (rotation == FB_ROTATE_UR)
  687. offset += var->yoffset * fix->line_length +
  688. var->xoffset * (var->bits_per_pixel >> 3);
  689. else if (rotation == FB_ROTATE_UD)
  690. offset -= var->yoffset * fix->line_length +
  691. var->xoffset * (var->bits_per_pixel >> 3);
  692. else if (rotation == FB_ROTATE_CW)
  693. offset -= var->xoffset * fix->line_length +
  694. var->yoffset * (var->bits_per_pixel >> 3);
  695. else if (rotation == FB_ROTATE_CCW)
  696. offset += var->xoffset * fix->line_length +
  697. var->yoffset * (var->bits_per_pixel >> 3);
  698. return offset;
  699. }
  700. static void omapfb_calc_addr(const struct omapfb_info *ofbi,
  701. const struct fb_var_screeninfo *var,
  702. const struct fb_fix_screeninfo *fix,
  703. int rotation, u32 *paddr, void __iomem **vaddr)
  704. {
  705. u32 data_start_p;
  706. void __iomem *data_start_v;
  707. int offset;
  708. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  709. data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
  710. data_start_v = NULL;
  711. } else {
  712. data_start_p = omapfb_get_region_paddr(ofbi);
  713. data_start_v = omapfb_get_region_vaddr(ofbi);
  714. }
  715. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  716. offset = calc_rotation_offset_vrfb(var, fix, rotation);
  717. else
  718. offset = calc_rotation_offset_dma(var, fix, rotation);
  719. data_start_p += offset;
  720. data_start_v += offset;
  721. if (offset)
  722. DBG("offset %d, %d = %d\n",
  723. var->xoffset, var->yoffset, offset);
  724. DBG("paddr %x, vaddr %p\n", data_start_p, data_start_v);
  725. *paddr = data_start_p;
  726. *vaddr = data_start_v;
  727. }
  728. /* setup overlay according to the fb */
  729. int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
  730. u16 posx, u16 posy, u16 outw, u16 outh)
  731. {
  732. int r = 0;
  733. struct omapfb_info *ofbi = FB2OFB(fbi);
  734. struct fb_var_screeninfo *var = &fbi->var;
  735. struct fb_fix_screeninfo *fix = &fbi->fix;
  736. enum omap_color_mode mode = 0;
  737. u32 data_start_p = 0;
  738. void __iomem *data_start_v = NULL;
  739. struct omap_overlay_info info;
  740. int xres, yres;
  741. int screen_width;
  742. int mirror;
  743. int rotation = var->rotate;
  744. int i;
  745. for (i = 0; i < ofbi->num_overlays; i++) {
  746. if (ovl != ofbi->overlays[i])
  747. continue;
  748. rotation = (rotation + ofbi->rotation[i]) % 4;
  749. break;
  750. }
  751. DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
  752. posx, posy, outw, outh);
  753. if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
  754. xres = var->yres;
  755. yres = var->xres;
  756. } else {
  757. xres = var->xres;
  758. yres = var->yres;
  759. }
  760. if (ofbi->region->size)
  761. omapfb_calc_addr(ofbi, var, fix, rotation,
  762. &data_start_p, &data_start_v);
  763. r = fb_mode_to_dss_mode(var, &mode);
  764. if (r) {
  765. DBG("fb_mode_to_dss_mode failed");
  766. goto err;
  767. }
  768. switch (var->nonstd) {
  769. case OMAPFB_COLOR_YUV422:
  770. case OMAPFB_COLOR_YUY422:
  771. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  772. screen_width = fix->line_length
  773. / (var->bits_per_pixel >> 2);
  774. break;
  775. }
  776. default:
  777. screen_width = fix->line_length / (var->bits_per_pixel >> 3);
  778. break;
  779. }
  780. ovl->get_overlay_info(ovl, &info);
  781. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  782. mirror = 0;
  783. else
  784. mirror = ofbi->mirror;
  785. info.paddr = data_start_p;
  786. info.vaddr = data_start_v;
  787. info.screen_width = screen_width;
  788. info.width = xres;
  789. info.height = yres;
  790. info.color_mode = mode;
  791. info.rotation_type = ofbi->rotation_type;
  792. info.rotation = rotation;
  793. info.mirror = mirror;
  794. info.pos_x = posx;
  795. info.pos_y = posy;
  796. info.out_width = outw;
  797. info.out_height = outh;
  798. r = ovl->set_overlay_info(ovl, &info);
  799. if (r) {
  800. DBG("ovl->setup_overlay_info failed\n");
  801. goto err;
  802. }
  803. return 0;
  804. err:
  805. DBG("setup_overlay failed\n");
  806. return r;
  807. }
  808. /* apply var to the overlay */
  809. int omapfb_apply_changes(struct fb_info *fbi, int init)
  810. {
  811. int r = 0;
  812. struct omapfb_info *ofbi = FB2OFB(fbi);
  813. struct fb_var_screeninfo *var = &fbi->var;
  814. struct omap_overlay *ovl;
  815. u16 posx, posy;
  816. u16 outw, outh;
  817. int i;
  818. #ifdef DEBUG
  819. if (omapfb_test_pattern)
  820. fill_fb(fbi);
  821. #endif
  822. for (i = 0; i < ofbi->num_overlays; i++) {
  823. ovl = ofbi->overlays[i];
  824. DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
  825. if (ofbi->region->size == 0) {
  826. /* the fb is not available. disable the overlay */
  827. omapfb_overlay_enable(ovl, 0);
  828. if (!init && ovl->manager)
  829. ovl->manager->apply(ovl->manager);
  830. continue;
  831. }
  832. if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
  833. int rotation = (var->rotate + ofbi->rotation[i]) % 4;
  834. if (rotation == FB_ROTATE_CW ||
  835. rotation == FB_ROTATE_CCW) {
  836. outw = var->yres;
  837. outh = var->xres;
  838. } else {
  839. outw = var->xres;
  840. outh = var->yres;
  841. }
  842. } else {
  843. outw = ovl->info.out_width;
  844. outh = ovl->info.out_height;
  845. }
  846. if (init) {
  847. posx = 0;
  848. posy = 0;
  849. } else {
  850. posx = ovl->info.pos_x;
  851. posy = ovl->info.pos_y;
  852. }
  853. r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
  854. if (r)
  855. goto err;
  856. if (!init && ovl->manager)
  857. ovl->manager->apply(ovl->manager);
  858. }
  859. return 0;
  860. err:
  861. DBG("apply_changes failed\n");
  862. return r;
  863. }
  864. /* checks var and eventually tweaks it to something supported,
  865. * DO NOT MODIFY PAR */
  866. static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
  867. {
  868. struct omapfb_info *ofbi = FB2OFB(fbi);
  869. int r;
  870. DBG("check_var(%d)\n", FB2OFB(fbi)->id);
  871. omapfb_get_mem_region(ofbi->region);
  872. r = check_fb_var(fbi, var);
  873. omapfb_put_mem_region(ofbi->region);
  874. return r;
  875. }
  876. /* set the video mode according to info->var */
  877. static int omapfb_set_par(struct fb_info *fbi)
  878. {
  879. struct omapfb_info *ofbi = FB2OFB(fbi);
  880. int r;
  881. DBG("set_par(%d)\n", FB2OFB(fbi)->id);
  882. omapfb_get_mem_region(ofbi->region);
  883. set_fb_fix(fbi);
  884. r = setup_vrfb_rotation(fbi);
  885. if (r)
  886. goto out;
  887. r = omapfb_apply_changes(fbi, 0);
  888. out:
  889. omapfb_put_mem_region(ofbi->region);
  890. return r;
  891. }
  892. static int omapfb_pan_display(struct fb_var_screeninfo *var,
  893. struct fb_info *fbi)
  894. {
  895. struct omapfb_info *ofbi = FB2OFB(fbi);
  896. struct fb_var_screeninfo new_var;
  897. int r;
  898. DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
  899. if (var->xoffset == fbi->var.xoffset &&
  900. var->yoffset == fbi->var.yoffset)
  901. return 0;
  902. new_var = fbi->var;
  903. new_var.xoffset = var->xoffset;
  904. new_var.yoffset = var->yoffset;
  905. fbi->var = new_var;
  906. omapfb_get_mem_region(ofbi->region);
  907. r = omapfb_apply_changes(fbi, 0);
  908. omapfb_put_mem_region(ofbi->region);
  909. return r;
  910. }
  911. static void mmap_user_open(struct vm_area_struct *vma)
  912. {
  913. struct omapfb2_mem_region *rg = vma->vm_private_data;
  914. omapfb_get_mem_region(rg);
  915. atomic_inc(&rg->map_count);
  916. omapfb_put_mem_region(rg);
  917. }
  918. static void mmap_user_close(struct vm_area_struct *vma)
  919. {
  920. struct omapfb2_mem_region *rg = vma->vm_private_data;
  921. omapfb_get_mem_region(rg);
  922. atomic_dec(&rg->map_count);
  923. omapfb_put_mem_region(rg);
  924. }
  925. static struct vm_operations_struct mmap_user_ops = {
  926. .open = mmap_user_open,
  927. .close = mmap_user_close,
  928. };
  929. static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
  930. {
  931. struct omapfb_info *ofbi = FB2OFB(fbi);
  932. struct fb_fix_screeninfo *fix = &fbi->fix;
  933. struct omapfb2_mem_region *rg;
  934. unsigned long off;
  935. unsigned long start;
  936. u32 len;
  937. int r = -EINVAL;
  938. if (vma->vm_end - vma->vm_start == 0)
  939. return 0;
  940. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  941. return -EINVAL;
  942. off = vma->vm_pgoff << PAGE_SHIFT;
  943. rg = omapfb_get_mem_region(ofbi->region);
  944. start = omapfb_get_region_paddr(ofbi);
  945. len = fix->smem_len;
  946. if (off >= len)
  947. goto error;
  948. if ((vma->vm_end - vma->vm_start + off) > len)
  949. goto error;
  950. off += start;
  951. DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
  952. vma->vm_pgoff = off >> PAGE_SHIFT;
  953. vma->vm_flags |= VM_IO | VM_RESERVED;
  954. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  955. vma->vm_ops = &mmap_user_ops;
  956. vma->vm_private_data = rg;
  957. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  958. vma->vm_end - vma->vm_start,
  959. vma->vm_page_prot)) {
  960. r = -EAGAIN;
  961. goto error;
  962. }
  963. /* vm_ops.open won't be called for mmap itself. */
  964. atomic_inc(&rg->map_count);
  965. omapfb_put_mem_region(rg);
  966. return 0;
  967. error:
  968. omapfb_put_mem_region(ofbi->region);
  969. return r;
  970. }
  971. /* Store a single color palette entry into a pseudo palette or the hardware
  972. * palette if one is available. For now we support only 16bpp and thus store
  973. * the entry only to the pseudo palette.
  974. */
  975. static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
  976. u_int blue, u_int transp, int update_hw_pal)
  977. {
  978. /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
  979. /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
  980. struct fb_var_screeninfo *var = &fbi->var;
  981. int r = 0;
  982. enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
  983. /*switch (plane->color_mode) {*/
  984. switch (mode) {
  985. case OMAPFB_COLOR_YUV422:
  986. case OMAPFB_COLOR_YUV420:
  987. case OMAPFB_COLOR_YUY422:
  988. r = -EINVAL;
  989. break;
  990. case OMAPFB_COLOR_CLUT_8BPP:
  991. case OMAPFB_COLOR_CLUT_4BPP:
  992. case OMAPFB_COLOR_CLUT_2BPP:
  993. case OMAPFB_COLOR_CLUT_1BPP:
  994. /*
  995. if (fbdev->ctrl->setcolreg)
  996. r = fbdev->ctrl->setcolreg(regno, red, green, blue,
  997. transp, update_hw_pal);
  998. */
  999. /* Fallthrough */
  1000. r = -EINVAL;
  1001. break;
  1002. case OMAPFB_COLOR_RGB565:
  1003. case OMAPFB_COLOR_RGB444:
  1004. case OMAPFB_COLOR_RGB24P:
  1005. case OMAPFB_COLOR_RGB24U:
  1006. if (r != 0)
  1007. break;
  1008. if (regno < 0) {
  1009. r = -EINVAL;
  1010. break;
  1011. }
  1012. if (regno < 16) {
  1013. u16 pal;
  1014. pal = ((red >> (16 - var->red.length)) <<
  1015. var->red.offset) |
  1016. ((green >> (16 - var->green.length)) <<
  1017. var->green.offset) |
  1018. (blue >> (16 - var->blue.length));
  1019. ((u32 *)(fbi->pseudo_palette))[regno] = pal;
  1020. }
  1021. break;
  1022. default:
  1023. BUG();
  1024. }
  1025. return r;
  1026. }
  1027. static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  1028. u_int transp, struct fb_info *info)
  1029. {
  1030. DBG("setcolreg\n");
  1031. return _setcolreg(info, regno, red, green, blue, transp, 1);
  1032. }
  1033. static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1034. {
  1035. int count, index, r;
  1036. u16 *red, *green, *blue, *transp;
  1037. u16 trans = 0xffff;
  1038. DBG("setcmap\n");
  1039. red = cmap->red;
  1040. green = cmap->green;
  1041. blue = cmap->blue;
  1042. transp = cmap->transp;
  1043. index = cmap->start;
  1044. for (count = 0; count < cmap->len; count++) {
  1045. if (transp)
  1046. trans = *transp++;
  1047. r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
  1048. count == cmap->len - 1);
  1049. if (r != 0)
  1050. return r;
  1051. }
  1052. return 0;
  1053. }
  1054. static int omapfb_blank(int blank, struct fb_info *fbi)
  1055. {
  1056. struct omapfb_info *ofbi = FB2OFB(fbi);
  1057. struct omapfb2_device *fbdev = ofbi->fbdev;
  1058. struct omap_dss_device *display = fb2display(fbi);
  1059. int do_update = 0;
  1060. int r = 0;
  1061. omapfb_lock(fbdev);
  1062. switch (blank) {
  1063. case FB_BLANK_UNBLANK:
  1064. if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
  1065. goto exit;
  1066. if (display->driver->resume)
  1067. r = display->driver->resume(display);
  1068. if (r == 0 && display->driver->get_update_mode &&
  1069. display->driver->get_update_mode(display) ==
  1070. OMAP_DSS_UPDATE_MANUAL)
  1071. do_update = 1;
  1072. break;
  1073. case FB_BLANK_NORMAL:
  1074. /* FB_BLANK_NORMAL could be implemented.
  1075. * Needs DSS additions. */
  1076. case FB_BLANK_VSYNC_SUSPEND:
  1077. case FB_BLANK_HSYNC_SUSPEND:
  1078. case FB_BLANK_POWERDOWN:
  1079. if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
  1080. goto exit;
  1081. if (display->driver->suspend)
  1082. r = display->driver->suspend(display);
  1083. break;
  1084. default:
  1085. r = -EINVAL;
  1086. }
  1087. exit:
  1088. omapfb_unlock(fbdev);
  1089. if (r == 0 && do_update && display->driver->update) {
  1090. u16 w, h;
  1091. display->driver->get_resolution(display, &w, &h);
  1092. r = display->driver->update(display, 0, 0, w, h);
  1093. }
  1094. return r;
  1095. }
  1096. #if 0
  1097. /* XXX fb_read and fb_write are needed for VRFB */
  1098. ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
  1099. size_t count, loff_t *ppos)
  1100. {
  1101. DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
  1102. /* XXX needed for VRFB */
  1103. return count;
  1104. }
  1105. #endif
  1106. static struct fb_ops omapfb_ops = {
  1107. .owner = THIS_MODULE,
  1108. .fb_open = omapfb_open,
  1109. .fb_release = omapfb_release,
  1110. .fb_fillrect = cfb_fillrect,
  1111. .fb_copyarea = cfb_copyarea,
  1112. .fb_imageblit = cfb_imageblit,
  1113. .fb_blank = omapfb_blank,
  1114. .fb_ioctl = omapfb_ioctl,
  1115. .fb_check_var = omapfb_check_var,
  1116. .fb_set_par = omapfb_set_par,
  1117. .fb_pan_display = omapfb_pan_display,
  1118. .fb_mmap = omapfb_mmap,
  1119. .fb_setcolreg = omapfb_setcolreg,
  1120. .fb_setcmap = omapfb_setcmap,
  1121. /*.fb_write = omapfb_write,*/
  1122. };
  1123. static void omapfb_free_fbmem(struct fb_info *fbi)
  1124. {
  1125. struct omapfb_info *ofbi = FB2OFB(fbi);
  1126. struct omapfb2_device *fbdev = ofbi->fbdev;
  1127. struct omapfb2_mem_region *rg;
  1128. rg = ofbi->region;
  1129. WARN_ON(atomic_read(&rg->map_count));
  1130. if (rg->paddr)
  1131. if (omap_vram_free(rg->paddr, rg->size))
  1132. dev_err(fbdev->dev, "VRAM FREE failed\n");
  1133. if (rg->vaddr)
  1134. iounmap(rg->vaddr);
  1135. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  1136. /* unmap the 0 angle rotation */
  1137. if (rg->vrfb.vaddr[0]) {
  1138. iounmap(rg->vrfb.vaddr[0]);
  1139. omap_vrfb_release_ctx(&rg->vrfb);
  1140. rg->vrfb.vaddr[0] = NULL;
  1141. }
  1142. }
  1143. rg->vaddr = NULL;
  1144. rg->paddr = 0;
  1145. rg->alloc = 0;
  1146. rg->size = 0;
  1147. }
  1148. static void clear_fb_info(struct fb_info *fbi)
  1149. {
  1150. memset(&fbi->var, 0, sizeof(fbi->var));
  1151. memset(&fbi->fix, 0, sizeof(fbi->fix));
  1152. strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
  1153. }
  1154. static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
  1155. {
  1156. int i;
  1157. DBG("free all fbmem\n");
  1158. for (i = 0; i < fbdev->num_fbs; i++) {
  1159. struct fb_info *fbi = fbdev->fbs[i];
  1160. omapfb_free_fbmem(fbi);
  1161. clear_fb_info(fbi);
  1162. }
  1163. return 0;
  1164. }
  1165. static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
  1166. unsigned long paddr)
  1167. {
  1168. struct omapfb_info *ofbi = FB2OFB(fbi);
  1169. struct omapfb2_device *fbdev = ofbi->fbdev;
  1170. struct omapfb2_mem_region *rg;
  1171. void __iomem *vaddr;
  1172. int r;
  1173. rg = ofbi->region;
  1174. rg->paddr = 0;
  1175. rg->vaddr = NULL;
  1176. memset(&rg->vrfb, 0, sizeof rg->vrfb);
  1177. rg->size = 0;
  1178. rg->type = 0;
  1179. rg->alloc = false;
  1180. rg->map = false;
  1181. size = PAGE_ALIGN(size);
  1182. if (!paddr) {
  1183. DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
  1184. r = omap_vram_alloc(OMAP_VRAM_MEMTYPE_SDRAM, size, &paddr);
  1185. } else {
  1186. DBG("reserving %lu bytes at %lx for fb %d\n", size, paddr,
  1187. ofbi->id);
  1188. r = omap_vram_reserve(paddr, size);
  1189. }
  1190. if (r) {
  1191. dev_err(fbdev->dev, "failed to allocate framebuffer\n");
  1192. return -ENOMEM;
  1193. }
  1194. if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
  1195. vaddr = ioremap_wc(paddr, size);
  1196. if (!vaddr) {
  1197. dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
  1198. omap_vram_free(paddr, size);
  1199. return -ENOMEM;
  1200. }
  1201. DBG("allocated VRAM paddr %lx, vaddr %p\n", paddr, vaddr);
  1202. } else {
  1203. r = omap_vrfb_request_ctx(&rg->vrfb);
  1204. if (r) {
  1205. dev_err(fbdev->dev, "vrfb create ctx failed\n");
  1206. return r;
  1207. }
  1208. vaddr = NULL;
  1209. }
  1210. rg->paddr = paddr;
  1211. rg->vaddr = vaddr;
  1212. rg->size = size;
  1213. rg->alloc = 1;
  1214. return 0;
  1215. }
  1216. /* allocate fbmem using display resolution as reference */
  1217. static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
  1218. unsigned long paddr)
  1219. {
  1220. struct omapfb_info *ofbi = FB2OFB(fbi);
  1221. struct omapfb2_device *fbdev = ofbi->fbdev;
  1222. struct omap_dss_device *display;
  1223. int bytespp;
  1224. display = fb2display(fbi);
  1225. if (!display)
  1226. return 0;
  1227. switch (omapfb_get_recommended_bpp(fbdev, display)) {
  1228. case 16:
  1229. bytespp = 2;
  1230. break;
  1231. case 24:
  1232. bytespp = 4;
  1233. break;
  1234. default:
  1235. bytespp = 4;
  1236. break;
  1237. }
  1238. if (!size) {
  1239. u16 w, h;
  1240. display->driver->get_resolution(display, &w, &h);
  1241. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  1242. size = max(omap_vrfb_min_phys_size(w, h, bytespp),
  1243. omap_vrfb_min_phys_size(h, w, bytespp));
  1244. DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
  1245. w * h * bytespp, size);
  1246. } else {
  1247. size = w * h * bytespp;
  1248. }
  1249. }
  1250. if (!size)
  1251. return 0;
  1252. return omapfb_alloc_fbmem(fbi, size, paddr);
  1253. }
  1254. static enum omap_color_mode fb_format_to_dss_mode(enum omapfb_color_format fmt)
  1255. {
  1256. enum omap_color_mode mode;
  1257. switch (fmt) {
  1258. case OMAPFB_COLOR_RGB565:
  1259. mode = OMAP_DSS_COLOR_RGB16;
  1260. break;
  1261. case OMAPFB_COLOR_YUV422:
  1262. mode = OMAP_DSS_COLOR_YUV2;
  1263. break;
  1264. case OMAPFB_COLOR_CLUT_8BPP:
  1265. mode = OMAP_DSS_COLOR_CLUT8;
  1266. break;
  1267. case OMAPFB_COLOR_CLUT_4BPP:
  1268. mode = OMAP_DSS_COLOR_CLUT4;
  1269. break;
  1270. case OMAPFB_COLOR_CLUT_2BPP:
  1271. mode = OMAP_DSS_COLOR_CLUT2;
  1272. break;
  1273. case OMAPFB_COLOR_CLUT_1BPP:
  1274. mode = OMAP_DSS_COLOR_CLUT1;
  1275. break;
  1276. case OMAPFB_COLOR_RGB444:
  1277. mode = OMAP_DSS_COLOR_RGB12U;
  1278. break;
  1279. case OMAPFB_COLOR_YUY422:
  1280. mode = OMAP_DSS_COLOR_UYVY;
  1281. break;
  1282. case OMAPFB_COLOR_ARGB16:
  1283. mode = OMAP_DSS_COLOR_ARGB16;
  1284. break;
  1285. case OMAPFB_COLOR_RGB24U:
  1286. mode = OMAP_DSS_COLOR_RGB24U;
  1287. break;
  1288. case OMAPFB_COLOR_RGB24P:
  1289. mode = OMAP_DSS_COLOR_RGB24P;
  1290. break;
  1291. case OMAPFB_COLOR_ARGB32:
  1292. mode = OMAP_DSS_COLOR_ARGB32;
  1293. break;
  1294. case OMAPFB_COLOR_RGBA32:
  1295. mode = OMAP_DSS_COLOR_RGBA32;
  1296. break;
  1297. case OMAPFB_COLOR_RGBX32:
  1298. mode = OMAP_DSS_COLOR_RGBX32;
  1299. break;
  1300. default:
  1301. mode = -EINVAL;
  1302. }
  1303. return mode;
  1304. }
  1305. static int omapfb_parse_vram_param(const char *param, int max_entries,
  1306. unsigned long *sizes, unsigned long *paddrs)
  1307. {
  1308. int fbnum;
  1309. unsigned long size;
  1310. unsigned long paddr = 0;
  1311. char *p, *start;
  1312. start = (char *)param;
  1313. while (1) {
  1314. p = start;
  1315. fbnum = simple_strtoul(p, &p, 10);
  1316. if (p == param)
  1317. return -EINVAL;
  1318. if (*p != ':')
  1319. return -EINVAL;
  1320. if (fbnum >= max_entries)
  1321. return -EINVAL;
  1322. size = memparse(p + 1, &p);
  1323. if (!size)
  1324. return -EINVAL;
  1325. paddr = 0;
  1326. if (*p == '@') {
  1327. paddr = simple_strtoul(p + 1, &p, 16);
  1328. if (!paddr)
  1329. return -EINVAL;
  1330. }
  1331. paddrs[fbnum] = paddr;
  1332. sizes[fbnum] = size;
  1333. if (*p == 0)
  1334. break;
  1335. if (*p != ',')
  1336. return -EINVAL;
  1337. ++p;
  1338. start = p;
  1339. }
  1340. return 0;
  1341. }
  1342. static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
  1343. {
  1344. int i, r;
  1345. unsigned long vram_sizes[10];
  1346. unsigned long vram_paddrs[10];
  1347. memset(&vram_sizes, 0, sizeof(vram_sizes));
  1348. memset(&vram_paddrs, 0, sizeof(vram_paddrs));
  1349. if (def_vram && omapfb_parse_vram_param(def_vram, 10,
  1350. vram_sizes, vram_paddrs)) {
  1351. dev_err(fbdev->dev, "failed to parse vram parameter\n");
  1352. memset(&vram_sizes, 0, sizeof(vram_sizes));
  1353. memset(&vram_paddrs, 0, sizeof(vram_paddrs));
  1354. }
  1355. if (fbdev->dev->platform_data) {
  1356. struct omapfb_platform_data *opd;
  1357. opd = fbdev->dev->platform_data;
  1358. for (i = 0; i < opd->mem_desc.region_cnt; ++i) {
  1359. if (!vram_sizes[i]) {
  1360. unsigned long size;
  1361. unsigned long paddr;
  1362. size = opd->mem_desc.region[i].size;
  1363. paddr = opd->mem_desc.region[i].paddr;
  1364. vram_sizes[i] = size;
  1365. vram_paddrs[i] = paddr;
  1366. }
  1367. }
  1368. }
  1369. for (i = 0; i < fbdev->num_fbs; i++) {
  1370. /* allocate memory automatically only for fb0, or if
  1371. * excplicitly defined with vram or plat data option */
  1372. if (i == 0 || vram_sizes[i] != 0) {
  1373. r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
  1374. vram_sizes[i], vram_paddrs[i]);
  1375. if (r)
  1376. return r;
  1377. }
  1378. }
  1379. for (i = 0; i < fbdev->num_fbs; i++) {
  1380. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  1381. struct omapfb2_mem_region *rg;
  1382. rg = ofbi->region;
  1383. DBG("region%d phys %08x virt %p size=%lu\n",
  1384. i,
  1385. rg->paddr,
  1386. rg->vaddr,
  1387. rg->size);
  1388. }
  1389. return 0;
  1390. }
  1391. int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
  1392. {
  1393. struct omapfb_info *ofbi = FB2OFB(fbi);
  1394. struct omapfb2_device *fbdev = ofbi->fbdev;
  1395. struct omap_dss_device *display = fb2display(fbi);
  1396. struct omapfb2_mem_region *rg = ofbi->region;
  1397. unsigned long old_size = rg->size;
  1398. unsigned long old_paddr = rg->paddr;
  1399. int old_type = rg->type;
  1400. int r;
  1401. if (type > OMAPFB_MEMTYPE_MAX)
  1402. return -EINVAL;
  1403. size = PAGE_ALIGN(size);
  1404. if (old_size == size && old_type == type)
  1405. return 0;
  1406. if (display && display->driver->sync)
  1407. display->driver->sync(display);
  1408. omapfb_free_fbmem(fbi);
  1409. if (size == 0) {
  1410. clear_fb_info(fbi);
  1411. return 0;
  1412. }
  1413. r = omapfb_alloc_fbmem(fbi, size, 0);
  1414. if (r) {
  1415. if (old_size)
  1416. omapfb_alloc_fbmem(fbi, old_size, old_paddr);
  1417. if (rg->size == 0)
  1418. clear_fb_info(fbi);
  1419. return r;
  1420. }
  1421. if (old_size == size)
  1422. return 0;
  1423. if (old_size == 0) {
  1424. DBG("initializing fb %d\n", ofbi->id);
  1425. r = omapfb_fb_init(fbdev, fbi);
  1426. if (r) {
  1427. DBG("omapfb_fb_init failed\n");
  1428. goto err;
  1429. }
  1430. r = omapfb_apply_changes(fbi, 1);
  1431. if (r) {
  1432. DBG("omapfb_apply_changes failed\n");
  1433. goto err;
  1434. }
  1435. } else {
  1436. struct fb_var_screeninfo new_var;
  1437. memcpy(&new_var, &fbi->var, sizeof(new_var));
  1438. r = check_fb_var(fbi, &new_var);
  1439. if (r)
  1440. goto err;
  1441. memcpy(&fbi->var, &new_var, sizeof(fbi->var));
  1442. set_fb_fix(fbi);
  1443. r = setup_vrfb_rotation(fbi);
  1444. if (r)
  1445. goto err;
  1446. }
  1447. return 0;
  1448. err:
  1449. omapfb_free_fbmem(fbi);
  1450. clear_fb_info(fbi);
  1451. return r;
  1452. }
  1453. /* initialize fb_info, var, fix to something sane based on the display */
  1454. static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
  1455. {
  1456. struct fb_var_screeninfo *var = &fbi->var;
  1457. struct omap_dss_device *display = fb2display(fbi);
  1458. struct omapfb_info *ofbi = FB2OFB(fbi);
  1459. int r = 0;
  1460. fbi->fbops = &omapfb_ops;
  1461. fbi->flags = FBINFO_FLAG_DEFAULT;
  1462. fbi->pseudo_palette = fbdev->pseudo_palette;
  1463. if (ofbi->region->size == 0) {
  1464. clear_fb_info(fbi);
  1465. return 0;
  1466. }
  1467. var->nonstd = 0;
  1468. var->bits_per_pixel = 0;
  1469. var->rotate = def_rotate;
  1470. /*
  1471. * Check if there is a default color format set in the board file,
  1472. * and use this format instead the default deducted from the
  1473. * display bpp.
  1474. */
  1475. if (fbdev->dev->platform_data) {
  1476. struct omapfb_platform_data *opd;
  1477. int id = ofbi->id;
  1478. opd = fbdev->dev->platform_data;
  1479. if (opd->mem_desc.region[id].format_used) {
  1480. enum omap_color_mode mode;
  1481. enum omapfb_color_format format;
  1482. format = opd->mem_desc.region[id].format;
  1483. mode = fb_format_to_dss_mode(format);
  1484. if (mode < 0) {
  1485. r = mode;
  1486. goto err;
  1487. }
  1488. r = dss_mode_to_fb_mode(mode, var);
  1489. if (r < 0)
  1490. goto err;
  1491. }
  1492. }
  1493. if (display) {
  1494. u16 w, h;
  1495. int rotation = (var->rotate + ofbi->rotation[0]) % 4;
  1496. display->driver->get_resolution(display, &w, &h);
  1497. if (rotation == FB_ROTATE_CW ||
  1498. rotation == FB_ROTATE_CCW) {
  1499. var->xres = h;
  1500. var->yres = w;
  1501. } else {
  1502. var->xres = w;
  1503. var->yres = h;
  1504. }
  1505. var->xres_virtual = var->xres;
  1506. var->yres_virtual = var->yres;
  1507. if (!var->bits_per_pixel) {
  1508. switch (omapfb_get_recommended_bpp(fbdev, display)) {
  1509. case 16:
  1510. var->bits_per_pixel = 16;
  1511. break;
  1512. case 24:
  1513. var->bits_per_pixel = 32;
  1514. break;
  1515. default:
  1516. dev_err(fbdev->dev, "illegal display "
  1517. "bpp\n");
  1518. return -EINVAL;
  1519. }
  1520. }
  1521. } else {
  1522. /* if there's no display, let's just guess some basic values */
  1523. var->xres = 320;
  1524. var->yres = 240;
  1525. var->xres_virtual = var->xres;
  1526. var->yres_virtual = var->yres;
  1527. if (!var->bits_per_pixel)
  1528. var->bits_per_pixel = 16;
  1529. }
  1530. r = check_fb_var(fbi, var);
  1531. if (r)
  1532. goto err;
  1533. set_fb_fix(fbi);
  1534. r = setup_vrfb_rotation(fbi);
  1535. if (r)
  1536. goto err;
  1537. r = fb_alloc_cmap(&fbi->cmap, 256, 0);
  1538. if (r)
  1539. dev_err(fbdev->dev, "unable to allocate color map memory\n");
  1540. err:
  1541. return r;
  1542. }
  1543. static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
  1544. {
  1545. fb_dealloc_cmap(&fbi->cmap);
  1546. }
  1547. static void omapfb_free_resources(struct omapfb2_device *fbdev)
  1548. {
  1549. int i;
  1550. DBG("free_resources\n");
  1551. if (fbdev == NULL)
  1552. return;
  1553. for (i = 0; i < fbdev->num_fbs; i++)
  1554. unregister_framebuffer(fbdev->fbs[i]);
  1555. /* free the reserved fbmem */
  1556. omapfb_free_all_fbmem(fbdev);
  1557. for (i = 0; i < fbdev->num_fbs; i++) {
  1558. fbinfo_cleanup(fbdev, fbdev->fbs[i]);
  1559. framebuffer_release(fbdev->fbs[i]);
  1560. }
  1561. for (i = 0; i < fbdev->num_displays; i++) {
  1562. if (fbdev->displays[i]->state != OMAP_DSS_DISPLAY_DISABLED)
  1563. fbdev->displays[i]->driver->disable(fbdev->displays[i]);
  1564. omap_dss_put_device(fbdev->displays[i]);
  1565. }
  1566. dev_set_drvdata(fbdev->dev, NULL);
  1567. kfree(fbdev);
  1568. }
  1569. static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
  1570. {
  1571. int r, i;
  1572. fbdev->num_fbs = 0;
  1573. DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
  1574. /* allocate fb_infos */
  1575. for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
  1576. struct fb_info *fbi;
  1577. struct omapfb_info *ofbi;
  1578. fbi = framebuffer_alloc(sizeof(struct omapfb_info),
  1579. fbdev->dev);
  1580. if (fbi == NULL) {
  1581. dev_err(fbdev->dev,
  1582. "unable to allocate memory for plane info\n");
  1583. return -ENOMEM;
  1584. }
  1585. clear_fb_info(fbi);
  1586. fbdev->fbs[i] = fbi;
  1587. ofbi = FB2OFB(fbi);
  1588. ofbi->fbdev = fbdev;
  1589. ofbi->id = i;
  1590. ofbi->region = &fbdev->regions[i];
  1591. ofbi->region->id = i;
  1592. mutex_init(&ofbi->region->mtx);
  1593. /* assign these early, so that fb alloc can use them */
  1594. ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
  1595. OMAP_DSS_ROT_DMA;
  1596. ofbi->mirror = def_mirror;
  1597. fbdev->num_fbs++;
  1598. }
  1599. DBG("fb_infos allocated\n");
  1600. /* assign overlays for the fbs */
  1601. for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
  1602. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  1603. ofbi->overlays[0] = fbdev->overlays[i];
  1604. ofbi->num_overlays = 1;
  1605. }
  1606. /* allocate fb memories */
  1607. r = omapfb_allocate_all_fbs(fbdev);
  1608. if (r) {
  1609. dev_err(fbdev->dev, "failed to allocate fbmem\n");
  1610. return r;
  1611. }
  1612. DBG("fbmems allocated\n");
  1613. /* setup fb_infos */
  1614. for (i = 0; i < fbdev->num_fbs; i++) {
  1615. struct fb_info *fbi = fbdev->fbs[i];
  1616. struct omapfb_info *ofbi = FB2OFB(fbi);
  1617. omapfb_get_mem_region(ofbi->region);
  1618. r = omapfb_fb_init(fbdev, fbi);
  1619. omapfb_put_mem_region(ofbi->region);
  1620. if (r) {
  1621. dev_err(fbdev->dev, "failed to setup fb_info\n");
  1622. return r;
  1623. }
  1624. }
  1625. DBG("fb_infos initialized\n");
  1626. for (i = 0; i < fbdev->num_fbs; i++) {
  1627. r = register_framebuffer(fbdev->fbs[i]);
  1628. if (r != 0) {
  1629. dev_err(fbdev->dev,
  1630. "registering framebuffer %d failed\n", i);
  1631. return r;
  1632. }
  1633. }
  1634. DBG("framebuffers registered\n");
  1635. for (i = 0; i < fbdev->num_fbs; i++) {
  1636. struct fb_info *fbi = fbdev->fbs[i];
  1637. struct omapfb_info *ofbi = FB2OFB(fbi);
  1638. omapfb_get_mem_region(ofbi->region);
  1639. r = omapfb_apply_changes(fbi, 1);
  1640. omapfb_put_mem_region(ofbi->region);
  1641. if (r) {
  1642. dev_err(fbdev->dev, "failed to change mode\n");
  1643. return r;
  1644. }
  1645. }
  1646. DBG("create sysfs for fbs\n");
  1647. r = omapfb_create_sysfs(fbdev);
  1648. if (r) {
  1649. dev_err(fbdev->dev, "failed to create sysfs entries\n");
  1650. return r;
  1651. }
  1652. /* Enable fb0 */
  1653. if (fbdev->num_fbs > 0) {
  1654. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
  1655. if (ofbi->num_overlays > 0) {
  1656. struct omap_overlay *ovl = ofbi->overlays[0];
  1657. r = omapfb_overlay_enable(ovl, 1);
  1658. if (r) {
  1659. dev_err(fbdev->dev,
  1660. "failed to enable overlay\n");
  1661. return r;
  1662. }
  1663. }
  1664. }
  1665. DBG("create_framebuffers done\n");
  1666. return 0;
  1667. }
  1668. static int omapfb_mode_to_timings(const char *mode_str,
  1669. struct omap_video_timings *timings, u8 *bpp)
  1670. {
  1671. struct fb_info fbi;
  1672. struct fb_var_screeninfo var;
  1673. struct fb_ops fbops;
  1674. int r;
  1675. #ifdef CONFIG_OMAP2_DSS_VENC
  1676. if (strcmp(mode_str, "pal") == 0) {
  1677. *timings = omap_dss_pal_timings;
  1678. *bpp = 0;
  1679. return 0;
  1680. } else if (strcmp(mode_str, "ntsc") == 0) {
  1681. *timings = omap_dss_ntsc_timings;
  1682. *bpp = 0;
  1683. return 0;
  1684. }
  1685. #endif
  1686. /* this is quite a hack, but I wanted to use the modedb and for
  1687. * that we need fb_info and var, so we create dummy ones */
  1688. memset(&fbi, 0, sizeof(fbi));
  1689. memset(&var, 0, sizeof(var));
  1690. memset(&fbops, 0, sizeof(fbops));
  1691. fbi.fbops = &fbops;
  1692. r = fb_find_mode(&var, &fbi, mode_str, NULL, 0, NULL, 24);
  1693. if (r != 0) {
  1694. timings->pixel_clock = PICOS2KHZ(var.pixclock);
  1695. timings->hfp = var.left_margin;
  1696. timings->hbp = var.right_margin;
  1697. timings->vfp = var.upper_margin;
  1698. timings->vbp = var.lower_margin;
  1699. timings->hsw = var.hsync_len;
  1700. timings->vsw = var.vsync_len;
  1701. timings->x_res = var.xres;
  1702. timings->y_res = var.yres;
  1703. switch (var.bits_per_pixel) {
  1704. case 16:
  1705. *bpp = 16;
  1706. break;
  1707. case 24:
  1708. case 32:
  1709. default:
  1710. *bpp = 24;
  1711. break;
  1712. }
  1713. return 0;
  1714. } else {
  1715. return -EINVAL;
  1716. }
  1717. }
  1718. static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
  1719. struct omap_dss_device *display, char *mode_str)
  1720. {
  1721. int r;
  1722. u8 bpp;
  1723. struct omap_video_timings timings;
  1724. r = omapfb_mode_to_timings(mode_str, &timings, &bpp);
  1725. if (r)
  1726. return r;
  1727. fbdev->bpp_overrides[fbdev->num_bpp_overrides].dssdev = display;
  1728. fbdev->bpp_overrides[fbdev->num_bpp_overrides].bpp = bpp;
  1729. ++fbdev->num_bpp_overrides;
  1730. if (!display->driver->check_timings || !display->driver->set_timings)
  1731. return -EINVAL;
  1732. r = display->driver->check_timings(display, &timings);
  1733. if (r)
  1734. return r;
  1735. display->driver->set_timings(display, &timings);
  1736. return 0;
  1737. }
  1738. static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
  1739. struct omap_dss_device *dssdev)
  1740. {
  1741. int i;
  1742. BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
  1743. for (i = 0; i < fbdev->num_bpp_overrides; ++i) {
  1744. if (dssdev == fbdev->bpp_overrides[i].dssdev)
  1745. return fbdev->bpp_overrides[i].bpp;
  1746. }
  1747. return dssdev->driver->get_recommended_bpp(dssdev);
  1748. }
  1749. static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
  1750. {
  1751. char *str, *options, *this_opt;
  1752. int r = 0;
  1753. str = kmalloc(strlen(def_mode) + 1, GFP_KERNEL);
  1754. strcpy(str, def_mode);
  1755. options = str;
  1756. while (!r && (this_opt = strsep(&options, ",")) != NULL) {
  1757. char *p, *display_str, *mode_str;
  1758. struct omap_dss_device *display;
  1759. int i;
  1760. p = strchr(this_opt, ':');
  1761. if (!p) {
  1762. r = -EINVAL;
  1763. break;
  1764. }
  1765. *p = 0;
  1766. display_str = this_opt;
  1767. mode_str = p + 1;
  1768. display = NULL;
  1769. for (i = 0; i < fbdev->num_displays; ++i) {
  1770. if (strcmp(fbdev->displays[i]->name,
  1771. display_str) == 0) {
  1772. display = fbdev->displays[i];
  1773. break;
  1774. }
  1775. }
  1776. if (!display) {
  1777. r = -EINVAL;
  1778. break;
  1779. }
  1780. r = omapfb_set_def_mode(fbdev, display, mode_str);
  1781. if (r)
  1782. break;
  1783. }
  1784. kfree(str);
  1785. return r;
  1786. }
  1787. static int omapfb_probe(struct platform_device *pdev)
  1788. {
  1789. struct omapfb2_device *fbdev = NULL;
  1790. int r = 0;
  1791. int i;
  1792. struct omap_overlay *ovl;
  1793. struct omap_dss_device *def_display;
  1794. struct omap_dss_device *dssdev;
  1795. DBG("omapfb_probe\n");
  1796. if (pdev->num_resources != 0) {
  1797. dev_err(&pdev->dev, "probed for an unknown device\n");
  1798. r = -ENODEV;
  1799. goto err0;
  1800. }
  1801. fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
  1802. if (fbdev == NULL) {
  1803. r = -ENOMEM;
  1804. goto err0;
  1805. }
  1806. mutex_init(&fbdev->mtx);
  1807. fbdev->dev = &pdev->dev;
  1808. platform_set_drvdata(pdev, fbdev);
  1809. r = 0;
  1810. fbdev->num_displays = 0;
  1811. dssdev = NULL;
  1812. for_each_dss_dev(dssdev) {
  1813. omap_dss_get_device(dssdev);
  1814. if (!dssdev->driver) {
  1815. dev_err(&pdev->dev, "no driver for display\n");
  1816. r = -ENODEV;
  1817. }
  1818. fbdev->displays[fbdev->num_displays++] = dssdev;
  1819. }
  1820. if (r)
  1821. goto cleanup;
  1822. if (fbdev->num_displays == 0) {
  1823. dev_err(&pdev->dev, "no displays\n");
  1824. r = -EINVAL;
  1825. goto cleanup;
  1826. }
  1827. fbdev->num_overlays = omap_dss_get_num_overlays();
  1828. for (i = 0; i < fbdev->num_overlays; i++)
  1829. fbdev->overlays[i] = omap_dss_get_overlay(i);
  1830. fbdev->num_managers = omap_dss_get_num_overlay_managers();
  1831. for (i = 0; i < fbdev->num_managers; i++)
  1832. fbdev->managers[i] = omap_dss_get_overlay_manager(i);
  1833. if (def_mode && strlen(def_mode) > 0) {
  1834. if (omapfb_parse_def_modes(fbdev))
  1835. dev_warn(&pdev->dev, "cannot parse default modes\n");
  1836. }
  1837. r = omapfb_create_framebuffers(fbdev);
  1838. if (r)
  1839. goto cleanup;
  1840. for (i = 0; i < fbdev->num_managers; i++) {
  1841. struct omap_overlay_manager *mgr;
  1842. mgr = fbdev->managers[i];
  1843. r = mgr->apply(mgr);
  1844. if (r)
  1845. dev_warn(fbdev->dev, "failed to apply dispc config\n");
  1846. }
  1847. DBG("mgr->apply'ed\n");
  1848. /* gfx overlay should be the default one. find a display
  1849. * connected to that, and use it as default display */
  1850. ovl = omap_dss_get_overlay(0);
  1851. if (ovl->manager && ovl->manager->device) {
  1852. def_display = ovl->manager->device;
  1853. } else {
  1854. dev_warn(&pdev->dev, "cannot find default display\n");
  1855. def_display = NULL;
  1856. }
  1857. if (def_display) {
  1858. struct omap_dss_driver *dssdrv = def_display->driver;
  1859. r = def_display->driver->enable(def_display);
  1860. if (r) {
  1861. dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
  1862. def_display->name);
  1863. goto cleanup;
  1864. }
  1865. if (def_display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
  1866. u16 w, h;
  1867. if (dssdrv->enable_te)
  1868. dssdrv->enable_te(def_display, 1);
  1869. if (dssdrv->set_update_mode)
  1870. dssdrv->set_update_mode(def_display,
  1871. OMAP_DSS_UPDATE_MANUAL);
  1872. dssdrv->get_resolution(def_display, &w, &h);
  1873. def_display->driver->update(def_display, 0, 0, w, h);
  1874. } else {
  1875. if (dssdrv->set_update_mode)
  1876. dssdrv->set_update_mode(def_display,
  1877. OMAP_DSS_UPDATE_AUTO);
  1878. }
  1879. }
  1880. return 0;
  1881. cleanup:
  1882. omapfb_free_resources(fbdev);
  1883. err0:
  1884. dev_err(&pdev->dev, "failed to setup omapfb\n");
  1885. return r;
  1886. }
  1887. static int omapfb_remove(struct platform_device *pdev)
  1888. {
  1889. struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
  1890. /* FIXME: wait till completion of pending events */
  1891. omapfb_remove_sysfs(fbdev);
  1892. omapfb_free_resources(fbdev);
  1893. return 0;
  1894. }
  1895. static struct platform_driver omapfb_driver = {
  1896. .probe = omapfb_probe,
  1897. .remove = omapfb_remove,
  1898. .driver = {
  1899. .name = "omapfb",
  1900. .owner = THIS_MODULE,
  1901. },
  1902. };
  1903. static int __init omapfb_init(void)
  1904. {
  1905. DBG("omapfb_init\n");
  1906. if (platform_driver_register(&omapfb_driver)) {
  1907. printk(KERN_ERR "failed to register omapfb driver\n");
  1908. return -ENODEV;
  1909. }
  1910. return 0;
  1911. }
  1912. static void __exit omapfb_exit(void)
  1913. {
  1914. DBG("omapfb_exit\n");
  1915. platform_driver_unregister(&omapfb_driver);
  1916. }
  1917. module_param_named(mode, def_mode, charp, 0);
  1918. module_param_named(vram, def_vram, charp, 0);
  1919. module_param_named(rotate, def_rotate, int, 0);
  1920. module_param_named(vrfb, def_vrfb, bool, 0);
  1921. module_param_named(mirror, def_mirror, bool, 0);
  1922. /* late_initcall to let panel/ctrl drivers loaded first.
  1923. * I guess better option would be a more dynamic approach,
  1924. * so that omapfb reacts to new panels when they are loaded */
  1925. late_initcall(omapfb_init);
  1926. /*module_init(omapfb_init);*/
  1927. module_exit(omapfb_exit);
  1928. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
  1929. MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
  1930. MODULE_LICENSE("GPL v2");