s3c2410fb.c 28 KB

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