William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * GPIO driver for the ACCES 104-IDIO-16 family |
| 3 | * Copyright (C) 2015 William Breathitt Gray |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | */ |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 14 | #include <linux/bitops.h> |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/gpio/driver.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/ioport.h> |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/irqdesc.h> |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | |
| 28 | static unsigned idio_16_base; |
| 29 | module_param(idio_16_base, uint, 0); |
| 30 | MODULE_PARM_DESC(idio_16_base, "ACCES 104-IDIO-16 base address"); |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 31 | static unsigned idio_16_irq; |
| 32 | module_param(idio_16_irq, uint, 0); |
| 33 | MODULE_PARM_DESC(idio_16_irq, "ACCES 104-IDIO-16 interrupt line number"); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * struct idio_16_gpio - GPIO device private data structure |
| 37 | * @chip: instance of the gpio_chip |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 38 | * @lock: synchronization lock to prevent I/O race conditions |
| 39 | * @irq_mask: I/O bits affected by interrupts |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 40 | * @base: base port address of the GPIO device |
| 41 | * @extent: extent of port address region of the GPIO device |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 42 | * @irq: Interrupt line number |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 43 | * @out_state: output bits state |
| 44 | */ |
| 45 | struct idio_16_gpio { |
| 46 | struct gpio_chip chip; |
| 47 | spinlock_t lock; |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 48 | unsigned long irq_mask; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 49 | unsigned base; |
| 50 | unsigned extent; |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 51 | unsigned irq; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 52 | unsigned out_state; |
| 53 | }; |
| 54 | |
| 55 | static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
| 56 | { |
| 57 | if (offset > 15) |
| 58 | return 1; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 64 | { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int idio_16_gpio_direction_output(struct gpio_chip *chip, |
| 69 | unsigned offset, int value) |
| 70 | { |
| 71 | chip->set(chip, offset, value); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static struct idio_16_gpio *to_idio16gpio(struct gpio_chip *gc) |
| 76 | { |
| 77 | return container_of(gc, struct idio_16_gpio, chip); |
| 78 | } |
| 79 | |
| 80 | static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 81 | { |
| 82 | struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip); |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 83 | const unsigned mask = BIT(offset-16); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 84 | |
| 85 | if (offset < 16) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | if (offset < 24) |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 89 | return !!(inb(idio16gpio->base + 1) & mask); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 90 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 91 | return !!(inb(idio16gpio->base + 5) & (mask>>8)); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 95 | { |
| 96 | struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip); |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 97 | const unsigned mask = BIT(offset); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 98 | unsigned long flags; |
| 99 | |
| 100 | if (offset > 15) |
| 101 | return; |
| 102 | |
| 103 | spin_lock_irqsave(&idio16gpio->lock, flags); |
| 104 | |
| 105 | if (value) |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 106 | idio16gpio->out_state |= mask; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 107 | else |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 108 | idio16gpio->out_state &= ~mask; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 109 | |
| 110 | if (offset > 7) |
| 111 | outb(idio16gpio->out_state >> 8, idio16gpio->base + 4); |
| 112 | else |
| 113 | outb(idio16gpio->out_state, idio16gpio->base); |
| 114 | |
| 115 | spin_unlock_irqrestore(&idio16gpio->lock, flags); |
| 116 | } |
| 117 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 118 | static void idio_16_irq_ack(struct irq_data *data) |
| 119 | { |
| 120 | struct gpio_chip *chip = irq_data_get_irq_chip_data(data); |
| 121 | struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip); |
| 122 | unsigned long flags; |
| 123 | |
| 124 | spin_lock_irqsave(&idio16gpio->lock, flags); |
| 125 | |
| 126 | outb(0, idio16gpio->base + 1); |
| 127 | |
| 128 | spin_unlock_irqrestore(&idio16gpio->lock, flags); |
| 129 | } |
| 130 | |
| 131 | static void idio_16_irq_mask(struct irq_data *data) |
| 132 | { |
| 133 | struct gpio_chip *chip = irq_data_get_irq_chip_data(data); |
| 134 | struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip); |
| 135 | const unsigned long mask = BIT(irqd_to_hwirq(data)); |
| 136 | unsigned long flags; |
| 137 | |
| 138 | idio16gpio->irq_mask &= ~mask; |
| 139 | |
| 140 | if (!idio16gpio->irq_mask) { |
| 141 | spin_lock_irqsave(&idio16gpio->lock, flags); |
| 142 | |
| 143 | outb(0, idio16gpio->base + 2); |
| 144 | |
| 145 | spin_unlock_irqrestore(&idio16gpio->lock, flags); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static void idio_16_irq_unmask(struct irq_data *data) |
| 150 | { |
| 151 | struct gpio_chip *chip = irq_data_get_irq_chip_data(data); |
| 152 | struct idio_16_gpio *const idio16gpio = to_idio16gpio(chip); |
| 153 | const unsigned long mask = BIT(irqd_to_hwirq(data)); |
| 154 | const unsigned long prev_irq_mask = idio16gpio->irq_mask; |
| 155 | unsigned long flags; |
| 156 | |
| 157 | idio16gpio->irq_mask |= mask; |
| 158 | |
| 159 | if (!prev_irq_mask) { |
| 160 | spin_lock_irqsave(&idio16gpio->lock, flags); |
| 161 | |
| 162 | outb(0, idio16gpio->base + 1); |
| 163 | inb(idio16gpio->base + 2); |
| 164 | |
| 165 | spin_unlock_irqrestore(&idio16gpio->lock, flags); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type) |
| 170 | { |
| 171 | /* The only valid irq types are none and both-edges */ |
| 172 | if (flow_type != IRQ_TYPE_NONE && |
| 173 | (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static struct irq_chip idio_16_irqchip = { |
| 180 | .name = "104-idio-16", |
| 181 | .irq_ack = idio_16_irq_ack, |
| 182 | .irq_mask = idio_16_irq_mask, |
| 183 | .irq_unmask = idio_16_irq_unmask, |
| 184 | .irq_set_type = idio_16_irq_set_type |
| 185 | }; |
| 186 | |
| 187 | static irqreturn_t idio_16_irq_handler(int irq, void *dev_id) |
| 188 | { |
| 189 | struct idio_16_gpio *const idio16gpio = dev_id; |
| 190 | struct gpio_chip *const chip = &idio16gpio->chip; |
| 191 | int gpio; |
| 192 | |
| 193 | for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio) |
| 194 | generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio)); |
| 195 | |
| 196 | return IRQ_HANDLED; |
| 197 | } |
| 198 | |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 199 | static int __init idio_16_probe(struct platform_device *pdev) |
| 200 | { |
| 201 | struct device *dev = &pdev->dev; |
| 202 | struct idio_16_gpio *idio16gpio; |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 203 | const unsigned base = idio_16_base; |
| 204 | const unsigned extent = 8; |
| 205 | const char *const name = dev_name(dev); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 206 | int err; |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 207 | const unsigned irq = idio_16_irq; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 208 | |
| 209 | idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL); |
| 210 | if (!idio16gpio) |
| 211 | return -ENOMEM; |
| 212 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 213 | if (!request_region(base, extent, name)) { |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 214 | dev_err(dev, "Unable to lock %s port addresses (0x%X-0x%X)\n", |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 215 | name, base, base + extent); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 216 | err = -EBUSY; |
| 217 | goto err_lock_io_port; |
| 218 | } |
| 219 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 220 | idio16gpio->chip.label = name; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 221 | idio16gpio->chip.parent = dev; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 222 | idio16gpio->chip.owner = THIS_MODULE; |
| 223 | idio16gpio->chip.base = -1; |
| 224 | idio16gpio->chip.ngpio = 32; |
| 225 | idio16gpio->chip.get_direction = idio_16_gpio_get_direction; |
| 226 | idio16gpio->chip.direction_input = idio_16_gpio_direction_input; |
| 227 | idio16gpio->chip.direction_output = idio_16_gpio_direction_output; |
| 228 | idio16gpio->chip.get = idio_16_gpio_get; |
| 229 | idio16gpio->chip.set = idio_16_gpio_set; |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 230 | idio16gpio->base = base; |
| 231 | idio16gpio->extent = extent; |
| 232 | idio16gpio->irq = irq; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 233 | idio16gpio->out_state = 0xFFFF; |
| 234 | |
| 235 | spin_lock_init(&idio16gpio->lock); |
| 236 | |
| 237 | dev_set_drvdata(dev, idio16gpio); |
| 238 | |
| 239 | err = gpiochip_add(&idio16gpio->chip); |
| 240 | if (err) { |
| 241 | dev_err(dev, "GPIO registering failed (%d)\n", err); |
| 242 | goto err_gpio_register; |
| 243 | } |
| 244 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 245 | err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0, |
| 246 | handle_edge_irq, IRQ_TYPE_NONE); |
| 247 | if (err) { |
| 248 | dev_err(dev, "Could not add irqchip (%d)\n", err); |
| 249 | goto err_gpiochip_irqchip_add; |
| 250 | } |
| 251 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 252 | err = request_irq(irq, idio_16_irq_handler, 0, name, idio16gpio); |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 253 | if (err) { |
| 254 | dev_err(dev, "IRQ handler registering failed (%d)\n", err); |
| 255 | goto err_request_irq; |
| 256 | } |
| 257 | |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 258 | return 0; |
| 259 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 260 | err_request_irq: |
| 261 | err_gpiochip_irqchip_add: |
| 262 | gpiochip_remove(&idio16gpio->chip); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 263 | err_gpio_register: |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame^] | 264 | release_region(base, extent); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 265 | err_lock_io_port: |
| 266 | return err; |
| 267 | } |
| 268 | |
| 269 | static int idio_16_remove(struct platform_device *pdev) |
| 270 | { |
| 271 | struct idio_16_gpio *const idio16gpio = platform_get_drvdata(pdev); |
| 272 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 273 | free_irq(idio16gpio->irq, idio16gpio); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 274 | gpiochip_remove(&idio16gpio->chip); |
| 275 | release_region(idio16gpio->base, idio16gpio->extent); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static struct platform_device *idio_16_device; |
| 281 | |
| 282 | static struct platform_driver idio_16_driver = { |
| 283 | .driver = { |
| 284 | .name = "104-idio-16" |
| 285 | }, |
| 286 | .remove = idio_16_remove |
| 287 | }; |
| 288 | |
| 289 | static void __exit idio_16_exit(void) |
| 290 | { |
| 291 | platform_device_unregister(idio_16_device); |
| 292 | platform_driver_unregister(&idio_16_driver); |
| 293 | } |
| 294 | |
| 295 | static int __init idio_16_init(void) |
| 296 | { |
| 297 | int err; |
| 298 | |
| 299 | idio_16_device = platform_device_alloc(idio_16_driver.driver.name, -1); |
| 300 | if (!idio_16_device) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | err = platform_device_add(idio_16_device); |
| 304 | if (err) |
| 305 | goto err_platform_device; |
| 306 | |
| 307 | err = platform_driver_probe(&idio_16_driver, idio_16_probe); |
| 308 | if (err) |
| 309 | goto err_platform_driver; |
| 310 | |
| 311 | return 0; |
| 312 | |
| 313 | err_platform_driver: |
| 314 | platform_device_del(idio_16_device); |
| 315 | err_platform_device: |
| 316 | platform_device_put(idio_16_device); |
| 317 | return err; |
| 318 | } |
| 319 | |
| 320 | module_init(idio_16_init); |
| 321 | module_exit(idio_16_exit); |
| 322 | |
| 323 | MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); |
| 324 | MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver"); |
| 325 | MODULE_LICENSE("GPL"); |