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>
24 #include <linux/irqdomain.h>
25 #include <linux/regmap.h>
28 static inline int irq_to_rk808_irq(struct rk808 *rk808,
31 return (irq - rk808->chip_irq);
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("%s,line=%d\n", __func__,__LINE__);
52 wake_lock(&rk808->irq_wake);
53 rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, ®);
55 rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, ®);
58 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, ®);
60 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, ®);
67 wake_unlock(&rk808->irq_wake);
71 for (i = 0; i < rk808->irq_num; i++) {
72 if (!(irq_sts & (1 << i)))
74 cur_irq = irq_find_mapping(rk808->irq_domain, i);
77 handle_nested_irq(cur_irq);
80 /* Write the STS register back to clear IRQs we handled */
83 rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
85 rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
86 wake_unlock(&rk808->irq_wake);
90 static void rk808_irq_lock(struct irq_data *data)
92 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
94 mutex_lock(&rk808->irq_lock);
97 static void rk808_irq_sync_unlock(struct irq_data *data)
99 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
103 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, ®);
105 rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, ®);
106 reg_mask |= reg << 8;
108 if (rk808->irq_mask != reg_mask) {
109 reg = rk808->irq_mask & 0xff;
110 // rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG1, 1, reg);
111 reg = rk808->irq_mask >> 8 & 0xff;
112 // rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG2, 1, reg);
114 mutex_unlock(&rk808->irq_lock);
117 static void rk808_irq_enable(struct irq_data *data)
119 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
121 rk808->irq_mask &= ~( 1 << irq_to_rk808_irq(rk808, data->irq));
124 static void rk808_irq_disable(struct irq_data *data)
126 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
128 rk808->irq_mask |= ( 1 << irq_to_rk808_irq(rk808, data->irq));
131 #ifdef CONFIG_PM_SLEEP
132 static int rk808_irq_set_wake(struct irq_data *data, unsigned int enable)
134 struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
135 return irq_set_irq_wake(rk808->chip_irq, enable);
138 #define rk808_irq_set_wake NULL
141 static struct irq_chip rk808_irq_chip = {
143 .irq_bus_lock = rk808_irq_lock,
144 .irq_bus_sync_unlock = rk808_irq_sync_unlock,
145 .irq_disable = rk808_irq_disable,
146 .irq_enable = rk808_irq_enable,
147 .irq_set_wake = rk808_irq_set_wake,
149 static int rk808_irq_domain_map(struct irq_domain *d, unsigned int irq,
152 struct rk808 *rk808 = d->host_data;
154 irq_set_chip_data(irq, rk808);
155 irq_set_chip_and_handler(irq, &rk808_irq_chip, handle_edge_irq);
156 irq_set_nested_thread(irq, 1);
158 set_irq_flags(irq, IRQF_VALID);
160 irq_set_noprobe(irq);
165 static struct irq_domain_ops rk808_irq_domain_ops = {
166 .map = rk808_irq_domain_map,
169 int rk808_irq_init(struct rk808 *rk808, int irq,struct rk808_board *pdata)
171 struct irq_domain *domain;
175 // printk("%s,line=%d\n", __func__,__LINE__);
177 dev_warn(rk808->dev, "No interrupt support, no core IRQ\n");
181 /* Clear unattended interrupts */
182 rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, ®);
183 rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
184 rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, ®);
185 rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
186 rk808_i2c_read(rk808, RK808_RTC_STATUS_REG, 1, ®);
187 rk808_i2c_write(rk808, RK808_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
189 /* Mask top level interrupts */
190 rk808->irq_mask = 0xFFFFFF;
191 mutex_init(&rk808->irq_lock);
192 wake_lock_init(&rk808->irq_wake, WAKE_LOCK_SUSPEND, "rk808_irq_wake");
193 rk808->irq_num = RK808_NUM_IRQ;
194 rk808->irq_gpio = pdata->irq_gpio;
195 if (rk808->irq_gpio && !rk808->chip_irq) {
196 rk808->chip_irq = gpio_to_irq(rk808->irq_gpio);
198 if (rk808->irq_gpio) {
199 ret = gpio_request(rk808->irq_gpio, "rk808_pmic_irq");
202 "Failed to request gpio %d with ret:"
203 "%d\n", rk808->irq_gpio, ret);
206 gpio_direction_input(rk808->irq_gpio);
207 val = gpio_get_value(rk808->irq_gpio);
208 gpio_free(rk808->irq_gpio);
209 pr_info("%s: rk808_pmic_irq=%x\n", __func__, val);
213 domain = irq_domain_add_linear(NULL, RK808_NUM_IRQ,
214 &rk808_irq_domain_ops, rk808);
216 dev_err(rk808->dev, "could not create irq domain\n");
219 rk808->irq_domain = domain;
221 ret = request_threaded_irq(rk808->chip_irq, NULL, rk808_irq, IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "rk808", rk808);
223 irq_set_irq_type(rk808->chip_irq, IRQ_TYPE_LEVEL_LOW);
226 dev_err(rk808->dev, "Failed to request IRQ: %d\n", ret);
231 int rk808_irq_exit(struct rk808 *rk808)
234 free_irq(rk808->chip_irq, rk808);