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/slab.h>
16 #include <linux/mutex.h>
18 #include <linux/of_device.h>
19 #include <asm/atomic.h>
20 #include <asm/uaccess.h> /* put_/get_user */
23 #include <asm/display7seg.h>
26 #define DRIVER_NAME "d7s"
27 #define PFX DRIVER_NAME ": "
29 static DEFINE_MUTEX(d7s_mutex
);
30 static int sol_compat
= 0; /* Solaris compatibility mode */
32 /* Solaris compatibility flag -
33 * The Solaris implementation omits support for several
34 * documented driver features (ref Sun doc 806-0180-03).
35 * By default, this module supports the documented driver
36 * abilities, rather than the Solaris implementation:
38 * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode
39 * upon closure of device or module unload.
40 * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
43 * If you wish the device to operate as under Solaris,
44 * omitting above features, set this parameter to non-zero.
46 module_param(sol_compat
, int, 0);
47 MODULE_PARM_DESC(sol_compat
,
48 "Disables documented functionality omitted from Solaris driver");
50 MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
51 MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
52 MODULE_LICENSE("GPL");
53 MODULE_SUPPORTED_DEVICE("d7s");
59 struct d7s
*d7s_device
;
62 * Register block address- see header for details
63 * -----------------------------------------
64 * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
65 * -----------------------------------------
67 * DP - Toggles decimal point on/off
68 * ALARM - Toggles "Alarm" LED green/red
69 * FLIP - Inverts display for upside-down mounted board
70 * bits 0-4 - 7-segment display contents
72 static atomic_t d7s_users
= ATOMIC_INIT(0);
74 static int d7s_open(struct inode
*inode
, struct file
*f
)
76 if (D7S_MINOR
!= iminor(inode
))
78 atomic_inc(&d7s_users
);
82 static int d7s_release(struct inode
*inode
, struct file
*f
)
84 /* Reset flipped state to OBP default only if
85 * no other users have the device open and we
86 * are not operating in solaris-compat mode
88 if (atomic_dec_and_test(&d7s_users
) && !sol_compat
) {
89 struct d7s
*p
= d7s_device
;
92 regval
= readb(p
->regs
);
97 writeb(regval
, p
->regs
);
103 static long d7s_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
105 struct d7s
*p
= d7s_device
;
106 u8 regs
= readb(p
->regs
);
110 if (D7S_MINOR
!= iminor(file
->f_path
.dentry
->d_inode
))
113 mutex_lock(&d7s_mutex
);
116 /* assign device register values we mask-out D7S_FLIP
117 * if in sol_compat mode
119 if (get_user(ireg
, (int __user
*) arg
)) {
129 writeb(ireg
, p
->regs
);
133 /* retrieve device register values
134 * NOTE: Solaris implementation returns D7S_FLIP bit
135 * as toggled by user, even though it does not honor it.
136 * This driver will not misinform you about the state
137 * of your hardware while in sol_compat mode
139 if (put_user(regs
, (int __user
*) arg
)) {
146 /* toggle device mode-- flip display orientation */
151 writeb(regs
, p
->regs
);
154 mutex_unlock(&d7s_mutex
);
159 static const struct file_operations d7s_fops
= {
160 .owner
= THIS_MODULE
,
161 .unlocked_ioctl
= d7s_ioctl
,
162 .compat_ioctl
= d7s_ioctl
,
164 .release
= d7s_release
,
165 .llseek
= noop_llseek
,
168 static struct miscdevice d7s_miscdev
= {
174 static int __devinit
d7s_probe(struct platform_device
*op
,
175 const struct of_device_id
*match
)
177 struct device_node
*opts
;
185 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
190 p
->regs
= of_ioremap(&op
->resource
[0], 0, sizeof(u8
), "d7s");
192 printk(KERN_ERR PFX
"Cannot map chip registers\n");
196 err
= misc_register(&d7s_miscdev
);
198 printk(KERN_ERR PFX
"Unable to acquire miscdevice minor %i\n",
203 /* OBP option "d7s-flipped?" is honored as default for the
204 * device, and reset default when detached
206 regs
= readb(p
->regs
);
207 opts
= of_find_node_by_path("/options");
209 of_get_property(opts
, "d7s-flipped?", NULL
))
217 writeb(regs
, p
->regs
);
219 printk(KERN_INFO PFX
"7-Segment Display%s at [%s:0x%llx] %s\n",
220 op
->dev
.of_node
->full_name
,
221 (regs
& D7S_FLIP
) ? " (FLIPPED)" : "",
222 op
->resource
[0].start
,
223 sol_compat
? "in sol_compat mode" : "");
225 dev_set_drvdata(&op
->dev
, p
);
233 of_iounmap(&op
->resource
[0], p
->regs
, sizeof(u8
));
240 static int __devexit
d7s_remove(struct platform_device
*op
)
242 struct d7s
*p
= dev_get_drvdata(&op
->dev
);
243 u8 regs
= readb(p
->regs
);
245 /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
251 writeb(regs
, p
->regs
);
254 misc_deregister(&d7s_miscdev
);
255 of_iounmap(&op
->resource
[0], p
->regs
, sizeof(u8
));
261 static const struct of_device_id d7s_match
[] = {
263 .name
= "display7seg",
267 MODULE_DEVICE_TABLE(of
, d7s_match
);
269 static struct of_platform_driver d7s_driver
= {
272 .owner
= THIS_MODULE
,
273 .of_match_table
= d7s_match
,
276 .remove
= __devexit_p(d7s_remove
),
279 static int __init
d7s_init(void)
281 return of_register_platform_driver(&d7s_driver
);
284 static void __exit
d7s_exit(void)
286 of_unregister_platform_driver(&d7s_driver
);
289 module_init(d7s_init
);
290 module_exit(d7s_exit
);