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/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/fb.h>
  21. #include <linux/init.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/clk.h>
  26. #include <linux/cpufreq.h>
  27. #include <asm/io.h>
  28. #include <asm/div64.h>
  29. #include <asm/mach/map.h>
  30. #include <mach/regs-lcd.h>
  31. #include <mach/regs-gpio.h>
  32. #include <mach/fb.h>
  33. #ifdef CONFIG_PM
  34. #include <linux/pm.h>
  35. #endif
  36. #include "s3c2410fb.h"
  37. /* Debugging stuff */
  38. #ifdef CONFIG_FB_S3C2410_DEBUG
  39. static int debug = 1;
  40. #else
  41. static int debug = 0;
  42. #endif
  43. #define dprintk(msg...) if (debug) { printk(KERN_DEBUG "s3c2410fb: " msg); }
  44. /* useful functions */
  45. static int is_s3c2412(struct s3c2410fb_info *fbi)
  46. {
  47. return (fbi->drv_type == DRV_S3C2412);
  48. }
  49. /* s3c2410fb_set_lcdaddr
  50. *
  51. * initialise lcd controller address pointers
  52. */
  53. static void s3c2410fb_set_lcdaddr(struct fb_info *info)
  54. {
  55. unsigned long saddr1, saddr2, saddr3;
  56. struct s3c2410fb_info *fbi = info->par;
  57. void __iomem *regs = fbi->io;
  58. saddr1 = info->fix.smem_start >> 1;
  59. saddr2 = info->fix.smem_start;
  60. saddr2 += info->fix.line_length * info->var.yres;
  61. saddr2 >>= 1;
  62. saddr3 = S3C2410_OFFSIZE(0) |
  63. S3C2410_PAGEWIDTH((info->fix.line_length / 2) & 0x3ff);
  64. dprintk("LCDSADDR1 = 0x%08lx\n", saddr1);
  65. dprintk("LCDSADDR2 = 0x%08lx\n", saddr2);
  66. dprintk("LCDSADDR3 = 0x%08lx\n", saddr3);
  67. writel(saddr1, regs + S3C2410_LCDSADDR1);
  68. writel(saddr2, regs + S3C2410_LCDSADDR2);
  69. writel(saddr3, regs + S3C2410_LCDSADDR3);
  70. }
  71. /* s3c2410fb_calc_pixclk()
  72. *
  73. * calculate divisor for clk->pixclk
  74. */
  75. static unsigned int s3c2410fb_calc_pixclk(struct s3c2410fb_info *fbi,
  76. unsigned long pixclk)
  77. {
  78. unsigned long clk = fbi->clk_rate;
  79. unsigned long long div;
  80. /* pixclk is in picoseconds, our clock is in Hz
  81. *
  82. * Hz -> picoseconds is / 10^-12
  83. */
  84. div = (unsigned long long)clk * pixclk;
  85. div >>= 12; /* div / 2^12 */
  86. do_div(div, 625 * 625UL * 625); /* div / 5^12 */
  87. dprintk("pixclk %ld, divisor is %ld\n", pixclk, (long)div);
  88. return div;
  89. }
  90. /*
  91. * s3c2410fb_check_var():
  92. * Get the video params out of 'var'. If a value doesn't fit, round it up,
  93. * if it's too big, return -EINVAL.
  94. *
  95. */
  96. static int s3c2410fb_check_var(struct fb_var_screeninfo *var,
  97. struct fb_info *info)
  98. {
  99. struct s3c2410fb_info *fbi = info->par;
  100. struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;
  101. struct s3c2410fb_display *display = NULL;
  102. struct s3c2410fb_display *default_display = mach_info->displays +
  103. mach_info->default_display;
  104. int type = default_display->type;
  105. unsigned i;
  106. dprintk("check_var(var=%p, info=%p)\n", var, info);
  107. /* validate x/y resolution */
  108. /* choose default mode if possible */
  109. if (var->yres == default_display->yres &&
  110. var->xres == default_display->xres &&
  111. var->bits_per_pixel == default_display->bpp)
  112. display = default_display;
  113. else
  114. for (i = 0; i < mach_info->num_displays; i++)
  115. if (type == mach_info->displays[i].type &&
  116. var->yres == mach_info->displays[i].yres &&
  117. var->xres == mach_info->displays[i].xres &&
  118. var->bits_per_pixel == mach_info->displays[i].bpp) {
  119. display = mach_info->displays + i;
  120. break;
  121. }
  122. if (!display) {
  123. dprintk("wrong resolution or depth %dx%d at %d bpp\n",
  124. var->xres, var->yres, var->bits_per_pixel);
  125. return -EINVAL;
  126. }
  127. /* it is always the size as the display */
  128. var->xres_virtual = display->xres;
  129. var->yres_virtual = display->yres;
  130. var->height = display->height;
  131. var->width = display->width;
  132. /* copy lcd settings */
  133. var->pixclock = display->pixclock;
  134. var->left_margin = display->left_margin;
  135. var->right_margin = display->right_margin;
  136. var->upper_margin = display->upper_margin;
  137. var->lower_margin = display->lower_margin;
  138. var->vsync_len = display->vsync_len;
  139. var->hsync_len = display->hsync_len;
  140. fbi->regs.lcdcon5 = display->lcdcon5;
  141. /* set display type */
  142. fbi->regs.lcdcon1 = display->type;
  143. var->transp.offset = 0;
  144. var->transp.length = 0;
  145. /* set r/g/b positions */
  146. switch (var->bits_per_pixel) {
  147. case 1:
  148. case 2:
  149. case 4:
  150. var->red.offset = 0;
  151. var->red.length = var->bits_per_pixel;
  152. var->green = var->red;
  153. var->blue = var->red;
  154. break;
  155. case 8:
  156. if (display->type != S3C2410_LCDCON1_TFT) {
  157. /* 8 bpp 332 */
  158. var->red.length = 3;
  159. var->red.offset = 5;
  160. var->green.length = 3;
  161. var->green.offset = 2;
  162. var->blue.length = 2;
  163. var->blue.offset = 0;
  164. } else {
  165. var->red.offset = 0;
  166. var->red.length = 8;
  167. var->green = var->red;
  168. var->blue = var->red;
  169. }
  170. break;
  171. case 12:
  172. /* 12 bpp 444 */
  173. var->red.length = 4;
  174. var->red.offset = 8;
  175. var->green.length = 4;
  176. var->green.offset = 4;
  177. var->blue.length = 4;
  178. var->blue.offset = 0;
  179. break;
  180. default:
  181. case 16:
  182. if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {
  183. /* 16 bpp, 565 format */
  184. var->red.offset = 11;
  185. var->green.offset = 5;
  186. var->blue.offset = 0;
  187. var->red.length = 5;
  188. var->green.length = 6;
  189. var->blue.length = 5;
  190. } else {
  191. /* 16 bpp, 5551 format */
  192. var->red.offset = 11;
  193. var->green.offset = 6;
  194. var->blue.offset = 1;
  195. var->red.length = 5;
  196. var->green.length = 5;
  197. var->blue.length = 5;
  198. }
  199. break;
  200. case 32:
  201. /* 24 bpp 888 and 8 dummy */
  202. var->red.length = 8;
  203. var->red.offset = 16;
  204. var->green.length = 8;
  205. var->green.offset = 8;
  206. var->blue.length = 8;
  207. var->blue.offset = 0;
  208. break;
  209. }
  210. return 0;
  211. }
  212. /* s3c2410fb_calculate_stn_lcd_regs
  213. *
  214. * calculate register values from var settings
  215. */
  216. static void s3c2410fb_calculate_stn_lcd_regs(const struct fb_info *info,
  217. struct s3c2410fb_hw *regs)
  218. {
  219. const struct s3c2410fb_info *fbi = info->par;
  220. const struct fb_var_screeninfo *var = &info->var;
  221. int type = regs->lcdcon1 & ~S3C2410_LCDCON1_TFT;
  222. int hs = var->xres >> 2;
  223. unsigned wdly = (var->left_margin >> 4) - 1;
  224. unsigned wlh = (var->hsync_len >> 4) - 1;
  225. if (type != S3C2410_LCDCON1_STN4)
  226. hs >>= 1;
  227. switch (var->bits_per_pixel) {
  228. case 1:
  229. regs->lcdcon1 |= S3C2410_LCDCON1_STN1BPP;
  230. break;
  231. case 2:
  232. regs->lcdcon1 |= S3C2410_LCDCON1_STN2GREY;
  233. break;
  234. case 4:
  235. regs->lcdcon1 |= S3C2410_LCDCON1_STN4GREY;
  236. break;
  237. case 8:
  238. regs->lcdcon1 |= S3C2410_LCDCON1_STN8BPP;
  239. hs *= 3;
  240. break;
  241. case 12:
  242. regs->lcdcon1 |= S3C2410_LCDCON1_STN12BPP;
  243. hs *= 3;
  244. break;
  245. default:
  246. /* invalid pixel depth */
  247. dev_err(fbi->dev, "invalid bpp %d\n",
  248. var->bits_per_pixel);
  249. }
  250. /* update X/Y info */
  251. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  252. var->left_margin, var->right_margin, var->hsync_len);
  253. regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1);
  254. if (wdly > 3)
  255. wdly = 3;
  256. if (wlh > 3)
  257. wlh = 3;
  258. regs->lcdcon3 = S3C2410_LCDCON3_WDLY(wdly) |
  259. S3C2410_LCDCON3_LINEBLANK(var->right_margin / 8) |
  260. S3C2410_LCDCON3_HOZVAL(hs - 1);
  261. regs->lcdcon4 = S3C2410_LCDCON4_WLH(wlh);
  262. }
  263. /* s3c2410fb_calculate_tft_lcd_regs
  264. *
  265. * calculate register values from var settings
  266. */
  267. static void s3c2410fb_calculate_tft_lcd_regs(const struct fb_info *info,
  268. struct s3c2410fb_hw *regs)
  269. {
  270. const struct s3c2410fb_info *fbi = info->par;
  271. const struct fb_var_screeninfo *var = &info->var;
  272. switch (var->bits_per_pixel) {
  273. case 1:
  274. regs->lcdcon1 |= S3C2410_LCDCON1_TFT1BPP;
  275. break;
  276. case 2:
  277. regs->lcdcon1 |= S3C2410_LCDCON1_TFT2BPP;
  278. break;
  279. case 4:
  280. regs->lcdcon1 |= S3C2410_LCDCON1_TFT4BPP;
  281. break;
  282. case 8:
  283. regs->lcdcon1 |= S3C2410_LCDCON1_TFT8BPP;
  284. regs->lcdcon5 |= S3C2410_LCDCON5_BSWP |
  285. S3C2410_LCDCON5_FRM565;
  286. regs->lcdcon5 &= ~S3C2410_LCDCON5_HWSWP;
  287. break;
  288. case 16:
  289. regs->lcdcon1 |= S3C2410_LCDCON1_TFT16BPP;
  290. regs->lcdcon5 &= ~S3C2410_LCDCON5_BSWP;
  291. regs->lcdcon5 |= S3C2410_LCDCON5_HWSWP;
  292. break;
  293. case 32:
  294. regs->lcdcon1 |= S3C2410_LCDCON1_TFT24BPP;
  295. regs->lcdcon5 &= ~(S3C2410_LCDCON5_BSWP |
  296. S3C2410_LCDCON5_HWSWP |
  297. S3C2410_LCDCON5_BPP24BL);
  298. break;
  299. default:
  300. /* invalid pixel depth */
  301. dev_err(fbi->dev, "invalid bpp %d\n",
  302. var->bits_per_pixel);
  303. }
  304. /* update X/Y info */
  305. dprintk("setting vert: up=%d, low=%d, sync=%d\n",
  306. var->upper_margin, var->lower_margin, var->vsync_len);
  307. dprintk("setting horz: lft=%d, rt=%d, sync=%d\n",
  308. var->left_margin, var->right_margin, var->hsync_len);
  309. regs->lcdcon2 = S3C2410_LCDCON2_LINEVAL(var->yres - 1) |
  310. S3C2410_LCDCON2_VBPD(var->upper_margin - 1) |
  311. S3C2410_LCDCON2_VFPD(var->lower_margin - 1) |
  312. S3C2410_LCDCON2_VSPW(var->vsync_len - 1);
  313. regs->lcdcon3 = S3C2410_LCDCON3_HBPD(var->right_margin - 1) |
  314. S3C2410_LCDCON3_HFPD(var->left_margin - 1) |
  315. S3C2410_LCDCON3_HOZVAL(var->xres - 1);
  316. regs->lcdcon4 = S3C2410_LCDCON4_HSPW(var->hsync_len - 1);
  317. }
  318. /* s3c2410fb_activate_var
  319. *
  320. * activate (set) the controller from the given framebuffer
  321. * information
  322. */
  323. static void s3c2410fb_activate_var(struct fb_info *info)
  324. {
  325. struct s3c2410fb_info *fbi = info->par;
  326. void __iomem *regs = fbi->io;
  327. int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;
  328. struct fb_var_screeninfo *var = &info->var;
  329. int clkdiv;
  330. clkdiv = DIV_ROUND_UP(s3c2410fb_calc_pixclk(fbi, var->pixclock), 2);
  331. dprintk("%s: var->xres = %d\n", __func__, var->xres);
  332. dprintk("%s: var->yres = %d\n", __func__, var->yres);
  333. dprintk("%s: var->bpp = %d\n", __func__, var->bits_per_pixel);
  334. if (type == S3C2410_LCDCON1_TFT) {
  335. s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);
  336. --clkdiv;
  337. if (clkdiv < 0)
  338. clkdiv = 0;
  339. } else {
  340. s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);
  341. if (clkdiv < 2)
  342. clkdiv = 2;
  343. }
  344. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_CLKVAL(clkdiv);
  345. /* write new registers */
  346. dprintk("new register set:\n");
  347. dprintk("lcdcon[1] = 0x%08lx\n", fbi->regs.lcdcon1);
  348. dprintk("lcdcon[2] = 0x%08lx\n", fbi->regs.lcdcon2);
  349. dprintk("lcdcon[3] = 0x%08lx\n", fbi->regs.lcdcon3);
  350. dprintk("lcdcon[4] = 0x%08lx\n", fbi->regs.lcdcon4);
  351. dprintk("lcdcon[5] = 0x%08lx\n", fbi->regs.lcdcon5);
  352. writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID,
  353. regs + S3C2410_LCDCON1);
  354. writel(fbi->regs.lcdcon2, regs + S3C2410_LCDCON2);
  355. writel(fbi->regs.lcdcon3, regs + S3C2410_LCDCON3);
  356. writel(fbi->regs.lcdcon4, regs + S3C2410_LCDCON4);
  357. writel(fbi->regs.lcdcon5, regs + S3C2410_LCDCON5);
  358. /* set lcd address pointers */
  359. s3c2410fb_set_lcdaddr(info);
  360. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,
  361. writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);
  362. }
  363. /*
  364. * s3c2410fb_set_par - Alters the hardware state.
  365. * @info: frame buffer structure that represents a single frame buffer
  366. *
  367. */
  368. static int s3c2410fb_set_par(struct fb_info *info)
  369. {
  370. struct fb_var_screeninfo *var = &info->var;
  371. switch (var->bits_per_pixel) {
  372. case 32:
  373. case 16:
  374. case 12:
  375. info->fix.visual = FB_VISUAL_TRUECOLOR;
  376. break;
  377. case 1:
  378. info->fix.visual = FB_VISUAL_MONO01;
  379. break;
  380. default:
  381. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  382. break;
  383. }
  384. info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
  385. /* activate this new configuration */
  386. s3c2410fb_activate_var(info);
  387. return 0;
  388. }
  389. static void schedule_palette_update(struct s3c2410fb_info *fbi,
  390. unsigned int regno, unsigned int val)
  391. {
  392. unsigned long flags;
  393. unsigned long irqen;
  394. void __iomem *irq_base = fbi->irq_base;
  395. local_irq_save(flags);
  396. fbi->palette_buffer[regno] = val;
  397. if (!fbi->palette_ready) {
  398. fbi->palette_ready = 1;
  399. /* enable IRQ */
  400. irqen = readl(irq_base + S3C24XX_LCDINTMSK);
  401. irqen &= ~S3C2410_LCDINT_FRSYNC;
  402. writel(irqen, irq_base + S3C24XX_LCDINTMSK);
  403. }
  404. local_irq_restore(flags);
  405. }
  406. /* from pxafb.c */
  407. static inline unsigned int chan_to_field(unsigned int chan,
  408. struct fb_bitfield *bf)
  409. {
  410. chan &= 0xffff;
  411. chan >>= 16 - bf->length;
  412. return chan << bf->offset;
  413. }
  414. static int s3c2410fb_setcolreg(unsigned regno,
  415. unsigned red, unsigned green, unsigned blue,
  416. unsigned transp, struct fb_info *info)
  417. {
  418. struct s3c2410fb_info *fbi = info->par;
  419. void __iomem *regs = fbi->io;
  420. unsigned int val;
  421. /* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n",
  422. regno, red, green, blue); */
  423. switch (info->fix.visual) {
  424. case FB_VISUAL_TRUECOLOR:
  425. /* true-colour, use pseudo-palette */
  426. if (regno < 16) {
  427. u32 *pal = info->pseudo_palette;
  428. val = chan_to_field(red, &info->var.red);
  429. val |= chan_to_field(green, &info->var.green);
  430. val |= chan_to_field(blue, &info->var.blue);
  431. pal[regno] = val;
  432. }
  433. break;
  434. case FB_VISUAL_PSEUDOCOLOR:
  435. if (regno < 256) {
  436. /* currently assume RGB 5-6-5 mode */
  437. val = (red >> 0) & 0xf800;
  438. val |= (green >> 5) & 0x07e0;
  439. val |= (blue >> 11) & 0x001f;
  440. writel(val, regs + S3C2410_TFTPAL(regno));
  441. schedule_palette_update(fbi, regno, val);
  442. }
  443. break;
  444. default:
  445. return 1; /* unknown type */
  446. }
  447. return 0;
  448. }
  449. /* s3c2410fb_lcd_enable
  450. *
  451. * shutdown the lcd controller
  452. */
  453. static void s3c2410fb_lcd_enable(struct s3c2410fb_info *fbi, int enable)
  454. {
  455. unsigned long flags;
  456. local_irq_save(flags);
  457. if (enable)
  458. fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID;
  459. else
  460. fbi->regs.lcdcon1 &= ~S3C2410_LCDCON1_ENVID;
  461. writel(fbi->regs.lcdcon1, fbi->io + S3C2410_LCDCON1);
  462. local_irq_restore(flags);
  463. }
  464. /*
  465. * s3c2410fb_blank
  466. * @blank_mode: the blank mode we want.
  467. * @info: frame buffer structure that represents a single frame buffer
  468. *
  469. * Blank the screen if blank_mode != 0, else unblank. Return 0 if
  470. * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
  471. * video mode which doesn't support it. Implements VESA suspend
  472. * and powerdown modes on hardware that supports disabling hsync/vsync:
  473. *
  474. * Returns negative errno on error, or zero on success.
  475. *
  476. */
  477. static int s3c2410fb_blank(int blank_mode, struct fb_info *info)
  478. {
  479. struct s3c2410fb_info *fbi = info->par;
  480. void __iomem *tpal_reg = fbi->io;
  481. dprintk("blank(mode=%d, info=%p)\n", blank_mode, info);
  482. tpal_reg += is_s3c2412(fbi) ? S3C2412_TPAL : S3C2410_TPAL;
  483. if (blank_mode == FB_BLANK_POWERDOWN) {
  484. s3c2410fb_lcd_enable(fbi, 0);
  485. } else {
  486. s3c2410fb_lcd_enable(fbi, 1);
  487. }
  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 __init 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 cpufreq_freqs *freqs = data;
  642. struct s3c2410fb_info *info;
  643. struct fb_info *fbinfo;
  644. long delta_f;
  645. info = container_of(nb, struct s3c2410fb_info, freq_transition);
  646. fbinfo = platform_get_drvdata(to_platform_device(info->dev));
  647. /* work out change, <0 for speed-up */
  648. delta_f = info->clk_rate - clk_get_rate(info->clk);
  649. if ((val == CPUFREQ_POSTCHANGE && delta_f > 0) ||
  650. (val == CPUFREQ_PRECHANGE && delta_f < 0)) {
  651. info->clk_rate = clk_get_rate(info->clk);
  652. s3c2410fb_activate_var(fbinfo);
  653. }
  654. return 0;
  655. }
  656. static inline int s3c2410fb_cpufreq_register(struct s3c2410fb_info *info)
  657. {
  658. info->freq_transition.notifier_call = s3c2410fb_cpufreq_transition;
  659. return cpufreq_register_notifier(&info->freq_transition,
  660. CPUFREQ_TRANSITION_NOTIFIER);
  661. }
  662. static inline void s3c2410fb_cpufreq_deregister(struct s3c2410fb_info *info)
  663. {
  664. cpufreq_unregister_notifier(&info->freq_transition,
  665. CPUFREQ_TRANSITION_NOTIFIER);
  666. }
  667. #else
  668. static inline int s3c2410fb_cpufreq_register(struct s3c2410fb_info *info)
  669. {
  670. return 0;
  671. }
  672. static inline void s3c2410fb_cpufreq_deregister(struct s3c2410fb_info *info)
  673. {
  674. }
  675. #endif
  676. static char driver_name[] = "s3c2410fb";
  677. static int __init s3c24xxfb_probe(struct platform_device *pdev,
  678. enum s3c_drv_type drv_type)
  679. {
  680. struct s3c2410fb_info *info;
  681. struct s3c2410fb_display *display;
  682. struct fb_info *fbinfo;
  683. struct s3c2410fb_mach_info *mach_info;
  684. struct resource *res;
  685. int ret;
  686. int irq;
  687. int i;
  688. int size;
  689. u32 lcdcon1;
  690. mach_info = pdev->dev.platform_data;
  691. if (mach_info == NULL) {
  692. dev_err(&pdev->dev,
  693. "no platform data for lcd, cannot attach\n");
  694. return -EINVAL;
  695. }
  696. if (mach_info->default_display >= mach_info->num_displays) {
  697. dev_err(&pdev->dev, "default is %d but only %d displays\n",
  698. mach_info->default_display, mach_info->num_displays);
  699. return -EINVAL;
  700. }
  701. display = mach_info->displays + mach_info->default_display;
  702. irq = platform_get_irq(pdev, 0);
  703. if (irq < 0) {
  704. dev_err(&pdev->dev, "no irq for device\n");
  705. return -ENOENT;
  706. }
  707. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
  708. if (!fbinfo)
  709. return -ENOMEM;
  710. platform_set_drvdata(pdev, fbinfo);
  711. info = fbinfo->par;
  712. info->dev = &pdev->dev;
  713. info->drv_type = drv_type;
  714. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  715. if (res == NULL) {
  716. dev_err(&pdev->dev, "failed to get memory registers\n");
  717. ret = -ENXIO;
  718. goto dealloc_fb;
  719. }
  720. size = (res->end - res->start) + 1;
  721. info->mem = request_mem_region(res->start, size, pdev->name);
  722. if (info->mem == NULL) {
  723. dev_err(&pdev->dev, "failed to get memory region\n");
  724. ret = -ENOENT;
  725. goto dealloc_fb;
  726. }
  727. info->io = ioremap(res->start, size);
  728. if (info->io == NULL) {
  729. dev_err(&pdev->dev, "ioremap() of registers failed\n");
  730. ret = -ENXIO;
  731. goto release_mem;
  732. }
  733. info->irq_base = info->io + ((drv_type == DRV_S3C2412) ? S3C2412_LCDINTBASE : S3C2410_LCDINTBASE);
  734. dprintk("devinit\n");
  735. strcpy(fbinfo->fix.id, driver_name);
  736. /* Stop the video */
  737. lcdcon1 = readl(info->io + S3C2410_LCDCON1);
  738. writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);
  739. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  740. fbinfo->fix.type_aux = 0;
  741. fbinfo->fix.xpanstep = 0;
  742. fbinfo->fix.ypanstep = 0;
  743. fbinfo->fix.ywrapstep = 0;
  744. fbinfo->fix.accel = FB_ACCEL_NONE;
  745. fbinfo->var.nonstd = 0;
  746. fbinfo->var.activate = FB_ACTIVATE_NOW;
  747. fbinfo->var.accel_flags = 0;
  748. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  749. fbinfo->fbops = &s3c2410fb_ops;
  750. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  751. fbinfo->pseudo_palette = &info->pseudo_pal;
  752. for (i = 0; i < 256; i++)
  753. info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  754. ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
  755. if (ret) {
  756. dev_err(&pdev->dev, "cannot get irq %d - err %d\n", irq, ret);
  757. ret = -EBUSY;
  758. goto release_regs;
  759. }
  760. info->clk = clk_get(NULL, "lcd");
  761. if (!info->clk || IS_ERR(info->clk)) {
  762. printk(KERN_ERR "failed to get lcd clock source\n");
  763. ret = -ENOENT;
  764. goto release_irq;
  765. }
  766. clk_enable(info->clk);
  767. dprintk("got and enabled clock\n");
  768. msleep(1);
  769. info->clk_rate = clk_get_rate(info->clk);
  770. /* find maximum required memory size for display */
  771. for (i = 0; i < mach_info->num_displays; i++) {
  772. unsigned long smem_len = mach_info->displays[i].xres;
  773. smem_len *= mach_info->displays[i].yres;
  774. smem_len *= mach_info->displays[i].bpp;
  775. smem_len >>= 3;
  776. if (fbinfo->fix.smem_len < smem_len)
  777. fbinfo->fix.smem_len = smem_len;
  778. }
  779. /* Initialize video memory */
  780. ret = s3c2410fb_map_video_memory(fbinfo);
  781. if (ret) {
  782. printk(KERN_ERR "Failed to allocate video RAM: %d\n", ret);
  783. ret = -ENOMEM;
  784. goto release_clock;
  785. }
  786. dprintk("got video memory\n");
  787. fbinfo->var.xres = display->xres;
  788. fbinfo->var.yres = display->yres;
  789. fbinfo->var.bits_per_pixel = display->bpp;
  790. s3c2410fb_init_registers(fbinfo);
  791. s3c2410fb_check_var(&fbinfo->var, fbinfo);
  792. ret = s3c2410fb_cpufreq_register(info);
  793. if (ret < 0) {
  794. dev_err(&pdev->dev, "Failed to register cpufreq\n");
  795. goto free_video_memory;
  796. }
  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_cpufreq;
  802. }
  803. /* create device files */
  804. ret = device_create_file(&pdev->dev, &dev_attr_debug);
  805. if (ret) {
  806. printk(KERN_ERR "failed to add debug attribute\n");
  807. }
  808. printk(KERN_INFO "fb%d: %s frame buffer device\n",
  809. fbinfo->node, fbinfo->fix.id);
  810. return 0;
  811. free_cpufreq:
  812. s3c2410fb_cpufreq_deregister(info);
  813. free_video_memory:
  814. s3c2410fb_unmap_video_memory(fbinfo);
  815. release_clock:
  816. clk_disable(info->clk);
  817. clk_put(info->clk);
  818. release_irq:
  819. free_irq(irq, info);
  820. release_regs:
  821. iounmap(info->io);
  822. release_mem:
  823. release_resource(info->mem);
  824. kfree(info->mem);
  825. dealloc_fb:
  826. platform_set_drvdata(pdev, NULL);
  827. framebuffer_release(fbinfo);
  828. return ret;
  829. }
  830. static int __init s3c2410fb_probe(struct platform_device *pdev)
  831. {
  832. return s3c24xxfb_probe(pdev, DRV_S3C2410);
  833. }
  834. static int __init s3c2412fb_probe(struct platform_device *pdev)
  835. {
  836. return s3c24xxfb_probe(pdev, DRV_S3C2412);
  837. }
  838. /*
  839. * Cleanup
  840. */
  841. static int 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. msleep(1);
  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_resource(info->mem);
  860. kfree(info->mem);
  861. platform_set_drvdata(pdev, NULL);
  862. framebuffer_release(fbinfo);
  863. return 0;
  864. }
  865. #ifdef CONFIG_PM
  866. /* suspend and resume support for the lcd controller */
  867. static int s3c2410fb_suspend(struct platform_device *dev, pm_message_t state)
  868. {
  869. struct fb_info *fbinfo = platform_get_drvdata(dev);
  870. struct s3c2410fb_info *info = fbinfo->par;
  871. s3c2410fb_lcd_enable(info, 0);
  872. /* sleep before disabling the clock, we need to ensure
  873. * the LCD DMA engine is not going to get back on the bus
  874. * before the clock goes off again (bjd) */
  875. msleep(1);
  876. clk_disable(info->clk);
  877. return 0;
  878. }
  879. static int s3c2410fb_resume(struct platform_device *dev)
  880. {
  881. struct fb_info *fbinfo = platform_get_drvdata(dev);
  882. struct s3c2410fb_info *info = fbinfo->par;
  883. clk_enable(info->clk);
  884. msleep(1);
  885. s3c2410fb_init_registers(fbinfo);
  886. /* re-activate our display after resume */
  887. s3c2410fb_activate_var(fbinfo);
  888. s3c2410fb_blank(FB_BLANK_UNBLANK, fbinfo);
  889. return 0;
  890. }
  891. #else
  892. #define s3c2410fb_suspend NULL
  893. #define s3c2410fb_resume NULL
  894. #endif
  895. static struct platform_driver s3c2410fb_driver = {
  896. .probe = s3c2410fb_probe,
  897. .remove = s3c2410fb_remove,
  898. .suspend = s3c2410fb_suspend,
  899. .resume = s3c2410fb_resume,
  900. .driver = {
  901. .name = "s3c2410-lcd",
  902. .owner = THIS_MODULE,
  903. },
  904. };
  905. static struct platform_driver s3c2412fb_driver = {
  906. .probe = s3c2412fb_probe,
  907. .remove = s3c2410fb_remove,
  908. .suspend = s3c2410fb_suspend,
  909. .resume = s3c2410fb_resume,
  910. .driver = {
  911. .name = "s3c2412-lcd",
  912. .owner = THIS_MODULE,
  913. },
  914. };
  915. int __init s3c2410fb_init(void)
  916. {
  917. int ret = platform_driver_register(&s3c2410fb_driver);
  918. if (ret == 0)
  919. ret = platform_driver_register(&s3c2412fb_driver);
  920. return ret;
  921. }
  922. static void __exit s3c2410fb_cleanup(void)
  923. {
  924. platform_driver_unregister(&s3c2410fb_driver);
  925. platform_driver_unregister(&s3c2412fb_driver);
  926. }
  927. module_init(s3c2410fb_init);
  928. module_exit(s3c2410fb_cleanup);
  929. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>, "
  930. "Ben Dooks <ben-linux@fluff.org>");
  931. MODULE_DESCRIPTION("Framebuffer driver for the s3c2410");
  932. MODULE_LICENSE("GPL");
  933. MODULE_ALIAS("platform:s3c2410-lcd");
  934. MODULE_ALIAS("platform:s3c2412-lcd");