display7seg.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/miscdevice.h>
  12. #include <linux/ioport.h> /* request_region */
  13. #include <linux/slab.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/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 DEFINE_MUTEX(d7s_mutex);
  25. static int sol_compat = 0; /* Solaris compatibility mode */
  26. /* Solaris compatibility flag -
  27. * The Solaris implementation omits support for several
  28. * documented driver features (ref Sun doc 806-0180-03).
  29. * By default, this module supports the documented driver
  30. * abilities, rather than the Solaris implementation:
  31. *
  32. * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode
  33. * upon closure of device or module unload.
  34. * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
  35. * FLIP bit
  36. *
  37. * If you wish the device to operate as under Solaris,
  38. * omitting above features, set this parameter to non-zero.
  39. */
  40. module_param(sol_compat, int, 0);
  41. MODULE_PARM_DESC(sol_compat,
  42. "Disables documented functionality omitted from Solaris driver");
  43. MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
  44. MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
  45. MODULE_LICENSE("GPL");
  46. MODULE_SUPPORTED_DEVICE("d7s");
  47. struct d7s {
  48. void __iomem *regs;
  49. bool flipped;
  50. };
  51. struct d7s *d7s_device;
  52. /*
  53. * Register block address- see header for details
  54. * -----------------------------------------
  55. * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
  56. * -----------------------------------------
  57. *
  58. * DP - Toggles decimal point on/off
  59. * ALARM - Toggles "Alarm" LED green/red
  60. * FLIP - Inverts display for upside-down mounted board
  61. * bits 0-4 - 7-segment display contents
  62. */
  63. static atomic_t d7s_users = ATOMIC_INIT(0);
  64. static int d7s_open(struct inode *inode, struct file *f)
  65. {
  66. if (D7S_MINOR != iminor(inode))
  67. return -ENODEV;
  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_inode(file)))
  96. return -ENODEV;
  97. mutex_lock(&d7s_mutex);
  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. mutex_unlock(&d7s_mutex);
  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. .llseek = noop_llseek,
  146. };
  147. static struct miscdevice d7s_miscdev = {
  148. .minor = D7S_MINOR,
  149. .name = DRIVER_NAME,
  150. .fops = &d7s_fops
  151. };
  152. static int d7s_probe(struct platform_device *op)
  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%llx] %s\n",
  189. op->dev.of_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 d7s_remove(struct platform_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 platform_driver d7s_driver = {
  229. .driver = {
  230. .name = DRIVER_NAME,
  231. .owner = THIS_MODULE,
  232. .of_match_table = d7s_match,
  233. },
  234. .probe = d7s_probe,
  235. .remove = d7s_remove,
  236. };
  237. module_platform_driver(d7s_driver);