s3c-fb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /* linux/drivers/video/s3c-fb.c
  2. *
  3. * Copyright 2008 Openmoko Inc.
  4. * Copyright 2008-2010 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. * http://armlinux.simtec.co.uk/
  7. *
  8. * Samsung SoC Framebuffer driver
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software FoundatIon.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/clk.h>
  21. #include <linux/fb.h>
  22. #include <linux/io.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/interrupt.h>
  25. #include <mach/map.h>
  26. #include <plat/regs-fb-v4.h>
  27. #include <plat/fb.h>
  28. /* This driver will export a number of framebuffer interfaces depending
  29. * on the configuration passed in via the platform data. Each fb instance
  30. * maps to a hardware window. Currently there is no support for runtime
  31. * setting of the alpha-blending functions that each window has, so only
  32. * window 0 is actually useful.
  33. *
  34. * Window 0 is treated specially, it is used for the basis of the LCD
  35. * output timings and as the control for the output power-down state.
  36. */
  37. /* note, the previous use of <mach/regs-fb.h> to get platform specific data
  38. * has been replaced by using the platform device name to pick the correct
  39. * configuration data for the system.
  40. */
  41. #ifdef CONFIG_FB_S3C_DEBUG_REGWRITE
  42. #undef writel
  43. #define writel(v, r) do { \
  44. printk(KERN_DEBUG "%s: %08x => %p\n", __func__, (unsigned int)v, r); \
  45. __raw_writel(v, r); } while(0)
  46. #endif /* FB_S3C_DEBUG_REGWRITE */
  47. /* irq_flags bits */
  48. #define S3C_FB_VSYNC_IRQ_EN 0
  49. #define VSYNC_TIMEOUT_MSEC 50
  50. struct s3c_fb;
  51. #define VALID_BPP(x) (1 << ((x) - 1))
  52. #define OSD_BASE(win, variant) ((variant).osd + ((win) * (variant).osd_stride))
  53. #define VIDOSD_A(win, variant) (OSD_BASE(win, variant) + 0x00)
  54. #define VIDOSD_B(win, variant) (OSD_BASE(win, variant) + 0x04)
  55. #define VIDOSD_C(win, variant) (OSD_BASE(win, variant) + 0x08)
  56. #define VIDOSD_D(win, variant) (OSD_BASE(win, variant) + 0x0C)
  57. /**
  58. * struct s3c_fb_variant - fb variant information
  59. * @is_2443: Set if S3C2443/S3C2416 style hardware.
  60. * @nr_windows: The number of windows.
  61. * @vidtcon: The base for the VIDTCONx registers
  62. * @wincon: The base for the WINxCON registers.
  63. * @winmap: The base for the WINxMAP registers.
  64. * @keycon: The abse for the WxKEYCON registers.
  65. * @buf_start: Offset of buffer start registers.
  66. * @buf_size: Offset of buffer size registers.
  67. * @buf_end: Offset of buffer end registers.
  68. * @osd: The base for the OSD registers.
  69. * @palette: Address of palette memory, or 0 if none.
  70. * @has_prtcon: Set if has PRTCON register.
  71. * @has_shadowcon: Set if has SHADOWCON register.
  72. */
  73. struct s3c_fb_variant {
  74. unsigned int is_2443:1;
  75. unsigned short nr_windows;
  76. unsigned short vidtcon;
  77. unsigned short wincon;
  78. unsigned short winmap;
  79. unsigned short keycon;
  80. unsigned short buf_start;
  81. unsigned short buf_end;
  82. unsigned short buf_size;
  83. unsigned short osd;
  84. unsigned short osd_stride;
  85. unsigned short palette[S3C_FB_MAX_WIN];
  86. unsigned int has_prtcon:1;
  87. unsigned int has_shadowcon:1;
  88. };
  89. /**
  90. * struct s3c_fb_win_variant
  91. * @has_osd_c: Set if has OSD C register.
  92. * @has_osd_d: Set if has OSD D register.
  93. * @has_osd_alpha: Set if can change alpha transparency for a window.
  94. * @palette_sz: Size of palette in entries.
  95. * @palette_16bpp: Set if palette is 16bits wide.
  96. * @osd_size_off: If != 0, supports setting up OSD for a window; the appropriate
  97. * register is located at the given offset from OSD_BASE.
  98. * @valid_bpp: 1 bit per BPP setting to show valid bits-per-pixel.
  99. *
  100. * valid_bpp bit x is set if (x+1)BPP is supported.
  101. */
  102. struct s3c_fb_win_variant {
  103. unsigned int has_osd_c:1;
  104. unsigned int has_osd_d:1;
  105. unsigned int has_osd_alpha:1;
  106. unsigned int palette_16bpp:1;
  107. unsigned short osd_size_off;
  108. unsigned short palette_sz;
  109. u32 valid_bpp;
  110. };
  111. /**
  112. * struct s3c_fb_driverdata - per-device type driver data for init time.
  113. * @variant: The variant information for this driver.
  114. * @win: The window information for each window.
  115. */
  116. struct s3c_fb_driverdata {
  117. struct s3c_fb_variant variant;
  118. struct s3c_fb_win_variant *win[S3C_FB_MAX_WIN];
  119. };
  120. /**
  121. * struct s3c_fb_palette - palette information
  122. * @r: Red bitfield.
  123. * @g: Green bitfield.
  124. * @b: Blue bitfield.
  125. * @a: Alpha bitfield.
  126. */
  127. struct s3c_fb_palette {
  128. struct fb_bitfield r;
  129. struct fb_bitfield g;
  130. struct fb_bitfield b;
  131. struct fb_bitfield a;
  132. };
  133. /**
  134. * struct s3c_fb_win - per window private data for each framebuffer.
  135. * @windata: The platform data supplied for the window configuration.
  136. * @parent: The hardware that this window is part of.
  137. * @fbinfo: Pointer pack to the framebuffer info for this window.
  138. * @varint: The variant information for this window.
  139. * @palette_buffer: Buffer/cache to hold palette entries.
  140. * @pseudo_palette: For use in TRUECOLOUR modes for entries 0..15/
  141. * @index: The window number of this window.
  142. * @palette: The bitfields for changing r/g/b into a hardware palette entry.
  143. */
  144. struct s3c_fb_win {
  145. struct s3c_fb_pd_win *windata;
  146. struct s3c_fb *parent;
  147. struct fb_info *fbinfo;
  148. struct s3c_fb_palette palette;
  149. struct s3c_fb_win_variant variant;
  150. u32 *palette_buffer;
  151. u32 pseudo_palette[16];
  152. unsigned int index;
  153. };
  154. /**
  155. * struct s3c_fb_vsync - vsync information
  156. * @wait: a queue for processes waiting for vsync
  157. * @count: vsync interrupt count
  158. */
  159. struct s3c_fb_vsync {
  160. wait_queue_head_t wait;
  161. unsigned int count;
  162. };
  163. /**
  164. * struct s3c_fb - overall hardware state of the hardware
  165. * @dev: The device that we bound to, for printing, etc.
  166. * @regs_res: The resource we claimed for the IO registers.
  167. * @bus_clk: The clk (hclk) feeding our interface and possibly pixclk.
  168. * @regs: The mapped hardware registers.
  169. * @variant: Variant information for this hardware.
  170. * @enabled: A bitmask of enabled hardware windows.
  171. * @pdata: The platform configuration data passed with the device.
  172. * @windows: The hardware windows that have been claimed.
  173. * @irq_no: IRQ line number
  174. * @irq_flags: irq flags
  175. * @vsync_info: VSYNC-related information (count, queues...)
  176. */
  177. struct s3c_fb {
  178. struct device *dev;
  179. struct resource *regs_res;
  180. struct clk *bus_clk;
  181. void __iomem *regs;
  182. struct s3c_fb_variant variant;
  183. unsigned char enabled;
  184. struct s3c_fb_platdata *pdata;
  185. struct s3c_fb_win *windows[S3C_FB_MAX_WIN];
  186. int irq_no;
  187. unsigned long irq_flags;
  188. struct s3c_fb_vsync vsync_info;
  189. };
  190. /**
  191. * s3c_fb_validate_win_bpp - validate the bits-per-pixel for this mode.
  192. * @win: The device window.
  193. * @bpp: The bit depth.
  194. */
  195. static bool s3c_fb_validate_win_bpp(struct s3c_fb_win *win, unsigned int bpp)
  196. {
  197. return win->variant.valid_bpp & VALID_BPP(bpp);
  198. }
  199. /**
  200. * s3c_fb_check_var() - framebuffer layer request to verify a given mode.
  201. * @var: The screen information to verify.
  202. * @info: The framebuffer device.
  203. *
  204. * Framebuffer layer call to verify the given information and allow us to
  205. * update various information depending on the hardware capabilities.
  206. */
  207. static int s3c_fb_check_var(struct fb_var_screeninfo *var,
  208. struct fb_info *info)
  209. {
  210. struct s3c_fb_win *win = info->par;
  211. struct s3c_fb_pd_win *windata = win->windata;
  212. struct s3c_fb *sfb = win->parent;
  213. dev_dbg(sfb->dev, "checking parameters\n");
  214. var->xres_virtual = max((unsigned int)windata->virtual_x, var->xres);
  215. var->yres_virtual = max((unsigned int)windata->virtual_y, var->yres);
  216. if (!s3c_fb_validate_win_bpp(win, var->bits_per_pixel)) {
  217. dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
  218. win->index, var->bits_per_pixel);
  219. return -EINVAL;
  220. }
  221. /* always ensure these are zero, for drop through cases below */
  222. var->transp.offset = 0;
  223. var->transp.length = 0;
  224. switch (var->bits_per_pixel) {
  225. case 1:
  226. case 2:
  227. case 4:
  228. case 8:
  229. if (sfb->variant.palette[win->index] != 0) {
  230. /* non palletised, A:1,R:2,G:3,B:2 mode */
  231. var->red.offset = 4;
  232. var->green.offset = 2;
  233. var->blue.offset = 0;
  234. var->red.length = 5;
  235. var->green.length = 3;
  236. var->blue.length = 2;
  237. var->transp.offset = 7;
  238. var->transp.length = 1;
  239. } else {
  240. var->red.offset = 0;
  241. var->red.length = var->bits_per_pixel;
  242. var->green = var->red;
  243. var->blue = var->red;
  244. }
  245. break;
  246. case 19:
  247. /* 666 with one bit alpha/transparency */
  248. var->transp.offset = 18;
  249. var->transp.length = 1;
  250. case 18:
  251. var->bits_per_pixel = 32;
  252. /* 666 format */
  253. var->red.offset = 12;
  254. var->green.offset = 6;
  255. var->blue.offset = 0;
  256. var->red.length = 6;
  257. var->green.length = 6;
  258. var->blue.length = 6;
  259. break;
  260. case 16:
  261. /* 16 bpp, 565 format */
  262. var->red.offset = 11;
  263. var->green.offset = 5;
  264. var->blue.offset = 0;
  265. var->red.length = 5;
  266. var->green.length = 6;
  267. var->blue.length = 5;
  268. break;
  269. case 28:
  270. case 25:
  271. var->transp.length = var->bits_per_pixel - 24;
  272. var->transp.offset = 24;
  273. /* drop through */
  274. case 24:
  275. /* our 24bpp is unpacked, so 32bpp */
  276. var->bits_per_pixel = 32;
  277. case 32:
  278. var->red.offset = 16;
  279. var->red.length = 8;
  280. var->green.offset = 8;
  281. var->green.length = 8;
  282. var->blue.offset = 0;
  283. var->blue.length = 8;
  284. break;
  285. default:
  286. dev_err(sfb->dev, "invalid bpp\n");
  287. }
  288. dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
  289. return 0;
  290. }
  291. /**
  292. * s3c_fb_calc_pixclk() - calculate the divider to create the pixel clock.
  293. * @sfb: The hardware state.
  294. * @pixclock: The pixel clock wanted, in picoseconds.
  295. *
  296. * Given the specified pixel clock, work out the necessary divider to get
  297. * close to the output frequency.
  298. */
  299. static int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
  300. {
  301. unsigned long clk = clk_get_rate(sfb->bus_clk);
  302. unsigned long long tmp;
  303. unsigned int result;
  304. tmp = (unsigned long long)clk;
  305. tmp *= pixclk;
  306. do_div(tmp, 1000000000UL);
  307. result = (unsigned int)tmp / 1000;
  308. dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
  309. pixclk, clk, result, clk / result);
  310. return result;
  311. }
  312. /**
  313. * s3c_fb_align_word() - align pixel count to word boundary
  314. * @bpp: The number of bits per pixel
  315. * @pix: The value to be aligned.
  316. *
  317. * Align the given pixel count so that it will start on an 32bit word
  318. * boundary.
  319. */
  320. static int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
  321. {
  322. int pix_per_word;
  323. if (bpp > 16)
  324. return pix;
  325. pix_per_word = (8 * 32) / bpp;
  326. return ALIGN(pix, pix_per_word);
  327. }
  328. /**
  329. * vidosd_set_size() - set OSD size for a window
  330. *
  331. * @win: the window to set OSD size for
  332. * @size: OSD size register value
  333. */
  334. static void vidosd_set_size(struct s3c_fb_win *win, u32 size)
  335. {
  336. struct s3c_fb *sfb = win->parent;
  337. /* OSD can be set up if osd_size_off != 0 for this window */
  338. if (win->variant.osd_size_off)
  339. writel(size, sfb->regs + OSD_BASE(win->index, sfb->variant)
  340. + win->variant.osd_size_off);
  341. }
  342. /**
  343. * vidosd_set_alpha() - set alpha transparency for a window
  344. *
  345. * @win: the window to set OSD size for
  346. * @alpha: alpha register value
  347. */
  348. static void vidosd_set_alpha(struct s3c_fb_win *win, u32 alpha)
  349. {
  350. struct s3c_fb *sfb = win->parent;
  351. if (win->variant.has_osd_alpha)
  352. writel(alpha, sfb->regs + VIDOSD_C(win->index, sfb->variant));
  353. }
  354. /**
  355. * shadow_protect_win() - disable updating values from shadow registers at vsync
  356. *
  357. * @win: window to protect registers for
  358. * @protect: 1 to protect (disable updates)
  359. */
  360. static void shadow_protect_win(struct s3c_fb_win *win, bool protect)
  361. {
  362. struct s3c_fb *sfb = win->parent;
  363. u32 reg;
  364. if (protect) {
  365. if (sfb->variant.has_prtcon) {
  366. writel(PRTCON_PROTECT, sfb->regs + PRTCON);
  367. } else if (sfb->variant.has_shadowcon) {
  368. reg = readl(sfb->regs + SHADOWCON);
  369. writel(reg | SHADOWCON_WINx_PROTECT(win->index),
  370. sfb->regs + SHADOWCON);
  371. }
  372. } else {
  373. if (sfb->variant.has_prtcon) {
  374. writel(0, sfb->regs + PRTCON);
  375. } else if (sfb->variant.has_shadowcon) {
  376. reg = readl(sfb->regs + SHADOWCON);
  377. writel(reg & ~SHADOWCON_WINx_PROTECT(win->index),
  378. sfb->regs + SHADOWCON);
  379. }
  380. }
  381. }
  382. /**
  383. * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
  384. * @info: The framebuffer to change.
  385. *
  386. * Framebuffer layer request to set a new mode for the specified framebuffer
  387. */
  388. static int s3c_fb_set_par(struct fb_info *info)
  389. {
  390. struct fb_var_screeninfo *var = &info->var;
  391. struct s3c_fb_win *win = info->par;
  392. struct s3c_fb *sfb = win->parent;
  393. void __iomem *regs = sfb->regs;
  394. void __iomem *buf = regs;
  395. int win_no = win->index;
  396. u32 alpha = 0;
  397. u32 data;
  398. u32 pagewidth;
  399. int clkdiv;
  400. dev_dbg(sfb->dev, "setting framebuffer parameters\n");
  401. shadow_protect_win(win, 1);
  402. switch (var->bits_per_pixel) {
  403. case 32:
  404. case 24:
  405. case 16:
  406. case 12:
  407. info->fix.visual = FB_VISUAL_TRUECOLOR;
  408. break;
  409. case 8:
  410. if (win->variant.palette_sz >= 256)
  411. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  412. else
  413. info->fix.visual = FB_VISUAL_TRUECOLOR;
  414. break;
  415. case 1:
  416. info->fix.visual = FB_VISUAL_MONO01;
  417. break;
  418. default:
  419. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  420. break;
  421. }
  422. info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
  423. info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
  424. info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
  425. /* disable the window whilst we update it */
  426. writel(0, regs + WINCON(win_no));
  427. /* use platform specified window as the basis for the lcd timings */
  428. if (win_no == sfb->pdata->default_win) {
  429. clkdiv = s3c_fb_calc_pixclk(sfb, var->pixclock);
  430. data = sfb->pdata->vidcon0;
  431. data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
  432. if (clkdiv > 1)
  433. data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
  434. else
  435. data &= ~VIDCON0_CLKDIR; /* 1:1 clock */
  436. /* write the timing data to the panel */
  437. if (sfb->variant.is_2443)
  438. data |= (1 << 5);
  439. data |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  440. writel(data, regs + VIDCON0);
  441. data = VIDTCON0_VBPD(var->upper_margin - 1) |
  442. VIDTCON0_VFPD(var->lower_margin - 1) |
  443. VIDTCON0_VSPW(var->vsync_len - 1);
  444. writel(data, regs + sfb->variant.vidtcon);
  445. data = VIDTCON1_HBPD(var->left_margin - 1) |
  446. VIDTCON1_HFPD(var->right_margin - 1) |
  447. VIDTCON1_HSPW(var->hsync_len - 1);
  448. /* VIDTCON1 */
  449. writel(data, regs + sfb->variant.vidtcon + 4);
  450. data = VIDTCON2_LINEVAL(var->yres - 1) |
  451. VIDTCON2_HOZVAL(var->xres - 1);
  452. writel(data, regs +sfb->variant.vidtcon + 8 );
  453. }
  454. /* write the buffer address */
  455. /* start and end registers stride is 8 */
  456. buf = regs + win_no * 8;
  457. writel(info->fix.smem_start, buf + sfb->variant.buf_start);
  458. data = info->fix.smem_start + info->fix.line_length * var->yres;
  459. writel(data, buf + sfb->variant.buf_end);
  460. pagewidth = (var->xres * var->bits_per_pixel) >> 3;
  461. data = VIDW_BUF_SIZE_OFFSET(info->fix.line_length - pagewidth) |
  462. VIDW_BUF_SIZE_PAGEWIDTH(pagewidth);
  463. writel(data, regs + sfb->variant.buf_size + (win_no * 4));
  464. /* write 'OSD' registers to control position of framebuffer */
  465. data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0);
  466. writel(data, regs + VIDOSD_A(win_no, sfb->variant));
  467. data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
  468. var->xres - 1)) |
  469. VIDOSDxB_BOTRIGHT_Y(var->yres - 1);
  470. writel(data, regs + VIDOSD_B(win_no, sfb->variant));
  471. data = var->xres * var->yres;
  472. alpha = VIDISD14C_ALPHA1_R(0xf) |
  473. VIDISD14C_ALPHA1_G(0xf) |
  474. VIDISD14C_ALPHA1_B(0xf);
  475. vidosd_set_alpha(win, alpha);
  476. vidosd_set_size(win, data);
  477. data = WINCONx_ENWIN;
  478. /* note, since we have to round up the bits-per-pixel, we end up
  479. * relying on the bitfield information for r/g/b/a to work out
  480. * exactly which mode of operation is intended. */
  481. switch (var->bits_per_pixel) {
  482. case 1:
  483. data |= WINCON0_BPPMODE_1BPP;
  484. data |= WINCONx_BITSWP;
  485. data |= WINCONx_BURSTLEN_4WORD;
  486. break;
  487. case 2:
  488. data |= WINCON0_BPPMODE_2BPP;
  489. data |= WINCONx_BITSWP;
  490. data |= WINCONx_BURSTLEN_8WORD;
  491. break;
  492. case 4:
  493. data |= WINCON0_BPPMODE_4BPP;
  494. data |= WINCONx_BITSWP;
  495. data |= WINCONx_BURSTLEN_8WORD;
  496. break;
  497. case 8:
  498. if (var->transp.length != 0)
  499. data |= WINCON1_BPPMODE_8BPP_1232;
  500. else
  501. data |= WINCON0_BPPMODE_8BPP_PALETTE;
  502. data |= WINCONx_BURSTLEN_8WORD;
  503. data |= WINCONx_BYTSWP;
  504. break;
  505. case 16:
  506. if (var->transp.length != 0)
  507. data |= WINCON1_BPPMODE_16BPP_A1555;
  508. else
  509. data |= WINCON0_BPPMODE_16BPP_565;
  510. data |= WINCONx_HAWSWP;
  511. data |= WINCONx_BURSTLEN_16WORD;
  512. break;
  513. case 24:
  514. case 32:
  515. if (var->red.length == 6) {
  516. if (var->transp.length != 0)
  517. data |= WINCON1_BPPMODE_19BPP_A1666;
  518. else
  519. data |= WINCON1_BPPMODE_18BPP_666;
  520. } else if (var->transp.length == 1)
  521. data |= WINCON1_BPPMODE_25BPP_A1888
  522. | WINCON1_BLD_PIX;
  523. else if (var->transp.length == 4)
  524. data |= WINCON1_BPPMODE_28BPP_A4888
  525. | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
  526. else
  527. data |= WINCON0_BPPMODE_24BPP_888;
  528. data |= WINCONx_WSWP;
  529. data |= WINCONx_BURSTLEN_16WORD;
  530. break;
  531. }
  532. /* Enable the colour keying for the window below this one */
  533. if (win_no > 0) {
  534. u32 keycon0_data = 0, keycon1_data = 0;
  535. void __iomem *keycon = regs + sfb->variant.keycon;
  536. keycon0_data = ~(WxKEYCON0_KEYBL_EN |
  537. WxKEYCON0_KEYEN_F |
  538. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  539. keycon1_data = WxKEYCON1_COLVAL(0xffffff);
  540. keycon += (win_no - 1) * 8;
  541. writel(keycon0_data, keycon + WKEYCON0);
  542. writel(keycon1_data, keycon + WKEYCON1);
  543. }
  544. writel(data, regs + sfb->variant.wincon + (win_no * 4));
  545. writel(0x0, regs + sfb->variant.winmap + (win_no * 4));
  546. /* Enable DMA channel for this window */
  547. if (sfb->variant.has_shadowcon) {
  548. data = readl(sfb->regs + SHADOWCON);
  549. data |= SHADOWCON_CHx_ENABLE(win_no);
  550. writel(data, sfb->regs + SHADOWCON);
  551. }
  552. shadow_protect_win(win, 0);
  553. return 0;
  554. }
  555. /**
  556. * s3c_fb_update_palette() - set or schedule a palette update.
  557. * @sfb: The hardware information.
  558. * @win: The window being updated.
  559. * @reg: The palette index being changed.
  560. * @value: The computed palette value.
  561. *
  562. * Change the value of a palette register, either by directly writing to
  563. * the palette (this requires the palette RAM to be disconnected from the
  564. * hardware whilst this is in progress) or schedule the update for later.
  565. *
  566. * At the moment, since we have no VSYNC interrupt support, we simply set
  567. * the palette entry directly.
  568. */
  569. static void s3c_fb_update_palette(struct s3c_fb *sfb,
  570. struct s3c_fb_win *win,
  571. unsigned int reg,
  572. u32 value)
  573. {
  574. void __iomem *palreg;
  575. u32 palcon;
  576. palreg = sfb->regs + sfb->variant.palette[win->index];
  577. dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
  578. __func__, win->index, reg, palreg, value);
  579. win->palette_buffer[reg] = value;
  580. palcon = readl(sfb->regs + WPALCON);
  581. writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
  582. if (win->variant.palette_16bpp)
  583. writew(value, palreg + (reg * 2));
  584. else
  585. writel(value, palreg + (reg * 4));
  586. writel(palcon, sfb->regs + WPALCON);
  587. }
  588. static inline unsigned int chan_to_field(unsigned int chan,
  589. struct fb_bitfield *bf)
  590. {
  591. chan &= 0xffff;
  592. chan >>= 16 - bf->length;
  593. return chan << bf->offset;
  594. }
  595. /**
  596. * s3c_fb_setcolreg() - framebuffer layer request to change palette.
  597. * @regno: The palette index to change.
  598. * @red: The red field for the palette data.
  599. * @green: The green field for the palette data.
  600. * @blue: The blue field for the palette data.
  601. * @trans: The transparency (alpha) field for the palette data.
  602. * @info: The framebuffer being changed.
  603. */
  604. static int s3c_fb_setcolreg(unsigned regno,
  605. unsigned red, unsigned green, unsigned blue,
  606. unsigned transp, struct fb_info *info)
  607. {
  608. struct s3c_fb_win *win = info->par;
  609. struct s3c_fb *sfb = win->parent;
  610. unsigned int val;
  611. dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
  612. __func__, win->index, regno, red, green, blue);
  613. switch (info->fix.visual) {
  614. case FB_VISUAL_TRUECOLOR:
  615. /* true-colour, use pseudo-palette */
  616. if (regno < 16) {
  617. u32 *pal = info->pseudo_palette;
  618. val = chan_to_field(red, &info->var.red);
  619. val |= chan_to_field(green, &info->var.green);
  620. val |= chan_to_field(blue, &info->var.blue);
  621. pal[regno] = val;
  622. }
  623. break;
  624. case FB_VISUAL_PSEUDOCOLOR:
  625. if (regno < win->variant.palette_sz) {
  626. val = chan_to_field(red, &win->palette.r);
  627. val |= chan_to_field(green, &win->palette.g);
  628. val |= chan_to_field(blue, &win->palette.b);
  629. s3c_fb_update_palette(sfb, win, regno, val);
  630. }
  631. break;
  632. default:
  633. return 1; /* unknown type */
  634. }
  635. return 0;
  636. }
  637. /**
  638. * s3c_fb_enable() - Set the state of the main LCD output
  639. * @sfb: The main framebuffer state.
  640. * @enable: The state to set.
  641. */
  642. static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
  643. {
  644. u32 vidcon0 = readl(sfb->regs + VIDCON0);
  645. if (enable)
  646. vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  647. else {
  648. /* see the note in the framebuffer datasheet about
  649. * why you cannot take both of these bits down at the
  650. * same time. */
  651. if (!(vidcon0 & VIDCON0_ENVID))
  652. return;
  653. vidcon0 |= VIDCON0_ENVID;
  654. vidcon0 &= ~VIDCON0_ENVID_F;
  655. }
  656. writel(vidcon0, sfb->regs + VIDCON0);
  657. }
  658. /**
  659. * s3c_fb_blank() - blank or unblank the given window
  660. * @blank_mode: The blank state from FB_BLANK_*
  661. * @info: The framebuffer to blank.
  662. *
  663. * Framebuffer layer request to change the power state.
  664. */
  665. static int s3c_fb_blank(int blank_mode, struct fb_info *info)
  666. {
  667. struct s3c_fb_win *win = info->par;
  668. struct s3c_fb *sfb = win->parent;
  669. unsigned int index = win->index;
  670. u32 wincon;
  671. dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
  672. wincon = readl(sfb->regs + sfb->variant.wincon + (index * 4));
  673. switch (blank_mode) {
  674. case FB_BLANK_POWERDOWN:
  675. wincon &= ~WINCONx_ENWIN;
  676. sfb->enabled &= ~(1 << index);
  677. /* fall through to FB_BLANK_NORMAL */
  678. case FB_BLANK_NORMAL:
  679. /* disable the DMA and display 0x0 (black) */
  680. writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
  681. sfb->regs + sfb->variant.winmap + (index * 4));
  682. break;
  683. case FB_BLANK_UNBLANK:
  684. writel(0x0, sfb->regs + sfb->variant.winmap + (index * 4));
  685. wincon |= WINCONx_ENWIN;
  686. sfb->enabled |= (1 << index);
  687. break;
  688. case FB_BLANK_VSYNC_SUSPEND:
  689. case FB_BLANK_HSYNC_SUSPEND:
  690. default:
  691. return 1;
  692. }
  693. writel(wincon, sfb->regs + sfb->variant.wincon + (index * 4));
  694. /* Check the enabled state to see if we need to be running the
  695. * main LCD interface, as if there are no active windows then
  696. * it is highly likely that we also do not need to output
  697. * anything.
  698. */
  699. /* We could do something like the following code, but the current
  700. * system of using framebuffer events means that we cannot make
  701. * the distinction between just window 0 being inactive and all
  702. * the windows being down.
  703. *
  704. * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
  705. */
  706. /* we're stuck with this until we can do something about overriding
  707. * the power control using the blanking event for a single fb.
  708. */
  709. if (index == sfb->pdata->default_win)
  710. s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
  711. return 0;
  712. }
  713. /**
  714. * s3c_fb_pan_display() - Pan the display.
  715. *
  716. * Note that the offsets can be written to the device at any time, as their
  717. * values are latched at each vsync automatically. This also means that only
  718. * the last call to this function will have any effect on next vsync, but
  719. * there is no need to sleep waiting for it to prevent tearing.
  720. *
  721. * @var: The screen information to verify.
  722. * @info: The framebuffer device.
  723. */
  724. static int s3c_fb_pan_display(struct fb_var_screeninfo *var,
  725. struct fb_info *info)
  726. {
  727. struct s3c_fb_win *win = info->par;
  728. struct s3c_fb *sfb = win->parent;
  729. void __iomem *buf = sfb->regs + win->index * 8;
  730. unsigned int start_boff, end_boff;
  731. /* Offset in bytes to the start of the displayed area */
  732. start_boff = var->yoffset * info->fix.line_length;
  733. /* X offset depends on the current bpp */
  734. if (info->var.bits_per_pixel >= 8) {
  735. start_boff += var->xoffset * (info->var.bits_per_pixel >> 3);
  736. } else {
  737. switch (info->var.bits_per_pixel) {
  738. case 4:
  739. start_boff += var->xoffset >> 1;
  740. break;
  741. case 2:
  742. start_boff += var->xoffset >> 2;
  743. break;
  744. case 1:
  745. start_boff += var->xoffset >> 3;
  746. break;
  747. default:
  748. dev_err(sfb->dev, "invalid bpp\n");
  749. return -EINVAL;
  750. }
  751. }
  752. /* Offset in bytes to the end of the displayed area */
  753. end_boff = start_boff + var->yres * info->fix.line_length;
  754. /* Temporarily turn off per-vsync update from shadow registers until
  755. * both start and end addresses are updated to prevent corruption */
  756. shadow_protect_win(win, 1);
  757. writel(info->fix.smem_start + start_boff, buf + sfb->variant.buf_start);
  758. writel(info->fix.smem_start + end_boff, buf + sfb->variant.buf_end);
  759. shadow_protect_win(win, 0);
  760. return 0;
  761. }
  762. /**
  763. * s3c_fb_enable_irq() - enable framebuffer interrupts
  764. * @sfb: main hardware state
  765. */
  766. static void s3c_fb_enable_irq(struct s3c_fb *sfb)
  767. {
  768. void __iomem *regs = sfb->regs;
  769. u32 irq_ctrl_reg;
  770. if (!test_and_set_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
  771. /* IRQ disabled, enable it */
  772. irq_ctrl_reg = readl(regs + VIDINTCON0);
  773. irq_ctrl_reg |= VIDINTCON0_INT_ENABLE;
  774. irq_ctrl_reg |= VIDINTCON0_INT_FRAME;
  775. irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL0_MASK;
  776. irq_ctrl_reg |= VIDINTCON0_FRAMESEL0_VSYNC;
  777. irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL1_MASK;
  778. irq_ctrl_reg |= VIDINTCON0_FRAMESEL1_NONE;
  779. writel(irq_ctrl_reg, regs + VIDINTCON0);
  780. }
  781. }
  782. /**
  783. * s3c_fb_disable_irq() - disable framebuffer interrupts
  784. * @sfb: main hardware state
  785. */
  786. static void s3c_fb_disable_irq(struct s3c_fb *sfb)
  787. {
  788. void __iomem *regs = sfb->regs;
  789. u32 irq_ctrl_reg;
  790. if (test_and_clear_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
  791. /* IRQ enabled, disable it */
  792. irq_ctrl_reg = readl(regs + VIDINTCON0);
  793. irq_ctrl_reg &= ~VIDINTCON0_INT_FRAME;
  794. irq_ctrl_reg &= ~VIDINTCON0_INT_ENABLE;
  795. writel(irq_ctrl_reg, regs + VIDINTCON0);
  796. }
  797. }
  798. static irqreturn_t s3c_fb_irq(int irq, void *dev_id)
  799. {
  800. struct s3c_fb *sfb = dev_id;
  801. void __iomem *regs = sfb->regs;
  802. u32 irq_sts_reg;
  803. irq_sts_reg = readl(regs + VIDINTCON1);
  804. if (irq_sts_reg & VIDINTCON1_INT_FRAME) {
  805. /* VSYNC interrupt, accept it */
  806. writel(VIDINTCON1_INT_FRAME, regs + VIDINTCON1);
  807. sfb->vsync_info.count++;
  808. wake_up_interruptible(&sfb->vsync_info.wait);
  809. }
  810. /* We only support waiting for VSYNC for now, so it's safe
  811. * to always disable irqs here.
  812. */
  813. s3c_fb_disable_irq(sfb);
  814. return IRQ_HANDLED;
  815. }
  816. /**
  817. * s3c_fb_wait_for_vsync() - sleep until next VSYNC interrupt or timeout
  818. * @sfb: main hardware state
  819. * @crtc: head index.
  820. */
  821. static int s3c_fb_wait_for_vsync(struct s3c_fb *sfb, u32 crtc)
  822. {
  823. unsigned long count;
  824. int ret;
  825. if (crtc != 0)
  826. return -ENODEV;
  827. count = sfb->vsync_info.count;
  828. s3c_fb_enable_irq(sfb);
  829. ret = wait_event_interruptible_timeout(sfb->vsync_info.wait,
  830. count != sfb->vsync_info.count,
  831. msecs_to_jiffies(VSYNC_TIMEOUT_MSEC));
  832. if (ret == 0)
  833. return -ETIMEDOUT;
  834. return 0;
  835. }
  836. static int s3c_fb_ioctl(struct fb_info *info, unsigned int cmd,
  837. unsigned long arg)
  838. {
  839. struct s3c_fb_win *win = info->par;
  840. struct s3c_fb *sfb = win->parent;
  841. int ret;
  842. u32 crtc;
  843. switch (cmd) {
  844. case FBIO_WAITFORVSYNC:
  845. if (get_user(crtc, (u32 __user *)arg)) {
  846. ret = -EFAULT;
  847. break;
  848. }
  849. ret = s3c_fb_wait_for_vsync(sfb, crtc);
  850. break;
  851. default:
  852. ret = -ENOTTY;
  853. }
  854. return ret;
  855. }
  856. static struct fb_ops s3c_fb_ops = {
  857. .owner = THIS_MODULE,
  858. .fb_check_var = s3c_fb_check_var,
  859. .fb_set_par = s3c_fb_set_par,
  860. .fb_blank = s3c_fb_blank,
  861. .fb_setcolreg = s3c_fb_setcolreg,
  862. .fb_fillrect = cfb_fillrect,
  863. .fb_copyarea = cfb_copyarea,
  864. .fb_imageblit = cfb_imageblit,
  865. .fb_pan_display = s3c_fb_pan_display,
  866. .fb_ioctl = s3c_fb_ioctl,
  867. };
  868. /**
  869. * s3c_fb_missing_pixclock() - calculates pixel clock
  870. * @mode: The video mode to change.
  871. *
  872. * Calculate the pixel clock when none has been given through platform data.
  873. */
  874. static void __devinit s3c_fb_missing_pixclock(struct fb_videomode *mode)
  875. {
  876. u64 pixclk = 1000000000000ULL;
  877. u32 div;
  878. div = mode->left_margin + mode->hsync_len + mode->right_margin +
  879. mode->xres;
  880. div *= mode->upper_margin + mode->vsync_len + mode->lower_margin +
  881. mode->yres;
  882. div *= mode->refresh ? : 60;
  883. do_div(pixclk, div);
  884. mode->pixclock = pixclk;
  885. }
  886. /**
  887. * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
  888. * @sfb: The base resources for the hardware.
  889. * @win: The window to initialise memory for.
  890. *
  891. * Allocate memory for the given framebuffer.
  892. */
  893. static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
  894. struct s3c_fb_win *win)
  895. {
  896. struct s3c_fb_pd_win *windata = win->windata;
  897. unsigned int real_size, virt_size, size;
  898. struct fb_info *fbi = win->fbinfo;
  899. dma_addr_t map_dma;
  900. dev_dbg(sfb->dev, "allocating memory for display\n");
  901. real_size = windata->win_mode.xres * windata->win_mode.yres;
  902. virt_size = windata->virtual_x * windata->virtual_y;
  903. dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
  904. real_size, windata->win_mode.xres, windata->win_mode.yres,
  905. virt_size, windata->virtual_x, windata->virtual_y);
  906. size = (real_size > virt_size) ? real_size : virt_size;
  907. size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
  908. size /= 8;
  909. fbi->fix.smem_len = size;
  910. size = PAGE_ALIGN(size);
  911. dev_dbg(sfb->dev, "want %u bytes for window\n", size);
  912. fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
  913. &map_dma, GFP_KERNEL);
  914. if (!fbi->screen_base)
  915. return -ENOMEM;
  916. dev_dbg(sfb->dev, "mapped %x to %p\n",
  917. (unsigned int)map_dma, fbi->screen_base);
  918. memset(fbi->screen_base, 0x0, size);
  919. fbi->fix.smem_start = map_dma;
  920. return 0;
  921. }
  922. /**
  923. * s3c_fb_free_memory() - free the display memory for the given window
  924. * @sfb: The base resources for the hardware.
  925. * @win: The window to free the display memory for.
  926. *
  927. * Free the display memory allocated by s3c_fb_alloc_memory().
  928. */
  929. static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
  930. {
  931. struct fb_info *fbi = win->fbinfo;
  932. if (fbi->screen_base)
  933. dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
  934. fbi->screen_base, fbi->fix.smem_start);
  935. }
  936. /**
  937. * s3c_fb_release_win() - release resources for a framebuffer window.
  938. * @win: The window to cleanup the resources for.
  939. *
  940. * Release the resources that where claimed for the hardware window,
  941. * such as the framebuffer instance and any memory claimed for it.
  942. */
  943. static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
  944. {
  945. u32 data;
  946. if (win->fbinfo) {
  947. if (sfb->variant.has_shadowcon) {
  948. data = readl(sfb->regs + SHADOWCON);
  949. data &= ~SHADOWCON_CHx_ENABLE(win->index);
  950. data &= ~SHADOWCON_CHx_LOCAL_ENABLE(win->index);
  951. writel(data, sfb->regs + SHADOWCON);
  952. }
  953. unregister_framebuffer(win->fbinfo);
  954. if (win->fbinfo->cmap.len)
  955. fb_dealloc_cmap(&win->fbinfo->cmap);
  956. s3c_fb_free_memory(sfb, win);
  957. framebuffer_release(win->fbinfo);
  958. }
  959. }
  960. /**
  961. * s3c_fb_probe_win() - register an hardware window
  962. * @sfb: The base resources for the hardware
  963. * @variant: The variant information for this window.
  964. * @res: Pointer to where to place the resultant window.
  965. *
  966. * Allocate and do the basic initialisation for one of the hardware's graphics
  967. * windows.
  968. */
  969. static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
  970. struct s3c_fb_win_variant *variant,
  971. struct s3c_fb_win **res)
  972. {
  973. struct fb_var_screeninfo *var;
  974. struct fb_videomode *initmode;
  975. struct s3c_fb_pd_win *windata;
  976. struct s3c_fb_win *win;
  977. struct fb_info *fbinfo;
  978. int palette_size;
  979. int ret;
  980. dev_dbg(sfb->dev, "probing window %d, variant %p\n", win_no, variant);
  981. init_waitqueue_head(&sfb->vsync_info.wait);
  982. palette_size = variant->palette_sz * 4;
  983. fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
  984. palette_size * sizeof(u32), sfb->dev);
  985. if (!fbinfo) {
  986. dev_err(sfb->dev, "failed to allocate framebuffer\n");
  987. return -ENOENT;
  988. }
  989. windata = sfb->pdata->win[win_no];
  990. initmode = &windata->win_mode;
  991. WARN_ON(windata->max_bpp == 0);
  992. WARN_ON(windata->win_mode.xres == 0);
  993. WARN_ON(windata->win_mode.yres == 0);
  994. win = fbinfo->par;
  995. *res = win;
  996. var = &fbinfo->var;
  997. win->variant = *variant;
  998. win->fbinfo = fbinfo;
  999. win->parent = sfb;
  1000. win->windata = windata;
  1001. win->index = win_no;
  1002. win->palette_buffer = (u32 *)(win + 1);
  1003. ret = s3c_fb_alloc_memory(sfb, win);
  1004. if (ret) {
  1005. dev_err(sfb->dev, "failed to allocate display memory\n");
  1006. return ret;
  1007. }
  1008. /* setup the r/b/g positions for the window's palette */
  1009. if (win->variant.palette_16bpp) {
  1010. /* Set RGB 5:6:5 as default */
  1011. win->palette.r.offset = 11;
  1012. win->palette.r.length = 5;
  1013. win->palette.g.offset = 5;
  1014. win->palette.g.length = 6;
  1015. win->palette.b.offset = 0;
  1016. win->palette.b.length = 5;
  1017. } else {
  1018. /* Set 8bpp or 8bpp and 1bit alpha */
  1019. win->palette.r.offset = 16;
  1020. win->palette.r.length = 8;
  1021. win->palette.g.offset = 8;
  1022. win->palette.g.length = 8;
  1023. win->palette.b.offset = 0;
  1024. win->palette.b.length = 8;
  1025. }
  1026. /* setup the initial video mode from the window */
  1027. fb_videomode_to_var(&fbinfo->var, initmode);
  1028. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  1029. fbinfo->fix.accel = FB_ACCEL_NONE;
  1030. fbinfo->var.activate = FB_ACTIVATE_NOW;
  1031. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  1032. fbinfo->var.bits_per_pixel = windata->default_bpp;
  1033. fbinfo->fbops = &s3c_fb_ops;
  1034. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  1035. fbinfo->pseudo_palette = &win->pseudo_palette;
  1036. /* prepare to actually start the framebuffer */
  1037. ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
  1038. if (ret < 0) {
  1039. dev_err(sfb->dev, "check_var failed on initial video params\n");
  1040. return ret;
  1041. }
  1042. /* create initial colour map */
  1043. ret = fb_alloc_cmap(&fbinfo->cmap, win->variant.palette_sz, 1);
  1044. if (ret == 0)
  1045. fb_set_cmap(&fbinfo->cmap, fbinfo);
  1046. else
  1047. dev_err(sfb->dev, "failed to allocate fb cmap\n");
  1048. s3c_fb_set_par(fbinfo);
  1049. dev_dbg(sfb->dev, "about to register framebuffer\n");
  1050. /* run the check_var and set_par on our configuration. */
  1051. ret = register_framebuffer(fbinfo);
  1052. if (ret < 0) {
  1053. dev_err(sfb->dev, "failed to register framebuffer\n");
  1054. return ret;
  1055. }
  1056. dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
  1057. return 0;
  1058. }
  1059. /**
  1060. * s3c_fb_clear_win() - clear hardware window registers.
  1061. * @sfb: The base resources for the hardware.
  1062. * @win: The window to process.
  1063. *
  1064. * Reset the specific window registers to a known state.
  1065. */
  1066. static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
  1067. {
  1068. void __iomem *regs = sfb->regs;
  1069. u32 reg;
  1070. writel(0, regs + sfb->variant.wincon + (win * 4));
  1071. writel(0, regs + VIDOSD_A(win, sfb->variant));
  1072. writel(0, regs + VIDOSD_B(win, sfb->variant));
  1073. writel(0, regs + VIDOSD_C(win, sfb->variant));
  1074. reg = readl(regs + SHADOWCON);
  1075. writel(reg & ~SHADOWCON_WINx_PROTECT(win), regs + SHADOWCON);
  1076. }
  1077. static int __devinit s3c_fb_probe(struct platform_device *pdev)
  1078. {
  1079. struct s3c_fb_driverdata *fbdrv;
  1080. struct device *dev = &pdev->dev;
  1081. struct s3c_fb_platdata *pd;
  1082. struct s3c_fb *sfb;
  1083. struct resource *res;
  1084. int win;
  1085. int ret = 0;
  1086. fbdrv = (struct s3c_fb_driverdata *)platform_get_device_id(pdev)->driver_data;
  1087. if (fbdrv->variant.nr_windows > S3C_FB_MAX_WIN) {
  1088. dev_err(dev, "too many windows, cannot attach\n");
  1089. return -EINVAL;
  1090. }
  1091. pd = pdev->dev.platform_data;
  1092. if (!pd) {
  1093. dev_err(dev, "no platform data specified\n");
  1094. return -EINVAL;
  1095. }
  1096. sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
  1097. if (!sfb) {
  1098. dev_err(dev, "no memory for framebuffers\n");
  1099. return -ENOMEM;
  1100. }
  1101. dev_dbg(dev, "allocate new framebuffer %p\n", sfb);
  1102. sfb->dev = dev;
  1103. sfb->pdata = pd;
  1104. sfb->variant = fbdrv->variant;
  1105. sfb->bus_clk = clk_get(dev, "lcd");
  1106. if (IS_ERR(sfb->bus_clk)) {
  1107. dev_err(dev, "failed to get bus clock\n");
  1108. goto err_sfb;
  1109. }
  1110. clk_enable(sfb->bus_clk);
  1111. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1112. if (!res) {
  1113. dev_err(dev, "failed to find registers\n");
  1114. ret = -ENOENT;
  1115. goto err_clk;
  1116. }
  1117. sfb->regs_res = request_mem_region(res->start, resource_size(res),
  1118. dev_name(dev));
  1119. if (!sfb->regs_res) {
  1120. dev_err(dev, "failed to claim register region\n");
  1121. ret = -ENOENT;
  1122. goto err_clk;
  1123. }
  1124. sfb->regs = ioremap(res->start, resource_size(res));
  1125. if (!sfb->regs) {
  1126. dev_err(dev, "failed to map registers\n");
  1127. ret = -ENXIO;
  1128. goto err_req_region;
  1129. }
  1130. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1131. if (!res) {
  1132. dev_err(dev, "failed to acquire irq resource\n");
  1133. ret = -ENOENT;
  1134. goto err_ioremap;
  1135. }
  1136. sfb->irq_no = res->start;
  1137. ret = request_irq(sfb->irq_no, s3c_fb_irq,
  1138. 0, "s3c_fb", sfb);
  1139. if (ret) {
  1140. dev_err(dev, "irq request failed\n");
  1141. goto err_ioremap;
  1142. }
  1143. dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
  1144. /* setup gpio and output polarity controls */
  1145. pd->setup_gpio();
  1146. writel(pd->vidcon1, sfb->regs + VIDCON1);
  1147. /* zero all windows before we do anything */
  1148. for (win = 0; win < fbdrv->variant.nr_windows; win++)
  1149. s3c_fb_clear_win(sfb, win);
  1150. /* initialise colour key controls */
  1151. for (win = 0; win < (fbdrv->variant.nr_windows - 1); win++) {
  1152. void __iomem *regs = sfb->regs + sfb->variant.keycon;
  1153. regs += (win * 8);
  1154. writel(0xffffff, regs + WKEYCON0);
  1155. writel(0xffffff, regs + WKEYCON1);
  1156. }
  1157. /* we have the register setup, start allocating framebuffers */
  1158. for (win = 0; win < fbdrv->variant.nr_windows; win++) {
  1159. if (!pd->win[win])
  1160. continue;
  1161. if (!pd->win[win]->win_mode.pixclock)
  1162. s3c_fb_missing_pixclock(&pd->win[win]->win_mode);
  1163. ret = s3c_fb_probe_win(sfb, win, fbdrv->win[win],
  1164. &sfb->windows[win]);
  1165. if (ret < 0) {
  1166. dev_err(dev, "failed to create window %d\n", win);
  1167. for (; win >= 0; win--)
  1168. s3c_fb_release_win(sfb, sfb->windows[win]);
  1169. goto err_irq;
  1170. }
  1171. }
  1172. platform_set_drvdata(pdev, sfb);
  1173. return 0;
  1174. err_irq:
  1175. free_irq(sfb->irq_no, sfb);
  1176. err_ioremap:
  1177. iounmap(sfb->regs);
  1178. err_req_region:
  1179. release_resource(sfb->regs_res);
  1180. kfree(sfb->regs_res);
  1181. err_clk:
  1182. clk_disable(sfb->bus_clk);
  1183. clk_put(sfb->bus_clk);
  1184. err_sfb:
  1185. kfree(sfb);
  1186. return ret;
  1187. }
  1188. /**
  1189. * s3c_fb_remove() - Cleanup on module finalisation
  1190. * @pdev: The platform device we are bound to.
  1191. *
  1192. * Shutdown and then release all the resources that the driver allocated
  1193. * on initialisation.
  1194. */
  1195. static int __devexit s3c_fb_remove(struct platform_device *pdev)
  1196. {
  1197. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1198. int win;
  1199. for (win = 0; win < S3C_FB_MAX_WIN; win++)
  1200. if (sfb->windows[win])
  1201. s3c_fb_release_win(sfb, sfb->windows[win]);
  1202. free_irq(sfb->irq_no, sfb);
  1203. iounmap(sfb->regs);
  1204. clk_disable(sfb->bus_clk);
  1205. clk_put(sfb->bus_clk);
  1206. release_resource(sfb->regs_res);
  1207. kfree(sfb->regs_res);
  1208. kfree(sfb);
  1209. return 0;
  1210. }
  1211. #ifdef CONFIG_PM
  1212. static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
  1213. {
  1214. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1215. struct s3c_fb_win *win;
  1216. int win_no;
  1217. for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
  1218. win = sfb->windows[win_no];
  1219. if (!win)
  1220. continue;
  1221. /* use the blank function to push into power-down */
  1222. s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
  1223. }
  1224. clk_disable(sfb->bus_clk);
  1225. return 0;
  1226. }
  1227. static int s3c_fb_resume(struct platform_device *pdev)
  1228. {
  1229. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  1230. struct s3c_fb_platdata *pd = sfb->pdata;
  1231. struct s3c_fb_win *win;
  1232. int win_no;
  1233. clk_enable(sfb->bus_clk);
  1234. /* setup registers */
  1235. writel(pd->vidcon1, sfb->regs + VIDCON1);
  1236. /* zero all windows before we do anything */
  1237. for (win_no = 0; win_no < sfb->variant.nr_windows; win_no++)
  1238. s3c_fb_clear_win(sfb, win_no);
  1239. for (win_no = 0; win_no < sfb->variant.nr_windows - 1; win_no++) {
  1240. void __iomem *regs = sfb->regs + sfb->variant.keycon;
  1241. regs += (win_no * 8);
  1242. writel(0xffffff, regs + WKEYCON0);
  1243. writel(0xffffff, regs + WKEYCON1);
  1244. }
  1245. /* restore framebuffers */
  1246. for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
  1247. win = sfb->windows[win_no];
  1248. if (!win)
  1249. continue;
  1250. dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
  1251. s3c_fb_set_par(win->fbinfo);
  1252. }
  1253. return 0;
  1254. }
  1255. #else
  1256. #define s3c_fb_suspend NULL
  1257. #define s3c_fb_resume NULL
  1258. #endif
  1259. #define VALID_BPP124 (VALID_BPP(1) | VALID_BPP(2) | VALID_BPP(4))
  1260. #define VALID_BPP1248 (VALID_BPP124 | VALID_BPP(8))
  1261. static struct s3c_fb_win_variant s3c_fb_data_64xx_wins[] = {
  1262. [0] = {
  1263. .has_osd_c = 1,
  1264. .osd_size_off = 0x8,
  1265. .palette_sz = 256,
  1266. .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
  1267. },
  1268. [1] = {
  1269. .has_osd_c = 1,
  1270. .has_osd_d = 1,
  1271. .osd_size_off = 0x12,
  1272. .has_osd_alpha = 1,
  1273. .palette_sz = 256,
  1274. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1275. VALID_BPP(18) | VALID_BPP(19) |
  1276. VALID_BPP(24) | VALID_BPP(25)),
  1277. },
  1278. [2] = {
  1279. .has_osd_c = 1,
  1280. .has_osd_d = 1,
  1281. .osd_size_off = 0x12,
  1282. .has_osd_alpha = 1,
  1283. .palette_sz = 16,
  1284. .palette_16bpp = 1,
  1285. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1286. VALID_BPP(18) | VALID_BPP(19) |
  1287. VALID_BPP(24) | VALID_BPP(25)),
  1288. },
  1289. [3] = {
  1290. .has_osd_c = 1,
  1291. .has_osd_alpha = 1,
  1292. .palette_sz = 16,
  1293. .palette_16bpp = 1,
  1294. .valid_bpp = (VALID_BPP124 | VALID_BPP(16) |
  1295. VALID_BPP(18) | VALID_BPP(19) |
  1296. VALID_BPP(24) | VALID_BPP(25)),
  1297. },
  1298. [4] = {
  1299. .has_osd_c = 1,
  1300. .has_osd_alpha = 1,
  1301. .palette_sz = 4,
  1302. .palette_16bpp = 1,
  1303. .valid_bpp = (VALID_BPP(1) | VALID_BPP(2) |
  1304. VALID_BPP(16) | VALID_BPP(18) |
  1305. VALID_BPP(24) | VALID_BPP(25)),
  1306. },
  1307. };
  1308. static struct s3c_fb_driverdata s3c_fb_data_64xx = {
  1309. .variant = {
  1310. .nr_windows = 5,
  1311. .vidtcon = VIDTCON0,
  1312. .wincon = WINCON(0),
  1313. .winmap = WINxMAP(0),
  1314. .keycon = WKEYCON,
  1315. .osd = VIDOSD_BASE,
  1316. .osd_stride = 16,
  1317. .buf_start = VIDW_BUF_START(0),
  1318. .buf_size = VIDW_BUF_SIZE(0),
  1319. .buf_end = VIDW_BUF_END(0),
  1320. .palette = {
  1321. [0] = 0x400,
  1322. [1] = 0x800,
  1323. [2] = 0x300,
  1324. [3] = 0x320,
  1325. [4] = 0x340,
  1326. },
  1327. .has_prtcon = 1,
  1328. },
  1329. .win[0] = &s3c_fb_data_64xx_wins[0],
  1330. .win[1] = &s3c_fb_data_64xx_wins[1],
  1331. .win[2] = &s3c_fb_data_64xx_wins[2],
  1332. .win[3] = &s3c_fb_data_64xx_wins[3],
  1333. .win[4] = &s3c_fb_data_64xx_wins[4],
  1334. };
  1335. static struct s3c_fb_driverdata s3c_fb_data_s5pc100 = {
  1336. .variant = {
  1337. .nr_windows = 5,
  1338. .vidtcon = VIDTCON0,
  1339. .wincon = WINCON(0),
  1340. .winmap = WINxMAP(0),
  1341. .keycon = WKEYCON,
  1342. .osd = VIDOSD_BASE,
  1343. .osd_stride = 16,
  1344. .buf_start = VIDW_BUF_START(0),
  1345. .buf_size = VIDW_BUF_SIZE(0),
  1346. .buf_end = VIDW_BUF_END(0),
  1347. .palette = {
  1348. [0] = 0x2400,
  1349. [1] = 0x2800,
  1350. [2] = 0x2c00,
  1351. [3] = 0x3000,
  1352. [4] = 0x3400,
  1353. },
  1354. .has_prtcon = 1,
  1355. },
  1356. .win[0] = &s3c_fb_data_64xx_wins[0],
  1357. .win[1] = &s3c_fb_data_64xx_wins[1],
  1358. .win[2] = &s3c_fb_data_64xx_wins[2],
  1359. .win[3] = &s3c_fb_data_64xx_wins[3],
  1360. .win[4] = &s3c_fb_data_64xx_wins[4],
  1361. };
  1362. static struct s3c_fb_driverdata s3c_fb_data_s5pv210 = {
  1363. .variant = {
  1364. .nr_windows = 5,
  1365. .vidtcon = VIDTCON0,
  1366. .wincon = WINCON(0),
  1367. .winmap = WINxMAP(0),
  1368. .keycon = WKEYCON,
  1369. .osd = VIDOSD_BASE,
  1370. .osd_stride = 16,
  1371. .buf_start = VIDW_BUF_START(0),
  1372. .buf_size = VIDW_BUF_SIZE(0),
  1373. .buf_end = VIDW_BUF_END(0),
  1374. .palette = {
  1375. [0] = 0x2400,
  1376. [1] = 0x2800,
  1377. [2] = 0x2c00,
  1378. [3] = 0x3000,
  1379. [4] = 0x3400,
  1380. },
  1381. .has_shadowcon = 1,
  1382. },
  1383. .win[0] = &s3c_fb_data_64xx_wins[0],
  1384. .win[1] = &s3c_fb_data_64xx_wins[1],
  1385. .win[2] = &s3c_fb_data_64xx_wins[2],
  1386. .win[3] = &s3c_fb_data_64xx_wins[3],
  1387. .win[4] = &s3c_fb_data_64xx_wins[4],
  1388. };
  1389. /* S3C2443/S3C2416 style hardware */
  1390. static struct s3c_fb_driverdata s3c_fb_data_s3c2443 = {
  1391. .variant = {
  1392. .nr_windows = 2,
  1393. .is_2443 = 1,
  1394. .vidtcon = 0x08,
  1395. .wincon = 0x14,
  1396. .winmap = 0xd0,
  1397. .keycon = 0xb0,
  1398. .osd = 0x28,
  1399. .osd_stride = 12,
  1400. .buf_start = 0x64,
  1401. .buf_size = 0x94,
  1402. .buf_end = 0x7c,
  1403. .palette = {
  1404. [0] = 0x400,
  1405. [1] = 0x800,
  1406. },
  1407. },
  1408. .win[0] = &(struct s3c_fb_win_variant) {
  1409. .palette_sz = 256,
  1410. .valid_bpp = VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
  1411. },
  1412. .win[1] = &(struct s3c_fb_win_variant) {
  1413. .has_osd_c = 1,
  1414. .has_osd_alpha = 1,
  1415. .palette_sz = 256,
  1416. .valid_bpp = (VALID_BPP1248 | VALID_BPP(16) |
  1417. VALID_BPP(18) | VALID_BPP(19) |
  1418. VALID_BPP(24) | VALID_BPP(25) |
  1419. VALID_BPP(28)),
  1420. },
  1421. };
  1422. static struct platform_device_id s3c_fb_driver_ids[] = {
  1423. {
  1424. .name = "s3c-fb",
  1425. .driver_data = (unsigned long)&s3c_fb_data_64xx,
  1426. }, {
  1427. .name = "s5pc100-fb",
  1428. .driver_data = (unsigned long)&s3c_fb_data_s5pc100,
  1429. }, {
  1430. .name = "s5pv210-fb",
  1431. .driver_data = (unsigned long)&s3c_fb_data_s5pv210,
  1432. }, {
  1433. .name = "s3c2443-fb",
  1434. .driver_data = (unsigned long)&s3c_fb_data_s3c2443,
  1435. },
  1436. {},
  1437. };
  1438. MODULE_DEVICE_TABLE(platform, s3c_fb_driver_ids);
  1439. static struct platform_driver s3c_fb_driver = {
  1440. .probe = s3c_fb_probe,
  1441. .remove = __devexit_p(s3c_fb_remove),
  1442. .suspend = s3c_fb_suspend,
  1443. .resume = s3c_fb_resume,
  1444. .id_table = s3c_fb_driver_ids,
  1445. .driver = {
  1446. .name = "s3c-fb",
  1447. .owner = THIS_MODULE,
  1448. },
  1449. };
  1450. static int __init s3c_fb_init(void)
  1451. {
  1452. return platform_driver_register(&s3c_fb_driver);
  1453. }
  1454. static void __exit s3c_fb_cleanup(void)
  1455. {
  1456. platform_driver_unregister(&s3c_fb_driver);
  1457. }
  1458. module_init(s3c_fb_init);
  1459. module_exit(s3c_fb_cleanup);
  1460. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1461. MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
  1462. MODULE_LICENSE("GPL");
  1463. MODULE_ALIAS("platform:s3c-fb");