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 | |
| 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> |
Peter Osterlund | fb911ee | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 14 | #include <linux/ioport.h> /* request_region */ |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 15 | #include <linux/smp_lock.h> |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/uaccess.h> /* put_/get_user */ |
David S. Miller | 3049f89 | 2007-05-13 22:22:47 -0700 | [diff] [blame] | 20 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/display7seg.h> |
| 23 | |
| 24 | #define D7S_MINOR 193 |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 25 | #define DRIVER_NAME "d7s" |
| 26 | #define PFX DRIVER_NAME ": " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | static int sol_compat = 0; /* Solaris compatibility mode */ |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /* Solaris compatibility flag - |
| 31 | * The Solaris implementation omits support for several |
| 32 | * documented driver features (ref Sun doc 806-0180-03). |
| 33 | * By default, this module supports the documented driver |
| 34 | * abilities, rather than the Solaris implementation: |
| 35 | * |
| 36 | * 1) Device ALWAYS reverts to OBP-specified FLIPPED mode |
| 37 | * upon closure of device or module unload. |
| 38 | * 2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of |
| 39 | * FLIP bit |
| 40 | * |
| 41 | * If you wish the device to operate as under Solaris, |
| 42 | * omitting above features, set this parameter to non-zero. |
| 43 | */ |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 44 | module_param(sol_compat, int, 0); |
| 45 | MODULE_PARM_DESC(sol_compat, |
| 46 | "Disables documented functionality omitted from Solaris driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 48 | MODULE_AUTHOR("Eric Brower <ebrower@usa.net>"); |
| 49 | MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | MODULE_LICENSE("GPL"); |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 51 | MODULE_SUPPORTED_DEVICE("d7s"); |
| 52 | |
| 53 | struct d7s { |
| 54 | void __iomem *regs; |
| 55 | bool flipped; |
| 56 | }; |
| 57 | struct d7s *d7s_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Register block address- see header for details |
| 61 | * ----------------------------------------- |
| 62 | * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 | |
| 63 | * ----------------------------------------- |
| 64 | * |
| 65 | * DP - Toggles decimal point on/off |
| 66 | * ALARM - Toggles "Alarm" LED green/red |
| 67 | * FLIP - Inverts display for upside-down mounted board |
| 68 | * bits 0-4 - 7-segment display contents |
| 69 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static atomic_t d7s_users = ATOMIC_INIT(0); |
| 71 | |
| 72 | static int d7s_open(struct inode *inode, struct file *f) |
| 73 | { |
| 74 | if (D7S_MINOR != iminor(inode)) |
| 75 | return -ENODEV; |
Arnd Bergmann | 009228d | 2008-05-20 19:15:47 +0200 | [diff] [blame] | 76 | cycle_kernel_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | atomic_inc(&d7s_users); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static 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. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 88 | struct d7s *p = d7s_device; |
| 89 | u8 regval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 91 | regval = readb(p->regs); |
| 92 | if (p->flipped) |
| 93 | regval |= D7S_FLIP; |
| 94 | else |
| 95 | regval &= ~D7S_FLIP; |
| 96 | writeb(regval, p->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 102 | 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] | 103 | { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 104 | struct d7s *p = d7s_device; |
| 105 | u8 regs = readb(p->regs); |
Andrew Morton | a5aac37 | 2005-11-10 21:14:16 -0800 | [diff] [blame] | 106 | int error = 0; |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 107 | u8 ireg = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Josef Sipek | 7fa95f7 | 2006-12-08 02:37:36 -0800 | [diff] [blame] | 109 | if (D7S_MINOR != iminor(file->f_path.dentry->d_inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return -ENODEV; |
| 111 | |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 112 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | switch (cmd) { |
| 114 | case D7SIOCWR: |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 115 | /* assign device register values we mask-out D7S_FLIP |
| 116 | * if in sol_compat mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 118 | if (get_user(ireg, (int __user *) arg)) { |
| 119 | error = -EFAULT; |
| 120 | break; |
| 121 | } |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 122 | if (sol_compat) { |
| 123 | if (regs & D7S_FLIP) |
| 124 | ireg |= D7S_FLIP; |
| 125 | else |
| 126 | ireg &= ~D7S_FLIP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 128 | writeb(ireg, p->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 138 | if (put_user(regs, (int __user *) arg)) { |
| 139 | error = -EFAULT; |
| 140 | break; |
| 141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case D7SIOCTM: |
| 145 | /* toggle device mode-- flip display orientation */ |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 146 | if (regs & D7S_FLIP) |
| 147 | regs &= ~D7S_FLIP; |
| 148 | else |
| 149 | regs |= D7S_FLIP; |
| 150 | writeb(regs, p->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | break; |
| 152 | }; |
David S. Miller | feee207 | 2005-11-09 12:05:37 -0800 | [diff] [blame] | 153 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 155 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 158 | static const struct file_operations d7s_fops = { |
Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 159 | .owner = THIS_MODULE, |
| 160 | .unlocked_ioctl = d7s_ioctl, |
| 161 | .compat_ioctl = d7s_ioctl, |
| 162 | .open = d7s_open, |
| 163 | .release = d7s_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 166 | static struct miscdevice d7s_miscdev = { |
| 167 | .minor = D7S_MINOR, |
| 168 | .name = DRIVER_NAME, |
| 169 | .fops = &d7s_fops |
| 170 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 172 | static int __devinit d7s_probe(struct of_device *op, |
| 173 | const struct of_device_id *match) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 175 | struct device_node *opts; |
| 176 | int err = -EINVAL; |
| 177 | struct d7s *p; |
| 178 | u8 regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 180 | if (d7s_device) |
| 181 | goto out; |
| 182 | |
| 183 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 184 | err = -ENOMEM; |
| 185 | if (!p) |
| 186 | goto out; |
| 187 | |
| 188 | p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s"); |
| 189 | if (!p->regs) { |
| 190 | printk(KERN_ERR PFX "Cannot map chip registers\n"); |
| 191 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 194 | err = misc_register(&d7s_miscdev); |
| 195 | if (err) { |
| 196 | printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n", |
| 197 | D7S_MINOR); |
| 198 | goto out_iounmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 201 | /* OBP option "d7s-flipped?" is honored as default for the |
| 202 | * device, and reset default when detached |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | */ |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 204 | regs = readb(p->regs); |
| 205 | opts = of_find_node_by_path("/options"); |
| 206 | if (opts && |
| 207 | of_get_property(opts, "d7s-flipped?", NULL)) |
| 208 | p->flipped = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 210 | if (p->flipped) |
| 211 | regs |= D7S_FLIP; |
| 212 | else |
| 213 | regs &= ~D7S_FLIP; |
| 214 | |
| 215 | writeb(regs, p->regs); |
| 216 | |
| 217 | printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%lx] %s\n", |
| 218 | op->node->full_name, |
| 219 | (regs & D7S_FLIP) ? " (FLIPPED)" : "", |
| 220 | op->resource[0].start, |
| 221 | sol_compat ? "in sol_compat mode" : ""); |
| 222 | |
| 223 | dev_set_drvdata(&op->dev, p); |
| 224 | d7s_device = p; |
| 225 | err = 0; |
| 226 | |
| 227 | out: |
| 228 | return err; |
| 229 | |
| 230 | out_iounmap: |
| 231 | of_iounmap(&op->resource[0], p->regs, sizeof(u8)); |
| 232 | |
| 233 | out_free: |
| 234 | kfree(p); |
| 235 | goto out; |
| 236 | } |
| 237 | |
| 238 | static int __devexit d7s_remove(struct of_device *op) |
| 239 | { |
| 240 | struct d7s *p = dev_get_drvdata(&op->dev); |
| 241 | u8 regs = readb(p->regs); |
| 242 | |
| 243 | /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */ |
| 244 | if (sol_compat) { |
| 245 | if (p->flipped) |
| 246 | regs |= D7S_FLIP; |
| 247 | else |
| 248 | regs &= ~D7S_FLIP; |
| 249 | writeb(regs, p->regs); |
| 250 | } |
| 251 | |
| 252 | misc_deregister(&d7s_miscdev); |
| 253 | of_iounmap(&op->resource[0], p->regs, sizeof(u8)); |
| 254 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 259 | static const struct of_device_id d7s_match[] = { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 260 | { |
| 261 | .name = "display7seg", |
| 262 | }, |
| 263 | {}, |
| 264 | }; |
| 265 | MODULE_DEVICE_TABLE(of, d7s_match); |
| 266 | |
| 267 | static struct of_platform_driver d7s_driver = { |
| 268 | .name = DRIVER_NAME, |
| 269 | .match_table = d7s_match, |
| 270 | .probe = d7s_probe, |
| 271 | .remove = __devexit_p(d7s_remove), |
| 272 | }; |
| 273 | |
| 274 | static int __init d7s_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 276 | return of_register_driver(&d7s_driver, &of_bus_type); |
| 277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 279 | static void __exit d7s_exit(void) |
| 280 | { |
| 281 | of_unregister_driver(&d7s_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | module_init(d7s_init); |
David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 285 | module_exit(d7s_exit); |