display7seg.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* display7seg.c - Driver implementation for the 7-segment display
  2. * present on Sun Microsystems CP1400 and CP1500
  3. *
  4. * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/errno.h>
  10. #include <linux/major.h>
  11. #include <linux/init.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/ioport.h> /* request_region */
  14. #include <linux/smp_lock.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <asm/atomic.h>
  18. #include <asm/uaccess.h> /* put_/get_user */
  19. #include <asm/io.h>
  20. #include <asm/display7seg.h>
  21. #define D7S_MINOR 193
  22. #define DRIVER_NAME "d7s"
  23. #define PFX DRIVER_NAME ": "
  24. static int sol_compat = 0; /* Solaris compatibility mode */
  25. /* Solaris compatibility flag -
  26. * The Solaris implementation omits support for several
  27. * documented driver features (ref Sun doc 806-0180-03).
  28. * By default, this module supports the documented driver
  29. * abilities, rather than the Solaris implementation:
  30. *
  31. * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode
  32. * upon closure of device or module unload.
  33. * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
  34. * FLIP bit
  35. *
  36. * If you wish the device to operate as under Solaris,
  37. * omitting above features, set this parameter to non-zero.
  38. */
  39. module_param(sol_compat, int, 0);
  40. MODULE_PARM_DESC(sol_compat,
  41. "Disables documented functionality omitted from Solaris driver");
  42. MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
  43. MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
  44. MODULE_LICENSE("GPL");
  45. MODULE_SUPPORTED_DEVICE("d7s");
  46. struct d7s {
  47. void __iomem *regs;
  48. bool flipped;
  49. };
  50. struct d7s *d7s_device;
  51. /*
  52. * Register block address- see header for details
  53. * -----------------------------------------
  54. * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
  55. * -----------------------------------------
  56. *
  57. * DP - Toggles decimal point on/off
  58. * ALARM - Toggles "Alarm" LED green/red
  59. * FLIP - Inverts display for upside-down mounted board
  60. * bits 0-4 - 7-segment display contents
  61. */
  62. static atomic_t d7s_users = ATOMIC_INIT(0);
  63. static int d7s_open(struct inode *inode, struct file *f)
  64. {
  65. if (D7S_MINOR != iminor(inode))
  66. return -ENODEV;
  67. cycle_kernel_lock();
  68. atomic_inc(&d7s_users);
  69. return 0;
  70. }
  71. static int d7s_release(struct inode *inode, struct file *f)
  72. {
  73. /* Reset flipped state to OBP default only if
  74. * no other users have the device open and we
  75. * are not operating in solaris-compat mode
  76. */
  77. if (atomic_dec_and_test(&d7s_users) && !sol_compat) {
  78. struct d7s *p = d7s_device;
  79. u8 regval = 0;
  80. regval = readb(p->regs);
  81. if (p->flipped)
  82. regval |= D7S_FLIP;
  83. else
  84. regval &= ~D7S_FLIP;
  85. writeb(regval, p->regs);
  86. }
  87. return 0;
  88. }
  89. static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  90. {
  91. struct d7s *p = d7s_device;
  92. u8 regs = readb(p->regs);
  93. int error = 0;
  94. u8 ireg = 0;
  95. if (D7S_MINOR != iminor(file->f_path.dentry->d_inode))
  96. return -ENODEV;
  97. lock_kernel();
  98. switch (cmd) {
  99. case D7SIOCWR:
  100. /* assign device register values we mask-out D7S_FLIP
  101. * if in sol_compat mode
  102. */
  103. if (get_user(ireg, (int __user *) arg)) {
  104. error = -EFAULT;
  105. break;
  106. }
  107. if (sol_compat) {
  108. if (regs & D7S_FLIP)
  109. ireg |= D7S_FLIP;
  110. else
  111. ireg &= ~D7S_FLIP;
  112. }
  113. writeb(ireg, p->regs);
  114. break;
  115. case D7SIOCRD:
  116. /* retrieve device register values
  117. * NOTE: Solaris implementation returns D7S_FLIP bit
  118. * as toggled by user, even though it does not honor it.
  119. * This driver will not misinform you about the state
  120. * of your hardware while in sol_compat mode
  121. */
  122. if (put_user(regs, (int __user *) arg)) {
  123. error = -EFAULT;
  124. break;
  125. }
  126. break;
  127. case D7SIOCTM:
  128. /* toggle device mode-- flip display orientation */
  129. if (regs & D7S_FLIP)
  130. regs &= ~D7S_FLIP;
  131. else
  132. regs |= D7S_FLIP;
  133. writeb(regs, p->regs);
  134. break;
  135. };
  136. unlock_kernel();
  137. return error;
  138. }
  139. static const struct file_operations d7s_fops = {
  140. .owner = THIS_MODULE,
  141. .unlocked_ioctl = d7s_ioctl,
  142. .compat_ioctl = d7s_ioctl,
  143. .open = d7s_open,
  144. .release = d7s_release,
  145. };
  146. static struct miscdevice d7s_miscdev = {
  147. .minor = D7S_MINOR,
  148. .name = DRIVER_NAME,
  149. .fops = &d7s_fops
  150. };
  151. static int __devinit d7s_probe(struct of_device *op,
  152. const struct of_device_id *match)
  153. {
  154. struct device_node *opts;
  155. int err = -EINVAL;
  156. struct d7s *p;
  157. u8 regs;
  158. if (d7s_device)
  159. goto out;
  160. p = kzalloc(sizeof(*p), GFP_KERNEL);
  161. err = -ENOMEM;
  162. if (!p)
  163. goto out;
  164. p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s");
  165. if (!p->regs) {
  166. printk(KERN_ERR PFX "Cannot map chip registers\n");
  167. goto out_free;
  168. }
  169. err = misc_register(&d7s_miscdev);
  170. if (err) {
  171. printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n",
  172. D7S_MINOR);
  173. goto out_iounmap;
  174. }
  175. /* OBP option "d7s-flipped?" is honored as default for the
  176. * device, and reset default when detached
  177. */
  178. regs = readb(p->regs);
  179. opts = of_find_node_by_path("/options");
  180. if (opts &&
  181. of_get_property(opts, "d7s-flipped?", NULL))
  182. p->flipped = true;
  183. if (p->flipped)
  184. regs |= D7S_FLIP;
  185. else
  186. regs &= ~D7S_FLIP;
  187. writeb(regs, p->regs);
  188. printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%lx] %s\n",
  189. op->node->full_name,
  190. (regs & D7S_FLIP) ? " (FLIPPED)" : "",
  191. op->resource[0].start,
  192. sol_compat ? "in sol_compat mode" : "");
  193. dev_set_drvdata(&op->dev, p);
  194. d7s_device = p;
  195. err = 0;
  196. out:
  197. return err;
  198. out_iounmap:
  199. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  200. out_free:
  201. kfree(p);
  202. goto out;
  203. }
  204. static int __devexit d7s_remove(struct of_device *op)
  205. {
  206. struct d7s *p = dev_get_drvdata(&op->dev);
  207. u8 regs = readb(p->regs);
  208. /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
  209. if (sol_compat) {
  210. if (p->flipped)
  211. regs |= D7S_FLIP;
  212. else
  213. regs &= ~D7S_FLIP;
  214. writeb(regs, p->regs);
  215. }
  216. misc_deregister(&d7s_miscdev);
  217. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  218. kfree(p);
  219. return 0;
  220. }
  221. static const struct of_device_id d7s_match[] = {
  222. {
  223. .name = "display7seg",
  224. },
  225. {},
  226. };
  227. MODULE_DEVICE_TABLE(of, d7s_match);
  228. static struct of_platform_driver d7s_driver = {
  229. .name = DRIVER_NAME,
  230. .match_table = d7s_match,
  231. .probe = d7s_probe,
  232. .remove = __devexit_p(d7s_remove),
  233. };
  234. static int __init d7s_init(void)
  235. {
  236. return of_register_driver(&d7s_driver, &of_bus_type);
  237. }
  238. static void __exit d7s_exit(void)
  239. {
  240. of_unregister_driver(&d7s_driver);
  241. }
  242. module_init(d7s_init);
  243. module_exit(d7s_exit);