cobalt_lcdfb.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Cobalt/SEAD3 LCD frame buffer driver.
  3. *
  4. * Copyright (C) 2008 Yoichi Yuasa <yuasa@linux-mips.org>
  5. * Copyright (C) 2012 MIPS Technologies, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/fb.h>
  23. #include <linux/init.h>
  24. #include <linux/io.h>
  25. #include <linux/ioport.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/module.h>
  29. /*
  30. * Cursor position address
  31. * \X 0 1 2 ... 14 15
  32. * Y+----+----+----+---+----+----+
  33. * 0|0x00|0x01|0x02|...|0x0e|0x0f|
  34. * +----+----+----+---+----+----+
  35. * 1|0x40|0x41|0x42|...|0x4e|0x4f|
  36. * +----+----+----+---+----+----+
  37. */
  38. #define LCD_DATA_REG_OFFSET 0x10
  39. #define LCD_XRES_MAX 16
  40. #define LCD_YRES_MAX 2
  41. #define LCD_CHARS_MAX 32
  42. #define LCD_CLEAR 0x01
  43. #define LCD_CURSOR_MOVE_HOME 0x02
  44. #define LCD_RESET 0x06
  45. #define LCD_OFF 0x08
  46. #define LCD_CURSOR_OFF 0x0c
  47. #define LCD_CURSOR_BLINK_OFF 0x0e
  48. #define LCD_CURSOR_ON 0x0f
  49. #define LCD_ON LCD_CURSOR_ON
  50. #define LCD_CURSOR_MOVE_LEFT 0x10
  51. #define LCD_CURSOR_MOVE_RIGHT 0x14
  52. #define LCD_DISPLAY_LEFT 0x18
  53. #define LCD_DISPLAY_RIGHT 0x1c
  54. #define LCD_PRERESET 0x3f /* execute 4 times continuously */
  55. #define LCD_BUSY 0x80
  56. #define LCD_GRAPHIC_MODE 0x40
  57. #define LCD_TEXT_MODE 0x80
  58. #define LCD_CUR_POS_MASK 0x7f
  59. #define LCD_CUR_POS(x) ((x) & LCD_CUR_POS_MASK)
  60. #define LCD_TEXT_POS(x) ((x) | LCD_TEXT_MODE)
  61. #ifdef CONFIG_MIPS_COBALT
  62. static inline void lcd_write_control(struct fb_info *info, u8 control)
  63. {
  64. writel((u32)control << 24, info->screen_base);
  65. }
  66. static inline u8 lcd_read_control(struct fb_info *info)
  67. {
  68. return readl(info->screen_base) >> 24;
  69. }
  70. static inline void lcd_write_data(struct fb_info *info, u8 data)
  71. {
  72. writel((u32)data << 24, info->screen_base + LCD_DATA_REG_OFFSET);
  73. }
  74. static inline u8 lcd_read_data(struct fb_info *info)
  75. {
  76. return readl(info->screen_base + LCD_DATA_REG_OFFSET) >> 24;
  77. }
  78. #else
  79. #define LCD_CTL 0x00
  80. #define LCD_DATA 0x08
  81. #define CPLD_STATUS 0x10
  82. #define CPLD_DATA 0x18
  83. static inline void cpld_wait(struct fb_info *info)
  84. {
  85. do {
  86. } while (readl(info->screen_base + CPLD_STATUS) & 1);
  87. }
  88. static inline void lcd_write_control(struct fb_info *info, u8 control)
  89. {
  90. cpld_wait(info);
  91. writel(control, info->screen_base + LCD_CTL);
  92. }
  93. static inline u8 lcd_read_control(struct fb_info *info)
  94. {
  95. cpld_wait(info);
  96. readl(info->screen_base + LCD_CTL);
  97. cpld_wait(info);
  98. return readl(info->screen_base + CPLD_DATA) & 0xff;
  99. }
  100. static inline void lcd_write_data(struct fb_info *info, u8 data)
  101. {
  102. cpld_wait(info);
  103. writel(data, info->screen_base + LCD_DATA);
  104. }
  105. static inline u8 lcd_read_data(struct fb_info *info)
  106. {
  107. cpld_wait(info);
  108. readl(info->screen_base + LCD_DATA);
  109. cpld_wait(info);
  110. return readl(info->screen_base + CPLD_DATA) & 0xff;
  111. }
  112. #endif
  113. static int lcd_busy_wait(struct fb_info *info)
  114. {
  115. u8 val = 0;
  116. int timeout = 10, retval = 0;
  117. do {
  118. val = lcd_read_control(info);
  119. val &= LCD_BUSY;
  120. if (val != LCD_BUSY)
  121. break;
  122. if (msleep_interruptible(1))
  123. return -EINTR;
  124. timeout--;
  125. } while (timeout);
  126. if (val == LCD_BUSY)
  127. retval = -EBUSY;
  128. return retval;
  129. }
  130. static void lcd_clear(struct fb_info *info)
  131. {
  132. int i;
  133. for (i = 0; i < 4; i++) {
  134. udelay(150);
  135. lcd_write_control(info, LCD_PRERESET);
  136. }
  137. udelay(150);
  138. lcd_write_control(info, LCD_CLEAR);
  139. udelay(150);
  140. lcd_write_control(info, LCD_RESET);
  141. }
  142. static struct fb_fix_screeninfo cobalt_lcdfb_fix = {
  143. .id = "cobalt-lcd",
  144. .type = FB_TYPE_TEXT,
  145. .type_aux = FB_AUX_TEXT_MDA,
  146. .visual = FB_VISUAL_MONO01,
  147. .line_length = LCD_XRES_MAX,
  148. .accel = FB_ACCEL_NONE,
  149. };
  150. static ssize_t cobalt_lcdfb_read(struct fb_info *info, char __user *buf,
  151. size_t count, loff_t *ppos)
  152. {
  153. char src[LCD_CHARS_MAX];
  154. unsigned long pos;
  155. int len, retval = 0;
  156. pos = *ppos;
  157. if (pos >= LCD_CHARS_MAX || count == 0)
  158. return 0;
  159. if (count > LCD_CHARS_MAX)
  160. count = LCD_CHARS_MAX;
  161. if (pos + count > LCD_CHARS_MAX)
  162. count = LCD_CHARS_MAX - pos;
  163. for (len = 0; len < count; len++) {
  164. retval = lcd_busy_wait(info);
  165. if (retval < 0)
  166. break;
  167. lcd_write_control(info, LCD_TEXT_POS(pos));
  168. retval = lcd_busy_wait(info);
  169. if (retval < 0)
  170. break;
  171. src[len] = lcd_read_data(info);
  172. if (pos == 0x0f)
  173. pos = 0x40;
  174. else
  175. pos++;
  176. }
  177. if (retval < 0 && signal_pending(current))
  178. return -ERESTARTSYS;
  179. if (copy_to_user(buf, src, len))
  180. return -EFAULT;
  181. *ppos += len;
  182. return len;
  183. }
  184. static ssize_t cobalt_lcdfb_write(struct fb_info *info, const char __user *buf,
  185. size_t count, loff_t *ppos)
  186. {
  187. char dst[LCD_CHARS_MAX];
  188. unsigned long pos;
  189. int len, retval = 0;
  190. pos = *ppos;
  191. if (pos >= LCD_CHARS_MAX || count == 0)
  192. return 0;
  193. if (count > LCD_CHARS_MAX)
  194. count = LCD_CHARS_MAX;
  195. if (pos + count > LCD_CHARS_MAX)
  196. count = LCD_CHARS_MAX - pos;
  197. if (copy_from_user(dst, buf, count))
  198. return -EFAULT;
  199. for (len = 0; len < count; len++) {
  200. retval = lcd_busy_wait(info);
  201. if (retval < 0)
  202. break;
  203. lcd_write_control(info, LCD_TEXT_POS(pos));
  204. retval = lcd_busy_wait(info);
  205. if (retval < 0)
  206. break;
  207. lcd_write_data(info, dst[len]);
  208. if (pos == 0x0f)
  209. pos = 0x40;
  210. else
  211. pos++;
  212. }
  213. if (retval < 0 && signal_pending(current))
  214. return -ERESTARTSYS;
  215. *ppos += len;
  216. return len;
  217. }
  218. static int cobalt_lcdfb_blank(int blank_mode, struct fb_info *info)
  219. {
  220. int retval;
  221. retval = lcd_busy_wait(info);
  222. if (retval < 0)
  223. return retval;
  224. switch (blank_mode) {
  225. case FB_BLANK_UNBLANK:
  226. lcd_write_control(info, LCD_ON);
  227. break;
  228. default:
  229. lcd_write_control(info, LCD_OFF);
  230. break;
  231. }
  232. return 0;
  233. }
  234. static int cobalt_lcdfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  235. {
  236. u32 x, y;
  237. int retval;
  238. switch (cursor->set) {
  239. case FB_CUR_SETPOS:
  240. x = cursor->image.dx;
  241. y = cursor->image.dy;
  242. if (x >= LCD_XRES_MAX || y >= LCD_YRES_MAX)
  243. return -EINVAL;
  244. retval = lcd_busy_wait(info);
  245. if (retval < 0)
  246. return retval;
  247. lcd_write_control(info,
  248. LCD_TEXT_POS(info->fix.line_length * y + x));
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. retval = lcd_busy_wait(info);
  254. if (retval < 0)
  255. return retval;
  256. if (cursor->enable)
  257. lcd_write_control(info, LCD_CURSOR_ON);
  258. else
  259. lcd_write_control(info, LCD_CURSOR_OFF);
  260. return 0;
  261. }
  262. static struct fb_ops cobalt_lcd_fbops = {
  263. .owner = THIS_MODULE,
  264. .fb_read = cobalt_lcdfb_read,
  265. .fb_write = cobalt_lcdfb_write,
  266. .fb_blank = cobalt_lcdfb_blank,
  267. .fb_cursor = cobalt_lcdfb_cursor,
  268. };
  269. static int cobalt_lcdfb_probe(struct platform_device *dev)
  270. {
  271. struct fb_info *info;
  272. struct resource *res;
  273. int retval;
  274. info = framebuffer_alloc(0, &dev->dev);
  275. if (!info)
  276. return -ENOMEM;
  277. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  278. if (!res) {
  279. framebuffer_release(info);
  280. return -EBUSY;
  281. }
  282. info->screen_size = resource_size(res);
  283. info->screen_base = devm_ioremap(&dev->dev, res->start,
  284. info->screen_size);
  285. info->fbops = &cobalt_lcd_fbops;
  286. info->fix = cobalt_lcdfb_fix;
  287. info->fix.smem_start = res->start;
  288. info->fix.smem_len = info->screen_size;
  289. info->pseudo_palette = NULL;
  290. info->par = NULL;
  291. info->flags = FBINFO_DEFAULT;
  292. retval = register_framebuffer(info);
  293. if (retval < 0) {
  294. framebuffer_release(info);
  295. return retval;
  296. }
  297. platform_set_drvdata(dev, info);
  298. lcd_clear(info);
  299. fb_info(info, "Cobalt server LCD frame buffer device\n");
  300. return 0;
  301. }
  302. static int cobalt_lcdfb_remove(struct platform_device *dev)
  303. {
  304. struct fb_info *info;
  305. info = platform_get_drvdata(dev);
  306. if (info) {
  307. unregister_framebuffer(info);
  308. framebuffer_release(info);
  309. }
  310. return 0;
  311. }
  312. static struct platform_driver cobalt_lcdfb_driver = {
  313. .probe = cobalt_lcdfb_probe,
  314. .remove = cobalt_lcdfb_remove,
  315. .driver = {
  316. .name = "cobalt-lcd",
  317. },
  318. };
  319. module_platform_driver(cobalt_lcdfb_driver);
  320. MODULE_LICENSE("GPL v2");
  321. MODULE_AUTHOR("Yoichi Yuasa");
  322. MODULE_DESCRIPTION("Cobalt server LCD frame buffer driver");