display7seg.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. cycle_kernel_lock();
  85. atomic_inc(&d7s_users);
  86. return 0;
  87. }
  88. static int d7s_release(struct inode *inode, struct file *f)
  89. {
  90. /* Reset flipped state to OBP default only if
  91. * no other users have the device open and we
  92. * are not operating in solaris-compat mode
  93. */
  94. if (atomic_dec_and_test(&d7s_users) && !sol_compat) {
  95. int regval = 0;
  96. regval = readb(d7s_regs);
  97. (0 == d7s_obpflipped()) ?
  98. writeb(regval |= D7S_FLIP, d7s_regs):
  99. writeb(regval &= ~D7S_FLIP, d7s_regs);
  100. }
  101. return 0;
  102. }
  103. static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  104. {
  105. __u8 regs = readb(d7s_regs);
  106. __u8 ireg = 0;
  107. int error = 0;
  108. if (D7S_MINOR != iminor(file->f_path.dentry->d_inode))
  109. return -ENODEV;
  110. lock_kernel();
  111. switch (cmd) {
  112. case D7SIOCWR:
  113. /* assign device register values
  114. * we mask-out D7S_FLIP if in sol_compat mode
  115. */
  116. if (get_user(ireg, (int __user *) arg)) {
  117. error = -EFAULT;
  118. break;
  119. }
  120. if (0 != sol_compat) {
  121. (regs & D7S_FLIP) ?
  122. (ireg |= D7S_FLIP) : (ireg &= ~D7S_FLIP);
  123. }
  124. writeb(ireg, d7s_regs);
  125. break;
  126. case D7SIOCRD:
  127. /* retrieve device register values
  128. * NOTE: Solaris implementation returns D7S_FLIP bit
  129. * as toggled by user, even though it does not honor it.
  130. * This driver will not misinform you about the state
  131. * of your hardware while in sol_compat mode
  132. */
  133. if (put_user(regs, (int __user *) arg)) {
  134. error = -EFAULT;
  135. break;
  136. }
  137. break;
  138. case D7SIOCTM:
  139. /* toggle device mode-- flip display orientation */
  140. (regs & D7S_FLIP) ?
  141. (regs &= ~D7S_FLIP) : (regs |= D7S_FLIP);
  142. writeb(regs, d7s_regs);
  143. break;
  144. };
  145. unlock_kernel();
  146. return error;
  147. }
  148. static const struct file_operations d7s_fops = {
  149. .owner = THIS_MODULE,
  150. .unlocked_ioctl = d7s_ioctl,
  151. .compat_ioctl = d7s_ioctl,
  152. .open = d7s_open,
  153. .release = d7s_release,
  154. };
  155. static struct miscdevice d7s_miscdev = { D7S_MINOR, D7S_DEVNAME, &d7s_fops };
  156. static int __init d7s_init(void)
  157. {
  158. struct linux_ebus *ebus = NULL;
  159. struct linux_ebus_device *edev = NULL;
  160. int iTmp = 0, regs = 0;
  161. for_each_ebus(ebus) {
  162. for_each_ebusdev(edev, ebus) {
  163. if (!strcmp(edev->prom_node->name, D7S_OBPNAME))
  164. goto ebus_done;
  165. }
  166. }
  167. ebus_done:
  168. if(!edev) {
  169. printk("%s: unable to locate device\n", D7S_DEVNAME);
  170. return -ENODEV;
  171. }
  172. d7s_regs = ioremap(edev->resource[0].start, sizeof(__u8));
  173. iTmp = misc_register(&d7s_miscdev);
  174. if (0 != iTmp) {
  175. printk("%s: unable to acquire miscdevice minor %i\n",
  176. D7S_DEVNAME, D7S_MINOR);
  177. iounmap(d7s_regs);
  178. return iTmp;
  179. }
  180. /* OBP option "d7s-flipped?" is honored as default
  181. * for the device, and reset default when detached
  182. */
  183. regs = readb(d7s_regs);
  184. iTmp = d7s_obpflipped();
  185. (0 == iTmp) ?
  186. writeb(regs |= D7S_FLIP, d7s_regs):
  187. writeb(regs &= ~D7S_FLIP, d7s_regs);
  188. printk("%s: 7-Segment Display%s at 0x%lx %s\n",
  189. D7S_DEVNAME,
  190. (0 == iTmp) ? (" (FLIPPED)") : (""),
  191. edev->resource[0].start,
  192. (0 != sol_compat) ? ("in sol_compat mode") : (""));
  193. return 0;
  194. }
  195. static void __exit d7s_cleanup(void)
  196. {
  197. int regs = readb(d7s_regs);
  198. /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
  199. if (0 == sol_compat) {
  200. (0 == d7s_obpflipped()) ?
  201. writeb(regs |= D7S_FLIP, d7s_regs):
  202. writeb(regs &= ~D7S_FLIP, d7s_regs);
  203. }
  204. misc_deregister(&d7s_miscdev);
  205. d7s_free();
  206. }
  207. module_init(d7s_init);
  208. module_exit(d7s_cleanup);