s3c2410fb.c 26 KB

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