s3c2410fb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 <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. #include <asm/hardware/clock.h>
  96. #ifdef CONFIG_PM
  97. #include <linux/pm.h>
  98. #endif
  99. #include "s3c2410fb.h"
  100. static struct s3c2410fb_mach_info *mach_info;
  101. /* Debugging stuff */
  102. #ifdef CONFIG_FB_S3C2410_DEBUG
  103. static int debug = 1;
  104. #else
  105. static int debug = 0;
  106. #endif
  107. #define dprintk(msg...) if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }
  108. /* useful functions */
  109. /* s3c2410fb_set_lcdaddr
  110. *
  111. * initialise lcd controller address pointers
  112. */
  113. static void s3c2410fb_set_lcdaddr(struct s3c2410fb_info *fbi)
  114. {
  115. struct fb_var_screeninfo *var = &fbi->fb->var;
  116. unsigned long saddr1, saddr2, saddr3;
  117. saddr1 = fbi->fb->fix.smem_start >> 1;
  118. saddr2 = fbi->fb->fix.smem_start;
  119. saddr2 += (var->xres * var->yres * var->bits_per_pixel)/8;
  120. saddr2>>= 1;
  121. saddr3 = S3C2410_OFFSIZE(0) | S3C2410_PAGEWIDTH(var->xres);
  122. dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
  123. dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
  124. dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
  125. writel(saddr1, S3C2410_LCDSADDR1);
  126. writel(saddr2, S3C2410_LCDSADDR2);
  127. writel(saddr3, S3C2410_LCDSADDR3);
  128. }
  129. /* s3c2410fb_calc_pixclk()
  130. *
  131. * calculate divisor for clk->pixclk
  132. */
  133. static unsigned int s3c2410fb_calc_pixclk(struct s3c2410fb_info *fbi,
  134. unsigned long pixclk)
  135. {
  136. unsigned long clk = clk_get_rate(fbi->clk);
  137. unsigned long long div;
  138. /* pixclk is in picoseoncds, our clock is in Hz
  139. *
  140. * Hz -> picoseconds is / 10^-12
  141. */
  142. div = (unsigned long long)clk * pixclk;
  143. do_div(div,1000000UL);
  144. do_div(div,1000000UL);
  145. dprintk("pixclk %ld, divisor is %ld\n", pixclk, (long)div);
  146. return div;
  147. }
  148. /*
  149. * s3c2410fb_check_var():
  150. * Get the video params out of 'var'. If a value doesn't fit, round it up,
  151. * if it's too big, return -EINVAL.
  152. *
  153. */
  154. static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
  155. struct fb_info *info)
  156. {
  157. struct s3c2410fb_info *fbi = info->par;
  158. dprintk("check_var(var=%p, info=%p)\n", var, info);
  159. /* validate x/y resolution */
  160. if (var->yres > fbi->mach_info->yres.max)
  161. var->yres = fbi->mach_info->yres.max;
  162. else if (var->yres < fbi->mach_info->yres.min)
  163. var->yres = fbi->mach_info->yres.min;
  164. if (var->xres > fbi->mach_info->xres.max)
  165. var->yres = fbi->mach_info->xres.max;
  166. else if (var->xres < fbi->mach_info->xres.min)
  167. var->xres = fbi->mach_info->xres.min;
  168. /* validate bpp */
  169. if (var->bits_per_pixel > fbi->mach_info->bpp.max)
  170. var->bits_per_pixel = fbi->mach_info->bpp.max;
  171. else if (var->bits_per_pixel < fbi->mach_info->bpp.min)
  172. var->bits_per_pixel = fbi->mach_info->bpp.min;
  173. /* set r/g/b positions */
  174. if (var->bits_per_pixel == 16) {
  175. var->red.offset = 11;
  176. var->green.offset = 5;
  177. var->blue.offset = 0;
  178. var->red.length = 5;
  179. var->green.length = 6;
  180. var->blue.length = 5;
  181. var->transp.length = 0;
  182. } else {
  183. var->red.length = var->bits_per_pixel;
  184. var->red.offset = 0;
  185. var->green.length = var->bits_per_pixel;
  186. var->green.offset = 0;
  187. var->blue.length = var->bits_per_pixel;
  188. var->blue.offset = 0;
  189. var->transp.length = 0;
  190. }
  191. return 0;
  192. }
  193. /* s3c2410fb_activate_var
  194. *
  195. * activate (set) the controller from the given framebuffer
  196. * information
  197. */
  198. static int s3c2410fb_activate_var(struct s3c2410fb_info *fbi,
  199. struct fb_var_screeninfo *var)
  200. {
  201. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_MODEMASK;
  202. dprintk("%s: var->xres = %d\n", __FUNCTION__, var->xres);
  203. dprintk("%s: var->yres = %d\n", __FUNCTION__, var->yres);
  204. dprintk("%s: var->bpp = %d\n", __FUNCTION__, var->bits_per_pixel);
  205. switch (var->bits_per_pixel) {
  206. case 1:
  207. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
  208. break;
  209. case 2:
  210. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
  211. break;
  212. case 4:
  213. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
  214. break;
  215. case 8:
  216. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
  217. break;
  218. case 16:
  219. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
  220. break;
  221. }
  222. /* check to see if we need to update sync/borders */
  223. if (!fbi->mach_info->fixed_syncs) {
  224. dprintk("setting vert: up=%d, low=%d, sync=%d\n",
  225. var->upper_margin, var->lower_margin,
  226. var->vsync_len);
  227. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  228. var->left_margin, var->right_margin,
  229. var->hsync_len);
  230. fbi->regs.lcdcon2 =
  231. S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
  232. S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
  233. S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
  234. fbi->regs.lcdcon3 =
  235. S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
  236. S3C2410_LCDCON3_HFPD(var->left_margin - 1);
  237. fbi->regs.lcdcon4 &= ~S3C2410_LCDCON4_HSPW(0xff);
  238. fbi->regs.lcdcon4 |= S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
  239. }
  240. /* update X/Y info */
  241. fbi->regs.lcdcon2 &= ~S3C2410_LCDCON2_LINEVAL(0x3ff);
  242. fbi->regs.lcdcon2 |= S3C2410_LCDCON2_LINEVAL(var->yres - 1);
  243. fbi->regs.lcdcon3 &= ~S3C2410_LCDCON3_HOZVAL(0x7ff);
  244. fbi->regs.lcdcon3 |= S3C2410_LCDCON3_HOZVAL(var->xres - 1);
  245. if (var->pixclock > 0) {
  246. int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock);
  247. clkdiv = (clkdiv / 2) -1;
  248. if (clkdiv < 0)
  249. clkdiv = 0;
  250. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_CLKVAL(0x3ff);
  251. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
  252. }
  253. /* write new registers */
  254. dprintk("new register set:\n");
  255. dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
  256. dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
  257. dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
  258. dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
  259. dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);
  260. writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
  261. writel(fbi->regs.lcdcon2, S3C2410_LCDCON2);
  262. writel(fbi->regs.lcdcon3, S3C2410_LCDCON3);
  263. writel(fbi->regs.lcdcon4, S3C2410_LCDCON4);
  264. writel(fbi->regs.lcdcon5, S3C2410_LCDCON5);
  265. /* set lcd address pointers */
  266. s3c2410fb_set_lcdaddr(fbi);
  267. writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
  268. }
  269. /*
  270. * s3c2410fb_set_par - Optional function. Alters the hardware state.
  271. * @info: frame buffer structure that represents a single frame buffer
  272. *
  273. */
  274. static int s3c2410fb_set_par(struct fb_info *info)
  275. {
  276. struct s3c2410fb_info *fbi = info->par;
  277. struct fb_var_screeninfo *var = &info->var;
  278. if (var->bits_per_pixel == 16)
  279. fbi->fb->fix.visual = FB_VISUAL_TRUECOLOR;
  280. else
  281. fbi->fb->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  282. fbi->fb->fix.line_length = (var->width*var->bits_per_pixel)/8;
  283. /* activate this new configuration */
  284. s3c2410fb_activate_var(fbi, var);
  285. return 0;
  286. }
  287. static void schedule_palette_update(struct s3c2410fb_info *fbi,
  288. unsigned int regno, unsigned int val)
  289. {
  290. unsigned long flags;
  291. unsigned long irqen;
  292. local_irq_save(flags);
  293. fbi->palette_buffer[regno] = val;
  294. if (!fbi->palette_ready) {
  295. fbi->palette_ready = 1;
  296. /* enable IRQ */
  297. irqen = readl(S3C2410_LCDINTMSK);
  298. irqen &= ~S3C2410_LCDINT_FRSYNC;
  299. writel(irqen, S3C2410_LCDINTMSK);
  300. }
  301. local_irq_restore(flags);
  302. }
  303. /* from pxafb.c */
  304. static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
  305. {
  306. chan &= 0xffff;
  307. chan >>= 16 - bf->length;
  308. return chan << bf->offset;
  309. }
  310. static int s3c2410fb_setcolreg(unsigned regno,
  311. unsigned red, unsigned green, unsigned blue,
  312. unsigned transp, struct fb_info *info)
  313. {
  314. struct s3c2410fb_info *fbi = info->par;
  315. unsigned int val;
  316. /* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n", regno, red, green, blue); */
  317. switch (fbi->fb->fix.visual) {
  318. case FB_VISUAL_TRUECOLOR:
  319. /* true-colour, use pseuo-palette */
  320. if (regno < 16) {
  321. u32 *pal = fbi->fb->pseudo_palette;
  322. val = chan_to_field(red, &fbi->fb->var.red);
  323. val |= chan_to_field(green, &fbi->fb->var.green);
  324. val |= chan_to_field(blue, &fbi->fb->var.blue);
  325. pal[regno] = val;
  326. }
  327. break;
  328. case FB_VISUAL_PSEUDOCOLOR:
  329. if (regno < 256) {
  330. /* currently assume RGB 5-6-5 mode */
  331. val = ((red >> 0) & 0xf800);
  332. val |= ((green >> 5) & 0x07e0);
  333. val |= ((blue >> 11) & 0x001f);
  334. writel(val, S3C2410_TFTPAL(regno));
  335. schedule_palette_update(fbi, regno, val);
  336. }
  337. break;
  338. default:
  339. return 1; /* unknown type */
  340. }
  341. return 0;
  342. }
  343. /**
  344. * s3c2410fb_blank
  345. * @blank_mode: the blank mode we want.
  346. * @info: frame buffer structure that represents a single frame buffer
  347. *
  348. * Blank the screen if blank_mode != 0, else unblank. Return 0 if
  349. * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
  350. * video mode which doesn't support it. Implements VESA suspend
  351. * and powerdown modes on hardware that supports disabling hsync/vsync:
  352. * blank_mode == 2: suspend vsync
  353. * blank_mode == 3: suspend hsync
  354. * blank_mode == 4: powerdown
  355. *
  356. * Returns negative errno on error, or zero on success.
  357. *
  358. */
  359. static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
  360. {
  361. dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
  362. if (mach_info == NULL)
  363. return -EINVAL;
  364. if (blank_mode == FB_BLANK_UNBLANK)
  365. writel(0x0, S3C2410_TPAL);
  366. else {
  367. dprintk("setting TPAL to output 0x000000\n");
  368. writel(S3C2410_TPAL_EN, S3C2410_TPAL);
  369. }
  370. return 0;
  371. }
  372. static int s3c2410fb_debug_show(struct device *dev, struct device_attribute *attr, char *buf)
  373. {
  374. return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
  375. }
  376. static int s3c2410fb_debug_store(struct device *dev, struct device_attribute *attr,
  377. const char *buf, size_t len)
  378. {
  379. if (mach_info == NULL)
  380. return -EINVAL;
  381. if (len < 1)
  382. return -EINVAL;
  383. if (strnicmp(buf, "on", 2) == 0 ||
  384. strnicmp(buf, "1", 1) == 0) {
  385. debug = 1;
  386. printk(KERN_DEBUG "s3c2410fb: Debug On");
  387. } else if (strnicmp(buf, "off", 3) == 0 ||
  388. strnicmp(buf, "0", 1) == 0) {
  389. debug = 0;
  390. printk(KERN_DEBUG "s3c2410fb: Debug Off");
  391. } else {
  392. return -EINVAL;
  393. }
  394. return len;
  395. }
  396. static DEVICE_ATTR(debug, 0666,
  397. s3c2410fb_debug_show,
  398. s3c2410fb_debug_store);
  399. static struct fb_ops s3c2410fb_ops = {
  400. .owner = THIS_MODULE,
  401. .fb_check_var = s3c2410fb_check_var,
  402. .fb_set_par = s3c2410fb_set_par,
  403. .fb_blank = s3c2410fb_blank,
  404. .fb_setcolreg = s3c2410fb_setcolreg,
  405. .fb_fillrect = cfb_fillrect,
  406. .fb_copyarea = cfb_copyarea,
  407. .fb_imageblit = cfb_imageblit,
  408. .fb_cursor = soft_cursor,
  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. 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. int __init s3c2410fb_probe(struct device *dev)
  512. {
  513. struct s3c2410fb_info *info;
  514. struct fb_info *fbinfo;
  515. struct platform_device *pdev = to_platform_device(dev);
  516. struct s3c2410fb_hw *mregs;
  517. int ret;
  518. int irq;
  519. int i;
  520. mach_info = dev->platform_data;
  521. if (mach_info == NULL) {
  522. dev_err(dev,"no platform data for lcd, cannot attach\n");
  523. return -EINVAL;
  524. }
  525. mregs = &mach_info->regs;
  526. irq = platform_get_irq(pdev, 0);
  527. if (irq < 0) {
  528. dev_err(dev, "no irq for device\n");
  529. return -ENOENT;
  530. }
  531. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), dev);
  532. if (!fbinfo) {
  533. return -ENOMEM;
  534. }
  535. info = fbinfo->par;
  536. info->fb = fbinfo;
  537. dev_set_drvdata(dev, fbinfo);
  538. s3c2410fb_init_registers(info);
  539. dprintk("devinit\n");
  540. strcpy(fbinfo->fix.id, driver_name);
  541. memcpy(&info->regs, &mach_info->regs, sizeof(info->regs));
  542. info->mach_info = dev->platform_data;
  543. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  544. fbinfo->fix.type_aux = 0;
  545. fbinfo->fix.xpanstep = 0;
  546. fbinfo->fix.ypanstep = 0;
  547. fbinfo->fix.ywrapstep = 0;
  548. fbinfo->fix.accel = FB_ACCEL_NONE;
  549. fbinfo->var.nonstd = 0;
  550. fbinfo->var.activate = FB_ACTIVATE_NOW;
  551. fbinfo->var.height = mach_info->height;
  552. fbinfo->var.width = mach_info->width;
  553. fbinfo->var.accel_flags = 0;
  554. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  555. fbinfo->fbops = &s3c2410fb_ops;
  556. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  557. fbinfo->pseudo_palette = &info->pseudo_pal;
  558. fbinfo->var.xres = mach_info->xres.defval;
  559. fbinfo->var.xres_virtual = mach_info->xres.defval;
  560. fbinfo->var.yres = mach_info->yres.defval;
  561. fbinfo->var.yres_virtual = mach_info->yres.defval;
  562. fbinfo->var.bits_per_pixel = mach_info->bpp.defval;
  563. fbinfo->var.upper_margin = S3C2410_LCDCON2_GET_VBPD(mregs->lcdcon2) +1;
  564. fbinfo->var.lower_margin = S3C2410_LCDCON2_GET_VFPD(mregs->lcdcon2) +1;
  565. fbinfo->var.vsync_len = S3C2410_LCDCON2_GET_VSPW(mregs->lcdcon2) + 1;
  566. fbinfo->var.left_margin = S3C2410_LCDCON3_GET_HFPD(mregs->lcdcon3) + 1;
  567. fbinfo->var.right_margin = S3C2410_LCDCON3_GET_HBPD(mregs->lcdcon3) + 1;
  568. fbinfo->var.hsync_len = S3C2410_LCDCON4_GET_HSPW(mregs->lcdcon4) + 1;
  569. fbinfo->var.red.offset = 11;
  570. fbinfo->var.green.offset = 5;
  571. fbinfo->var.blue.offset = 0;
  572. fbinfo->var.transp.offset = 0;
  573. fbinfo->var.red.length = 5;
  574. fbinfo->var.green.length = 6;
  575. fbinfo->var.blue.length = 5;
  576. fbinfo->var.transp.length = 0;
  577. fbinfo->fix.smem_len = mach_info->xres.max *
  578. mach_info->yres.max *
  579. mach_info->bpp.max / 8;
  580. for (i = 0; i < 256; i++)
  581. info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  582. if (!request_mem_region((unsigned long)S3C24XX_VA_LCD, SZ_1M, "s3c2410-lcd")) {
  583. ret = -EBUSY;
  584. goto dealloc_fb;
  585. }
  586. dprintk("got LCD region\n");
  587. ret = request_irq(irq, s3c2410fb_irq, SA_INTERRUPT, pdev->name, info);
  588. if (ret) {
  589. dev_err(dev, "cannot get irq %d - err %d\n", irq, ret);
  590. ret = -EBUSY;
  591. goto release_mem;
  592. }
  593. info->clk = clk_get(NULL, "lcd");
  594. if (!info->clk || IS_ERR(info->clk)) {
  595. printk(KERN_ERR "failed to get lcd clock source\n");
  596. ret = -ENOENT;
  597. goto release_irq;
  598. }
  599. clk_use(info->clk);
  600. clk_enable(info->clk);
  601. dprintk("got and enabled clock\n");
  602. msleep(1);
  603. /* Initialize video memory */
  604. ret = s3c2410fb_map_video_memory(info);
  605. if (ret) {
  606. printk( KERN_ERR "Failed to allocate video RAM: %d\n", ret);
  607. ret = -ENOMEM;
  608. goto release_clock;
  609. }
  610. dprintk("got video memory\n");
  611. ret = s3c2410fb_init_registers(info);
  612. ret = s3c2410fb_check_var(&fbinfo->var, fbinfo);
  613. ret = register_framebuffer(fbinfo);
  614. if (ret < 0) {
  615. printk(KERN_ERR "Failed to register framebuffer device: %d\n", ret);
  616. goto free_video_memory;
  617. }
  618. /* create device files */
  619. device_create_file(dev, &dev_attr_debug);
  620. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  621. fbinfo->node, fbinfo->fix.id);
  622. return 0;
  623. free_video_memory:
  624. s3c2410fb_unmap_video_memory(info);
  625. release_clock:
  626. clk_disable(info->clk);
  627. clk_unuse(info->clk);
  628. clk_put(info->clk);
  629. release_irq:
  630. free_irq(irq,info);
  631. release_mem:
  632. release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
  633. dealloc_fb:
  634. framebuffer_release(fbinfo);
  635. return ret;
  636. }
  637. /* s3c2410fb_stop_lcd
  638. *
  639. * shutdown the lcd controller
  640. */
  641. static void s3c2410fb_stop_lcd(void)
  642. {
  643. unsigned long flags;
  644. unsigned long tmp;
  645. local_irq_save(flags);
  646. tmp = readl(S3C2410_LCDCON1);
  647. writel(tmp & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
  648. local_irq_restore(flags);
  649. }
  650. /*
  651. * Cleanup
  652. */
  653. static int s3c2410fb_remove(struct device *dev)
  654. {
  655. struct platform_device *pdev = to_platform_device(dev);
  656. struct fb_info *fbinfo = dev_get_drvdata(dev);
  657. struct s3c2410fb_info *info = fbinfo->par;
  658. int irq;
  659. s3c2410fb_stop_lcd();
  660. msleep(1);
  661. s3c2410fb_unmap_video_memory(info);
  662. if (info->clk) {
  663. clk_disable(info->clk);
  664. clk_unuse(info->clk);
  665. clk_put(info->clk);
  666. info->clk = NULL;
  667. }
  668. irq = platform_get_irq(pdev, 0);
  669. free_irq(irq,info);
  670. release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
  671. unregister_framebuffer(fbinfo);
  672. return 0;
  673. }
  674. #ifdef CONFIG_PM
  675. /* suspend and resume support for the lcd controller */
  676. static int s3c2410fb_suspend(struct device *dev, pm_message_t state, u32 level)
  677. {
  678. struct fb_info *fbinfo = dev_get_drvdata(dev);
  679. struct s3c2410fb_info *info = fbinfo->par;
  680. if (level == SUSPEND_DISABLE || level == SUSPEND_POWER_DOWN) {
  681. s3c2410fb_stop_lcd();
  682. /* sleep before disabling the clock, we need to ensure
  683. * the LCD DMA engine is not going to get back on the bus
  684. * before the clock goes off again (bjd) */
  685. msleep(1);
  686. clk_disable(info->clk);
  687. }
  688. return 0;
  689. }
  690. static int s3c2410fb_resume(struct device *dev, u32 level)
  691. {
  692. struct fb_info *fbinfo = dev_get_drvdata(dev);
  693. struct s3c2410fb_info *info = fbinfo->par;
  694. if (level == RESUME_ENABLE) {
  695. clk_enable(info->clk);
  696. msleep(1);
  697. s3c2410fb_init_registers(info);
  698. }
  699. return 0;
  700. }
  701. #else
  702. #define s3c2410fb_suspend NULL
  703. #define s3c2410fb_resume NULL
  704. #endif
  705. static struct device_driver s3c2410fb_driver = {
  706. .name = "s3c2410-lcd",
  707. .bus = &platform_bus_type,
  708. .probe = s3c2410fb_probe,
  709. .suspend = s3c2410fb_suspend,
  710. .resume = s3c2410fb_resume,
  711. .remove = s3c2410fb_remove
  712. };
  713. int __devinit s3c2410fb_init(void)
  714. {
  715. return driver_register(&s3c2410fb_driver);
  716. }
  717. static void __exit s3c2410fb_cleanup(void)
  718. {
  719. driver_unregister(&s3c2410fb_driver);
  720. }
  721. module_init(s3c2410fb_init);
  722. module_exit(s3c2410fb_cleanup);
  723. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, Ben Dooks <ben-linux@fluff.org>");
  724. MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
  725. MODULE_LICENSE("GPL");