cobalt_lcdfb.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Cobalt server LCD frame buffer driver.
  3. *
  4. * Copyright (C) 2008 Yoichi Yuasa <yuasa@linux-mips.org>
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/fb.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. #include <linux/ioport.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/platform_device.h>
  27. /*
  28. * Cursor position address
  29. * \X 0 1 2 ... 14 15
  30. * Y+----+----+----+---+----+----+
  31. * 0|0x00|0x01|0x02|...|0x0e|0x0f|
  32. * +----+----+----+---+----+----+
  33. * 1|0x40|0x41|0x42|...|0x4e|0x4f|
  34. * +----+----+----+---+----+----+
  35. */
  36. #define LCD_DATA_REG_OFFSET 0x10
  37. #define LCD_XRES_MAX 16
  38. #define LCD_YRES_MAX 2
  39. #define LCD_CHARS_MAX 32
  40. #define LCD_CLEAR 0x01
  41. #define LCD_CURSOR_MOVE_HOME 0x02
  42. #define LCD_RESET 0x06
  43. #define LCD_OFF 0x08
  44. #define LCD_CURSOR_OFF 0x0c
  45. #define LCD_CURSOR_BLINK_OFF 0x0e
  46. #define LCD_CURSOR_ON 0x0f
  47. #define LCD_ON LCD_CURSOR_ON
  48. #define LCD_CURSOR_MOVE_LEFT 0x10
  49. #define LCD_CURSOR_MOVE_RIGHT 0x14
  50. #define LCD_DISPLAY_LEFT 0x18
  51. #define LCD_DISPLAY_RIGHT 0x1c
  52. #define LCD_PRERESET 0x3f /* execute 4 times continuously */
  53. #define LCD_BUSY 0x80
  54. #define LCD_GRAPHIC_MODE 0x40
  55. #define LCD_TEXT_MODE 0x80
  56. #define LCD_CUR_POS_MASK 0x7f
  57. #define LCD_CUR_POS(x) ((x) & LCD_CUR_POS_MASK)
  58. #define LCD_TEXT_POS(x) ((x) | LCD_TEXT_MODE)
  59. static inline void lcd_write_control(struct fb_info *info, u8 control)
  60. {
  61. writel((u32)control << 24, info->screen_base);
  62. }
  63. static inline u8 lcd_read_control(struct fb_info *info)
  64. {
  65. return readl(info->screen_base) >> 24;
  66. }
  67. static inline void lcd_write_data(struct fb_info *info, u8 data)
  68. {
  69. writel((u32)data << 24, info->screen_base + LCD_DATA_REG_OFFSET);
  70. }
  71. static inline u8 lcd_read_data(struct fb_info *info)
  72. {
  73. return readl(info->screen_base + LCD_DATA_REG_OFFSET) >> 24;
  74. }
  75. static int lcd_busy_wait(struct fb_info *info)
  76. {
  77. u8 val = 0;
  78. int timeout = 10, retval = 0;
  79. do {
  80. val = lcd_read_control(info);
  81. val &= LCD_BUSY;
  82. if (val != LCD_BUSY)
  83. break;
  84. if (msleep_interruptible(1))
  85. return -EINTR;
  86. timeout--;
  87. } while (timeout);
  88. if (val == LCD_BUSY)
  89. retval = -EBUSY;
  90. return retval;
  91. }
  92. static void lcd_clear(struct fb_info *info)
  93. {
  94. int i;
  95. for (i = 0; i < 4; i++) {
  96. udelay(150);
  97. lcd_write_control(info, LCD_PRERESET);
  98. }
  99. udelay(150);
  100. lcd_write_control(info, LCD_CLEAR);
  101. udelay(150);
  102. lcd_write_control(info, LCD_RESET);
  103. }
  104. static struct fb_fix_screeninfo cobalt_lcdfb_fix __devinitdata = {
  105. .id = "cobalt-lcd",
  106. .type = FB_TYPE_TEXT,
  107. .type_aux = FB_AUX_TEXT_MDA,
  108. .visual = FB_VISUAL_MONO01,
  109. .line_length = LCD_XRES_MAX,
  110. .accel = FB_ACCEL_NONE,
  111. };
  112. static ssize_t cobalt_lcdfb_read(struct fb_info *info, char __user *buf,
  113. size_t count, loff_t *ppos)
  114. {
  115. char src[LCD_CHARS_MAX];
  116. unsigned long pos;
  117. int len, retval = 0;
  118. pos = *ppos;
  119. if (pos >= LCD_CHARS_MAX || count == 0)
  120. return 0;
  121. if (count > LCD_CHARS_MAX)
  122. count = LCD_CHARS_MAX;
  123. if (pos + count > LCD_CHARS_MAX)
  124. count = LCD_CHARS_MAX - pos;
  125. for (len = 0; len < count; len++) {
  126. retval = lcd_busy_wait(info);
  127. if (retval < 0)
  128. break;
  129. lcd_write_control(info, LCD_TEXT_POS(pos));
  130. retval = lcd_busy_wait(info);
  131. if (retval < 0)
  132. break;
  133. src[len] = lcd_read_data(info);
  134. if (pos == 0x0f)
  135. pos = 0x40;
  136. else
  137. pos++;
  138. }
  139. if (retval < 0 && signal_pending(current))
  140. return -ERESTARTSYS;
  141. if (copy_to_user(buf, src, len))
  142. return -EFAULT;
  143. *ppos += len;
  144. return len;
  145. }
  146. static ssize_t cobalt_lcdfb_write(struct fb_info *info, const char __user *buf,
  147. size_t count, loff_t *ppos)
  148. {
  149. char dst[LCD_CHARS_MAX];
  150. unsigned long pos;
  151. int len, retval = 0;
  152. pos = *ppos;
  153. if (pos >= LCD_CHARS_MAX || count == 0)
  154. return 0;
  155. if (count > LCD_CHARS_MAX)
  156. count = LCD_CHARS_MAX;
  157. if (pos + count > LCD_CHARS_MAX)
  158. count = LCD_CHARS_MAX - pos;
  159. if (copy_from_user(dst, buf, count))
  160. return -EFAULT;
  161. for (len = 0; len < count; len++) {
  162. retval = lcd_busy_wait(info);
  163. if (retval < 0)
  164. break;
  165. lcd_write_control(info, LCD_TEXT_POS(pos));
  166. retval = lcd_busy_wait(info);
  167. if (retval < 0)
  168. break;
  169. lcd_write_data(info, dst[len]);
  170. if (pos == 0x0f)
  171. pos = 0x40;
  172. else
  173. pos++;
  174. }
  175. if (retval < 0 && signal_pending(current))
  176. return -ERESTARTSYS;
  177. *ppos += len;
  178. return len;
  179. }
  180. static int cobalt_lcdfb_blank(int blank_mode, struct fb_info *info)
  181. {
  182. int retval;
  183. retval = lcd_busy_wait(info);
  184. if (retval < 0)
  185. return retval;
  186. switch (blank_mode) {
  187. case FB_BLANK_UNBLANK:
  188. lcd_write_control(info, LCD_ON);
  189. break;
  190. default:
  191. lcd_write_control(info, LCD_OFF);
  192. break;
  193. }
  194. return 0;
  195. }
  196. static int cobalt_lcdfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  197. {
  198. u32 x, y;
  199. int retval;
  200. switch (cursor->set) {
  201. case FB_CUR_SETPOS:
  202. x = cursor->image.dx;
  203. y = cursor->image.dy;
  204. if (x >= LCD_XRES_MAX || y >= LCD_YRES_MAX)
  205. return -EINVAL;
  206. retval = lcd_busy_wait(info);
  207. if (retval < 0)
  208. return retval;
  209. lcd_write_control(info,
  210. LCD_TEXT_POS(info->fix.line_length * y + x));
  211. break;
  212. default:
  213. return -EINVAL;
  214. }
  215. retval = lcd_busy_wait(info);
  216. if (retval < 0)
  217. return retval;
  218. if (cursor->enable)
  219. lcd_write_control(info, LCD_CURSOR_ON);
  220. else
  221. lcd_write_control(info, LCD_CURSOR_OFF);
  222. return 0;
  223. }
  224. static struct fb_ops cobalt_lcd_fbops = {
  225. .owner = THIS_MODULE,
  226. .fb_read = cobalt_lcdfb_read,
  227. .fb_write = cobalt_lcdfb_write,
  228. .fb_blank = cobalt_lcdfb_blank,
  229. .fb_cursor = cobalt_lcdfb_cursor,
  230. };
  231. static int __devinit cobalt_lcdfb_probe(struct platform_device *dev)
  232. {
  233. struct fb_info *info;
  234. struct resource *res;
  235. int retval;
  236. info = framebuffer_alloc(0, &dev->dev);
  237. if (!info)
  238. return -ENOMEM;
  239. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  240. if (!res) {
  241. framebuffer_release(info);
  242. return -EBUSY;
  243. }
  244. info->screen_size = res->end - res->start + 1;
  245. info->screen_base = ioremap(res->start, info->screen_size);
  246. info->fbops = &cobalt_lcd_fbops;
  247. info->fix = cobalt_lcdfb_fix;
  248. info->fix.smem_start = res->start;
  249. info->fix.smem_len = info->screen_size;
  250. info->pseudo_palette = NULL;
  251. info->par = NULL;
  252. info->flags = FBINFO_DEFAULT;
  253. retval = register_framebuffer(info);
  254. if (retval < 0) {
  255. iounmap(info->screen_base);
  256. framebuffer_release(info);
  257. return retval;
  258. }
  259. platform_set_drvdata(dev, info);
  260. lcd_clear(info);
  261. printk(KERN_INFO "fb%d: Cobalt server LCD frame buffer device\n",
  262. info->node);
  263. return 0;
  264. }
  265. static int __devexit cobalt_lcdfb_remove(struct platform_device *dev)
  266. {
  267. struct fb_info *info;
  268. info = platform_get_drvdata(dev);
  269. if (info) {
  270. iounmap(info->screen_base);
  271. unregister_framebuffer(info);
  272. framebuffer_release(info);
  273. }
  274. return 0;
  275. }
  276. static struct platform_driver cobalt_lcdfb_driver = {
  277. .probe = cobalt_lcdfb_probe,
  278. .remove = __devexit_p(cobalt_lcdfb_remove),
  279. .driver = {
  280. .name = "cobalt-lcd",
  281. .owner = THIS_MODULE,
  282. },
  283. };
  284. static int __init cobalt_lcdfb_init(void)
  285. {
  286. return platform_driver_register(&cobalt_lcdfb_driver);
  287. }
  288. static void __exit cobalt_lcdfb_exit(void)
  289. {
  290. platform_driver_unregister(&cobalt_lcdfb_driver);
  291. }
  292. module_init(cobalt_lcdfb_init);
  293. module_exit(cobalt_lcdfb_exit);
  294. MODULE_LICENSE("GPL v2");
  295. MODULE_AUTHOR("Yoichi Yuasa");
  296. MODULE_DESCRIPTION("Cobalt server LCD frame buffer driver");