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 | |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 75 | static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 76 | { |
Linus Walleij | d602ae9 | 2015-12-03 18:18:06 +0100 | [diff] [blame] | 77 | struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 78 | const unsigned mask = BIT(offset-16); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 79 | |
| 80 | if (offset < 16) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | if (offset < 24) |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 84 | return !!(inb(idio16gpio->base + 1) & mask); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 85 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 86 | return !!(inb(idio16gpio->base + 5) & (mask>>8)); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 90 | { |
Linus Walleij | d602ae9 | 2015-12-03 18:18:06 +0100 | [diff] [blame] | 91 | struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 92 | const unsigned mask = BIT(offset); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 93 | unsigned long flags; |
| 94 | |
| 95 | if (offset > 15) |
| 96 | return; |
| 97 | |
| 98 | spin_lock_irqsave(&idio16gpio->lock, flags); |
| 99 | |
| 100 | if (value) |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 101 | idio16gpio->out_state |= mask; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 102 | else |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 103 | idio16gpio->out_state &= ~mask; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 104 | |
| 105 | if (offset > 7) |
| 106 | outb(idio16gpio->out_state >> 8, idio16gpio->base + 4); |
| 107 | else |
| 108 | outb(idio16gpio->out_state, idio16gpio->base); |
| 109 | |
| 110 | spin_unlock_irqrestore(&idio16gpio->lock, flags); |
| 111 | } |
| 112 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 113 | static void idio_16_irq_ack(struct irq_data *data) |
| 114 | { |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void idio_16_irq_mask(struct irq_data *data) |
| 118 | { |
| 119 | struct gpio_chip *chip = irq_data_get_irq_chip_data(data); |
Linus Walleij | d602ae9 | 2015-12-03 18:18:06 +0100 | [diff] [blame] | 120 | struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 121 | const unsigned long mask = BIT(irqd_to_hwirq(data)); |
| 122 | unsigned long flags; |
| 123 | |
| 124 | idio16gpio->irq_mask &= ~mask; |
| 125 | |
| 126 | if (!idio16gpio->irq_mask) { |
| 127 | spin_lock_irqsave(&idio16gpio->lock, flags); |
| 128 | |
| 129 | outb(0, idio16gpio->base + 2); |
| 130 | |
| 131 | spin_unlock_irqrestore(&idio16gpio->lock, flags); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void idio_16_irq_unmask(struct irq_data *data) |
| 136 | { |
| 137 | struct gpio_chip *chip = irq_data_get_irq_chip_data(data); |
Linus Walleij | d602ae9 | 2015-12-03 18:18:06 +0100 | [diff] [blame] | 138 | struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 139 | const unsigned long mask = BIT(irqd_to_hwirq(data)); |
| 140 | const unsigned long prev_irq_mask = idio16gpio->irq_mask; |
| 141 | unsigned long flags; |
| 142 | |
| 143 | idio16gpio->irq_mask |= mask; |
| 144 | |
| 145 | if (!prev_irq_mask) { |
| 146 | spin_lock_irqsave(&idio16gpio->lock, flags); |
| 147 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 148 | inb(idio16gpio->base + 2); |
| 149 | |
| 150 | spin_unlock_irqrestore(&idio16gpio->lock, flags); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type) |
| 155 | { |
| 156 | /* The only valid irq types are none and both-edges */ |
| 157 | if (flow_type != IRQ_TYPE_NONE && |
| 158 | (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static struct irq_chip idio_16_irqchip = { |
| 165 | .name = "104-idio-16", |
| 166 | .irq_ack = idio_16_irq_ack, |
| 167 | .irq_mask = idio_16_irq_mask, |
| 168 | .irq_unmask = idio_16_irq_unmask, |
| 169 | .irq_set_type = idio_16_irq_set_type |
| 170 | }; |
| 171 | |
| 172 | static irqreturn_t idio_16_irq_handler(int irq, void *dev_id) |
| 173 | { |
| 174 | struct idio_16_gpio *const idio16gpio = dev_id; |
| 175 | struct gpio_chip *const chip = &idio16gpio->chip; |
| 176 | int gpio; |
| 177 | |
| 178 | for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio) |
| 179 | generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio)); |
| 180 | |
William Breathitt Gray | 12b61c9 | 2015-12-02 20:23:04 -0500 | [diff] [blame] | 181 | spin_lock(&idio16gpio->lock); |
| 182 | |
| 183 | outb(0, idio16gpio->base + 1); |
| 184 | |
| 185 | spin_unlock(&idio16gpio->lock); |
| 186 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 187 | return IRQ_HANDLED; |
| 188 | } |
| 189 | |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 190 | static int __init idio_16_probe(struct platform_device *pdev) |
| 191 | { |
| 192 | struct device *dev = &pdev->dev; |
| 193 | struct idio_16_gpio *idio16gpio; |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 194 | const unsigned base = idio_16_base; |
| 195 | const unsigned extent = 8; |
| 196 | const char *const name = dev_name(dev); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 197 | int err; |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 198 | const unsigned irq = idio_16_irq; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 199 | |
| 200 | idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL); |
| 201 | if (!idio16gpio) |
| 202 | return -ENOMEM; |
| 203 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 204 | if (!request_region(base, extent, name)) { |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 205 | 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] | 206 | name, base, base + extent); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 207 | err = -EBUSY; |
| 208 | goto err_lock_io_port; |
| 209 | } |
| 210 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 211 | idio16gpio->chip.label = name; |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 212 | idio16gpio->chip.parent = dev; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 213 | idio16gpio->chip.owner = THIS_MODULE; |
| 214 | idio16gpio->chip.base = -1; |
| 215 | idio16gpio->chip.ngpio = 32; |
| 216 | idio16gpio->chip.get_direction = idio_16_gpio_get_direction; |
| 217 | idio16gpio->chip.direction_input = idio_16_gpio_direction_input; |
| 218 | idio16gpio->chip.direction_output = idio_16_gpio_direction_output; |
| 219 | idio16gpio->chip.get = idio_16_gpio_get; |
| 220 | idio16gpio->chip.set = idio_16_gpio_set; |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 221 | idio16gpio->base = base; |
| 222 | idio16gpio->extent = extent; |
| 223 | idio16gpio->irq = irq; |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 224 | idio16gpio->out_state = 0xFFFF; |
| 225 | |
| 226 | spin_lock_init(&idio16gpio->lock); |
| 227 | |
| 228 | dev_set_drvdata(dev, idio16gpio); |
| 229 | |
Linus Walleij | d602ae9 | 2015-12-03 18:18:06 +0100 | [diff] [blame] | 230 | err = gpiochip_add_data(&idio16gpio->chip, idio16gpio); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 231 | if (err) { |
| 232 | dev_err(dev, "GPIO registering failed (%d)\n", err); |
| 233 | goto err_gpio_register; |
| 234 | } |
| 235 | |
William Breathitt Gray | fb50cdf | 2015-11-22 11:38:55 -0500 | [diff] [blame] | 236 | /* Disable IRQ by default */ |
| 237 | outb(0, base + 2); |
William Breathitt Gray | 12b61c9 | 2015-12-02 20:23:04 -0500 | [diff] [blame] | 238 | outb(0, base + 1); |
William Breathitt Gray | fb50cdf | 2015-11-22 11:38:55 -0500 | [diff] [blame] | 239 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 240 | err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0, |
| 241 | handle_edge_irq, IRQ_TYPE_NONE); |
| 242 | if (err) { |
| 243 | dev_err(dev, "Could not add irqchip (%d)\n", err); |
| 244 | goto err_gpiochip_irqchip_add; |
| 245 | } |
| 246 | |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 247 | err = request_irq(irq, idio_16_irq_handler, 0, name, idio16gpio); |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 248 | if (err) { |
| 249 | dev_err(dev, "IRQ handler registering failed (%d)\n", err); |
| 250 | goto err_request_irq; |
| 251 | } |
| 252 | |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 253 | return 0; |
| 254 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 255 | err_request_irq: |
| 256 | err_gpiochip_irqchip_add: |
| 257 | gpiochip_remove(&idio16gpio->chip); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 258 | err_gpio_register: |
William Breathitt Gray | 6e0171b | 2015-11-17 18:58:16 -0500 | [diff] [blame] | 259 | release_region(base, extent); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 260 | err_lock_io_port: |
| 261 | return err; |
| 262 | } |
| 263 | |
| 264 | static int idio_16_remove(struct platform_device *pdev) |
| 265 | { |
| 266 | struct idio_16_gpio *const idio16gpio = platform_get_drvdata(pdev); |
| 267 | |
William Breathitt Gray | a118414 | 2015-11-03 07:54:23 -0500 | [diff] [blame] | 268 | free_irq(idio16gpio->irq, idio16gpio); |
William Breathitt Gray | 1ceacea | 2015-10-19 12:59:14 -0400 | [diff] [blame] | 269 | gpiochip_remove(&idio16gpio->chip); |
| 270 | release_region(idio16gpio->base, idio16gpio->extent); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static struct platform_device *idio_16_device; |
| 276 | |
| 277 | static struct platform_driver idio_16_driver = { |
| 278 | .driver = { |
| 279 | .name = "104-idio-16" |
| 280 | }, |
| 281 | .remove = idio_16_remove |
| 282 | }; |
| 283 | |
| 284 | static void __exit idio_16_exit(void) |
| 285 | { |
| 286 | platform_device_unregister(idio_16_device); |
| 287 | platform_driver_unregister(&idio_16_driver); |
| 288 | } |
| 289 | |
| 290 | static int __init idio_16_init(void) |
| 291 | { |
| 292 | int err; |
| 293 | |
| 294 | idio_16_device = platform_device_alloc(idio_16_driver.driver.name, -1); |
| 295 | if (!idio_16_device) |
| 296 | return -ENOMEM; |
| 297 | |
| 298 | err = platform_device_add(idio_16_device); |
| 299 | if (err) |
| 300 | goto err_platform_device; |
| 301 | |
| 302 | err = platform_driver_probe(&idio_16_driver, idio_16_probe); |
| 303 | if (err) |
| 304 | goto err_platform_driver; |
| 305 | |
| 306 | return 0; |
| 307 | |
| 308 | err_platform_driver: |
| 309 | platform_device_del(idio_16_device); |
| 310 | err_platform_device: |
| 311 | platform_device_put(idio_16_device); |
| 312 | return err; |
| 313 | } |
| 314 | |
| 315 | module_init(idio_16_init); |
| 316 | module_exit(idio_16_exit); |
| 317 | |
| 318 | MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); |
| 319 | MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver"); |
| 320 | MODULE_LICENSE("GPL"); |