| 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 */ | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 15 | #include <linux/slab.h> | 
| Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 16 | #include <linux/smp_lock.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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/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 |  | 
|  | 29 | static int sol_compat = 0;		/* Solaris compatibility mode	*/ | 
|  | 30 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* 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. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 45 | module_param(sol_compat, int, 0); | 
|  | 46 | MODULE_PARM_DESC(sol_compat, | 
|  | 47 | "Disables documented functionality omitted from Solaris driver"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 49 | MODULE_AUTHOR("Eric Brower <ebrower@usa.net>"); | 
|  | 50 | MODULE_DESCRIPTION("7-Segment Display driver for Sun Microsystems CP1400/1500"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | MODULE_LICENSE("GPL"); | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 52 | MODULE_SUPPORTED_DEVICE("d7s"); | 
|  | 53 |  | 
|  | 54 | struct d7s { | 
|  | 55 | void __iomem	*regs; | 
|  | 56 | bool		flipped; | 
|  | 57 | }; | 
|  | 58 | struct d7s *d7s_device; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 |  | 
|  | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | static atomic_t d7s_users = ATOMIC_INIT(0); | 
|  | 72 |  | 
|  | 73 | static int d7s_open(struct inode *inode, struct file *f) | 
|  | 74 | { | 
|  | 75 | if (D7S_MINOR != iminor(inode)) | 
|  | 76 | return -ENODEV; | 
| Arnd Bergmann | 009228d | 2008-05-20 19:15:47 +0200 | [diff] [blame] | 77 | cycle_kernel_lock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Josef Sipek | 7fa95f7 | 2006-12-08 02:37:36 -0800 | [diff] [blame] | 110 | if (D7S_MINOR != iminor(file->f_path.dentry->d_inode)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return -ENODEV; | 
|  | 112 |  | 
| Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 113 | lock_kernel(); | 
| 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 */ | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 147 | if (regs & D7S_FLIP) | 
|  | 148 | regs &= ~D7S_FLIP; | 
|  | 149 | else | 
|  | 150 | regs |= D7S_FLIP; | 
|  | 151 | writeb(regs, p->regs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | break; | 
|  | 153 | }; | 
| David S. Miller | feee207 | 2005-11-09 12:05:37 -0800 | [diff] [blame] | 154 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 |  | 
| Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 156 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 159 | static const struct file_operations d7s_fops = { | 
| Christoph Hellwig | 1d5d00b | 2005-11-07 14:13:01 -0800 | [diff] [blame] | 160 | .owner =		THIS_MODULE, | 
|  | 161 | .unlocked_ioctl =	d7s_ioctl, | 
|  | 162 | .compat_ioctl =		d7s_ioctl, | 
|  | 163 | .open =			d7s_open, | 
|  | 164 | .release =		d7s_release, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | }; | 
|  | 166 |  | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 167 | static struct miscdevice d7s_miscdev = { | 
|  | 168 | .minor		= D7S_MINOR, | 
|  | 169 | .name		= DRIVER_NAME, | 
|  | 170 | .fops		= &d7s_fops | 
|  | 171 | }; | 
| 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 | static int __devinit d7s_probe(struct of_device *op, | 
|  | 174 | const struct of_device_id *match) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 176 | struct device_node *opts; | 
|  | 177 | int err = -EINVAL; | 
|  | 178 | struct d7s *p; | 
|  | 179 | u8 regs; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 |  | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 181 | if (d7s_device) | 
|  | 182 | goto out; | 
|  | 183 |  | 
|  | 184 | p = kzalloc(sizeof(*p), GFP_KERNEL); | 
|  | 185 | err = -ENOMEM; | 
|  | 186 | if (!p) | 
|  | 187 | goto out; | 
|  | 188 |  | 
|  | 189 | p->regs = of_ioremap(&op->resource[0], 0, sizeof(u8), "d7s"); | 
|  | 190 | if (!p->regs) { | 
|  | 191 | printk(KERN_ERR PFX "Cannot map chip registers\n"); | 
|  | 192 | goto out_free; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 195 | err = misc_register(&d7s_miscdev); | 
|  | 196 | if (err) { | 
|  | 197 | printk(KERN_ERR PFX "Unable to acquire miscdevice minor %i\n", | 
|  | 198 | D7S_MINOR); | 
|  | 199 | goto out_iounmap; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 202 | /* OBP option "d7s-flipped?" is honored as default for the | 
|  | 203 | * device, and reset default when detached | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | */ | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 205 | regs = readb(p->regs); | 
|  | 206 | opts = of_find_node_by_path("/options"); | 
|  | 207 | if (opts && | 
|  | 208 | of_get_property(opts, "d7s-flipped?", NULL)) | 
|  | 209 | p->flipped = true; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 |  | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 211 | if (p->flipped) | 
|  | 212 | regs |= D7S_FLIP; | 
|  | 213 | else | 
|  | 214 | regs &= ~D7S_FLIP; | 
|  | 215 |  | 
|  | 216 | writeb(regs,  p->regs); | 
|  | 217 |  | 
| Sam Ravnborg | 3f4528d | 2009-01-06 13:20:38 -0800 | [diff] [blame] | 218 | printk(KERN_INFO PFX "7-Segment Display%s at [%s:0x%llx] %s\n", | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 219 | op->node->full_name, | 
|  | 220 | (regs & D7S_FLIP) ? " (FLIPPED)" : "", | 
|  | 221 | op->resource[0].start, | 
|  | 222 | sol_compat ? "in sol_compat mode" : ""); | 
|  | 223 |  | 
|  | 224 | dev_set_drvdata(&op->dev, p); | 
|  | 225 | d7s_device = p; | 
|  | 226 | err = 0; | 
|  | 227 |  | 
|  | 228 | out: | 
|  | 229 | return err; | 
|  | 230 |  | 
|  | 231 | out_iounmap: | 
|  | 232 | of_iounmap(&op->resource[0], p->regs, sizeof(u8)); | 
|  | 233 |  | 
|  | 234 | out_free: | 
|  | 235 | kfree(p); | 
|  | 236 | goto out; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | static int __devexit d7s_remove(struct of_device *op) | 
|  | 240 | { | 
|  | 241 | struct d7s *p = dev_get_drvdata(&op->dev); | 
|  | 242 | u8 regs = readb(p->regs); | 
|  | 243 |  | 
|  | 244 | /* Honor OBP d7s-flipped? unless operating in solaris-compat mode */ | 
|  | 245 | if (sol_compat) { | 
|  | 246 | if (p->flipped) | 
|  | 247 | regs |= D7S_FLIP; | 
|  | 248 | else | 
|  | 249 | regs &= ~D7S_FLIP; | 
|  | 250 | writeb(regs, p->regs); | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | misc_deregister(&d7s_miscdev); | 
|  | 254 | of_iounmap(&op->resource[0], p->regs, sizeof(u8)); | 
|  | 255 | kfree(p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 |  | 
|  | 257 | return 0; | 
|  | 258 | } | 
|  | 259 |  | 
| David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 260 | static const struct of_device_id d7s_match[] = { | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 261 | { | 
|  | 262 | .name = "display7seg", | 
|  | 263 | }, | 
|  | 264 | {}, | 
|  | 265 | }; | 
|  | 266 | MODULE_DEVICE_TABLE(of, d7s_match); | 
|  | 267 |  | 
|  | 268 | static struct of_platform_driver d7s_driver = { | 
|  | 269 | .name		= DRIVER_NAME, | 
|  | 270 | .match_table	= d7s_match, | 
|  | 271 | .probe		= d7s_probe, | 
|  | 272 | .remove		= __devexit_p(d7s_remove), | 
|  | 273 | }; | 
|  | 274 |  | 
|  | 275 | static int __init d7s_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 277 | return of_register_driver(&d7s_driver, &of_bus_type); | 
|  | 278 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 |  | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 280 | static void __exit d7s_exit(void) | 
|  | 281 | { | 
|  | 282 | of_unregister_driver(&d7s_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } | 
|  | 284 |  | 
|  | 285 | module_init(d7s_init); | 
| David S. Miller | 95d4390 | 2008-08-29 18:01:58 -0700 | [diff] [blame] | 286 | module_exit(d7s_exit); |