clps711xfb.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/slab.h>
  26. #include <linux/fb.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <mach/hardware.h>
  30. #include <asm/mach-types.h>
  31. #include <linux/uaccess.h>
  32. struct fb_info *cfb;
  33. #define CMAP_MAX_SIZE 16
  34. /*
  35. * LCD AC Prescale. This comes from the LCD panel manufacturers specifications.
  36. * This determines how many clocks + 1 of CL1 before the M signal toggles.
  37. * The number of lines on the display must not be divisible by this number.
  38. */
  39. static unsigned int lcd_ac_prescale = 13;
  40. /*
  41. * Set a single color register. Return != 0 for invalid regno.
  42. */
  43. static int
  44. clps7111fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  45. u_int transp, struct fb_info *info)
  46. {
  47. unsigned int level, mask, shift, pal;
  48. if (regno >= (1 << info->var.bits_per_pixel))
  49. return 1;
  50. /* gray = 0.30*R + 0.58*G + 0.11*B */
  51. level = (red * 77 + green * 151 + blue * 28) >> 20;
  52. /*
  53. * On an LCD, a high value is dark, while a low value is light.
  54. * So we invert the level.
  55. *
  56. * This isn't true on all machines, so we only do it on EDB7211.
  57. * --rmk
  58. */
  59. if (machine_is_edb7211()) {
  60. level = 15 - level;
  61. }
  62. shift = 4 * (regno & 7);
  63. level <<= shift;
  64. mask = 15 << shift;
  65. level &= mask;
  66. regno = regno < 8 ? PALLSW : PALMSW;
  67. pal = clps_readl(regno);
  68. pal = (pal & ~mask) | level;
  69. clps_writel(pal, regno);
  70. return 0;
  71. }
  72. /*
  73. * Validate the purposed mode.
  74. */
  75. static int
  76. clps7111fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  77. {
  78. var->transp.msb_right = 0;
  79. var->transp.offset = 0;
  80. var->transp.length = 0;
  81. var->red.msb_right = 0;
  82. var->red.offset = 0;
  83. var->red.length = var->bits_per_pixel;
  84. var->green = var->red;
  85. var->blue = var->red;
  86. if (var->bits_per_pixel > 4)
  87. return -EINVAL;
  88. return 0;
  89. }
  90. /*
  91. * Set the hardware state.
  92. */
  93. static int
  94. clps7111fb_set_par(struct fb_info *info)
  95. {
  96. unsigned int lcdcon, syscon, pixclock;
  97. switch (info->var.bits_per_pixel) {
  98. case 1:
  99. info->fix.visual = FB_VISUAL_MONO01;
  100. break;
  101. case 2:
  102. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  103. break;
  104. case 4:
  105. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  106. break;
  107. }
  108. info->fix.line_length = info->var.xres_virtual * info->var.bits_per_pixel / 8;
  109. lcdcon = (info->var.xres_virtual * info->var.yres_virtual * info->var.bits_per_pixel) / 128 - 1;
  110. lcdcon |= ((info->var.xres_virtual / 16) - 1) << 13;
  111. lcdcon |= lcd_ac_prescale << 25;
  112. /*
  113. * Calculate pixel prescale value from the pixclock. This is:
  114. * 36.864MHz / pixclock_mhz - 1.
  115. * However, pixclock is in picoseconds, so this ends up being:
  116. * 36864000 * pixclock_ps / 10^12 - 1
  117. * and this will overflow the 32-bit math. We perform this as
  118. * (9 * 4096000 == 36864000):
  119. * pixclock_ps * 9 * (4096000 / 10^12) - 1
  120. */
  121. pixclock = 9 * info->var.pixclock / 244140 - 1;
  122. lcdcon |= pixclock << 19;
  123. if (info->var.bits_per_pixel == 4)
  124. lcdcon |= LCDCON_GSMD;
  125. if (info->var.bits_per_pixel >= 2)
  126. lcdcon |= LCDCON_GSEN;
  127. /*
  128. * LCDCON must only be changed while the LCD is disabled
  129. */
  130. syscon = clps_readl(SYSCON1);
  131. clps_writel(syscon & ~SYSCON1_LCDEN, SYSCON1);
  132. clps_writel(lcdcon, LCDCON);
  133. clps_writel(syscon | SYSCON1_LCDEN, SYSCON1);
  134. return 0;
  135. }
  136. static int clps7111fb_blank(int blank, struct fb_info *info)
  137. {
  138. /* Enable/Disable LCD controller. */
  139. if (blank)
  140. clps_writel(clps_readl(SYSCON1) & ~SYSCON1_LCDEN, SYSCON1);
  141. else
  142. clps_writel(clps_readl(SYSCON1) | SYSCON1_LCDEN, SYSCON1);
  143. return 0;
  144. }
  145. static struct fb_ops clps7111fb_ops = {
  146. .owner = THIS_MODULE,
  147. .fb_check_var = clps7111fb_check_var,
  148. .fb_set_par = clps7111fb_set_par,
  149. .fb_setcolreg = clps7111fb_setcolreg,
  150. .fb_blank = clps7111fb_blank,
  151. .fb_fillrect = cfb_fillrect,
  152. .fb_copyarea = cfb_copyarea,
  153. .fb_imageblit = cfb_imageblit,
  154. };
  155. static void clps711x_guess_lcd_params(struct fb_info *info)
  156. {
  157. unsigned int lcdcon, syscon, size;
  158. unsigned long phys_base = PAGE_OFFSET;
  159. void *virt_base = (void *)PAGE_OFFSET;
  160. info->var.xres_virtual = 640;
  161. info->var.yres_virtual = 240;
  162. info->var.bits_per_pixel = 4;
  163. info->var.activate = FB_ACTIVATE_NOW;
  164. info->var.height = -1;
  165. info->var.width = -1;
  166. info->var.pixclock = 93006; /* 10.752MHz pixel clock */
  167. /*
  168. * If the LCD controller is already running, decode the values
  169. * in LCDCON to xres/yres/bpp/pixclock/acprescale
  170. */
  171. syscon = clps_readl(SYSCON1);
  172. if (syscon & SYSCON1_LCDEN) {
  173. lcdcon = clps_readl(LCDCON);
  174. /*
  175. * Decode GSMD and GSEN bits to bits per pixel
  176. */
  177. switch (lcdcon & (LCDCON_GSMD | LCDCON_GSEN)) {
  178. case LCDCON_GSMD | LCDCON_GSEN:
  179. info->var.bits_per_pixel = 4;
  180. break;
  181. case LCDCON_GSEN:
  182. info->var.bits_per_pixel = 2;
  183. break;
  184. default:
  185. info->var.bits_per_pixel = 1;
  186. break;
  187. }
  188. /*
  189. * Decode xres/yres
  190. */
  191. info->var.xres_virtual = (((lcdcon >> 13) & 0x3f) + 1) * 16;
  192. info->var.yres_virtual = (((lcdcon & 0x1fff) + 1) * 128) /
  193. (info->var.xres_virtual *
  194. info->var.bits_per_pixel);
  195. /*
  196. * Calculate pixclock
  197. */
  198. info->var.pixclock = (((lcdcon >> 19) & 0x3f) + 1) * 244140 / 9;
  199. /*
  200. * Grab AC prescale
  201. */
  202. lcd_ac_prescale = (lcdcon >> 25) & 0x1f;
  203. }
  204. info->var.xres = info->var.xres_virtual;
  205. info->var.yres = info->var.yres_virtual;
  206. info->var.grayscale = info->var.bits_per_pixel > 1;
  207. size = info->var.xres * info->var.yres * info->var.bits_per_pixel / 8;
  208. /*
  209. * Might be worth checking to see if we can use the on-board
  210. * RAM if size here...
  211. * CLPS7110 - no on-board SRAM
  212. * EP7212 - 38400 bytes
  213. */
  214. if (size <= 38400) {
  215. printk(KERN_INFO "CLPS711xFB: could use on-board SRAM?\n");
  216. }
  217. if ((syscon & SYSCON1_LCDEN) == 0) {
  218. /*
  219. * The display isn't running. Ensure that
  220. * the display memory is empty.
  221. */
  222. memset(virt_base, 0, size);
  223. }
  224. info->screen_base = virt_base;
  225. info->fix.smem_start = phys_base;
  226. info->fix.smem_len = PAGE_ALIGN(size);
  227. info->fix.type = FB_TYPE_PACKED_PIXELS;
  228. }
  229. static int clps711x_fb_probe(struct platform_device *pdev)
  230. {
  231. int err = -ENOMEM;
  232. if (fb_get_options("clps711xfb", NULL))
  233. return -ENODEV;
  234. cfb = kzalloc(sizeof(*cfb), GFP_KERNEL);
  235. if (!cfb)
  236. goto out;
  237. strcpy(cfb->fix.id, "clps711x");
  238. cfb->fbops = &clps7111fb_ops;
  239. cfb->flags = FBINFO_DEFAULT;
  240. clps711x_guess_lcd_params(cfb);
  241. fb_alloc_cmap(&cfb->cmap, CMAP_MAX_SIZE, 0);
  242. err = register_framebuffer(cfb);
  243. out: return err;
  244. }
  245. static int clps711x_fb_remove(struct platform_device *pdev)
  246. {
  247. unregister_framebuffer(cfb);
  248. kfree(cfb);
  249. return 0;
  250. }
  251. static struct platform_driver clps711x_fb_driver = {
  252. .driver = {
  253. .name = "video-clps711x",
  254. .owner = THIS_MODULE,
  255. },
  256. .probe = clps711x_fb_probe,
  257. .remove = clps711x_fb_remove,
  258. };
  259. module_platform_driver(clps711x_fb_driver);
  260. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  261. MODULE_DESCRIPTION("CLPS711X framebuffer driver");
  262. MODULE_LICENSE("GPL");