s3c2410fb.c 27 KB

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