s3c2410fb.c 26 KB

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