Merge tag 's390-4.21-1' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[linux-2.6-block.git] / drivers / sbus / char / display7seg.c
CommitLineData
95d43905
DM
1/* display7seg.c - Driver implementation for the 7-segment display
2 * present on Sun Microsystems CP1400 and CP1500
1da177e4
LT
3 *
4 * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
1da177e4
LT
5 */
6
57a4a3d7 7#include <linux/device.h>
1da177e4
LT
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/fs.h>
11#include <linux/errno.h>
12#include <linux/major.h>
1da177e4 13#include <linux/miscdevice.h>
fb911ee8 14#include <linux/ioport.h> /* request_region */
5a0e3ad6 15#include <linux/slab.h>
a3108ca2 16#include <linux/mutex.h>
95d43905
DM
17#include <linux/of.h>
18#include <linux/of_device.h>
60063497 19#include <linux/atomic.h>
7c0f6ba6 20#include <linux/uaccess.h> /* put_/get_user */
3049f89d 21#include <asm/io.h>
1da177e4
LT
22
23#include <asm/display7seg.h>
24
95d43905
DM
25#define DRIVER_NAME "d7s"
26#define PFX DRIVER_NAME ": "
1da177e4 27
a3108ca2 28static DEFINE_MUTEX(d7s_mutex);
1da177e4
LT
29static int sol_compat = 0; /* Solaris compatibility mode */
30
1da177e4
LT
31/* Solaris compatibility flag -
32 * The Solaris implementation omits support for several
33 * documented driver features (ref Sun doc 806-0180-03).
34 * By default, this module supports the documented driver
35 * abilities, rather than the Solaris implementation:
36 *
37 * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode
38 * upon closure of device or module unload.
39 * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
40 * FLIP bit
41 *
42 * If you wish the device to operate as under Solaris,
43 * omitting above features, set this parameter to non-zero.
44 */
95d43905
DM
45module_param(sol_compat, int, 0);
46MODULE_PARM_DESC(sol_compat,
47 "Disables documented functionality omitted from Solaris driver");
48
49MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
50MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
1da177e4 51MODULE_LICENSE("GPL");
95d43905
DM
52MODULE_SUPPORTED_DEVICE("d7s");
53
54struct d7s {
55 void __iomem *regs;
56 bool flipped;
57};
58struct d7s *d7s_device;
1da177e4
LT
59
60/*
61 * Register block address- see header for details
62 * -----------------------------------------
63 * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
64 * -----------------------------------------
65 *
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
70 */
1da177e4
LT
71static atomic_t d7s_users = ATOMIC_INIT(0);
72
73static int d7s_open(struct inode *inode, struct file *f)
74{
75 if (D7S_MINOR != iminor(inode))
76 return -ENODEV;
77 atomic_inc(&d7s_users);
78 return 0;
79}
80
81static int d7s_release(struct inode *inode, struct file *f)
82{
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
86 */
87 if (atomic_dec_and_test(&d7s_users) && !sol_compat) {
95d43905
DM
88 struct d7s *p = d7s_device;
89 u8 regval = 0;
90
91 regval = readb(p->regs);
92 if (p->flipped)
93 regval |= D7S_FLIP;
94 else
95 regval &= ~D7S_FLIP;
96 writeb(regval, p->regs);
1da177e4
LT
97 }
98
99 return 0;
100}
101
1d5d00bd 102static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4 103{
95d43905
DM
104 struct d7s *p = d7s_device;
105 u8 regs = readb(p->regs);
a5aac37f 106 int error = 0;
95d43905 107 u8 ireg = 0;
1da177e4 108
496ad9aa 109 if (D7S_MINOR != iminor(file_inode(file)))
1da177e4
LT
110 return -ENODEV;
111
a3108ca2 112 mutex_lock(&d7s_mutex);
1da177e4
LT
113 switch (cmd) {
114 case D7SIOCWR:
95d43905
DM
115 /* assign device register values we mask-out D7S_FLIP
116 * if in sol_compat mode
1da177e4 117 */
1d5d00bd
CH
118 if (get_user(ireg, (int __user *) arg)) {
119 error = -EFAULT;
120 break;
121 }
95d43905
DM
122 if (sol_compat) {
123 if (regs & D7S_FLIP)
124 ireg |= D7S_FLIP;
125 else
126 ireg &= ~D7S_FLIP;
1da177e4 127 }
95d43905 128 writeb(ireg, p->regs);
1da177e4
LT
129 break;
130
131 case D7SIOCRD:
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
137 */
1d5d00bd
CH
138 if (put_user(regs, (int __user *) arg)) {
139 error = -EFAULT;
140 break;
141 }
1da177e4
LT
142 break;
143
144 case D7SIOCTM:
145 /* toggle device mode-- flip display orientation */
54dcf0ce 146 regs ^= D7S_FLIP;
95d43905 147 writeb(regs, p->regs);
1da177e4 148 break;
da201161 149 }
a3108ca2 150 mutex_unlock(&d7s_mutex);
1da177e4 151
1d5d00bd 152 return error;
1da177e4
LT
153}
154
00977a59 155static const struct file_operations d7s_fops = {
1d5d00bd
CH
156 .owner = THIS_MODULE,
157 .unlocked_ioctl = d7s_ioctl,
158 .compat_ioctl = d7s_ioctl,
159 .open = d7s_open,
160 .release = d7s_release,
6038f373 161 .llseek = noop_llseek,
1da177e4
LT
162};
163
95d43905
DM
164static struct miscdevice d7s_miscdev = {
165 .minor = D7S_MINOR,
166 .name = DRIVER_NAME,
167 .fops = &d7s_fops
168};
1da177e4 169
082a2004 170static int d7s_probe(struct platform_device *op)
1da177e4 171{
95d43905
DM
172 struct device_node *opts;
173 int err = -EINVAL;
174 struct d7s *p;
175 u8 regs;
176
177 if (d7s_device)
178 goto out;
179
57a4a3d7 180 p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
95d43905
DM
181 err = -ENOMEM;
182 if (!p)
183 goto out;
184
185 p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s");
186 if (!p->regs) {
187 printk(KERN_ERR PFX "Cannot map chip registers\n");
188 goto out_free;
1da177e4
LT
189 }
190
95d43905
DM
191 err = misc_register(&d7s_miscdev);
192 if (err) {
193 printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n",
194 D7S_MINOR);
195 goto out_iounmap;
1da177e4
LT
196 }
197
95d43905
DM
198 /* OBP option "d7s-flipped?" is honored as default for the
199 * device, and reset default when detached
1da177e4 200 */
95d43905
DM
201 regs = readb(p->regs);
202 opts = of_find_node_by_path("/options");
203 if (opts &&
204 of_get_property(opts, "d7s-flipped?", NULL))
205 p->flipped = true;
206
207 if (p->flipped)
208 regs |= D7S_FLIP;
209 else
210 regs &= ~D7S_FLIP;
211
212 writeb(regs, p->regs);
213
8cd3ec51
RH
214 printk(KERN_INFO PFX "7-Segment Display%pOF at [%s:0x%llx] %s\n",
215 op->dev.of_node,
95d43905
DM
216 (regs & D7S_FLIP) ? " (FLIPPED)" : "",
217 op->resource[0].start,
218 sol_compat ? "in sol_compat mode" : "");
219
220 dev_set_drvdata(&op->dev, p);
221 d7s_device = p;
222 err = 0;
87d81a23 223 of_node_put(opts);
95d43905
DM
224
225out:
226 return err;
227
228out_iounmap:
229 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
230
231out_free:
95d43905 232 goto out;
1da177e4
LT
233}
234
082a2004 235static int d7s_remove(struct platform_device *op)
1da177e4 236{
95d43905
DM
237 struct d7s *p = dev_get_drvdata(&op->dev);
238 u8 regs = readb(p->regs);
1da177e4
LT
239
240 /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
95d43905
DM
241 if (sol_compat) {
242 if (p->flipped)
243 regs |= D7S_FLIP;
244 else
245 regs &= ~D7S_FLIP;
246 writeb(regs, p->regs);
1da177e4
LT
247 }
248
249 misc_deregister(&d7s_miscdev);
95d43905 250 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
95d43905
DM
251
252 return 0;
253}
254
fd098316 255static const struct of_device_id d7s_match[] = {
95d43905
DM
256 {
257 .name = "display7seg",
258 },
259 {},
260};
261MODULE_DEVICE_TABLE(of, d7s_match);
262
4ebb24f7 263static struct platform_driver d7s_driver = {
4018294b
GL
264 .driver = {
265 .name = DRIVER_NAME,
4018294b
GL
266 .of_match_table = d7s_match,
267 },
95d43905 268 .probe = d7s_probe,
082a2004 269 .remove = d7s_remove,
95d43905
DM
270};
271
dbf2b92d 272module_platform_driver(d7s_driver);