4 * Author: zhangqing <zhangqing@rock-chips.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/bug.h>
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/gpio.h>
21 #include <linux/mfd/rk808.h>
22 #include <linux/wakelock.h>
23 #include <linux/kthread.h>
25 static inline int irq_to_rk808_irq(struct rk808 *rk808,
28 return (irq - rk808->irq_base);
32 * This is a threaded IRQ handler so can access I2C/SPI. Since all
33 * interrupts are clear on read the IRQ line will be reasserted and
34 * the physical IRQ will be handled again if another interrupt is
35 * asserted while we run - in the normal course of events this is a
36 * rare occurrence so we save I2C/SPI reads. We're also assuming that
37 * it's rare to get lots of interrupts firing simultaneously so try to
40 static irqreturn_t rk808_irq(int irq, void *irq_data)
42 struct rk808 *rk808 = irq_data;
47 //printk(" rk808 irq %d \n",irq);
48 wake_lock(&rk808->irq_wake);
49 rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, ®);
51 rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, ®);
54 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, ®);
56 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, ®);
63 wake_unlock(&rk808->irq_wake);
67 for (i = 0; i < rk808->irq_num; i++) {
69 if (!(irq_sts & (1 << i)))
72 handle_nested_irq(rk808->irq_base + i);
75 /* Write the STS register back to clear IRQs we handled */
78 rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
80 rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
81 wake_unlock(&rk808->irq_wake);
85 static void rk808_irq_lock(struct irq_data *data)
87 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
89 mutex_lock(&rk808->irq_lock);
92 static void rk808_irq_sync_unlock(struct irq_data *data)
94 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
98 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, ®);
100 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, ®);
101 reg_mask |= reg << 8;
103 if (rk808->irq_mask != reg_mask) {
104 reg = rk808->irq_mask & 0xff;
105 // rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG1, 1, reg);
106 reg = rk808->irq_mask >> 8 & 0xff;
107 // rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG2, 1, reg);
109 mutex_unlock(&rk808->irq_lock);
112 static void rk808_irq_enable(struct irq_data *data)
114 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
116 rk808->irq_mask &= ~( 1 << irq_to_rk808_irq(rk808, data->irq));
119 static void rk808_irq_disable(struct irq_data *data)
121 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
123 rk808->irq_mask |= ( 1 << irq_to_rk808_irq(rk808, data->irq));
126 #ifdef CONFIG_PM_SLEEP
127 static int rk808_irq_set_wake(struct irq_data *data, unsigned int enable)
129 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
130 return irq_set_irq_wake(rk808->chip_irq, enable);
133 #define rk808_irq_set_wake NULL
136 static struct irq_chip rk808_irq_chip = {
138 .irq_bus_lock = rk808_irq_lock,
139 .irq_bus_sync_unlock = rk808_irq_sync_unlock,
140 .irq_disable = rk808_irq_disable,
141 .irq_enable = rk808_irq_enable,
142 .irq_set_wake = rk808_irq_set_wake,
145 int rk808_irq_init(struct rk808 *rk808, int irq,struct rk808_platform_data *pdata)
148 int flags = IRQF_ONESHOT;
151 printk("%s,line=%d\n", __func__,__LINE__);
155 dev_warn(rk808->dev, "No interrupt support, no core IRQ\n");
159 if (!pdata || !pdata->irq_base) {
160 dev_warn(rk808->dev, "No interrupt support, no IRQ base\n");
164 /* Clear unattended interrupts */
165 rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, ®);
166 rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
167 rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, ®);
168 rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
169 rk808_i2c_read(rk808, RK808_RTC_STATUS_REG, 1, ®);
170 rk808_i2c_write(rk808, RK808_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
172 /* Mask top level interrupts */
173 rk808->irq_mask = 0xFFFFFF;
175 mutex_init(&rk808->irq_lock);
176 wake_lock_init(&rk808->irq_wake, WAKE_LOCK_SUSPEND, "rk808_irq_wake");
177 rk808->chip_irq = irq;
178 rk808->irq_base = pdata->irq_base;
180 rk808->irq_num = RK808_NUM_IRQ;
182 /* Register with genirq */
183 for (cur_irq = rk808->irq_base;
184 cur_irq < rk808->irq_num + rk808->irq_base;
186 irq_set_chip_data(cur_irq, rk808);
187 irq_set_chip_and_handler(cur_irq, &rk808_irq_chip,
189 irq_set_nested_thread(cur_irq, 1);
191 /* ARM needs us to explicitly flag the IRQ as valid
192 * and will set them noprobe when we do so. */
194 set_irq_flags(cur_irq, IRQF_VALID);
196 irq_set_noprobe(cur_irq);
200 ret = request_threaded_irq(irq, NULL, rk808_irq, flags, "rk808", rk808);
202 irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
205 dev_err(rk808->dev, "Failed to request IRQ: %d\n", ret);
210 int rk808_irq_exit(struct rk808 *rk808)
213 free_irq(rk808->chip_irq, rk808);