display7seg.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* display7seg.c - Driver implementation for the 7-segment display
  3. * present on Sun Microsystems CP1400 and CP1500
  4. *
  5. * Copyright (c) 2000 Eric Brower ([email protected])
  6. */
  7. #include <linux/device.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/fs.h>
  11. #include <linux/errno.h>
  12. #include <linux/major.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/ioport.h> /* request_region */
  15. #include <linux/slab.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/atomic.h>
  20. #include <linux/uaccess.h> /* put_/get_user */
  21. #include <asm/io.h>
  22. #include <asm/display7seg.h>
  23. #define DRIVER_NAME "d7s"
  24. #define PFX DRIVER_NAME ": "
  25. static DEFINE_MUTEX(d7s_mutex);
  26. static int sol_compat = 0; /* Solaris compatibility mode */
  27. /* Solaris compatibility flag -
  28. * The Solaris implementation omits support for several
  29. * documented driver features (ref Sun doc 806-0180-03).
  30. * By default, this module supports the documented driver
  31. * abilities, rather than the Solaris implementation:
  32. *
  33. * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode
  34. * upon closure of device or module unload.
  35. * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
  36. * FLIP bit
  37. *
  38. * If you wish the device to operate as under Solaris,
  39. * omitting above features, set this parameter to non-zero.
  40. */
  41. module_param(sol_compat, int, 0);
  42. MODULE_PARM_DESC(sol_compat,
  43. "Disables documented functionality omitted from Solaris driver");
  44. MODULE_AUTHOR("Eric Brower <[email protected]>");
  45. MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
  46. MODULE_LICENSE("GPL");
  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. regs ^= D7S_FLIP;
  130. writeb(regs, p->regs);
  131. break;
  132. }
  133. mutex_unlock(&d7s_mutex);
  134. return error;
  135. }
  136. static const struct file_operations d7s_fops = {
  137. .owner = THIS_MODULE,
  138. .unlocked_ioctl = d7s_ioctl,
  139. .compat_ioctl = compat_ptr_ioctl,
  140. .open = d7s_open,
  141. .release = d7s_release,
  142. .llseek = noop_llseek,
  143. };
  144. static struct miscdevice d7s_miscdev = {
  145. .minor = D7S_MINOR,
  146. .name = DRIVER_NAME,
  147. .fops = &d7s_fops
  148. };
  149. static int d7s_probe(struct platform_device *op)
  150. {
  151. struct device_node *opts;
  152. int err = -EINVAL;
  153. struct d7s *p;
  154. u8 regs;
  155. if (d7s_device)
  156. goto out;
  157. p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
  158. err = -ENOMEM;
  159. if (!p)
  160. goto out;
  161. p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s");
  162. if (!p->regs) {
  163. printk(KERN_ERR PFX "Cannot map chip registers\n");
  164. goto out;
  165. }
  166. err = misc_register(&d7s_miscdev);
  167. if (err) {
  168. printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n",
  169. D7S_MINOR);
  170. goto out_iounmap;
  171. }
  172. /* OBP option "d7s-flipped?" is honored as default for the
  173. * device, and reset default when detached
  174. */
  175. regs = readb(p->regs);
  176. opts = of_find_node_by_path("/options");
  177. if (opts &&
  178. of_get_property(opts, "d7s-flipped?", NULL))
  179. p->flipped = true;
  180. if (p->flipped)
  181. regs |= D7S_FLIP;
  182. else
  183. regs &= ~D7S_FLIP;
  184. writeb(regs, p->regs);
  185. printk(KERN_INFO PFX "7-Segment Display%pOF at [%s:0x%llx] %s\n",
  186. op->dev.of_node,
  187. (regs & D7S_FLIP) ? " (FLIPPED)" : "",
  188. op->resource[0].start,
  189. sol_compat ? "in sol_compat mode" : "");
  190. dev_set_drvdata(&op->dev, p);
  191. d7s_device = p;
  192. err = 0;
  193. of_node_put(opts);
  194. out:
  195. return err;
  196. out_iounmap:
  197. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  198. goto out;
  199. }
  200. static int d7s_remove(struct platform_device *op)
  201. {
  202. struct d7s *p = dev_get_drvdata(&op->dev);
  203. u8 regs = readb(p->regs);
  204. /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
  205. if (sol_compat) {
  206. if (p->flipped)
  207. regs |= D7S_FLIP;
  208. else
  209. regs &= ~D7S_FLIP;
  210. writeb(regs, p->regs);
  211. }
  212. misc_deregister(&d7s_miscdev);
  213. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  214. return 0;
  215. }
  216. static const struct of_device_id d7s_match[] = {
  217. {
  218. .name = "display7seg",
  219. },
  220. {},
  221. };
  222. MODULE_DEVICE_TABLE(of, d7s_match);
  223. static struct platform_driver d7s_driver = {
  224. .driver = {
  225. .name = DRIVER_NAME,
  226. .of_match_table = d7s_match,
  227. },
  228. .probe = d7s_probe,
  229. .remove = d7s_remove,
  230. };
  231. module_platform_driver(d7s_driver);