omapfb-main.c 57 KB

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