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