s3c2410fb.c 28 KB

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