s3c2410fb.c 23 KB

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