s3c-fb.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /* linux/drivers/video/s3c-fb.c
  2. *
  3. * Copyright 2008 Openmoko Inc.
  4. * Copyright 2008 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 <mach/map.h>
  24. #include <mach/regs-fb.h>
  25. #include <plat/fb.h>
  26. /* This driver will export a number of framebuffer interfaces depending
  27. * on the configuration passed in via the platform data. Each fb instance
  28. * maps to a hardware window. Currently there is no support for runtime
  29. * setting of the alpha-blending functions that each window has, so only
  30. * window 0 is actually useful.
  31. *
  32. * Window 0 is treated specially, it is used for the basis of the LCD
  33. * output timings and as the control for the output power-down state.
  34. */
  35. /* note, some of the functions that get called are derived from including
  36. * <mach/regs-fb.h> as they are specific to the architecture that the code
  37. * is being built for.
  38. */
  39. #ifdef CONFIG_FB_S3C_DEBUG_REGWRITE
  40. #undef writel
  41. #define writel(v, r) do { \
  42. printk(KERN_DEBUG "%s: %08x => %p\n", __func__, (unsigned int)v, r); \
  43. __raw_writel(v, r); } while(0)
  44. #endif /* FB_S3C_DEBUG_REGWRITE */
  45. struct s3c_fb;
  46. /**
  47. * struct s3c_fb_win - per window private data for each framebuffer.
  48. * @windata: The platform data supplied for the window configuration.
  49. * @parent: The hardware that this window is part of.
  50. * @fbinfo: Pointer pack to the framebuffer info for this window.
  51. * @palette_buffer: Buffer/cache to hold palette entries.
  52. * @pseudo_palette: For use in TRUECOLOUR modes for entries 0..15/
  53. * @index: The window number of this window.
  54. * @palette: The bitfields for changing r/g/b into a hardware palette entry.
  55. */
  56. struct s3c_fb_win {
  57. struct s3c_fb_pd_win *windata;
  58. struct s3c_fb *parent;
  59. struct fb_info *fbinfo;
  60. struct s3c_fb_palette palette;
  61. u32 *palette_buffer;
  62. u32 pseudo_palette[16];
  63. unsigned int index;
  64. };
  65. /**
  66. * struct s3c_fb - overall hardware state of the hardware
  67. * @dev: The device that we bound to, for printing, etc.
  68. * @regs_res: The resource we claimed for the IO registers.
  69. * @bus_clk: The clk (hclk) feeding our interface and possibly pixclk.
  70. * @regs: The mapped hardware registers.
  71. * @enabled: A bitmask of enabled hardware windows.
  72. * @pdata: The platform configuration data passed with the device.
  73. * @windows: The hardware windows that have been claimed.
  74. */
  75. struct s3c_fb {
  76. struct device *dev;
  77. struct resource *regs_res;
  78. struct clk *bus_clk;
  79. void __iomem *regs;
  80. unsigned char enabled;
  81. struct s3c_fb_platdata *pdata;
  82. struct s3c_fb_win *windows[S3C_FB_MAX_WIN];
  83. };
  84. /**
  85. * s3c_fb_win_has_palette() - determine if a mode has a palette
  86. * @win: The window number being queried.
  87. * @bpp: The number of bits per pixel to test.
  88. *
  89. * Work out if the given window supports palletised data at the specified bpp.
  90. */
  91. static int s3c_fb_win_has_palette(unsigned int win, unsigned int bpp)
  92. {
  93. return s3c_fb_win_pal_size(win) <= (1 << bpp);
  94. }
  95. /**
  96. * s3c_fb_check_var() - framebuffer layer request to verify a given mode.
  97. * @var: The screen information to verify.
  98. * @info: The framebuffer device.
  99. *
  100. * Framebuffer layer call to verify the given information and allow us to
  101. * update various information depending on the hardware capabilities.
  102. */
  103. static int s3c_fb_check_var(struct fb_var_screeninfo *var,
  104. struct fb_info *info)
  105. {
  106. struct s3c_fb_win *win = info->par;
  107. struct s3c_fb_pd_win *windata = win->windata;
  108. struct s3c_fb *sfb = win->parent;
  109. dev_dbg(sfb->dev, "checking parameters\n");
  110. var->xres_virtual = max((unsigned int)windata->virtual_x, var->xres);
  111. var->yres_virtual = max((unsigned int)windata->virtual_y, var->yres);
  112. if (!s3c_fb_validate_win_bpp(win->index, var->bits_per_pixel)) {
  113. dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
  114. win->index, var->bits_per_pixel);
  115. return -EINVAL;
  116. }
  117. /* always ensure these are zero, for drop through cases below */
  118. var->transp.offset = 0;
  119. var->transp.length = 0;
  120. switch (var->bits_per_pixel) {
  121. case 1:
  122. case 2:
  123. case 4:
  124. case 8:
  125. if (!s3c_fb_win_has_palette(win->index, var->bits_per_pixel)) {
  126. /* non palletised, A:1,R:2,G:3,B:2 mode */
  127. var->red.offset = 4;
  128. var->green.offset = 2;
  129. var->blue.offset = 0;
  130. var->red.length = 5;
  131. var->green.length = 3;
  132. var->blue.length = 2;
  133. var->transp.offset = 7;
  134. var->transp.length = 1;
  135. } else {
  136. var->red.offset = 0;
  137. var->red.length = var->bits_per_pixel;
  138. var->green = var->red;
  139. var->blue = var->red;
  140. }
  141. break;
  142. case 19:
  143. /* 666 with one bit alpha/transparency */
  144. var->transp.offset = 18;
  145. var->transp.length = 1;
  146. case 18:
  147. var->bits_per_pixel = 32;
  148. /* 666 format */
  149. var->red.offset = 12;
  150. var->green.offset = 6;
  151. var->blue.offset = 0;
  152. var->red.length = 6;
  153. var->green.length = 6;
  154. var->blue.length = 6;
  155. break;
  156. case 16:
  157. /* 16 bpp, 565 format */
  158. var->red.offset = 11;
  159. var->green.offset = 5;
  160. var->blue.offset = 0;
  161. var->red.length = 5;
  162. var->green.length = 6;
  163. var->blue.length = 5;
  164. break;
  165. case 28:
  166. case 25:
  167. var->transp.length = var->bits_per_pixel - 24;
  168. var->transp.offset = 24;
  169. /* drop through */
  170. case 24:
  171. /* our 24bpp is unpacked, so 32bpp */
  172. var->bits_per_pixel = 32;
  173. case 32:
  174. var->red.offset = 16;
  175. var->red.length = 8;
  176. var->green.offset = 8;
  177. var->green.length = 8;
  178. var->blue.offset = 0;
  179. var->blue.length = 8;
  180. break;
  181. default:
  182. dev_err(sfb->dev, "invalid bpp\n");
  183. }
  184. dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
  185. return 0;
  186. }
  187. /**
  188. * s3c_fb_calc_pixclk() - calculate the divider to create the pixel clock.
  189. * @sfb: The hardware state.
  190. * @pixclock: The pixel clock wanted, in picoseconds.
  191. *
  192. * Given the specified pixel clock, work out the necessary divider to get
  193. * close to the output frequency.
  194. */
  195. static int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
  196. {
  197. unsigned long clk = clk_get_rate(sfb->bus_clk);
  198. unsigned long long tmp;
  199. unsigned int result;
  200. tmp = (unsigned long long)clk;
  201. tmp *= pixclk;
  202. do_div(tmp, 1000000000UL);
  203. result = (unsigned int)tmp / 1000;
  204. dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
  205. pixclk, clk, result, clk / result);
  206. return result;
  207. }
  208. /**
  209. * s3c_fb_align_word() - align pixel count to word boundary
  210. * @bpp: The number of bits per pixel
  211. * @pix: The value to be aligned.
  212. *
  213. * Align the given pixel count so that it will start on an 32bit word
  214. * boundary.
  215. */
  216. static int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
  217. {
  218. int pix_per_word;
  219. if (bpp > 16)
  220. return pix;
  221. pix_per_word = (8 * 32) / bpp;
  222. return ALIGN(pix, pix_per_word);
  223. }
  224. /**
  225. * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
  226. * @info: The framebuffer to change.
  227. *
  228. * Framebuffer layer request to set a new mode for the specified framebuffer
  229. */
  230. static int s3c_fb_set_par(struct fb_info *info)
  231. {
  232. struct fb_var_screeninfo *var = &info->var;
  233. struct s3c_fb_win *win = info->par;
  234. struct s3c_fb *sfb = win->parent;
  235. void __iomem *regs = sfb->regs;
  236. int win_no = win->index;
  237. u32 osdc_data = 0;
  238. u32 data;
  239. u32 pagewidth;
  240. int clkdiv;
  241. dev_dbg(sfb->dev, "setting framebuffer parameters\n");
  242. switch (var->bits_per_pixel) {
  243. case 32:
  244. case 24:
  245. case 16:
  246. case 12:
  247. info->fix.visual = FB_VISUAL_TRUECOLOR;
  248. break;
  249. case 8:
  250. if (s3c_fb_win_has_palette(win_no, 8))
  251. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  252. else
  253. info->fix.visual = FB_VISUAL_TRUECOLOR;
  254. break;
  255. case 1:
  256. info->fix.visual = FB_VISUAL_MONO01;
  257. break;
  258. default:
  259. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  260. break;
  261. }
  262. info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
  263. /* disable the window whilst we update it */
  264. writel(0, regs + WINCON(win_no));
  265. /* use window 0 as the basis for the lcd output timings */
  266. if (win_no == 0) {
  267. clkdiv = s3c_fb_calc_pixclk(sfb, var->pixclock);
  268. data = sfb->pdata->vidcon0;
  269. data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
  270. if (clkdiv > 1)
  271. data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
  272. else
  273. data &= ~VIDCON0_CLKDIR; /* 1:1 clock */
  274. /* write the timing data to the panel */
  275. data |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  276. writel(data, regs + VIDCON0);
  277. data = VIDTCON0_VBPD(var->upper_margin - 1) |
  278. VIDTCON0_VFPD(var->lower_margin - 1) |
  279. VIDTCON0_VSPW(var->vsync_len - 1);
  280. writel(data, regs + VIDTCON0);
  281. data = VIDTCON1_HBPD(var->left_margin - 1) |
  282. VIDTCON1_HFPD(var->right_margin - 1) |
  283. VIDTCON1_HSPW(var->hsync_len - 1);
  284. writel(data, regs + VIDTCON1);
  285. data = VIDTCON2_LINEVAL(var->yres - 1) |
  286. VIDTCON2_HOZVAL(var->xres - 1);
  287. writel(data, regs + VIDTCON2);
  288. }
  289. /* write the buffer address */
  290. writel(info->fix.smem_start, regs + VIDW_BUF_START(win_no));
  291. data = info->fix.smem_start + info->fix.line_length * var->yres;
  292. writel(data, regs + VIDW_BUF_END(win_no));
  293. pagewidth = (var->xres * var->bits_per_pixel) >> 3;
  294. data = VIDW_BUF_SIZE_OFFSET(info->fix.line_length - pagewidth) |
  295. VIDW_BUF_SIZE_PAGEWIDTH(pagewidth);
  296. writel(data, regs + VIDW_BUF_SIZE(win_no));
  297. /* write 'OSD' registers to control position of framebuffer */
  298. data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0);
  299. writel(data, regs + VIDOSD_A(win_no));
  300. data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
  301. var->xres - 1)) |
  302. VIDOSDxB_BOTRIGHT_Y(var->yres - 1);
  303. writel(data, regs + VIDOSD_B(win_no));
  304. data = var->xres * var->yres;
  305. osdc_data = VIDISD14C_ALPHA1_R(0xf) |
  306. VIDISD14C_ALPHA1_G(0xf) |
  307. VIDISD14C_ALPHA1_B(0xf);
  308. if (s3c_fb_has_osd_d(win_no)) {
  309. writel(data, regs + VIDOSD_D(win_no));
  310. writel(osdc_data, regs + VIDOSD_C(win_no));
  311. } else
  312. writel(data, regs + VIDOSD_C(win_no));
  313. data = WINCONx_ENWIN;
  314. /* note, since we have to round up the bits-per-pixel, we end up
  315. * relying on the bitfield information for r/g/b/a to work out
  316. * exactly which mode of operation is intended. */
  317. switch (var->bits_per_pixel) {
  318. case 1:
  319. data |= WINCON0_BPPMODE_1BPP;
  320. data |= WINCONx_BITSWP;
  321. data |= WINCONx_BURSTLEN_4WORD;
  322. break;
  323. case 2:
  324. data |= WINCON0_BPPMODE_2BPP;
  325. data |= WINCONx_BITSWP;
  326. data |= WINCONx_BURSTLEN_8WORD;
  327. break;
  328. case 4:
  329. data |= WINCON0_BPPMODE_4BPP;
  330. data |= WINCONx_BITSWP;
  331. data |= WINCONx_BURSTLEN_8WORD;
  332. break;
  333. case 8:
  334. if (var->transp.length != 0)
  335. data |= WINCON1_BPPMODE_8BPP_1232;
  336. else
  337. data |= WINCON0_BPPMODE_8BPP_PALETTE;
  338. data |= WINCONx_BURSTLEN_8WORD;
  339. data |= WINCONx_BYTSWP;
  340. break;
  341. case 16:
  342. if (var->transp.length != 0)
  343. data |= WINCON1_BPPMODE_16BPP_A1555;
  344. else
  345. data |= WINCON0_BPPMODE_16BPP_565;
  346. data |= WINCONx_HAWSWP;
  347. data |= WINCONx_BURSTLEN_16WORD;
  348. break;
  349. case 24:
  350. case 32:
  351. if (var->red.length == 6) {
  352. if (var->transp.length != 0)
  353. data |= WINCON1_BPPMODE_19BPP_A1666;
  354. else
  355. data |= WINCON1_BPPMODE_18BPP_666;
  356. } else if (var->transp.length == 1)
  357. data |= WINCON1_BPPMODE_25BPP_A1888
  358. | WINCON1_BLD_PIX;
  359. else if (var->transp.length == 4)
  360. data |= WINCON1_BPPMODE_28BPP_A4888
  361. | WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
  362. else
  363. data |= WINCON0_BPPMODE_24BPP_888;
  364. data |= WINCONx_BURSTLEN_16WORD;
  365. break;
  366. }
  367. /* It has no color key control register for window0 */
  368. if (win_no > 0) {
  369. u32 keycon0_data = 0, keycon1_data = 0;
  370. keycon0_data = ~(WxKEYCON0_KEYBL_EN |
  371. WxKEYCON0_KEYEN_F |
  372. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  373. keycon1_data = WxKEYCON1_COLVAL(0xffffff);
  374. writel(keycon0_data, regs + WxKEYCONy(win_no-1, 0));
  375. writel(keycon1_data, regs + WxKEYCONy(win_no-1, 1));
  376. }
  377. writel(data, regs + WINCON(win_no));
  378. writel(0x0, regs + WINxMAP(win_no));
  379. return 0;
  380. }
  381. /**
  382. * s3c_fb_update_palette() - set or schedule a palette update.
  383. * @sfb: The hardware information.
  384. * @win: The window being updated.
  385. * @reg: The palette index being changed.
  386. * @value: The computed palette value.
  387. *
  388. * Change the value of a palette register, either by directly writing to
  389. * the palette (this requires the palette RAM to be disconnected from the
  390. * hardware whilst this is in progress) or schedule the update for later.
  391. *
  392. * At the moment, since we have no VSYNC interrupt support, we simply set
  393. * the palette entry directly.
  394. */
  395. static void s3c_fb_update_palette(struct s3c_fb *sfb,
  396. struct s3c_fb_win *win,
  397. unsigned int reg,
  398. u32 value)
  399. {
  400. void __iomem *palreg;
  401. u32 palcon;
  402. palreg = sfb->regs + s3c_fb_pal_reg(win->index, reg);
  403. dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
  404. __func__, win->index, reg, palreg, value);
  405. win->palette_buffer[reg] = value;
  406. palcon = readl(sfb->regs + WPALCON);
  407. writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
  408. if (s3c_fb_pal_is16(win->index))
  409. writew(value, palreg);
  410. else
  411. writel(value, palreg);
  412. writel(palcon, sfb->regs + WPALCON);
  413. }
  414. static inline unsigned int chan_to_field(unsigned int chan,
  415. struct fb_bitfield *bf)
  416. {
  417. chan &= 0xffff;
  418. chan >>= 16 - bf->length;
  419. return chan << bf->offset;
  420. }
  421. /**
  422. * s3c_fb_setcolreg() - framebuffer layer request to change palette.
  423. * @regno: The palette index to change.
  424. * @red: The red field for the palette data.
  425. * @green: The green field for the palette data.
  426. * @blue: The blue field for the palette data.
  427. * @trans: The transparency (alpha) field for the palette data.
  428. * @info: The framebuffer being changed.
  429. */
  430. static int s3c_fb_setcolreg(unsigned regno,
  431. unsigned red, unsigned green, unsigned blue,
  432. unsigned transp, struct fb_info *info)
  433. {
  434. struct s3c_fb_win *win = info->par;
  435. struct s3c_fb *sfb = win->parent;
  436. unsigned int val;
  437. dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
  438. __func__, win->index, regno, red, green, blue);
  439. switch (info->fix.visual) {
  440. case FB_VISUAL_TRUECOLOR:
  441. /* true-colour, use pseudo-palette */
  442. if (regno < 16) {
  443. u32 *pal = info->pseudo_palette;
  444. val = chan_to_field(red, &info->var.red);
  445. val |= chan_to_field(green, &info->var.green);
  446. val |= chan_to_field(blue, &info->var.blue);
  447. pal[regno] = val;
  448. }
  449. break;
  450. case FB_VISUAL_PSEUDOCOLOR:
  451. if (regno < s3c_fb_win_pal_size(win->index)) {
  452. val = chan_to_field(red, &win->palette.r);
  453. val |= chan_to_field(green, &win->palette.g);
  454. val |= chan_to_field(blue, &win->palette.b);
  455. s3c_fb_update_palette(sfb, win, regno, val);
  456. }
  457. break;
  458. default:
  459. return 1; /* unknown type */
  460. }
  461. return 0;
  462. }
  463. /**
  464. * s3c_fb_enable() - Set the state of the main LCD output
  465. * @sfb: The main framebuffer state.
  466. * @enable: The state to set.
  467. */
  468. static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
  469. {
  470. u32 vidcon0 = readl(sfb->regs + VIDCON0);
  471. if (enable)
  472. vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  473. else {
  474. /* see the note in the framebuffer datasheet about
  475. * why you cannot take both of these bits down at the
  476. * same time. */
  477. if (!(vidcon0 & VIDCON0_ENVID))
  478. return;
  479. vidcon0 |= VIDCON0_ENVID;
  480. vidcon0 &= ~VIDCON0_ENVID_F;
  481. }
  482. writel(vidcon0, sfb->regs + VIDCON0);
  483. }
  484. /**
  485. * s3c_fb_blank() - blank or unblank the given window
  486. * @blank_mode: The blank state from FB_BLANK_*
  487. * @info: The framebuffer to blank.
  488. *
  489. * Framebuffer layer request to change the power state.
  490. */
  491. static int s3c_fb_blank(int blank_mode, struct fb_info *info)
  492. {
  493. struct s3c_fb_win *win = info->par;
  494. struct s3c_fb *sfb = win->parent;
  495. unsigned int index = win->index;
  496. u32 wincon;
  497. dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
  498. wincon = readl(sfb->regs + WINCON(index));
  499. switch (blank_mode) {
  500. case FB_BLANK_POWERDOWN:
  501. wincon &= ~WINCONx_ENWIN;
  502. sfb->enabled &= ~(1 << index);
  503. /* fall through to FB_BLANK_NORMAL */
  504. case FB_BLANK_NORMAL:
  505. /* disable the DMA and display 0x0 (black) */
  506. writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
  507. sfb->regs + WINxMAP(index));
  508. break;
  509. case FB_BLANK_UNBLANK:
  510. writel(0x0, sfb->regs + WINxMAP(index));
  511. wincon |= WINCONx_ENWIN;
  512. sfb->enabled |= (1 << index);
  513. break;
  514. case FB_BLANK_VSYNC_SUSPEND:
  515. case FB_BLANK_HSYNC_SUSPEND:
  516. default:
  517. return 1;
  518. }
  519. writel(wincon, sfb->regs + WINCON(index));
  520. /* Check the enabled state to see if we need to be running the
  521. * main LCD interface, as if there are no active windows then
  522. * it is highly likely that we also do not need to output
  523. * anything.
  524. */
  525. /* We could do something like the following code, but the current
  526. * system of using framebuffer events means that we cannot make
  527. * the distinction between just window 0 being inactive and all
  528. * the windows being down.
  529. *
  530. * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
  531. */
  532. /* we're stuck with this until we can do something about overriding
  533. * the power control using the blanking event for a single fb.
  534. */
  535. if (index == 0)
  536. s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
  537. return 0;
  538. }
  539. static struct fb_ops s3c_fb_ops = {
  540. .owner = THIS_MODULE,
  541. .fb_check_var = s3c_fb_check_var,
  542. .fb_set_par = s3c_fb_set_par,
  543. .fb_blank = s3c_fb_blank,
  544. .fb_setcolreg = s3c_fb_setcolreg,
  545. .fb_fillrect = cfb_fillrect,
  546. .fb_copyarea = cfb_copyarea,
  547. .fb_imageblit = cfb_imageblit,
  548. };
  549. /**
  550. * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
  551. * @sfb: The base resources for the hardware.
  552. * @win: The window to initialise memory for.
  553. *
  554. * Allocate memory for the given framebuffer.
  555. */
  556. static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
  557. struct s3c_fb_win *win)
  558. {
  559. struct s3c_fb_pd_win *windata = win->windata;
  560. unsigned int real_size, virt_size, size;
  561. struct fb_info *fbi = win->fbinfo;
  562. dma_addr_t map_dma;
  563. dev_dbg(sfb->dev, "allocating memory for display\n");
  564. real_size = windata->win_mode.xres * windata->win_mode.yres;
  565. virt_size = windata->virtual_x * windata->virtual_y;
  566. dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
  567. real_size, windata->win_mode.xres, windata->win_mode.yres,
  568. virt_size, windata->virtual_x, windata->virtual_y);
  569. size = (real_size > virt_size) ? real_size : virt_size;
  570. size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
  571. size /= 8;
  572. fbi->fix.smem_len = size;
  573. size = PAGE_ALIGN(size);
  574. dev_dbg(sfb->dev, "want %u bytes for window\n", size);
  575. fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
  576. &map_dma, GFP_KERNEL);
  577. if (!fbi->screen_base)
  578. return -ENOMEM;
  579. dev_dbg(sfb->dev, "mapped %x to %p\n",
  580. (unsigned int)map_dma, fbi->screen_base);
  581. memset(fbi->screen_base, 0x0, size);
  582. fbi->fix.smem_start = map_dma;
  583. return 0;
  584. }
  585. /**
  586. * s3c_fb_free_memory() - free the display memory for the given window
  587. * @sfb: The base resources for the hardware.
  588. * @win: The window to free the display memory for.
  589. *
  590. * Free the display memory allocated by s3c_fb_alloc_memory().
  591. */
  592. static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
  593. {
  594. struct fb_info *fbi = win->fbinfo;
  595. dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
  596. fbi->screen_base, fbi->fix.smem_start);
  597. }
  598. /**
  599. * s3c_fb_release_win() - release resources for a framebuffer window.
  600. * @win: The window to cleanup the resources for.
  601. *
  602. * Release the resources that where claimed for the hardware window,
  603. * such as the framebuffer instance and any memory claimed for it.
  604. */
  605. static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
  606. {
  607. if (win->fbinfo) {
  608. unregister_framebuffer(win->fbinfo);
  609. fb_dealloc_cmap(&win->fbinfo->cmap);
  610. s3c_fb_free_memory(sfb, win);
  611. framebuffer_release(win->fbinfo);
  612. }
  613. }
  614. /**
  615. * s3c_fb_probe_win() - register an hardware window
  616. * @sfb: The base resources for the hardware
  617. * @res: Pointer to where to place the resultant window.
  618. *
  619. * Allocate and do the basic initialisation for one of the hardware's graphics
  620. * windows.
  621. */
  622. static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
  623. struct s3c_fb_win **res)
  624. {
  625. struct fb_var_screeninfo *var;
  626. struct fb_videomode *initmode;
  627. struct s3c_fb_pd_win *windata;
  628. struct s3c_fb_win *win;
  629. struct fb_info *fbinfo;
  630. int palette_size;
  631. int ret;
  632. dev_dbg(sfb->dev, "probing window %d\n", win_no);
  633. palette_size = s3c_fb_win_pal_size(win_no);
  634. fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
  635. palette_size * sizeof(u32), sfb->dev);
  636. if (!fbinfo) {
  637. dev_err(sfb->dev, "failed to allocate framebuffer\n");
  638. return -ENOENT;
  639. }
  640. windata = sfb->pdata->win[win_no];
  641. initmode = &windata->win_mode;
  642. WARN_ON(windata->max_bpp == 0);
  643. WARN_ON(windata->win_mode.xres == 0);
  644. WARN_ON(windata->win_mode.yres == 0);
  645. win = fbinfo->par;
  646. var = &fbinfo->var;
  647. win->fbinfo = fbinfo;
  648. win->parent = sfb;
  649. win->windata = windata;
  650. win->index = win_no;
  651. win->palette_buffer = (u32 *)(win + 1);
  652. ret = s3c_fb_alloc_memory(sfb, win);
  653. if (ret) {
  654. dev_err(sfb->dev, "failed to allocate display memory\n");
  655. return ret;
  656. }
  657. /* setup the r/b/g positions for the window's palette */
  658. s3c_fb_init_palette(win_no, &win->palette);
  659. /* setup the initial video mode from the window */
  660. fb_videomode_to_var(&fbinfo->var, initmode);
  661. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  662. fbinfo->fix.accel = FB_ACCEL_NONE;
  663. fbinfo->var.activate = FB_ACTIVATE_NOW;
  664. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  665. fbinfo->var.bits_per_pixel = windata->default_bpp;
  666. fbinfo->fbops = &s3c_fb_ops;
  667. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  668. fbinfo->pseudo_palette = &win->pseudo_palette;
  669. /* prepare to actually start the framebuffer */
  670. ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
  671. if (ret < 0) {
  672. dev_err(sfb->dev, "check_var failed on initial video params\n");
  673. return ret;
  674. }
  675. /* create initial colour map */
  676. ret = fb_alloc_cmap(&fbinfo->cmap, s3c_fb_win_pal_size(win_no), 1);
  677. if (ret == 0)
  678. fb_set_cmap(&fbinfo->cmap, fbinfo);
  679. else
  680. dev_err(sfb->dev, "failed to allocate fb cmap\n");
  681. s3c_fb_set_par(fbinfo);
  682. dev_dbg(sfb->dev, "about to register framebuffer\n");
  683. /* run the check_var and set_par on our configuration. */
  684. ret = register_framebuffer(fbinfo);
  685. if (ret < 0) {
  686. dev_err(sfb->dev, "failed to register framebuffer\n");
  687. return ret;
  688. }
  689. *res = win;
  690. dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
  691. return 0;
  692. }
  693. /**
  694. * s3c_fb_clear_win() - clear hardware window registers.
  695. * @sfb: The base resources for the hardware.
  696. * @win: The window to process.
  697. *
  698. * Reset the specific window registers to a known state.
  699. */
  700. static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
  701. {
  702. void __iomem *regs = sfb->regs;
  703. writel(0, regs + WINCON(win));
  704. writel(0xffffff, regs + WxKEYCONy(win, 0));
  705. writel(0xffffff, regs + WxKEYCONy(win, 1));
  706. writel(0, regs + VIDOSD_A(win));
  707. writel(0, regs + VIDOSD_B(win));
  708. writel(0, regs + VIDOSD_C(win));
  709. }
  710. static int __devinit s3c_fb_probe(struct platform_device *pdev)
  711. {
  712. struct device *dev = &pdev->dev;
  713. struct s3c_fb_platdata *pd;
  714. struct s3c_fb *sfb;
  715. struct resource *res;
  716. int win;
  717. int ret = 0;
  718. pd = pdev->dev.platform_data;
  719. if (!pd) {
  720. dev_err(dev, "no platform data specified\n");
  721. return -EINVAL;
  722. }
  723. sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
  724. if (!sfb) {
  725. dev_err(dev, "no memory for framebuffers\n");
  726. return -ENOMEM;
  727. }
  728. sfb->dev = dev;
  729. sfb->pdata = pd;
  730. sfb->bus_clk = clk_get(dev, "lcd");
  731. if (IS_ERR(sfb->bus_clk)) {
  732. dev_err(dev, "failed to get bus clock\n");
  733. goto err_sfb;
  734. }
  735. clk_enable(sfb->bus_clk);
  736. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  737. if (!res) {
  738. dev_err(dev, "failed to find registers\n");
  739. ret = -ENOENT;
  740. goto err_clk;
  741. }
  742. sfb->regs_res = request_mem_region(res->start, resource_size(res),
  743. dev_name(dev));
  744. if (!sfb->regs_res) {
  745. dev_err(dev, "failed to claim register region\n");
  746. ret = -ENOENT;
  747. goto err_clk;
  748. }
  749. sfb->regs = ioremap(res->start, resource_size(res));
  750. if (!sfb->regs) {
  751. dev_err(dev, "failed to map registers\n");
  752. ret = -ENXIO;
  753. goto err_req_region;
  754. }
  755. dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
  756. /* setup gpio and output polarity controls */
  757. pd->setup_gpio();
  758. writel(pd->vidcon1, sfb->regs + VIDCON1);
  759. /* zero all windows before we do anything */
  760. for (win = 0; win < S3C_FB_MAX_WIN; win++)
  761. s3c_fb_clear_win(sfb, win);
  762. /* we have the register setup, start allocating framebuffers */
  763. for (win = 0; win < S3C_FB_MAX_WIN; win++) {
  764. if (!pd->win[win])
  765. continue;
  766. ret = s3c_fb_probe_win(sfb, win, &sfb->windows[win]);
  767. if (ret < 0) {
  768. dev_err(dev, "failed to create window %d\n", win);
  769. for (; win >= 0; win--)
  770. s3c_fb_release_win(sfb, sfb->windows[win]);
  771. goto err_ioremap;
  772. }
  773. }
  774. platform_set_drvdata(pdev, sfb);
  775. return 0;
  776. err_ioremap:
  777. iounmap(sfb->regs);
  778. err_req_region:
  779. release_resource(sfb->regs_res);
  780. kfree(sfb->regs_res);
  781. err_clk:
  782. clk_disable(sfb->bus_clk);
  783. clk_put(sfb->bus_clk);
  784. err_sfb:
  785. kfree(sfb);
  786. return ret;
  787. }
  788. /**
  789. * s3c_fb_remove() - Cleanup on module finalisation
  790. * @pdev: The platform device we are bound to.
  791. *
  792. * Shutdown and then release all the resources that the driver allocated
  793. * on initialisation.
  794. */
  795. static int __devexit s3c_fb_remove(struct platform_device *pdev)
  796. {
  797. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  798. int win;
  799. for (win = 0; win < S3C_FB_MAX_WIN; win++)
  800. if (sfb->windows[win])
  801. s3c_fb_release_win(sfb, sfb->windows[win]);
  802. iounmap(sfb->regs);
  803. clk_disable(sfb->bus_clk);
  804. clk_put(sfb->bus_clk);
  805. release_resource(sfb->regs_res);
  806. kfree(sfb->regs_res);
  807. kfree(sfb);
  808. return 0;
  809. }
  810. #ifdef CONFIG_PM
  811. static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
  812. {
  813. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  814. struct s3c_fb_win *win;
  815. int win_no;
  816. for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
  817. win = sfb->windows[win_no];
  818. if (!win)
  819. continue;
  820. /* use the blank function to push into power-down */
  821. s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
  822. }
  823. clk_disable(sfb->bus_clk);
  824. return 0;
  825. }
  826. static int s3c_fb_resume(struct platform_device *pdev)
  827. {
  828. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  829. struct s3c_fb_platdata *pd = sfb->pdata;
  830. struct s3c_fb_win *win;
  831. int win_no;
  832. clk_enable(sfb->bus_clk);
  833. /* setup registers */
  834. writel(pd->vidcon1, sfb->regs + VIDCON1);
  835. /* zero all windows before we do anything */
  836. for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++)
  837. s3c_fb_clear_win(sfb, win_no);
  838. /* restore framebuffers */
  839. for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
  840. win = sfb->windows[win_no];
  841. if (!win)
  842. continue;
  843. dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
  844. s3c_fb_set_par(win->fbinfo);
  845. }
  846. return 0;
  847. }
  848. #else
  849. #define s3c_fb_suspend NULL
  850. #define s3c_fb_resume NULL
  851. #endif
  852. static struct platform_driver s3c_fb_driver = {
  853. .probe = s3c_fb_probe,
  854. .remove = __devexit_p(s3c_fb_remove),
  855. .suspend = s3c_fb_suspend,
  856. .resume = s3c_fb_resume,
  857. .driver = {
  858. .name = "s3c-fb",
  859. .owner = THIS_MODULE,
  860. },
  861. };
  862. static int __init s3c_fb_init(void)
  863. {
  864. return platform_driver_register(&s3c_fb_driver);
  865. }
  866. static void __exit s3c_fb_cleanup(void)
  867. {
  868. platform_driver_unregister(&s3c_fb_driver);
  869. }
  870. module_init(s3c_fb_init);
  871. module_exit(s3c_fb_cleanup);
  872. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  873. MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
  874. MODULE_LICENSE("GPL");
  875. MODULE_ALIAS("platform:s3c-fb");