omapfb-main.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  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. r = 0;
  1738. fbdev->num_displays = 0;
  1739. dssdev = NULL;
  1740. for_each_dss_dev(dssdev) {
  1741. omap_dss_get_device(dssdev);
  1742. if (!dssdev->driver) {
  1743. dev_err(&pdev->dev, "no driver for display\n");
  1744. r = -ENODEV;
  1745. }
  1746. fbdev->displays[fbdev->num_displays++] = dssdev;
  1747. }
  1748. if (r)
  1749. goto cleanup;
  1750. if (fbdev->num_displays == 0) {
  1751. dev_err(&pdev->dev, "no displays\n");
  1752. r = -EINVAL;
  1753. goto cleanup;
  1754. }
  1755. fbdev->num_overlays = omap_dss_get_num_overlays();
  1756. for (i = 0; i < fbdev->num_overlays; i++)
  1757. fbdev->overlays[i] = omap_dss_get_overlay(i);
  1758. fbdev->num_managers = omap_dss_get_num_overlay_managers();
  1759. for (i = 0; i < fbdev->num_managers; i++)
  1760. fbdev->managers[i] = omap_dss_get_overlay_manager(i);
  1761. if (def_mode && strlen(def_mode) > 0) {
  1762. if (omapfb_parse_def_modes(fbdev))
  1763. dev_warn(&pdev->dev, "cannot parse default modes\n");
  1764. }
  1765. r = omapfb_create_framebuffers(fbdev);
  1766. if (r)
  1767. goto cleanup;
  1768. for (i = 0; i < fbdev->num_managers; i++) {
  1769. struct omap_overlay_manager *mgr;
  1770. mgr = fbdev->managers[i];
  1771. r = mgr->apply(mgr);
  1772. if (r)
  1773. dev_warn(fbdev->dev, "failed to apply dispc config\n");
  1774. }
  1775. DBG("mgr->apply'ed\n");
  1776. /* gfx overlay should be the default one. find a display
  1777. * connected to that, and use it as default display */
  1778. ovl = omap_dss_get_overlay(0);
  1779. if (ovl->manager && ovl->manager->device) {
  1780. def_display = ovl->manager->device;
  1781. } else {
  1782. dev_warn(&pdev->dev, "cannot find default display\n");
  1783. def_display = NULL;
  1784. }
  1785. if (def_display) {
  1786. #ifndef CONFIG_FB_OMAP2_FORCE_AUTO_UPDATE
  1787. u16 w, h;
  1788. #endif
  1789. r = def_display->enable(def_display);
  1790. if (r) {
  1791. dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
  1792. def_display->name);
  1793. goto cleanup;
  1794. }
  1795. /* set the update mode */
  1796. if (def_display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
  1797. #ifdef CONFIG_FB_OMAP2_FORCE_AUTO_UPDATE
  1798. if (def_display->enable_te)
  1799. def_display->enable_te(def_display, 1);
  1800. if (def_display->set_update_mode)
  1801. def_display->set_update_mode(def_display,
  1802. OMAP_DSS_UPDATE_AUTO);
  1803. #else /* MANUAL_UPDATE */
  1804. if (def_display->enable_te)
  1805. def_display->enable_te(def_display, 0);
  1806. if (def_display->set_update_mode)
  1807. def_display->set_update_mode(def_display,
  1808. OMAP_DSS_UPDATE_MANUAL);
  1809. def_display->get_resolution(def_display, &w, &h);
  1810. def_display->update(def_display, 0, 0, w, h);
  1811. #endif
  1812. } else {
  1813. if (def_display->set_update_mode)
  1814. def_display->set_update_mode(def_display,
  1815. OMAP_DSS_UPDATE_AUTO);
  1816. }
  1817. }
  1818. return 0;
  1819. cleanup:
  1820. omapfb_free_resources(fbdev);
  1821. err0:
  1822. dev_err(&pdev->dev, "failed to setup omapfb\n");
  1823. return r;
  1824. }
  1825. static int omapfb_remove(struct platform_device *pdev)
  1826. {
  1827. struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
  1828. /* FIXME: wait till completion of pending events */
  1829. omapfb_remove_sysfs(fbdev);
  1830. omapfb_free_resources(fbdev);
  1831. return 0;
  1832. }
  1833. static struct platform_driver omapfb_driver = {
  1834. .probe = omapfb_probe,
  1835. .remove = omapfb_remove,
  1836. .driver = {
  1837. .name = "omapfb",
  1838. .owner = THIS_MODULE,
  1839. },
  1840. };
  1841. static int __init omapfb_init(void)
  1842. {
  1843. DBG("omapfb_init\n");
  1844. if (platform_driver_register(&omapfb_driver)) {
  1845. printk(KERN_ERR "failed to register omapfb driver\n");
  1846. return -ENODEV;
  1847. }
  1848. return 0;
  1849. }
  1850. static void __exit omapfb_exit(void)
  1851. {
  1852. DBG("omapfb_exit\n");
  1853. platform_driver_unregister(&omapfb_driver);
  1854. }
  1855. module_param_named(mode, def_mode, charp, 0);
  1856. module_param_named(vram, def_vram, charp, 0);
  1857. module_param_named(rotate, def_rotate, int, 0);
  1858. module_param_named(vrfb, def_vrfb, bool, 0);
  1859. module_param_named(mirror, def_mirror, bool, 0);
  1860. /* late_initcall to let panel/ctrl drivers loaded first.
  1861. * I guess better option would be a more dynamic approach,
  1862. * so that omapfb reacts to new panels when they are loaded */
  1863. late_initcall(omapfb_init);
  1864. /*module_init(omapfb_init);*/
  1865. module_exit(omapfb_exit);
  1866. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
  1867. MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
  1868. MODULE_LICENSE("GPL v2");