omapfb-main.c 51 KB

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