clps711xfb.c 7.6 KB

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