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