s3c2410fb.c 26 KB

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