s3c2410fb.c 27 KB

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