omapfb-main.c 57 KB

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