omapfb-main.c 51 KB

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