s3c2410fb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 messages
  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 exists
  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/platform_device.h>
  85. #include <linux/clk.h>
  86. #include <asm/io.h>
  87. #include <asm/div64.h>
  88. #include <asm/mach/map.h>
  89. #include <asm/arch/regs-lcd.h>
  90. #include <asm/arch/regs-gpio.h>
  91. #include <asm/arch/fb.h>
  92. #ifdef CONFIG_PM
  93. #include <linux/pm.h>
  94. #endif
  95. #include "s3c2410fb.h"
  96. /* Debugging stuff */
  97. #ifdef CONFIG_FB_S3C2410_DEBUG
  98. static int debug = 1;
  99. #else
  100. static int debug = 0;
  101. #endif
  102. #define dprintk(msg...) if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }
  103. /* useful functions */
  104. /* s3c2410fb_set_lcdaddr
  105. *
  106. * initialise lcd controller address pointers
  107. */
  108. static void s3c2410fb_set_lcdaddr(struct fb_info *info)
  109. {
  110. unsigned long saddr1, saddr2, saddr3;
  111. struct s3c2410fb_info *fbi = info->par;
  112. void __iomem *regs = fbi->io;
  113. saddr1 = info->fix.smem_start >> 1;
  114. saddr2 = info->fix.smem_start;
  115. saddr2 += info->fix.line_length * info->var.yres;
  116. saddr2 >>= 1;
  117. saddr3 = S3C2410_OFFSIZE(0) |
  118. S3C2410_PAGEWIDTH((info->fix.line_length / 2) & 0x3ff);
  119. dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
  120. dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
  121. dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
  122. writel(saddr1, regs + S3C2410_LCDSADDR1);
  123. writel(saddr2, regs + S3C2410_LCDSADDR2);
  124. writel(saddr3, regs + S3C2410_LCDSADDR3);
  125. }
  126. /* s3c2410fb_calc_pixclk()
  127. *
  128. * calculate divisor for clk->pixclk
  129. */
  130. static unsigned int s3c2410fb_calc_pixclk(struct s3c2410fb_info *fbi,
  131. unsigned long pixclk)
  132. {
  133. unsigned long clk = clk_get_rate(fbi->clk);
  134. unsigned long long div;
  135. /* pixclk is in picoseconds, our clock is in Hz
  136. *
  137. * Hz -> picoseconds is / 10^-12
  138. */
  139. div = (unsigned long long)clk * pixclk;
  140. div >>= 12; /* div / 2^12 */
  141. do_div(div, 625 * 625UL * 625); /* div / 5^12 */
  142. dprintk("pixclk %ld, divisor is %ld\n", pixclk, (long)div);
  143. return div;
  144. }
  145. /*
  146. * s3c2410fb_check_var():
  147. * Get the video params out of 'var'. If a value doesn't fit, round it up,
  148. * if it's too big, return -EINVAL.
  149. *
  150. */
  151. static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
  152. struct fb_info *info)
  153. {
  154. struct s3c2410fb_info *fbi = info->par;
  155. struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
  156. struct s3c2410fb_display *display = NULL;
  157. unsigned i;
  158. dprintk("check_var(var=%p, info=%p)\n", var, info);
  159. /* validate x/y resolution */
  160. for (i = 0; i < mach_info->num_displays; i++)
  161. if (var->yres == mach_info->displays[i].yres &&
  162. var->xres == mach_info->displays[i].xres &&
  163. var->bits_per_pixel == mach_info->displays[i].bpp) {
  164. display = mach_info->displays + i;
  165. break;
  166. }
  167. if (!display) {
  168. dprintk("wrong resolution or depth %dx%d at %d bpp\n",
  169. var->xres, var->yres, var->bits_per_pixel);
  170. return -EINVAL;
  171. }
  172. /* it is always the size as the display */
  173. var->xres_virtual = display->xres;
  174. var->yres_virtual = display->yres;
  175. var->height = display->height;
  176. var->width = display->width;
  177. /* copy lcd settings */
  178. var->left_margin = display->left_margin;
  179. var->right_margin = display->right_margin;
  180. var->upper_margin = display->upper_margin;
  181. var->lower_margin = display->lower_margin;
  182. var->vsync_len = display->vsync_len;
  183. var->hsync_len = display->hsync_len;
  184. fbi->regs.lcdcon1 = display->lcdcon1;
  185. fbi->regs.lcdcon5 = display->lcdcon5;
  186. /* set display type */
  187. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_TFT;
  188. fbi->regs.lcdcon1 |= display->type;
  189. var->transp.offset = 0;
  190. var->transp.length = 0;
  191. /* set r/g/b positions */
  192. switch (var->bits_per_pixel) {
  193. case 1:
  194. case 2:
  195. case 4:
  196. var->red.offset = 0;
  197. var->red.length = var->bits_per_pixel;
  198. var->green = var->red;
  199. var->blue = var->red;
  200. break;
  201. case 8:
  202. if (display->type != S3C2410_LCDCON1_TFT) {
  203. /* 8 bpp 332 */
  204. var->red.length = 3;
  205. var->red.offset = 5;
  206. var->green.length = 3;
  207. var->green.offset = 2;
  208. var->blue.length = 2;
  209. var->blue.offset = 0;
  210. } else {
  211. var->red.offset = 0;
  212. var->red.length = 8;
  213. var->green = var->red;
  214. var->blue = var->red;
  215. }
  216. break;
  217. case 12:
  218. /* 12 bpp 444 */
  219. var->red.length = 4;
  220. var->red.offset = 8;
  221. var->green.length = 4;
  222. var->green.offset = 4;
  223. var->blue.length = 4;
  224. var->blue.offset = 0;
  225. break;
  226. default:
  227. case 16:
  228. if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {
  229. /* 16 bpp, 565 format */
  230. var->red.offset = 11;
  231. var->green.offset = 5;
  232. var->blue.offset = 0;
  233. var->red.length = 5;
  234. var->green.length = 6;
  235. var->blue.length = 5;
  236. } else {
  237. /* 16 bpp, 5551 format */
  238. var->red.offset = 11;
  239. var->green.offset = 6;
  240. var->blue.offset = 1;
  241. var->red.length = 5;
  242. var->green.length = 5;
  243. var->blue.length = 5;
  244. }
  245. break;
  246. case 32:
  247. /* 24 bpp 888 and 8 dummy */
  248. var->red.length = 8;
  249. var->red.offset = 16;
  250. var->green.length = 8;
  251. var->green.offset = 8;
  252. var->blue.length = 8;
  253. var->blue.offset = 0;
  254. break;
  255. }
  256. return 0;
  257. }
  258. /* s3c2410fb_calculate_stn_lcd_regs
  259. *
  260. * calculate register values from var settings
  261. */
  262. static void s3c2410fb_calculate_stn_lcd_regs(const struct fb_info *info,
  263. struct s3c2410fb_hw *regs)
  264. {
  265. const struct s3c2410fb_info *fbi = info->par;
  266. const struct fb_var_screeninfo *var = &info->var;
  267. int type = regs->lcdcon1 & ~S3C2410_LCDCON1_TFT;
  268. int hs = var->xres >> 2;
  269. unsigned wdly = (var->left_margin >> 4) - 1;
  270. unsigned wlh = (var->hsync_len >> 4) - 1;
  271. dprintk("%s: var->xres = %d\n", __FUNCTION__, var->xres);
  272. dprintk("%s: var->yres = %d\n", __FUNCTION__, var->yres);
  273. dprintk("%s: var->bpp = %d\n", __FUNCTION__, var->bits_per_pixel);
  274. if (type != S3C2410_LCDCON1_STN4)
  275. hs >>= 1;
  276. regs->lcdcon1 &= ~S3C2410_LCDCON1_MODEMASK;
  277. switch (var->bits_per_pixel) {
  278. case 1:
  279. regs->lcdcon1 |= S3C2410_LCDCON1_STN1BPP;
  280. break;
  281. case 2:
  282. regs->lcdcon1 |= S3C2410_LCDCON1_STN2GREY;
  283. break;
  284. case 4:
  285. regs->lcdcon1 |= S3C2410_LCDCON1_STN4GREY;
  286. break;
  287. case 8:
  288. regs->lcdcon1 |= S3C2410_LCDCON1_STN8BPP;
  289. hs *= 3;
  290. break;
  291. case 12:
  292. regs->lcdcon1 |= S3C2410_LCDCON1_STN12BPP;
  293. hs *= 3;
  294. break;
  295. default:
  296. /* invalid pixel depth */
  297. dev_err(fbi->dev, "invalid bpp %d\n",
  298. var->bits_per_pixel);
  299. }
  300. /* update X/Y info */
  301. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  302. var->left_margin, var->right_margin, var->hsync_len);
  303. regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1);
  304. if (wdly > 3)
  305. wdly = 3;
  306. if (wlh > 3)
  307. wlh = 3;
  308. regs->lcdcon3 = S3C2410_LCDCON3_WDLY(wdly) |
  309. S3C2410_LCDCON3_LINEBLANK(var->right_margin / 8) |
  310. S3C2410_LCDCON3_HOZVAL(hs - 1);
  311. regs->lcdcon4 = S3C2410_LCDCON4_WLH(wlh);
  312. }
  313. /* s3c2410fb_calculate_tft_lcd_regs
  314. *
  315. * calculate register values from var settings
  316. */
  317. static void s3c2410fb_calculate_tft_lcd_regs(const struct fb_info *info,
  318. struct s3c2410fb_hw *regs)
  319. {
  320. const struct s3c2410fb_info *fbi = info->par;
  321. const struct fb_var_screeninfo *var = &info->var;
  322. dprintk("%s: var->xres = %d\n", __FUNCTION__, var->xres);
  323. dprintk("%s: var->yres = %d\n", __FUNCTION__, var->yres);
  324. dprintk("%s: var->bpp = %d\n", __FUNCTION__, var->bits_per_pixel);
  325. regs->lcdcon1 &= ~S3C2410_LCDCON1_MODEMASK;
  326. switch (var->bits_per_pixel) {
  327. case 1:
  328. regs->lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
  329. break;
  330. case 2:
  331. regs->lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
  332. break;
  333. case 4:
  334. regs->lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
  335. break;
  336. case 8:
  337. regs->lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
  338. regs->lcdcon5 |= S3C2410_LCDCON5_BSWP |
  339. S3C2410_LCDCON5_FRM565;
  340. regs->lcdcon5 &= ~S3C2410_LCDCON5_HWSWP;
  341. break;
  342. case 16:
  343. regs->lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
  344. regs->lcdcon5 &= ~S3C2410_LCDCON5_BSWP;
  345. regs->lcdcon5 |= S3C2410_LCDCON5_HWSWP;
  346. break;
  347. case 32:
  348. regs->lcdcon1 |= S3C2410_LCDCON1_TFT24BPP;
  349. regs->lcdcon5 &= ~(S3C2410_LCDCON5_BSWP |
  350. S3C2410_LCDCON5_HWSWP |
  351. S3C2410_LCDCON5_BPP24BL);
  352. break;
  353. default:
  354. /* invalid pixel depth */
  355. dev_err(fbi->dev, "invalid bpp %d\n",
  356. var->bits_per_pixel);
  357. }
  358. /* update X/Y info */
  359. dprintk("setting vert: up=%d, low=%d, sync=%d\n",
  360. var->upper_margin, var->lower_margin, var->vsync_len);
  361. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  362. var->left_margin, var->right_margin, var->hsync_len);
  363. regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1) |
  364. S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
  365. S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
  366. S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
  367. regs->lcdcon3 = S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
  368. S3C2410_LCDCON3_HFPD(var->left_margin - 1) |
  369. S3C2410_LCDCON3_HOZVAL(var->xres - 1);
  370. regs->lcdcon4 = S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
  371. }
  372. /* s3c2410fb_activate_var
  373. *
  374. * activate (set) the controller from the given framebuffer
  375. * information
  376. */
  377. static void s3c2410fb_activate_var(struct fb_info *info)
  378. {
  379. struct s3c2410fb_info *fbi = info->par;
  380. void __iomem *regs = fbi->io;
  381. int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;
  382. struct fb_var_screeninfo *var = &info->var;
  383. if (var->pixclock > 0) {
  384. int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock);
  385. if (type == S3C2410_LCDCON1_TFT) {
  386. clkdiv = (clkdiv / 2) - 1;
  387. if (clkdiv < 0)
  388. clkdiv = 0;
  389. } else {
  390. clkdiv = (clkdiv / 2);
  391. if (clkdiv < 2)
  392. clkdiv = 2;
  393. }
  394. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_CLKVAL(0x3ff);
  395. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
  396. }
  397. if (type == S3C2410_LCDCON1_TFT)
  398. s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);
  399. else
  400. s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);
  401. /* write new registers */
  402. dprintk("new register set:\n");
  403. dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
  404. dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
  405. dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
  406. dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
  407. dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);
  408. writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID,
  409. regs + S3C2410_LCDCON1);
  410. writel(fbi->regs.lcdcon2, regs + S3C2410_LCDCON2);
  411. writel(fbi->regs.lcdcon3, regs + S3C2410_LCDCON3);
  412. writel(fbi->regs.lcdcon4, regs + S3C2410_LCDCON4);
  413. writel(fbi->regs.lcdcon5, regs + S3C2410_LCDCON5);
  414. /* set lcd address pointers */
  415. s3c2410fb_set_lcdaddr(info);
  416. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,
  417. writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);
  418. }
  419. /*
  420. * s3c2410fb_set_par - Alters the hardware state.
  421. * @info: frame buffer structure that represents a single frame buffer
  422. *
  423. */
  424. static int s3c2410fb_set_par(struct fb_info *info)
  425. {
  426. struct fb_var_screeninfo *var = &info->var;
  427. switch (var->bits_per_pixel) {
  428. case 32:
  429. case 16:
  430. case 12:
  431. info->fix.visual = FB_VISUAL_TRUECOLOR;
  432. break;
  433. case 1:
  434. info->fix.visual = FB_VISUAL_MONO01;
  435. break;
  436. default:
  437. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  438. break;
  439. }
  440. info->fix.line_length = (var->width * var->bits_per_pixel) / 8;
  441. /* activate this new configuration */
  442. s3c2410fb_activate_var(info);
  443. return 0;
  444. }
  445. static void schedule_palette_update(struct s3c2410fb_info *fbi,
  446. unsigned int regno, unsigned int val)
  447. {
  448. unsigned long flags;
  449. unsigned long irqen;
  450. void __iomem *regs = fbi->io;
  451. local_irq_save(flags);
  452. fbi->palette_buffer[regno] = val;
  453. if (!fbi->palette_ready) {
  454. fbi->palette_ready = 1;
  455. /* enable IRQ */
  456. irqen = readl(regs + S3C2410_LCDINTMSK);
  457. irqen &= ~S3C2410_LCDINT_FRSYNC;
  458. writel(irqen, regs + S3C2410_LCDINTMSK);
  459. }
  460. local_irq_restore(flags);
  461. }
  462. /* from pxafb.c */
  463. static inline unsigned int chan_to_field(unsigned int chan,
  464. struct fb_bitfield *bf)
  465. {
  466. chan &= 0xffff;
  467. chan >>= 16 - bf->length;
  468. return chan << bf->offset;
  469. }
  470. static int s3c2410fb_setcolreg(unsigned regno,
  471. unsigned red, unsigned green, unsigned blue,
  472. unsigned transp, struct fb_info *info)
  473. {
  474. struct s3c2410fb_info *fbi = info->par;
  475. void __iomem *regs = fbi->io;
  476. unsigned int val;
  477. /* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n",
  478. regno, red, green, blue); */
  479. switch (info->fix.visual) {
  480. case FB_VISUAL_TRUECOLOR:
  481. /* true-colour, use pseudo-palette */
  482. if (regno < 16) {
  483. u32 *pal = info->pseudo_palette;
  484. val = chan_to_field(red, &info->var.red);
  485. val |= chan_to_field(green, &info->var.green);
  486. val |= chan_to_field(blue, &info->var.blue);
  487. pal[regno] = val;
  488. }
  489. break;
  490. case FB_VISUAL_PSEUDOCOLOR:
  491. if (regno < 256) {
  492. /* currently assume RGB 5-6-5 mode */
  493. val = (red >> 0) & 0xf800;
  494. val |= (green >> 5) & 0x07e0;
  495. val |= (blue >> 11) & 0x001f;
  496. writel(val, regs + S3C2410_TFTPAL(regno));
  497. schedule_palette_update(fbi, regno, val);
  498. }
  499. break;
  500. default:
  501. return 1; /* unknown type */
  502. }
  503. return 0;
  504. }
  505. /*
  506. * s3c2410fb_blank
  507. * @blank_mode: the blank mode we want.
  508. * @info: frame buffer structure that represents a single frame buffer
  509. *
  510. * Blank the screen if blank_mode != 0, else unblank. Return 0 if
  511. * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
  512. * video mode which doesn't support it. Implements VESA suspend
  513. * and powerdown modes on hardware that supports disabling hsync/vsync:
  514. * blank_mode == 2: suspend vsync
  515. * blank_mode == 3: suspend hsync
  516. * blank_mode == 4: powerdown
  517. *
  518. * Returns negative errno on error, or zero on success.
  519. *
  520. */
  521. static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
  522. {
  523. struct s3c2410fb_info *fbi = info->par;
  524. void __iomem *regs = fbi->io;
  525. dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
  526. if (blank_mode == FB_BLANK_UNBLANK)
  527. writel(0x0, regs + S3C2410_TPAL);
  528. else {
  529. dprintk("setting TPAL to output 0x000000\n");
  530. writel(S3C2410_TPAL_EN, regs + S3C2410_TPAL);
  531. }
  532. return 0;
  533. }
  534. static int s3c2410fb_debug_show(struct device *dev,
  535. struct device_attribute *attr, char *buf)
  536. {
  537. return snprintf(buf, PAGE_SIZE, "%s\n", debug ? "on" : "off");
  538. }
  539. static int s3c2410fb_debug_store(struct device *dev,
  540. struct device_attribute *attr,
  541. const char *buf, size_t len)
  542. {
  543. if (len < 1)
  544. return -EINVAL;
  545. if (strnicmp(buf, "on", 2) == 0 ||
  546. strnicmp(buf, "1", 1) == 0) {
  547. debug = 1;
  548. printk(KERN_DEBUG "s3c2410fb: Debug On");
  549. } else if (strnicmp(buf, "off", 3) == 0 ||
  550. strnicmp(buf, "0", 1) == 0) {
  551. debug = 0;
  552. printk(KERN_DEBUG "s3c2410fb: Debug Off");
  553. } else {
  554. return -EINVAL;
  555. }
  556. return len;
  557. }
  558. static DEVICE_ATTR(debug, 0666, s3c2410fb_debug_show, s3c2410fb_debug_store);
  559. static struct fb_ops s3c2410fb_ops = {
  560. .owner = THIS_MODULE,
  561. .fb_check_var = s3c2410fb_check_var,
  562. .fb_set_par = s3c2410fb_set_par,
  563. .fb_blank = s3c2410fb_blank,
  564. .fb_setcolreg = s3c2410fb_setcolreg,
  565. .fb_fillrect = cfb_fillrect,
  566. .fb_copyarea = cfb_copyarea,
  567. .fb_imageblit = cfb_imageblit,
  568. };
  569. /*
  570. * s3c2410fb_map_video_memory():
  571. * Allocates the DRAM memory for the frame buffer. This buffer is
  572. * remapped into a non-cached, non-buffered, memory region to
  573. * allow palette and pixel writes to occur without flushing the
  574. * cache. Once this area is remapped, all virtual memory
  575. * access to the video memory should occur at the new region.
  576. */
  577. static int __init s3c2410fb_map_video_memory(struct fb_info *info)
  578. {
  579. struct s3c2410fb_info *fbi = info->par;
  580. dma_addr_t map_dma;
  581. unsigned map_size = PAGE_ALIGN(info->fix.smem_len);
  582. dprintk("map_video_memory(fbi=%p)\n", fbi);
  583. info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,
  584. &map_dma, GFP_KERNEL);
  585. if (info->screen_base) {
  586. /* prevent initial garbage on screen */
  587. dprintk("map_video_memory: clear %p:%08x\n",
  588. info->screen_base, map_size);
  589. memset(info->screen_base, 0xf0, map_size);
  590. info->fix.smem_start = map_dma;
  591. dprintk("map_video_memory: dma=%08lx cpu=%p size=%08x\n",
  592. info->fix.smem_start, info->screen_base, map_size);
  593. }
  594. return info->screen_base ? 0 : -ENOMEM;
  595. }
  596. static inline void s3c2410fb_unmap_video_memory(struct fb_info *info)
  597. {
  598. struct s3c2410fb_info *fbi = info->par;
  599. dma_free_writecombine(fbi->dev, PAGE_ALIGN(info->fix.smem_len),
  600. info->screen_base, info->fix.smem_start);
  601. }
  602. static inline void modify_gpio(void __iomem *reg,
  603. unsigned long set, unsigned long mask)
  604. {
  605. unsigned long tmp;
  606. tmp = readl(reg) & ~mask;
  607. writel(tmp | set, reg);
  608. }
  609. /*
  610. * s3c2410fb_init_registers - Initialise all LCD-related registers
  611. */
  612. static int s3c2410fb_init_registers(struct fb_info *info)
  613. {
  614. struct s3c2410fb_info *fbi = info->par;
  615. struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
  616. unsigned long flags;
  617. void __iomem *regs = fbi->io;
  618. /* Initialise LCD with values from haret */
  619. local_irq_save(flags);
  620. /* modify the gpio(s) with interrupts set (bjd) */
  621. modify_gpio(S3C2410_GPCUP, mach_info->gpcup, mach_info->gpcup_mask);
  622. modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);
  623. modify_gpio(S3C2410_GPDUP, mach_info->gpdup, mach_info->gpdup_mask);
  624. modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);
  625. local_irq_restore(flags);
  626. dprintk("LPCSEL = 0x%08lx\n", mach_info->lpcsel);
  627. writel(mach_info->lpcsel, regs + S3C2410_LPCSEL);
  628. dprintk("replacing TPAL %08x\n", readl(regs + S3C2410_TPAL));
  629. /* ensure temporary palette disabled */
  630. writel(0x00, regs + S3C2410_TPAL);
  631. return 0;
  632. }
  633. static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)
  634. {
  635. unsigned int i;
  636. void __iomem *regs = fbi->io;
  637. fbi->palette_ready = 0;
  638. for (i = 0; i < 256; i++) {
  639. unsigned long ent = fbi->palette_buffer[i];
  640. if (ent == PALETTE_BUFF_CLEAR)
  641. continue;
  642. writel(ent, regs + S3C2410_TFTPAL(i));
  643. /* it seems the only way to know exactly
  644. * if the palette wrote ok, is to check
  645. * to see if the value verifies ok
  646. */
  647. if (readw(regs + S3C2410_TFTPAL(i)) == ent)
  648. fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  649. else
  650. fbi->palette_ready = 1; /* retry */
  651. }
  652. }
  653. static irqreturn_t s3c2410fb_irq(int irq, void *dev_id)
  654. {
  655. struct s3c2410fb_info *fbi = dev_id;
  656. void __iomem *regs = fbi->io;
  657. unsigned long lcdirq = readl(regs + S3C2410_LCDINTPND);
  658. if (lcdirq & S3C2410_LCDINT_FRSYNC) {
  659. if (fbi->palette_ready)
  660. s3c2410fb_write_palette(fbi);
  661. writel(S3C2410_LCDINT_FRSYNC, regs + S3C2410_LCDINTPND);
  662. writel(S3C2410_LCDINT_FRSYNC, regs + S3C2410_LCDSRCPND);
  663. }
  664. return IRQ_HANDLED;
  665. }
  666. static char driver_name[] = "s3c2410fb";
  667. static int __init s3c2410fb_probe(struct platform_device *pdev)
  668. {
  669. struct s3c2410fb_info *info;
  670. struct s3c2410fb_display *display;
  671. struct fb_info *fbinfo;
  672. struct s3c2410fb_mach_info *mach_info;
  673. struct resource *res;
  674. int ret;
  675. int irq;
  676. int i;
  677. int size;
  678. u32 lcdcon1;
  679. mach_info = pdev->dev.platform_data;
  680. if (mach_info == NULL) {
  681. dev_err(&pdev->dev,
  682. "no platform data for lcd, cannot attach\n");
  683. return -EINVAL;
  684. }
  685. display = mach_info->displays + mach_info->default_display;
  686. irq = platform_get_irq(pdev, 0);
  687. if (irq < 0) {
  688. dev_err(&pdev->dev, "no irq for device\n");
  689. return -ENOENT;
  690. }
  691. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
  692. if (!fbinfo)
  693. return -ENOMEM;
  694. platform_set_drvdata(pdev, fbinfo);
  695. info = fbinfo->par;
  696. info->dev = &pdev->dev;
  697. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  698. if (res == NULL) {
  699. dev_err(&pdev->dev, "failed to get memory registers\n");
  700. ret = -ENXIO;
  701. goto dealloc_fb;
  702. }
  703. size = (res->end - res->start) + 1;
  704. info->mem = request_mem_region(res->start, size, pdev->name);
  705. if (info->mem == NULL) {
  706. dev_err(&pdev->dev, "failed to get memory region\n");
  707. ret = -ENOENT;
  708. goto dealloc_fb;
  709. }
  710. info->io = ioremap(res->start, size);
  711. if (info->io == NULL) {
  712. dev_err(&pdev->dev, "ioremap() of registers failed\n");
  713. ret = -ENXIO;
  714. goto release_mem;
  715. }
  716. dprintk("devinit\n");
  717. strcpy(fbinfo->fix.id, driver_name);
  718. /* Stop the video */
  719. lcdcon1 = readl(info->io + S3C2410_LCDCON1);
  720. writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);
  721. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  722. fbinfo->fix.type_aux = 0;
  723. fbinfo->fix.xpanstep = 0;
  724. fbinfo->fix.ypanstep = 0;
  725. fbinfo->fix.ywrapstep = 0;
  726. fbinfo->fix.accel = FB_ACCEL_NONE;
  727. fbinfo->var.nonstd = 0;
  728. fbinfo->var.activate = FB_ACTIVATE_NOW;
  729. fbinfo->var.accel_flags = 0;
  730. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  731. fbinfo->fbops = &s3c2410fb_ops;
  732. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  733. fbinfo->pseudo_palette = &info->pseudo_pal;
  734. for (i = 0; i < 256; i++)
  735. info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  736. ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
  737. if (ret) {
  738. dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
  739. ret = -EBUSY;
  740. goto release_regs;
  741. }
  742. info->clk = clk_get(NULL, "lcd");
  743. if (!info->clk || IS_ERR(info->clk)) {
  744. printk(KERN_ERR "failed to get lcd clock source\n");
  745. ret = -ENOENT;
  746. goto release_irq;
  747. }
  748. clk_enable(info->clk);
  749. dprintk("got and enabled clock\n");
  750. msleep(1);
  751. /* find maximum required memory size for display */
  752. for (i = 0; i < mach_info->num_displays; i++) {
  753. unsigned long smem_len = mach_info->displays[i].xres;
  754. smem_len *= mach_info->displays[i].yres;
  755. smem_len *= mach_info->displays[i].bpp;
  756. smem_len >>= 3;
  757. if (fbinfo->fix.smem_len < smem_len)
  758. fbinfo->fix.smem_len = smem_len;
  759. }
  760. /* Initialize video memory */
  761. ret = s3c2410fb_map_video_memory(fbinfo);
  762. if (ret) {
  763. printk(KERN_ERR "Failed to allocate video RAM: %d\n", ret);
  764. ret = -ENOMEM;
  765. goto release_clock;
  766. }
  767. dprintk("got video memory\n");
  768. fbinfo->var.xres = display->xres;
  769. fbinfo->var.yres = display->yres;
  770. fbinfo->var.bits_per_pixel = display->bpp;
  771. s3c2410fb_init_registers(fbinfo);
  772. s3c2410fb_check_var(&fbinfo->var, fbinfo);
  773. ret = register_framebuffer(fbinfo);
  774. if (ret < 0) {
  775. printk(KERN_ERR "Failed to register framebuffer device: %d\n",
  776. ret);
  777. goto free_video_memory;
  778. }
  779. /* create device files */
  780. device_create_file(&pdev->dev, &dev_attr_debug);
  781. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  782. fbinfo->node, fbinfo->fix.id);
  783. return 0;
  784. free_video_memory:
  785. s3c2410fb_unmap_video_memory(fbinfo);
  786. release_clock:
  787. clk_disable(info->clk);
  788. clk_put(info->clk);
  789. release_irq:
  790. free_irq(irq, info);
  791. release_regs:
  792. iounmap(info->io);
  793. release_mem:
  794. release_resource(info->mem);
  795. kfree(info->mem);
  796. dealloc_fb:
  797. platform_set_drvdata(pdev, NULL);
  798. framebuffer_release(fbinfo);
  799. return ret;
  800. }
  801. /* s3c2410fb_stop_lcd
  802. *
  803. * shutdown the lcd controller
  804. */
  805. static void s3c2410fb_stop_lcd(struct s3c2410fb_info *fbi)
  806. {
  807. unsigned long flags;
  808. local_irq_save(flags);
  809. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
  810. writel(fbi->regs.lcdcon1, fbi->io + S3C2410_LCDCON1);
  811. local_irq_restore(flags);
  812. }
  813. /*
  814. * Cleanup
  815. */
  816. static int s3c2410fb_remove(struct platform_device *pdev)
  817. {
  818. struct fb_info *fbinfo = platform_get_drvdata(pdev);
  819. struct s3c2410fb_info *info = fbinfo->par;
  820. int irq;
  821. unregister_framebuffer(fbinfo);
  822. s3c2410fb_stop_lcd(info);
  823. msleep(1);
  824. s3c2410fb_unmap_video_memory(fbinfo);
  825. if (info->clk) {
  826. clk_disable(info->clk);
  827. clk_put(info->clk);
  828. info->clk = NULL;
  829. }
  830. irq = platform_get_irq(pdev, 0);
  831. free_irq(irq, info);
  832. iounmap(info->io);
  833. release_resource(info->mem);
  834. kfree(info->mem);
  835. platform_set_drvdata(pdev, NULL);
  836. framebuffer_release(fbinfo);
  837. return 0;
  838. }
  839. #ifdef CONFIG_PM
  840. /* suspend and resume support for the lcd controller */
  841. static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
  842. {
  843. struct fb_info *fbinfo = platform_get_drvdata(dev);
  844. struct s3c2410fb_info *info = fbinfo->par;
  845. s3c2410fb_stop_lcd(info);
  846. /* sleep before disabling the clock, we need to ensure
  847. * the LCD DMA engine is not going to get back on the bus
  848. * before the clock goes off again (bjd) */
  849. msleep(1);
  850. clk_disable(info->clk);
  851. return 0;
  852. }
  853. static int s3c2410fb_resume(struct platform_device *dev)
  854. {
  855. struct fb_info *fbinfo = platform_get_drvdata(dev);
  856. struct s3c2410fb_info *info = fbinfo->par;
  857. clk_enable(info->clk);
  858. msleep(1);
  859. s3c2410fb_init_registers(info);
  860. return 0;
  861. }
  862. #else
  863. #define s3c2410fb_suspend NULL
  864. #define s3c2410fb_resume NULL
  865. #endif
  866. static struct platform_driver s3c2410fb_driver = {
  867. .probe = s3c2410fb_probe,
  868. .remove = s3c2410fb_remove,
  869. .suspend = s3c2410fb_suspend,
  870. .resume = s3c2410fb_resume,
  871. .driver = {
  872. .name = "s3c2410-lcd",
  873. .owner = THIS_MODULE,
  874. },
  875. };
  876. int __init s3c2410fb_init(void)
  877. {
  878. return platform_driver_register(&s3c2410fb_driver);
  879. }
  880. static void __exit s3c2410fb_cleanup(void)
  881. {
  882. platform_driver_unregister(&s3c2410fb_driver);
  883. }
  884. module_init(s3c2410fb_init);
  885. module_exit(s3c2410fb_cleanup);
  886. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
  887. "Ben Dooks <ben-linux@fluff.org>");
  888. MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
  889. MODULE_LICENSE("GPL");