s3c2410fb.c 27 KB

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