Milo Kim | eea6b7c | 2012-09-19 18:53:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * TI LP8788 MFD - interrupt handler |
| 3 | * |
| 4 | * Copyright 2012 Texas Instruments |
| 5 | * |
| 6 | * Author: Milo(Woogyom) Kim <milo.kim@ti.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 | |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/irqdomain.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/mfd/lp8788.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | |
| 24 | /* register address */ |
| 25 | #define LP8788_INT_1 0x00 |
| 26 | #define LP8788_INTEN_1 0x03 |
| 27 | |
| 28 | #define BASE_INTEN_ADDR LP8788_INTEN_1 |
| 29 | #define SIZE_REG 8 |
| 30 | #define NUM_REGS 3 |
| 31 | |
| 32 | /* |
| 33 | * struct lp8788_irq_data |
| 34 | * @lp : used for accessing to lp8788 registers |
| 35 | * @irq_lock : mutex for enabling/disabling the interrupt |
| 36 | * @domain : IRQ domain for handling nested interrupt |
| 37 | * @enabled : status of enabled interrupt |
| 38 | */ |
| 39 | struct lp8788_irq_data { |
| 40 | struct lp8788 *lp; |
| 41 | struct mutex irq_lock; |
| 42 | struct irq_domain *domain; |
| 43 | int enabled[LP8788_INT_MAX]; |
| 44 | }; |
| 45 | |
| 46 | static inline u8 _irq_to_addr(enum lp8788_int_id id) |
| 47 | { |
| 48 | return id / SIZE_REG; |
| 49 | } |
| 50 | |
| 51 | static inline u8 _irq_to_enable_addr(enum lp8788_int_id id) |
| 52 | { |
| 53 | return _irq_to_addr(id) + BASE_INTEN_ADDR; |
| 54 | } |
| 55 | |
| 56 | static inline u8 _irq_to_mask(enum lp8788_int_id id) |
| 57 | { |
| 58 | return 1 << (id % SIZE_REG); |
| 59 | } |
| 60 | |
| 61 | static inline u8 _irq_to_val(enum lp8788_int_id id, int enable) |
| 62 | { |
| 63 | return enable << (id % SIZE_REG); |
| 64 | } |
| 65 | |
| 66 | static void lp8788_irq_enable(struct irq_data *data) |
| 67 | { |
| 68 | struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data); |
Lee Jones | 52149ad | 2014-07-21 13:44:01 +0100 | [diff] [blame] | 69 | |
Milo Kim | eea6b7c | 2012-09-19 18:53:33 +0200 | [diff] [blame] | 70 | irqd->enabled[data->hwirq] = 1; |
| 71 | } |
| 72 | |
| 73 | static void lp8788_irq_disable(struct irq_data *data) |
| 74 | { |
| 75 | struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data); |
Lee Jones | 52149ad | 2014-07-21 13:44:01 +0100 | [diff] [blame] | 76 | |
Milo Kim | eea6b7c | 2012-09-19 18:53:33 +0200 | [diff] [blame] | 77 | irqd->enabled[data->hwirq] = 0; |
| 78 | } |
| 79 | |
| 80 | static void lp8788_irq_bus_lock(struct irq_data *data) |
| 81 | { |
| 82 | struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data); |
| 83 | |
| 84 | mutex_lock(&irqd->irq_lock); |
| 85 | } |
| 86 | |
| 87 | static void lp8788_irq_bus_sync_unlock(struct irq_data *data) |
| 88 | { |
| 89 | struct lp8788_irq_data *irqd = irq_data_get_irq_chip_data(data); |
| 90 | enum lp8788_int_id irq = data->hwirq; |
| 91 | u8 addr, mask, val; |
| 92 | |
| 93 | addr = _irq_to_enable_addr(irq); |
| 94 | mask = _irq_to_mask(irq); |
| 95 | val = _irq_to_val(irq, irqd->enabled[irq]); |
| 96 | |
| 97 | lp8788_update_bits(irqd->lp, addr, mask, val); |
| 98 | |
| 99 | mutex_unlock(&irqd->irq_lock); |
| 100 | } |
| 101 | |
| 102 | static struct irq_chip lp8788_irq_chip = { |
| 103 | .name = "lp8788", |
| 104 | .irq_enable = lp8788_irq_enable, |
| 105 | .irq_disable = lp8788_irq_disable, |
| 106 | .irq_bus_lock = lp8788_irq_bus_lock, |
| 107 | .irq_bus_sync_unlock = lp8788_irq_bus_sync_unlock, |
| 108 | }; |
| 109 | |
| 110 | static irqreturn_t lp8788_irq_handler(int irq, void *ptr) |
| 111 | { |
| 112 | struct lp8788_irq_data *irqd = ptr; |
| 113 | struct lp8788 *lp = irqd->lp; |
| 114 | u8 status[NUM_REGS], addr, mask; |
| 115 | bool handled; |
| 116 | int i; |
| 117 | |
| 118 | if (lp8788_read_multi_bytes(lp, LP8788_INT_1, status, NUM_REGS)) |
| 119 | return IRQ_NONE; |
| 120 | |
| 121 | for (i = 0 ; i < LP8788_INT_MAX ; i++) { |
| 122 | addr = _irq_to_addr(i); |
| 123 | mask = _irq_to_mask(i); |
| 124 | |
| 125 | /* reporting only if the irq is enabled */ |
| 126 | if (status[addr] & mask) { |
| 127 | handle_nested_irq(irq_find_mapping(irqd->domain, i)); |
| 128 | handled = true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return handled ? IRQ_HANDLED : IRQ_NONE; |
| 133 | } |
| 134 | |
| 135 | static int lp8788_irq_map(struct irq_domain *d, unsigned int virq, |
| 136 | irq_hw_number_t hwirq) |
| 137 | { |
| 138 | struct lp8788_irq_data *irqd = d->host_data; |
| 139 | struct irq_chip *chip = &lp8788_irq_chip; |
| 140 | |
| 141 | irq_set_chip_data(virq, irqd); |
| 142 | irq_set_chip_and_handler(virq, chip, handle_edge_irq); |
| 143 | irq_set_nested_thread(virq, 1); |
| 144 | |
| 145 | #ifdef CONFIG_ARM |
| 146 | set_irq_flags(virq, IRQF_VALID); |
| 147 | #else |
| 148 | irq_set_noprobe(virq); |
| 149 | #endif |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static struct irq_domain_ops lp8788_domain_ops = { |
| 155 | .map = lp8788_irq_map, |
| 156 | }; |
| 157 | |
| 158 | int lp8788_irq_init(struct lp8788 *lp, int irq) |
| 159 | { |
| 160 | struct lp8788_irq_data *irqd; |
| 161 | int ret; |
| 162 | |
| 163 | if (irq <= 0) { |
| 164 | dev_warn(lp->dev, "invalid irq number: %d\n", irq); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | irqd = devm_kzalloc(lp->dev, sizeof(*irqd), GFP_KERNEL); |
| 169 | if (!irqd) |
| 170 | return -ENOMEM; |
| 171 | |
| 172 | irqd->lp = lp; |
| 173 | irqd->domain = irq_domain_add_linear(lp->dev->of_node, LP8788_INT_MAX, |
| 174 | &lp8788_domain_ops, irqd); |
| 175 | if (!irqd->domain) { |
| 176 | dev_err(lp->dev, "failed to add irq domain err\n"); |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
| 180 | lp->irqdm = irqd->domain; |
| 181 | mutex_init(&irqd->irq_lock); |
| 182 | |
| 183 | ret = request_threaded_irq(irq, NULL, lp8788_irq_handler, |
| 184 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 185 | "lp8788-irq", irqd); |
| 186 | if (ret) { |
| 187 | dev_err(lp->dev, "failed to create a thread for IRQ_N\n"); |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | lp->irq = irq; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | void lp8788_irq_exit(struct lp8788 *lp) |
| 197 | { |
| 198 | if (lp->irq) |
| 199 | free_irq(lp->irq, lp->irqdm); |
| 200 | } |