omapfb-main.c 52 KB

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