s3c-fb.c 26 KB

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