omapfb-main.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  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. if (ofbi->region.size == 0)
  577. return 0;
  578. r = fb_mode_to_dss_mode(var, &mode);
  579. if (r) {
  580. DBG("cannot convert var to omap dss mode\n");
  581. return r;
  582. }
  583. for (i = 0; i < ofbi->num_overlays; ++i) {
  584. if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
  585. DBG("invalid mode\n");
  586. return -EINVAL;
  587. }
  588. }
  589. if (var->rotate < 0 || var->rotate > 3)
  590. return -EINVAL;
  591. if (check_fb_res_bounds(var))
  592. return -EINVAL;
  593. if (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. /* setup overlay according to the fb */
  702. static int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
  703. u16 posx, u16 posy, u16 outw, u16 outh)
  704. {
  705. int r = 0;
  706. struct omapfb_info *ofbi = FB2OFB(fbi);
  707. struct fb_var_screeninfo *var = &fbi->var;
  708. struct fb_fix_screeninfo *fix = &fbi->fix;
  709. enum omap_color_mode mode = 0;
  710. int offset;
  711. u32 data_start_p;
  712. void __iomem *data_start_v;
  713. struct omap_overlay_info info;
  714. int xres, yres;
  715. int screen_width;
  716. int mirror;
  717. int rotation = var->rotate;
  718. int i;
  719. for (i = 0; i < ofbi->num_overlays; i++) {
  720. if (ovl != ofbi->overlays[i])
  721. continue;
  722. rotation = (rotation + ofbi->rotation[i]) % 4;
  723. break;
  724. }
  725. DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
  726. posx, posy, outw, outh);
  727. if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
  728. xres = var->yres;
  729. yres = var->xres;
  730. } else {
  731. xres = var->xres;
  732. yres = var->yres;
  733. }
  734. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  735. data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
  736. data_start_v = NULL;
  737. } else {
  738. data_start_p = omapfb_get_region_paddr(ofbi);
  739. data_start_v = omapfb_get_region_vaddr(ofbi);
  740. }
  741. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  742. offset = calc_rotation_offset_vrfb(var, fix, rotation);
  743. else
  744. offset = calc_rotation_offset_dma(var, fix, rotation);
  745. data_start_p += offset;
  746. data_start_v += offset;
  747. if (offset)
  748. DBG("offset %d, %d = %d\n",
  749. var->xoffset, var->yoffset, offset);
  750. DBG("paddr %x, vaddr %p\n", data_start_p, data_start_v);
  751. r = fb_mode_to_dss_mode(var, &mode);
  752. if (r) {
  753. DBG("fb_mode_to_dss_mode failed");
  754. goto err;
  755. }
  756. switch (var->nonstd) {
  757. case OMAPFB_COLOR_YUV422:
  758. case OMAPFB_COLOR_YUY422:
  759. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  760. screen_width = fix->line_length
  761. / (var->bits_per_pixel >> 2);
  762. break;
  763. }
  764. default:
  765. screen_width = fix->line_length / (var->bits_per_pixel >> 3);
  766. break;
  767. }
  768. ovl->get_overlay_info(ovl, &info);
  769. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
  770. mirror = 0;
  771. else
  772. mirror = ofbi->mirror;
  773. info.paddr = data_start_p;
  774. info.vaddr = data_start_v;
  775. info.screen_width = screen_width;
  776. info.width = xres;
  777. info.height = yres;
  778. info.color_mode = mode;
  779. info.rotation_type = ofbi->rotation_type;
  780. info.rotation = rotation;
  781. info.mirror = mirror;
  782. info.pos_x = posx;
  783. info.pos_y = posy;
  784. info.out_width = outw;
  785. info.out_height = outh;
  786. r = ovl->set_overlay_info(ovl, &info);
  787. if (r) {
  788. DBG("ovl->setup_overlay_info failed\n");
  789. goto err;
  790. }
  791. return 0;
  792. err:
  793. DBG("setup_overlay failed\n");
  794. return r;
  795. }
  796. /* apply var to the overlay */
  797. int omapfb_apply_changes(struct fb_info *fbi, int init)
  798. {
  799. int r = 0;
  800. struct omapfb_info *ofbi = FB2OFB(fbi);
  801. struct fb_var_screeninfo *var = &fbi->var;
  802. struct omap_overlay *ovl;
  803. u16 posx, posy;
  804. u16 outw, outh;
  805. int i;
  806. #ifdef DEBUG
  807. if (omapfb_test_pattern)
  808. fill_fb(fbi);
  809. #endif
  810. for (i = 0; i < ofbi->num_overlays; i++) {
  811. ovl = ofbi->overlays[i];
  812. DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
  813. if (ofbi->region.size == 0) {
  814. /* the fb is not available. disable the overlay */
  815. omapfb_overlay_enable(ovl, 0);
  816. if (!init && ovl->manager)
  817. ovl->manager->apply(ovl->manager);
  818. continue;
  819. }
  820. if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
  821. int rotation = (var->rotate + ofbi->rotation[i]) % 4;
  822. if (rotation == FB_ROTATE_CW ||
  823. rotation == FB_ROTATE_CCW) {
  824. outw = var->yres;
  825. outh = var->xres;
  826. } else {
  827. outw = var->xres;
  828. outh = var->yres;
  829. }
  830. } else {
  831. outw = ovl->info.out_width;
  832. outh = ovl->info.out_height;
  833. }
  834. if (init) {
  835. posx = 0;
  836. posy = 0;
  837. } else {
  838. posx = ovl->info.pos_x;
  839. posy = ovl->info.pos_y;
  840. }
  841. r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
  842. if (r)
  843. goto err;
  844. if (!init && ovl->manager)
  845. ovl->manager->apply(ovl->manager);
  846. }
  847. return 0;
  848. err:
  849. DBG("apply_changes failed\n");
  850. return r;
  851. }
  852. /* checks var and eventually tweaks it to something supported,
  853. * DO NOT MODIFY PAR */
  854. static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
  855. {
  856. int r;
  857. DBG("check_var(%d)\n", FB2OFB(fbi)->id);
  858. r = check_fb_var(fbi, var);
  859. return r;
  860. }
  861. /* set the video mode according to info->var */
  862. static int omapfb_set_par(struct fb_info *fbi)
  863. {
  864. int r;
  865. DBG("set_par(%d)\n", FB2OFB(fbi)->id);
  866. set_fb_fix(fbi);
  867. r = setup_vrfb_rotation(fbi);
  868. if (r)
  869. return r;
  870. r = omapfb_apply_changes(fbi, 0);
  871. return r;
  872. }
  873. static int omapfb_pan_display(struct fb_var_screeninfo *var,
  874. struct fb_info *fbi)
  875. {
  876. struct fb_var_screeninfo new_var;
  877. int r;
  878. DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
  879. if (var->xoffset == fbi->var.xoffset &&
  880. var->yoffset == fbi->var.yoffset)
  881. return 0;
  882. new_var = fbi->var;
  883. new_var.xoffset = var->xoffset;
  884. new_var.yoffset = var->yoffset;
  885. fbi->var = new_var;
  886. r = omapfb_apply_changes(fbi, 0);
  887. return r;
  888. }
  889. static void mmap_user_open(struct vm_area_struct *vma)
  890. {
  891. struct omapfb_info *ofbi = (struct omapfb_info *)vma->vm_private_data;
  892. atomic_inc(&ofbi->map_count);
  893. }
  894. static void mmap_user_close(struct vm_area_struct *vma)
  895. {
  896. struct omapfb_info *ofbi = (struct omapfb_info *)vma->vm_private_data;
  897. atomic_dec(&ofbi->map_count);
  898. }
  899. static struct vm_operations_struct mmap_user_ops = {
  900. .open = mmap_user_open,
  901. .close = mmap_user_close,
  902. };
  903. static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
  904. {
  905. struct omapfb_info *ofbi = FB2OFB(fbi);
  906. struct fb_fix_screeninfo *fix = &fbi->fix;
  907. unsigned long off;
  908. unsigned long start;
  909. u32 len;
  910. if (vma->vm_end - vma->vm_start == 0)
  911. return 0;
  912. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  913. return -EINVAL;
  914. off = vma->vm_pgoff << PAGE_SHIFT;
  915. start = omapfb_get_region_paddr(ofbi);
  916. len = fix->smem_len;
  917. if (off >= len)
  918. return -EINVAL;
  919. if ((vma->vm_end - vma->vm_start + off) > len)
  920. return -EINVAL;
  921. off += start;
  922. DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
  923. vma->vm_pgoff = off >> PAGE_SHIFT;
  924. vma->vm_flags |= VM_IO | VM_RESERVED;
  925. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  926. vma->vm_ops = &mmap_user_ops;
  927. vma->vm_private_data = ofbi;
  928. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  929. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  930. return -EAGAIN;
  931. /* vm_ops.open won't be called for mmap itself. */
  932. atomic_inc(&ofbi->map_count);
  933. return 0;
  934. }
  935. /* Store a single color palette entry into a pseudo palette or the hardware
  936. * palette if one is available. For now we support only 16bpp and thus store
  937. * the entry only to the pseudo palette.
  938. */
  939. static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
  940. u_int blue, u_int transp, int update_hw_pal)
  941. {
  942. /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
  943. /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
  944. struct fb_var_screeninfo *var = &fbi->var;
  945. int r = 0;
  946. enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
  947. /*switch (plane->color_mode) {*/
  948. switch (mode) {
  949. case OMAPFB_COLOR_YUV422:
  950. case OMAPFB_COLOR_YUV420:
  951. case OMAPFB_COLOR_YUY422:
  952. r = -EINVAL;
  953. break;
  954. case OMAPFB_COLOR_CLUT_8BPP:
  955. case OMAPFB_COLOR_CLUT_4BPP:
  956. case OMAPFB_COLOR_CLUT_2BPP:
  957. case OMAPFB_COLOR_CLUT_1BPP:
  958. /*
  959. if (fbdev->ctrl->setcolreg)
  960. r = fbdev->ctrl->setcolreg(regno, red, green, blue,
  961. transp, update_hw_pal);
  962. */
  963. /* Fallthrough */
  964. r = -EINVAL;
  965. break;
  966. case OMAPFB_COLOR_RGB565:
  967. case OMAPFB_COLOR_RGB444:
  968. case OMAPFB_COLOR_RGB24P:
  969. case OMAPFB_COLOR_RGB24U:
  970. if (r != 0)
  971. break;
  972. if (regno < 0) {
  973. r = -EINVAL;
  974. break;
  975. }
  976. if (regno < 16) {
  977. u16 pal;
  978. pal = ((red >> (16 - var->red.length)) <<
  979. var->red.offset) |
  980. ((green >> (16 - var->green.length)) <<
  981. var->green.offset) |
  982. (blue >> (16 - var->blue.length));
  983. ((u32 *)(fbi->pseudo_palette))[regno] = pal;
  984. }
  985. break;
  986. default:
  987. BUG();
  988. }
  989. return r;
  990. }
  991. static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  992. u_int transp, struct fb_info *info)
  993. {
  994. DBG("setcolreg\n");
  995. return _setcolreg(info, regno, red, green, blue, transp, 1);
  996. }
  997. static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  998. {
  999. int count, index, r;
  1000. u16 *red, *green, *blue, *transp;
  1001. u16 trans = 0xffff;
  1002. DBG("setcmap\n");
  1003. red = cmap->red;
  1004. green = cmap->green;
  1005. blue = cmap->blue;
  1006. transp = cmap->transp;
  1007. index = cmap->start;
  1008. for (count = 0; count < cmap->len; count++) {
  1009. if (transp)
  1010. trans = *transp++;
  1011. r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
  1012. count == cmap->len - 1);
  1013. if (r != 0)
  1014. return r;
  1015. }
  1016. return 0;
  1017. }
  1018. static int omapfb_blank(int blank, struct fb_info *fbi)
  1019. {
  1020. struct omapfb_info *ofbi = FB2OFB(fbi);
  1021. struct omapfb2_device *fbdev = ofbi->fbdev;
  1022. struct omap_dss_device *display = fb2display(fbi);
  1023. int do_update = 0;
  1024. int r = 0;
  1025. omapfb_lock(fbdev);
  1026. switch (blank) {
  1027. case FB_BLANK_UNBLANK:
  1028. if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
  1029. goto exit;
  1030. if (display->driver->resume)
  1031. r = display->driver->resume(display);
  1032. if (r == 0 && display->driver->get_update_mode &&
  1033. display->driver->get_update_mode(display) ==
  1034. OMAP_DSS_UPDATE_MANUAL)
  1035. do_update = 1;
  1036. break;
  1037. case FB_BLANK_NORMAL:
  1038. /* FB_BLANK_NORMAL could be implemented.
  1039. * Needs DSS additions. */
  1040. case FB_BLANK_VSYNC_SUSPEND:
  1041. case FB_BLANK_HSYNC_SUSPEND:
  1042. case FB_BLANK_POWERDOWN:
  1043. if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
  1044. goto exit;
  1045. if (display->driver->suspend)
  1046. r = display->driver->suspend(display);
  1047. break;
  1048. default:
  1049. r = -EINVAL;
  1050. }
  1051. exit:
  1052. omapfb_unlock(fbdev);
  1053. if (r == 0 && do_update && display->driver->update) {
  1054. u16 w, h;
  1055. display->driver->get_resolution(display, &w, &h);
  1056. r = display->driver->update(display, 0, 0, w, h);
  1057. }
  1058. return r;
  1059. }
  1060. #if 0
  1061. /* XXX fb_read and fb_write are needed for VRFB */
  1062. ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
  1063. size_t count, loff_t *ppos)
  1064. {
  1065. DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
  1066. /* XXX needed for VRFB */
  1067. return count;
  1068. }
  1069. #endif
  1070. static struct fb_ops omapfb_ops = {
  1071. .owner = THIS_MODULE,
  1072. .fb_open = omapfb_open,
  1073. .fb_release = omapfb_release,
  1074. .fb_fillrect = cfb_fillrect,
  1075. .fb_copyarea = cfb_copyarea,
  1076. .fb_imageblit = cfb_imageblit,
  1077. .fb_blank = omapfb_blank,
  1078. .fb_ioctl = omapfb_ioctl,
  1079. .fb_check_var = omapfb_check_var,
  1080. .fb_set_par = omapfb_set_par,
  1081. .fb_pan_display = omapfb_pan_display,
  1082. .fb_mmap = omapfb_mmap,
  1083. .fb_setcolreg = omapfb_setcolreg,
  1084. .fb_setcmap = omapfb_setcmap,
  1085. /*.fb_write = omapfb_write,*/
  1086. };
  1087. static void omapfb_free_fbmem(struct fb_info *fbi)
  1088. {
  1089. struct omapfb_info *ofbi = FB2OFB(fbi);
  1090. struct omapfb2_device *fbdev = ofbi->fbdev;
  1091. struct omapfb2_mem_region *rg;
  1092. rg = &ofbi->region;
  1093. if (rg->paddr)
  1094. if (omap_vram_free(rg->paddr, rg->size))
  1095. dev_err(fbdev->dev, "VRAM FREE failed\n");
  1096. if (rg->vaddr)
  1097. iounmap(rg->vaddr);
  1098. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  1099. /* unmap the 0 angle rotation */
  1100. if (rg->vrfb.vaddr[0]) {
  1101. iounmap(rg->vrfb.vaddr[0]);
  1102. omap_vrfb_release_ctx(&rg->vrfb);
  1103. rg->vrfb.vaddr[0] = NULL;
  1104. }
  1105. }
  1106. rg->vaddr = NULL;
  1107. rg->paddr = 0;
  1108. rg->alloc = 0;
  1109. rg->size = 0;
  1110. }
  1111. static void clear_fb_info(struct fb_info *fbi)
  1112. {
  1113. memset(&fbi->var, 0, sizeof(fbi->var));
  1114. memset(&fbi->fix, 0, sizeof(fbi->fix));
  1115. strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
  1116. }
  1117. static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
  1118. {
  1119. int i;
  1120. DBG("free all fbmem\n");
  1121. for (i = 0; i < fbdev->num_fbs; i++) {
  1122. struct fb_info *fbi = fbdev->fbs[i];
  1123. omapfb_free_fbmem(fbi);
  1124. clear_fb_info(fbi);
  1125. }
  1126. return 0;
  1127. }
  1128. static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
  1129. unsigned long paddr)
  1130. {
  1131. struct omapfb_info *ofbi = FB2OFB(fbi);
  1132. struct omapfb2_device *fbdev = ofbi->fbdev;
  1133. struct omapfb2_mem_region *rg;
  1134. void __iomem *vaddr;
  1135. int r;
  1136. rg = &ofbi->region;
  1137. memset(rg, 0, sizeof(*rg));
  1138. size = PAGE_ALIGN(size);
  1139. if (!paddr) {
  1140. DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
  1141. r = omap_vram_alloc(OMAP_VRAM_MEMTYPE_SDRAM, size, &paddr);
  1142. } else {
  1143. DBG("reserving %lu bytes at %lx for fb %d\n", size, paddr,
  1144. ofbi->id);
  1145. r = omap_vram_reserve(paddr, size);
  1146. }
  1147. if (r) {
  1148. dev_err(fbdev->dev, "failed to allocate framebuffer\n");
  1149. return -ENOMEM;
  1150. }
  1151. if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
  1152. vaddr = ioremap_wc(paddr, size);
  1153. if (!vaddr) {
  1154. dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
  1155. omap_vram_free(paddr, size);
  1156. return -ENOMEM;
  1157. }
  1158. DBG("allocated VRAM paddr %lx, vaddr %p\n", paddr, vaddr);
  1159. } else {
  1160. r = omap_vrfb_request_ctx(&rg->vrfb);
  1161. if (r) {
  1162. dev_err(fbdev->dev, "vrfb create ctx failed\n");
  1163. return r;
  1164. }
  1165. vaddr = NULL;
  1166. }
  1167. rg->paddr = paddr;
  1168. rg->vaddr = vaddr;
  1169. rg->size = size;
  1170. rg->alloc = 1;
  1171. return 0;
  1172. }
  1173. /* allocate fbmem using display resolution as reference */
  1174. static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
  1175. unsigned long paddr)
  1176. {
  1177. struct omapfb_info *ofbi = FB2OFB(fbi);
  1178. struct omapfb2_device *fbdev = ofbi->fbdev;
  1179. struct omap_dss_device *display;
  1180. int bytespp;
  1181. display = fb2display(fbi);
  1182. if (!display)
  1183. return 0;
  1184. switch (omapfb_get_recommended_bpp(fbdev, display)) {
  1185. case 16:
  1186. bytespp = 2;
  1187. break;
  1188. case 24:
  1189. bytespp = 4;
  1190. break;
  1191. default:
  1192. bytespp = 4;
  1193. break;
  1194. }
  1195. if (!size) {
  1196. u16 w, h;
  1197. display->driver->get_resolution(display, &w, &h);
  1198. if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
  1199. size = max(omap_vrfb_min_phys_size(w, h, bytespp),
  1200. omap_vrfb_min_phys_size(h, w, bytespp));
  1201. DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
  1202. w * h * bytespp, size);
  1203. } else {
  1204. size = w * h * bytespp;
  1205. }
  1206. }
  1207. if (!size)
  1208. return 0;
  1209. return omapfb_alloc_fbmem(fbi, size, paddr);
  1210. }
  1211. static enum omap_color_mode fb_format_to_dss_mode(enum omapfb_color_format fmt)
  1212. {
  1213. enum omap_color_mode mode;
  1214. switch (fmt) {
  1215. case OMAPFB_COLOR_RGB565:
  1216. mode = OMAP_DSS_COLOR_RGB16;
  1217. break;
  1218. case OMAPFB_COLOR_YUV422:
  1219. mode = OMAP_DSS_COLOR_YUV2;
  1220. break;
  1221. case OMAPFB_COLOR_CLUT_8BPP:
  1222. mode = OMAP_DSS_COLOR_CLUT8;
  1223. break;
  1224. case OMAPFB_COLOR_CLUT_4BPP:
  1225. mode = OMAP_DSS_COLOR_CLUT4;
  1226. break;
  1227. case OMAPFB_COLOR_CLUT_2BPP:
  1228. mode = OMAP_DSS_COLOR_CLUT2;
  1229. break;
  1230. case OMAPFB_COLOR_CLUT_1BPP:
  1231. mode = OMAP_DSS_COLOR_CLUT1;
  1232. break;
  1233. case OMAPFB_COLOR_RGB444:
  1234. mode = OMAP_DSS_COLOR_RGB12U;
  1235. break;
  1236. case OMAPFB_COLOR_YUY422:
  1237. mode = OMAP_DSS_COLOR_UYVY;
  1238. break;
  1239. case OMAPFB_COLOR_ARGB16:
  1240. mode = OMAP_DSS_COLOR_ARGB16;
  1241. break;
  1242. case OMAPFB_COLOR_RGB24U:
  1243. mode = OMAP_DSS_COLOR_RGB24U;
  1244. break;
  1245. case OMAPFB_COLOR_RGB24P:
  1246. mode = OMAP_DSS_COLOR_RGB24P;
  1247. break;
  1248. case OMAPFB_COLOR_ARGB32:
  1249. mode = OMAP_DSS_COLOR_ARGB32;
  1250. break;
  1251. case OMAPFB_COLOR_RGBA32:
  1252. mode = OMAP_DSS_COLOR_RGBA32;
  1253. break;
  1254. case OMAPFB_COLOR_RGBX32:
  1255. mode = OMAP_DSS_COLOR_RGBX32;
  1256. break;
  1257. default:
  1258. mode = -EINVAL;
  1259. }
  1260. return mode;
  1261. }
  1262. static int omapfb_parse_vram_param(const char *param, int max_entries,
  1263. unsigned long *sizes, unsigned long *paddrs)
  1264. {
  1265. int fbnum;
  1266. unsigned long size;
  1267. unsigned long paddr = 0;
  1268. char *p, *start;
  1269. start = (char *)param;
  1270. while (1) {
  1271. p = start;
  1272. fbnum = simple_strtoul(p, &p, 10);
  1273. if (p == param)
  1274. return -EINVAL;
  1275. if (*p != ':')
  1276. return -EINVAL;
  1277. if (fbnum >= max_entries)
  1278. return -EINVAL;
  1279. size = memparse(p + 1, &p);
  1280. if (!size)
  1281. return -EINVAL;
  1282. paddr = 0;
  1283. if (*p == '@') {
  1284. paddr = simple_strtoul(p + 1, &p, 16);
  1285. if (!paddr)
  1286. return -EINVAL;
  1287. }
  1288. paddrs[fbnum] = paddr;
  1289. sizes[fbnum] = size;
  1290. if (*p == 0)
  1291. break;
  1292. if (*p != ',')
  1293. return -EINVAL;
  1294. ++p;
  1295. start = p;
  1296. }
  1297. return 0;
  1298. }
  1299. static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
  1300. {
  1301. int i, r;
  1302. unsigned long vram_sizes[10];
  1303. unsigned long vram_paddrs[10];
  1304. memset(&vram_sizes, 0, sizeof(vram_sizes));
  1305. memset(&vram_paddrs, 0, sizeof(vram_paddrs));
  1306. if (def_vram && omapfb_parse_vram_param(def_vram, 10,
  1307. vram_sizes, vram_paddrs)) {
  1308. dev_err(fbdev->dev, "failed to parse vram parameter\n");
  1309. memset(&vram_sizes, 0, sizeof(vram_sizes));
  1310. memset(&vram_paddrs, 0, sizeof(vram_paddrs));
  1311. }
  1312. if (fbdev->dev->platform_data) {
  1313. struct omapfb_platform_data *opd;
  1314. opd = fbdev->dev->platform_data;
  1315. for (i = 0; i < opd->mem_desc.region_cnt; ++i) {
  1316. if (!vram_sizes[i]) {
  1317. unsigned long size;
  1318. unsigned long paddr;
  1319. size = opd->mem_desc.region[i].size;
  1320. paddr = opd->mem_desc.region[i].paddr;
  1321. vram_sizes[i] = size;
  1322. vram_paddrs[i] = paddr;
  1323. }
  1324. }
  1325. }
  1326. for (i = 0; i < fbdev->num_fbs; i++) {
  1327. /* allocate memory automatically only for fb0, or if
  1328. * excplicitly defined with vram or plat data option */
  1329. if (i == 0 || vram_sizes[i] != 0) {
  1330. r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
  1331. vram_sizes[i], vram_paddrs[i]);
  1332. if (r)
  1333. return r;
  1334. }
  1335. }
  1336. for (i = 0; i < fbdev->num_fbs; i++) {
  1337. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  1338. struct omapfb2_mem_region *rg;
  1339. rg = &ofbi->region;
  1340. DBG("region%d phys %08x virt %p size=%lu\n",
  1341. i,
  1342. rg->paddr,
  1343. rg->vaddr,
  1344. rg->size);
  1345. }
  1346. return 0;
  1347. }
  1348. int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
  1349. {
  1350. struct omapfb_info *ofbi = FB2OFB(fbi);
  1351. struct omapfb2_device *fbdev = ofbi->fbdev;
  1352. struct omap_dss_device *display = fb2display(fbi);
  1353. struct omapfb2_mem_region *rg = &ofbi->region;
  1354. unsigned long old_size = rg->size;
  1355. unsigned long old_paddr = rg->paddr;
  1356. int old_type = rg->type;
  1357. int r;
  1358. if (type > OMAPFB_MEMTYPE_MAX)
  1359. return -EINVAL;
  1360. size = PAGE_ALIGN(size);
  1361. if (old_size == size && old_type == type)
  1362. return 0;
  1363. if (display && display->driver->sync)
  1364. display->driver->sync(display);
  1365. omapfb_free_fbmem(fbi);
  1366. if (size == 0) {
  1367. clear_fb_info(fbi);
  1368. return 0;
  1369. }
  1370. r = omapfb_alloc_fbmem(fbi, size, 0);
  1371. if (r) {
  1372. if (old_size)
  1373. omapfb_alloc_fbmem(fbi, old_size, old_paddr);
  1374. if (rg->size == 0)
  1375. clear_fb_info(fbi);
  1376. return r;
  1377. }
  1378. if (old_size == size)
  1379. return 0;
  1380. if (old_size == 0) {
  1381. DBG("initializing fb %d\n", ofbi->id);
  1382. r = omapfb_fb_init(fbdev, fbi);
  1383. if (r) {
  1384. DBG("omapfb_fb_init failed\n");
  1385. goto err;
  1386. }
  1387. r = omapfb_apply_changes(fbi, 1);
  1388. if (r) {
  1389. DBG("omapfb_apply_changes failed\n");
  1390. goto err;
  1391. }
  1392. } else {
  1393. struct fb_var_screeninfo new_var;
  1394. memcpy(&new_var, &fbi->var, sizeof(new_var));
  1395. r = check_fb_var(fbi, &new_var);
  1396. if (r)
  1397. goto err;
  1398. memcpy(&fbi->var, &new_var, sizeof(fbi->var));
  1399. set_fb_fix(fbi);
  1400. r = setup_vrfb_rotation(fbi);
  1401. if (r)
  1402. goto err;
  1403. }
  1404. return 0;
  1405. err:
  1406. omapfb_free_fbmem(fbi);
  1407. clear_fb_info(fbi);
  1408. return r;
  1409. }
  1410. /* initialize fb_info, var, fix to something sane based on the display */
  1411. static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
  1412. {
  1413. struct fb_var_screeninfo *var = &fbi->var;
  1414. struct omap_dss_device *display = fb2display(fbi);
  1415. struct omapfb_info *ofbi = FB2OFB(fbi);
  1416. int r = 0;
  1417. fbi->fbops = &omapfb_ops;
  1418. fbi->flags = FBINFO_FLAG_DEFAULT;
  1419. fbi->pseudo_palette = fbdev->pseudo_palette;
  1420. if (ofbi->region.size == 0) {
  1421. clear_fb_info(fbi);
  1422. return 0;
  1423. }
  1424. var->nonstd = 0;
  1425. var->bits_per_pixel = 0;
  1426. var->rotate = def_rotate;
  1427. /*
  1428. * Check if there is a default color format set in the board file,
  1429. * and use this format instead the default deducted from the
  1430. * display bpp.
  1431. */
  1432. if (fbdev->dev->platform_data) {
  1433. struct omapfb_platform_data *opd;
  1434. int id = ofbi->id;
  1435. opd = fbdev->dev->platform_data;
  1436. if (opd->mem_desc.region[id].format_used) {
  1437. enum omap_color_mode mode;
  1438. enum omapfb_color_format format;
  1439. format = opd->mem_desc.region[id].format;
  1440. mode = fb_format_to_dss_mode(format);
  1441. if (mode < 0) {
  1442. r = mode;
  1443. goto err;
  1444. }
  1445. r = dss_mode_to_fb_mode(mode, var);
  1446. if (r < 0)
  1447. goto err;
  1448. }
  1449. }
  1450. if (display) {
  1451. u16 w, h;
  1452. int rotation = (var->rotate + ofbi->rotation[0]) % 4;
  1453. display->driver->get_resolution(display, &w, &h);
  1454. if (rotation == FB_ROTATE_CW ||
  1455. rotation == FB_ROTATE_CCW) {
  1456. var->xres = h;
  1457. var->yres = w;
  1458. } else {
  1459. var->xres = w;
  1460. var->yres = h;
  1461. }
  1462. var->xres_virtual = var->xres;
  1463. var->yres_virtual = var->yres;
  1464. if (!var->bits_per_pixel) {
  1465. switch (omapfb_get_recommended_bpp(fbdev, display)) {
  1466. case 16:
  1467. var->bits_per_pixel = 16;
  1468. break;
  1469. case 24:
  1470. var->bits_per_pixel = 32;
  1471. break;
  1472. default:
  1473. dev_err(fbdev->dev, "illegal display "
  1474. "bpp\n");
  1475. return -EINVAL;
  1476. }
  1477. }
  1478. } else {
  1479. /* if there's no display, let's just guess some basic values */
  1480. var->xres = 320;
  1481. var->yres = 240;
  1482. var->xres_virtual = var->xres;
  1483. var->yres_virtual = var->yres;
  1484. if (!var->bits_per_pixel)
  1485. var->bits_per_pixel = 16;
  1486. }
  1487. r = check_fb_var(fbi, var);
  1488. if (r)
  1489. goto err;
  1490. set_fb_fix(fbi);
  1491. r = setup_vrfb_rotation(fbi);
  1492. if (r)
  1493. goto err;
  1494. r = fb_alloc_cmap(&fbi->cmap, 256, 0);
  1495. if (r)
  1496. dev_err(fbdev->dev, "unable to allocate color map memory\n");
  1497. err:
  1498. return r;
  1499. }
  1500. static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
  1501. {
  1502. fb_dealloc_cmap(&fbi->cmap);
  1503. }
  1504. static void omapfb_free_resources(struct omapfb2_device *fbdev)
  1505. {
  1506. int i;
  1507. DBG("free_resources\n");
  1508. if (fbdev == NULL)
  1509. return;
  1510. for (i = 0; i < fbdev->num_fbs; i++)
  1511. unregister_framebuffer(fbdev->fbs[i]);
  1512. /* free the reserved fbmem */
  1513. omapfb_free_all_fbmem(fbdev);
  1514. for (i = 0; i < fbdev->num_fbs; i++) {
  1515. fbinfo_cleanup(fbdev, fbdev->fbs[i]);
  1516. framebuffer_release(fbdev->fbs[i]);
  1517. }
  1518. for (i = 0; i < fbdev->num_displays; i++) {
  1519. if (fbdev->displays[i]->state != OMAP_DSS_DISPLAY_DISABLED)
  1520. fbdev->displays[i]->driver->disable(fbdev->displays[i]);
  1521. omap_dss_put_device(fbdev->displays[i]);
  1522. }
  1523. dev_set_drvdata(fbdev->dev, NULL);
  1524. kfree(fbdev);
  1525. }
  1526. static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
  1527. {
  1528. int r, i;
  1529. fbdev->num_fbs = 0;
  1530. DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
  1531. /* allocate fb_infos */
  1532. for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
  1533. struct fb_info *fbi;
  1534. struct omapfb_info *ofbi;
  1535. fbi = framebuffer_alloc(sizeof(struct omapfb_info),
  1536. fbdev->dev);
  1537. if (fbi == NULL) {
  1538. dev_err(fbdev->dev,
  1539. "unable to allocate memory for plane info\n");
  1540. return -ENOMEM;
  1541. }
  1542. clear_fb_info(fbi);
  1543. fbdev->fbs[i] = fbi;
  1544. ofbi = FB2OFB(fbi);
  1545. ofbi->fbdev = fbdev;
  1546. ofbi->id = i;
  1547. /* assign these early, so that fb alloc can use them */
  1548. ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
  1549. OMAP_DSS_ROT_DMA;
  1550. ofbi->mirror = def_mirror;
  1551. fbdev->num_fbs++;
  1552. }
  1553. DBG("fb_infos allocated\n");
  1554. /* assign overlays for the fbs */
  1555. for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
  1556. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
  1557. ofbi->overlays[0] = fbdev->overlays[i];
  1558. ofbi->num_overlays = 1;
  1559. }
  1560. /* allocate fb memories */
  1561. r = omapfb_allocate_all_fbs(fbdev);
  1562. if (r) {
  1563. dev_err(fbdev->dev, "failed to allocate fbmem\n");
  1564. return r;
  1565. }
  1566. DBG("fbmems allocated\n");
  1567. /* setup fb_infos */
  1568. for (i = 0; i < fbdev->num_fbs; i++) {
  1569. r = omapfb_fb_init(fbdev, fbdev->fbs[i]);
  1570. if (r) {
  1571. dev_err(fbdev->dev, "failed to setup fb_info\n");
  1572. return r;
  1573. }
  1574. }
  1575. DBG("fb_infos initialized\n");
  1576. for (i = 0; i < fbdev->num_fbs; i++) {
  1577. r = register_framebuffer(fbdev->fbs[i]);
  1578. if (r != 0) {
  1579. dev_err(fbdev->dev,
  1580. "registering framebuffer %d failed\n", i);
  1581. return r;
  1582. }
  1583. }
  1584. DBG("framebuffers registered\n");
  1585. for (i = 0; i < fbdev->num_fbs; i++) {
  1586. r = omapfb_apply_changes(fbdev->fbs[i], 1);
  1587. if (r) {
  1588. dev_err(fbdev->dev, "failed to change mode\n");
  1589. return r;
  1590. }
  1591. }
  1592. DBG("create sysfs for fbs\n");
  1593. r = omapfb_create_sysfs(fbdev);
  1594. if (r) {
  1595. dev_err(fbdev->dev, "failed to create sysfs entries\n");
  1596. return r;
  1597. }
  1598. /* Enable fb0 */
  1599. if (fbdev->num_fbs > 0) {
  1600. struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
  1601. if (ofbi->num_overlays > 0) {
  1602. struct omap_overlay *ovl = ofbi->overlays[0];
  1603. r = omapfb_overlay_enable(ovl, 1);
  1604. if (r) {
  1605. dev_err(fbdev->dev,
  1606. "failed to enable overlay\n");
  1607. return r;
  1608. }
  1609. }
  1610. }
  1611. DBG("create_framebuffers done\n");
  1612. return 0;
  1613. }
  1614. static int omapfb_mode_to_timings(const char *mode_str,
  1615. struct omap_video_timings *timings, u8 *bpp)
  1616. {
  1617. struct fb_info fbi;
  1618. struct fb_var_screeninfo var;
  1619. struct fb_ops fbops;
  1620. int r;
  1621. #ifdef CONFIG_OMAP2_DSS_VENC
  1622. if (strcmp(mode_str, "pal") == 0) {
  1623. *timings = omap_dss_pal_timings;
  1624. *bpp = 0;
  1625. return 0;
  1626. } else if (strcmp(mode_str, "ntsc") == 0) {
  1627. *timings = omap_dss_ntsc_timings;
  1628. *bpp = 0;
  1629. return 0;
  1630. }
  1631. #endif
  1632. /* this is quite a hack, but I wanted to use the modedb and for
  1633. * that we need fb_info and var, so we create dummy ones */
  1634. memset(&fbi, 0, sizeof(fbi));
  1635. memset(&var, 0, sizeof(var));
  1636. memset(&fbops, 0, sizeof(fbops));
  1637. fbi.fbops = &fbops;
  1638. r = fb_find_mode(&var, &fbi, mode_str, NULL, 0, NULL, 24);
  1639. if (r != 0) {
  1640. timings->pixel_clock = PICOS2KHZ(var.pixclock);
  1641. timings->hfp = var.left_margin;
  1642. timings->hbp = var.right_margin;
  1643. timings->vfp = var.upper_margin;
  1644. timings->vbp = var.lower_margin;
  1645. timings->hsw = var.hsync_len;
  1646. timings->vsw = var.vsync_len;
  1647. timings->x_res = var.xres;
  1648. timings->y_res = var.yres;
  1649. switch (var.bits_per_pixel) {
  1650. case 16:
  1651. *bpp = 16;
  1652. break;
  1653. case 24:
  1654. case 32:
  1655. default:
  1656. *bpp = 24;
  1657. break;
  1658. }
  1659. return 0;
  1660. } else {
  1661. return -EINVAL;
  1662. }
  1663. }
  1664. static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
  1665. struct omap_dss_device *display, char *mode_str)
  1666. {
  1667. int r;
  1668. u8 bpp;
  1669. struct omap_video_timings timings;
  1670. r = omapfb_mode_to_timings(mode_str, &timings, &bpp);
  1671. if (r)
  1672. return r;
  1673. fbdev->bpp_overrides[fbdev->num_bpp_overrides].dssdev = display;
  1674. fbdev->bpp_overrides[fbdev->num_bpp_overrides].bpp = bpp;
  1675. ++fbdev->num_bpp_overrides;
  1676. if (!display->driver->check_timings || !display->driver->set_timings)
  1677. return -EINVAL;
  1678. r = display->driver->check_timings(display, &timings);
  1679. if (r)
  1680. return r;
  1681. display->driver->set_timings(display, &timings);
  1682. return 0;
  1683. }
  1684. static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
  1685. struct omap_dss_device *dssdev)
  1686. {
  1687. int i;
  1688. BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
  1689. for (i = 0; i < fbdev->num_bpp_overrides; ++i) {
  1690. if (dssdev == fbdev->bpp_overrides[i].dssdev)
  1691. return fbdev->bpp_overrides[i].bpp;
  1692. }
  1693. return dssdev->driver->get_recommended_bpp(dssdev);
  1694. }
  1695. static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
  1696. {
  1697. char *str, *options, *this_opt;
  1698. int r = 0;
  1699. str = kmalloc(strlen(def_mode) + 1, GFP_KERNEL);
  1700. strcpy(str, def_mode);
  1701. options = str;
  1702. while (!r && (this_opt = strsep(&options, ",")) != NULL) {
  1703. char *p, *display_str, *mode_str;
  1704. struct omap_dss_device *display;
  1705. int i;
  1706. p = strchr(this_opt, ':');
  1707. if (!p) {
  1708. r = -EINVAL;
  1709. break;
  1710. }
  1711. *p = 0;
  1712. display_str = this_opt;
  1713. mode_str = p + 1;
  1714. display = NULL;
  1715. for (i = 0; i < fbdev->num_displays; ++i) {
  1716. if (strcmp(fbdev->displays[i]->name,
  1717. display_str) == 0) {
  1718. display = fbdev->displays[i];
  1719. break;
  1720. }
  1721. }
  1722. if (!display) {
  1723. r = -EINVAL;
  1724. break;
  1725. }
  1726. r = omapfb_set_def_mode(fbdev, display, mode_str);
  1727. if (r)
  1728. break;
  1729. }
  1730. kfree(str);
  1731. return r;
  1732. }
  1733. static int omapfb_probe(struct platform_device *pdev)
  1734. {
  1735. struct omapfb2_device *fbdev = NULL;
  1736. int r = 0;
  1737. int i;
  1738. struct omap_overlay *ovl;
  1739. struct omap_dss_device *def_display;
  1740. struct omap_dss_device *dssdev;
  1741. DBG("omapfb_probe\n");
  1742. if (pdev->num_resources != 0) {
  1743. dev_err(&pdev->dev, "probed for an unknown device\n");
  1744. r = -ENODEV;
  1745. goto err0;
  1746. }
  1747. fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
  1748. if (fbdev == NULL) {
  1749. r = -ENOMEM;
  1750. goto err0;
  1751. }
  1752. mutex_init(&fbdev->mtx);
  1753. fbdev->dev = &pdev->dev;
  1754. platform_set_drvdata(pdev, fbdev);
  1755. r = 0;
  1756. fbdev->num_displays = 0;
  1757. dssdev = NULL;
  1758. for_each_dss_dev(dssdev) {
  1759. omap_dss_get_device(dssdev);
  1760. if (!dssdev->driver) {
  1761. dev_err(&pdev->dev, "no driver for display\n");
  1762. r = -ENODEV;
  1763. }
  1764. fbdev->displays[fbdev->num_displays++] = dssdev;
  1765. }
  1766. if (r)
  1767. goto cleanup;
  1768. if (fbdev->num_displays == 0) {
  1769. dev_err(&pdev->dev, "no displays\n");
  1770. r = -EINVAL;
  1771. goto cleanup;
  1772. }
  1773. fbdev->num_overlays = omap_dss_get_num_overlays();
  1774. for (i = 0; i < fbdev->num_overlays; i++)
  1775. fbdev->overlays[i] = omap_dss_get_overlay(i);
  1776. fbdev->num_managers = omap_dss_get_num_overlay_managers();
  1777. for (i = 0; i < fbdev->num_managers; i++)
  1778. fbdev->managers[i] = omap_dss_get_overlay_manager(i);
  1779. if (def_mode && strlen(def_mode) > 0) {
  1780. if (omapfb_parse_def_modes(fbdev))
  1781. dev_warn(&pdev->dev, "cannot parse default modes\n");
  1782. }
  1783. r = omapfb_create_framebuffers(fbdev);
  1784. if (r)
  1785. goto cleanup;
  1786. for (i = 0; i < fbdev->num_managers; i++) {
  1787. struct omap_overlay_manager *mgr;
  1788. mgr = fbdev->managers[i];
  1789. r = mgr->apply(mgr);
  1790. if (r)
  1791. dev_warn(fbdev->dev, "failed to apply dispc config\n");
  1792. }
  1793. DBG("mgr->apply'ed\n");
  1794. /* gfx overlay should be the default one. find a display
  1795. * connected to that, and use it as default display */
  1796. ovl = omap_dss_get_overlay(0);
  1797. if (ovl->manager && ovl->manager->device) {
  1798. def_display = ovl->manager->device;
  1799. } else {
  1800. dev_warn(&pdev->dev, "cannot find default display\n");
  1801. def_display = NULL;
  1802. }
  1803. if (def_display) {
  1804. struct omap_dss_driver *dssdrv = def_display->driver;
  1805. r = def_display->driver->enable(def_display);
  1806. if (r) {
  1807. dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
  1808. def_display->name);
  1809. goto cleanup;
  1810. }
  1811. if (def_display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
  1812. u16 w, h;
  1813. if (dssdrv->enable_te)
  1814. dssdrv->enable_te(def_display, 1);
  1815. if (dssdrv->set_update_mode)
  1816. dssdrv->set_update_mode(def_display,
  1817. OMAP_DSS_UPDATE_MANUAL);
  1818. dssdrv->get_resolution(def_display, &w, &h);
  1819. def_display->driver->update(def_display, 0, 0, w, h);
  1820. } else {
  1821. if (dssdrv->set_update_mode)
  1822. dssdrv->set_update_mode(def_display,
  1823. OMAP_DSS_UPDATE_AUTO);
  1824. }
  1825. }
  1826. return 0;
  1827. cleanup:
  1828. omapfb_free_resources(fbdev);
  1829. err0:
  1830. dev_err(&pdev->dev, "failed to setup omapfb\n");
  1831. return r;
  1832. }
  1833. static int omapfb_remove(struct platform_device *pdev)
  1834. {
  1835. struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
  1836. /* FIXME: wait till completion of pending events */
  1837. omapfb_remove_sysfs(fbdev);
  1838. omapfb_free_resources(fbdev);
  1839. return 0;
  1840. }
  1841. static struct platform_driver omapfb_driver = {
  1842. .probe = omapfb_probe,
  1843. .remove = omapfb_remove,
  1844. .driver = {
  1845. .name = "omapfb",
  1846. .owner = THIS_MODULE,
  1847. },
  1848. };
  1849. static int __init omapfb_init(void)
  1850. {
  1851. DBG("omapfb_init\n");
  1852. if (platform_driver_register(&omapfb_driver)) {
  1853. printk(KERN_ERR "failed to register omapfb driver\n");
  1854. return -ENODEV;
  1855. }
  1856. return 0;
  1857. }
  1858. static void __exit omapfb_exit(void)
  1859. {
  1860. DBG("omapfb_exit\n");
  1861. platform_driver_unregister(&omapfb_driver);
  1862. }
  1863. module_param_named(mode, def_mode, charp, 0);
  1864. module_param_named(vram, def_vram, charp, 0);
  1865. module_param_named(rotate, def_rotate, int, 0);
  1866. module_param_named(vrfb, def_vrfb, bool, 0);
  1867. module_param_named(mirror, def_mirror, bool, 0);
  1868. /* late_initcall to let panel/ctrl drivers loaded first.
  1869. * I guess better option would be a more dynamic approach,
  1870. * so that omapfb reacts to new panels when they are loaded */
  1871. late_initcall(omapfb_init);
  1872. /*module_init(omapfb_init);*/
  1873. module_exit(omapfb_exit);
  1874. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
  1875. MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
  1876. MODULE_LICENSE("GPL v2");