omapfb-main.c 56 KB

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