blob: a36e4cf1841d9da7fd22cb5f6491d134b9b7f96f [file] [log] [blame]
David S. Miller95d43902008-08-29 18:01:58 -07001/* display7seg.c - Driver implementation for the 7-segment display
2 * present on Sun Microsystems CP1400 and CP1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Himangi Saraogi57a4a3d2014-05-27 03:02:07 +05307#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/fs.h>
11#include <linux/errno.h>
12#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/miscdevice.h>
Peter Osterlundfb911ee2005-09-13 01:25:15 -070014#include <linux/ioport.h> /* request_region */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070016#include <linux/mutex.h>
David S. Miller95d43902008-08-29 18:01:58 -070017#include <linux/of.h>
18#include <linux/of_device.h>
Arun Sharma600634972011-07-26 16:09:06 -070019#include <linux/atomic.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h> /* put_/get_user */
David S. Miller3049f892007-05-13 22:22:47 -070021#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/display7seg.h>
24
David S. Miller95d43902008-08-29 18:01:58 -070025#define DRIVER_NAME "d7s"
26#define PFX DRIVER_NAME ": "
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070028static DEFINE_MUTEX(d7s_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int sol_compat = 0; /* Solaris compatibility mode */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* 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 */
David S. Miller95d43902008-08-29 18:01:58 -070045module_param(sol_compat, int, 0);
46MODULE_PARM_DESC(sol_compat,
47 "Disables documented functionality omitted from Solaris driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David S. Miller95d43902008-08-29 18:01:58 -070049MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
50MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_LICENSE("GPL");
David S. Miller95d43902008-08-29 18:01:58 -070052MODULE_SUPPORTED_DEVICE("d7s");
53
54struct d7s {
55 void __iomem *regs;
56 bool flipped;
57};
58struct d7s *d7s_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static 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) {
David S. Miller95d43902008-08-29 18:01:58 -070088 struct d7s *p = d7s_device;
89 u8 regval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
David S. Miller95d43902008-08-29 18:01:58 -070091 regval = readb(p->regs);
92 if (p->flipped)
93 regval |= D7S_FLIP;
94 else
95 regval &= ~D7S_FLIP;
96 writeb(regval, p->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
99 return 0;
100}
101
Christoph Hellwig1d5d00b2005-11-07 14:13:01 -0800102static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
David S. Miller95d43902008-08-29 18:01:58 -0700104 struct d7s *p = d7s_device;
105 u8 regs = readb(p->regs);
Andrew Mortona5aac372005-11-10 21:14:16 -0800106 int error = 0;
David S. Miller95d43902008-08-29 18:01:58 -0700107 u8 ireg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Al Viro496ad9a2013-01-23 17:07:38 -0500109 if (D7S_MINOR != iminor(file_inode(file)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -ENODEV;
111
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700112 mutex_lock(&d7s_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 switch (cmd) {
114 case D7SIOCWR:
David S. Miller95d43902008-08-29 18:01:58 -0700115 /* assign device register values we mask-out D7S_FLIP
116 * if in sol_compat mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Christoph Hellwig1d5d00b2005-11-07 14:13:01 -0800118 if (get_user(ireg, (int __user *) arg)) {
119 error = -EFAULT;
120 break;
121 }
David S. Miller95d43902008-08-29 18:01:58 -0700122 if (sol_compat) {
123 if (regs & D7S_FLIP)
124 ireg |= D7S_FLIP;
125 else
126 ireg &= ~D7S_FLIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
David S. Miller95d43902008-08-29 18:01:58 -0700128 writeb(ireg, p->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 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 */
Christoph Hellwig1d5d00b2005-11-07 14:13:01 -0800138 if (put_user(regs, (int __user *) arg)) {
139 error = -EFAULT;
140 break;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 break;
143
144 case D7SIOCTM:
145 /* toggle device mode-- flip display orientation */
Rasmus Villemoes54dcf0c2014-06-19 14:31:52 +0200146 regs ^= D7S_FLIP;
David S. Miller95d43902008-08-29 18:01:58 -0700147 writeb(regs, p->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 break;
Peter Senna Tschudinda201162012-09-12 07:03:12 +0000149 }
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700150 mutex_unlock(&d7s_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Christoph Hellwig1d5d00b2005-11-07 14:13:01 -0800152 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Arjan van de Ven00977a52007-02-12 00:55:34 -0800155static const struct file_operations d7s_fops = {
Christoph Hellwig1d5d00b2005-11-07 14:13:01 -0800156 .owner = THIS_MODULE,
157 .unlocked_ioctl = d7s_ioctl,
158 .compat_ioctl = d7s_ioctl,
159 .open = d7s_open,
160 .release = d7s_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200161 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
David S. Miller95d43902008-08-29 18:01:58 -0700164static struct miscdevice d7s_miscdev = {
165 .minor = D7S_MINOR,
166 .name = DRIVER_NAME,
167 .fops = &d7s_fops
168};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800170static int d7s_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
David S. Miller95d43902008-08-29 18:01:58 -0700172 struct device_node *opts;
173 int err = -EINVAL;
174 struct d7s *p;
175 u8 regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David S. Miller95d43902008-08-29 18:01:58 -0700177 if (d7s_device)
178 goto out;
179
Himangi Saraogi57a4a3d2014-05-27 03:02:07 +0530180 p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
David S. Miller95d43902008-08-29 18:01:58 -0700181 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
David S. Miller95d43902008-08-29 18:01:58 -0700191 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
David S. Miller95d43902008-08-29 18:01:58 -0700198 /* OBP option "d7s-flipped?" is honored as default for the
199 * device, and reset default when detached
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
David S. Miller95d43902008-08-29 18:01:58 -0700201 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David S. Miller95d43902008-08-29 18:01:58 -0700207 if (p->flipped)
208 regs |= D7S_FLIP;
209 else
210 regs &= ~D7S_FLIP;
211
212 writeb(regs, p->regs);
213
Rob Herring8cd3ec52017-07-18 16:43:27 -0500214 printk(KERN_INFO PFX "7-Segment Display%pOF at [%s:0x%llx] %s\n",
215 op->dev.of_node,
David S. Miller95d43902008-08-29 18:01:58 -0700216 (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;
Yangtao Li87d81a22018-11-20 08:30:40 -0500223 of_node_put(opts);
David S. Miller95d43902008-08-29 18:01:58 -0700224
225out:
226 return err;
227
228out_iounmap:
229 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
230
231out_free:
David S. Miller95d43902008-08-29 18:01:58 -0700232 goto out;
233}
234
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800235static int d7s_remove(struct platform_device *op)
David S. Miller95d43902008-08-29 18:01:58 -0700236{
237 struct d7s *p = dev_get_drvdata(&op->dev);
238 u8 regs = readb(p->regs);
239
240 /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
241 if (sol_compat) {
242 if (p->flipped)
243 regs |= D7S_FLIP;
244 else
245 regs &= ~D7S_FLIP;
246 writeb(regs, p->regs);
247 }
248
249 misc_deregister(&d7s_miscdev);
250 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 return 0;
253}
254
David S. Millerfd098312008-08-31 01:23:17 -0700255static const struct of_device_id d7s_match[] = {
David S. Miller95d43902008-08-29 18:01:58 -0700256 {
257 .name = "display7seg",
258 },
259 {},
260};
261MODULE_DEVICE_TABLE(of, d7s_match);
262
Grant Likely4ebb24f2011-02-22 20:01:33 -0700263static struct platform_driver d7s_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700264 .driver = {
265 .name = DRIVER_NAME,
Grant Likely40182942010-04-13 16:13:02 -0700266 .of_match_table = d7s_match,
267 },
David S. Miller95d43902008-08-29 18:01:58 -0700268 .probe = d7s_probe,
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800269 .remove = d7s_remove,
David S. Miller95d43902008-08-29 18:01:58 -0700270};
271
Axel Lindbf2b922011-11-26 19:04:25 +0000272module_platform_driver(d7s_driver);