s3c2410fb.c 27 KB

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