Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GPIO Greybus driver. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
Alex Elder | a46e967 | 2014-12-12 12:08:42 -0600 | [diff] [blame] | 5 | * Copyright 2014 Linaro Ltd. |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/gpio.h> |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 14 | #include <linux/irq.h> |
| 15 | #include <linux/irqdomain.h> |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 16 | #include <linux/mutex.h> |
| 17 | |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 18 | #include "greybus.h" |
| 19 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 20 | struct gb_gpio_line { |
| 21 | /* The following has to be an array of line_max entries */ |
| 22 | /* --> make them just a flags field */ |
| 23 | u8 active: 1, |
| 24 | direction: 1, /* 0 = output, 1 = input */ |
| 25 | value: 1; /* 0 = low, 1 = high */ |
| 26 | u16 debounce_usec; |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 27 | |
| 28 | u8 irq_type; |
| 29 | bool irq_type_pending; |
| 30 | bool masked; |
| 31 | bool masked_pending; |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 34 | struct gb_gpio_controller { |
| 35 | struct gb_connection *connection; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 36 | u8 line_max; /* max line number */ |
| 37 | struct gb_gpio_line *lines; |
| 38 | |
| 39 | struct gpio_chip chip; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 40 | struct irq_chip irqc; |
| 41 | struct irq_chip *irqchip; |
| 42 | struct irq_domain *irqdomain; |
| 43 | unsigned int irq_base; |
| 44 | irq_flow_handler_t irq_handler; |
| 45 | unsigned int irq_default_type; |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 46 | struct mutex irq_lock; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 47 | }; |
| 48 | #define gpio_chip_to_gb_gpio_controller(chip) \ |
| 49 | container_of(chip, struct gb_gpio_controller, chip) |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 50 | #define irq_data_to_gpio_chip(d) (d->domain->host_data) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 51 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 52 | static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc) |
| 53 | { |
| 54 | struct gb_gpio_line_count_response response; |
| 55 | int ret; |
| 56 | |
| 57 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT, |
| 58 | NULL, 0, &response, sizeof(response)); |
| 59 | if (!ret) |
| 60 | ggc->line_max = response.count; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 61 | return ret; |
| 62 | } |
| 63 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 64 | static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 65 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 66 | struct gb_gpio_activate_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 67 | int ret; |
| 68 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 69 | request.which = which; |
| 70 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE, |
| 71 | &request, sizeof(request), NULL, 0); |
| 72 | if (!ret) |
| 73 | ggc->lines[which].active = true; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 74 | return ret; |
| 75 | } |
| 76 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 77 | static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 78 | u8 which) |
| 79 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 80 | struct gb_gpio_deactivate_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 81 | int ret; |
| 82 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 83 | request.which = which; |
| 84 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE, |
| 85 | &request, sizeof(request), NULL, 0); |
Johan Hovold | 792c17e | 2015-03-19 16:55:23 +0100 | [diff] [blame] | 86 | if (ret) { |
| 87 | dev_err(ggc->chip.dev, "failed to deactivate gpio %u\n", |
| 88 | which); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | ggc->lines[which].active = false; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 95 | static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 96 | u8 which) |
| 97 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 98 | struct gb_gpio_get_direction_request request; |
| 99 | struct gb_gpio_get_direction_response response; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 100 | int ret; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 101 | u8 direction; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 102 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 103 | request.which = which; |
| 104 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION, |
| 105 | &request, sizeof(request), |
| 106 | &response, sizeof(response)); |
| 107 | if (ret) |
| 108 | return ret; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 109 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 110 | direction = response.direction; |
Johan Hovold | e5032d8 | 2015-03-19 16:51:16 +0100 | [diff] [blame] | 111 | if (direction && direction != 1) { |
| 112 | dev_warn(ggc->chip.dev, |
| 113 | "gpio %u direction was %u (should be 0 or 1)\n", |
| 114 | which, direction); |
| 115 | } |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 116 | ggc->lines[which].direction = direction ? 1 : 0; |
| 117 | return 0; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 120 | static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 121 | u8 which) |
| 122 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 123 | struct gb_gpio_direction_in_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 124 | int ret; |
| 125 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 126 | request.which = which; |
| 127 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN, |
| 128 | &request, sizeof(request), NULL, 0); |
| 129 | if (!ret) |
| 130 | ggc->lines[which].direction = 1; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 131 | return ret; |
| 132 | } |
| 133 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 134 | static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 135 | u8 which, bool value_high) |
| 136 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 137 | struct gb_gpio_direction_out_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 138 | int ret; |
| 139 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 140 | request.which = which; |
| 141 | request.value = value_high ? 1 : 0; |
| 142 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT, |
| 143 | &request, sizeof(request), NULL, 0); |
| 144 | if (!ret) |
| 145 | ggc->lines[which].direction = 0; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 149 | static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 150 | u8 which) |
| 151 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 152 | struct gb_gpio_get_value_request request; |
| 153 | struct gb_gpio_get_value_response response; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 154 | int ret; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 155 | u8 value; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 156 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 157 | request.which = which; |
| 158 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE, |
| 159 | &request, sizeof(request), |
| 160 | &response, sizeof(response)); |
Johan Hovold | 792c17e | 2015-03-19 16:55:23 +0100 | [diff] [blame] | 161 | if (ret) { |
| 162 | dev_err(ggc->chip.dev, "failed to get value of gpio %u\n", |
| 163 | which); |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 164 | return ret; |
Johan Hovold | 792c17e | 2015-03-19 16:55:23 +0100 | [diff] [blame] | 165 | } |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 166 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 167 | value = response.value; |
Johan Hovold | e5032d8 | 2015-03-19 16:51:16 +0100 | [diff] [blame] | 168 | if (value && value != 1) { |
| 169 | dev_warn(ggc->chip.dev, |
| 170 | "gpio %u value was %u (should be 0 or 1)\n", |
| 171 | which, value); |
| 172 | } |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 173 | ggc->lines[which].value = value ? 1 : 0; |
| 174 | return 0; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 177 | static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 178 | u8 which, bool value_high) |
| 179 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 180 | struct gb_gpio_set_value_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 181 | int ret; |
| 182 | |
Johan Hovold | 7bfa078 | 2015-03-19 16:55:22 +0100 | [diff] [blame] | 183 | if (ggc->lines[which].direction == 1) { |
| 184 | dev_warn(ggc->chip.dev, |
| 185 | "refusing to set value of input gpio %u\n", which); |
| 186 | return; |
| 187 | } |
| 188 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 189 | request.which = which; |
| 190 | request.value = value_high ? 1 : 0; |
| 191 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE, |
| 192 | &request, sizeof(request), NULL, 0); |
Johan Hovold | 792c17e | 2015-03-19 16:55:23 +0100 | [diff] [blame] | 193 | if (ret) { |
| 194 | dev_err(ggc->chip.dev, "failed to set value of gpio %u\n", |
| 195 | which); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | ggc->lines[which].value = request.value; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 202 | static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 203 | u8 which, u16 debounce_usec) |
| 204 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 205 | struct gb_gpio_set_debounce_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 206 | int ret; |
| 207 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 208 | request.which = which; |
| 209 | request.usec = cpu_to_le16(debounce_usec); |
| 210 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE, |
| 211 | &request, sizeof(request), NULL, 0); |
| 212 | if (!ret) |
| 213 | ggc->lines[which].debounce_usec = debounce_usec; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 214 | return ret; |
| 215 | } |
| 216 | |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 217 | static void _gb_gpio_irq_mask(struct gb_gpio_controller *ggc, u8 hwirq) |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 218 | { |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 219 | struct gb_gpio_irq_mask_request request; |
| 220 | int ret; |
| 221 | |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 222 | request.which = hwirq; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 223 | ret = gb_operation_sync(ggc->connection, |
| 224 | GB_GPIO_TYPE_IRQ_MASK, |
| 225 | &request, sizeof(request), NULL, 0); |
| 226 | if (ret) |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 227 | dev_err(ggc->chip.dev, "failed to mask irq: %d\n", ret); |
| 228 | } |
| 229 | |
| 230 | static void _gb_gpio_irq_unmask(struct gb_gpio_controller *ggc, u8 hwirq) |
| 231 | { |
| 232 | struct gb_gpio_irq_unmask_request request; |
| 233 | int ret; |
| 234 | |
| 235 | request.which = hwirq; |
| 236 | ret = gb_operation_sync(ggc->connection, |
| 237 | GB_GPIO_TYPE_IRQ_UNMASK, |
| 238 | &request, sizeof(request), NULL, 0); |
| 239 | if (ret) |
| 240 | dev_err(ggc->chip.dev, "failed to unmask irq: %d\n", ret); |
| 241 | } |
| 242 | |
| 243 | static void _gb_gpio_irq_set_type(struct gb_gpio_controller *ggc, |
| 244 | u8 hwirq, u8 type) |
| 245 | { |
| 246 | struct gb_gpio_irq_type_request request; |
| 247 | int ret; |
| 248 | |
| 249 | request.which = hwirq; |
| 250 | request.type = type; |
| 251 | |
| 252 | ret = gb_operation_sync(ggc->connection, |
| 253 | GB_GPIO_TYPE_IRQ_TYPE, |
| 254 | &request, sizeof(request), NULL, 0); |
| 255 | if (ret) |
| 256 | dev_err(ggc->chip.dev, "failed to set irq type: %d\n", ret); |
| 257 | } |
| 258 | |
| 259 | static void gb_gpio_irq_mask(struct irq_data *d) |
| 260 | { |
| 261 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 262 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 263 | struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
| 264 | |
| 265 | line->masked = true; |
| 266 | line->masked_pending = true; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Johan Hovold | 0cb918d | 2015-05-26 15:29:23 +0200 | [diff] [blame] | 269 | static void gb_gpio_irq_unmask(struct irq_data *d) |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 270 | { |
| 271 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 272 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 273 | struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 274 | |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 275 | line->masked = false; |
| 276 | line->masked_pending = true; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 280 | { |
| 281 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 282 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 283 | struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 284 | u8 irq_type; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 285 | |
| 286 | switch (type) { |
| 287 | case IRQ_TYPE_NONE: |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 288 | irq_type = GB_GPIO_IRQ_TYPE_NONE; |
| 289 | break; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 290 | case IRQ_TYPE_EDGE_RISING: |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 291 | irq_type = GB_GPIO_IRQ_TYPE_EDGE_RISING; |
| 292 | break; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 293 | case IRQ_TYPE_EDGE_FALLING: |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 294 | irq_type = GB_GPIO_IRQ_TYPE_EDGE_FALLING; |
| 295 | break; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 296 | case IRQ_TYPE_EDGE_BOTH: |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 297 | irq_type = GB_GPIO_IRQ_TYPE_EDGE_BOTH; |
| 298 | break; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 299 | case IRQ_TYPE_LEVEL_LOW: |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 300 | irq_type = GB_GPIO_IRQ_TYPE_LEVEL_LOW; |
| 301 | break; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 302 | case IRQ_TYPE_LEVEL_HIGH: |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 303 | irq_type = GB_GPIO_IRQ_TYPE_LEVEL_HIGH; |
Viresh Kumar | 8de925b | 2015-05-20 16:32:28 +0530 | [diff] [blame] | 304 | break; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 305 | default: |
Johan Hovold | e5032d8 | 2015-03-19 16:51:16 +0100 | [diff] [blame] | 306 | dev_err(chip->dev, "unsupported irq type: %u\n", type); |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 307 | return -EINVAL; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Johan Hovold | 7ba864a | 2015-05-28 19:03:34 +0200 | [diff] [blame] | 310 | line->irq_type = irq_type; |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 311 | line->irq_type_pending = true; |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static void gb_gpio_irq_bus_lock(struct irq_data *d) |
| 317 | { |
| 318 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 319 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 320 | |
| 321 | mutex_lock(&ggc->irq_lock); |
| 322 | } |
| 323 | |
| 324 | static void gb_gpio_irq_bus_sync_unlock(struct irq_data *d) |
| 325 | { |
| 326 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 327 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 328 | struct gb_gpio_line *line = &ggc->lines[d->hwirq]; |
| 329 | |
| 330 | if (line->irq_type_pending) { |
| 331 | _gb_gpio_irq_set_type(ggc, d->hwirq, line->irq_type); |
| 332 | line->irq_type_pending = false; |
| 333 | } |
| 334 | |
| 335 | if (line->masked_pending) { |
| 336 | if (line->masked) |
| 337 | _gb_gpio_irq_mask(ggc, d->hwirq); |
| 338 | else |
| 339 | _gb_gpio_irq_unmask(ggc, d->hwirq); |
| 340 | line->masked_pending = false; |
| 341 | } |
| 342 | |
| 343 | mutex_unlock(&ggc->irq_lock); |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 344 | } |
| 345 | |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 346 | static int gb_gpio_request_recv(u8 type, struct gb_operation *op) |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 347 | { |
Johan Hovold | e5032d8 | 2015-03-19 16:51:16 +0100 | [diff] [blame] | 348 | struct gb_connection *connection = op->connection; |
Johan Hovold | fef96a2 | 2015-03-27 12:45:43 +0100 | [diff] [blame] | 349 | struct gb_gpio_controller *ggc = connection->private; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 350 | struct gb_message *request; |
| 351 | struct gb_gpio_irq_event_request *event; |
| 352 | int irq; |
| 353 | struct irq_desc *desc; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 354 | |
| 355 | if (type != GB_GPIO_TYPE_IRQ_EVENT) { |
Greg Kroah-Hartman | c7eb46e | 2015-10-14 11:17:55 -0700 | [diff] [blame] | 356 | dev_err(&connection->bundle->dev, |
Johan Hovold | e5032d8 | 2015-03-19 16:51:16 +0100 | [diff] [blame] | 357 | "unsupported unsolicited request: %u\n", type); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 358 | return -EINVAL; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 361 | request = op->request; |
Johan Hovold | 1842dd8 | 2015-03-27 12:45:41 +0100 | [diff] [blame] | 362 | |
| 363 | if (request->payload_size < sizeof(*event)) { |
Viresh Kumar | 782c3b7 | 2015-08-06 12:44:51 +0530 | [diff] [blame] | 364 | dev_err(ggc->chip.dev, "short event received (%zu < %zu)\n", |
| 365 | request->payload_size, sizeof(*event)); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 366 | return -EINVAL; |
Johan Hovold | 1842dd8 | 2015-03-27 12:45:41 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 369 | event = request->payload; |
| 370 | if (event->which > ggc->line_max) { |
Johan Hovold | e5032d8 | 2015-03-19 16:51:16 +0100 | [diff] [blame] | 371 | dev_err(ggc->chip.dev, "invalid hw irq: %d\n", event->which); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 372 | return -EINVAL; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 373 | } |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 374 | |
Johan Hovold | ec76211 | 2015-05-26 15:29:22 +0200 | [diff] [blame] | 375 | irq = irq_find_mapping(ggc->irqdomain, event->which); |
| 376 | if (!irq) { |
| 377 | dev_err(ggc->chip.dev, "failed to find IRQ\n"); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 378 | return -EINVAL; |
Johan Hovold | 244b5a2 | 2015-03-27 12:45:42 +0100 | [diff] [blame] | 379 | } |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 380 | desc = irq_to_desc(irq); |
Johan Hovold | 244b5a2 | 2015-03-27 12:45:42 +0100 | [diff] [blame] | 381 | if (!desc) { |
| 382 | dev_err(ggc->chip.dev, "failed to look up irq\n"); |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 383 | return -EINVAL; |
Johan Hovold | 244b5a2 | 2015-03-27 12:45:42 +0100 | [diff] [blame] | 384 | } |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 385 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 386 | local_irq_disable(); |
Rui Miguel Silva | 3c9426a | 2015-09-30 11:11:57 +0100 | [diff] [blame] | 387 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) |
Johan Hovold | 2611ebe | 2015-05-26 15:29:21 +0200 | [diff] [blame] | 388 | generic_handle_irq_desc(irq, desc); |
Greg Kroah-Hartman | 4ee1441 | 2015-09-29 20:39:17 +0200 | [diff] [blame] | 389 | #else |
| 390 | generic_handle_irq_desc(desc); |
| 391 | #endif |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 392 | local_irq_enable(); |
| 393 | |
Johan Hovold | 973ccfd | 2015-03-27 12:45:49 +0100 | [diff] [blame] | 394 | return 0; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 397 | static int gb_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 398 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 399 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 400 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 401 | return gb_gpio_activate_operation(ggc, (u8)offset); |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 402 | } |
| 403 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 404 | static void gb_gpio_free(struct gpio_chip *chip, unsigned offset) |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 405 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 406 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 407 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 408 | gb_gpio_deactivate_operation(ggc, (u8)offset); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
| 412 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 413 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 414 | u8 which; |
| 415 | int ret; |
| 416 | |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 417 | which = (u8)offset; |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 418 | ret = gb_gpio_get_direction_operation(ggc, which); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 419 | if (ret) |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 420 | return ret; |
| 421 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 422 | return ggc->lines[which].direction ? 1 : 0; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 426 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 427 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 428 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 429 | return gb_gpio_direction_in_operation(ggc, (u8)offset); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 433 | int value) |
| 434 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 435 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 436 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 437 | return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static int gb_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 441 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 442 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 443 | u8 which; |
| 444 | int ret; |
| 445 | |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 446 | which = (u8)offset; |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 447 | ret = gb_gpio_get_value_operation(ggc, which); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 448 | if (ret) |
| 449 | return ret; |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 450 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 451 | return ggc->lines[which].value; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 455 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 456 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 457 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 458 | gb_gpio_set_value_operation(ggc, (u8)offset, !!value); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset, |
| 462 | unsigned debounce) |
| 463 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 464 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 465 | u16 usec; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 466 | |
Johan Hovold | c2a6610 | 2015-03-19 16:51:09 +0100 | [diff] [blame] | 467 | if (debounce > U16_MAX) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 468 | return -EINVAL; |
Johan Hovold | c2a6610 | 2015-03-19 16:51:09 +0100 | [diff] [blame] | 469 | usec = (u16)debounce; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 470 | |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 471 | return gb_gpio_set_debounce_operation(ggc, (u8)offset, usec); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 474 | static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 475 | { |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 476 | int ret; |
| 477 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 478 | /* Now find out how many lines there are */ |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 479 | ret = gb_gpio_line_count_operation(ggc); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 480 | if (ret) |
Johan Hovold | 86c6816 | 2015-03-19 16:51:15 +0100 | [diff] [blame] | 481 | return ret; |
| 482 | |
Johan Hovold | 64d2f4e | 2015-03-19 16:51:17 +0100 | [diff] [blame] | 483 | ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines), |
| 484 | GFP_KERNEL); |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 485 | if (!ggc->lines) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 486 | return -ENOMEM; |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 491 | /** |
| 492 | * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip |
| 493 | * @d: the irqdomain used by this irqchip |
| 494 | * @irq: the global irq number used by this GB gpio irqchip irq |
| 495 | * @hwirq: the local IRQ/GPIO line offset on this GB gpio |
| 496 | * |
| 497 | * This function will set up the mapping for a certain IRQ line on a |
| 498 | * GB gpio by assigning the GB gpio as chip data, and using the irqchip |
| 499 | * stored inside the GB gpio. |
| 500 | */ |
| 501 | static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq, |
| 502 | irq_hw_number_t hwirq) |
| 503 | { |
| 504 | struct gpio_chip *chip = domain->host_data; |
| 505 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 506 | |
| 507 | irq_set_chip_data(irq, ggc); |
| 508 | irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler); |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 509 | irq_set_noprobe(irq); |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 510 | /* |
| 511 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
| 512 | * is passed as default type. |
| 513 | */ |
| 514 | if (ggc->irq_default_type != IRQ_TYPE_NONE) |
| 515 | irq_set_irq_type(irq, ggc->irq_default_type); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq) |
| 521 | { |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 522 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 523 | irq_set_chip_data(irq, NULL); |
| 524 | } |
| 525 | |
| 526 | static const struct irq_domain_ops gb_gpio_domain_ops = { |
| 527 | .map = gb_gpio_irq_map, |
| 528 | .unmap = gb_gpio_irq_unmap, |
| 529 | }; |
| 530 | |
| 531 | /** |
| 532 | * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller |
| 533 | * @ggc: the gb_gpio_controller to remove the irqchip from |
| 534 | * |
| 535 | * This is called only from gb_gpio_remove() |
| 536 | */ |
| 537 | static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc) |
| 538 | { |
| 539 | unsigned int offset; |
| 540 | |
| 541 | /* Remove all IRQ mappings and delete the domain */ |
| 542 | if (ggc->irqdomain) { |
| 543 | for (offset = 0; offset < (ggc->line_max + 1); offset++) |
| 544 | irq_dispose_mapping(irq_find_mapping(ggc->irqdomain, offset)); |
| 545 | irq_domain_remove(ggc->irqdomain); |
| 546 | } |
| 547 | |
| 548 | if (ggc->irqchip) { |
| 549 | ggc->irqchip = NULL; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | |
| 554 | /** |
| 555 | * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip |
| 556 | * @chip: the gpio chip to add the irqchip to |
| 557 | * @irqchip: the irqchip to add to the adapter |
| 558 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
| 559 | * allocate gpio irqs from |
| 560 | * @handler: the irq handler to use (often a predefined irq core function) |
| 561 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
| 562 | * to have the core avoid setting up any default type in the hardware. |
| 563 | * |
| 564 | * This function closely associates a certain irqchip with a certain |
| 565 | * gpio chip, providing an irq domain to translate the local IRQs to |
| 566 | * global irqs, and making sure that the gpio chip |
| 567 | * is passed as chip data to all related functions. Driver callbacks |
| 568 | * need to use container_of() to get their local state containers back |
| 569 | * from the gpio chip passed as chip data. An irqdomain will be stored |
| 570 | * in the gpio chip that shall be used by the driver to handle IRQ number |
| 571 | * translation. The gpio chip will need to be initialized and registered |
| 572 | * before calling this function. |
| 573 | */ |
| 574 | static int gb_gpio_irqchip_add(struct gpio_chip *chip, |
| 575 | struct irq_chip *irqchip, |
| 576 | unsigned int first_irq, |
| 577 | irq_flow_handler_t handler, |
| 578 | unsigned int type) |
| 579 | { |
| 580 | struct gb_gpio_controller *ggc; |
| 581 | unsigned int offset; |
| 582 | unsigned irq_base; |
| 583 | |
| 584 | if (!chip || !irqchip) |
| 585 | return -EINVAL; |
| 586 | |
| 587 | ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 588 | |
| 589 | ggc->irqchip = irqchip; |
| 590 | ggc->irq_handler = handler; |
| 591 | ggc->irq_default_type = type; |
| 592 | ggc->irqdomain = irq_domain_add_simple(NULL, |
| 593 | ggc->line_max + 1, first_irq, |
| 594 | &gb_gpio_domain_ops, chip); |
| 595 | if (!ggc->irqdomain) { |
| 596 | ggc->irqchip = NULL; |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Prepare the mapping since the irqchip shall be orthogonal to |
| 602 | * any gpio calls. If the first_irq was zero, this is |
| 603 | * necessary to allocate descriptors for all IRQs. |
| 604 | */ |
| 605 | for (offset = 0; offset < (ggc->line_max + 1); offset++) { |
| 606 | irq_base = irq_create_mapping(ggc->irqdomain, offset); |
| 607 | if (offset == 0) |
| 608 | ggc->irq_base = irq_base; |
| 609 | } |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 615 | { |
| 616 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 617 | |
| 618 | return irq_find_mapping(ggc->irqdomain, offset); |
| 619 | } |
| 620 | |
Alex Elder | 3689f97 | 2014-10-27 06:04:30 -0500 | [diff] [blame] | 621 | static int gb_gpio_connection_init(struct gb_connection *connection) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 622 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 623 | struct gb_gpio_controller *ggc; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 624 | struct gpio_chip *gpio; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 625 | struct irq_chip *irqc; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 626 | int ret; |
| 627 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 628 | ggc = kzalloc(sizeof(*ggc), GFP_KERNEL); |
| 629 | if (!ggc) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 630 | return -ENOMEM; |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 631 | ggc->connection = connection; |
| 632 | connection->private = ggc; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 633 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 634 | ret = gb_gpio_controller_setup(ggc); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 635 | if (ret) |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 636 | goto err_free_controller; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 637 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 638 | irqc = &ggc->irqc; |
Johan Hovold | 0cb918d | 2015-05-26 15:29:23 +0200 | [diff] [blame] | 639 | irqc->irq_mask = gb_gpio_irq_mask; |
| 640 | irqc->irq_unmask = gb_gpio_irq_unmask; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 641 | irqc->irq_set_type = gb_gpio_irq_set_type; |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 642 | irqc->irq_bus_lock = gb_gpio_irq_bus_lock; |
| 643 | irqc->irq_bus_sync_unlock = gb_gpio_irq_bus_sync_unlock; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 644 | irqc->name = "greybus_gpio"; |
| 645 | |
Johan Hovold | 25f11ed | 2015-05-26 15:29:24 +0200 | [diff] [blame] | 646 | mutex_init(&ggc->irq_lock); |
| 647 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 648 | gpio = &ggc->chip; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 649 | |
| 650 | gpio->label = "greybus_gpio"; |
Greg Kroah-Hartman | c7eb46e | 2015-10-14 11:17:55 -0700 | [diff] [blame] | 651 | gpio->dev = &connection->bundle->dev; |
Johan Hovold | 56c2da1 | 2015-03-19 16:51:10 +0100 | [diff] [blame] | 652 | gpio->owner = THIS_MODULE; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 653 | |
| 654 | gpio->request = gb_gpio_request; |
| 655 | gpio->free = gb_gpio_free; |
| 656 | gpio->get_direction = gb_gpio_get_direction; |
| 657 | gpio->direction_input = gb_gpio_direction_input; |
| 658 | gpio->direction_output = gb_gpio_direction_output; |
| 659 | gpio->get = gb_gpio_get; |
| 660 | gpio->set = gb_gpio_set; |
| 661 | gpio->set_debounce = gb_gpio_set_debounce; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 662 | gpio->to_irq = gb_gpio_to_irq; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 663 | gpio->base = -1; /* Allocate base dynamically */ |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 664 | gpio->ngpio = ggc->line_max + 1; |
Johan Hovold | 56c2da1 | 2015-03-19 16:51:10 +0100 | [diff] [blame] | 665 | gpio->can_sleep = true; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 666 | |
| 667 | ret = gpiochip_add(gpio); |
| 668 | if (ret) { |
Greg Kroah-Hartman | c7eb46e | 2015-10-14 11:17:55 -0700 | [diff] [blame] | 669 | dev_err(&connection->bundle->dev, |
| 670 | "failed to add gpio chip: %d\n", ret); |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 671 | goto err_free_lines; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | ret = gb_gpio_irqchip_add(gpio, irqc, 0, |
Johan Hovold | 1409c4d | 2015-05-26 15:29:25 +0200 | [diff] [blame] | 675 | handle_level_irq, IRQ_TYPE_NONE); |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 676 | if (ret) { |
Greg Kroah-Hartman | c7eb46e | 2015-10-14 11:17:55 -0700 | [diff] [blame] | 677 | dev_err(&connection->bundle->dev, |
| 678 | "failed to add irq chip: %d\n", ret); |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 679 | goto irqchip_err; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 680 | } |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 681 | |
| 682 | return 0; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 683 | |
| 684 | irqchip_err: |
| 685 | gb_gpiochip_remove(gpio); |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 686 | err_free_lines: |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 687 | kfree(ggc->lines); |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 688 | err_free_controller: |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 689 | kfree(ggc); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 690 | return ret; |
| 691 | } |
| 692 | |
Alex Elder | 3689f97 | 2014-10-27 06:04:30 -0500 | [diff] [blame] | 693 | static void gb_gpio_connection_exit(struct gb_connection *connection) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 694 | { |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 695 | struct gb_gpio_controller *ggc = connection->private; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 696 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 697 | if (!ggc) |
Alex Elder | 051fb04 | 2014-10-16 06:35:24 -0500 | [diff] [blame] | 698 | return; |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 699 | |
Johan Hovold | 8aff1ac | 2015-03-19 16:51:14 +0100 | [diff] [blame] | 700 | gb_gpio_irqchip_remove(ggc); |
| 701 | gb_gpiochip_remove(&ggc->chip); |
| 702 | /* kref_put(ggc->connection) */ |
| 703 | kfree(ggc->lines); |
| 704 | kfree(ggc); |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 705 | } |
| 706 | |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 707 | static struct gb_protocol gpio_protocol = { |
Greg Kroah-Hartman | 7422a1e | 2014-12-24 13:01:45 -0800 | [diff] [blame] | 708 | .name = "gpio", |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 709 | .id = GREYBUS_PROTOCOL_GPIO, |
Viresh Kumar | 8590ed3 | 2015-08-08 10:25:34 +0530 | [diff] [blame] | 710 | .major = GB_GPIO_VERSION_MAJOR, |
| 711 | .minor = GB_GPIO_VERSION_MINOR, |
Alex Elder | 5d9fd7e | 2014-11-05 16:12:54 -0600 | [diff] [blame] | 712 | .connection_init = gb_gpio_connection_init, |
| 713 | .connection_exit = gb_gpio_connection_exit, |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 714 | .request_recv = gb_gpio_request_recv, |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 715 | }; |
| 716 | |
Viresh Kumar | e18822e | 2015-07-01 12:13:52 +0530 | [diff] [blame] | 717 | gb_builtin_protocol_driver(gpio_protocol); |