s3c2410fb.c 28 KB

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