clps711xfb.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. /* Disable LCD controller. */
  146. clps_writel(clps_readl(SYSCON1) & ~SYSCON1_LCDEN,
  147. SYSCON1);
  148. }
  149. } else {
  150. if (machine_is_edb7211()) {
  151. /* Enable LCD controller. */
  152. clps_writel(clps_readl(SYSCON1) | SYSCON1_LCDEN,
  153. SYSCON1);
  154. /* Turn on the LCD backlight. */
  155. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  156. }
  157. }
  158. return 0;
  159. }
  160. static struct fb_ops clps7111fb_ops = {
  161. .owner = THIS_MODULE,
  162. .fb_check_var = clps7111fb_check_var,
  163. .fb_set_par = clps7111fb_set_par,
  164. .fb_setcolreg = clps7111fb_setcolreg,
  165. .fb_blank = clps7111fb_blank,
  166. .fb_fillrect = cfb_fillrect,
  167. .fb_copyarea = cfb_copyarea,
  168. .fb_imageblit = cfb_imageblit,
  169. };
  170. static int backlight_proc_show(struct seq_file *m, void *v)
  171. {
  172. if (machine_is_edb7211()) {
  173. seq_printf(m, "%d\n",
  174. (clps_readb(PDDR) & EDB_PD3_LCDBL) ? 1 : 0);
  175. }
  176. return 0;
  177. }
  178. static int backlight_proc_open(struct inode *inode, struct file *file)
  179. {
  180. return single_open(file, backlight_proc_show, NULL);
  181. }
  182. static ssize_t backlight_proc_write(struct file *file, const char *buffer,
  183. size_t count, loff_t *pos)
  184. {
  185. unsigned char char_value;
  186. int value;
  187. if (count < 1) {
  188. return -EINVAL;
  189. }
  190. if (copy_from_user(&char_value, buffer, 1))
  191. return -EFAULT;
  192. value = char_value - '0';
  193. if (machine_is_edb7211()) {
  194. unsigned char port_d;
  195. port_d = clps_readb(PDDR);
  196. if (value) {
  197. port_d |= EDB_PD3_LCDBL;
  198. } else {
  199. port_d &= ~EDB_PD3_LCDBL;
  200. }
  201. clps_writeb(port_d, PDDR);
  202. }
  203. return count;
  204. }
  205. static const struct file_operations backlight_proc_fops = {
  206. .owner = THIS_MODULE,
  207. .open = backlight_proc_open,
  208. .read = seq_read,
  209. .llseek = seq_lseek,
  210. .release = single_release,
  211. .write = backlight_proc_write,
  212. };
  213. static void __devinit clps711x_guess_lcd_params(struct fb_info *info)
  214. {
  215. unsigned int lcdcon, syscon, size;
  216. unsigned long phys_base = PAGE_OFFSET;
  217. void *virt_base = (void *)PAGE_OFFSET;
  218. info->var.xres_virtual = 640;
  219. info->var.yres_virtual = 240;
  220. info->var.bits_per_pixel = 4;
  221. info->var.activate = FB_ACTIVATE_NOW;
  222. info->var.height = -1;
  223. info->var.width = -1;
  224. info->var.pixclock = 93006; /* 10.752MHz pixel clock */
  225. /*
  226. * If the LCD controller is already running, decode the values
  227. * in LCDCON to xres/yres/bpp/pixclock/acprescale
  228. */
  229. syscon = clps_readl(SYSCON1);
  230. if (syscon & SYSCON1_LCDEN) {
  231. lcdcon = clps_readl(LCDCON);
  232. /*
  233. * Decode GSMD and GSEN bits to bits per pixel
  234. */
  235. switch (lcdcon & (LCDCON_GSMD | LCDCON_GSEN)) {
  236. case LCDCON_GSMD | LCDCON_GSEN:
  237. info->var.bits_per_pixel = 4;
  238. break;
  239. case LCDCON_GSEN:
  240. info->var.bits_per_pixel = 2;
  241. break;
  242. default:
  243. info->var.bits_per_pixel = 1;
  244. break;
  245. }
  246. /*
  247. * Decode xres/yres
  248. */
  249. info->var.xres_virtual = (((lcdcon >> 13) & 0x3f) + 1) * 16;
  250. info->var.yres_virtual = (((lcdcon & 0x1fff) + 1) * 128) /
  251. (info->var.xres_virtual *
  252. info->var.bits_per_pixel);
  253. /*
  254. * Calculate pixclock
  255. */
  256. info->var.pixclock = (((lcdcon >> 19) & 0x3f) + 1) * 244140 / 9;
  257. /*
  258. * Grab AC prescale
  259. */
  260. lcd_ac_prescale = (lcdcon >> 25) & 0x1f;
  261. }
  262. info->var.xres = info->var.xres_virtual;
  263. info->var.yres = info->var.yres_virtual;
  264. info->var.grayscale = info->var.bits_per_pixel > 1;
  265. size = info->var.xres * info->var.yres * info->var.bits_per_pixel / 8;
  266. /*
  267. * Might be worth checking to see if we can use the on-board
  268. * RAM if size here...
  269. * CLPS7110 - no on-board SRAM
  270. * EP7212 - 38400 bytes
  271. */
  272. if (size <= 38400) {
  273. printk(KERN_INFO "CLPS711xFB: could use on-board SRAM?\n");
  274. }
  275. if ((syscon & SYSCON1_LCDEN) == 0) {
  276. /*
  277. * The display isn't running. Ensure that
  278. * the display memory is empty.
  279. */
  280. memset(virt_base, 0, size);
  281. }
  282. info->screen_base = virt_base;
  283. info->fix.smem_start = phys_base;
  284. info->fix.smem_len = PAGE_ALIGN(size);
  285. info->fix.type = FB_TYPE_PACKED_PIXELS;
  286. }
  287. static int __devinit clps711x_fb_probe(struct platform_device *pdev)
  288. {
  289. int err = -ENOMEM;
  290. if (fb_get_options("clps711xfb", NULL))
  291. return -ENODEV;
  292. cfb = kzalloc(sizeof(*cfb), GFP_KERNEL);
  293. if (!cfb)
  294. goto out;
  295. strcpy(cfb->fix.id, "clps711x");
  296. cfb->fbops = &clps7111fb_ops;
  297. cfb->flags = FBINFO_DEFAULT;
  298. clps711x_guess_lcd_params(cfb);
  299. fb_alloc_cmap(&cfb->cmap, CMAP_MAX_SIZE, 0);
  300. if (!proc_create("backlight", 0444, NULL, &backlight_proc_fops)) {
  301. printk("Couldn't create the /proc entry for the backlight.\n");
  302. return -EINVAL;
  303. }
  304. /*
  305. * Power up the LCD
  306. */
  307. if (machine_is_p720t())
  308. PLD_PWR |= PLD_S3_ON;
  309. if (machine_is_edb7211()) {
  310. /* Turn on the LCD backlight. */
  311. clps_writeb(clps_readb(PDDR) | EDB_PD3_LCDBL, PDDR);
  312. }
  313. err = register_framebuffer(cfb);
  314. out: return err;
  315. }
  316. static int __devexit clps711x_fb_remove(struct platform_device *pdev)
  317. {
  318. unregister_framebuffer(cfb);
  319. kfree(cfb);
  320. /*
  321. * Power down the LCD
  322. */
  323. if (machine_is_p720t())
  324. PLD_PWR &= ~PLD_S3_ON;
  325. return 0;
  326. }
  327. static struct platform_driver clps711x_fb_driver = {
  328. .driver = {
  329. .name = "video-clps711x",
  330. .owner = THIS_MODULE,
  331. },
  332. .probe = clps711x_fb_probe,
  333. .remove = __devexit_p(clps711x_fb_remove),
  334. };
  335. module_platform_driver(clps711x_fb_driver);
  336. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  337. MODULE_DESCRIPTION("CLPS711X framebuffer driver");
  338. MODULE_LICENSE("GPL");