omapfb-main.c 53 KB

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