hecubafb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * linux/drivers/video/hecubafb.c -- FB driver for Hecuba controller
  3. *
  4. * Copyright (C) 2006, Jaya Kumar
  5. * This work was sponsored by CIS(M) Sdn Bhd
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. *
  11. * Layout is based on skeletonfb.c by James Simmons and Geert Uytterhoeven.
  12. * This work was possible because of apollo display code from E-Ink's website
  13. * http://support.eink.com/community
  14. * All information used to write this code is from public material made
  15. * available by E-Ink on its support site. Some commands such as 0xA4
  16. * were found by looping through cmd=0x00 thru 0xFF and supplying random
  17. * values. There are other commands that the display is capable of,
  18. * beyond the 5 used here but they are more complex.
  19. *
  20. * This driver is written to be used with the Hecuba display controller
  21. * board, and tested with the EInk 800x600 display in 1 bit mode.
  22. * The interface between Hecuba and the host is TTL based GPIO. The
  23. * GPIO requirements are 8 writable data lines and 6 lines for control.
  24. * Only 4 of the controls are actually used here but 6 for future use.
  25. * The driver requires the IO addresses for data and control GPIO at
  26. * load time. It is also possible to use this display with a standard
  27. * PC parallel port.
  28. *
  29. * General notes:
  30. * - User must set hecubafb_enable=1 to enable it
  31. * - User must set dio_addr=0xIOADDR cio_addr=0xIOADDR c2io_addr=0xIOADDR
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/errno.h>
  37. #include <linux/string.h>
  38. #include <linux/mm.h>
  39. #include <linux/slab.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/delay.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/fb.h>
  44. #include <linux/init.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/list.h>
  47. #include <asm/uaccess.h>
  48. /* Apollo controller specific defines */
  49. #define APOLLO_START_NEW_IMG 0xA0
  50. #define APOLLO_STOP_IMG_DATA 0xA1
  51. #define APOLLO_DISPLAY_IMG 0xA2
  52. #define APOLLO_ERASE_DISPLAY 0xA3
  53. #define APOLLO_INIT_DISPLAY 0xA4
  54. /* Hecuba interface specific defines */
  55. /* WUP is inverted, CD is inverted, DS is inverted */
  56. #define HCB_NWUP_BIT 0x01
  57. #define HCB_NDS_BIT 0x02
  58. #define HCB_RW_BIT 0x04
  59. #define HCB_NCD_BIT 0x08
  60. #define HCB_ACK_BIT 0x80
  61. /* Display specific information */
  62. #define DPY_W 600
  63. #define DPY_H 800
  64. struct hecubafb_par {
  65. unsigned long dio_addr;
  66. unsigned long cio_addr;
  67. unsigned long c2io_addr;
  68. unsigned char ctl;
  69. struct fb_info *info;
  70. unsigned int irq;
  71. };
  72. static struct fb_fix_screeninfo hecubafb_fix __devinitdata = {
  73. .id = "hecubafb",
  74. .type = FB_TYPE_PACKED_PIXELS,
  75. .visual = FB_VISUAL_MONO01,
  76. .xpanstep = 0,
  77. .ypanstep = 0,
  78. .ywrapstep = 0,
  79. .accel = FB_ACCEL_NONE,
  80. };
  81. static struct fb_var_screeninfo hecubafb_var __devinitdata = {
  82. .xres = DPY_W,
  83. .yres = DPY_H,
  84. .xres_virtual = DPY_W,
  85. .yres_virtual = DPY_H,
  86. .bits_per_pixel = 1,
  87. .nonstd = 1,
  88. };
  89. static unsigned long dio_addr;
  90. static unsigned long cio_addr;
  91. static unsigned long c2io_addr;
  92. static unsigned long splashval;
  93. static unsigned int nosplash;
  94. static unsigned int hecubafb_enable;
  95. static unsigned int irq;
  96. static DECLARE_WAIT_QUEUE_HEAD(hecubafb_waitq);
  97. static void hcb_set_ctl(struct hecubafb_par *par)
  98. {
  99. outb(par->ctl, par->cio_addr);
  100. }
  101. static unsigned char hcb_get_ctl(struct hecubafb_par *par)
  102. {
  103. return inb(par->c2io_addr);
  104. }
  105. static void hcb_set_data(struct hecubafb_par *par, unsigned char value)
  106. {
  107. outb(value, par->dio_addr);
  108. }
  109. static int __devinit apollo_init_control(struct hecubafb_par *par)
  110. {
  111. unsigned char ctl;
  112. /* for init, we want the following setup to be set:
  113. WUP = lo
  114. ACK = hi
  115. DS = hi
  116. RW = hi
  117. CD = lo
  118. */
  119. /* write WUP to lo, DS to hi, RW to hi, CD to lo */
  120. par->ctl = HCB_NWUP_BIT | HCB_RW_BIT | HCB_NCD_BIT ;
  121. par->ctl &= ~HCB_NDS_BIT;
  122. hcb_set_ctl(par);
  123. /* check ACK is not lo */
  124. ctl = hcb_get_ctl(par);
  125. if ((ctl & HCB_ACK_BIT)) {
  126. printk(KERN_ERR "Fail because ACK is already low\n");
  127. return -ENXIO;
  128. }
  129. return 0;
  130. }
  131. static void hcb_wait_for_ack(struct hecubafb_par *par)
  132. {
  133. int timeout;
  134. unsigned char ctl;
  135. timeout=500;
  136. do {
  137. ctl = hcb_get_ctl(par);
  138. if ((ctl & HCB_ACK_BIT))
  139. return;
  140. udelay(1);
  141. } while (timeout--);
  142. printk(KERN_ERR "timed out waiting for ack\n");
  143. }
  144. static void hcb_wait_for_ack_clear(struct hecubafb_par *par)
  145. {
  146. int timeout;
  147. unsigned char ctl;
  148. timeout=500;
  149. do {
  150. ctl = hcb_get_ctl(par);
  151. if (!(ctl & HCB_ACK_BIT))
  152. return;
  153. udelay(1);
  154. } while (timeout--);
  155. printk(KERN_ERR "timed out waiting for clear\n");
  156. }
  157. static void apollo_send_data(struct hecubafb_par *par, unsigned char data)
  158. {
  159. /* set data */
  160. hcb_set_data(par, data);
  161. /* set DS low */
  162. par->ctl |= HCB_NDS_BIT;
  163. hcb_set_ctl(par);
  164. hcb_wait_for_ack(par);
  165. /* set DS hi */
  166. par->ctl &= ~(HCB_NDS_BIT);
  167. hcb_set_ctl(par);
  168. hcb_wait_for_ack_clear(par);
  169. }
  170. static void apollo_send_command(struct hecubafb_par *par, unsigned char data)
  171. {
  172. /* command so set CD to high */
  173. par->ctl &= ~(HCB_NCD_BIT);
  174. hcb_set_ctl(par);
  175. /* actually strobe with command */
  176. apollo_send_data(par, data);
  177. /* clear CD back to low */
  178. par->ctl |= (HCB_NCD_BIT);
  179. hcb_set_ctl(par);
  180. }
  181. /* main hecubafb functions */
  182. static void hecubafb_dpy_update(struct hecubafb_par *par)
  183. {
  184. int i;
  185. unsigned char *buf = (unsigned char __force *)par->info->screen_base;
  186. apollo_send_command(par, 0xA0);
  187. for (i=0; i < (DPY_W*DPY_H/8); i++) {
  188. apollo_send_data(par, *(buf++));
  189. }
  190. apollo_send_command(par, 0xA1);
  191. apollo_send_command(par, 0xA2);
  192. }
  193. /* this is called back from the deferred io workqueue */
  194. static void hecubafb_dpy_deferred_io(struct fb_info *info,
  195. struct list_head *pagelist)
  196. {
  197. hecubafb_dpy_update(info->par);
  198. }
  199. static void hecubafb_fillrect(struct fb_info *info,
  200. const struct fb_fillrect *rect)
  201. {
  202. struct hecubafb_par *par = info->par;
  203. sys_fillrect(info, rect);
  204. hecubafb_dpy_update(par);
  205. }
  206. static void hecubafb_copyarea(struct fb_info *info,
  207. const struct fb_copyarea *area)
  208. {
  209. struct hecubafb_par *par = info->par;
  210. sys_copyarea(info, area);
  211. hecubafb_dpy_update(par);
  212. }
  213. static void hecubafb_imageblit(struct fb_info *info,
  214. const struct fb_image *image)
  215. {
  216. struct hecubafb_par *par = info->par;
  217. sys_imageblit(info, image);
  218. hecubafb_dpy_update(par);
  219. }
  220. /*
  221. * this is the slow path from userspace. they can seek and write to
  222. * the fb. it's inefficient to do anything less than a full screen draw
  223. */
  224. static ssize_t hecubafb_write(struct fb_info *info, const char __user *buf,
  225. size_t count, loff_t *ppos)
  226. {
  227. unsigned long p;
  228. int err=-EINVAL;
  229. struct hecubafb_par *par;
  230. unsigned int xres;
  231. unsigned int fbmemlength;
  232. p = *ppos;
  233. par = info->par;
  234. xres = info->var.xres;
  235. fbmemlength = (xres * info->var.yres)/8;
  236. if (p > fbmemlength)
  237. return -ENOSPC;
  238. err = 0;
  239. if ((count + p) > fbmemlength) {
  240. count = fbmemlength - p;
  241. err = -ENOSPC;
  242. }
  243. if (count) {
  244. char *base_addr;
  245. base_addr = (char __force *)info->screen_base;
  246. count -= copy_from_user(base_addr + p, buf, count);
  247. *ppos += count;
  248. err = -EFAULT;
  249. }
  250. hecubafb_dpy_update(par);
  251. if (count)
  252. return count;
  253. return err;
  254. }
  255. static struct fb_ops hecubafb_ops = {
  256. .owner = THIS_MODULE,
  257. .fb_read = fb_sys_read,
  258. .fb_write = hecubafb_write,
  259. .fb_fillrect = hecubafb_fillrect,
  260. .fb_copyarea = hecubafb_copyarea,
  261. .fb_imageblit = hecubafb_imageblit,
  262. };
  263. static struct fb_deferred_io hecubafb_defio = {
  264. .delay = HZ,
  265. .deferred_io = hecubafb_dpy_deferred_io,
  266. };
  267. static int __devinit hecubafb_probe(struct platform_device *dev)
  268. {
  269. struct fb_info *info;
  270. int retval = -ENOMEM;
  271. int videomemorysize;
  272. unsigned char *videomemory;
  273. struct hecubafb_par *par;
  274. videomemorysize = (DPY_W*DPY_H)/8;
  275. if (!(videomemory = vmalloc(videomemorysize)))
  276. return retval;
  277. memset(videomemory, 0, videomemorysize);
  278. info = framebuffer_alloc(sizeof(struct hecubafb_par), &dev->dev);
  279. if (!info)
  280. goto err;
  281. info->screen_base = (char __iomem *) videomemory;
  282. info->fbops = &hecubafb_ops;
  283. info->var = hecubafb_var;
  284. info->fix = hecubafb_fix;
  285. info->fix.smem_len = videomemorysize;
  286. par = info->par;
  287. par->info = info;
  288. if (!dio_addr || !cio_addr || !c2io_addr) {
  289. printk(KERN_WARNING "no IO addresses supplied\n");
  290. goto err1;
  291. }
  292. par->dio_addr = dio_addr;
  293. par->cio_addr = cio_addr;
  294. par->c2io_addr = c2io_addr;
  295. info->flags = FBINFO_FLAG_DEFAULT;
  296. info->fbdefio = &hecubafb_defio;
  297. fb_deferred_io_init(info);
  298. retval = register_framebuffer(info);
  299. if (retval < 0)
  300. goto err1;
  301. platform_set_drvdata(dev, info);
  302. printk(KERN_INFO
  303. "fb%d: Hecuba frame buffer device, using %dK of video memory\n",
  304. info->node, videomemorysize >> 10);
  305. /* this inits the dpy */
  306. apollo_init_control(par);
  307. apollo_send_command(par, APOLLO_INIT_DISPLAY);
  308. apollo_send_data(par, 0x81);
  309. /* have to wait while display resets */
  310. udelay(1000);
  311. /* if we were told to splash the screen, we just clear it */
  312. if (!nosplash) {
  313. apollo_send_command(par, APOLLO_ERASE_DISPLAY);
  314. apollo_send_data(par, splashval);
  315. }
  316. return 0;
  317. err1:
  318. framebuffer_release(info);
  319. err:
  320. vfree(videomemory);
  321. return retval;
  322. }
  323. static int __devexit hecubafb_remove(struct platform_device *dev)
  324. {
  325. struct fb_info *info = platform_get_drvdata(dev);
  326. if (info) {
  327. fb_deferred_io_cleanup(info);
  328. unregister_framebuffer(info);
  329. vfree((void __force *)info->screen_base);
  330. framebuffer_release(info);
  331. }
  332. return 0;
  333. }
  334. static struct platform_driver hecubafb_driver = {
  335. .probe = hecubafb_probe,
  336. .remove = hecubafb_remove,
  337. .driver = {
  338. .name = "hecubafb",
  339. },
  340. };
  341. static struct platform_device *hecubafb_device;
  342. static int __init hecubafb_init(void)
  343. {
  344. int ret;
  345. if (!hecubafb_enable) {
  346. printk(KERN_ERR "Use hecubafb_enable to enable the device\n");
  347. return -ENXIO;
  348. }
  349. ret = platform_driver_register(&hecubafb_driver);
  350. if (!ret) {
  351. hecubafb_device = platform_device_alloc("hecubafb", 0);
  352. if (hecubafb_device)
  353. ret = platform_device_add(hecubafb_device);
  354. else
  355. ret = -ENOMEM;
  356. if (ret) {
  357. platform_device_put(hecubafb_device);
  358. platform_driver_unregister(&hecubafb_driver);
  359. }
  360. }
  361. return ret;
  362. }
  363. static void __exit hecubafb_exit(void)
  364. {
  365. platform_device_unregister(hecubafb_device);
  366. platform_driver_unregister(&hecubafb_driver);
  367. }
  368. module_param(nosplash, uint, 0);
  369. MODULE_PARM_DESC(nosplash, "Disable doing the splash screen");
  370. module_param(hecubafb_enable, uint, 0);
  371. MODULE_PARM_DESC(hecubafb_enable, "Enable communication with Hecuba board");
  372. module_param(dio_addr, ulong, 0);
  373. MODULE_PARM_DESC(dio_addr, "IO address for data, eg: 0x480");
  374. module_param(cio_addr, ulong, 0);
  375. MODULE_PARM_DESC(cio_addr, "IO address for control, eg: 0x400");
  376. module_param(c2io_addr, ulong, 0);
  377. MODULE_PARM_DESC(c2io_addr, "IO address for secondary control, eg: 0x408");
  378. module_param(splashval, ulong, 0);
  379. MODULE_PARM_DESC(splashval, "Splash pattern: 0x00 is black, 0x01 is white");
  380. module_param(irq, uint, 0);
  381. MODULE_PARM_DESC(irq, "IRQ for the Hecuba board");
  382. module_init(hecubafb_init);
  383. module_exit(hecubafb_exit);
  384. MODULE_DESCRIPTION("fbdev driver for Hecuba board");
  385. MODULE_AUTHOR("Jaya Kumar");
  386. MODULE_LICENSE("GPL");