display7seg.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* $Id: display7seg.c,v 1.6 2002/01/08 16:00:16 davem Exp $
  2. *
  3. * display7seg - Driver implementation for the 7-segment display
  4. * present on Sun Microsystems CP1400 and CP1500
  5. *
  6. * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
  7. *
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/errno.h>
  13. #include <linux/major.h>
  14. #include <linux/init.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/ioport.h> /* request_region */
  17. #include <linux/smp_lock.h>
  18. #include <asm/atomic.h>
  19. #include <asm/ebus.h> /* EBus device */
  20. #include <asm/oplib.h> /* OpenProm Library */
  21. #include <asm/uaccess.h> /* put_/get_user */
  22. #include <asm/io.h>
  23. #include <asm/display7seg.h>
  24. #define D7S_MINOR 193
  25. #define D7S_OBPNAME "display7seg"
  26. #define D7S_DEVNAME "d7s"
  27. static int sol_compat = 0; /* Solaris compatibility mode */
  28. #ifdef MODULE
  29. /* Solaris compatibility flag -
  30. * The Solaris implementation omits support for several
  31. * documented driver features (ref Sun doc 806-0180-03).
  32. * By default, this module supports the documented driver
  33. * abilities, rather than the Solaris implementation:
  34. *
  35. * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode
  36. * upon closure of device or module unload.
  37. * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
  38. * FLIP bit
  39. *
  40. * If you wish the device to operate as under Solaris,
  41. * omitting above features, set this parameter to non-zero.
  42. */
  43. module_param
  44. (sol_compat, int, 0);
  45. MODULE_PARM_DESC
  46. (sol_compat,
  47. "Disables documented functionality omitted from Solaris driver");
  48. MODULE_AUTHOR
  49. ("Eric Brower <ebrower@usa.net>");
  50. MODULE_DESCRIPTION
  51. ("7-Segment Display driver for Sun Microsystems CP1400/1500");
  52. MODULE_LICENSE("GPL");
  53. MODULE_SUPPORTED_DEVICE
  54. ("d7s");
  55. #endif /* ifdef MODULE */
  56. /*
  57. * Register block address- see header for details
  58. * -----------------------------------------
  59. * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
  60. * -----------------------------------------
  61. *
  62. * DP - Toggles decimal point on/off
  63. * ALARM - Toggles "Alarm" LED green/red
  64. * FLIP - Inverts display for upside-down mounted board
  65. * bits 0-4 - 7-segment display contents
  66. */
  67. static void __iomem* d7s_regs;
  68. static inline void d7s_free(void)
  69. {
  70. iounmap(d7s_regs);
  71. }
  72. static inline int d7s_obpflipped(void)
  73. {
  74. int opt_node;
  75. opt_node = prom_getchild(prom_root_node);
  76. opt_node = prom_searchsiblings(opt_node, "options");
  77. return ((-1 != prom_getintdefault(opt_node, "d7s-flipped?", -1)) ? 0 : 1);
  78. }
  79. static atomic_t d7s_users = ATOMIC_INIT(0);
  80. static int d7s_open(struct inode *inode, struct file *f)
  81. {
  82. if (D7S_MINOR != iminor(inode))
  83. return -ENODEV;
  84. atomic_inc(&d7s_users);
  85. return 0;
  86. }
  87. static int d7s_release(struct inode *inode, struct file *f)
  88. {
  89. /* Reset flipped state to OBP default only if
  90. * no other users have the device open and we
  91. * are not operating in solaris-compat mode
  92. */
  93. if (atomic_dec_and_test(&d7s_users) && !sol_compat) {
  94. int regval = 0;
  95. regval = readb(d7s_regs);
  96. (0 == d7s_obpflipped()) ?
  97. writeb(regval |= D7S_FLIP, d7s_regs):
  98. writeb(regval &= ~D7S_FLIP, d7s_regs);
  99. }
  100. return 0;
  101. }
  102. static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  103. {
  104. __u8 regs = readb(d7s_regs);
  105. __u8 ireg = 0;
  106. int error = 0;
  107. if (D7S_MINOR != iminor(file->f_path.dentry->d_inode))
  108. return -ENODEV;
  109. lock_kernel();
  110. switch (cmd) {
  111. case D7SIOCWR:
  112. /* assign device register values
  113. * we mask-out D7S_FLIP if in sol_compat mode
  114. */
  115. if (get_user(ireg, (int __user *) arg)) {
  116. error = -EFAULT;
  117. break;
  118. }
  119. if (0 != sol_compat) {
  120. (regs & D7S_FLIP) ?
  121. (ireg |= D7S_FLIP) : (ireg &= ~D7S_FLIP);
  122. }
  123. writeb(ireg, d7s_regs);
  124. break;
  125. case D7SIOCRD:
  126. /* retrieve device register values
  127. * NOTE: Solaris implementation returns D7S_FLIP bit
  128. * as toggled by user, even though it does not honor it.
  129. * This driver will not misinform you about the state
  130. * of your hardware while in sol_compat mode
  131. */
  132. if (put_user(regs, (int __user *) arg)) {
  133. error = -EFAULT;
  134. break;
  135. }
  136. break;
  137. case D7SIOCTM:
  138. /* toggle device mode-- flip display orientation */
  139. (regs & D7S_FLIP) ?
  140. (regs &= ~D7S_FLIP) : (regs |= D7S_FLIP);
  141. writeb(regs, d7s_regs);
  142. break;
  143. };
  144. unlock_kernel();
  145. return error;
  146. }
  147. static const struct file_operations d7s_fops = {
  148. .owner = THIS_MODULE,
  149. .unlocked_ioctl = d7s_ioctl,
  150. .compat_ioctl = d7s_ioctl,
  151. .open = d7s_open,
  152. .release = d7s_release,
  153. };
  154. static struct miscdevice d7s_miscdev = { D7S_MINOR, D7S_DEVNAME, &d7s_fops };
  155. static int __init d7s_init(void)
  156. {
  157. struct linux_ebus *ebus = NULL;
  158. struct linux_ebus_device *edev = NULL;
  159. int iTmp = 0, regs = 0;
  160. for_each_ebus(ebus) {
  161. for_each_ebusdev(edev, ebus) {
  162. if (!strcmp(edev->prom_node->name, D7S_OBPNAME))
  163. goto ebus_done;
  164. }
  165. }
  166. ebus_done:
  167. if(!edev) {
  168. printk("%s: unable to locate device\n", D7S_DEVNAME);
  169. return -ENODEV;
  170. }
  171. d7s_regs = ioremap(edev->resource[0].start, sizeof(__u8));
  172. iTmp = misc_register(&d7s_miscdev);
  173. if (0 != iTmp) {
  174. printk("%s: unable to acquire miscdevice minor %i\n",
  175. D7S_DEVNAME, D7S_MINOR);
  176. iounmap(d7s_regs);
  177. return iTmp;
  178. }
  179. /* OBP option "d7s-flipped?" is honored as default
  180. * for the device, and reset default when detached
  181. */
  182. regs = readb(d7s_regs);
  183. iTmp = d7s_obpflipped();
  184. (0 == iTmp) ?
  185. writeb(regs |= D7S_FLIP, d7s_regs):
  186. writeb(regs &= ~D7S_FLIP, d7s_regs);
  187. printk("%s: 7-Segment Display%s at 0x%lx %s\n",
  188. D7S_DEVNAME,
  189. (0 == iTmp) ? (" (FLIPPED)") : (""),
  190. edev->resource[0].start,
  191. (0 != sol_compat) ? ("in sol_compat mode") : (""));
  192. return 0;
  193. }
  194. static void __exit d7s_cleanup(void)
  195. {
  196. int regs = readb(d7s_regs);
  197. /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
  198. if (0 == sol_compat) {
  199. (0 == d7s_obpflipped()) ?
  200. writeb(regs |= D7S_FLIP, d7s_regs):
  201. writeb(regs &= ~D7S_FLIP, d7s_regs);
  202. }
  203. misc_deregister(&d7s_miscdev);
  204. d7s_free();
  205. }
  206. module_init(d7s_init);
  207. module_exit(d7s_cleanup);