]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/sbus/char/display7seg.c
dt/powerpc: Eliminate users of of_platform_{,un}register_driver
[mirror_ubuntu-artful-kernel.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
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/fs.h>
10#include <linux/errno.h>
11#include <linux/major.h>
12#include <linux/init.h>
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>
1da177e4 19#include <asm/atomic.h>
1da177e4 20#include <asm/uaccess.h> /* put_/get_user */
3049f89d 21#include <asm/io.h>
1da177e4
LT
22
23#include <asm/display7seg.h>
24
25#define D7S_MINOR 193
95d43905
DM
26#define DRIVER_NAME "d7s"
27#define PFX DRIVER_NAME ": "
1da177e4 28
a3108ca2 29static DEFINE_MUTEX(d7s_mutex);
1da177e4
LT
30static int sol_compat = 0; /* Solaris compatibility mode */
31
1da177e4
LT
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:
37 *
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
41 * FLIP bit
42 *
43 * If you wish the device to operate as under Solaris,
44 * omitting above features, set this parameter to non-zero.
45 */
95d43905
DM
46module_param(sol_compat, int, 0);
47MODULE_PARM_DESC(sol_compat,
48 "Disables documented functionality omitted from Solaris driver");
49
50MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
51MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
1da177e4 52MODULE_LICENSE("GPL");
95d43905
DM
53MODULE_SUPPORTED_DEVICE("d7s");
54
55struct d7s {
56 void __iomem *regs;
57 bool flipped;
58};
59struct d7s *d7s_device;
1da177e4
LT
60
61/*
62 * Register block address- see header for details
63 * -----------------------------------------
64 * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
65 * -----------------------------------------
66 *
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
71 */
1da177e4
LT
72static atomic_t d7s_users = ATOMIC_INIT(0);
73
74static int d7s_open(struct inode *inode, struct file *f)
75{
76 if (D7S_MINOR != iminor(inode))
77 return -ENODEV;
78 atomic_inc(&d7s_users);
79 return 0;
80}
81
82static int d7s_release(struct inode *inode, struct file *f)
83{
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
87 */
88 if (atomic_dec_and_test(&d7s_users) && !sol_compat) {
95d43905
DM
89 struct d7s *p = d7s_device;
90 u8 regval = 0;
91
92 regval = readb(p->regs);
93 if (p->flipped)
94 regval |= D7S_FLIP;
95 else
96 regval &= ~D7S_FLIP;
97 writeb(regval, p->regs);
1da177e4
LT
98 }
99
100 return 0;
101}
102
1d5d00bd 103static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4 104{
95d43905
DM
105 struct d7s *p = d7s_device;
106 u8 regs = readb(p->regs);
a5aac37f 107 int error = 0;
95d43905 108 u8 ireg = 0;
1da177e4 109
7fa95f72 110 if (D7S_MINOR != iminor(file->f_path.dentry->d_inode))
1da177e4
LT
111 return -ENODEV;
112
a3108ca2 113 mutex_lock(&d7s_mutex);
1da177e4
LT
114 switch (cmd) {
115 case D7SIOCWR:
95d43905
DM
116 /* assign device register values we mask-out D7S_FLIP
117 * if in sol_compat mode
1da177e4 118 */
1d5d00bd
CH
119 if (get_user(ireg, (int __user *) arg)) {
120 error = -EFAULT;
121 break;
122 }
95d43905
DM
123 if (sol_compat) {
124 if (regs & D7S_FLIP)
125 ireg |= D7S_FLIP;
126 else
127 ireg &= ~D7S_FLIP;
1da177e4 128 }
95d43905 129 writeb(ireg, p->regs);
1da177e4
LT
130 break;
131
132 case D7SIOCRD:
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
138 */
1d5d00bd
CH
139 if (put_user(regs, (int __user *) arg)) {
140 error = -EFAULT;
141 break;
142 }
1da177e4
LT
143 break;
144
145 case D7SIOCTM:
146 /* toggle device mode-- flip display orientation */
95d43905
DM
147 if (regs & D7S_FLIP)
148 regs &= ~D7S_FLIP;
149 else
150 regs |= D7S_FLIP;
151 writeb(regs, p->regs);
1da177e4
LT
152 break;
153 };
a3108ca2 154 mutex_unlock(&d7s_mutex);
1da177e4 155
1d5d00bd 156 return error;
1da177e4
LT
157}
158
00977a59 159static const struct file_operations d7s_fops = {
1d5d00bd
CH
160 .owner = THIS_MODULE,
161 .unlocked_ioctl = d7s_ioctl,
162 .compat_ioctl = d7s_ioctl,
163 .open = d7s_open,
164 .release = d7s_release,
6038f373 165 .llseek = noop_llseek,
1da177e4
LT
166};
167
95d43905
DM
168static struct miscdevice d7s_miscdev = {
169 .minor = D7S_MINOR,
170 .name = DRIVER_NAME,
171 .fops = &d7s_fops
172};
1da177e4 173
2dc11581 174static int __devinit d7s_probe(struct platform_device *op,
95d43905 175 const struct of_device_id *match)
1da177e4 176{
95d43905
DM
177 struct device_node *opts;
178 int err = -EINVAL;
179 struct d7s *p;
180 u8 regs;
181
182 if (d7s_device)
183 goto out;
184
185 p = kzalloc(sizeof(*p), GFP_KERNEL);
186 err = -ENOMEM;
187 if (!p)
188 goto out;
189
190 p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s");
191 if (!p->regs) {
192 printk(KERN_ERR PFX "Cannot map chip registers\n");
193 goto out_free;
1da177e4
LT
194 }
195
95d43905
DM
196 err = misc_register(&d7s_miscdev);
197 if (err) {
198 printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n",
199 D7S_MINOR);
200 goto out_iounmap;
1da177e4
LT
201 }
202
95d43905
DM
203 /* OBP option "d7s-flipped?" is honored as default for the
204 * device, and reset default when detached
1da177e4 205 */
95d43905
DM
206 regs = readb(p->regs);
207 opts = of_find_node_by_path("/options");
208 if (opts &&
209 of_get_property(opts, "d7s-flipped?", NULL))
210 p->flipped = true;
211
212 if (p->flipped)
213 regs |= D7S_FLIP;
214 else
215 regs &= ~D7S_FLIP;
216
217 writeb(regs, p->regs);
218
3f4528d6 219 printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%llx] %s\n",
61c7a080 220 op->dev.of_node->full_name,
95d43905
DM
221 (regs & D7S_FLIP) ? " (FLIPPED)" : "",
222 op->resource[0].start,
223 sol_compat ? "in sol_compat mode" : "");
224
225 dev_set_drvdata(&op->dev, p);
226 d7s_device = p;
227 err = 0;
228
229out:
230 return err;
231
232out_iounmap:
233 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
234
235out_free:
236 kfree(p);
237 goto out;
1da177e4
LT
238}
239
2dc11581 240static int __devexit d7s_remove(struct platform_device *op)
1da177e4 241{
95d43905
DM
242 struct d7s *p = dev_get_drvdata(&op->dev);
243 u8 regs = readb(p->regs);
1da177e4
LT
244
245 /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
95d43905
DM
246 if (sol_compat) {
247 if (p->flipped)
248 regs |= D7S_FLIP;
249 else
250 regs &= ~D7S_FLIP;
251 writeb(regs, p->regs);
1da177e4
LT
252 }
253
254 misc_deregister(&d7s_miscdev);
95d43905
DM
255 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
256 kfree(p);
257
258 return 0;
259}
260
fd098316 261static const struct of_device_id d7s_match[] = {
95d43905
DM
262 {
263 .name = "display7seg",
264 },
265 {},
266};
267MODULE_DEVICE_TABLE(of, d7s_match);
268
269static struct of_platform_driver d7s_driver = {
4018294b
GL
270 .driver = {
271 .name = DRIVER_NAME,
272 .owner = THIS_MODULE,
273 .of_match_table = d7s_match,
274 },
95d43905
DM
275 .probe = d7s_probe,
276 .remove = __devexit_p(d7s_remove),
277};
278
279static int __init d7s_init(void)
280{
1ab1d63a 281 return of_register_platform_driver(&d7s_driver);
95d43905
DM
282}
283
284static void __exit d7s_exit(void)
285{
1ab1d63a 286 of_unregister_platform_driver(&d7s_driver);
1da177e4
LT
287}
288
289module_init(d7s_init);
95d43905 290module_exit(d7s_exit);