2 * rk808-irq.c -- TI TPS6591x
4 * Copyright 2010 Texas Instruments Inc.
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/bug.h>
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/gpio.h>
24 #include <linux/mfd/rk808.h>
25 #include <linux/wakelock.h>
26 #include <linux/kthread.h>
28 static inline int irq_to_rk808_irq(struct rk808 *rk808,
31 return (irq - rk808->irq_base);
35 * This is a threaded IRQ handler so can access I2C/SPI. Since all
36 * interrupts are clear on read the IRQ line will be reasserted and
37 * the physical IRQ will be handled again if another interrupt is
38 * asserted while we run - in the normal course of events this is a
39 * rare occurrence so we save I2C/SPI reads. We're also assuming that
40 * it's rare to get lots of interrupts firing simultaneously so try to
43 static irqreturn_t rk808_irq(int irq, void *irq_data)
45 struct rk808 *rk808 = irq_data;
50 //printk(" rk808 irq %d \n",irq);
51 wake_lock(&rk808->irq_wake);
52 rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, ®);
54 rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, ®);
57 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, ®);
59 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, ®);
66 wake_unlock(&rk808->irq_wake);
70 for (i = 0; i < rk808->irq_num; i++) {
72 if (!(irq_sts & (1 << i)))
75 handle_nested_irq(rk808->irq_base + i);
78 /* Write the STS register back to clear IRQs we handled */
81 rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
83 rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
84 wake_unlock(&rk808->irq_wake);
88 static void rk808_irq_lock(struct irq_data *data)
90 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
92 mutex_lock(&rk808->irq_lock);
95 static void rk808_irq_sync_unlock(struct irq_data *data)
97 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
101 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, ®);
103 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, ®);
104 reg_mask |= reg << 8;
106 if (rk808->irq_mask != reg_mask) {
107 reg = rk808->irq_mask & 0xff;
108 // rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG1, 1, reg);
109 reg = rk808->irq_mask >> 8 & 0xff;
110 // rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG2, 1, reg);
112 mutex_unlock(&rk808->irq_lock);
115 static void rk808_irq_enable(struct irq_data *data)
117 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
119 rk808->irq_mask &= ~( 1 << irq_to_rk808_irq(rk808, data->irq));
122 static void rk808_irq_disable(struct irq_data *data)
124 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
126 rk808->irq_mask |= ( 1 << irq_to_rk808_irq(rk808, data->irq));
129 #ifdef CONFIG_PM_SLEEP
130 static int rk808_irq_set_wake(struct irq_data *data, unsigned int enable)
132 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
133 return irq_set_irq_wake(rk808->chip_irq, enable);
136 #define rk808_irq_set_wake NULL
139 static struct irq_chip rk808_irq_chip = {
141 .irq_bus_lock = rk808_irq_lock,
142 .irq_bus_sync_unlock = rk808_irq_sync_unlock,
143 .irq_disable = rk808_irq_disable,
144 .irq_enable = rk808_irq_enable,
145 .irq_set_wake = rk808_irq_set_wake,
148 int rk808_irq_init(struct rk808 *rk808, int irq,struct rk808_platform_data *pdata)
151 int flags = IRQF_ONESHOT;
154 printk("%s,line=%d\n", __func__,__LINE__);
158 dev_warn(rk808->dev, "No interrupt support, no core IRQ\n");
162 if (!pdata || !pdata->irq_base) {
163 dev_warn(rk808->dev, "No interrupt support, no IRQ base\n");
167 /* Clear unattended interrupts */
168 rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, ®);
169 rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
170 rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, ®);
171 rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
172 rk808_i2c_read(rk808, RK808_RTC_STATUS_REG, 1, ®);
173 rk808_i2c_write(rk808, RK808_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
175 /* Mask top level interrupts */
176 rk808->irq_mask = 0xFFFFFF;
178 mutex_init(&rk808->irq_lock);
179 wake_lock_init(&rk808->irq_wake, WAKE_LOCK_SUSPEND, "rk808_irq_wake");
180 rk808->chip_irq = irq;
181 rk808->irq_base = pdata->irq_base;
183 rk808->irq_num = RK808_NUM_IRQ;
185 /* Register with genirq */
186 for (cur_irq = rk808->irq_base;
187 cur_irq < rk808->irq_num + rk808->irq_base;
189 irq_set_chip_data(cur_irq, rk808);
190 irq_set_chip_and_handler(cur_irq, &rk808_irq_chip,
192 irq_set_nested_thread(cur_irq, 1);
194 /* ARM needs us to explicitly flag the IRQ as valid
195 * and will set them noprobe when we do so. */
197 set_irq_flags(cur_irq, IRQF_VALID);
199 irq_set_noprobe(cur_irq);
203 ret = request_threaded_irq(irq, NULL, rk808_irq, flags, "rk808", rk808);
205 irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
208 dev_err(rk808->dev, "Failed to request IRQ: %d\n", ret);
213 int rk808_irq_exit(struct rk808 *rk808)
216 free_irq(rk808->chip_irq, rk808);