blob: d93595b39afa97a3cc705ebf6f27a4a39bcab354 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
David S. Miller95d43902008-08-29 18:01:58 -07002/* display7seg.c - Driver implementation for the 7-segment display
3 * present on Sun Microsystems CP1400 and CP1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Himangi Saraogi57a4a3d2014-05-27 03:02:07 +05308#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/fs.h>
12#include <linux/errno.h>
13#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/miscdevice.h>
Peter Osterlundfb911ee2005-09-13 01:25:15 -070015#include <linux/ioport.h> /* request_region */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070017#include <linux/mutex.h>
David S. Miller95d43902008-08-29 18:01:58 -070018#include <linux/of.h>
19#include <linux/of_device.h>
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h> /* put_/get_user */
David S. Miller3049f892007-05-13 22:22:47 -070022#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/display7seg.h>
25
David S. Miller95d43902008-08-29 18:01:58 -070026#define DRIVER_NAME "d7s"
27#define PFX DRIVER_NAME ": "
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070029static DEFINE_MUTEX(d7s_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int sol_compat = 0; /* Solaris compatibility mode */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/* 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 */
David S. Miller95d43902008-08-29 18:01:58 -070046module_param(sol_compat, int, 0);
47MODULE_PARM_DESC(sol_compat,
48 "Disables documented functionality omitted from Solaris driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David S. Miller95d43902008-08-29 18:01:58 -070050MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
51MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052MODULE_LICENSE("GPL");
David S. Miller95d43902008-08-29 18:01:58 -070053
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,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200158 .compat_ioctl = compat_ptr_ioctl,
Christoph Hellwig1d5d00b2005-11-07 14:13:01 -0800159 .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");
Jing Xiangfengbf8c5542020-09-11 15:13:41 +0800188 goto out;
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));
David S. Miller95d43902008-08-29 18:01:58 -0700230 goto out;
231}
232
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800233static int d7s_remove(struct platform_device *op)
David S. Miller95d43902008-08-29 18:01:58 -0700234{
235 struct d7s *p = dev_get_drvdata(&op->dev);
236 u8 regs = readb(p->regs);
237
238 /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
239 if (sol_compat) {
240 if (p->flipped)
241 regs |= D7S_FLIP;
242 else
243 regs &= ~D7S_FLIP;
244 writeb(regs, p->regs);
245 }
246
247 misc_deregister(&d7s_miscdev);
248 of_iounmap(&op->resource[0], p->regs, sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 return 0;
251}
252
David S. Millerfd098312008-08-31 01:23:17 -0700253static const struct of_device_id d7s_match[] = {
David S. Miller95d43902008-08-29 18:01:58 -0700254 {
255 .name = "display7seg",
256 },
257 {},
258};
259MODULE_DEVICE_TABLE(of, d7s_match);
260
Grant Likely4ebb24f2011-02-22 20:01:33 -0700261static struct platform_driver d7s_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700262 .driver = {
263 .name = DRIVER_NAME,
Grant Likely40182942010-04-13 16:13:02 -0700264 .of_match_table = d7s_match,
265 },
David S. Miller95d43902008-08-29 18:01:58 -0700266 .probe = d7s_probe,
Greg Kroah-Hartman082a2002012-12-21 13:24:23 -0800267 .remove = d7s_remove,
David S. Miller95d43902008-08-29 18:01:58 -0700268};
269
Axel Lindbf2b922011-11-26 19:04:25 +0000270module_platform_driver(d7s_driver);