omapfb-main.c 53 KB

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