s3c2410fb.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * linux/drivers/video/s3c2410fb.c
  3. * Copyright (c) Arnaud Patard, Ben Dooks
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file COPYING in the main directory of this archive for
  7. * more details.
  8. *
  9. * S3C2410 LCD Controller Frame Buffer Driver
  10. * based on skeletonfb.c, sa1100fb.c and others
  11. *
  12. * ChangeLog
  13. * 2005-04-07: Arnaud Patard <arnaud.patard@rtp-net.org>
  14. * - u32 state -> pm_message_t state
  15. * - S3C2410_{VA,SZ}_LCD -> S3C24XX
  16. *
  17. * 2005-03-15: Arnaud Patard <arnaud.patard@rtp-net.org>
  18. * - Removed the ioctl
  19. * - use readl/writel instead of __raw_writel/__raw_readl
  20. *
  21. * 2004-12-04: Arnaud Patard <arnaud.patard@rtp-net.org>
  22. * - Added the possibility to set on or off the
  23. * debugging messages
  24. * - Replaced 0 and 1 by on or off when reading the
  25. * /sys files
  26. *
  27. * 2005-03-23: Ben Dooks <ben-linux@fluff.org>
  28. * - added non 16bpp modes
  29. * - updated platform information for range of x/y/bpp
  30. * - add code to ensure palette is written correctly
  31. * - add pixel clock divisor control
  32. *
  33. * 2004-11-11: Arnaud Patard <arnaud.patard@rtp-net.org>
  34. * - Removed the use of currcon as it no more exists
  35. * - Added LCD power sysfs interface
  36. *
  37. * 2004-11-03: Ben Dooks <ben-linux@fluff.org>
  38. * - minor cleanups
  39. * - add suspend/resume support
  40. * - s3c2410fb_setcolreg() not valid in >8bpp modes
  41. * - removed last CONFIG_FB_S3C2410_FIXED
  42. * - ensure lcd controller stopped before cleanup
  43. * - added sysfs interface for backlight power
  44. * - added mask for gpio configuration
  45. * - ensured IRQs disabled during GPIO configuration
  46. * - disable TPAL before enabling video
  47. *
  48. * 2004-09-20: Arnaud Patard <arnaud.patard@rtp-net.org>
  49. * - Suppress command line options
  50. *
  51. * 2004-09-15: Arnaud Patard <arnaud.patard@rtp-net.org>
  52. * - code cleanup
  53. *
  54. * 2004-09-07: Arnaud Patard <arnaud.patard@rtp-net.org>
  55. * - Renamed from h1940fb.c to s3c2410fb.c
  56. * - Add support for different devices
  57. * - Backlight support
  58. *
  59. * 2004-09-05: Herbert Pötzl <herbert@13thfloor.at>
  60. * - added clock (de-)allocation code
  61. * - added fixem fbmem option
  62. *
  63. * 2004-07-27: Arnaud Patard <arnaud.patard@rtp-net.org>
  64. * - code cleanup
  65. * - added a forgotten return in h1940fb_init
  66. *
  67. * 2004-07-19: Herbert Pötzl <herbert@13thfloor.at>
  68. * - code cleanup and extended debugging
  69. *
  70. * 2004-07-15: Arnaud Patard <arnaud.patard@rtp-net.org>
  71. * - First version
  72. */
  73. #include <linux/module.h>
  74. #include <linux/kernel.h>
  75. #include <linux/errno.h>
  76. #include <linux/string.h>
  77. #include <linux/mm.h>
  78. #include <linux/slab.h>
  79. #include <linux/delay.h>
  80. #include <linux/fb.h>
  81. #include <linux/init.h>
  82. #include <linux/dma-mapping.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/platform_device.h>
  85. #include <linux/clk.h>
  86. #include <asm/io.h>
  87. #include <asm/div64.h>
  88. #include <asm/mach/map.h>
  89. #include <asm/arch/regs-lcd.h>
  90. #include <asm/arch/regs-gpio.h>
  91. #include <asm/arch/fb.h>
  92. #ifdef CONFIG_PM
  93. #include <linux/pm.h>
  94. #endif
  95. #include "s3c2410fb.h"
  96. /* Debugging stuff */
  97. #ifdef CONFIG_FB_S3C2410_DEBUG
  98. static int debug = 1;
  99. #else
  100. static int debug = 0;
  101. #endif
  102. #define dprintk(msg...) if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }
  103. /* useful functions */
  104. /* s3c2410fb_set_lcdaddr
  105. *
  106. * initialise lcd controller address pointers
  107. */
  108. static void s3c2410fb_set_lcdaddr(struct fb_info *info)
  109. {
  110. unsigned long saddr1, saddr2, saddr3;
  111. struct s3c2410fb_info *fbi = info->par;
  112. void __iomem *regs = fbi->io;
  113. saddr1 = info->fix.smem_start >> 1;
  114. saddr2 = info->fix.smem_start;
  115. saddr2 += info->fix.line_length * info->var.yres;
  116. saddr2 >>= 1;
  117. saddr3 = S3C2410_OFFSIZE(0) |
  118. S3C2410_PAGEWIDTH((info->fix.line_length / 2) & 0x3ff);
  119. dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
  120. dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
  121. dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
  122. writel(saddr1, regs + S3C2410_LCDSADDR1);
  123. writel(saddr2, regs + S3C2410_LCDSADDR2);
  124. writel(saddr3, regs + S3C2410_LCDSADDR3);
  125. }
  126. /* s3c2410fb_calc_pixclk()
  127. *
  128. * calculate divisor for clk->pixclk
  129. */
  130. static unsigned int s3c2410fb_calc_pixclk(struct s3c2410fb_info *fbi,
  131. unsigned long pixclk)
  132. {
  133. unsigned long clk = clk_get_rate(fbi->clk);
  134. unsigned long long div;
  135. /* pixclk is in picoseconds, our clock is in Hz
  136. *
  137. * Hz -> picoseconds is / 10^-12
  138. */
  139. div = (unsigned long long)clk * pixclk;
  140. div >>= 12; /* div / 2^12 */
  141. do_div(div, 625 * 625UL * 625); /* div / 5^12 */
  142. dprintk("pixclk %ld, divisor is %ld\n", pixclk, (long)div);
  143. return div;
  144. }
  145. /*
  146. * s3c2410fb_check_var():
  147. * Get the video params out of 'var'. If a value doesn't fit, round it up,
  148. * if it's too big, return -EINVAL.
  149. *
  150. */
  151. static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
  152. struct fb_info *info)
  153. {
  154. struct s3c2410fb_info *fbi = info->par;
  155. struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
  156. struct s3c2410fb_display *display = NULL;
  157. unsigned i;
  158. dprintk("check_var(var=%p, info=%p)\n", var, info);
  159. /* validate x/y resolution */
  160. for (i = 0; i < mach_info->num_displays; i++)
  161. if (var->yres == mach_info->displays[i].yres &&
  162. var->xres == mach_info->displays[i].xres &&
  163. var->bits_per_pixel == mach_info->displays[i].bpp) {
  164. display = mach_info->displays + i;
  165. break;
  166. }
  167. if (!display) {
  168. dprintk("wrong resolution or depth %dx%d at %d bpp\n",
  169. var->xres, var->yres, var->bits_per_pixel);
  170. return -EINVAL;
  171. }
  172. /* it is always the size as the display */
  173. var->xres_virtual = display->xres;
  174. var->yres_virtual = display->yres;
  175. var->height = display->height;
  176. var->width = display->width;
  177. /* copy lcd settings */
  178. var->pixclock = display->pixclock;
  179. var->left_margin = display->left_margin;
  180. var->right_margin = display->right_margin;
  181. var->upper_margin = display->upper_margin;
  182. var->lower_margin = display->lower_margin;
  183. var->vsync_len = display->vsync_len;
  184. var->hsync_len = display->hsync_len;
  185. fbi->regs.lcdcon5 = display->lcdcon5;
  186. /* set display type */
  187. fbi->regs.lcdcon1 = display->type;
  188. var->transp.offset = 0;
  189. var->transp.length = 0;
  190. /* set r/g/b positions */
  191. switch (var->bits_per_pixel) {
  192. case 1:
  193. case 2:
  194. case 4:
  195. var->red.offset = 0;
  196. var->red.length = var->bits_per_pixel;
  197. var->green = var->red;
  198. var->blue = var->red;
  199. break;
  200. case 8:
  201. if (display->type != S3C2410_LCDCON1_TFT) {
  202. /* 8 bpp 332 */
  203. var->red.length = 3;
  204. var->red.offset = 5;
  205. var->green.length = 3;
  206. var->green.offset = 2;
  207. var->blue.length = 2;
  208. var->blue.offset = 0;
  209. } else {
  210. var->red.offset = 0;
  211. var->red.length = 8;
  212. var->green = var->red;
  213. var->blue = var->red;
  214. }
  215. break;
  216. case 12:
  217. /* 12 bpp 444 */
  218. var->red.length = 4;
  219. var->red.offset = 8;
  220. var->green.length = 4;
  221. var->green.offset = 4;
  222. var->blue.length = 4;
  223. var->blue.offset = 0;
  224. break;
  225. default:
  226. case 16:
  227. if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {
  228. /* 16 bpp, 565 format */
  229. var->red.offset = 11;
  230. var->green.offset = 5;
  231. var->blue.offset = 0;
  232. var->red.length = 5;
  233. var->green.length = 6;
  234. var->blue.length = 5;
  235. } else {
  236. /* 16 bpp, 5551 format */
  237. var->red.offset = 11;
  238. var->green.offset = 6;
  239. var->blue.offset = 1;
  240. var->red.length = 5;
  241. var->green.length = 5;
  242. var->blue.length = 5;
  243. }
  244. break;
  245. case 32:
  246. /* 24 bpp 888 and 8 dummy */
  247. var->red.length = 8;
  248. var->red.offset = 16;
  249. var->green.length = 8;
  250. var->green.offset = 8;
  251. var->blue.length = 8;
  252. var->blue.offset = 0;
  253. break;
  254. }
  255. return 0;
  256. }
  257. /* s3c2410fb_calculate_stn_lcd_regs
  258. *
  259. * calculate register values from var settings
  260. */
  261. static void s3c2410fb_calculate_stn_lcd_regs(const struct fb_info *info,
  262. struct s3c2410fb_hw *regs)
  263. {
  264. const struct s3c2410fb_info *fbi = info->par;
  265. const struct fb_var_screeninfo *var = &info->var;
  266. int type = regs->lcdcon1 & ~S3C2410_LCDCON1_TFT;
  267. int hs = var->xres >> 2;
  268. unsigned wdly = (var->left_margin >> 4) - 1;
  269. unsigned wlh = (var->hsync_len >> 4) - 1;
  270. if (type != S3C2410_LCDCON1_STN4)
  271. hs >>= 1;
  272. switch (var->bits_per_pixel) {
  273. case 1:
  274. regs->lcdcon1 |= S3C2410_LCDCON1_STN1BPP;
  275. break;
  276. case 2:
  277. regs->lcdcon1 |= S3C2410_LCDCON1_STN2GREY;
  278. break;
  279. case 4:
  280. regs->lcdcon1 |= S3C2410_LCDCON1_STN4GREY;
  281. break;
  282. case 8:
  283. regs->lcdcon1 |= S3C2410_LCDCON1_STN8BPP;
  284. hs *= 3;
  285. break;
  286. case 12:
  287. regs->lcdcon1 |= S3C2410_LCDCON1_STN12BPP;
  288. hs *= 3;
  289. break;
  290. default:
  291. /* invalid pixel depth */
  292. dev_err(fbi->dev, "invalid bpp %d\n",
  293. var->bits_per_pixel);
  294. }
  295. /* update X/Y info */
  296. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  297. var->left_margin, var->right_margin, var->hsync_len);
  298. regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1);
  299. if (wdly > 3)
  300. wdly = 3;
  301. if (wlh > 3)
  302. wlh = 3;
  303. regs->lcdcon3 = S3C2410_LCDCON3_WDLY(wdly) |
  304. S3C2410_LCDCON3_LINEBLANK(var->right_margin / 8) |
  305. S3C2410_LCDCON3_HOZVAL(hs - 1);
  306. regs->lcdcon4 = S3C2410_LCDCON4_WLH(wlh);
  307. }
  308. /* s3c2410fb_calculate_tft_lcd_regs
  309. *
  310. * calculate register values from var settings
  311. */
  312. static void s3c2410fb_calculate_tft_lcd_regs(const struct fb_info *info,
  313. struct s3c2410fb_hw *regs)
  314. {
  315. const struct s3c2410fb_info *fbi = info->par;
  316. const struct fb_var_screeninfo *var = &info->var;
  317. switch (var->bits_per_pixel) {
  318. case 1:
  319. regs->lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
  320. break;
  321. case 2:
  322. regs->lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
  323. break;
  324. case 4:
  325. regs->lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
  326. break;
  327. case 8:
  328. regs->lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
  329. regs->lcdcon5 |= S3C2410_LCDCON5_BSWP |
  330. S3C2410_LCDCON5_FRM565;
  331. regs->lcdcon5 &= ~S3C2410_LCDCON5_HWSWP;
  332. break;
  333. case 16:
  334. regs->lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
  335. regs->lcdcon5 &= ~S3C2410_LCDCON5_BSWP;
  336. regs->lcdcon5 |= S3C2410_LCDCON5_HWSWP;
  337. break;
  338. case 32:
  339. regs->lcdcon1 |= S3C2410_LCDCON1_TFT24BPP;
  340. regs->lcdcon5 &= ~(S3C2410_LCDCON5_BSWP |
  341. S3C2410_LCDCON5_HWSWP |
  342. S3C2410_LCDCON5_BPP24BL);
  343. break;
  344. default:
  345. /* invalid pixel depth */
  346. dev_err(fbi->dev, "invalid bpp %d\n",
  347. var->bits_per_pixel);
  348. }
  349. /* update X/Y info */
  350. dprintk("setting vert: up=%d, low=%d, sync=%d\n",
  351. var->upper_margin, var->lower_margin, var->vsync_len);
  352. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  353. var->left_margin, var->right_margin, var->hsync_len);
  354. regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1) |
  355. S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
  356. S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
  357. S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
  358. regs->lcdcon3 = S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
  359. S3C2410_LCDCON3_HFPD(var->left_margin - 1) |
  360. S3C2410_LCDCON3_HOZVAL(var->xres - 1);
  361. regs->lcdcon4 = S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
  362. }
  363. /* s3c2410fb_activate_var
  364. *
  365. * activate (set) the controller from the given framebuffer
  366. * information
  367. */
  368. static void s3c2410fb_activate_var(struct fb_info *info)
  369. {
  370. struct s3c2410fb_info *fbi = info->par;
  371. void __iomem *regs = fbi->io;
  372. int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;
  373. struct fb_var_screeninfo *var = &info->var;
  374. int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock) / 2;
  375. dprintk("%s: var->xres = %d\n", __FUNCTION__, var->xres);
  376. dprintk("%s: var->yres = %d\n", __FUNCTION__, var->yres);
  377. dprintk("%s: var->bpp = %d\n", __FUNCTION__, var->bits_per_pixel);
  378. if (type == S3C2410_LCDCON1_TFT) {
  379. s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);
  380. --clkdiv;
  381. if (clkdiv < 0)
  382. clkdiv = 0;
  383. } else {
  384. s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);
  385. if (clkdiv < 2)
  386. clkdiv = 2;
  387. }
  388. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
  389. /* write new registers */
  390. dprintk("new register set:\n");
  391. dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
  392. dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
  393. dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
  394. dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
  395. dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);
  396. writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID,
  397. regs + S3C2410_LCDCON1);
  398. writel(fbi->regs.lcdcon2, regs + S3C2410_LCDCON2);
  399. writel(fbi->regs.lcdcon3, regs + S3C2410_LCDCON3);
  400. writel(fbi->regs.lcdcon4, regs + S3C2410_LCDCON4);
  401. writel(fbi->regs.lcdcon5, regs + S3C2410_LCDCON5);
  402. /* set lcd address pointers */
  403. s3c2410fb_set_lcdaddr(info);
  404. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,
  405. writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);
  406. }
  407. /*
  408. * s3c2410fb_set_par - Alters the hardware state.
  409. * @info: frame buffer structure that represents a single frame buffer
  410. *
  411. */
  412. static int s3c2410fb_set_par(struct fb_info *info)
  413. {
  414. struct fb_var_screeninfo *var = &info->var;
  415. switch (var->bits_per_pixel) {
  416. case 32:
  417. case 16:
  418. case 12:
  419. info->fix.visual = FB_VISUAL_TRUECOLOR;
  420. break;
  421. case 1:
  422. info->fix.visual = FB_VISUAL_MONO01;
  423. break;
  424. default:
  425. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  426. break;
  427. }
  428. info->fix.line_length = (var->width * var->bits_per_pixel) / 8;
  429. /* activate this new configuration */
  430. s3c2410fb_activate_var(info);
  431. return 0;
  432. }
  433. static void schedule_palette_update(struct s3c2410fb_info *fbi,
  434. unsigned int regno, unsigned int val)
  435. {
  436. unsigned long flags;
  437. unsigned long irqen;
  438. void __iomem *regs = fbi->io;
  439. local_irq_save(flags);
  440. fbi->palette_buffer[regno] = val;
  441. if (!fbi->palette_ready) {
  442. fbi->palette_ready = 1;
  443. /* enable IRQ */
  444. irqen = readl(regs + S3C2410_LCDINTMSK);
  445. irqen &= ~S3C2410_LCDINT_FRSYNC;
  446. writel(irqen, regs + S3C2410_LCDINTMSK);
  447. }
  448. local_irq_restore(flags);
  449. }
  450. /* from pxafb.c */
  451. static inline unsigned int chan_to_field(unsigned int chan,
  452. struct fb_bitfield *bf)
  453. {
  454. chan &= 0xffff;
  455. chan >>= 16 - bf->length;
  456. return chan << bf->offset;
  457. }
  458. static int s3c2410fb_setcolreg(unsigned regno,
  459. unsigned red, unsigned green, unsigned blue,
  460. unsigned transp, struct fb_info *info)
  461. {
  462. struct s3c2410fb_info *fbi = info->par;
  463. void __iomem *regs = fbi->io;
  464. unsigned int val;
  465. /* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n",
  466. regno, red, green, blue); */
  467. switch (info->fix.visual) {
  468. case FB_VISUAL_TRUECOLOR:
  469. /* true-colour, use pseudo-palette */
  470. if (regno < 16) {
  471. u32 *pal = info->pseudo_palette;
  472. val = chan_to_field(red, &info->var.red);
  473. val |= chan_to_field(green, &info->var.green);
  474. val |= chan_to_field(blue, &info->var.blue);
  475. pal[regno] = val;
  476. }
  477. break;
  478. case FB_VISUAL_PSEUDOCOLOR:
  479. if (regno < 256) {
  480. /* currently assume RGB 5-6-5 mode */
  481. val = (red >> 0) & 0xf800;
  482. val |= (green >> 5) & 0x07e0;
  483. val |= (blue >> 11) & 0x001f;
  484. writel(val, regs + S3C2410_TFTPAL(regno));
  485. schedule_palette_update(fbi, regno, val);
  486. }
  487. break;
  488. default:
  489. return 1; /* unknown type */
  490. }
  491. return 0;
  492. }
  493. /*
  494. * s3c2410fb_blank
  495. * @blank_mode: the blank mode we want.
  496. * @info: frame buffer structure that represents a single frame buffer
  497. *
  498. * Blank the screen if blank_mode != 0, else unblank. Return 0 if
  499. * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
  500. * video mode which doesn't support it. Implements VESA suspend
  501. * and powerdown modes on hardware that supports disabling hsync/vsync:
  502. * blank_mode == 2: suspend vsync
  503. * blank_mode == 3: suspend hsync
  504. * blank_mode == 4: powerdown
  505. *
  506. * Returns negative errno on error, or zero on success.
  507. *
  508. */
  509. static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
  510. {
  511. struct s3c2410fb_info *fbi = info->par;
  512. void __iomem *regs = fbi->io;
  513. dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
  514. if (blank_mode == FB_BLANK_UNBLANK)
  515. writel(0x0, regs + S3C2410_TPAL);
  516. else {
  517. dprintk("setting TPAL to output 0x000000\n");
  518. writel(S3C2410_TPAL_EN, regs + S3C2410_TPAL);
  519. }
  520. return 0;
  521. }
  522. static int s3c2410fb_debug_show(struct device *dev,
  523. struct device_attribute *attr, char *buf)
  524. {
  525. return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
  526. }
  527. static int s3c2410fb_debug_store(struct device *dev,
  528. struct device_attribute *attr,
  529. const char *buf, size_t len)
  530. {
  531. if (len < 1)
  532. return -EINVAL;
  533. if (strnicmp(buf, "on", 2) == 0 ||
  534. strnicmp(buf, "1", 1) == 0) {
  535. debug = 1;
  536. printk(KERN_DEBUG "s3c2410fb: Debug On");
  537. } else if (strnicmp(buf, "off", 3) == 0 ||
  538. strnicmp(buf, "0", 1) == 0) {
  539. debug = 0;
  540. printk(KERN_DEBUG "s3c2410fb: Debug Off");
  541. } else {
  542. return -EINVAL;
  543. }
  544. return len;
  545. }
  546. static DEVICE_ATTR(debug, 0666, s3c2410fb_debug_show, s3c2410fb_debug_store);
  547. static struct fb_ops s3c2410fb_ops = {
  548. .owner = THIS_MODULE,
  549. .fb_check_var = s3c2410fb_check_var,
  550. .fb_set_par = s3c2410fb_set_par,
  551. .fb_blank = s3c2410fb_blank,
  552. .fb_setcolreg = s3c2410fb_setcolreg,
  553. .fb_fillrect = cfb_fillrect,
  554. .fb_copyarea = cfb_copyarea,
  555. .fb_imageblit = cfb_imageblit,
  556. };
  557. /*
  558. * s3c2410fb_map_video_memory():
  559. * Allocates the DRAM memory for the frame buffer. This buffer is
  560. * remapped into a non-cached, non-buffered, memory region to
  561. * allow palette and pixel writes to occur without flushing the
  562. * cache. Once this area is remapped, all virtual memory
  563. * access to the video memory should occur at the new region.
  564. */
  565. static int __init s3c2410fb_map_video_memory(struct fb_info *info)
  566. {
  567. struct s3c2410fb_info *fbi = info->par;
  568. dma_addr_t map_dma;
  569. unsigned map_size = PAGE_ALIGN(info->fix.smem_len);
  570. dprintk("map_video_memory(fbi=%p)\n", fbi);
  571. info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,
  572. &map_dma, GFP_KERNEL);
  573. if (info->screen_base) {
  574. /* prevent initial garbage on screen */
  575. dprintk("map_video_memory: clear %p:%08x\n",
  576. info->screen_base, map_size);
  577. memset(info->screen_base, 0xf0, map_size);
  578. info->fix.smem_start = map_dma;
  579. dprintk("map_video_memory: dma=%08lx cpu=%p size=%08x\n",
  580. info->fix.smem_start, info->screen_base, map_size);
  581. }
  582. return info->screen_base ? 0 : -ENOMEM;
  583. }
  584. static inline void s3c2410fb_unmap_video_memory(struct fb_info *info)
  585. {
  586. struct s3c2410fb_info *fbi = info->par;
  587. dma_free_writecombine(fbi->dev, PAGE_ALIGN(info->fix.smem_len),
  588. info->screen_base, info->fix.smem_start);
  589. }
  590. static inline void modify_gpio(void __iomem *reg,
  591. unsigned long set, unsigned long mask)
  592. {
  593. unsigned long tmp;
  594. tmp = readl(reg) & ~mask;
  595. writel(tmp | set, reg);
  596. }
  597. /*
  598. * s3c2410fb_init_registers - Initialise all LCD-related registers
  599. */
  600. static int s3c2410fb_init_registers(struct fb_info *info)
  601. {
  602. struct s3c2410fb_info *fbi = info->par;
  603. struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
  604. unsigned long flags;
  605. void __iomem *regs = fbi->io;
  606. /* Initialise LCD with values from haret */
  607. local_irq_save(flags);
  608. /* modify the gpio(s) with interrupts set (bjd) */
  609. modify_gpio(S3C2410_GPCUP, mach_info->gpcup, mach_info->gpcup_mask);
  610. modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
  611. modify_gpio(S3C2410_GPDUP, mach_info->gpdup, mach_info->gpdup_mask);
  612. modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);
  613. local_irq_restore(flags);
  614. dprintk("LPCSEL = 0x%08lx\n", mach_info->lpcsel);
  615. writel(mach_info->lpcsel, regs + S3C2410_LPCSEL);
  616. dprintk("replacing TPAL %08x\n", readl(regs + S3C2410_TPAL));
  617. /* ensure temporary palette disabled */
  618. writel(0x00, regs + S3C2410_TPAL);
  619. return 0;
  620. }
  621. static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)
  622. {
  623. unsigned int i;
  624. void __iomem *regs = fbi->io;
  625. fbi->palette_ready = 0;
  626. for (i = 0; i < 256; i++) {
  627. unsigned long ent = fbi->palette_buffer[i];
  628. if (ent == PALETTE_BUFF_CLEAR)
  629. continue;
  630. writel(ent, regs + S3C2410_TFTPAL(i));
  631. /* it seems the only way to know exactly
  632. * if the palette wrote ok, is to check
  633. * to see if the value verifies ok
  634. */
  635. if (readw(regs + S3C2410_TFTPAL(i)) == ent)
  636. fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  637. else
  638. fbi->palette_ready = 1; /* retry */
  639. }
  640. }
  641. static irqreturn_t s3c2410fb_irq(int irq, void *dev_id)
  642. {
  643. struct s3c2410fb_info *fbi = dev_id;
  644. void __iomem *regs = fbi->io;
  645. unsigned long lcdirq = readl(regs + S3C2410_LCDINTPND);
  646. if (lcdirq & S3C2410_LCDINT_FRSYNC) {
  647. if (fbi->palette_ready)
  648. s3c2410fb_write_palette(fbi);
  649. writel(S3C2410_LCDINT_FRSYNC, regs + S3C2410_LCDINTPND);
  650. writel(S3C2410_LCDINT_FRSYNC, regs + S3C2410_LCDSRCPND);
  651. }
  652. return IRQ_HANDLED;
  653. }
  654. static char driver_name[] = "s3c2410fb";
  655. static int __init s3c2410fb_probe(struct platform_device *pdev)
  656. {
  657. struct s3c2410fb_info *info;
  658. struct s3c2410fb_display *display;
  659. struct fb_info *fbinfo;
  660. struct s3c2410fb_mach_info *mach_info;
  661. struct resource *res;
  662. int ret;
  663. int irq;
  664. int i;
  665. int size;
  666. u32 lcdcon1;
  667. mach_info = pdev->dev.platform_data;
  668. if (mach_info == NULL) {
  669. dev_err(&pdev->dev,
  670. "no platform data for lcd, cannot attach\n");
  671. return -EINVAL;
  672. }
  673. display = mach_info->displays + mach_info->default_display;
  674. irq = platform_get_irq(pdev, 0);
  675. if (irq < 0) {
  676. dev_err(&pdev->dev, "no irq for device\n");
  677. return -ENOENT;
  678. }
  679. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
  680. if (!fbinfo)
  681. return -ENOMEM;
  682. platform_set_drvdata(pdev, fbinfo);
  683. info = fbinfo->par;
  684. info->dev = &pdev->dev;
  685. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  686. if (res == NULL) {
  687. dev_err(&pdev->dev, "failed to get memory registers\n");
  688. ret = -ENXIO;
  689. goto dealloc_fb;
  690. }
  691. size = (res->end - res->start) + 1;
  692. info->mem = request_mem_region(res->start, size, pdev->name);
  693. if (info->mem == NULL) {
  694. dev_err(&pdev->dev, "failed to get memory region\n");
  695. ret = -ENOENT;
  696. goto dealloc_fb;
  697. }
  698. info->io = ioremap(res->start, size);
  699. if (info->io == NULL) {
  700. dev_err(&pdev->dev, "ioremap() of registers failed\n");
  701. ret = -ENXIO;
  702. goto release_mem;
  703. }
  704. dprintk("devinit\n");
  705. strcpy(fbinfo->fix.id, driver_name);
  706. /* Stop the video */
  707. lcdcon1 = readl(info->io + S3C2410_LCDCON1);
  708. writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);
  709. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  710. fbinfo->fix.type_aux = 0;
  711. fbinfo->fix.xpanstep = 0;
  712. fbinfo->fix.ypanstep = 0;
  713. fbinfo->fix.ywrapstep = 0;
  714. fbinfo->fix.accel = FB_ACCEL_NONE;
  715. fbinfo->var.nonstd = 0;
  716. fbinfo->var.activate = FB_ACTIVATE_NOW;
  717. fbinfo->var.accel_flags = 0;
  718. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  719. fbinfo->fbops = &s3c2410fb_ops;
  720. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  721. fbinfo->pseudo_palette = &info->pseudo_pal;
  722. for (i = 0; i < 256; i++)
  723. info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  724. ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
  725. if (ret) {
  726. dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
  727. ret = -EBUSY;
  728. goto release_regs;
  729. }
  730. info->clk = clk_get(NULL, "lcd");
  731. if (!info->clk || IS_ERR(info->clk)) {
  732. printk(KERN_ERR "failed to get lcd clock source\n");
  733. ret = -ENOENT;
  734. goto release_irq;
  735. }
  736. clk_enable(info->clk);
  737. dprintk("got and enabled clock\n");
  738. msleep(1);
  739. /* find maximum required memory size for display */
  740. for (i = 0; i < mach_info->num_displays; i++) {
  741. unsigned long smem_len = mach_info->displays[i].xres;
  742. smem_len *= mach_info->displays[i].yres;
  743. smem_len *= mach_info->displays[i].bpp;
  744. smem_len >>= 3;
  745. if (fbinfo->fix.smem_len < smem_len)
  746. fbinfo->fix.smem_len = smem_len;
  747. }
  748. /* Initialize video memory */
  749. ret = s3c2410fb_map_video_memory(fbinfo);
  750. if (ret) {
  751. printk(KERN_ERR "Failed to allocate video RAM: %d\n", ret);
  752. ret = -ENOMEM;
  753. goto release_clock;
  754. }
  755. dprintk("got video memory\n");
  756. fbinfo->var.xres = display->xres;
  757. fbinfo->var.yres = display->yres;
  758. fbinfo->var.bits_per_pixel = display->bpp;
  759. s3c2410fb_init_registers(fbinfo);
  760. s3c2410fb_check_var(&fbinfo->var, fbinfo);
  761. ret = register_framebuffer(fbinfo);
  762. if (ret < 0) {
  763. printk(KERN_ERR "Failed to register framebuffer device: %d\n",
  764. ret);
  765. goto free_video_memory;
  766. }
  767. /* create device files */
  768. device_create_file(&pdev->dev, &dev_attr_debug);
  769. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  770. fbinfo->node, fbinfo->fix.id);
  771. return 0;
  772. free_video_memory:
  773. s3c2410fb_unmap_video_memory(fbinfo);
  774. release_clock:
  775. clk_disable(info->clk);
  776. clk_put(info->clk);
  777. release_irq:
  778. free_irq(irq, info);
  779. release_regs:
  780. iounmap(info->io);
  781. release_mem:
  782. release_resource(info->mem);
  783. kfree(info->mem);
  784. dealloc_fb:
  785. platform_set_drvdata(pdev, NULL);
  786. framebuffer_release(fbinfo);
  787. return ret;
  788. }
  789. /* s3c2410fb_stop_lcd
  790. *
  791. * shutdown the lcd controller
  792. */
  793. static void s3c2410fb_stop_lcd(struct s3c2410fb_info *fbi)
  794. {
  795. unsigned long flags;
  796. local_irq_save(flags);
  797. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
  798. writel(fbi->regs.lcdcon1, fbi->io + S3C2410_LCDCON1);
  799. local_irq_restore(flags);
  800. }
  801. /*
  802. * Cleanup
  803. */
  804. static int s3c2410fb_remove(struct platform_device *pdev)
  805. {
  806. struct fb_info *fbinfo = platform_get_drvdata(pdev);
  807. struct s3c2410fb_info *info = fbinfo->par;
  808. int irq;
  809. unregister_framebuffer(fbinfo);
  810. s3c2410fb_stop_lcd(info);
  811. msleep(1);
  812. s3c2410fb_unmap_video_memory(fbinfo);
  813. if (info->clk) {
  814. clk_disable(info->clk);
  815. clk_put(info->clk);
  816. info->clk = NULL;
  817. }
  818. irq = platform_get_irq(pdev, 0);
  819. free_irq(irq, info);
  820. iounmap(info->io);
  821. release_resource(info->mem);
  822. kfree(info->mem);
  823. platform_set_drvdata(pdev, NULL);
  824. framebuffer_release(fbinfo);
  825. return 0;
  826. }
  827. #ifdef CONFIG_PM
  828. /* suspend and resume support for the lcd controller */
  829. static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
  830. {
  831. struct fb_info *fbinfo = platform_get_drvdata(dev);
  832. struct s3c2410fb_info *info = fbinfo->par;
  833. s3c2410fb_stop_lcd(info);
  834. /* sleep before disabling the clock, we need to ensure
  835. * the LCD DMA engine is not going to get back on the bus
  836. * before the clock goes off again (bjd) */
  837. msleep(1);
  838. clk_disable(info->clk);
  839. return 0;
  840. }
  841. static int s3c2410fb_resume(struct platform_device *dev)
  842. {
  843. struct fb_info *fbinfo = platform_get_drvdata(dev);
  844. struct s3c2410fb_info *info = fbinfo->par;
  845. clk_enable(info->clk);
  846. msleep(1);
  847. s3c2410fb_init_registers(info);
  848. return 0;
  849. }
  850. #else
  851. #define s3c2410fb_suspend NULL
  852. #define s3c2410fb_resume NULL
  853. #endif
  854. static struct platform_driver s3c2410fb_driver = {
  855. .probe = s3c2410fb_probe,
  856. .remove = s3c2410fb_remove,
  857. .suspend = s3c2410fb_suspend,
  858. .resume = s3c2410fb_resume,
  859. .driver = {
  860. .name = "s3c2410-lcd",
  861. .owner = THIS_MODULE,
  862. },
  863. };
  864. int __init s3c2410fb_init(void)
  865. {
  866. return platform_driver_register(&s3c2410fb_driver);
  867. }
  868. static void __exit s3c2410fb_cleanup(void)
  869. {
  870. platform_driver_unregister(&s3c2410fb_driver);
  871. }
  872. module_init(s3c2410fb_init);
  873. module_exit(s3c2410fb_cleanup);
  874. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
  875. "Ben Dooks <ben-linux@fluff.org>");
  876. MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
  877. MODULE_LICENSE("GPL");