clps711xfb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <linux/delay.h>
  29. #include <asm/hardware.h>
  30. #include <asm/mach-types.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/hardware/clps7111.h>
  33. #include <asm/arch/syspld.h>
  34. struct fb_info *cfb;
  35. #define CMAP_MAX_SIZE 16
  36. /* The /proc entry for the backlight. */
  37. static struct proc_dir_entry *clps7111fb_backlight_proc_entry = NULL;
  38. static int clps7111fb_proc_backlight_read(char *page, char **start, off_t off,
  39. int count, int *eof, void *data);
  40. static int clps7111fb_proc_backlight_write(struct file *file,
  41. const char *buffer, unsigned long count, void *data);
  42. /*
  43. * LCD AC Prescale. This comes from the LCD panel manufacturers specifications.
  44. * This determines how many clocks + 1 of CL1 before the M signal toggles.
  45. * The number of lines on the display must not be divisible by this number.
  46. */
  47. static unsigned int lcd_ac_prescale = 13;
  48. /*
  49. * Set a single color register. Return != 0 for invalid regno.
  50. */
  51. static int
  52. clps7111fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  53. u_int transp, struct fb_info *info)
  54. {
  55. unsigned int level, mask, shift, pal;
  56. if (regno >= (1 << info->var.bits_per_pixel))
  57. return 1;
  58. /* gray = 0.30*R + 0.58*G + 0.11*B */
  59. level = (red * 77 + green * 151 + blue * 28) >> 20;
  60. /*
  61. * On an LCD, a high value is dark, while a low value is light.
  62. * So we invert the level.
  63. *
  64. * This isn't true on all machines, so we only do it on EDB7211.
  65. * --rmk
  66. */
  67. if (machine_is_edb7211()) {
  68. level = 15 - level;
  69. }
  70. shift = 4 * (regno & 7);
  71. level <<= shift;
  72. mask = 15 << shift;
  73. level &= mask;
  74. regno = regno < 8 ? PALLSW : PALMSW;
  75. pal = clps_readl(regno);
  76. pal = (pal & ~mask) | level;
  77. clps_writel(pal, regno);
  78. return 0;
  79. }
  80. /*
  81. * Validate the purposed mode.
  82. */
  83. static int
  84. clps7111fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  85. {
  86. var->transp.msb_right = 0;
  87. var->transp.offset = 0;
  88. var->transp.length = 0;
  89. var->red.msb_right = 0;
  90. var->red.offset = 0;
  91. var->red.length = var->bits_per_pixel;
  92. var->green = var->red;
  93. var->blue = var->red;
  94. if (var->bits_per_pixel > 4)
  95. return -EINVAL;
  96. return 0;
  97. }
  98. /*
  99. * Set the hardware state.
  100. */
  101. static int
  102. clps7111fb_set_par(struct fb_info *info)
  103. {
  104. unsigned int lcdcon, syscon, pixclock;
  105. switch (info->var.bits_per_pixel) {
  106. case 1:
  107. info->fix.visual = FB_VISUAL_MONO01;
  108. break;
  109. case 2:
  110. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  111. break;
  112. case 4:
  113. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  114. break;
  115. }
  116. info->fix.line_length = info->var.xres_virtual * info->var.bits_per_pixel / 8;
  117. lcdcon = (info->var.xres_virtual * info->var.yres_virtual * info->var.bits_per_pixel) / 128 - 1;
  118. lcdcon |= ((info->var.xres_virtual / 16) - 1) << 13;
  119. lcdcon |= lcd_ac_prescale << 25;
  120. /*
  121. * Calculate pixel prescale value from the pixclock. This is:
  122. * 36.864MHz / pixclock_mhz - 1.
  123. * However, pixclock is in picoseconds, so this ends up being:
  124. * 36864000 * pixclock_ps / 10^12 - 1
  125. * and this will overflow the 32-bit math. We perform this as
  126. * (9 * 4096000 == 36864000):
  127. * pixclock_ps * 9 * (4096000 / 10^12) - 1
  128. */
  129. pixclock = 9 * info->var.pixclock / 244140 - 1;
  130. lcdcon |= pixclock << 19;
  131. if (info->var.bits_per_pixel == 4)
  132. lcdcon |= LCDCON_GSMD;
  133. if (info->var.bits_per_pixel >= 2)
  134. lcdcon |= LCDCON_GSEN;
  135. /*
  136. * LCDCON must only be changed while the LCD is disabled
  137. */
  138. syscon = clps_readl(SYSCON1);
  139. clps_writel(syscon & ~SYSCON1_LCDEN, SYSCON1);
  140. clps_writel(lcdcon, LCDCON);
  141. clps_writel(syscon | SYSCON1_LCDEN, SYSCON1);
  142. return 0;
  143. }
  144. static int clps7111fb_blank(int blank, struct fb_info *info)
  145. {
  146. if (blank) {
  147. if (machine_is_edb7211()) {
  148. /* Turn off the LCD backlight. */
  149. clps_writeb(clps_readb(PDDR) & ~EDB_PD3_LCDBL, PDDR);
  150. /* Power off the LCD DC-DC converter. */
  151. clps_writeb(clps_readb(PDDR) & ~EDB_PD1_LCD_DC_DC_EN, PDDR);
  152. /* Delay for a little while (half a second). */
  153. udelay(100);
  154. /* Power off the LCD panel. */
  155. clps_writeb(clps_readb(PDDR) & ~EDB_PD2_LCDEN, PDDR);
  156. /* Power off the LCD controller. */
  157. clps_writel(clps_readl(SYSCON1) & ~SYSCON1_LCDEN,
  158. SYSCON1);
  159. }
  160. } else {
  161. if (machine_is_edb7211()) {
  162. /* Power up the LCD controller. */
  163. clps_writel(clps_readl(SYSCON1) | SYSCON1_LCDEN,
  164. SYSCON1);
  165. /* Power up the LCD panel. */
  166. clps_writeb(clps_readb(PDDR) | EDB_PD2_LCDEN, PDDR);
  167. /* Delay for a little while. */
  168. udelay(100);
  169. /* Power up the LCD DC-DC converter. */
  170. clps_writeb(clps_readb(PDDR) | EDB_PD1_LCD_DC_DC_EN,
  171. PDDR);
  172. /* Turn on the LCD backlight. */
  173. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  174. }
  175. }
  176. return 0;
  177. }
  178. static struct fb_ops clps7111fb_ops = {
  179. .owner = THIS_MODULE,
  180. .fb_check_var = clps7111fb_check_var,
  181. .fb_set_par = clps7111fb_set_par,
  182. .fb_setcolreg = clps7111fb_setcolreg,
  183. .fb_blank = clps7111fb_blank,
  184. .fb_fillrect = cfb_fillrect,
  185. .fb_copyarea = cfb_copyarea,
  186. .fb_imageblit = cfb_imageblit,
  187. };
  188. static int
  189. clps7111fb_proc_backlight_read(char *page, char **start, off_t off,
  190. int count, int *eof, void *data)
  191. {
  192. /* We need at least two characters, one for the digit, and one for
  193. * the terminating NULL. */
  194. if (count < 2)
  195. return -EINVAL;
  196. if (machine_is_edb7211()) {
  197. return sprintf(page, "%d\n",
  198. (clps_readb(PDDR) & EDB_PD3_LCDBL) ? 1 : 0);
  199. }
  200. return 0;
  201. }
  202. static int
  203. clps7111fb_proc_backlight_write(struct file *file, const char *buffer,
  204. unsigned long count, void *data)
  205. {
  206. unsigned char char_value;
  207. int value;
  208. if (count < 1) {
  209. return -EINVAL;
  210. }
  211. if (copy_from_user(&char_value, buffer, 1))
  212. return -EFAULT;
  213. value = char_value - '0';
  214. if (machine_is_edb7211()) {
  215. unsigned char port_d;
  216. port_d = clps_readb(PDDR);
  217. if (value) {
  218. port_d |= EDB_PD3_LCDBL;
  219. } else {
  220. port_d &= ~EDB_PD3_LCDBL;
  221. }
  222. clps_writeb(port_d, PDDR);
  223. }
  224. return count;
  225. }
  226. static void __init clps711x_guess_lcd_params(struct fb_info *info)
  227. {
  228. unsigned int lcdcon, syscon, size;
  229. unsigned long phys_base = PAGE_OFFSET;
  230. void *virt_base = (void *)PAGE_OFFSET;
  231. info->var.xres_virtual = 640;
  232. info->var.yres_virtual = 240;
  233. info->var.bits_per_pixel = 4;
  234. info->var.activate = FB_ACTIVATE_NOW;
  235. info->var.height = -1;
  236. info->var.width = -1;
  237. info->var.pixclock = 93006; /* 10.752MHz pixel clock */
  238. /*
  239. * If the LCD controller is already running, decode the values
  240. * in LCDCON to xres/yres/bpp/pixclock/acprescale
  241. */
  242. syscon = clps_readl(SYSCON1);
  243. if (syscon & SYSCON1_LCDEN) {
  244. lcdcon = clps_readl(LCDCON);
  245. /*
  246. * Decode GSMD and GSEN bits to bits per pixel
  247. */
  248. switch (lcdcon & (LCDCON_GSMD | LCDCON_GSEN)) {
  249. case LCDCON_GSMD | LCDCON_GSEN:
  250. info->var.bits_per_pixel = 4;
  251. break;
  252. case LCDCON_GSEN:
  253. info->var.bits_per_pixel = 2;
  254. break;
  255. default:
  256. info->var.bits_per_pixel = 1;
  257. break;
  258. }
  259. /*
  260. * Decode xres/yres
  261. */
  262. info->var.xres_virtual = (((lcdcon >> 13) & 0x3f) + 1) * 16;
  263. info->var.yres_virtual = (((lcdcon & 0x1fff) + 1) * 128) /
  264. (info->var.xres_virtual *
  265. info->var.bits_per_pixel);
  266. /*
  267. * Calculate pixclock
  268. */
  269. info->var.pixclock = (((lcdcon >> 19) & 0x3f) + 1) * 244140 / 9;
  270. /*
  271. * Grab AC prescale
  272. */
  273. lcd_ac_prescale = (lcdcon >> 25) & 0x1f;
  274. }
  275. info->var.xres = info->var.xres_virtual;
  276. info->var.yres = info->var.yres_virtual;
  277. info->var.grayscale = info->var.bits_per_pixel > 1;
  278. size = info->var.xres * info->var.yres * info->var.bits_per_pixel / 8;
  279. /*
  280. * Might be worth checking to see if we can use the on-board
  281. * RAM if size here...
  282. * CLPS7110 - no on-board SRAM
  283. * EP7212 - 38400 bytes
  284. */
  285. if (size <= 38400) {
  286. printk(KERN_INFO "CLPS711xFB: could use on-board SRAM?\n");
  287. }
  288. if ((syscon & SYSCON1_LCDEN) == 0) {
  289. /*
  290. * The display isn't running. Ensure that
  291. * the display memory is empty.
  292. */
  293. memset(virt_base, 0, size);
  294. }
  295. info->screen_base = virt_base;
  296. info->fix.smem_start = phys_base;
  297. info->fix.smem_len = PAGE_ALIGN(size);
  298. info->fix.type = FB_TYPE_PACKED_PIXELS;
  299. }
  300. int __init clps711xfb_init(void)
  301. {
  302. int err = -ENOMEM;
  303. if (fb_get_options("clps711xfb", NULL))
  304. return -ENODEV;
  305. cfb = kmalloc(sizeof(*cfb), GFP_KERNEL);
  306. if (!cfb)
  307. goto out;
  308. memset(cfb, 0, sizeof(*cfb));
  309. strcpy(cfb->fix.id, "clps711x");
  310. cfb->fbops = &clps7111fb_ops;
  311. cfb->flags = FBINFO_DEFAULT;
  312. clps711x_guess_lcd_params(cfb);
  313. fb_alloc_cmap(&cfb->cmap, CMAP_MAX_SIZE, 0);
  314. /* Register the /proc entries. */
  315. clps7111fb_backlight_proc_entry = create_proc_entry("backlight", 0444,
  316. &proc_root);
  317. if (clps7111fb_backlight_proc_entry == NULL) {
  318. printk("Couldn't create the /proc entry for the backlight.\n");
  319. return -EINVAL;
  320. }
  321. clps7111fb_backlight_proc_entry->read_proc =
  322. &clps7111fb_proc_backlight_read;
  323. clps7111fb_backlight_proc_entry->write_proc =
  324. &clps7111fb_proc_backlight_write;
  325. /*
  326. * Power up the LCD
  327. */
  328. if (machine_is_p720t()) {
  329. PLD_LCDEN = PLD_LCDEN_EN;
  330. PLD_PWR |= (PLD_S4_ON|PLD_S3_ON|PLD_S2_ON|PLD_S1_ON);
  331. }
  332. if (machine_is_edb7211()) {
  333. /* Power up the LCD panel. */
  334. clps_writeb(clps_readb(PDDR) | EDB_PD2_LCDEN, PDDR);
  335. /* Delay for a little while. */
  336. udelay(100);
  337. /* Power up the LCD DC-DC converter. */
  338. clps_writeb(clps_readb(PDDR) | EDB_PD1_LCD_DC_DC_EN, PDDR);
  339. /* Turn on the LCD backlight. */
  340. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  341. }
  342. err = register_framebuffer(cfb);
  343. out: return err;
  344. }
  345. static void __exit clps711xfb_exit(void)
  346. {
  347. unregister_framebuffer(cfb);
  348. kfree(cfb);
  349. /*
  350. * Power down the LCD
  351. */
  352. if (machine_is_p720t()) {
  353. PLD_LCDEN = 0;
  354. PLD_PWR &= ~(PLD_S4_ON|PLD_S3_ON|PLD_S2_ON|PLD_S1_ON);
  355. }
  356. }
  357. module_init(clps711xfb_init);
  358. module_exit(clps711xfb_exit);
  359. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  360. MODULE_DESCRIPTION("CLPS711x framebuffer driver");
  361. MODULE_LICENSE("GPL");