1 /* display7seg.c - Driver implementation for the 7-segment display
2 * present on Sun Microsystems CP1400 and CP1500
4 * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/major.h>
12 #include <linux/init.h>
13 #include <linux/miscdevice.h>
14 #include <linux/ioport.h> /* request_region */
15 #include <linux/smp_lock.h>
17 #include <linux/of_device.h>
18 #include <asm/atomic.h>
19 #include <asm/uaccess.h> /* put_/get_user */
22 #include <asm/display7seg.h>
25 #define DRIVER_NAME "d7s"
26 #define PFX DRIVER_NAME ": "
28 static int sol_compat = 0; /* Solaris compatibility mode */
30 /* Solaris compatibility flag -
31 * The Solaris implementation omits support for several
32 * documented driver features (ref Sun doc 806-0180-03).
33 * By default, this module supports the documented driver
34 * abilities, rather than the Solaris implementation:
36 * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode
37 * upon closure of device or module unload.
38 * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
41 * If you wish the device to operate as under Solaris,
42 * omitting above features, set this parameter to non-zero.
44 module_param(sol_compat, int, 0);
45 MODULE_PARM_DESC(sol_compat,
46 "Disables documented functionality omitted from Solaris driver");
48 MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
49 MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
50 MODULE_LICENSE("GPL");
51 MODULE_SUPPORTED_DEVICE("d7s");
57 struct d7s *d7s_device;
60 * Register block address- see header for details
61 * -----------------------------------------
62 * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
63 * -----------------------------------------
65 * DP - Toggles decimal point on/off
66 * ALARM - Toggles "Alarm" LED green/red
67 * FLIP - Inverts display for upside-down mounted board
68 * bits 0-4 - 7-segment display contents
70 static atomic_t d7s_users = ATOMIC_INIT(0);
72 static int d7s_open(struct inode *inode, struct file *f)
74 if (D7S_MINOR != iminor(inode))
77 atomic_inc(&d7s_users);
81 static int d7s_release(struct inode *inode, struct file *f)
83 /* Reset flipped state to OBP default only if
84 * no other users have the device open and we
85 * are not operating in solaris-compat mode
87 if (atomic_dec_and_test(&d7s_users) && !sol_compat) {
88 struct d7s *p = d7s_device;
91 regval = readb(p->regs);
96 writeb(regval, p->regs);
102 static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
104 struct d7s *p = d7s_device;
105 u8 regs = readb(p->regs);
109 if (D7S_MINOR != iminor(file->f_path.dentry->d_inode))
115 /* assign device register values we mask-out D7S_FLIP
116 * if in sol_compat mode
118 if (get_user(ireg, (int __user *) arg)) {
128 writeb(ireg, p->regs);
132 /* retrieve device register values
133 * NOTE: Solaris implementation returns D7S_FLIP bit
134 * as toggled by user, even though it does not honor it.
135 * This driver will not misinform you about the state
136 * of your hardware while in sol_compat mode
138 if (put_user(regs, (int __user *) arg)) {
145 /* toggle device mode-- flip display orientation */
150 writeb(regs, p->regs);
158 static const struct file_operations d7s_fops = {
159 .owner = THIS_MODULE,
160 .unlocked_ioctl = d7s_ioctl,
161 .compat_ioctl = d7s_ioctl,
163 .release = d7s_release,
166 static struct miscdevice d7s_miscdev = {
172 static int __devinit d7s_probe(struct of_device *op,
173 const struct of_device_id *match)
175 struct device_node *opts;
183 p = kzalloc(sizeof(*p), GFP_KERNEL);
188 p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s");
190 printk(KERN_ERR PFX "Cannot map chip registers\n");
194 err = misc_register(&d7s_miscdev);
196 printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n",
201 /* OBP option "d7s-flipped?" is honored as default for the
202 * device, and reset default when detached
204 regs = readb(p->regs);
205 opts = of_find_node_by_path("/options");
207 of_get_property(opts, "d7s-flipped?", NULL))
215 writeb(regs, p->regs);
217 printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%lx] %s\n",
219 (regs & D7S_FLIP) ? " (FLIPPED)" : "",
220 op->resource[0].start,
221 sol_compat ? "in sol_compat mode" : "");
223 dev_set_drvdata(&op->dev, p);
231 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
238 static int __devexit d7s_remove(struct of_device *op)
240 struct d7s *p = dev_get_drvdata(&op->dev);
241 u8 regs = readb(p->regs);
243 /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
249 writeb(regs, p->regs);
252 misc_deregister(&d7s_miscdev);
253 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
259 static const struct of_device_id d7s_match[] = {
261 .name = "display7seg",
265 MODULE_DEVICE_TABLE(of, d7s_match);
267 static struct of_platform_driver d7s_driver = {
269 .match_table = d7s_match,
271 .remove = __devexit_p(d7s_remove),
274 static int __init d7s_init(void)
276 return of_register_driver(&d7s_driver, &of_bus_type);
279 static void __exit d7s_exit(void)
281 of_unregister_driver(&d7s_driver);
284 module_init(d7s_init);
285 module_exit(d7s_exit);