···11+/*22+ * regmap based irq_chip33+ *44+ * Copyright 2011 Wolfson Microelectronics plc55+ *66+ * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>77+ *88+ * This program is free software; you can redistribute it and/or modify99+ * it under the terms of the GNU General Public License version 2 as1010+ * published by the Free Software Foundation.1111+ */1212+1313+#include <linux/export.h>1414+#include <linux/regmap.h>1515+#include <linux/irq.h>1616+#include <linux/interrupt.h>1717+#include <linux/slab.h>1818+1919+#include "internal.h"2020+2121+struct regmap_irq_chip_data {2222+ struct mutex lock;2323+2424+ struct regmap *map;2525+ struct regmap_irq_chip *chip;2626+2727+ int irq_base;2828+2929+ void *status_reg_buf;3030+ unsigned int *status_buf;3131+ unsigned int *mask_buf;3232+ unsigned int *mask_buf_def;3333+};3434+3535+static inline const3636+struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,3737+ int irq)3838+{3939+ return &data->chip->irqs[irq - data->irq_base];4040+}4141+4242+static void regmap_irq_lock(struct irq_data *data)4343+{4444+ struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);4545+4646+ mutex_lock(&d->lock);4747+}4848+4949+static void regmap_irq_sync_unlock(struct irq_data *data)5050+{5151+ struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);5252+ int i, ret;5353+5454+ /*5555+ * If there's been a change in the mask write it back to the5656+ * hardware. We rely on the use of the regmap core cache to5757+ * suppress pointless writes.5858+ */5959+ for (i = 0; i < d->chip->num_regs; i++) {6060+ ret = regmap_update_bits(d->map, d->chip->mask_base + i,6161+ d->mask_buf_def[i], d->mask_buf[i]);6262+ if (ret != 0)6363+ dev_err(d->map->dev, "Failed to sync masks in %x\n",6464+ d->chip->mask_base + i);6565+ }6666+6767+ mutex_unlock(&d->lock);6868+}6969+7070+static void regmap_irq_enable(struct irq_data *data)7171+{7272+ struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);7373+ const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);7474+7575+ d->mask_buf[irq_data->reg_offset] &= ~irq_data->mask;7676+}7777+7878+static void regmap_irq_disable(struct irq_data *data)7979+{8080+ struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);8181+ const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);8282+8383+ d->mask_buf[irq_data->reg_offset] |= irq_data->mask;8484+}8585+8686+static struct irq_chip regmap_irq_chip = {8787+ .name = "regmap",8888+ .irq_bus_lock = regmap_irq_lock,8989+ .irq_bus_sync_unlock = regmap_irq_sync_unlock,9090+ .irq_disable = regmap_irq_disable,9191+ .irq_enable = regmap_irq_enable,9292+};9393+9494+static irqreturn_t regmap_irq_thread(int irq, void *d)9595+{9696+ struct regmap_irq_chip_data *data = d;9797+ struct regmap_irq_chip *chip = data->chip;9898+ struct regmap *map = data->map;9999+ int ret, i;100100+ u8 *buf8 = data->status_reg_buf;101101+ u16 *buf16 = data->status_reg_buf;102102+ u32 *buf32 = data->status_reg_buf;103103+ bool handled = false;104104+105105+ ret = regmap_bulk_read(map, chip->status_base, data->status_reg_buf,106106+ chip->num_regs);107107+ if (ret != 0) {108108+ dev_err(map->dev, "Failed to read IRQ status: %d\n", ret);109109+ return IRQ_NONE;110110+ }111111+112112+ /*113113+ * Ignore masked IRQs and ack if we need to; we ack early so114114+ * there is no race between handling and acknowleding the115115+ * interrupt. We assume that typically few of the interrupts116116+ * will fire simultaneously so don't worry about overhead from117117+ * doing a write per register.118118+ */119119+ for (i = 0; i < data->chip->num_regs; i++) {120120+ switch (map->format.val_bytes) {121121+ case 1:122122+ data->status_buf[i] = buf8[i];123123+ break;124124+ case 2:125125+ data->status_buf[i] = buf16[i];126126+ break;127127+ case 4:128128+ data->status_buf[i] = buf32[i];129129+ break;130130+ default:131131+ BUG();132132+ return IRQ_NONE;133133+ }134134+135135+ data->status_buf[i] &= ~data->mask_buf[i];136136+137137+ if (data->status_buf[i] && chip->ack_base) {138138+ ret = regmap_write(map, chip->ack_base + i,139139+ data->status_buf[i]);140140+ if (ret != 0)141141+ dev_err(map->dev, "Failed to ack 0x%x: %d\n",142142+ chip->ack_base + i, ret);143143+ }144144+ }145145+146146+ for (i = 0; i < chip->num_irqs; i++) {147147+ if (data->status_buf[chip->irqs[i].reg_offset] &148148+ chip->irqs[i].mask) {149149+ handle_nested_irq(data->irq_base + i);150150+ handled = true;151151+ }152152+ }153153+154154+ if (handled)155155+ return IRQ_HANDLED;156156+ else157157+ return IRQ_NONE;158158+}159159+160160+/**161161+ * regmap_add_irq_chip(): Use standard regmap IRQ controller handling162162+ *163163+ * map: The regmap for the device.164164+ * irq: The IRQ the device uses to signal interrupts165165+ * irq_flags: The IRQF_ flags to use for the primary interrupt.166166+ * chip: Configuration for the interrupt controller.167167+ * data: Runtime data structure for the controller, allocated on success168168+ *169169+ * Returns 0 on success or an errno on failure.170170+ *171171+ * In order for this to be efficient the chip really should use a172172+ * register cache. The chip driver is responsible for restoring the173173+ * register values used by the IRQ controller over suspend and resume.174174+ */175175+int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,176176+ int irq_base, struct regmap_irq_chip *chip,177177+ struct regmap_irq_chip_data **data)178178+{179179+ struct regmap_irq_chip_data *d;180180+ int cur_irq, i;181181+ int ret = -ENOMEM;182182+183183+ irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);184184+ if (irq_base < 0) {185185+ dev_warn(map->dev, "Failed to allocate IRQs: %d\n",186186+ irq_base);187187+ return irq_base;188188+ }189189+190190+ d = kzalloc(sizeof(*d), GFP_KERNEL);191191+ if (!d)192192+ return -ENOMEM;193193+194194+ d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,195195+ GFP_KERNEL);196196+ if (!d->status_buf)197197+ goto err_alloc;198198+199199+ d->status_reg_buf = kzalloc(map->format.val_bytes * chip->num_regs,200200+ GFP_KERNEL);201201+ if (!d->status_reg_buf)202202+ goto err_alloc;203203+204204+ d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,205205+ GFP_KERNEL);206206+ if (!d->mask_buf)207207+ goto err_alloc;208208+209209+ d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,210210+ GFP_KERNEL);211211+ if (!d->mask_buf_def)212212+ goto err_alloc;213213+214214+ d->map = map;215215+ d->chip = chip;216216+ d->irq_base = irq_base;217217+ mutex_init(&d->lock);218218+219219+ for (i = 0; i < chip->num_irqs; i++)220220+ d->mask_buf_def[chip->irqs[i].reg_offset]221221+ |= chip->irqs[i].mask;222222+223223+ /* Mask all the interrupts by default */224224+ for (i = 0; i < chip->num_regs; i++) {225225+ d->mask_buf[i] = d->mask_buf_def[i];226226+ ret = regmap_write(map, chip->mask_base + i, d->mask_buf[i]);227227+ if (ret != 0) {228228+ dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",229229+ chip->mask_base + i, ret);230230+ goto err_alloc;231231+ }232232+ }233233+234234+ /* Register them with genirq */235235+ for (cur_irq = irq_base;236236+ cur_irq < chip->num_irqs + irq_base;237237+ cur_irq++) {238238+ irq_set_chip_data(cur_irq, d);239239+ irq_set_chip_and_handler(cur_irq, ®map_irq_chip,240240+ handle_edge_irq);241241+ irq_set_nested_thread(cur_irq, 1);242242+243243+ /* ARM needs us to explicitly flag the IRQ as valid244244+ * and will set them noprobe when we do so. */245245+#ifdef CONFIG_ARM246246+ set_irq_flags(cur_irq, IRQF_VALID);247247+#else248248+ irq_set_noprobe(cur_irq);249249+#endif250250+ }251251+252252+ ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,253253+ chip->name, d);254254+ if (ret != 0) {255255+ dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);256256+ goto err_alloc;257257+ }258258+259259+ return 0;260260+261261+err_alloc:262262+ kfree(d->mask_buf_def);263263+ kfree(d->mask_buf);264264+ kfree(d->status_reg_buf);265265+ kfree(d->status_buf);266266+ kfree(d);267267+ return ret;268268+}269269+EXPORT_SYMBOL_GPL(regmap_add_irq_chip);270270+271271+/**272272+ * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip273273+ *274274+ * @irq: Primary IRQ for the device275275+ * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()276276+ */277277+void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)278278+{279279+ if (!d)280280+ return;281281+282282+ free_irq(irq, d);283283+ kfree(d->mask_buf_def);284284+ kfree(d->mask_buf);285285+ kfree(d->status_reg_buf);286286+ kfree(d->status_buf);287287+ kfree(d);288288+}289289+EXPORT_SYMBOL_GPL(regmap_del_irq_chip);290290+291291+/**292292+ * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip293293+ *294294+ * Useful for drivers to request their own IRQs.295295+ *296296+ * @data: regmap_irq controller to operate on.297297+ */298298+int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)299299+{300300+ return data->irq_base;301301+}302302+EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
+48
include/linux/regmap.h
···144144void regcache_cache_only(struct regmap *map, bool enable);145145void regcache_cache_bypass(struct regmap *map, bool enable);146146147147+/**148148+ * Description of an IRQ for the generic regmap irq_chip.149149+ *150150+ * @reg_offset: Offset of the status/mask register within the bank151151+ * @mask: Mask used to flag/control the register.152152+ */153153+struct regmap_irq {154154+ unsigned int reg_offset;155155+ unsigned int mask;156156+};157157+158158+/**159159+ * Description of a generic regmap irq_chip. This is not intended to160160+ * handle every possible interrupt controller, but it should handle a161161+ * substantial proportion of those that are found in the wild.162162+ *163163+ * @name: Descriptive name for IRQ controller.164164+ *165165+ * @status_base: Base status register address.166166+ * @mask_base: Base mask register address.167167+ * @ack_base: Base ack address. If zero then the chip is clear on read.168168+ *169169+ * @num_regs: Number of registers in each control bank.170170+ * @irqs: Descriptors for individual IRQs. Interrupt numbers are171171+ * assigned based on the index in the array of the interrupt.172172+ * @num_irqs: Number of descriptors.173173+ */174174+struct regmap_irq_chip {175175+ const char *name;176176+177177+ unsigned int status_base;178178+ unsigned int mask_base;179179+ unsigned int ack_base;180180+181181+ int num_regs;182182+183183+ const struct regmap_irq *irqs;184184+ int num_irqs;185185+};186186+187187+struct regmap_irq_chip_data;188188+189189+int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,190190+ int irq_base, struct regmap_irq_chip *chip,191191+ struct regmap_irq_chip_data **data);192192+void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);193193+int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);194194+147195#endif