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
5 * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/major.h>
14 #include <linux/miscdevice.h>
15 #include <linux/ioport.h> /* request_region */
16 #include <linux/slab.h>
17 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/atomic.h>
21 #include <linux/uaccess.h> /* put_/get_user */
24 #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");
58 struct d7s
*d7s_device
;
61 * Register block address- see header for details
62 * -----------------------------------------
63 * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
64 * -----------------------------------------
66 * DP - Toggles decimal point on/off
67 * ALARM - Toggles "Alarm" LED green/red
68 * FLIP - Inverts display for upside-down mounted board
69 * bits 0-4 - 7-segment display contents
71 static atomic_t d7s_users
= ATOMIC_INIT(0);
73 static int d7s_open(struct inode
*inode
, struct file
*f
)
75 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_inode(file
)))
112 mutex_lock(&d7s_mutex
);
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 */
147 writeb(regs
, p
->regs
);
150 mutex_unlock(&d7s_mutex
);
155 static const struct file_operations d7s_fops
= {
156 .owner
= THIS_MODULE
,
157 .unlocked_ioctl
= d7s_ioctl
,
158 .compat_ioctl
= compat_ptr_ioctl
,
160 .release
= d7s_release
,
161 .llseek
= noop_llseek
,
164 static struct miscdevice d7s_miscdev
= {
170 static int d7s_probe(struct platform_device
*op
)
172 struct device_node
*opts
;
180 p
= devm_kzalloc(&op
->dev
, sizeof(*p
), GFP_KERNEL
);
185 p
->regs
= of_ioremap(&op
->resource
[0], 0, sizeof(u8
), "d7s");
187 printk(KERN_ERR PFX
"Cannot map chip registers\n");
191 err
= misc_register(&d7s_miscdev
);
193 printk(KERN_ERR PFX
"Unable to acquire miscdevice minor %i\n",
198 /* OBP option "d7s-flipped?" is honored as default for the
199 * device, and reset default when detached
201 regs
= readb(p
->regs
);
202 opts
= of_find_node_by_path("/options");
204 p
->flipped
= of_property_read_bool(opts
, "d7s-flipped?");
211 writeb(regs
, p
->regs
);
213 printk(KERN_INFO PFX
"7-Segment Display%pOF at [%s:0x%llx] %s\n",
215 (regs
& D7S_FLIP
) ? " (FLIPPED)" : "",
216 op
->resource
[0].start
,
217 sol_compat
? "in sol_compat mode" : "");
219 dev_set_drvdata(&op
->dev
, p
);
228 of_iounmap(&op
->resource
[0], p
->regs
, sizeof(u8
));
232 static void d7s_remove(struct platform_device
*op
)
234 struct d7s
*p
= dev_get_drvdata(&op
->dev
);
235 u8 regs
= readb(p
->regs
);
237 /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
243 writeb(regs
, p
->regs
);
246 misc_deregister(&d7s_miscdev
);
247 of_iounmap(&op
->resource
[0], p
->regs
, sizeof(u8
));
250 static const struct of_device_id d7s_match
[] = {
252 .name
= "display7seg",
256 MODULE_DEVICE_TABLE(of
, d7s_match
);
258 static struct platform_driver d7s_driver
= {
261 .of_match_table
= d7s_match
,
264 .remove_new
= d7s_remove
,
267 module_platform_driver(d7s_driver
);