s3c2410fb.c 26 KB

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