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