clps711xfb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <mach/syspld.h>
  35. struct fb_info *cfb;
  36. #define CMAP_MAX_SIZE 16
  37. /*
  38. * LCD AC Prescale. This comes from the LCD panel manufacturers specifications.
  39. * This determines how many clocks + 1 of CL1 before the M signal toggles.
  40. * The number of lines on the display must not be divisible by this number.
  41. */
  42. static unsigned int lcd_ac_prescale = 13;
  43. /*
  44. * Set a single color register. Return != 0 for invalid regno.
  45. */
  46. static int
  47. clps7111fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  48. u_int transp, struct fb_info *info)
  49. {
  50. unsigned int level, mask, shift, pal;
  51. if (regno >= (1 << info->var.bits_per_pixel))
  52. return 1;
  53. /* gray = 0.30*R + 0.58*G + 0.11*B */
  54. level = (red * 77 + green * 151 + blue * 28) >> 20;
  55. /*
  56. * On an LCD, a high value is dark, while a low value is light.
  57. * So we invert the level.
  58. *
  59. * This isn't true on all machines, so we only do it on EDB7211.
  60. * --rmk
  61. */
  62. if (machine_is_edb7211()) {
  63. level = 15 - level;
  64. }
  65. shift = 4 * (regno & 7);
  66. level <<= shift;
  67. mask = 15 << shift;
  68. level &= mask;
  69. regno = regno < 8 ? PALLSW : PALMSW;
  70. pal = clps_readl(regno);
  71. pal = (pal & ~mask) | level;
  72. clps_writel(pal, regno);
  73. return 0;
  74. }
  75. /*
  76. * Validate the purposed mode.
  77. */
  78. static int
  79. clps7111fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  80. {
  81. var->transp.msb_right = 0;
  82. var->transp.offset = 0;
  83. var->transp.length = 0;
  84. var->red.msb_right = 0;
  85. var->red.offset = 0;
  86. var->red.length = var->bits_per_pixel;
  87. var->green = var->red;
  88. var->blue = var->red;
  89. if (var->bits_per_pixel > 4)
  90. return -EINVAL;
  91. return 0;
  92. }
  93. /*
  94. * Set the hardware state.
  95. */
  96. static int
  97. clps7111fb_set_par(struct fb_info *info)
  98. {
  99. unsigned int lcdcon, syscon, pixclock;
  100. switch (info->var.bits_per_pixel) {
  101. case 1:
  102. info->fix.visual = FB_VISUAL_MONO01;
  103. break;
  104. case 2:
  105. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  106. break;
  107. case 4:
  108. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  109. break;
  110. }
  111. info->fix.line_length = info->var.xres_virtual * info->var.bits_per_pixel / 8;
  112. lcdcon = (info->var.xres_virtual * info->var.yres_virtual * info->var.bits_per_pixel) / 128 - 1;
  113. lcdcon |= ((info->var.xres_virtual / 16) - 1) << 13;
  114. lcdcon |= lcd_ac_prescale << 25;
  115. /*
  116. * Calculate pixel prescale value from the pixclock. This is:
  117. * 36.864MHz / pixclock_mhz - 1.
  118. * However, pixclock is in picoseconds, so this ends up being:
  119. * 36864000 * pixclock_ps / 10^12 - 1
  120. * and this will overflow the 32-bit math. We perform this as
  121. * (9 * 4096000 == 36864000):
  122. * pixclock_ps * 9 * (4096000 / 10^12) - 1
  123. */
  124. pixclock = 9 * info->var.pixclock / 244140 - 1;
  125. lcdcon |= pixclock << 19;
  126. if (info->var.bits_per_pixel == 4)
  127. lcdcon |= LCDCON_GSMD;
  128. if (info->var.bits_per_pixel >= 2)
  129. lcdcon |= LCDCON_GSEN;
  130. /*
  131. * LCDCON must only be changed while the LCD is disabled
  132. */
  133. syscon = clps_readl(SYSCON1);
  134. clps_writel(syscon & ~SYSCON1_LCDEN, SYSCON1);
  135. clps_writel(lcdcon, LCDCON);
  136. clps_writel(syscon | SYSCON1_LCDEN, SYSCON1);
  137. return 0;
  138. }
  139. static int clps7111fb_blank(int blank, struct fb_info *info)
  140. {
  141. if (blank) {
  142. if (machine_is_edb7211()) {
  143. /* Turn off the LCD backlight. */
  144. clps_writeb(clps_readb(PDDR) & ~EDB_PD3_LCDBL, PDDR);
  145. /* Power off the LCD DC-DC converter. */
  146. clps_writeb(clps_readb(PDDR) & ~EDB_PD1_LCD_DC_DC_EN, PDDR);
  147. /* Delay for a little while (half a second). */
  148. udelay(100);
  149. /* Power off the LCD panel. */
  150. clps_writeb(clps_readb(PDDR) & ~EDB_PD2_LCDEN, PDDR);
  151. /* Power off the LCD controller. */
  152. clps_writel(clps_readl(SYSCON1) & ~SYSCON1_LCDEN,
  153. SYSCON1);
  154. }
  155. } else {
  156. if (machine_is_edb7211()) {
  157. /* Power up the LCD controller. */
  158. clps_writel(clps_readl(SYSCON1) | SYSCON1_LCDEN,
  159. SYSCON1);
  160. /* Power up the LCD panel. */
  161. clps_writeb(clps_readb(PDDR) | EDB_PD2_LCDEN, PDDR);
  162. /* Delay for a little while. */
  163. udelay(100);
  164. /* Power up the LCD DC-DC converter. */
  165. clps_writeb(clps_readb(PDDR) | EDB_PD1_LCD_DC_DC_EN,
  166. PDDR);
  167. /* Turn on the LCD backlight. */
  168. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  169. }
  170. }
  171. return 0;
  172. }
  173. static struct fb_ops clps7111fb_ops = {
  174. .owner = THIS_MODULE,
  175. .fb_check_var = clps7111fb_check_var,
  176. .fb_set_par = clps7111fb_set_par,
  177. .fb_setcolreg = clps7111fb_setcolreg,
  178. .fb_blank = clps7111fb_blank,
  179. .fb_fillrect = cfb_fillrect,
  180. .fb_copyarea = cfb_copyarea,
  181. .fb_imageblit = cfb_imageblit,
  182. };
  183. static int backlight_proc_show(struct seq_file *m, void *v)
  184. {
  185. if (machine_is_edb7211()) {
  186. seq_printf(m, "%d\n",
  187. (clps_readb(PDDR) & EDB_PD3_LCDBL) ? 1 : 0);
  188. }
  189. return 0;
  190. }
  191. static int backlight_proc_open(struct inode *inode, struct file *file)
  192. {
  193. return single_open(file, backlight_proc_show, NULL);
  194. }
  195. static ssize_t backlight_proc_write(struct file *file, const char *buffer,
  196. size_t count, loff_t *pos)
  197. {
  198. unsigned char char_value;
  199. int value;
  200. if (count < 1) {
  201. return -EINVAL;
  202. }
  203. if (copy_from_user(&char_value, buffer, 1))
  204. return -EFAULT;
  205. value = char_value - '0';
  206. if (machine_is_edb7211()) {
  207. unsigned char port_d;
  208. port_d = clps_readb(PDDR);
  209. if (value) {
  210. port_d |= EDB_PD3_LCDBL;
  211. } else {
  212. port_d &= ~EDB_PD3_LCDBL;
  213. }
  214. clps_writeb(port_d, PDDR);
  215. }
  216. return count;
  217. }
  218. static const struct file_operations backlight_proc_fops = {
  219. .owner = THIS_MODULE,
  220. .open = backlight_proc_open,
  221. .read = seq_read,
  222. .llseek = seq_lseek,
  223. .release = single_release,
  224. .write = backlight_proc_write,
  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 = kzalloc(sizeof(*cfb), GFP_KERNEL);
  306. if (!cfb)
  307. goto out;
  308. strcpy(cfb->fix.id, "clps711x");
  309. cfb->fbops = &clps7111fb_ops;
  310. cfb->flags = FBINFO_DEFAULT;
  311. clps711x_guess_lcd_params(cfb);
  312. fb_alloc_cmap(&cfb->cmap, CMAP_MAX_SIZE, 0);
  313. if (!proc_create("backlight", 0444, NULL, &backlight_proc_fops)) {
  314. printk("Couldn't create the /proc entry for the backlight.\n");
  315. return -EINVAL;
  316. }
  317. /*
  318. * Power up the LCD
  319. */
  320. if (machine_is_p720t()) {
  321. PLD_LCDEN = PLD_LCDEN_EN;
  322. PLD_PWR |= (PLD_S4_ON|PLD_S3_ON|PLD_S2_ON|PLD_S1_ON);
  323. }
  324. if (machine_is_edb7211()) {
  325. /* Power up the LCD panel. */
  326. clps_writeb(clps_readb(PDDR) | EDB_PD2_LCDEN, PDDR);
  327. /* Delay for a little while. */
  328. udelay(100);
  329. /* Power up the LCD DC-DC converter. */
  330. clps_writeb(clps_readb(PDDR) | EDB_PD1_LCD_DC_DC_EN, PDDR);
  331. /* Turn on the LCD backlight. */
  332. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  333. }
  334. err = register_framebuffer(cfb);
  335. out: return err;
  336. }
  337. static void __exit clps711xfb_exit(void)
  338. {
  339. unregister_framebuffer(cfb);
  340. kfree(cfb);
  341. /*
  342. * Power down the LCD
  343. */
  344. if (machine_is_p720t()) {
  345. PLD_LCDEN = 0;
  346. PLD_PWR &= ~(PLD_S4_ON|PLD_S3_ON|PLD_S2_ON|PLD_S1_ON);
  347. }
  348. }
  349. module_init(clps711xfb_init);
  350. module_exit(clps711xfb_exit);
  351. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  352. MODULE_DESCRIPTION("CLPS711x framebuffer driver");
  353. MODULE_LICENSE("GPL");