clps711xfb.c 10 KB

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