s3c2410fb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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/tty.h>
  79. #include <linux/slab.h>
  80. #include <linux/delay.h>
  81. #include <linux/fb.h>
  82. #include <linux/init.h>
  83. #include <linux/dma-mapping.h>
  84. #include <linux/string.h>
  85. #include <linux/interrupt.h>
  86. #include <linux/workqueue.h>
  87. #include <linux/wait.h>
  88. #include <linux/platform_device.h>
  89. #include <linux/clk.h>
  90. #include <asm/io.h>
  91. #include <asm/uaccess.h>
  92. #include <asm/div64.h>
  93. #include <asm/mach/map.h>
  94. #include <asm/arch/regs-lcd.h>
  95. #include <asm/arch/regs-gpio.h>
  96. #include <asm/arch/fb.h>
  97. #ifdef CONFIG_PM
  98. #include <linux/pm.h>
  99. #endif
  100. #include "s3c2410fb.h"
  101. static struct s3c2410fb_mach_info *mach_info;
  102. /* Debugging stuff */
  103. #ifdef CONFIG_FB_S3C2410_DEBUG
  104. static int debug = 1;
  105. #else
  106. static int debug = 0;
  107. #endif
  108. #define dprintk(msg...) if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }
  109. /* useful functions */
  110. /* s3c2410fb_set_lcdaddr
  111. *
  112. * initialise lcd controller address pointers
  113. */
  114. static void s3c2410fb_set_lcdaddr(struct s3c2410fb_info *fbi)
  115. {
  116. struct fb_var_screeninfo *var = &fbi->fb->var;
  117. unsigned long saddr1, saddr2, saddr3;
  118. saddr1 = fbi->fb->fix.smem_start >> 1;
  119. saddr2 = fbi->fb->fix.smem_start;
  120. saddr2 += (var->xres * var->yres * var->bits_per_pixel)/8;
  121. saddr2>>= 1;
  122. saddr3 = S3C2410_OFFSIZE(0) | S3C2410_PAGEWIDTH(var->xres);
  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. /* set r/g/b positions */
  175. if (var->bits_per_pixel == 16) {
  176. var->red.offset = 11;
  177. var->green.offset = 5;
  178. var->blue.offset = 0;
  179. var->red.length = 5;
  180. var->green.length = 6;
  181. var->blue.length = 5;
  182. var->transp.length = 0;
  183. } else {
  184. var->red.length = var->bits_per_pixel;
  185. var->red.offset = 0;
  186. var->green.length = var->bits_per_pixel;
  187. var->green.offset = 0;
  188. var->blue.length = var->bits_per_pixel;
  189. var->blue.offset = 0;
  190. var->transp.length = 0;
  191. }
  192. return 0;
  193. }
  194. /* s3c2410fb_activate_var
  195. *
  196. * activate (set) the controller from the given framebuffer
  197. * information
  198. */
  199. static void s3c2410fb_activate_var(struct s3c2410fb_info *fbi,
  200. struct fb_var_screeninfo *var)
  201. {
  202. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_MODEMASK;
  203. dprintk("%s: var->xres = %d\n", __FUNCTION__, var->xres);
  204. dprintk("%s: var->yres = %d\n", __FUNCTION__, var->yres);
  205. dprintk("%s: var->bpp = %d\n", __FUNCTION__, var->bits_per_pixel);
  206. switch (var->bits_per_pixel) {
  207. case 1:
  208. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
  209. break;
  210. case 2:
  211. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
  212. break;
  213. case 4:
  214. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
  215. break;
  216. case 8:
  217. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
  218. break;
  219. case 16:
  220. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
  221. break;
  222. }
  223. /* check to see if we need to update sync/borders */
  224. if (!fbi->mach_info->fixed_syncs) {
  225. dprintk("setting vert: up=%d, low=%d, sync=%d\n",
  226. var->upper_margin, var->lower_margin,
  227. var->vsync_len);
  228. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  229. var->left_margin, var->right_margin,
  230. var->hsync_len);
  231. fbi->regs.lcdcon2 =
  232. S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
  233. S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
  234. S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
  235. fbi->regs.lcdcon3 =
  236. S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
  237. S3C2410_LCDCON3_HFPD(var->left_margin - 1);
  238. fbi->regs.lcdcon4 &= ~S3C2410_LCDCON4_HSPW(0xff);
  239. fbi->regs.lcdcon4 |= S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
  240. }
  241. /* update X/Y info */
  242. fbi->regs.lcdcon2 &= ~S3C2410_LCDCON2_LINEVAL(0x3ff);
  243. fbi->regs.lcdcon2 |= S3C2410_LCDCON2_LINEVAL(var->yres - 1);
  244. fbi->regs.lcdcon3 &= ~S3C2410_LCDCON3_HOZVAL(0x7ff);
  245. fbi->regs.lcdcon3 |= S3C2410_LCDCON3_HOZVAL(var->xres - 1);
  246. if (var->pixclock > 0) {
  247. int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock);
  248. clkdiv = (clkdiv / 2) -1;
  249. if (clkdiv < 0)
  250. clkdiv = 0;
  251. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_CLKVAL(0x3ff);
  252. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
  253. }
  254. /* write new registers */
  255. dprintk("new register set:\n");
  256. dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
  257. dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
  258. dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
  259. dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
  260. dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);
  261. writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
  262. writel(fbi->regs.lcdcon2, S3C2410_LCDCON2);
  263. writel(fbi->regs.lcdcon3, S3C2410_LCDCON3);
  264. writel(fbi->regs.lcdcon4, S3C2410_LCDCON4);
  265. writel(fbi->regs.lcdcon5, S3C2410_LCDCON5);
  266. /* set lcd address pointers */
  267. s3c2410fb_set_lcdaddr(fbi);
  268. writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
  269. }
  270. /*
  271. * s3c2410fb_set_par - Optional function. Alters the hardware state.
  272. * @info: frame buffer structure that represents a single frame buffer
  273. *
  274. */
  275. static int s3c2410fb_set_par(struct fb_info *info)
  276. {
  277. struct s3c2410fb_info *fbi = info->par;
  278. struct fb_var_screeninfo *var = &info->var;
  279. if (var->bits_per_pixel == 16)
  280. fbi->fb->fix.visual = FB_VISUAL_TRUECOLOR;
  281. else
  282. fbi->fb->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  283. fbi->fb->fix.line_length = (var->width*var->bits_per_pixel)/8;
  284. /* activate this new configuration */
  285. s3c2410fb_activate_var(fbi, var);
  286. return 0;
  287. }
  288. static void schedule_palette_update(struct s3c2410fb_info *fbi,
  289. unsigned int regno, unsigned int val)
  290. {
  291. unsigned long flags;
  292. unsigned long irqen;
  293. local_irq_save(flags);
  294. fbi->palette_buffer[regno] = val;
  295. if (!fbi->palette_ready) {
  296. fbi->palette_ready = 1;
  297. /* enable IRQ */
  298. irqen = readl(S3C2410_LCDINTMSK);
  299. irqen &= ~S3C2410_LCDINT_FRSYNC;
  300. writel(irqen, S3C2410_LCDINTMSK);
  301. }
  302. local_irq_restore(flags);
  303. }
  304. /* from pxafb.c */
  305. static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
  306. {
  307. chan &= 0xffff;
  308. chan >>= 16 - bf->length;
  309. return chan << bf->offset;
  310. }
  311. static int s3c2410fb_setcolreg(unsigned regno,
  312. unsigned red, unsigned green, unsigned blue,
  313. unsigned transp, struct fb_info *info)
  314. {
  315. struct s3c2410fb_info *fbi = info->par;
  316. unsigned int val;
  317. /* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n", regno, red, green, blue); */
  318. switch (fbi->fb->fix.visual) {
  319. case FB_VISUAL_TRUECOLOR:
  320. /* true-colour, use pseuo-palette */
  321. if (regno < 16) {
  322. u32 *pal = fbi->fb->pseudo_palette;
  323. val = chan_to_field(red, &fbi->fb->var.red);
  324. val |= chan_to_field(green, &fbi->fb->var.green);
  325. val |= chan_to_field(blue, &fbi->fb->var.blue);
  326. pal[regno] = val;
  327. }
  328. break;
  329. case FB_VISUAL_PSEUDOCOLOR:
  330. if (regno < 256) {
  331. /* currently assume RGB 5-6-5 mode */
  332. val = ((red >> 0) & 0xf800);
  333. val |= ((green >> 5) & 0x07e0);
  334. val |= ((blue >> 11) & 0x001f);
  335. writel(val, S3C2410_TFTPAL(regno));
  336. schedule_palette_update(fbi, regno, val);
  337. }
  338. break;
  339. default:
  340. return 1; /* unknown type */
  341. }
  342. return 0;
  343. }
  344. /**
  345. * s3c2410fb_blank
  346. * @blank_mode: the blank mode we want.
  347. * @info: frame buffer structure that represents a single frame buffer
  348. *
  349. * Blank the screen if blank_mode != 0, else unblank. Return 0 if
  350. * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
  351. * video mode which doesn't support it. Implements VESA suspend
  352. * and powerdown modes on hardware that supports disabling hsync/vsync:
  353. * blank_mode == 2: suspend vsync
  354. * blank_mode == 3: suspend hsync
  355. * blank_mode == 4: powerdown
  356. *
  357. * Returns negative errno on error, or zero on success.
  358. *
  359. */
  360. static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
  361. {
  362. dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
  363. if (mach_info == NULL)
  364. return -EINVAL;
  365. if (blank_mode == FB_BLANK_UNBLANK)
  366. writel(0x0, S3C2410_TPAL);
  367. else {
  368. dprintk("setting TPAL to output 0x000000\n");
  369. writel(S3C2410_TPAL_EN, S3C2410_TPAL);
  370. }
  371. return 0;
  372. }
  373. static int s3c2410fb_debug_show(struct device *dev, struct device_attribute *attr, char *buf)
  374. {
  375. return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
  376. }
  377. static int s3c2410fb_debug_store(struct device *dev, struct device_attribute *attr,
  378. const char *buf, size_t len)
  379. {
  380. if (mach_info == NULL)
  381. return -EINVAL;
  382. if (len < 1)
  383. return -EINVAL;
  384. if (strnicmp(buf, "on", 2) == 0 ||
  385. strnicmp(buf, "1", 1) == 0) {
  386. debug = 1;
  387. printk(KERN_DEBUG "s3c2410fb: Debug On");
  388. } else if (strnicmp(buf, "off", 3) == 0 ||
  389. strnicmp(buf, "0", 1) == 0) {
  390. debug = 0;
  391. printk(KERN_DEBUG "s3c2410fb: Debug Off");
  392. } else {
  393. return -EINVAL;
  394. }
  395. return len;
  396. }
  397. static DEVICE_ATTR(debug, 0666,
  398. s3c2410fb_debug_show,
  399. s3c2410fb_debug_store);
  400. static struct fb_ops s3c2410fb_ops = {
  401. .owner = THIS_MODULE,
  402. .fb_check_var = s3c2410fb_check_var,
  403. .fb_set_par = s3c2410fb_set_par,
  404. .fb_blank = s3c2410fb_blank,
  405. .fb_setcolreg = s3c2410fb_setcolreg,
  406. .fb_fillrect = cfb_fillrect,
  407. .fb_copyarea = cfb_copyarea,
  408. .fb_imageblit = cfb_imageblit,
  409. };
  410. /*
  411. * s3c2410fb_map_video_memory():
  412. * Allocates the DRAM memory for the frame buffer. This buffer is
  413. * remapped into a non-cached, non-buffered, memory region to
  414. * allow palette and pixel writes to occur without flushing the
  415. * cache. Once this area is remapped, all virtual memory
  416. * access to the video memory should occur at the new region.
  417. */
  418. static int __init s3c2410fb_map_video_memory(struct s3c2410fb_info *fbi)
  419. {
  420. dprintk("map_video_memory(fbi=%p)\n", fbi);
  421. fbi->map_size = PAGE_ALIGN(fbi->fb->fix.smem_len + PAGE_SIZE);
  422. fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
  423. &fbi->map_dma, GFP_KERNEL);
  424. fbi->map_size = fbi->fb->fix.smem_len;
  425. if (fbi->map_cpu) {
  426. /* prevent initial garbage on screen */
  427. dprintk("map_video_memory: clear %p:%08x\n",
  428. fbi->map_cpu, fbi->map_size);
  429. memset(fbi->map_cpu, 0xf0, fbi->map_size);
  430. fbi->screen_dma = fbi->map_dma;
  431. fbi->fb->screen_base = fbi->map_cpu;
  432. fbi->fb->fix.smem_start = fbi->screen_dma;
  433. dprintk("map_video_memory: dma=%08x cpu=%p size=%08x\n",
  434. fbi->map_dma, fbi->map_cpu, fbi->fb->fix.smem_len);
  435. }
  436. return fbi->map_cpu ? 0 : -ENOMEM;
  437. }
  438. static inline void s3c2410fb_unmap_video_memory(struct s3c2410fb_info *fbi)
  439. {
  440. dma_free_writecombine(fbi->dev,fbi->map_size,fbi->map_cpu, fbi->map_dma);
  441. }
  442. static inline void modify_gpio(void __iomem *reg,
  443. unsigned long set, unsigned long mask)
  444. {
  445. unsigned long tmp;
  446. tmp = readl(reg) & ~mask;
  447. writel(tmp | set, reg);
  448. }
  449. /*
  450. * s3c2410fb_init_registers - Initialise all LCD-related registers
  451. */
  452. static int s3c2410fb_init_registers(struct s3c2410fb_info *fbi)
  453. {
  454. unsigned long flags;
  455. /* Initialise LCD with values from haret */
  456. local_irq_save(flags);
  457. /* modify the gpio(s) with interrupts set (bjd) */
  458. modify_gpio(S3C2410_GPCUP, mach_info->gpcup, mach_info->gpcup_mask);
  459. modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
  460. modify_gpio(S3C2410_GPDUP, mach_info->gpdup, mach_info->gpdup_mask);
  461. modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);
  462. local_irq_restore(flags);
  463. writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
  464. writel(fbi->regs.lcdcon2, S3C2410_LCDCON2);
  465. writel(fbi->regs.lcdcon3, S3C2410_LCDCON3);
  466. writel(fbi->regs.lcdcon4, S3C2410_LCDCON4);
  467. writel(fbi->regs.lcdcon5, S3C2410_LCDCON5);
  468. s3c2410fb_set_lcdaddr(fbi);
  469. dprintk("LPCSEL = 0x%08lx\n", mach_info->lpcsel);
  470. writel(mach_info->lpcsel, S3C2410_LPCSEL);
  471. dprintk("replacing TPAL %08x\n", readl(S3C2410_TPAL));
  472. /* ensure temporary palette disabled */
  473. writel(0x00, S3C2410_TPAL);
  474. /* Enable video by setting the ENVID bit to 1 */
  475. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID;
  476. writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
  477. return 0;
  478. }
  479. static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)
  480. {
  481. unsigned int i;
  482. unsigned long ent;
  483. fbi->palette_ready = 0;
  484. for (i = 0; i < 256; i++) {
  485. if ((ent = fbi->palette_buffer[i]) == PALETTE_BUFF_CLEAR)
  486. continue;
  487. writel(ent, S3C2410_TFTPAL(i));
  488. /* it seems the only way to know exactly
  489. * if the palette wrote ok, is to check
  490. * to see if the value verifies ok
  491. */
  492. if (readw(S3C2410_TFTPAL(i)) == ent)
  493. fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  494. else
  495. fbi->palette_ready = 1; /* retry */
  496. }
  497. }
  498. static irqreturn_t s3c2410fb_irq(int irq, void *dev_id, struct pt_regs *r)
  499. {
  500. struct s3c2410fb_info *fbi = dev_id;
  501. unsigned long lcdirq = readl(S3C2410_LCDINTPND);
  502. if (lcdirq & S3C2410_LCDINT_FRSYNC) {
  503. if (fbi->palette_ready)
  504. s3c2410fb_write_palette(fbi);
  505. writel(S3C2410_LCDINT_FRSYNC, S3C2410_LCDINTPND);
  506. writel(S3C2410_LCDINT_FRSYNC, S3C2410_LCDSRCPND);
  507. }
  508. return IRQ_HANDLED;
  509. }
  510. static char driver_name[]="s3c2410fb";
  511. static int __init s3c2410fb_probe(struct platform_device *pdev)
  512. {
  513. struct s3c2410fb_info *info;
  514. struct fb_info *fbinfo;
  515. struct s3c2410fb_hw *mregs;
  516. int ret;
  517. int irq;
  518. int i;
  519. mach_info = pdev->dev.platform_data;
  520. if (mach_info == NULL) {
  521. dev_err(&pdev->dev,"no platform data for lcd, cannot attach\n");
  522. return -EINVAL;
  523. }
  524. mregs = &mach_info->regs;
  525. irq = platform_get_irq(pdev, 0);
  526. if (irq < 0) {
  527. dev_err(&pdev->dev, "no irq for device\n");
  528. return -ENOENT;
  529. }
  530. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
  531. if (!fbinfo) {
  532. return -ENOMEM;
  533. }
  534. info = fbinfo->par;
  535. info->fb = fbinfo;
  536. platform_set_drvdata(pdev, fbinfo);
  537. dprintk("devinit\n");
  538. strcpy(fbinfo->fix.id, driver_name);
  539. memcpy(&info->regs, &mach_info->regs, sizeof(info->regs));
  540. info->mach_info = pdev->dev.platform_data;
  541. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  542. fbinfo->fix.type_aux = 0;
  543. fbinfo->fix.xpanstep = 0;
  544. fbinfo->fix.ypanstep = 0;
  545. fbinfo->fix.ywrapstep = 0;
  546. fbinfo->fix.accel = FB_ACCEL_NONE;
  547. fbinfo->var.nonstd = 0;
  548. fbinfo->var.activate = FB_ACTIVATE_NOW;
  549. fbinfo->var.height = mach_info->height;
  550. fbinfo->var.width = mach_info->width;
  551. fbinfo->var.accel_flags = 0;
  552. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  553. fbinfo->fbops = &s3c2410fb_ops;
  554. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  555. fbinfo->pseudo_palette = &info->pseudo_pal;
  556. fbinfo->var.xres = mach_info->xres.defval;
  557. fbinfo->var.xres_virtual = mach_info->xres.defval;
  558. fbinfo->var.yres = mach_info->yres.defval;
  559. fbinfo->var.yres_virtual = mach_info->yres.defval;
  560. fbinfo->var.bits_per_pixel = mach_info->bpp.defval;
  561. fbinfo->var.upper_margin = S3C2410_LCDCON2_GET_VBPD(mregs->lcdcon2) + 1;
  562. fbinfo->var.lower_margin = S3C2410_LCDCON2_GET_VFPD(mregs->lcdcon2) + 1;
  563. fbinfo->var.vsync_len = S3C2410_LCDCON2_GET_VSPW(mregs->lcdcon2) + 1;
  564. fbinfo->var.left_margin = S3C2410_LCDCON3_GET_HFPD(mregs->lcdcon3) + 1;
  565. fbinfo->var.right_margin = S3C2410_LCDCON3_GET_HBPD(mregs->lcdcon3) + 1;
  566. fbinfo->var.hsync_len = S3C2410_LCDCON4_GET_HSPW(mregs->lcdcon4) + 1;
  567. fbinfo->var.red.offset = 11;
  568. fbinfo->var.green.offset = 5;
  569. fbinfo->var.blue.offset = 0;
  570. fbinfo->var.transp.offset = 0;
  571. fbinfo->var.red.length = 5;
  572. fbinfo->var.green.length = 6;
  573. fbinfo->var.blue.length = 5;
  574. fbinfo->var.transp.length = 0;
  575. fbinfo->fix.smem_len = mach_info->xres.max *
  576. mach_info->yres.max *
  577. mach_info->bpp.max / 8;
  578. for (i = 0; i < 256; i++)
  579. info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  580. if (!request_mem_region((unsigned long)S3C24XX_VA_LCD, SZ_1M, "s3c2410-lcd")) {
  581. ret = -EBUSY;
  582. goto dealloc_fb;
  583. }
  584. dprintk("got LCD region\n");
  585. ret = request_irq(irq, s3c2410fb_irq, SA_INTERRUPT, pdev->name, info);
  586. if (ret) {
  587. dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
  588. ret = -EBUSY;
  589. goto release_mem;
  590. }
  591. info->clk = clk_get(NULL, "lcd");
  592. if (!info->clk || IS_ERR(info->clk)) {
  593. printk(KERN_ERR "failed to get lcd clock source\n");
  594. ret = -ENOENT;
  595. goto release_irq;
  596. }
  597. clk_enable(info->clk);
  598. dprintk("got and enabled clock\n");
  599. msleep(1);
  600. /* Initialize video memory */
  601. ret = s3c2410fb_map_video_memory(info);
  602. if (ret) {
  603. printk( KERN_ERR "Failed to allocate video RAM: %d\n", ret);
  604. ret = -ENOMEM;
  605. goto release_clock;
  606. }
  607. dprintk("got video memory\n");
  608. ret = s3c2410fb_init_registers(info);
  609. ret = s3c2410fb_check_var(&fbinfo->var, fbinfo);
  610. ret = register_framebuffer(fbinfo);
  611. if (ret < 0) {
  612. printk(KERN_ERR "Failed to register framebuffer device: %d\n", ret);
  613. goto free_video_memory;
  614. }
  615. /* create device files */
  616. device_create_file(&pdev->dev, &dev_attr_debug);
  617. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  618. fbinfo->node, fbinfo->fix.id);
  619. return 0;
  620. free_video_memory:
  621. s3c2410fb_unmap_video_memory(info);
  622. release_clock:
  623. clk_disable(info->clk);
  624. clk_put(info->clk);
  625. release_irq:
  626. free_irq(irq,info);
  627. release_mem:
  628. release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
  629. dealloc_fb:
  630. framebuffer_release(fbinfo);
  631. return ret;
  632. }
  633. /* s3c2410fb_stop_lcd
  634. *
  635. * shutdown the lcd controller
  636. */
  637. static void s3c2410fb_stop_lcd(void)
  638. {
  639. unsigned long flags;
  640. unsigned long tmp;
  641. local_irq_save(flags);
  642. tmp = readl(S3C2410_LCDCON1);
  643. writel(tmp & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
  644. local_irq_restore(flags);
  645. }
  646. /*
  647. * Cleanup
  648. */
  649. static int s3c2410fb_remove(struct platform_device *pdev)
  650. {
  651. struct fb_info *fbinfo = platform_get_drvdata(pdev);
  652. struct s3c2410fb_info *info = fbinfo->par;
  653. int irq;
  654. s3c2410fb_stop_lcd();
  655. msleep(1);
  656. s3c2410fb_unmap_video_memory(info);
  657. if (info->clk) {
  658. clk_disable(info->clk);
  659. clk_put(info->clk);
  660. info->clk = NULL;
  661. }
  662. irq = platform_get_irq(pdev, 0);
  663. free_irq(irq,info);
  664. release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
  665. unregister_framebuffer(fbinfo);
  666. return 0;
  667. }
  668. #ifdef CONFIG_PM
  669. /* suspend and resume support for the lcd controller */
  670. static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
  671. {
  672. struct fb_info *fbinfo = platform_get_drvdata(dev);
  673. struct s3c2410fb_info *info = fbinfo->par;
  674. s3c2410fb_stop_lcd();
  675. /* sleep before disabling the clock, we need to ensure
  676. * the LCD DMA engine is not going to get back on the bus
  677. * before the clock goes off again (bjd) */
  678. msleep(1);
  679. clk_disable(info->clk);
  680. return 0;
  681. }
  682. static int s3c2410fb_resume(struct platform_device *dev)
  683. {
  684. struct fb_info *fbinfo = platform_get_drvdata(dev);
  685. struct s3c2410fb_info *info = fbinfo->par;
  686. clk_enable(info->clk);
  687. msleep(1);
  688. s3c2410fb_init_registers(info);
  689. return 0;
  690. }
  691. #else
  692. #define s3c2410fb_suspend NULL
  693. #define s3c2410fb_resume NULL
  694. #endif
  695. static struct platform_driver s3c2410fb_driver = {
  696. .probe = s3c2410fb_probe,
  697. .remove = s3c2410fb_remove,
  698. .suspend = s3c2410fb_suspend,
  699. .resume = s3c2410fb_resume,
  700. .driver = {
  701. .name = "s3c2410-lcd",
  702. .owner = THIS_MODULE,
  703. },
  704. };
  705. int __devinit s3c2410fb_init(void)
  706. {
  707. return platform_driver_register(&s3c2410fb_driver);
  708. }
  709. static void __exit s3c2410fb_cleanup(void)
  710. {
  711. platform_driver_unregister(&s3c2410fb_driver);
  712. }
  713. module_init(s3c2410fb_init);
  714. module_exit(s3c2410fb_cleanup);
  715. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, Ben Dooks <ben-linux@fluff.org>");
  716. MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
  717. MODULE_LICENSE("GPL");