David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 1 | /* display7seg.c - Driver implementation for the 7-segment display |
| 2 | * present on Sun Microsystems CP1400 and CP1500 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2000 Eric Brower (ebrower@usa.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Himangi Saraogi | 57a4a3d | 2014-05-27 03:02:07 +0530 | [diff] [blame] | 7 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/miscdevice.h> |
Peter Osterlund | fb911ee | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 14 | #include <linux/ioport.h> /* request_region */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 16 | #include <linux/mutex.h> |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 19 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/uaccess.h> /* put_/get_user */ |
David S. Miller | 3049f89 | 2007-05-13 22:22:47 -0700 | [diff] [blame] | 21 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/display7seg.h> |
| 24 | |
| 25 | #define D7S_MINOR 193 |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 26 | #define DRIVER_NAME "d7s" |
| 27 | #define PFX DRIVER_NAME ": " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 29 | static DEFINE_MUTEX(d7s_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static int sol_compat = 0; /* Solaris compatibility mode */ |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 46 | module_param(sol_compat, int, 0); |
| 47 | MODULE_PARM_DESC(sol_compat, |
| 48 | "Disables documented functionality omitted from Solaris driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 50 | MODULE_AUTHOR("Eric Brower <ebrower@usa.net>"); |
| 51 | MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | MODULE_LICENSE("GPL"); |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 53 | MODULE_SUPPORTED_DEVICE("d7s"); |
| 54 | |
| 55 | struct d7s { |
| 56 | void __iomem *regs; |
| 57 | bool flipped; |
| 58 | }; |
| 59 | struct d7s *d7s_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static atomic_t d7s_users = ATOMIC_INIT(0); |
| 73 | |
| 74 | static 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 | |
| 82 | static 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) { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 89 | struct d7s *p = d7s_device; |
| 90 | u8 regval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 92 | regval = readb(p->regs); |
| 93 | if (p->flipped) |
| 94 | regval |= D7S_FLIP; |
| 95 | else |
| 96 | regval &= ~D7S_FLIP; |
| 97 | writeb(regval, p->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 103 | static long d7s_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 105 | struct d7s *p = d7s_device; |
| 106 | u8 regs = readb(p->regs); |
Andrew Morton | a5aac37 | 2005-11-10 21:14:16 -0800 | [diff] [blame] | 107 | int error = 0; |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 108 | u8 ireg = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 110 | if (D7S_MINOR != iminor(file_inode(file))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return -ENODEV; |
| 112 | |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 113 | mutex_lock(&d7s_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | switch (cmd) { |
| 115 | case D7SIOCWR: |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 116 | /* assign device register values we mask-out D7S_FLIP |
| 117 | * if in sol_compat mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | */ |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 119 | if (get_user(ireg, (int __user *) arg)) { |
| 120 | error = -EFAULT; |
| 121 | break; |
| 122 | } |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 123 | if (sol_compat) { |
| 124 | if (regs & D7S_FLIP) |
| 125 | ireg |= D7S_FLIP; |
| 126 | else |
| 127 | ireg &= ~D7S_FLIP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 129 | writeb(ireg, p->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 139 | if (put_user(regs, (int __user *) arg)) { |
| 140 | error = -EFAULT; |
| 141 | break; |
| 142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | break; |
| 144 | |
| 145 | case D7SIOCTM: |
| 146 | /* toggle device mode-- flip display orientation */ |
Rasmus Villemoes | 54dcf0c | 2014-06-19 14:31:52 +0200 | [diff] [blame] | 147 | regs ^= D7S_FLIP; |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 148 | writeb(regs, p->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | break; |
Peter Senna Tschudin | da20116 | 2012-09-12 07:03:12 +0000 | [diff] [blame] | 150 | } |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 151 | mutex_unlock(&d7s_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 153 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 156 | static const struct file_operations d7s_fops = { |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 157 | .owner = THIS_MODULE, |
| 158 | .unlocked_ioctl = d7s_ioctl, |
| 159 | .compat_ioctl = d7s_ioctl, |
| 160 | .open = d7s_open, |
| 161 | .release = d7s_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 162 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 165 | static struct miscdevice d7s_miscdev = { |
| 166 | .minor = D7S_MINOR, |
| 167 | .name = DRIVER_NAME, |
| 168 | .fops = &d7s_fops |
| 169 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Greg Kroah-Hartman | 082a200 | 2012-12-21 13:24:23 -0800 | [diff] [blame] | 171 | static int d7s_probe(struct platform_device *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 173 | struct device_node *opts; |
| 174 | int err = -EINVAL; |
| 175 | struct d7s *p; |
| 176 | u8 regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 178 | if (d7s_device) |
| 179 | goto out; |
| 180 | |
Himangi Saraogi | 57a4a3d | 2014-05-27 03:02:07 +0530 | [diff] [blame] | 181 | p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL); |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 182 | err = -ENOMEM; |
| 183 | if (!p) |
| 184 | goto out; |
| 185 | |
| 186 | p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s"); |
| 187 | if (!p->regs) { |
| 188 | printk(KERN_ERR PFX "Cannot map chip registers\n"); |
| 189 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 192 | err = misc_register(&d7s_miscdev); |
| 193 | if (err) { |
| 194 | printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n", |
| 195 | D7S_MINOR); |
| 196 | goto out_iounmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 199 | /* OBP option "d7s-flipped?" is honored as default for the |
| 200 | * device, and reset default when detached |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | */ |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 202 | regs = readb(p->regs); |
| 203 | opts = of_find_node_by_path("/options"); |
| 204 | if (opts && |
| 205 | of_get_property(opts, "d7s-flipped?", NULL)) |
| 206 | p->flipped = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 208 | if (p->flipped) |
| 209 | regs |= D7S_FLIP; |
| 210 | else |
| 211 | regs &= ~D7S_FLIP; |
| 212 | |
| 213 | writeb(regs, p->regs); |
| 214 | |
Sam Ravnborg | 3f4528d | 2009-01-06 13:20:38 -0800 | [diff] [blame] | 215 | printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%llx] %s\n", |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 216 | op->dev.of_node->full_name, |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 217 | (regs & D7S_FLIP) ? " (FLIPPED)" : "", |
| 218 | op->resource[0].start, |
| 219 | sol_compat ? "in sol_compat mode" : ""); |
| 220 | |
| 221 | dev_set_drvdata(&op->dev, p); |
| 222 | d7s_device = p; |
| 223 | err = 0; |
| 224 | |
| 225 | out: |
| 226 | return err; |
| 227 | |
| 228 | out_iounmap: |
| 229 | of_iounmap(&op->resource[0], p->regs, sizeof(u8)); |
| 230 | |
| 231 | out_free: |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 232 | goto out; |
| 233 | } |
| 234 | |
Greg Kroah-Hartman | 082a200 | 2012-12-21 13:24:23 -0800 | [diff] [blame] | 235 | static int d7s_remove(struct platform_device *op) |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 236 | { |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 255 | static const struct of_device_id d7s_match[] = { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 256 | { |
| 257 | .name = "display7seg", |
| 258 | }, |
| 259 | {}, |
| 260 | }; |
| 261 | MODULE_DEVICE_TABLE(of, d7s_match); |
| 262 | |
Grant Likely | 4ebb24f | 2011-02-22 20:01:33 -0700 | [diff] [blame] | 263 | static struct platform_driver d7s_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 264 | .driver = { |
| 265 | .name = DRIVER_NAME, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 266 | .of_match_table = d7s_match, |
| 267 | }, |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 268 | .probe = d7s_probe, |
Greg Kroah-Hartman | 082a200 | 2012-12-21 13:24:23 -0800 | [diff] [blame] | 269 | .remove = d7s_remove, |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
Axel Lin | dbf2b92 | 2011-11-26 19:04:25 +0000 | [diff] [blame] | 272 | module_platform_driver(d7s_driver); |