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; 97 int i, ret; 98 99 ret = pm_runtime_get_sync(arizona->dev); 100 if (ret < 0) { 101 dev_err(arizona->dev, "Failed to resume device: %d\n", ret); 102 return IRQ_NONE; 103 } 104 105 /* Check both domains */ 106 for (i = 0; i < 2; i++) 107 handle_nested_irq(irq_find_mapping(arizona->virq, i)); 108 109 pm_runtime_mark_last_busy(arizona->dev); 110 pm_runtime_put_autosuspend(arizona->dev); 111 112 return IRQ_HANDLED; 113 } 114 115 static void arizona_irq_enable(struct irq_data *data) 116 { 117 } 118 119 static void arizona_irq_disable(struct irq_data *data) 120 { 121 } 122 123 static struct irq_chip arizona_irq_chip = { 124 .name = "arizona", 125 .irq_disable = arizona_irq_disable, 126 .irq_enable = arizona_irq_enable, 127 }; 128 129 static int arizona_irq_map(struct irq_domain *h, unsigned int virq, 130 irq_hw_number_t hw) 131 { 132 struct regmap_irq_chip_data *data = h->host_data; 133 134 irq_set_chip_data(virq, data); 135 irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_edge_irq); 136 irq_set_nested_thread(virq, 1); 137 138 /* ARM needs us to explicitly flag the IRQ as valid 139 * and will set them noprobe when we do so. */ 140 #ifdef CONFIG_ARM 141 set_irq_flags(virq, IRQF_VALID); 142 #else 143 irq_set_noprobe(virq); 144 #endif 145 146 return 0; 147 } 148 149 static struct irq_domain_ops arizona_domain_ops = { 150 .map = arizona_irq_map, 151 .xlate = irq_domain_xlate_twocell, 152 }; 153 154 int arizona_irq_init(struct arizona *arizona) 155 { 156 int flags = IRQF_ONESHOT; 157 int ret, i; 158 const struct regmap_irq_chip *aod, *irq; 159 160 switch (arizona->type) { 161 #ifdef CONFIG_MFD_WM5102 162 case WM5102: 163 aod = &wm5102_aod; 164 irq = &wm5102_irq; 165 break; 166 #endif 167 #ifdef CONFIG_MFD_WM5110 168 case WM5110: 169 aod = &wm5110_aod; 170 irq = &wm5110_irq; 171 break; 172 #endif 173 default: 174 BUG_ON("Unknown Arizona class device" == NULL); 175 return -EINVAL; 176 } 177 178 if (arizona->pdata.irq_active_high) { 179 ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1, 180 ARIZONA_IRQ_POL, 0); 181 if (ret != 0) { 182 dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n", 183 ret); 184 goto err; 185 } 186 187 flags |= IRQF_TRIGGER_HIGH; 188 } else { 189 flags |= IRQF_TRIGGER_LOW; 190 } 191 192 /* Allocate a virtual IRQ domain to distribute to the regmap domains */ 193 arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops, 194 arizona); 195 if (!arizona->virq) { 196 ret = -EINVAL; 197 goto err; 198 } 199 200 ret = regmap_add_irq_chip(arizona->regmap, 201 irq_create_mapping(arizona->virq, 0), 202 IRQF_ONESHOT, -1, aod, 203 &arizona->aod_irq_chip); 204 if (ret != 0) { 205 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret); 206 goto err_domain; 207 } 208 209 ret = regmap_add_irq_chip(arizona->regmap, 210 irq_create_mapping(arizona->virq, 1), 211 IRQF_ONESHOT, -1, irq, 212 &arizona->irq_chip); 213 if (ret != 0) { 214 dev_err(arizona->dev, "Failed to add AOD IRQs: %d\n", ret); 215 goto err_aod; 216 } 217 218 /* Make sure the boot done IRQ is unmasked for resumes */ 219 i = arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE); 220 ret = request_threaded_irq(i, NULL, arizona_boot_done, IRQF_ONESHOT, 221 "Boot done", arizona); 222 if (ret != 0) { 223 dev_err(arizona->dev, "Failed to request boot done %d: %d\n", 224 arizona->irq, ret); 225 goto err_boot_done; 226 } 227 228 /* Handle control interface errors in the core */ 229 i = arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR); 230 ret = request_threaded_irq(i, NULL, arizona_ctrlif_err, IRQF_ONESHOT, 231 "Control interface error", arizona); 232 if (ret != 0) { 233 dev_err(arizona->dev, "Failed to request boot done %d: %d\n", 234 arizona->irq, ret); 235 goto err_ctrlif; 236 } 237 238 ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread, 239 flags, "arizona", arizona); 240 241 if (ret != 0) { 242 dev_err(arizona->dev, "Failed to request IRQ %d: %d\n", 243 arizona->irq, ret); 244 goto err_main_irq; 245 } 246 247 return 0; 248 249 err_main_irq: 250 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona); 251 err_ctrlif: 252 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); 253 err_boot_done: 254 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), 255 arizona->irq_chip); 256 err_aod: 257 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), 258 arizona->aod_irq_chip); 259 err_domain: 260 err: 261 return ret; 262 } 263 264 int arizona_irq_exit(struct arizona *arizona) 265 { 266 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR), arizona); 267 free_irq(arizona_map_irq(arizona, ARIZONA_IRQ_BOOT_DONE), arizona); 268 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 1), 269 arizona->irq_chip); 270 regmap_del_irq_chip(irq_create_mapping(arizona->virq, 0), 271 arizona->aod_irq_chip); 272 free_irq(arizona->irq, arizona); 273 274 return 0; 275 } 276