s3c-fb.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 platform specified window as the basis for the lcd timings */
  266. if (win_no == sfb->pdata->default_win) {
  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_WSWP;
  365. data |= WINCONx_BURSTLEN_16WORD;
  366. break;
  367. }
  368. /* It has no color key control register for window0 */
  369. if (win_no > 0) {
  370. u32 keycon0_data = 0, keycon1_data = 0;
  371. keycon0_data = ~(WxKEYCON0_KEYBL_EN |
  372. WxKEYCON0_KEYEN_F |
  373. WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
  374. keycon1_data = WxKEYCON1_COLVAL(0xffffff);
  375. writel(keycon0_data, regs + WxKEYCONy(win_no-1, 0));
  376. writel(keycon1_data, regs + WxKEYCONy(win_no-1, 1));
  377. }
  378. writel(data, regs + WINCON(win_no));
  379. writel(0x0, regs + WINxMAP(win_no));
  380. return 0;
  381. }
  382. /**
  383. * s3c_fb_update_palette() - set or schedule a palette update.
  384. * @sfb: The hardware information.
  385. * @win: The window being updated.
  386. * @reg: The palette index being changed.
  387. * @value: The computed palette value.
  388. *
  389. * Change the value of a palette register, either by directly writing to
  390. * the palette (this requires the palette RAM to be disconnected from the
  391. * hardware whilst this is in progress) or schedule the update for later.
  392. *
  393. * At the moment, since we have no VSYNC interrupt support, we simply set
  394. * the palette entry directly.
  395. */
  396. static void s3c_fb_update_palette(struct s3c_fb *sfb,
  397. struct s3c_fb_win *win,
  398. unsigned int reg,
  399. u32 value)
  400. {
  401. void __iomem *palreg;
  402. u32 palcon;
  403. palreg = sfb->regs + s3c_fb_pal_reg(win->index, reg);
  404. dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
  405. __func__, win->index, reg, palreg, value);
  406. win->palette_buffer[reg] = value;
  407. palcon = readl(sfb->regs + WPALCON);
  408. writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
  409. if (s3c_fb_pal_is16(win->index))
  410. writew(value, palreg);
  411. else
  412. writel(value, palreg);
  413. writel(palcon, sfb->regs + WPALCON);
  414. }
  415. static inline unsigned int chan_to_field(unsigned int chan,
  416. struct fb_bitfield *bf)
  417. {
  418. chan &= 0xffff;
  419. chan >>= 16 - bf->length;
  420. return chan << bf->offset;
  421. }
  422. /**
  423. * s3c_fb_setcolreg() - framebuffer layer request to change palette.
  424. * @regno: The palette index to change.
  425. * @red: The red field for the palette data.
  426. * @green: The green field for the palette data.
  427. * @blue: The blue field for the palette data.
  428. * @trans: The transparency (alpha) field for the palette data.
  429. * @info: The framebuffer being changed.
  430. */
  431. static int s3c_fb_setcolreg(unsigned regno,
  432. unsigned red, unsigned green, unsigned blue,
  433. unsigned transp, struct fb_info *info)
  434. {
  435. struct s3c_fb_win *win = info->par;
  436. struct s3c_fb *sfb = win->parent;
  437. unsigned int val;
  438. dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
  439. __func__, win->index, regno, red, green, blue);
  440. switch (info->fix.visual) {
  441. case FB_VISUAL_TRUECOLOR:
  442. /* true-colour, use pseudo-palette */
  443. if (regno < 16) {
  444. u32 *pal = info->pseudo_palette;
  445. val = chan_to_field(red, &info->var.red);
  446. val |= chan_to_field(green, &info->var.green);
  447. val |= chan_to_field(blue, &info->var.blue);
  448. pal[regno] = val;
  449. }
  450. break;
  451. case FB_VISUAL_PSEUDOCOLOR:
  452. if (regno < s3c_fb_win_pal_size(win->index)) {
  453. val = chan_to_field(red, &win->palette.r);
  454. val |= chan_to_field(green, &win->palette.g);
  455. val |= chan_to_field(blue, &win->palette.b);
  456. s3c_fb_update_palette(sfb, win, regno, val);
  457. }
  458. break;
  459. default:
  460. return 1; /* unknown type */
  461. }
  462. return 0;
  463. }
  464. /**
  465. * s3c_fb_enable() - Set the state of the main LCD output
  466. * @sfb: The main framebuffer state.
  467. * @enable: The state to set.
  468. */
  469. static void s3c_fb_enable(struct s3c_fb *sfb, int enable)
  470. {
  471. u32 vidcon0 = readl(sfb->regs + VIDCON0);
  472. if (enable)
  473. vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
  474. else {
  475. /* see the note in the framebuffer datasheet about
  476. * why you cannot take both of these bits down at the
  477. * same time. */
  478. if (!(vidcon0 & VIDCON0_ENVID))
  479. return;
  480. vidcon0 |= VIDCON0_ENVID;
  481. vidcon0 &= ~VIDCON0_ENVID_F;
  482. }
  483. writel(vidcon0, sfb->regs + VIDCON0);
  484. }
  485. /**
  486. * s3c_fb_blank() - blank or unblank the given window
  487. * @blank_mode: The blank state from FB_BLANK_*
  488. * @info: The framebuffer to blank.
  489. *
  490. * Framebuffer layer request to change the power state.
  491. */
  492. static int s3c_fb_blank(int blank_mode, struct fb_info *info)
  493. {
  494. struct s3c_fb_win *win = info->par;
  495. struct s3c_fb *sfb = win->parent;
  496. unsigned int index = win->index;
  497. u32 wincon;
  498. dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
  499. wincon = readl(sfb->regs + WINCON(index));
  500. switch (blank_mode) {
  501. case FB_BLANK_POWERDOWN:
  502. wincon &= ~WINCONx_ENWIN;
  503. sfb->enabled &= ~(1 << index);
  504. /* fall through to FB_BLANK_NORMAL */
  505. case FB_BLANK_NORMAL:
  506. /* disable the DMA and display 0x0 (black) */
  507. writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
  508. sfb->regs + WINxMAP(index));
  509. break;
  510. case FB_BLANK_UNBLANK:
  511. writel(0x0, sfb->regs + WINxMAP(index));
  512. wincon |= WINCONx_ENWIN;
  513. sfb->enabled |= (1 << index);
  514. break;
  515. case FB_BLANK_VSYNC_SUSPEND:
  516. case FB_BLANK_HSYNC_SUSPEND:
  517. default:
  518. return 1;
  519. }
  520. writel(wincon, sfb->regs + WINCON(index));
  521. /* Check the enabled state to see if we need to be running the
  522. * main LCD interface, as if there are no active windows then
  523. * it is highly likely that we also do not need to output
  524. * anything.
  525. */
  526. /* We could do something like the following code, but the current
  527. * system of using framebuffer events means that we cannot make
  528. * the distinction between just window 0 being inactive and all
  529. * the windows being down.
  530. *
  531. * s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
  532. */
  533. /* we're stuck with this until we can do something about overriding
  534. * the power control using the blanking event for a single fb.
  535. */
  536. if (index == sfb->pdata->default_win)
  537. s3c_fb_enable(sfb, blank_mode != FB_BLANK_POWERDOWN ? 1 : 0);
  538. return 0;
  539. }
  540. static struct fb_ops s3c_fb_ops = {
  541. .owner = THIS_MODULE,
  542. .fb_check_var = s3c_fb_check_var,
  543. .fb_set_par = s3c_fb_set_par,
  544. .fb_blank = s3c_fb_blank,
  545. .fb_setcolreg = s3c_fb_setcolreg,
  546. .fb_fillrect = cfb_fillrect,
  547. .fb_copyarea = cfb_copyarea,
  548. .fb_imageblit = cfb_imageblit,
  549. };
  550. /**
  551. * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
  552. * @sfb: The base resources for the hardware.
  553. * @win: The window to initialise memory for.
  554. *
  555. * Allocate memory for the given framebuffer.
  556. */
  557. static int __devinit s3c_fb_alloc_memory(struct s3c_fb *sfb,
  558. struct s3c_fb_win *win)
  559. {
  560. struct s3c_fb_pd_win *windata = win->windata;
  561. unsigned int real_size, virt_size, size;
  562. struct fb_info *fbi = win->fbinfo;
  563. dma_addr_t map_dma;
  564. dev_dbg(sfb->dev, "allocating memory for display\n");
  565. real_size = windata->win_mode.xres * windata->win_mode.yres;
  566. virt_size = windata->virtual_x * windata->virtual_y;
  567. dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
  568. real_size, windata->win_mode.xres, windata->win_mode.yres,
  569. virt_size, windata->virtual_x, windata->virtual_y);
  570. size = (real_size > virt_size) ? real_size : virt_size;
  571. size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
  572. size /= 8;
  573. fbi->fix.smem_len = size;
  574. size = PAGE_ALIGN(size);
  575. dev_dbg(sfb->dev, "want %u bytes for window\n", size);
  576. fbi->screen_base = dma_alloc_writecombine(sfb->dev, size,
  577. &map_dma, GFP_KERNEL);
  578. if (!fbi->screen_base)
  579. return -ENOMEM;
  580. dev_dbg(sfb->dev, "mapped %x to %p\n",
  581. (unsigned int)map_dma, fbi->screen_base);
  582. memset(fbi->screen_base, 0x0, size);
  583. fbi->fix.smem_start = map_dma;
  584. return 0;
  585. }
  586. /**
  587. * s3c_fb_free_memory() - free the display memory for the given window
  588. * @sfb: The base resources for the hardware.
  589. * @win: The window to free the display memory for.
  590. *
  591. * Free the display memory allocated by s3c_fb_alloc_memory().
  592. */
  593. static void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
  594. {
  595. struct fb_info *fbi = win->fbinfo;
  596. dma_free_writecombine(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
  597. fbi->screen_base, fbi->fix.smem_start);
  598. }
  599. /**
  600. * s3c_fb_release_win() - release resources for a framebuffer window.
  601. * @win: The window to cleanup the resources for.
  602. *
  603. * Release the resources that where claimed for the hardware window,
  604. * such as the framebuffer instance and any memory claimed for it.
  605. */
  606. static void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
  607. {
  608. if (win->fbinfo) {
  609. unregister_framebuffer(win->fbinfo);
  610. fb_dealloc_cmap(&win->fbinfo->cmap);
  611. s3c_fb_free_memory(sfb, win);
  612. framebuffer_release(win->fbinfo);
  613. }
  614. }
  615. /**
  616. * s3c_fb_probe_win() - register an hardware window
  617. * @sfb: The base resources for the hardware
  618. * @res: Pointer to where to place the resultant window.
  619. *
  620. * Allocate and do the basic initialisation for one of the hardware's graphics
  621. * windows.
  622. */
  623. static int __devinit s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
  624. struct s3c_fb_win **res)
  625. {
  626. struct fb_var_screeninfo *var;
  627. struct fb_videomode *initmode;
  628. struct s3c_fb_pd_win *windata;
  629. struct s3c_fb_win *win;
  630. struct fb_info *fbinfo;
  631. int palette_size;
  632. int ret;
  633. dev_dbg(sfb->dev, "probing window %d\n", win_no);
  634. palette_size = s3c_fb_win_pal_size(win_no);
  635. fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
  636. palette_size * sizeof(u32), sfb->dev);
  637. if (!fbinfo) {
  638. dev_err(sfb->dev, "failed to allocate framebuffer\n");
  639. return -ENOENT;
  640. }
  641. windata = sfb->pdata->win[win_no];
  642. initmode = &windata->win_mode;
  643. WARN_ON(windata->max_bpp == 0);
  644. WARN_ON(windata->win_mode.xres == 0);
  645. WARN_ON(windata->win_mode.yres == 0);
  646. win = fbinfo->par;
  647. var = &fbinfo->var;
  648. win->fbinfo = fbinfo;
  649. win->parent = sfb;
  650. win->windata = windata;
  651. win->index = win_no;
  652. win->palette_buffer = (u32 *)(win + 1);
  653. ret = s3c_fb_alloc_memory(sfb, win);
  654. if (ret) {
  655. dev_err(sfb->dev, "failed to allocate display memory\n");
  656. return ret;
  657. }
  658. /* setup the r/b/g positions for the window's palette */
  659. s3c_fb_init_palette(win_no, &win->palette);
  660. /* setup the initial video mode from the window */
  661. fb_videomode_to_var(&fbinfo->var, initmode);
  662. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  663. fbinfo->fix.accel = FB_ACCEL_NONE;
  664. fbinfo->var.activate = FB_ACTIVATE_NOW;
  665. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  666. fbinfo->var.bits_per_pixel = windata->default_bpp;
  667. fbinfo->fbops = &s3c_fb_ops;
  668. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  669. fbinfo->pseudo_palette = &win->pseudo_palette;
  670. /* prepare to actually start the framebuffer */
  671. ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
  672. if (ret < 0) {
  673. dev_err(sfb->dev, "check_var failed on initial video params\n");
  674. return ret;
  675. }
  676. /* create initial colour map */
  677. ret = fb_alloc_cmap(&fbinfo->cmap, s3c_fb_win_pal_size(win_no), 1);
  678. if (ret == 0)
  679. fb_set_cmap(&fbinfo->cmap, fbinfo);
  680. else
  681. dev_err(sfb->dev, "failed to allocate fb cmap\n");
  682. s3c_fb_set_par(fbinfo);
  683. dev_dbg(sfb->dev, "about to register framebuffer\n");
  684. /* run the check_var and set_par on our configuration. */
  685. ret = register_framebuffer(fbinfo);
  686. if (ret < 0) {
  687. dev_err(sfb->dev, "failed to register framebuffer\n");
  688. return ret;
  689. }
  690. *res = win;
  691. dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
  692. return 0;
  693. }
  694. /**
  695. * s3c_fb_clear_win() - clear hardware window registers.
  696. * @sfb: The base resources for the hardware.
  697. * @win: The window to process.
  698. *
  699. * Reset the specific window registers to a known state.
  700. */
  701. static void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
  702. {
  703. void __iomem *regs = sfb->regs;
  704. writel(0, regs + WINCON(win));
  705. writel(0xffffff, regs + WxKEYCONy(win, 0));
  706. writel(0xffffff, regs + WxKEYCONy(win, 1));
  707. writel(0, regs + VIDOSD_A(win));
  708. writel(0, regs + VIDOSD_B(win));
  709. writel(0, regs + VIDOSD_C(win));
  710. }
  711. static int __devinit s3c_fb_probe(struct platform_device *pdev)
  712. {
  713. struct device *dev = &pdev->dev;
  714. struct s3c_fb_platdata *pd;
  715. struct s3c_fb *sfb;
  716. struct resource *res;
  717. int win;
  718. int ret = 0;
  719. pd = pdev->dev.platform_data;
  720. if (!pd) {
  721. dev_err(dev, "no platform data specified\n");
  722. return -EINVAL;
  723. }
  724. sfb = kzalloc(sizeof(struct s3c_fb), GFP_KERNEL);
  725. if (!sfb) {
  726. dev_err(dev, "no memory for framebuffers\n");
  727. return -ENOMEM;
  728. }
  729. sfb->dev = dev;
  730. sfb->pdata = pd;
  731. sfb->bus_clk = clk_get(dev, "lcd");
  732. if (IS_ERR(sfb->bus_clk)) {
  733. dev_err(dev, "failed to get bus clock\n");
  734. goto err_sfb;
  735. }
  736. clk_enable(sfb->bus_clk);
  737. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  738. if (!res) {
  739. dev_err(dev, "failed to find registers\n");
  740. ret = -ENOENT;
  741. goto err_clk;
  742. }
  743. sfb->regs_res = request_mem_region(res->start, resource_size(res),
  744. dev_name(dev));
  745. if (!sfb->regs_res) {
  746. dev_err(dev, "failed to claim register region\n");
  747. ret = -ENOENT;
  748. goto err_clk;
  749. }
  750. sfb->regs = ioremap(res->start, resource_size(res));
  751. if (!sfb->regs) {
  752. dev_err(dev, "failed to map registers\n");
  753. ret = -ENXIO;
  754. goto err_req_region;
  755. }
  756. dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
  757. /* setup gpio and output polarity controls */
  758. pd->setup_gpio();
  759. writel(pd->vidcon1, sfb->regs + VIDCON1);
  760. /* zero all windows before we do anything */
  761. for (win = 0; win < S3C_FB_MAX_WIN; win++)
  762. s3c_fb_clear_win(sfb, win);
  763. /* we have the register setup, start allocating framebuffers */
  764. for (win = 0; win < S3C_FB_MAX_WIN; win++) {
  765. if (!pd->win[win])
  766. continue;
  767. ret = s3c_fb_probe_win(sfb, win, &sfb->windows[win]);
  768. if (ret < 0) {
  769. dev_err(dev, "failed to create window %d\n", win);
  770. for (; win >= 0; win--)
  771. s3c_fb_release_win(sfb, sfb->windows[win]);
  772. goto err_ioremap;
  773. }
  774. }
  775. platform_set_drvdata(pdev, sfb);
  776. return 0;
  777. err_ioremap:
  778. iounmap(sfb->regs);
  779. err_req_region:
  780. release_resource(sfb->regs_res);
  781. kfree(sfb->regs_res);
  782. err_clk:
  783. clk_disable(sfb->bus_clk);
  784. clk_put(sfb->bus_clk);
  785. err_sfb:
  786. kfree(sfb);
  787. return ret;
  788. }
  789. /**
  790. * s3c_fb_remove() - Cleanup on module finalisation
  791. * @pdev: The platform device we are bound to.
  792. *
  793. * Shutdown and then release all the resources that the driver allocated
  794. * on initialisation.
  795. */
  796. static int __devexit s3c_fb_remove(struct platform_device *pdev)
  797. {
  798. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  799. int win;
  800. for (win = 0; win < S3C_FB_MAX_WIN; win++)
  801. if (sfb->windows[win])
  802. s3c_fb_release_win(sfb, sfb->windows[win]);
  803. iounmap(sfb->regs);
  804. clk_disable(sfb->bus_clk);
  805. clk_put(sfb->bus_clk);
  806. release_resource(sfb->regs_res);
  807. kfree(sfb->regs_res);
  808. kfree(sfb);
  809. return 0;
  810. }
  811. #ifdef CONFIG_PM
  812. static int s3c_fb_suspend(struct platform_device *pdev, pm_message_t state)
  813. {
  814. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  815. struct s3c_fb_win *win;
  816. int win_no;
  817. for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
  818. win = sfb->windows[win_no];
  819. if (!win)
  820. continue;
  821. /* use the blank function to push into power-down */
  822. s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
  823. }
  824. clk_disable(sfb->bus_clk);
  825. return 0;
  826. }
  827. static int s3c_fb_resume(struct platform_device *pdev)
  828. {
  829. struct s3c_fb *sfb = platform_get_drvdata(pdev);
  830. struct s3c_fb_platdata *pd = sfb->pdata;
  831. struct s3c_fb_win *win;
  832. int win_no;
  833. clk_enable(sfb->bus_clk);
  834. /* setup registers */
  835. writel(pd->vidcon1, sfb->regs + VIDCON1);
  836. /* zero all windows before we do anything */
  837. for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++)
  838. s3c_fb_clear_win(sfb, win_no);
  839. /* restore framebuffers */
  840. for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
  841. win = sfb->windows[win_no];
  842. if (!win)
  843. continue;
  844. dev_dbg(&pdev->dev, "resuming window %d\n", win_no);
  845. s3c_fb_set_par(win->fbinfo);
  846. }
  847. return 0;
  848. }
  849. #else
  850. #define s3c_fb_suspend NULL
  851. #define s3c_fb_resume NULL
  852. #endif
  853. static struct platform_driver s3c_fb_driver = {
  854. .probe = s3c_fb_probe,
  855. .remove = __devexit_p(s3c_fb_remove),
  856. .suspend = s3c_fb_suspend,
  857. .resume = s3c_fb_resume,
  858. .driver = {
  859. .name = "s3c-fb",
  860. .owner = THIS_MODULE,
  861. },
  862. };
  863. static int __init s3c_fb_init(void)
  864. {
  865. return platform_driver_register(&s3c_fb_driver);
  866. }
  867. static void __exit s3c_fb_cleanup(void)
  868. {
  869. platform_driver_unregister(&s3c_fb_driver);
  870. }
  871. module_init(s3c_fb_init);
  872. module_exit(s3c_fb_cleanup);
  873. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  874. MODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
  875. MODULE_LICENSE("GPL");
  876. MODULE_ALIAS("platform:s3c-fb");