clps711xfb.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:12k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/drivers/video/clps711xfb.c
  3.  *
  4.  *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  *  Framebuffer driver for the CLPS7111 and EP7212 processors.
  21.  */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/fb.h>
  26. #include <linux/init.h>
  27. #include <linux/proc_fs.h>
  28. #include <video/fbcon.h>
  29. #include <video/fbcon-cfb4.h>
  30. #include <asm/hardware.h>
  31. #include <asm/mach-types.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/hardware/clps7111.h>
  34. #include <asm/arch/syspld.h>
  35. static struct clps7111fb_info {
  36. struct fb_info fb;
  37. int currcon;
  38. } *cfb;
  39. #define CMAP_SIZE 16
  40. /* The /proc entry for the backlight. */
  41. static struct proc_dir_entry *clps7111fb_backlight_proc_entry = NULL;
  42. static int clps7111fb_proc_backlight_read(char *page, char **start, off_t off,
  43. int count, int *eof, void *data);
  44. static int clps7111fb_proc_backlight_write(struct file *file, 
  45. const char *buffer, unsigned long count, void *data);
  46. /*
  47.  *    Set a single color register. Return != 0 for invalid regno.
  48.  */
  49. static int
  50. clps7111fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  51.      u_int transp, struct fb_info *info)
  52. {
  53. unsigned int level, mask, shift, pal;
  54. if (regno >= CMAP_SIZE)
  55. return 1;
  56. /* gray = 0.30*R + 0.58*G + 0.11*B */
  57. level = (red * 77 + green * 151 + blue * 28) >> 20;
  58. /*
  59.  * On an LCD, a high value is dark, while a low value is light. 
  60.  * So we invert the level.
  61.  *
  62.  * This isn't true on all machines, so we only do it on EDB7211.
  63.  *  --rmk
  64.  */
  65. if (machine_is_edb7211()) {
  66. level = 15 - level;
  67. }
  68. shift = 4 * (regno & 7);
  69. level <<= shift;
  70. mask  = 15 << shift;
  71. level &= mask;
  72. regno = regno < 8 ? PALLSW : PALMSW;
  73. pal = clps_readl(regno);
  74. pal = (pal & ~mask) | level;
  75. clps_writel(pal, regno);
  76. return 0;
  77. }
  78.     
  79. /*
  80.  * Set the colormap
  81.  */
  82. static int
  83. clps7111fb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
  84.     struct fb_info *info)
  85. {
  86. struct clps7111fb_info *cfb = (struct clps7111fb_info *)info;
  87. struct fb_cmap *dcmap = &fb_display[con].cmap;
  88. int err = 0;
  89. /* no colormap allocated? */
  90. if (!dcmap->len)
  91. err = fb_alloc_cmap(dcmap, CMAP_SIZE, 0);
  92. if (!err && con == cfb->currcon) {
  93. err = fb_set_cmap(cmap, kspc, clps7111fb_setcolreg, &cfb->fb);
  94. dcmap = &cfb->fb.cmap;
  95. }
  96. if (!err)
  97. fb_copy_cmap(cmap, dcmap, kspc ? 0 : 1);
  98. return err;
  99. }
  100. /*
  101.  *    Set the User Defined Part of the Display
  102.  */
  103. static int
  104. clps7111fb_set_var(struct fb_var_screeninfo *var, int con,
  105.    struct fb_info *info)
  106. {
  107. struct display *display;
  108. unsigned int lcdcon, syscon;
  109. int chgvar = 0;
  110. if (var->activate & FB_ACTIVATE_TEST)
  111. return 0;
  112. if ((var->activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW)
  113. return -EINVAL;
  114. if (cfb->fb.var.xres != var->xres)
  115. chgvar = 1;
  116. if (cfb->fb.var.yres != var->yres)
  117. chgvar = 1;
  118. if (cfb->fb.var.xres_virtual != var->xres_virtual)
  119. chgvar = 1;
  120. if (cfb->fb.var.yres_virtual != var->yres_virtual)
  121. chgvar = 1;
  122. if (cfb->fb.var.bits_per_pixel != var->bits_per_pixel)
  123. chgvar = 1;
  124. if (con < 0) {
  125. display = cfb->fb.disp;
  126. chgvar = 0;
  127. } else {
  128. display = fb_display + con;
  129. }
  130. var->transp.msb_right = 0;
  131. var->transp.offset = 0;
  132. var->transp.length = 0;
  133. var->red.msb_right = 0;
  134. var->red.offset = 0;
  135. var->red.length = var->bits_per_pixel;
  136. var->green = var->red;
  137. var->blue = var->red;
  138. switch (var->bits_per_pixel) {
  139. #ifdef FBCON_HAS_MFB
  140. case 1:
  141. cfb->fb.fix.visual = FB_VISUAL_MONO01;
  142. display->dispsw = &fbcon_mfb;
  143. display->dispsw_data = NULL;
  144. break;
  145. #endif
  146. #ifdef FBCON_HAS_CFB2
  147. case 2:
  148. cfb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  149. display->dispsw = &fbcon_cfb2;
  150. display->dispsw_data = NULL;
  151. break;
  152. #endif
  153. #ifdef FBCON_HAS_CFB4
  154. case 4:
  155. cfb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  156. display->dispsw = &fbcon_cfb4;
  157. display->dispsw_data = NULL;
  158. break;
  159. #endif
  160. default:
  161. return -EINVAL;
  162. }
  163. display->next_line = var->xres_virtual * var->bits_per_pixel / 8;
  164. cfb->fb.fix.line_length = display->next_line;
  165. display->screen_base = cfb->fb.screen_base;
  166. display->line_length = cfb->fb.fix.line_length;
  167. display->visual = cfb->fb.fix.visual;
  168. display->type = cfb->fb.fix.type;
  169. display->type_aux = cfb->fb.fix.type_aux;
  170. display->ypanstep = cfb->fb.fix.ypanstep;
  171. display->ywrapstep = cfb->fb.fix.ywrapstep;
  172. display->can_soft_blank = 1;
  173. display->inverse = 0;
  174. cfb->fb.var = *var;
  175. cfb->fb.var.activate &= ~FB_ACTIVATE_ALL;
  176. /*
  177.  * Update the old var.  The fbcon drivers still use this.
  178.  * Once they are using cfb->fb.var, this can be dropped.
  179.  *                                      --rmk
  180.  */
  181. display->var = cfb->fb.var;
  182. /*
  183.  * If we are setting all the virtual consoles, also set the
  184.  * defaults used to create new consoles.
  185.  */
  186. if (var->activate & FB_ACTIVATE_ALL)
  187. cfb->fb.disp->var = cfb->fb.var;
  188. if (chgvar && info && cfb->fb.changevar)
  189. cfb->fb.changevar(con);
  190. /*
  191.  * LCDCON must only be changed while the LCD is disabled
  192.  */
  193. lcdcon = (var->xres_virtual * var->yres_virtual * var->bits_per_pixel) / 128 - 1;
  194. lcdcon |= ((var->xres_virtual / 16) - 1) << 13;
  195. lcdcon |= 2 << 19;
  196. lcdcon |= 13 << 25;
  197. lcdcon |= LCDCON_GSEN;
  198. lcdcon |= LCDCON_GSMD;
  199. syscon = clps_readl(SYSCON1);
  200. clps_writel(syscon & ~SYSCON1_LCDEN, SYSCON1);
  201. clps_writel(lcdcon, LCDCON);
  202. clps_writel(syscon | SYSCON1_LCDEN, SYSCON1);
  203. fb_set_cmap(&cfb->fb.cmap, 1, clps7111fb_setcolreg, &cfb->fb);
  204. return 0;
  205. }
  206. /*
  207.  * Get the currently displayed virtual consoles colormap.
  208.  */
  209. static int
  210. gen_get_cmap(struct fb_cmap *cmap, int kspc, int con, struct fb_info *info)
  211. {
  212. fb_copy_cmap(&info->cmap, cmap, kspc ? 0 : 2);
  213. return 0;
  214. }
  215. /*
  216.  * Get the currently displayed virtual consoles fixed part of the display.
  217.  */
  218. static int
  219. gen_get_fix(struct fb_fix_screeninfo *fix, int con, struct fb_info *info)
  220. {
  221. *fix = info->fix;
  222. return 0;
  223. }
  224. /*
  225.  * Get the current user defined part of the display.
  226.  */
  227. static int
  228. gen_get_var(struct fb_var_screeninfo *var, int con, struct fb_info *info)
  229. {
  230. *var = info->var;
  231. return 0;
  232. }
  233. static struct fb_ops clps7111fb_ops = {
  234. owner: THIS_MODULE,
  235. fb_set_var: clps7111fb_set_var,
  236. fb_set_cmap: clps7111fb_set_cmap,
  237. fb_get_fix: gen_get_fix,
  238. fb_get_var: gen_get_var,
  239. fb_get_cmap: gen_get_cmap,
  240. };
  241. static int clps7111fb_switch(int con, struct fb_info *info)
  242. {
  243. struct clps7111fb_info *cfb = (struct clps7111fb_info *)info;
  244. struct display *disp;
  245. struct fb_cmap *cmap;
  246. if (cfb->currcon >= 0) {
  247. disp = fb_display + cfb->currcon;
  248. /*
  249.  * Save the old colormap and video mode.
  250.  */
  251. disp->var = cfb->fb.var;
  252. if (disp->cmap.len)
  253. fb_copy_cmap(&cfb->fb.cmap, &disp->cmap, 0);
  254. }
  255. cfb->currcon = con;
  256. disp = fb_display + con;
  257. /*
  258.  * Install the new colormap and change the video mode.  By default,
  259.  * fbcon sets all the colormaps and video modes to the default
  260.  * values at bootup.
  261.  */
  262. if (disp->cmap.len)
  263. cmap = &disp->cmap;
  264. else
  265. cmap = fb_default_cmap(CMAP_SIZE);
  266. fb_copy_cmap(cmap, &cfb->fb.cmap, 0);
  267. cfb->fb.var = disp->var;
  268. cfb->fb.var.activate = FB_ACTIVATE_NOW;
  269. clps7111fb_set_var(&cfb->fb.var, con, &cfb->fb);
  270. return 0;
  271. }
  272. static int clps7111fb_updatevar(int con, struct fb_info *info)
  273. {
  274. return -EINVAL;
  275. }
  276. static void clps7111fb_blank(int blank, struct fb_info *info)
  277. {
  278.      if (blank) {
  279. if (machine_is_edb7211()) {
  280. int i;
  281. /* Turn off the LCD backlight. */
  282. clps_writeb(clps_readb(PDDR) & ~EDB_PD3_LCDBL, PDDR);
  283. /* Power off the LCD DC-DC converter. */
  284. clps_writeb(clps_readb(PDDR) & ~EDB_PD1_LCD_DC_DC_EN, PDDR);
  285. /* Delay for a little while (half a second). */
  286. for (i=0; i<65536*4; i++);
  287. /* Power off the LCD panel. */
  288. clps_writeb(clps_readb(PDDR) & ~EDB_PD2_LCDEN, PDDR);
  289. /* Power off the LCD controller. */
  290. clps_writel(clps_readl(SYSCON1) & ~SYSCON1_LCDEN, 
  291. SYSCON1);
  292. }
  293. } else {
  294. if (machine_is_edb7211()) {
  295. int i;
  296. /* Power up the LCD controller. */
  297. clps_writel(clps_readl(SYSCON1) | SYSCON1_LCDEN,
  298. SYSCON1);
  299. /* Power up the LCD panel. */
  300. clps_writeb(clps_readb(PDDR) | EDB_PD2_LCDEN, PDDR);
  301. /* Delay for a little while. */
  302. for (i=0; i<65536*4; i++);
  303. /* Power up the LCD DC-DC converter. */
  304. clps_writeb(clps_readb(PDDR) | EDB_PD1_LCD_DC_DC_EN,
  305. PDDR);
  306. /* Turn on the LCD backlight. */
  307. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  308. }
  309. }
  310. }
  311. static int 
  312. clps7111fb_proc_backlight_read(char *page, char **start, off_t off,
  313. int count, int *eof, void *data)
  314. {
  315. /* We need at least two characters, one for the digit, and one for
  316.  * the terminating NULL. */
  317. if (count < 2) 
  318. return -EINVAL;
  319. if (machine_is_edb7211()) {
  320. return sprintf(page, "%dn", 
  321. (clps_readb(PDDR) & EDB_PD3_LCDBL) ? 1 : 0);
  322. }
  323. return 0;
  324. }
  325. static int 
  326. clps7111fb_proc_backlight_write(struct file *file, const char *buffer, 
  327. unsigned long count, void *data)
  328. {
  329. unsigned char char_value;
  330. int value;
  331. if (count < 1) {
  332. return -EINVAL;
  333. }
  334. if (copy_from_user(&char_value, buffer, 1)) 
  335. return -EFAULT;
  336. value = char_value - '0';
  337. if (machine_is_edb7211()) {
  338. unsigned char port_d;
  339. port_d = clps_readb(PDDR);
  340. if (value) {
  341. port_d |= EDB_PD3_LCDBL;
  342. } else {
  343. port_d &= ~EDB_PD3_LCDBL;
  344. }
  345. clps_writeb(port_d, PDDR);
  346. }
  347. return count;
  348. }
  349. int __init clps711xfb_init(void)
  350. {
  351. int err = -ENOMEM;
  352. cfb = kmalloc(sizeof(*cfb) + sizeof(struct display), GFP_KERNEL);
  353. if (!cfb)
  354. goto out;
  355. memset(cfb, 0, sizeof(*cfb) + sizeof(struct display));
  356. memset((void *)PAGE_OFFSET, 0, 0x14000);
  357. cfb->currcon = -1;
  358. strcpy(cfb->fb.fix.id, "clps7111");
  359. cfb->fb.screen_base = (void *)PAGE_OFFSET;
  360. cfb->fb.fix.smem_start = PAGE_OFFSET;
  361. cfb->fb.fix.smem_len = 0x14000;
  362. cfb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  363. cfb->fb.var.xres  = 640;
  364. cfb->fb.var.xres_virtual = 640;
  365. cfb->fb.var.yres  = 240;
  366. cfb->fb.var.yres_virtual = 240;
  367. cfb->fb.var.bits_per_pixel = 4;
  368. cfb->fb.var.grayscale   = 1;
  369. cfb->fb.var.activate = FB_ACTIVATE_NOW;
  370. cfb->fb.var.height = -1;
  371. cfb->fb.var.width = -1;
  372. cfb->fb.fbops = &clps7111fb_ops;
  373. cfb->fb.changevar = NULL;
  374. cfb->fb.switch_con = clps7111fb_switch;
  375. cfb->fb.updatevar = clps7111fb_updatevar;
  376. cfb->fb.blank = clps7111fb_blank;
  377. cfb->fb.flags = FBINFO_FLAG_DEFAULT;
  378. cfb->fb.disp = (struct display *)(cfb + 1);
  379. fb_alloc_cmap(&cfb->fb.cmap, CMAP_SIZE, 0);
  380. /* Register the /proc entries. */
  381. clps7111fb_backlight_proc_entry = create_proc_entry("backlight", 0444,
  382. &proc_root);
  383. if (clps7111fb_backlight_proc_entry == NULL) {
  384. printk("Couldn't create the /proc entry for the backlight.n");
  385. return -EINVAL;
  386. }
  387. clps7111fb_backlight_proc_entry->read_proc = 
  388. &clps7111fb_proc_backlight_read;
  389. clps7111fb_backlight_proc_entry->write_proc = 
  390. &clps7111fb_proc_backlight_write;
  391. /*
  392.  * Power up the LCD
  393.  */
  394. if (machine_is_p720t()) {
  395. PLD_LCDEN = PLD_LCDEN_EN;
  396. PLD_PWR |= (PLD_S4_ON|PLD_S3_ON|PLD_S2_ON|PLD_S1_ON);
  397. }
  398. if (machine_is_edb7211()) {
  399. int i;
  400. /* Power up the LCD panel. */
  401. clps_writeb(clps_readb(PDDR) | EDB_PD2_LCDEN, PDDR);
  402. /* Delay for a little while. */
  403. for (i=0; i<65536*4; i++);
  404. /* Power up the LCD DC-DC converter. */
  405. clps_writeb(clps_readb(PDDR) | EDB_PD1_LCD_DC_DC_EN, PDDR);
  406. /* Turn on the LCD backlight. */
  407. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  408. }
  409. clps7111fb_set_var(&cfb->fb.var, -1, &cfb->fb);
  410. err = register_framebuffer(&cfb->fb);
  411. out: return err;
  412. }
  413. static void __exit clps711xfb_exit(void)
  414. {
  415. unregister_framebuffer(&cfb->fb);
  416. kfree(cfb);
  417. /*
  418.  * Power down the LCD
  419.  */
  420. if (machine_is_p720t()) {
  421. PLD_LCDEN = 0;
  422. PLD_PWR &= ~(PLD_S4_ON|PLD_S3_ON|PLD_S2_ON|PLD_S1_ON);
  423. }
  424. }
  425. #ifdef MODULE
  426. module_init(clps711xfb_init);
  427. #endif
  428. module_exit(clps711xfb_exit);