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 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 171 | return -EINVAL; |
| 172 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 173 | request.which = which; |
| 174 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE, |
| 175 | &request, sizeof(request), NULL, 0); |
| 176 | if (!ret) |
| 177 | ggc->lines[which].active = true; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 178 | return ret; |
| 179 | } |
| 180 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 181 | static int gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 182 | u8 which) |
| 183 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 184 | struct gb_gpio_deactivate_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 185 | int ret; |
| 186 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 187 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 188 | return -EINVAL; |
| 189 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 190 | request.which = which; |
| 191 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE, |
| 192 | &request, sizeof(request), NULL, 0); |
| 193 | if (!ret) |
| 194 | ggc->lines[which].active = false; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 195 | return ret; |
| 196 | } |
| 197 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 198 | static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 199 | u8 which) |
| 200 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 201 | struct gb_gpio_get_direction_request request; |
| 202 | struct gb_gpio_get_direction_response response; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 203 | int ret; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 204 | u8 direction; |
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 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 207 | return -EINVAL; |
| 208 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 209 | request.which = which; |
| 210 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION, |
| 211 | &request, sizeof(request), |
| 212 | &response, sizeof(response)); |
| 213 | if (ret) |
| 214 | return ret; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 215 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 216 | direction = response.direction; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 217 | if (direction && direction != 1) |
| 218 | pr_warn("gpio %u direction was %u (should be 0 or 1)\n", |
| 219 | which, direction); |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 220 | ggc->lines[which].direction = direction ? 1 : 0; |
| 221 | return 0; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 224 | static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 225 | u8 which) |
| 226 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 227 | struct gb_gpio_direction_in_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 228 | int ret; |
| 229 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 230 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 231 | return -EINVAL; |
| 232 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 233 | request.which = which; |
| 234 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN, |
| 235 | &request, sizeof(request), NULL, 0); |
| 236 | if (!ret) |
| 237 | ggc->lines[which].direction = 1; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 238 | return ret; |
| 239 | } |
| 240 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 241 | static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 242 | u8 which, bool value_high) |
| 243 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 244 | struct gb_gpio_direction_out_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 245 | int ret; |
| 246 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 247 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 248 | return -EINVAL; |
| 249 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 250 | request.which = which; |
| 251 | request.value = value_high ? 1 : 0; |
| 252 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT, |
| 253 | &request, sizeof(request), NULL, 0); |
| 254 | if (!ret) |
| 255 | ggc->lines[which].direction = 0; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 259 | static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 260 | u8 which) |
| 261 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 262 | struct gb_gpio_get_value_request request; |
| 263 | struct gb_gpio_get_value_response response; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 264 | int ret; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 265 | u8 value; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 266 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 267 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 270 | request.which = which; |
| 271 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE, |
| 272 | &request, sizeof(request), |
| 273 | &response, sizeof(response)); |
| 274 | if (ret) |
| 275 | return ret; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 276 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 277 | value = response.value; |
Alex Elder | 23383de | 2014-11-21 19:29:12 -0600 | [diff] [blame] | 278 | if (value && value != 1) |
| 279 | pr_warn("gpio %u value was %u (should be 0 or 1)\n", |
| 280 | which, value); |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 281 | ggc->lines[which].value = value ? 1 : 0; |
| 282 | return 0; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 285 | static int gb_gpio_set_value_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 286 | u8 which, bool value_high) |
| 287 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 288 | struct gb_gpio_set_value_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 289 | int ret; |
| 290 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 291 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 292 | return -EINVAL; |
| 293 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 294 | request.which = which; |
| 295 | request.value = value_high ? 1 : 0; |
| 296 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE, |
| 297 | &request, sizeof(request), NULL, 0); |
| 298 | if (!ret) { |
| 299 | /* XXX should this set direction to out? */ |
| 300 | ggc->lines[which].value = request.value; |
| 301 | } |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 302 | return ret; |
| 303 | } |
| 304 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 305 | static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc, |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 306 | u8 which, u16 debounce_usec) |
| 307 | { |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 308 | struct gb_gpio_set_debounce_request request; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 309 | int ret; |
| 310 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 311 | if (which > ggc->line_max) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 312 | return -EINVAL; |
| 313 | |
Greg Kroah-Hartman | 7d5bbb1 | 2014-11-23 17:45:22 -0800 | [diff] [blame] | 314 | request.which = which; |
| 315 | request.usec = cpu_to_le16(debounce_usec); |
| 316 | ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE, |
| 317 | &request, sizeof(request), NULL, 0); |
| 318 | if (!ret) |
| 319 | ggc->lines[which].debounce_usec = debounce_usec; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 323 | static void gb_gpio_ack_irq(struct irq_data *d) |
| 324 | { |
| 325 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 326 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 327 | struct gb_gpio_irq_ack_request request; |
| 328 | int ret; |
| 329 | |
| 330 | request.which = d->hwirq; |
| 331 | ret = gb_operation_sync(ggc->connection, |
| 332 | GB_GPIO_TYPE_IRQ_ACK, |
| 333 | &request, sizeof(request), NULL, 0); |
| 334 | if (ret) |
| 335 | pr_err("irq ack operation failed (%d)\n", ret); |
| 336 | } |
| 337 | |
| 338 | static void gb_gpio_mask_irq(struct irq_data *d) |
| 339 | { |
| 340 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 341 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 342 | struct gb_gpio_irq_mask_request request; |
| 343 | int ret; |
| 344 | |
| 345 | request.which = d->hwirq; |
| 346 | ret = gb_operation_sync(ggc->connection, |
| 347 | GB_GPIO_TYPE_IRQ_MASK, |
| 348 | &request, sizeof(request), NULL, 0); |
| 349 | if (ret) |
| 350 | pr_err("irq mask operation failed (%d)\n", ret); |
| 351 | } |
| 352 | |
| 353 | static void gb_gpio_unmask_irq(struct irq_data *d) |
| 354 | { |
| 355 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 356 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 357 | struct gb_gpio_irq_unmask_request request; |
| 358 | int ret; |
| 359 | |
| 360 | request.which = d->hwirq; |
| 361 | ret = gb_operation_sync(ggc->connection, |
| 362 | GB_GPIO_TYPE_IRQ_UNMASK, |
| 363 | &request, sizeof(request), NULL, 0); |
| 364 | if (ret) |
| 365 | pr_err("irq unmask operation failed (%d)\n", ret); |
| 366 | } |
| 367 | |
| 368 | static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type) |
| 369 | { |
| 370 | struct gpio_chip *chip = irq_data_to_gpio_chip(d); |
| 371 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 372 | struct gb_gpio_irq_type_request request; |
| 373 | int ret = 0; |
| 374 | |
| 375 | request.which = d->hwirq; |
| 376 | request.type = type; |
| 377 | |
| 378 | switch (type) { |
| 379 | case IRQ_TYPE_NONE: |
| 380 | break; |
| 381 | case IRQ_TYPE_EDGE_RISING: |
| 382 | case IRQ_TYPE_EDGE_FALLING: |
| 383 | case IRQ_TYPE_EDGE_BOTH: |
| 384 | case IRQ_TYPE_LEVEL_LOW: |
| 385 | case IRQ_TYPE_LEVEL_HIGH: |
| 386 | ret = gb_operation_sync(ggc->connection, |
| 387 | GB_GPIO_TYPE_IRQ_TYPE, |
| 388 | &request, sizeof(request), NULL, 0); |
| 389 | if (ret) |
| 390 | pr_err("irq type operation failed (%d)\n", ret); |
| 391 | break; |
| 392 | default: |
| 393 | pr_err("No such irq type %d", type); |
| 394 | ret = -EINVAL; |
| 395 | } |
| 396 | |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static void gb_gpio_request_recv(u8 type, struct gb_operation *op) |
| 401 | { |
| 402 | struct gb_gpio_controller *ggc; |
| 403 | struct gb_connection *connection; |
| 404 | struct gb_message *request; |
| 405 | struct gb_gpio_irq_event_request *event; |
| 406 | int irq; |
| 407 | struct irq_desc *desc; |
| 408 | int ret; |
| 409 | |
| 410 | if (type != GB_GPIO_TYPE_IRQ_EVENT) { |
| 411 | pr_err("unsupported unsolicited request\n"); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | connection = op->connection; |
| 416 | ggc = connection->private; |
| 417 | |
| 418 | request = op->request; |
| 419 | event = request->payload; |
| 420 | if (event->which > ggc->line_max) { |
| 421 | pr_err("Unsupported hw irq %d\n", event->which); |
| 422 | return; |
| 423 | } |
Alexandre Bailon | d1b20d7 | 2015-03-02 17:32:43 +0100 | [diff] [blame] | 424 | irq = gpio_to_irq(ggc->chip.base + event->which); |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 425 | desc = irq_to_desc(irq); |
| 426 | |
| 427 | /* Dispatch interrupt */ |
| 428 | local_irq_disable(); |
| 429 | handle_simple_irq(irq, desc); |
| 430 | local_irq_enable(); |
| 431 | |
| 432 | ret = gb_operation_response_send(op, 0); |
| 433 | if (ret) |
| 434 | pr_err("error %d sending response status %d\n", ret, 0); |
| 435 | } |
| 436 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 437 | static int gb_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 438 | { |
| 439 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 440 | int ret; |
| 441 | |
Bill Pemberton | 76590b1 | 2015-01-16 13:57:32 -0500 | [diff] [blame] | 442 | if (offset >= chip->ngpio) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 443 | return -EINVAL; |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 444 | ret = gb_gpio_activate_operation(gb_gpio_controller, (u8)offset); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 445 | if (ret) |
| 446 | ; /* return ret; */ |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 450 | static void gb_gpio_free(struct gpio_chip *chip, unsigned offset) |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 451 | { |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 452 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 453 | int ret; |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 454 | |
Bill Pemberton | 76590b1 | 2015-01-16 13:57:32 -0500 | [diff] [blame] | 455 | if (offset >= chip->ngpio) { |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 456 | pr_err("bad offset %u supplied (must be 0..%u)\n", |
| 457 | offset, chip->ngpio - 1); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 458 | return; |
| 459 | } |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 460 | ret = gb_gpio_deactivate_operation(gb_gpio_controller, (u8)offset); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 461 | if (ret) |
| 462 | ; /* return ret; */ |
| 463 | } |
| 464 | |
| 465 | static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
| 466 | { |
| 467 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
| 468 | u8 which; |
| 469 | int ret; |
| 470 | |
Bill Pemberton | 76590b1 | 2015-01-16 13:57:32 -0500 | [diff] [blame] | 471 | if (offset >= chip->ngpio) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 472 | return -EINVAL; |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 473 | which = (u8)offset; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 474 | ret = gb_gpio_get_direction_operation(gb_gpio_controller, which); |
| 475 | if (ret) |
| 476 | ; /* return ret; */ |
| 477 | return gb_gpio_controller->lines[which].direction ? 1 : 0; |
| 478 | } |
| 479 | |
| 480 | static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 481 | { |
| 482 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 483 | int ret; |
| 484 | |
Bill Pemberton | 76590b1 | 2015-01-16 13:57:32 -0500 | [diff] [blame] | 485 | if (offset >= chip->ngpio) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 486 | return -EINVAL; |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 487 | ret = gb_gpio_direction_in_operation(gb_gpio_controller, (u8)offset); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 488 | if (ret) |
| 489 | ; /* return ret; */ |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 494 | int value) |
| 495 | { |
| 496 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 497 | int ret; |
| 498 | |
Bill Pemberton | 76590b1 | 2015-01-16 13:57:32 -0500 | [diff] [blame] | 499 | if (offset >= chip->ngpio) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 500 | return -EINVAL; |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 501 | ret = gb_gpio_direction_out_operation(gb_gpio_controller, (u8)offset, !!value); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 502 | if (ret) |
| 503 | ; /* return ret; */ |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int gb_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 508 | { |
| 509 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
| 510 | u8 which; |
| 511 | int ret; |
| 512 | |
Bill Pemberton | 76590b1 | 2015-01-16 13:57:32 -0500 | [diff] [blame] | 513 | if (offset >= chip->ngpio) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 514 | return -EINVAL; |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 515 | which = (u8)offset; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 516 | ret = gb_gpio_get_value_operation(gb_gpio_controller, which); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | return (int)gb_gpio_controller->lines[which].value; |
| 520 | } |
| 521 | |
| 522 | static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 523 | { |
| 524 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 525 | int ret; |
| 526 | |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 527 | if (offset < 0 || offset >= chip->ngpio) { |
| 528 | pr_err("bad offset %u supplied (must be 0..%u)\n", |
| 529 | offset, chip->ngpio - 1); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 530 | return; |
| 531 | } |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 532 | ret = gb_gpio_set_value_operation(gb_gpio_controller, (u8)offset, !!value); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 533 | if (ret) |
| 534 | ; /* return ret; */ |
| 535 | } |
| 536 | |
| 537 | static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset, |
| 538 | unsigned debounce) |
| 539 | { |
| 540 | struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip); |
| 541 | u16 usec; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 542 | int ret; |
| 543 | |
Bill Pemberton | 76590b1 | 2015-01-16 13:57:32 -0500 | [diff] [blame] | 544 | if (offset >= chip->ngpio) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 545 | return -EINVAL; |
Johan Hovold | c2a6610 | 2015-03-19 16:51:09 +0100 | [diff] [blame] | 546 | if (debounce > U16_MAX) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 547 | return -EINVAL; |
Johan Hovold | c2a6610 | 2015-03-19 16:51:09 +0100 | [diff] [blame] | 548 | usec = (u16)debounce; |
Matt Porter | ff6e0b9 | 2014-10-20 06:39:45 -0400 | [diff] [blame] | 549 | ret = gb_gpio_set_debounce_operation(gb_gpio_controller, (u8)offset, usec); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 550 | if (ret) |
| 551 | ; /* return ret; */ |
| 552 | |
| 553 | return 0; /* XXX */ |
| 554 | } |
| 555 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 556 | static void gb_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 557 | { |
| 558 | return; /* XXX */ |
| 559 | } |
| 560 | |
Greg Kroah-Hartman | d4c8247 | 2014-10-27 12:33:47 +0800 | [diff] [blame] | 561 | static int gb_gpio_controller_setup(struct gb_gpio_controller *gb_gpio_controller) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 562 | { |
| 563 | u32 line_count; |
| 564 | size_t size; |
| 565 | int ret; |
| 566 | |
| 567 | /* First thing we need to do is check the version */ |
Viresh Kumar | 36e79de | 2015-01-21 18:12:36 +0530 | [diff] [blame] | 568 | ret = get_version(gb_gpio_controller); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 569 | if (ret) |
| 570 | ; /* return ret; */ |
| 571 | |
| 572 | /* Now find out how many lines there are */ |
| 573 | ret = gb_gpio_line_count_operation(gb_gpio_controller); |
| 574 | if (ret) |
| 575 | ; /* return ret; */ |
| 576 | line_count = (u32)gb_gpio_controller->line_max + 1; |
| 577 | size = line_count * sizeof(*gb_gpio_controller->lines); |
| 578 | gb_gpio_controller->lines = kzalloc(size, GFP_KERNEL); |
| 579 | if (!gb_gpio_controller->lines) |
| 580 | return -ENOMEM; |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 585 | /** |
| 586 | * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip |
| 587 | * @d: the irqdomain used by this irqchip |
| 588 | * @irq: the global irq number used by this GB gpio irqchip irq |
| 589 | * @hwirq: the local IRQ/GPIO line offset on this GB gpio |
| 590 | * |
| 591 | * This function will set up the mapping for a certain IRQ line on a |
| 592 | * GB gpio by assigning the GB gpio as chip data, and using the irqchip |
| 593 | * stored inside the GB gpio. |
| 594 | */ |
| 595 | static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq, |
| 596 | irq_hw_number_t hwirq) |
| 597 | { |
| 598 | struct gpio_chip *chip = domain->host_data; |
| 599 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 600 | |
| 601 | irq_set_chip_data(irq, ggc); |
| 602 | irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler); |
| 603 | #ifdef CONFIG_ARM |
| 604 | set_irq_flags(irq, IRQF_VALID); |
| 605 | #else |
| 606 | irq_set_noprobe(irq); |
| 607 | #endif |
| 608 | /* |
| 609 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
| 610 | * is passed as default type. |
| 611 | */ |
| 612 | if (ggc->irq_default_type != IRQ_TYPE_NONE) |
| 613 | irq_set_irq_type(irq, ggc->irq_default_type); |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq) |
| 619 | { |
| 620 | #ifdef CONFIG_ARM |
| 621 | set_irq_flags(irq, 0); |
| 622 | #endif |
| 623 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 624 | irq_set_chip_data(irq, NULL); |
| 625 | } |
| 626 | |
| 627 | static const struct irq_domain_ops gb_gpio_domain_ops = { |
| 628 | .map = gb_gpio_irq_map, |
| 629 | .unmap = gb_gpio_irq_unmap, |
| 630 | }; |
| 631 | |
| 632 | /** |
| 633 | * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller |
| 634 | * @ggc: the gb_gpio_controller to remove the irqchip from |
| 635 | * |
| 636 | * This is called only from gb_gpio_remove() |
| 637 | */ |
| 638 | static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc) |
| 639 | { |
| 640 | unsigned int offset; |
| 641 | |
| 642 | /* Remove all IRQ mappings and delete the domain */ |
| 643 | if (ggc->irqdomain) { |
| 644 | for (offset = 0; offset < (ggc->line_max + 1); offset++) |
| 645 | irq_dispose_mapping(irq_find_mapping(ggc->irqdomain, offset)); |
| 646 | irq_domain_remove(ggc->irqdomain); |
| 647 | } |
| 648 | |
| 649 | if (ggc->irqchip) { |
| 650 | ggc->irqchip = NULL; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | |
| 655 | /** |
| 656 | * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip |
| 657 | * @chip: the gpio chip to add the irqchip to |
| 658 | * @irqchip: the irqchip to add to the adapter |
| 659 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
| 660 | * allocate gpio irqs from |
| 661 | * @handler: the irq handler to use (often a predefined irq core function) |
| 662 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
| 663 | * to have the core avoid setting up any default type in the hardware. |
| 664 | * |
| 665 | * This function closely associates a certain irqchip with a certain |
| 666 | * gpio chip, providing an irq domain to translate the local IRQs to |
| 667 | * global irqs, and making sure that the gpio chip |
| 668 | * is passed as chip data to all related functions. Driver callbacks |
| 669 | * need to use container_of() to get their local state containers back |
| 670 | * from the gpio chip passed as chip data. An irqdomain will be stored |
| 671 | * in the gpio chip that shall be used by the driver to handle IRQ number |
| 672 | * translation. The gpio chip will need to be initialized and registered |
| 673 | * before calling this function. |
| 674 | */ |
| 675 | static int gb_gpio_irqchip_add(struct gpio_chip *chip, |
| 676 | struct irq_chip *irqchip, |
| 677 | unsigned int first_irq, |
| 678 | irq_flow_handler_t handler, |
| 679 | unsigned int type) |
| 680 | { |
| 681 | struct gb_gpio_controller *ggc; |
| 682 | unsigned int offset; |
| 683 | unsigned irq_base; |
| 684 | |
| 685 | if (!chip || !irqchip) |
| 686 | return -EINVAL; |
| 687 | |
| 688 | ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 689 | |
| 690 | ggc->irqchip = irqchip; |
| 691 | ggc->irq_handler = handler; |
| 692 | ggc->irq_default_type = type; |
| 693 | ggc->irqdomain = irq_domain_add_simple(NULL, |
| 694 | ggc->line_max + 1, first_irq, |
| 695 | &gb_gpio_domain_ops, chip); |
| 696 | if (!ggc->irqdomain) { |
| 697 | ggc->irqchip = NULL; |
| 698 | return -EINVAL; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * Prepare the mapping since the irqchip shall be orthogonal to |
| 703 | * any gpio calls. If the first_irq was zero, this is |
| 704 | * necessary to allocate descriptors for all IRQs. |
| 705 | */ |
| 706 | for (offset = 0; offset < (ggc->line_max + 1); offset++) { |
| 707 | irq_base = irq_create_mapping(ggc->irqdomain, offset); |
| 708 | if (offset == 0) |
| 709 | ggc->irq_base = irq_base; |
| 710 | } |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 716 | { |
| 717 | struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip); |
| 718 | |
| 719 | return irq_find_mapping(ggc->irqdomain, offset); |
| 720 | } |
| 721 | |
Alex Elder | 3689f97 | 2014-10-27 06:04:30 -0500 | [diff] [blame] | 722 | static int gb_gpio_connection_init(struct gb_connection *connection) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 723 | { |
| 724 | struct gb_gpio_controller *gb_gpio_controller; |
| 725 | struct gpio_chip *gpio; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 726 | struct irq_chip *irqc; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 727 | int ret; |
| 728 | |
| 729 | gb_gpio_controller = kzalloc(sizeof(*gb_gpio_controller), GFP_KERNEL); |
| 730 | if (!gb_gpio_controller) |
| 731 | return -ENOMEM; |
| 732 | gb_gpio_controller->connection = connection; |
Alex Elder | 93bbe85 | 2014-12-03 12:27:42 -0600 | [diff] [blame] | 733 | connection->private = gb_gpio_controller; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 734 | |
| 735 | ret = gb_gpio_controller_setup(gb_gpio_controller); |
| 736 | if (ret) |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 737 | goto err_free_controller; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 738 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 739 | irqc = &gb_gpio_controller->irqc; |
| 740 | irqc->irq_ack = gb_gpio_ack_irq; |
| 741 | irqc->irq_mask = gb_gpio_mask_irq; |
| 742 | irqc->irq_unmask = gb_gpio_unmask_irq; |
| 743 | irqc->irq_set_type = gb_gpio_irq_set_type; |
| 744 | irqc->name = "greybus_gpio"; |
| 745 | |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 746 | gpio = &gb_gpio_controller->chip; |
| 747 | |
| 748 | gpio->label = "greybus_gpio"; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 749 | gpio->dev = &connection->dev; |
Johan Hovold | 56c2da1 | 2015-03-19 16:51:10 +0100 | [diff] [blame^] | 750 | gpio->owner = THIS_MODULE; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 751 | |
| 752 | gpio->request = gb_gpio_request; |
| 753 | gpio->free = gb_gpio_free; |
| 754 | gpio->get_direction = gb_gpio_get_direction; |
| 755 | gpio->direction_input = gb_gpio_direction_input; |
| 756 | gpio->direction_output = gb_gpio_direction_output; |
| 757 | gpio->get = gb_gpio_get; |
| 758 | gpio->set = gb_gpio_set; |
| 759 | gpio->set_debounce = gb_gpio_set_debounce; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 760 | gpio->dbg_show = gb_gpio_dbg_show; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 761 | gpio->to_irq = gb_gpio_to_irq; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 762 | gpio->base = -1; /* Allocate base dynamically */ |
| 763 | gpio->ngpio = gb_gpio_controller->line_max + 1; |
Johan Hovold | 56c2da1 | 2015-03-19 16:51:10 +0100 | [diff] [blame^] | 764 | gpio->can_sleep = true; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 765 | |
| 766 | ret = gpiochip_add(gpio); |
| 767 | if (ret) { |
| 768 | pr_err("Failed to register GPIO\n"); |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 769 | goto err_free_lines; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | ret = gb_gpio_irqchip_add(gpio, irqc, 0, |
| 773 | handle_simple_irq, IRQ_TYPE_NONE); |
| 774 | if (ret) { |
| 775 | pr_err("Couldn't add irqchip to Greybus GPIO controller %d\n", ret); |
| 776 | goto irqchip_err; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 777 | } |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 778 | |
| 779 | return 0; |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 780 | |
| 781 | irqchip_err: |
| 782 | gb_gpiochip_remove(gpio); |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 783 | err_free_lines: |
| 784 | kfree(gb_gpio_controller->lines); |
| 785 | err_free_controller: |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 786 | kfree(gb_gpio_controller); |
| 787 | return ret; |
| 788 | } |
| 789 | |
Alex Elder | 3689f97 | 2014-10-27 06:04:30 -0500 | [diff] [blame] | 790 | static void gb_gpio_connection_exit(struct gb_connection *connection) |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 791 | { |
| 792 | struct gb_gpio_controller *gb_gpio_controller = connection->private; |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 793 | |
| 794 | if (!gb_gpio_controller) |
Alex Elder | 051fb04 | 2014-10-16 06:35:24 -0500 | [diff] [blame] | 795 | return; |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 796 | |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 797 | gb_gpio_irqchip_remove(gb_gpio_controller); |
Greg Kroah-Hartman | 213aefe | 2014-10-20 13:40:02 +0800 | [diff] [blame] | 798 | gb_gpiochip_remove(&gb_gpio_controller->chip); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 799 | /* kref_put(gb_gpio_controller->connection) */ |
Johan Hovold | 35a64f2 | 2015-02-13 14:58:04 +0800 | [diff] [blame] | 800 | kfree(gb_gpio_controller->lines); |
Alex Elder | bb2e1c9 | 2014-10-16 06:35:39 -0500 | [diff] [blame] | 801 | kfree(gb_gpio_controller); |
Greg Kroah-Hartman | c16854c | 2014-08-12 12:00:16 +0800 | [diff] [blame] | 802 | } |
| 803 | |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 804 | static struct gb_protocol gpio_protocol = { |
Greg Kroah-Hartman | 7422a1e | 2014-12-24 13:01:45 -0800 | [diff] [blame] | 805 | .name = "gpio", |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 806 | .id = GREYBUS_PROTOCOL_GPIO, |
| 807 | .major = 0, |
| 808 | .minor = 1, |
Alex Elder | 5d9fd7e | 2014-11-05 16:12:54 -0600 | [diff] [blame] | 809 | .connection_init = gb_gpio_connection_init, |
| 810 | .connection_exit = gb_gpio_connection_exit, |
Matt Porter | 036aad9 | 2015-02-17 10:48:23 -0500 | [diff] [blame] | 811 | .request_recv = gb_gpio_request_recv, |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 812 | }; |
| 813 | |
Greg Kroah-Hartman | 7c7d5b9 | 2014-12-23 15:16:51 -0800 | [diff] [blame] | 814 | int gb_gpio_protocol_init(void) |
Alex Elder | 19d03de | 2014-11-05 16:12:53 -0600 | [diff] [blame] | 815 | { |
| 816 | return gb_protocol_register(&gpio_protocol); |
| 817 | } |
| 818 | |
| 819 | void gb_gpio_protocol_exit(void) |
| 820 | { |
| 821 | gb_protocol_deregister(&gpio_protocol); |
| 822 | } |