Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Diolan DLN-2 USB-GPIO adapter |
| 3 | * |
| 4 | * Copyright (c) 2014 Intel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/irqdomain.h> |
| 16 | #include <linux/irq.h> |
| 17 | #include <linux/irqchip/chained_irq.h> |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 18 | #include <linux/gpio/driver.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/mfd/dln2.h> |
| 21 | |
| 22 | #define DLN2_GPIO_ID 0x01 |
| 23 | |
| 24 | #define DLN2_GPIO_GET_PIN_COUNT DLN2_CMD(0x01, DLN2_GPIO_ID) |
| 25 | #define DLN2_GPIO_SET_DEBOUNCE DLN2_CMD(0x04, DLN2_GPIO_ID) |
| 26 | #define DLN2_GPIO_GET_DEBOUNCE DLN2_CMD(0x05, DLN2_GPIO_ID) |
| 27 | #define DLN2_GPIO_PORT_GET_VAL DLN2_CMD(0x06, DLN2_GPIO_ID) |
| 28 | #define DLN2_GPIO_PIN_GET_VAL DLN2_CMD(0x0B, DLN2_GPIO_ID) |
| 29 | #define DLN2_GPIO_PIN_SET_OUT_VAL DLN2_CMD(0x0C, DLN2_GPIO_ID) |
| 30 | #define DLN2_GPIO_PIN_GET_OUT_VAL DLN2_CMD(0x0D, DLN2_GPIO_ID) |
| 31 | #define DLN2_GPIO_CONDITION_MET_EV DLN2_CMD(0x0F, DLN2_GPIO_ID) |
| 32 | #define DLN2_GPIO_PIN_ENABLE DLN2_CMD(0x10, DLN2_GPIO_ID) |
| 33 | #define DLN2_GPIO_PIN_DISABLE DLN2_CMD(0x11, DLN2_GPIO_ID) |
| 34 | #define DLN2_GPIO_PIN_SET_DIRECTION DLN2_CMD(0x13, DLN2_GPIO_ID) |
| 35 | #define DLN2_GPIO_PIN_GET_DIRECTION DLN2_CMD(0x14, DLN2_GPIO_ID) |
| 36 | #define DLN2_GPIO_PIN_SET_EVENT_CFG DLN2_CMD(0x1E, DLN2_GPIO_ID) |
| 37 | #define DLN2_GPIO_PIN_GET_EVENT_CFG DLN2_CMD(0x1F, DLN2_GPIO_ID) |
| 38 | |
| 39 | #define DLN2_GPIO_EVENT_NONE 0 |
| 40 | #define DLN2_GPIO_EVENT_CHANGE 1 |
| 41 | #define DLN2_GPIO_EVENT_LVL_HIGH 2 |
| 42 | #define DLN2_GPIO_EVENT_LVL_LOW 3 |
| 43 | #define DLN2_GPIO_EVENT_CHANGE_RISING 0x11 |
| 44 | #define DLN2_GPIO_EVENT_CHANGE_FALLING 0x21 |
| 45 | #define DLN2_GPIO_EVENT_MASK 0x0F |
| 46 | |
| 47 | #define DLN2_GPIO_MAX_PINS 32 |
| 48 | |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 49 | struct dln2_gpio { |
| 50 | struct platform_device *pdev; |
| 51 | struct gpio_chip gpio; |
| 52 | |
| 53 | /* |
| 54 | * Cache pin direction to save us one transfer, since the hardware has |
| 55 | * separate commands to read the in and out values. |
| 56 | */ |
| 57 | DECLARE_BITMAP(output_enabled, DLN2_GPIO_MAX_PINS); |
| 58 | |
Octavian Purdila | 0acb0e7 | 2014-12-16 17:57:12 +0200 | [diff] [blame] | 59 | /* active IRQs - not synced to hardware */ |
| 60 | DECLARE_BITMAP(unmasked_irqs, DLN2_GPIO_MAX_PINS); |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 61 | /* active IRQS - synced to hardware */ |
| 62 | DECLARE_BITMAP(enabled_irqs, DLN2_GPIO_MAX_PINS); |
| 63 | int irq_type[DLN2_GPIO_MAX_PINS]; |
| 64 | struct mutex irq_lock; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct dln2_gpio_pin { |
| 68 | __le16 pin; |
| 69 | }; |
| 70 | |
| 71 | struct dln2_gpio_pin_val { |
| 72 | __le16 pin __packed; |
| 73 | u8 value; |
| 74 | }; |
| 75 | |
| 76 | static int dln2_gpio_get_pin_count(struct platform_device *pdev) |
| 77 | { |
| 78 | int ret; |
| 79 | __le16 count; |
| 80 | int len = sizeof(count); |
| 81 | |
| 82 | ret = dln2_transfer_rx(pdev, DLN2_GPIO_GET_PIN_COUNT, &count, &len); |
| 83 | if (ret < 0) |
| 84 | return ret; |
| 85 | if (len < sizeof(count)) |
| 86 | return -EPROTO; |
| 87 | |
| 88 | return le16_to_cpu(count); |
| 89 | } |
| 90 | |
| 91 | static int dln2_gpio_pin_cmd(struct dln2_gpio *dln2, int cmd, unsigned pin) |
| 92 | { |
| 93 | struct dln2_gpio_pin req = { |
| 94 | .pin = cpu_to_le16(pin), |
| 95 | }; |
| 96 | |
| 97 | return dln2_transfer_tx(dln2->pdev, cmd, &req, sizeof(req)); |
| 98 | } |
| 99 | |
| 100 | static int dln2_gpio_pin_val(struct dln2_gpio *dln2, int cmd, unsigned int pin) |
| 101 | { |
| 102 | int ret; |
| 103 | struct dln2_gpio_pin req = { |
| 104 | .pin = cpu_to_le16(pin), |
| 105 | }; |
| 106 | struct dln2_gpio_pin_val rsp; |
| 107 | int len = sizeof(rsp); |
| 108 | |
| 109 | ret = dln2_transfer(dln2->pdev, cmd, &req, sizeof(req), &rsp, &len); |
| 110 | if (ret < 0) |
| 111 | return ret; |
| 112 | if (len < sizeof(rsp) || req.pin != rsp.pin) |
| 113 | return -EPROTO; |
| 114 | |
| 115 | return rsp.value; |
| 116 | } |
| 117 | |
| 118 | static int dln2_gpio_pin_get_in_val(struct dln2_gpio *dln2, unsigned int pin) |
| 119 | { |
| 120 | int ret; |
| 121 | |
| 122 | ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_VAL, pin); |
| 123 | if (ret < 0) |
| 124 | return ret; |
| 125 | return !!ret; |
| 126 | } |
| 127 | |
| 128 | static int dln2_gpio_pin_get_out_val(struct dln2_gpio *dln2, unsigned int pin) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | ret = dln2_gpio_pin_val(dln2, DLN2_GPIO_PIN_GET_OUT_VAL, pin); |
| 133 | if (ret < 0) |
| 134 | return ret; |
| 135 | return !!ret; |
| 136 | } |
| 137 | |
Axel Lin | 5afb287 | 2014-12-17 17:47:14 +0800 | [diff] [blame] | 138 | static int dln2_gpio_pin_set_out_val(struct dln2_gpio *dln2, |
| 139 | unsigned int pin, int value) |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 140 | { |
| 141 | struct dln2_gpio_pin_val req = { |
| 142 | .pin = cpu_to_le16(pin), |
| 143 | .value = value, |
| 144 | }; |
| 145 | |
Axel Lin | 5afb287 | 2014-12-17 17:47:14 +0800 | [diff] [blame] | 146 | return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_OUT_VAL, &req, |
| 147 | sizeof(req)); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | #define DLN2_GPIO_DIRECTION_IN 0 |
| 151 | #define DLN2_GPIO_DIRECTION_OUT 1 |
| 152 | |
| 153 | static int dln2_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 154 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 155 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 156 | struct dln2_gpio_pin req = { |
| 157 | .pin = cpu_to_le16(offset), |
| 158 | }; |
| 159 | struct dln2_gpio_pin_val rsp; |
| 160 | int len = sizeof(rsp); |
| 161 | int ret; |
| 162 | |
| 163 | ret = dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_ENABLE, offset); |
| 164 | if (ret < 0) |
| 165 | return ret; |
| 166 | |
| 167 | /* cache the pin direction */ |
| 168 | ret = dln2_transfer(dln2->pdev, DLN2_GPIO_PIN_GET_DIRECTION, |
| 169 | &req, sizeof(req), &rsp, &len); |
| 170 | if (ret < 0) |
| 171 | return ret; |
| 172 | if (len < sizeof(rsp) || req.pin != rsp.pin) { |
| 173 | ret = -EPROTO; |
| 174 | goto out_disable; |
| 175 | } |
| 176 | |
| 177 | switch (rsp.value) { |
| 178 | case DLN2_GPIO_DIRECTION_IN: |
| 179 | clear_bit(offset, dln2->output_enabled); |
| 180 | return 0; |
| 181 | case DLN2_GPIO_DIRECTION_OUT: |
| 182 | set_bit(offset, dln2->output_enabled); |
| 183 | return 0; |
| 184 | default: |
| 185 | ret = -EPROTO; |
| 186 | goto out_disable; |
| 187 | } |
| 188 | |
| 189 | out_disable: |
| 190 | dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static void dln2_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 195 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 196 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 197 | |
| 198 | dln2_gpio_pin_cmd(dln2, DLN2_GPIO_PIN_DISABLE, offset); |
| 199 | } |
| 200 | |
| 201 | static int dln2_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
| 202 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 203 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 204 | |
| 205 | if (test_bit(offset, dln2->output_enabled)) |
Linus Walleij | 1cb6610 | 2018-01-24 13:55:22 +0100 | [diff] [blame] | 206 | return 0; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 207 | |
Linus Walleij | 1cb6610 | 2018-01-24 13:55:22 +0100 | [diff] [blame] | 208 | return 1; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static int dln2_gpio_get(struct gpio_chip *chip, unsigned int offset) |
| 212 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 213 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 214 | int dir; |
| 215 | |
| 216 | dir = dln2_gpio_get_direction(chip, offset); |
| 217 | if (dir < 0) |
| 218 | return dir; |
| 219 | |
Linus Walleij | 1cb6610 | 2018-01-24 13:55:22 +0100 | [diff] [blame] | 220 | if (dir == 1) |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 221 | return dln2_gpio_pin_get_in_val(dln2, offset); |
| 222 | |
| 223 | return dln2_gpio_pin_get_out_val(dln2, offset); |
| 224 | } |
| 225 | |
| 226 | static void dln2_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 227 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 228 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 229 | |
| 230 | dln2_gpio_pin_set_out_val(dln2, offset, value); |
| 231 | } |
| 232 | |
| 233 | static int dln2_gpio_set_direction(struct gpio_chip *chip, unsigned offset, |
| 234 | unsigned dir) |
| 235 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 236 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 237 | struct dln2_gpio_pin_val req = { |
| 238 | .pin = cpu_to_le16(offset), |
| 239 | .value = dir, |
| 240 | }; |
| 241 | int ret; |
| 242 | |
| 243 | ret = dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_DIRECTION, |
| 244 | &req, sizeof(req)); |
| 245 | if (ret < 0) |
| 246 | return ret; |
| 247 | |
| 248 | if (dir == DLN2_GPIO_DIRECTION_OUT) |
| 249 | set_bit(offset, dln2->output_enabled); |
| 250 | else |
| 251 | clear_bit(offset, dln2->output_enabled); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static int dln2_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 257 | { |
| 258 | return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_IN); |
| 259 | } |
| 260 | |
| 261 | static int dln2_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 262 | int value) |
| 263 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 264 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Axel Lin | 5afb287 | 2014-12-17 17:47:14 +0800 | [diff] [blame] | 265 | int ret; |
| 266 | |
| 267 | ret = dln2_gpio_pin_set_out_val(dln2, offset, value); |
| 268 | if (ret < 0) |
| 269 | return ret; |
| 270 | |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 271 | return dln2_gpio_set_direction(chip, offset, DLN2_GPIO_DIRECTION_OUT); |
| 272 | } |
| 273 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 274 | static int dln2_gpio_set_config(struct gpio_chip *chip, unsigned offset, |
| 275 | unsigned long config) |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 276 | { |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 277 | struct dln2_gpio *dln2 = gpiochip_get_data(chip); |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 278 | __le32 duration; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 279 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 280 | if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE) |
| 281 | return -ENOTSUPP; |
| 282 | |
| 283 | duration = cpu_to_le32(pinconf_to_config_argument(config)); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 284 | return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_SET_DEBOUNCE, |
| 285 | &duration, sizeof(duration)); |
| 286 | } |
| 287 | |
| 288 | static int dln2_gpio_set_event_cfg(struct dln2_gpio *dln2, unsigned pin, |
| 289 | unsigned type, unsigned period) |
| 290 | { |
| 291 | struct { |
| 292 | __le16 pin; |
| 293 | u8 type; |
| 294 | __le16 period; |
| 295 | } __packed req = { |
| 296 | .pin = cpu_to_le16(pin), |
| 297 | .type = type, |
| 298 | .period = cpu_to_le16(period), |
| 299 | }; |
| 300 | |
| 301 | return dln2_transfer_tx(dln2->pdev, DLN2_GPIO_PIN_SET_EVENT_CFG, |
| 302 | &req, sizeof(req)); |
| 303 | } |
| 304 | |
Octavian Purdila | 0acb0e7 | 2014-12-16 17:57:12 +0200 | [diff] [blame] | 305 | static void dln2_irq_unmask(struct irq_data *irqd) |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 306 | { |
| 307 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 308 | struct dln2_gpio *dln2 = gpiochip_get_data(gc); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 309 | int pin = irqd_to_hwirq(irqd); |
| 310 | |
Octavian Purdila | 0acb0e7 | 2014-12-16 17:57:12 +0200 | [diff] [blame] | 311 | set_bit(pin, dln2->unmasked_irqs); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static void dln2_irq_mask(struct irq_data *irqd) |
| 315 | { |
| 316 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 317 | struct dln2_gpio *dln2 = gpiochip_get_data(gc); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 318 | int pin = irqd_to_hwirq(irqd); |
| 319 | |
Octavian Purdila | 0acb0e7 | 2014-12-16 17:57:12 +0200 | [diff] [blame] | 320 | clear_bit(pin, dln2->unmasked_irqs); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static int dln2_irq_set_type(struct irq_data *irqd, unsigned type) |
| 324 | { |
| 325 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 326 | struct dln2_gpio *dln2 = gpiochip_get_data(gc); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 327 | int pin = irqd_to_hwirq(irqd); |
| 328 | |
| 329 | switch (type) { |
| 330 | case IRQ_TYPE_LEVEL_HIGH: |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 331 | dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_HIGH; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 332 | break; |
| 333 | case IRQ_TYPE_LEVEL_LOW: |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 334 | dln2->irq_type[pin] = DLN2_GPIO_EVENT_LVL_LOW; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 335 | break; |
| 336 | case IRQ_TYPE_EDGE_BOTH: |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 337 | dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 338 | break; |
| 339 | case IRQ_TYPE_EDGE_RISING: |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 340 | dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_RISING; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 341 | break; |
| 342 | case IRQ_TYPE_EDGE_FALLING: |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 343 | dln2->irq_type[pin] = DLN2_GPIO_EVENT_CHANGE_FALLING; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 344 | break; |
| 345 | default: |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 352 | static void dln2_irq_bus_lock(struct irq_data *irqd) |
| 353 | { |
| 354 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 355 | struct dln2_gpio *dln2 = gpiochip_get_data(gc); |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 356 | |
| 357 | mutex_lock(&dln2->irq_lock); |
| 358 | } |
| 359 | |
| 360 | static void dln2_irq_bus_unlock(struct irq_data *irqd) |
| 361 | { |
| 362 | struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd); |
Linus Walleij | 1880657 | 2015-12-06 00:32:16 +0100 | [diff] [blame] | 363 | struct dln2_gpio *dln2 = gpiochip_get_data(gc); |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 364 | int pin = irqd_to_hwirq(irqd); |
| 365 | int enabled, unmasked; |
| 366 | unsigned type; |
| 367 | int ret; |
| 368 | |
| 369 | enabled = test_bit(pin, dln2->enabled_irqs); |
| 370 | unmasked = test_bit(pin, dln2->unmasked_irqs); |
| 371 | |
| 372 | if (enabled != unmasked) { |
| 373 | if (unmasked) { |
| 374 | type = dln2->irq_type[pin] & DLN2_GPIO_EVENT_MASK; |
| 375 | set_bit(pin, dln2->enabled_irqs); |
| 376 | } else { |
| 377 | type = DLN2_GPIO_EVENT_NONE; |
| 378 | clear_bit(pin, dln2->enabled_irqs); |
| 379 | } |
| 380 | |
| 381 | ret = dln2_gpio_set_event_cfg(dln2, pin, type, 0); |
| 382 | if (ret) |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 383 | dev_err(dln2->gpio.parent, "failed to set event\n"); |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | mutex_unlock(&dln2->irq_lock); |
| 387 | } |
| 388 | |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 389 | static struct irq_chip dln2_gpio_irqchip = { |
| 390 | .name = "dln2-irq", |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 391 | .irq_mask = dln2_irq_mask, |
| 392 | .irq_unmask = dln2_irq_unmask, |
| 393 | .irq_set_type = dln2_irq_set_type, |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 394 | .irq_bus_lock = dln2_irq_bus_lock, |
| 395 | .irq_bus_sync_unlock = dln2_irq_bus_unlock, |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | static void dln2_gpio_event(struct platform_device *pdev, u16 echo, |
| 399 | const void *data, int len) |
| 400 | { |
| 401 | int pin, irq; |
Mohammad Jamal | 1fbb29c | 2015-01-13 20:39:38 +0530 | [diff] [blame] | 402 | |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 403 | const struct { |
| 404 | __le16 count; |
| 405 | __u8 type; |
| 406 | __le16 pin; |
| 407 | __u8 value; |
| 408 | } __packed *event = data; |
| 409 | struct dln2_gpio *dln2 = platform_get_drvdata(pdev); |
| 410 | |
| 411 | if (len < sizeof(*event)) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 412 | dev_err(dln2->gpio.parent, "short event message\n"); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | pin = le16_to_cpu(event->pin); |
| 417 | if (pin >= dln2->gpio.ngpio) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 418 | dev_err(dln2->gpio.parent, "out of bounds pin %d\n", pin); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 422 | irq = irq_find_mapping(dln2->gpio.irq.domain, pin); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 423 | if (!irq) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 424 | dev_err(dln2->gpio.parent, "pin %d not mapped to IRQ\n", pin); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 428 | switch (dln2->irq_type[pin]) { |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 429 | case DLN2_GPIO_EVENT_CHANGE_RISING: |
| 430 | if (event->value) |
| 431 | generic_handle_irq(irq); |
| 432 | break; |
| 433 | case DLN2_GPIO_EVENT_CHANGE_FALLING: |
| 434 | if (!event->value) |
| 435 | generic_handle_irq(irq); |
| 436 | break; |
| 437 | default: |
| 438 | generic_handle_irq(irq); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static int dln2_gpio_probe(struct platform_device *pdev) |
| 443 | { |
| 444 | struct dln2_gpio *dln2; |
| 445 | struct device *dev = &pdev->dev; |
| 446 | int pins; |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 447 | int ret; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 448 | |
| 449 | pins = dln2_gpio_get_pin_count(pdev); |
| 450 | if (pins < 0) { |
| 451 | dev_err(dev, "failed to get pin count: %d\n", pins); |
| 452 | return pins; |
| 453 | } |
| 454 | if (pins > DLN2_GPIO_MAX_PINS) { |
| 455 | pins = DLN2_GPIO_MAX_PINS; |
| 456 | dev_warn(dev, "clamping pins to %d\n", DLN2_GPIO_MAX_PINS); |
| 457 | } |
| 458 | |
| 459 | dln2 = devm_kzalloc(&pdev->dev, sizeof(*dln2), GFP_KERNEL); |
| 460 | if (!dln2) |
| 461 | return -ENOMEM; |
| 462 | |
Octavian Purdila | 96b932b | 2014-12-11 15:02:30 +0200 | [diff] [blame] | 463 | mutex_init(&dln2->irq_lock); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 464 | |
| 465 | dln2->pdev = pdev; |
| 466 | |
| 467 | dln2->gpio.label = "dln2"; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 468 | dln2->gpio.parent = dev; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 469 | dln2->gpio.owner = THIS_MODULE; |
| 470 | dln2->gpio.base = -1; |
| 471 | dln2->gpio.ngpio = pins; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 472 | dln2->gpio.can_sleep = true; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 473 | dln2->gpio.set = dln2_gpio_set; |
| 474 | dln2->gpio.get = dln2_gpio_get; |
| 475 | dln2->gpio.request = dln2_gpio_request; |
| 476 | dln2->gpio.free = dln2_gpio_free; |
| 477 | dln2->gpio.get_direction = dln2_gpio_get_direction; |
| 478 | dln2->gpio.direction_input = dln2_gpio_direction_input; |
| 479 | dln2->gpio.direction_output = dln2_gpio_direction_output; |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 480 | dln2->gpio.set_config = dln2_gpio_set_config; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 481 | |
| 482 | platform_set_drvdata(pdev, dln2); |
| 483 | |
Laxman Dewangan | 1ab79a6 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 484 | ret = devm_gpiochip_add_data(dev, &dln2->gpio, dln2); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 485 | if (ret < 0) { |
| 486 | dev_err(dev, "failed to add gpio chip: %d\n", ret); |
Laxman Dewangan | 1ab79a6 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 487 | return ret; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | ret = gpiochip_irqchip_add(&dln2->gpio, &dln2_gpio_irqchip, 0, |
| 491 | handle_simple_irq, IRQ_TYPE_NONE); |
| 492 | if (ret < 0) { |
| 493 | dev_err(dev, "failed to add irq chip: %d\n", ret); |
Laxman Dewangan | 1ab79a6 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 494 | return ret; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | ret = dln2_register_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV, |
| 498 | dln2_gpio_event); |
| 499 | if (ret) { |
| 500 | dev_err(dev, "failed to register event cb: %d\n", ret); |
Laxman Dewangan | 1ab79a6 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 501 | return ret; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | return 0; |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static int dln2_gpio_remove(struct platform_device *pdev) |
| 508 | { |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 509 | dln2_unregister_event_cb(pdev, DLN2_GPIO_CONDITION_MET_EV); |
Daniel Baluta | 6732127 | 2014-11-06 15:48:05 +0200 | [diff] [blame] | 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static struct platform_driver dln2_gpio_driver = { |
| 515 | .driver.name = "dln2-gpio", |
| 516 | .probe = dln2_gpio_probe, |
| 517 | .remove = dln2_gpio_remove, |
| 518 | }; |
| 519 | |
| 520 | module_platform_driver(dln2_gpio_driver); |
| 521 | |
| 522 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com"); |
| 523 | MODULE_DESCRIPTION("Driver for the Diolan DLN2 GPIO interface"); |
| 524 | MODULE_LICENSE("GPL v2"); |
| 525 | MODULE_ALIAS("platform:dln2-gpio"); |