s3c2410fb.c 26 KB

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