Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Arizona interrupt support |
| 3 | * |
| 4 | * Copyright 2012 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/gpio.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/irq.h> |
| 17 | #include <linux/irqdomain.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/pm_runtime.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/regulator/consumer.h> |
| 22 | #include <linux/slab.h> |
| 23 | |
| 24 | #include <linux/mfd/arizona/core.h> |
| 25 | #include <linux/mfd/arizona/registers.h> |
| 26 | |
| 27 | #include "arizona.h" |
| 28 | |
| 29 | static int arizona_map_irq(struct arizona *arizona, int irq) |
| 30 | { |
| 31 | int ret; |
| 32 | |
| 33 | ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq); |
| 34 | if (ret < 0) |
| 35 | ret = regmap_irq_get_virq(arizona->irq_chip, irq); |
| 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | int arizona_request_irq(struct arizona *arizona, int irq, char *name, |
| 41 | irq_handler_t handler, void *data) |
| 42 | { |
| 43 | irq = arizona_map_irq(arizona, irq); |
| 44 | if (irq < 0) |
| 45 | return irq; |
| 46 | |
| 47 | return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT, |
| 48 | name, data); |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(arizona_request_irq); |
| 51 | |
| 52 | void arizona_free_irq(struct arizona *arizona, int irq, void *data) |
| 53 | { |
| 54 | irq = arizona_map_irq(arizona, irq); |
| 55 | if (irq < 0) |
| 56 | return; |
| 57 | |
| 58 | free_irq(irq, data); |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(arizona_free_irq); |
| 61 | |
| 62 | int arizona_set_irq_wake(struct arizona *arizona, int irq, int on) |
| 63 | { |
| 64 | irq = arizona_map_irq(arizona, irq); |
| 65 | if (irq < 0) |
| 66 | return irq; |
| 67 | |
| 68 | return irq_set_irq_wake(irq, on); |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(arizona_set_irq_wake); |
| 71 | |
| 72 | static irqreturn_t arizona_boot_done(int irq, void *data) |
| 73 | { |
| 74 | struct arizona *arizona = data; |
| 75 | |
| 76 | dev_dbg(arizona->dev, "Boot done\n"); |
| 77 | |
| 78 | return IRQ_HANDLED; |
| 79 | } |
| 80 | |
| 81 | static irqreturn_t arizona_ctrlif_err(int irq, void *data) |
| 82 | { |
| 83 | struct arizona *arizona = data; |
| 84 | |
| 85 | /* |
| 86 | * For pretty much all potential sources a register cache sync |
| 87 | * won't help, we've just got a software bug somewhere. |
| 88 | */ |
| 89 | dev_err(arizona->dev, "Control interface error\n"); |
| 90 | |
| 91 | return IRQ_HANDLED; |
| 92 | } |
| 93 | |
| 94 | static irqreturn_t arizona_irq_thread(int irq, void *data) |
| 95 | { |
| 96 | struct arizona *arizona = data; |
Mark Brown | 3092f80 | 2013-03-24 23:05:58 +0000 | [diff] [blame] | 97 | bool poll; |
Mark Brown | 3080de4 | 2012-08-21 20:02:03 +0100 | [diff] [blame] | 98 | unsigned int val; |
Mark Brown | cdabc1c | 2012-09-08 09:00:59 +0800 | [diff] [blame] | 99 | int ret; |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 100 | |
| 101 | ret = pm_runtime_get_sync(arizona->dev); |
| 102 | if (ret < 0) { |
| 103 | dev_err(arizona->dev, "Failed to resume device: %d\n", ret); |
| 104 | return IRQ_NONE; |
| 105 | } |
| 106 | |
Mark Brown | 3092f80 | 2013-03-24 23:05:58 +0000 | [diff] [blame] | 107 | do { |
| 108 | poll = false; |
Mark Brown | 3080de4 | 2012-08-21 20:02:03 +0100 | [diff] [blame] | 109 | |
Mark Brown | 3092f80 | 2013-03-24 23:05:58 +0000 | [diff] [blame] | 110 | /* Always handle the AoD domain */ |
| 111 | handle_nested_irq(irq_find_mapping(arizona->virq, 0)); |
| 112 | |
| 113 | /* |
| 114 | * Check if one of the main interrupts is asserted and only |
| 115 | * check that domain if it is. |
| 116 | */ |
| 117 | ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS, |
| 118 | &val); |
| 119 | if (ret == 0 && val & ARIZONA_IRQ1_STS) { |
| 120 | handle_nested_irq(irq_find_mapping(arizona->virq, 1)); |
| 121 | } else if (ret != 0) { |
| 122 | dev_err(arizona->dev, |
| 123 | "Failed to read main IRQ status: %d\n", ret); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Poll the IRQ pin status to see if we're really done |
| 128 | * if the interrupt controller can't do it for us. |
| 129 | */ |
| 130 | if (!arizona->pdata.irq_gpio) { |
| 131 | break; |
| 132 | } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING && |
| 133 | gpio_get_value_cansleep(arizona->pdata.irq_gpio)) { |
| 134 | poll = true; |
| 135 | } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING && |
| 136 | !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) { |
| 137 | poll = true; |
| 138 | } |
| 139 | } while (poll); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 140 | |
| 141 | pm_runtime_mark_last_busy(arizona->dev); |
| 142 | pm_runtime_put_autosuspend(arizona->dev); |
| 143 | |
| 144 | return IRQ_HANDLED; |
| 145 | } |
| 146 | |
| 147 | static void arizona_irq_enable(struct irq_data *data) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | static void arizona_irq_disable(struct irq_data *data) |
| 152 | { |
| 153 | } |
| 154 | |
Charles Keepax | c38715f | 2014-09-01 15:29:11 +0100 | [diff] [blame] | 155 | static int arizona_irq_set_wake(struct irq_data *data, unsigned int on) |
| 156 | { |
| 157 | struct arizona *arizona = irq_data_get_irq_chip_data(data); |
| 158 | |
| 159 | return irq_set_irq_wake(arizona->irq, on); |
| 160 | } |
| 161 | |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 162 | static struct irq_chip arizona_irq_chip = { |
| 163 | .name = "arizona", |
| 164 | .irq_disable = arizona_irq_disable, |
| 165 | .irq_enable = arizona_irq_enable, |
Charles Keepax | c38715f | 2014-09-01 15:29:11 +0100 | [diff] [blame] | 166 | .irq_set_wake = arizona_irq_set_wake, |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | static int arizona_irq_map(struct irq_domain *h, unsigned int virq, |
| 170 | irq_hw_number_t hw) |
| 171 | { |
Charles Keepax | 0a464df | 2015-09-11 16:07:56 +0100 | [diff] [blame] | 172 | struct arizona *data = h->host_data; |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 173 | |
| 174 | irq_set_chip_data(virq, data); |
Charles Keepax | cfeb35d | 2014-09-09 17:00:09 +0100 | [diff] [blame] | 175 | irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_simple_irq); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 176 | irq_set_nested_thread(virq, 1); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 177 | irq_set_noprobe(virq); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
Krzysztof Kozlowski | 7ce7b26 | 2015-04-27 21:54:13 +0900 | [diff] [blame] | 182 | static const struct irq_domain_ops arizona_domain_ops = { |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 183 | .map = arizona_irq_map, |
| 184 | .xlate = irq_domain_xlate_twocell, |
| 185 | }; |
| 186 | |
| 187 | int arizona_irq_init(struct arizona *arizona) |
| 188 | { |
| 189 | int flags = IRQF_ONESHOT; |
| 190 | int ret, i; |
| 191 | const struct regmap_irq_chip *aod, *irq; |
Mark Brown | 22c75fe | 2013-03-24 23:16:56 +0000 | [diff] [blame] | 192 | struct irq_data *irq_data; |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 193 | |
Charles Keepax | 30a2af3 | 2014-07-15 11:21:50 +0100 | [diff] [blame] | 194 | arizona->ctrlif_error = true; |
| 195 | |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 196 | switch (arizona->type) { |
Mark Brown | 863df8d | 2012-07-05 20:35:31 +0100 | [diff] [blame] | 197 | #ifdef CONFIG_MFD_WM5102 |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 198 | case WM5102: |
| 199 | aod = &wm5102_aod; |
| 200 | irq = &wm5102_irq; |
Mark Brown | 92d8013 | 2012-08-07 19:57:53 +0100 | [diff] [blame] | 201 | |
Charles Keepax | 30a2af3 | 2014-07-15 11:21:50 +0100 | [diff] [blame] | 202 | arizona->ctrlif_error = false; |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 203 | break; |
Mark Brown | 863df8d | 2012-07-05 20:35:31 +0100 | [diff] [blame] | 204 | #endif |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 205 | #ifdef CONFIG_MFD_WM5110 |
| 206 | case WM5110: |
Richard Fitzgerald | e5d4ef0 | 2015-01-17 15:21:22 +0000 | [diff] [blame] | 207 | case WM8280: |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 208 | aod = &wm5110_aod; |
Charles Keepax | 3215501 | 2014-07-15 11:21:48 +0100 | [diff] [blame] | 209 | |
| 210 | switch (arizona->rev) { |
| 211 | case 0 ... 2: |
| 212 | irq = &wm5110_irq; |
| 213 | break; |
| 214 | default: |
| 215 | irq = &wm5110_revd_irq; |
| 216 | break; |
| 217 | } |
Mark Brown | 92d8013 | 2012-08-07 19:57:53 +0100 | [diff] [blame] | 218 | |
Charles Keepax | 30a2af3 | 2014-07-15 11:21:50 +0100 | [diff] [blame] | 219 | arizona->ctrlif_error = false; |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 220 | break; |
| 221 | #endif |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 222 | #ifdef CONFIG_MFD_WM8997 |
| 223 | case WM8997: |
| 224 | aod = &wm8997_aod; |
| 225 | irq = &wm8997_irq; |
| 226 | |
Charles Keepax | 30a2af3 | 2014-07-15 11:21:50 +0100 | [diff] [blame] | 227 | arizona->ctrlif_error = false; |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 228 | break; |
| 229 | #endif |
Richard Fitzgerald | 6887b04 | 2015-07-03 16:16:35 +0100 | [diff] [blame] | 230 | #ifdef CONFIG_MFD_WM8998 |
| 231 | case WM8998: |
| 232 | case WM1814: |
| 233 | aod = &wm8998_aod; |
| 234 | irq = &wm8998_irq; |
| 235 | |
| 236 | arizona->ctrlif_error = false; |
| 237 | break; |
| 238 | #endif |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 239 | default: |
| 240 | BUG_ON("Unknown Arizona class device" == NULL); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
Mark Brown | 1816cb3 | 2013-01-17 16:54:18 +0900 | [diff] [blame] | 244 | /* Disable all wake sources by default */ |
| 245 | regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0); |
| 246 | |
Mark Brown | 22c75fe | 2013-03-24 23:16:56 +0000 | [diff] [blame] | 247 | /* Read the flags from the interrupt controller if not specified */ |
| 248 | if (!arizona->pdata.irq_flags) { |
| 249 | irq_data = irq_get_irq_data(arizona->irq); |
| 250 | if (!irq_data) { |
| 251 | dev_err(arizona->dev, "Invalid IRQ: %d\n", |
| 252 | arizona->irq); |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data); |
| 257 | switch (arizona->pdata.irq_flags) { |
| 258 | case IRQF_TRIGGER_LOW: |
| 259 | case IRQF_TRIGGER_HIGH: |
| 260 | case IRQF_TRIGGER_RISING: |
| 261 | case IRQF_TRIGGER_FALLING: |
| 262 | break; |
| 263 | |
| 264 | case IRQ_TYPE_NONE: |
| 265 | default: |
| 266 | /* Device default */ |
| 267 | arizona->pdata.irq_flags = IRQF_TRIGGER_LOW; |
| 268 | break; |
| 269 | } |
| 270 | } |
Mark Brown | f8a0941 | 2013-03-22 12:59:33 +0100 | [diff] [blame] | 271 | |
| 272 | if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH | |
| 273 | IRQF_TRIGGER_RISING)) { |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 274 | ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1, |
| 275 | ARIZONA_IRQ_POL, 0); |
| 276 | if (ret != 0) { |
| 277 | dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n", |
| 278 | ret); |
| 279 | goto err; |
| 280 | } |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Mark Brown | f8a0941 | 2013-03-22 12:59:33 +0100 | [diff] [blame] | 283 | flags |= arizona->pdata.irq_flags; |
| 284 | |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 285 | /* Allocate a virtual IRQ domain to distribute to the regmap domains */ |
| 286 | arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops, |
| 287 | arizona); |
| 288 | if (!arizona->virq) { |
Mark Brown | b7dea5d | 2012-12-05 11:46:26 +0900 | [diff] [blame] | 289 | dev_err(arizona->dev, "Failed to add core IRQ domain\n"); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 290 | ret = -EINVAL; |
| 291 | goto err; |
| 292 | } |
| 293 | |
| 294 | ret = regmap_add_irq_chip(arizona->regmap, |
| 295 | irq_create_mapping(arizona->virq, 0), |
Charles Keepax | 2a3377e | 2014-08-12 14:51:22 +0100 | [diff] [blame] | 296 | IRQF_ONESHOT, 0, aod, |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 297 | &arizona->aod_irq_chip); |
| 298 | if (ret != 0) { |
| 299 | dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret); |
| 300 | goto err_domain; |
| 301 | } |
| 302 | |
| 303 | ret = regmap_add_irq_chip(arizona->regmap, |
| 304 | irq_create_mapping(arizona->virq, 1), |
Charles Keepax | 2a3377e | 2014-08-12 14:51:22 +0100 | [diff] [blame] | 305 | IRQF_ONESHOT, 0, irq, |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 306 | &arizona->irq_chip); |
| 307 | if (ret != 0) { |
Charles Keepax | d1cb4cc | 2014-05-19 17:35:29 +0100 | [diff] [blame] | 308 | dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 309 | goto err_aod; |
| 310 | } |
| 311 | |
| 312 | /* Make sure the boot done IRQ is unmasked for resumes */ |
| 313 | i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE); |
| 314 | ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT, |
| 315 | "Boot done", arizona); |
| 316 | if (ret != 0) { |
| 317 | dev_err(arizona->dev, "Failed to request boot done %d: %d\n", |
| 318 | arizona->irq, ret); |
| 319 | goto err_boot_done; |
| 320 | } |
| 321 | |
| 322 | /* Handle control interface errors in the core */ |
Charles Keepax | 30a2af3 | 2014-07-15 11:21:50 +0100 | [diff] [blame] | 323 | if (arizona->ctrlif_error) { |
Mark Brown | 92d8013 | 2012-08-07 19:57:53 +0100 | [diff] [blame] | 324 | i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR); |
| 325 | ret = request_threaded_irq(i, NULL, arizona_ctrlif_err, |
| 326 | IRQF_ONESHOT, |
| 327 | "Control interface error", arizona); |
| 328 | if (ret != 0) { |
| 329 | dev_err(arizona->dev, |
| 330 | "Failed to request CTRLIF_ERR %d: %d\n", |
| 331 | arizona->irq, ret); |
| 332 | goto err_ctrlif; |
| 333 | } |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Mark Brown | 3092f80 | 2013-03-24 23:05:58 +0000 | [diff] [blame] | 336 | /* Used to emulate edge trigger and to work around broken pinmux */ |
| 337 | if (arizona->pdata.irq_gpio) { |
| 338 | if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) { |
| 339 | dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n", |
| 340 | arizona->irq, arizona->pdata.irq_gpio, |
| 341 | gpio_to_irq(arizona->pdata.irq_gpio)); |
| 342 | arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio); |
| 343 | } |
| 344 | |
| 345 | ret = devm_gpio_request_one(arizona->dev, |
| 346 | arizona->pdata.irq_gpio, |
| 347 | GPIOF_IN, "arizona IRQ"); |
| 348 | if (ret != 0) { |
| 349 | dev_err(arizona->dev, |
| 350 | "Failed to request IRQ GPIO %d:: %d\n", |
| 351 | arizona->pdata.irq_gpio, ret); |
| 352 | arizona->pdata.irq_gpio = 0; |
| 353 | } |
| 354 | } |
| 355 | |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 356 | ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread, |
| 357 | flags, "arizona", arizona); |
| 358 | |
| 359 | if (ret != 0) { |
Mark Brown | 7994c66 | 2013-03-19 09:51:14 +0000 | [diff] [blame] | 360 | dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n", |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 361 | arizona->irq, ret); |
| 362 | goto err_main_irq; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | |
| 367 | err_main_irq: |
Charles Keepax | 30a2af3 | 2014-07-15 11:21:50 +0100 | [diff] [blame] | 368 | if (arizona->ctrlif_error) |
| 369 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), |
| 370 | arizona); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 371 | err_ctrlif: |
| 372 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); |
| 373 | err_boot_done: |
| 374 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), |
| 375 | arizona->irq_chip); |
| 376 | err_aod: |
| 377 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), |
| 378 | arizona->aod_irq_chip); |
| 379 | err_domain: |
| 380 | err: |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | int arizona_irq_exit(struct arizona *arizona) |
| 385 | { |
Charles Keepax | 30a2af3 | 2014-07-15 11:21:50 +0100 | [diff] [blame] | 386 | if (arizona->ctrlif_error) |
| 387 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), |
| 388 | arizona); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 389 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); |
| 390 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), |
| 391 | arizona->irq_chip); |
| 392 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), |
| 393 | arizona->aod_irq_chip); |
| 394 | free_irq(arizona->irq, arizona); |
| 395 | |
| 396 | return 0; |
| 397 | } |