s3c2410fb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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/interrupt.h>
  85. #include <linux/workqueue.h>
  86. #include <linux/wait.h>
  87. #include <linux/platform_device.h>
  88. #include <linux/clk.h>
  89. #include <asm/io.h>
  90. #include <asm/uaccess.h>
  91. #include <asm/div64.h>
  92. #include <asm/mach/map.h>
  93. #include <asm/arch/regs-lcd.h>
  94. #include <asm/arch/regs-gpio.h>
  95. #include <asm/arch/fb.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 void 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. };
  409. /*
  410. * s3c2410fb_map_video_memory():
  411. * Allocates the DRAM memory for the frame buffer. This buffer is
  412. * remapped into a non-cached, non-buffered, memory region to
  413. * allow palette and pixel writes to occur without flushing the
  414. * cache. Once this area is remapped, all virtual memory
  415. * access to the video memory should occur at the new region.
  416. */
  417. static int __init s3c2410fb_map_video_memory(struct s3c2410fb_info *fbi)
  418. {
  419. dprintk("map_video_memory(fbi=%p)\n", fbi);
  420. fbi->map_size = PAGE_ALIGN(fbi->fb->fix.smem_len + PAGE_SIZE);
  421. fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
  422. &fbi->map_dma, GFP_KERNEL);
  423. fbi->map_size = fbi->fb->fix.smem_len;
  424. if (fbi->map_cpu) {
  425. /* prevent initial garbage on screen */
  426. dprintk("map_video_memory: clear %p:%08x\n",
  427. fbi->map_cpu, fbi->map_size);
  428. memset(fbi->map_cpu, 0xf0, fbi->map_size);
  429. fbi->screen_dma = fbi->map_dma;
  430. fbi->fb->screen_base = fbi->map_cpu;
  431. fbi->fb->fix.smem_start = fbi->screen_dma;
  432. dprintk("map_video_memory: dma=%08x cpu=%p size=%08x\n",
  433. fbi->map_dma, fbi->map_cpu, fbi->fb->fix.smem_len);
  434. }
  435. return fbi->map_cpu ? 0 : -ENOMEM;
  436. }
  437. static inline void s3c2410fb_unmap_video_memory(struct s3c2410fb_info *fbi)
  438. {
  439. dma_free_writecombine(fbi->dev,fbi->map_size,fbi->map_cpu, fbi->map_dma);
  440. }
  441. static inline void modify_gpio(void __iomem *reg,
  442. unsigned long set, unsigned long mask)
  443. {
  444. unsigned long tmp;
  445. tmp = readl(reg) & ~mask;
  446. writel(tmp | set, reg);
  447. }
  448. /*
  449. * s3c2410fb_init_registers - Initialise all LCD-related registers
  450. */
  451. static int s3c2410fb_init_registers(struct s3c2410fb_info *fbi)
  452. {
  453. unsigned long flags;
  454. /* Initialise LCD with values from haret */
  455. local_irq_save(flags);
  456. /* modify the gpio(s) with interrupts set (bjd) */
  457. modify_gpio(S3C2410_GPCUP, mach_info->gpcup, mach_info->gpcup_mask);
  458. modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
  459. modify_gpio(S3C2410_GPDUP, mach_info->gpdup, mach_info->gpdup_mask);
  460. modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);
  461. local_irq_restore(flags);
  462. writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
  463. writel(fbi->regs.lcdcon2, S3C2410_LCDCON2);
  464. writel(fbi->regs.lcdcon3, S3C2410_LCDCON3);
  465. writel(fbi->regs.lcdcon4, S3C2410_LCDCON4);
  466. writel(fbi->regs.lcdcon5, S3C2410_LCDCON5);
  467. s3c2410fb_set_lcdaddr(fbi);
  468. dprintk("LPCSEL = 0x%08lx\n", mach_info->lpcsel);
  469. writel(mach_info->lpcsel, S3C2410_LPCSEL);
  470. dprintk("replacing TPAL %08x\n", readl(S3C2410_TPAL));
  471. /* ensure temporary palette disabled */
  472. writel(0x00, S3C2410_TPAL);
  473. /* Enable video by setting the ENVID bit to 1 */
  474. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID;
  475. writel(fbi->regs.lcdcon1, S3C2410_LCDCON1);
  476. return 0;
  477. }
  478. static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)
  479. {
  480. unsigned int i;
  481. unsigned long ent;
  482. fbi->palette_ready = 0;
  483. for (i = 0; i < 256; i++) {
  484. if ((ent = fbi->palette_buffer[i]) == PALETTE_BUFF_CLEAR)
  485. continue;
  486. writel(ent, S3C2410_TFTPAL(i));
  487. /* it seems the only way to know exactly
  488. * if the palette wrote ok, is to check
  489. * to see if the value verifies ok
  490. */
  491. if (readw(S3C2410_TFTPAL(i)) == ent)
  492. fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  493. else
  494. fbi->palette_ready = 1; /* retry */
  495. }
  496. }
  497. static irqreturn_t s3c2410fb_irq(int irq, void *dev_id, struct pt_regs *r)
  498. {
  499. struct s3c2410fb_info *fbi = dev_id;
  500. unsigned long lcdirq = readl(S3C2410_LCDINTPND);
  501. if (lcdirq & S3C2410_LCDINT_FRSYNC) {
  502. if (fbi->palette_ready)
  503. s3c2410fb_write_palette(fbi);
  504. writel(S3C2410_LCDINT_FRSYNC, S3C2410_LCDINTPND);
  505. writel(S3C2410_LCDINT_FRSYNC, S3C2410_LCDSRCPND);
  506. }
  507. return IRQ_HANDLED;
  508. }
  509. static char driver_name[]="s3c2410fb";
  510. static int __init s3c2410fb_probe(struct platform_device *pdev)
  511. {
  512. struct s3c2410fb_info *info;
  513. struct fb_info *fbinfo;
  514. struct s3c2410fb_hw *mregs;
  515. int ret;
  516. int irq;
  517. int i;
  518. mach_info = pdev->dev.platform_data;
  519. if (mach_info == NULL) {
  520. dev_err(&pdev->dev,"no platform data for lcd, cannot attach\n");
  521. return -EINVAL;
  522. }
  523. mregs = &mach_info->regs;
  524. irq = platform_get_irq(pdev, 0);
  525. if (irq < 0) {
  526. dev_err(&pdev->dev, "no irq for device\n");
  527. return -ENOENT;
  528. }
  529. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
  530. if (!fbinfo) {
  531. return -ENOMEM;
  532. }
  533. info = fbinfo->par;
  534. info->fb = fbinfo;
  535. platform_set_drvdata(pdev, fbinfo);
  536. dprintk("devinit\n");
  537. strcpy(fbinfo->fix.id, driver_name);
  538. memcpy(&info->regs, &mach_info->regs, sizeof(info->regs));
  539. info->mach_info = pdev->dev.platform_data;
  540. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  541. fbinfo->fix.type_aux = 0;
  542. fbinfo->fix.xpanstep = 0;
  543. fbinfo->fix.ypanstep = 0;
  544. fbinfo->fix.ywrapstep = 0;
  545. fbinfo->fix.accel = FB_ACCEL_NONE;
  546. fbinfo->var.nonstd = 0;
  547. fbinfo->var.activate = FB_ACTIVATE_NOW;
  548. fbinfo->var.height = mach_info->height;
  549. fbinfo->var.width = mach_info->width;
  550. fbinfo->var.accel_flags = 0;
  551. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  552. fbinfo->fbops = &s3c2410fb_ops;
  553. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  554. fbinfo->pseudo_palette = &info->pseudo_pal;
  555. fbinfo->var.xres = mach_info->xres.defval;
  556. fbinfo->var.xres_virtual = mach_info->xres.defval;
  557. fbinfo->var.yres = mach_info->yres.defval;
  558. fbinfo->var.yres_virtual = mach_info->yres.defval;
  559. fbinfo->var.bits_per_pixel = mach_info->bpp.defval;
  560. fbinfo->var.upper_margin = S3C2410_LCDCON2_GET_VBPD(mregs->lcdcon2) + 1;
  561. fbinfo->var.lower_margin = S3C2410_LCDCON2_GET_VFPD(mregs->lcdcon2) + 1;
  562. fbinfo->var.vsync_len = S3C2410_LCDCON2_GET_VSPW(mregs->lcdcon2) + 1;
  563. fbinfo->var.left_margin = S3C2410_LCDCON3_GET_HFPD(mregs->lcdcon3) + 1;
  564. fbinfo->var.right_margin = S3C2410_LCDCON3_GET_HBPD(mregs->lcdcon3) + 1;
  565. fbinfo->var.hsync_len = S3C2410_LCDCON4_GET_HSPW(mregs->lcdcon4) + 1;
  566. fbinfo->var.red.offset = 11;
  567. fbinfo->var.green.offset = 5;
  568. fbinfo->var.blue.offset = 0;
  569. fbinfo->var.transp.offset = 0;
  570. fbinfo->var.red.length = 5;
  571. fbinfo->var.green.length = 6;
  572. fbinfo->var.blue.length = 5;
  573. fbinfo->var.transp.length = 0;
  574. fbinfo->fix.smem_len = mach_info->xres.max *
  575. mach_info->yres.max *
  576. mach_info->bpp.max / 8;
  577. for (i = 0; i < 256; i++)
  578. info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  579. if (!request_mem_region((unsigned long)S3C24XX_VA_LCD, SZ_1M, "s3c2410-lcd")) {
  580. ret = -EBUSY;
  581. goto dealloc_fb;
  582. }
  583. dprintk("got LCD region\n");
  584. ret = request_irq(irq, s3c2410fb_irq, SA_INTERRUPT, pdev->name, info);
  585. if (ret) {
  586. dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
  587. ret = -EBUSY;
  588. goto release_mem;
  589. }
  590. info->clk = clk_get(NULL, "lcd");
  591. if (!info->clk || IS_ERR(info->clk)) {
  592. printk(KERN_ERR "failed to get lcd clock source\n");
  593. ret = -ENOENT;
  594. goto release_irq;
  595. }
  596. clk_enable(info->clk);
  597. dprintk("got and enabled clock\n");
  598. msleep(1);
  599. /* Initialize video memory */
  600. ret = s3c2410fb_map_video_memory(info);
  601. if (ret) {
  602. printk( KERN_ERR "Failed to allocate video RAM: %d\n", ret);
  603. ret = -ENOMEM;
  604. goto release_clock;
  605. }
  606. dprintk("got video memory\n");
  607. ret = s3c2410fb_init_registers(info);
  608. ret = s3c2410fb_check_var(&fbinfo->var, fbinfo);
  609. ret = register_framebuffer(fbinfo);
  610. if (ret < 0) {
  611. printk(KERN_ERR "Failed to register framebuffer device: %d\n", ret);
  612. goto free_video_memory;
  613. }
  614. /* create device files */
  615. device_create_file(&pdev->dev, &dev_attr_debug);
  616. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  617. fbinfo->node, fbinfo->fix.id);
  618. return 0;
  619. free_video_memory:
  620. s3c2410fb_unmap_video_memory(info);
  621. release_clock:
  622. clk_disable(info->clk);
  623. clk_put(info->clk);
  624. release_irq:
  625. free_irq(irq,info);
  626. release_mem:
  627. release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
  628. dealloc_fb:
  629. framebuffer_release(fbinfo);
  630. return ret;
  631. }
  632. /* s3c2410fb_stop_lcd
  633. *
  634. * shutdown the lcd controller
  635. */
  636. static void s3c2410fb_stop_lcd(void)
  637. {
  638. unsigned long flags;
  639. unsigned long tmp;
  640. local_irq_save(flags);
  641. tmp = readl(S3C2410_LCDCON1);
  642. writel(tmp & ~S3C2410_LCDCON1_ENVID, S3C2410_LCDCON1);
  643. local_irq_restore(flags);
  644. }
  645. /*
  646. * Cleanup
  647. */
  648. static int s3c2410fb_remove(struct platform_device *pdev)
  649. {
  650. struct fb_info *fbinfo = platform_get_drvdata(pdev);
  651. struct s3c2410fb_info *info = fbinfo->par;
  652. int irq;
  653. s3c2410fb_stop_lcd();
  654. msleep(1);
  655. s3c2410fb_unmap_video_memory(info);
  656. if (info->clk) {
  657. clk_disable(info->clk);
  658. clk_put(info->clk);
  659. info->clk = NULL;
  660. }
  661. irq = platform_get_irq(pdev, 0);
  662. free_irq(irq,info);
  663. release_mem_region((unsigned long)S3C24XX_VA_LCD, S3C24XX_SZ_LCD);
  664. unregister_framebuffer(fbinfo);
  665. return 0;
  666. }
  667. #ifdef CONFIG_PM
  668. /* suspend and resume support for the lcd controller */
  669. static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
  670. {
  671. struct fb_info *fbinfo = platform_get_drvdata(dev);
  672. struct s3c2410fb_info *info = fbinfo->par;
  673. s3c2410fb_stop_lcd();
  674. /* sleep before disabling the clock, we need to ensure
  675. * the LCD DMA engine is not going to get back on the bus
  676. * before the clock goes off again (bjd) */
  677. msleep(1);
  678. clk_disable(info->clk);
  679. return 0;
  680. }
  681. static int s3c2410fb_resume(struct platform_device *dev)
  682. {
  683. struct fb_info *fbinfo = platform_get_drvdata(dev);
  684. struct s3c2410fb_info *info = fbinfo->par;
  685. clk_enable(info->clk);
  686. msleep(1);
  687. s3c2410fb_init_registers(info);
  688. return 0;
  689. }
  690. #else
  691. #define s3c2410fb_suspend NULL
  692. #define s3c2410fb_resume NULL
  693. #endif
  694. static struct platform_driver s3c2410fb_driver = {
  695. .probe = s3c2410fb_probe,
  696. .remove = s3c2410fb_remove,
  697. .suspend = s3c2410fb_suspend,
  698. .resume = s3c2410fb_resume,
  699. .driver = {
  700. .name = "s3c2410-lcd",
  701. .owner = THIS_MODULE,
  702. },
  703. };
  704. int __devinit s3c2410fb_init(void)
  705. {
  706. return platform_driver_register(&s3c2410fb_driver);
  707. }
  708. static void __exit s3c2410fb_cleanup(void)
  709. {
  710. platform_driver_unregister(&s3c2410fb_driver);
  711. }
  712. module_init(s3c2410fb_init);
  713. module_exit(s3c2410fb_cleanup);
  714. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, Ben Dooks <ben-linux@fluff.org>");
  715. MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
  716. MODULE_LICENSE("GPL");