display7seg.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/init.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->f_path.dentry->d_inode))
  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. if (regs & D7S_FLIP)
  131. regs &= ~D7S_FLIP;
  132. else
  133. regs |= D7S_FLIP;
  134. writeb(regs, p->regs);
  135. break;
  136. };
  137. mutex_unlock(&d7s_mutex);
  138. return error;
  139. }
  140. static const struct file_operations d7s_fops = {
  141. .owner = THIS_MODULE,
  142. .unlocked_ioctl = d7s_ioctl,
  143. .compat_ioctl = d7s_ioctl,
  144. .open = d7s_open,
  145. .release = d7s_release,
  146. .llseek = noop_llseek,
  147. };
  148. static struct miscdevice d7s_miscdev = {
  149. .minor = D7S_MINOR,
  150. .name = DRIVER_NAME,
  151. .fops = &d7s_fops
  152. };
  153. static int __devinit d7s_probe(struct platform_device *op)
  154. {
  155. struct device_node *opts;
  156. int err = -EINVAL;
  157. struct d7s *p;
  158. u8 regs;
  159. if (d7s_device)
  160. goto out;
  161. p = kzalloc(sizeof(*p), GFP_KERNEL);
  162. err = -ENOMEM;
  163. if (!p)
  164. goto out;
  165. p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s");
  166. if (!p->regs) {
  167. printk(KERN_ERR PFX "Cannot map chip registers\n");
  168. goto out_free;
  169. }
  170. err = misc_register(&d7s_miscdev);
  171. if (err) {
  172. printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n",
  173. D7S_MINOR);
  174. goto out_iounmap;
  175. }
  176. /* OBP option "d7s-flipped?" is honored as default for the
  177. * device, and reset default when detached
  178. */
  179. regs = readb(p->regs);
  180. opts = of_find_node_by_path("/options");
  181. if (opts &&
  182. of_get_property(opts, "d7s-flipped?", NULL))
  183. p->flipped = true;
  184. if (p->flipped)
  185. regs |= D7S_FLIP;
  186. else
  187. regs &= ~D7S_FLIP;
  188. writeb(regs, p->regs);
  189. printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%llx] %s\n",
  190. op->dev.of_node->full_name,
  191. (regs & D7S_FLIP) ? " (FLIPPED)" : "",
  192. op->resource[0].start,
  193. sol_compat ? "in sol_compat mode" : "");
  194. dev_set_drvdata(&op->dev, p);
  195. d7s_device = p;
  196. err = 0;
  197. out:
  198. return err;
  199. out_iounmap:
  200. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  201. out_free:
  202. kfree(p);
  203. goto out;
  204. }
  205. static int __devexit d7s_remove(struct platform_device *op)
  206. {
  207. struct d7s *p = dev_get_drvdata(&op->dev);
  208. u8 regs = readb(p->regs);
  209. /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
  210. if (sol_compat) {
  211. if (p->flipped)
  212. regs |= D7S_FLIP;
  213. else
  214. regs &= ~D7S_FLIP;
  215. writeb(regs, p->regs);
  216. }
  217. misc_deregister(&d7s_miscdev);
  218. of_iounmap(&op->resource[0], p->regs, sizeof(u8));
  219. kfree(p);
  220. return 0;
  221. }
  222. static const struct of_device_id d7s_match[] = {
  223. {
  224. .name = "display7seg",
  225. },
  226. {},
  227. };
  228. MODULE_DEVICE_TABLE(of, d7s_match);
  229. static struct platform_driver d7s_driver = {
  230. .driver = {
  231. .name = DRIVER_NAME,
  232. .owner = THIS_MODULE,
  233. .of_match_table = d7s_match,
  234. },
  235. .probe = d7s_probe,
  236. .remove = __devexit_p(d7s_remove),
  237. };
  238. module_platform_driver(d7s_driver);