display7seg.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/device.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/errno.h>
  11. #include <linux/major.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/ioport.h> /* request_region */
  14. #include <linux/slab.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/atomic.h>
  19. #include <asm/uaccess.h> /* put_/get_user */
  20. #include <asm/io.h>
  21. #include <asm/display7seg.h>
  22. #define D7S_MINOR 193
  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 <ebrower@usa.net>");
  45. MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
  46. MODULE_LICENSE("GPL");
  47. MODULE_SUPPORTED_DEVICE("d7s");
  48. struct d7s {
  49. void __iomem *regs;
  50. bool flipped;
  51. };
  52. struct d7s *d7s_device;
  53. /*
  54. * Register block address- see header for details
  55. * -----------------------------------------
  56. * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
  57. * -----------------------------------------
  58. *
  59. * DP - Toggles decimal point on/off
  60. * ALARM - Toggles "Alarm" LED green/red
  61. * FLIP - Inverts display for upside-down mounted board
  62. * bits 0-4 - 7-segment display contents
  63. */
  64. static atomic_t d7s_users = ATOMIC_INIT(0);
  65. static int d7s_open(struct inode *inode, struct file *f)
  66. {
  67. if (D7S_MINOR != iminor(inode))
  68. return -ENODEV;
  69. atomic_inc(&d7s_users);
  70. return 0;
  71. }
  72. static int d7s_release(struct inode *inode, struct file *f)
  73. {
  74. /* Reset flipped state to OBP default only if
  75. * no other users have the device open and we
  76. * are not operating in solaris-compat mode
  77. */
  78. if (atomic_dec_and_test(&d7s_users) && !sol_compat) {
  79. struct d7s *p = d7s_device;
  80. u8 regval = 0;
  81. regval = readb(p->regs);
  82. if (p->flipped)
  83. regval |= D7S_FLIP;
  84. else
  85. regval &= ~D7S_FLIP;
  86. writeb(regval, p->regs);
  87. }
  88. return 0;
  89. }
  90. static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  91. {
  92. struct d7s *p = d7s_device;
  93. u8 regs = readb(p->regs);
  94. int error = 0;
  95. u8 ireg = 0;
  96. if (D7S_MINOR != iminor(file_inode(file)))
  97. return -ENODEV;
  98. mutex_lock(&d7s_mutex);
  99. switch (cmd) {
  100. case D7SIOCWR:
  101. /* assign device register values we mask-out D7S_FLIP
  102. * if in sol_compat mode
  103. */
  104. if (get_user(ireg, (int __user *) arg)) {
  105. error = -EFAULT;
  106. break;
  107. }
  108. if (sol_compat) {
  109. if (regs & D7S_FLIP)
  110. ireg |= D7S_FLIP;
  111. else
  112. ireg &= ~D7S_FLIP;
  113. }
  114. writeb(ireg, p->regs);
  115. break;
  116. case D7SIOCRD:
  117. /* retrieve device register values
  118. * NOTE: Solaris implementation returns D7S_FLIP bit
  119. * as toggled by user, even though it does not honor it.
  120. * This driver will not misinform you about the state
  121. * of your hardware while in sol_compat mode
  122. */
  123. if (put_user(regs, (int __user *) arg)) {
  124. error = -EFAULT;
  125. break;
  126. }
  127. break;
  128. case D7SIOCTM:
  129. /* toggle device mode-- flip display orientation */
  130. regs ^= D7S_FLIP;
  131. writeb(regs, p->regs);
  132. break;
  133. }
  134. mutex_unlock(&d7s_mutex);
  135. return error;
  136. }
  137. static const struct file_operations d7s_fops = {
  138. .owner = THIS_MODULE,
  139. .unlocked_ioctl = d7s_ioctl,
  140. .compat_ioctl = d7s_ioctl,
  141. .open = d7s_open,
  142. .release = d7s_release,
  143. .llseek = noop_llseek,
  144. };
  145. static struct miscdevice d7s_miscdev = {
  146. .minor = D7S_MINOR,
  147. .name = DRIVER_NAME,
  148. .fops = &d7s_fops
  149. };
  150. static int d7s_probe(struct platform_device *op)
  151. {
  152. struct device_node *opts;
  153. int err = -EINVAL;
  154. struct d7s *p;
  155. u8 regs;
  156. if (d7s_device)
  157. goto out;
  158. p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
  159. err = -ENOMEM;
  160. if (!p)
  161. goto out;
  162. p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s");
  163. if (!p->regs) {
  164. printk(KERN_ERR PFX "Cannot map chip registers\n");
  165. goto out_free;
  166. }
  167. err = misc_register(&d7s_miscdev);
  168. if (err) {
  169. printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n",
  170. D7S_MINOR);
  171. goto out_iounmap;
  172. }
  173. /* OBP option "d7s-flipped?" is honored as default for the
  174. * device, and reset default when detached
  175. */
  176. regs = readb(p->regs);
  177. opts = of_find_node_by_path("/options");
  178. if (opts &&
  179. of_get_property(opts, "d7s-flipped?", NULL))
  180. p->flipped = true;
  181. if (p->flipped)
  182. regs |= D7S_FLIP;
  183. else
  184. regs &= ~D7S_FLIP;
  185. writeb(regs, p->regs);
  186. printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%llx] %s\n",
  187. op->dev.of_node->full_name,
  188. (regs & D7S_FLIP) ? " (FLIPPED)" : "",
  189. op->resource[0].start,
  190. sol_compat ? "in sol_compat mode" : "");
  191. dev_set_drvdata(&op->dev, p);
  192. d7s_device = p;
  193. err = 0;
  194. out:
  195. return err;
  196. out_iounmap:
  197. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  198. out_free:
  199. goto out;
  200. }
  201. static int d7s_remove(struct platform_device *op)
  202. {
  203. struct d7s *p = dev_get_drvdata(&op->dev);
  204. u8 regs = readb(p->regs);
  205. /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
  206. if (sol_compat) {
  207. if (p->flipped)
  208. regs |= D7S_FLIP;
  209. else
  210. regs &= ~D7S_FLIP;
  211. writeb(regs, p->regs);
  212. }
  213. misc_deregister(&d7s_miscdev);
  214. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  215. return 0;
  216. }
  217. static const struct of_device_id d7s_match[] = {
  218. {
  219. .name = "display7seg",
  220. },
  221. {},
  222. };
  223. MODULE_DEVICE_TABLE(of, d7s_match);
  224. static struct platform_driver d7s_driver = {
  225. .driver = {
  226. .name = DRIVER_NAME,
  227. .of_match_table = d7s_match,
  228. },
  229. .probe = d7s_probe,
  230. .remove = d7s_remove,
  231. };
  232. module_platform_driver(d7s_driver);