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 | 3080de4 | 2012-08-21 20:02:03 +0100 | [diff] [blame] | 97 | unsigned int val; |
Mark Brown | cdabc1c | 2012-09-08 09:00:59 +0800 | [diff] [blame] | 98 | int ret; |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 99 | |
| 100 | ret = pm_runtime_get_sync(arizona->dev); |
| 101 | if (ret < 0) { |
| 102 | dev_err(arizona->dev, "Failed to resume device: %d\n", ret); |
| 103 | return IRQ_NONE; |
| 104 | } |
| 105 | |
Mark Brown | 3080de4 | 2012-08-21 20:02:03 +0100 | [diff] [blame] | 106 | /* Always handle the AoD domain */ |
| 107 | handle_nested_irq(irq_find_mapping(arizona->virq, 0)); |
| 108 | |
| 109 | /* |
| 110 | * Check if one of the main interrupts is asserted and only |
| 111 | * check that domain if it is. |
| 112 | */ |
| 113 | ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS, &val); |
| 114 | if (ret == 0 && val & ARIZONA_IRQ1_STS) { |
| 115 | handle_nested_irq(irq_find_mapping(arizona->virq, 1)); |
| 116 | } else if (ret != 0) { |
| 117 | dev_err(arizona->dev, "Failed to read main IRQ status: %d\n", |
| 118 | ret); |
| 119 | } |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 120 | |
| 121 | pm_runtime_mark_last_busy(arizona->dev); |
| 122 | pm_runtime_put_autosuspend(arizona->dev); |
| 123 | |
| 124 | return IRQ_HANDLED; |
| 125 | } |
| 126 | |
| 127 | static void arizona_irq_enable(struct irq_data *data) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | static void arizona_irq_disable(struct irq_data *data) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | static struct irq_chip arizona_irq_chip = { |
| 136 | .name = "arizona", |
| 137 | .irq_disable = arizona_irq_disable, |
| 138 | .irq_enable = arizona_irq_enable, |
| 139 | }; |
| 140 | |
| 141 | static int arizona_irq_map(struct irq_domain *h, unsigned int virq, |
| 142 | irq_hw_number_t hw) |
| 143 | { |
| 144 | struct regmap_irq_chip_data *data = h->host_data; |
| 145 | |
| 146 | irq_set_chip_data(virq, data); |
| 147 | irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_edge_irq); |
| 148 | irq_set_nested_thread(virq, 1); |
| 149 | |
| 150 | /* ARM needs us to explicitly flag the IRQ as valid |
| 151 | * and will set them noprobe when we do so. */ |
| 152 | #ifdef CONFIG_ARM |
| 153 | set_irq_flags(virq, IRQF_VALID); |
| 154 | #else |
| 155 | irq_set_noprobe(virq); |
| 156 | #endif |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static struct irq_domain_ops arizona_domain_ops = { |
| 162 | .map = arizona_irq_map, |
| 163 | .xlate = irq_domain_xlate_twocell, |
| 164 | }; |
| 165 | |
| 166 | int arizona_irq_init(struct arizona *arizona) |
| 167 | { |
| 168 | int flags = IRQF_ONESHOT; |
| 169 | int ret, i; |
| 170 | const struct regmap_irq_chip *aod, *irq; |
Mark Brown | 92d8013 | 2012-08-07 19:57:53 +0100 | [diff] [blame] | 171 | bool ctrlif_error = true; |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 172 | |
| 173 | switch (arizona->type) { |
Mark Brown | 863df8d | 2012-07-05 20:35:31 +0100 | [diff] [blame] | 174 | #ifdef CONFIG_MFD_WM5102 |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 175 | case WM5102: |
| 176 | aod = &wm5102_aod; |
| 177 | irq = &wm5102_irq; |
Mark Brown | 92d8013 | 2012-08-07 19:57:53 +0100 | [diff] [blame] | 178 | |
Mark Brown | e1bfe75 | 2012-12-05 11:46:04 +0900 | [diff] [blame] | 179 | ctrlif_error = false; |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 180 | break; |
Mark Brown | 863df8d | 2012-07-05 20:35:31 +0100 | [diff] [blame] | 181 | #endif |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 182 | #ifdef CONFIG_MFD_WM5110 |
| 183 | case WM5110: |
| 184 | aod = &wm5110_aod; |
| 185 | irq = &wm5110_irq; |
Mark Brown | 92d8013 | 2012-08-07 19:57:53 +0100 | [diff] [blame] | 186 | |
Mark Brown | e1bfe75 | 2012-12-05 11:46:04 +0900 | [diff] [blame] | 187 | ctrlif_error = false; |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 188 | break; |
| 189 | #endif |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 190 | default: |
| 191 | BUG_ON("Unknown Arizona class device" == NULL); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
Mark Brown | 1816cb3 | 2013-01-17 16:54:18 +0900 | [diff] [blame^] | 195 | /* Disable all wake sources by default */ |
| 196 | regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0); |
| 197 | |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 198 | if (arizona->pdata.irq_active_high) { |
| 199 | ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1, |
| 200 | ARIZONA_IRQ_POL, 0); |
| 201 | if (ret != 0) { |
| 202 | dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n", |
| 203 | ret); |
| 204 | goto err; |
| 205 | } |
| 206 | |
| 207 | flags |= IRQF_TRIGGER_HIGH; |
| 208 | } else { |
| 209 | flags |= IRQF_TRIGGER_LOW; |
| 210 | } |
| 211 | |
| 212 | /* Allocate a virtual IRQ domain to distribute to the regmap domains */ |
| 213 | arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops, |
| 214 | arizona); |
| 215 | if (!arizona->virq) { |
Mark Brown | b7dea5d | 2012-12-05 11:46:26 +0900 | [diff] [blame] | 216 | dev_err(arizona->dev, "Failed to add core IRQ domain\n"); |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 217 | ret = -EINVAL; |
| 218 | goto err; |
| 219 | } |
| 220 | |
| 221 | ret = regmap_add_irq_chip(arizona->regmap, |
| 222 | irq_create_mapping(arizona->virq, 0), |
| 223 | IRQF_ONESHOT, -1, aod, |
| 224 | &arizona->aod_irq_chip); |
| 225 | if (ret != 0) { |
| 226 | dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret); |
| 227 | goto err_domain; |
| 228 | } |
| 229 | |
| 230 | ret = regmap_add_irq_chip(arizona->regmap, |
| 231 | irq_create_mapping(arizona->virq, 1), |
| 232 | IRQF_ONESHOT, -1, irq, |
| 233 | &arizona->irq_chip); |
| 234 | if (ret != 0) { |
| 235 | dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret); |
| 236 | goto err_aod; |
| 237 | } |
| 238 | |
| 239 | /* Make sure the boot done IRQ is unmasked for resumes */ |
| 240 | i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE); |
| 241 | ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT, |
| 242 | "Boot done", arizona); |
| 243 | if (ret != 0) { |
| 244 | dev_err(arizona->dev, "Failed to request boot done %d: %d\n", |
| 245 | arizona->irq, ret); |
| 246 | goto err_boot_done; |
| 247 | } |
| 248 | |
| 249 | /* Handle control interface errors in the core */ |
Mark Brown | 92d8013 | 2012-08-07 19:57:53 +0100 | [diff] [blame] | 250 | if (ctrlif_error) { |
| 251 | i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR); |
| 252 | ret = request_threaded_irq(i, NULL, arizona_ctrlif_err, |
| 253 | IRQF_ONESHOT, |
| 254 | "Control interface error", arizona); |
| 255 | if (ret != 0) { |
| 256 | dev_err(arizona->dev, |
| 257 | "Failed to request CTRLIF_ERR %d: %d\n", |
| 258 | arizona->irq, ret); |
| 259 | goto err_ctrlif; |
| 260 | } |
Mark Brown | 966cdc9 | 2012-06-19 16:34:23 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread, |
| 264 | flags, "arizona", arizona); |
| 265 | |
| 266 | if (ret != 0) { |
| 267 | dev_err(arizona->dev, "Failed to request IRQ %d: %d\n", |
| 268 | arizona->irq, ret); |
| 269 | goto err_main_irq; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | |
| 274 | err_main_irq: |
| 275 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona); |
| 276 | err_ctrlif: |
| 277 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); |
| 278 | err_boot_done: |
| 279 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), |
| 280 | arizona->irq_chip); |
| 281 | err_aod: |
| 282 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), |
| 283 | arizona->aod_irq_chip); |
| 284 | err_domain: |
| 285 | err: |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | int arizona_irq_exit(struct arizona *arizona) |
| 290 | { |
| 291 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona); |
| 292 | free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); |
| 293 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), |
| 294 | arizona->irq_chip); |
| 295 | regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), |
| 296 | arizona->aod_irq_chip); |
| 297 | free_irq(arizona->irq, arizona); |
| 298 | |
| 299 | return 0; |
| 300 | } |