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/rk818.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_rk818_irq(struct rk818 *rk818,
31 return (irq - rk818->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 rk818_irq(int irq, void *irq_data)
45 struct rk818 *rk818 = irq_data;
50 //printk(" rk818 irq %d \n",irq);
51 wake_lock(&rk818->irq_wake);
52 rk818_i2c_read(rk818, RK818_INT_STS_REG1, 1, ®);
54 rk818_i2c_read(rk818, RK818_INT_STS_REG2, 1, ®);
57 rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG1, 1, ®);
59 rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG2, 1, ®);
66 wake_unlock(&rk818->irq_wake);
70 for (i = 0; i < rk818->irq_num; i++) {
72 if (!(irq_sts & (1 << i)))
75 handle_nested_irq(rk818->irq_base + i);
78 /* Write the STS register back to clear IRQs we handled */
81 rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
83 rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
84 wake_unlock(&rk818->irq_wake);
88 static void rk818_irq_lock(struct irq_data *data)
90 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
92 mutex_lock(&rk818->irq_lock);
95 static void rk818_irq_sync_unlock(struct irq_data *data)
97 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
101 rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG1, 1, ®);
103 rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG2, 1, ®);
104 reg_mask |= reg << 8;
106 if (rk818->irq_mask != reg_mask) {
107 reg = rk818->irq_mask & 0xff;
108 // rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG1, 1, reg);
109 reg = rk818->irq_mask >> 8 & 0xff;
110 // rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG2, 1, reg);
112 mutex_unlock(&rk818->irq_lock);
115 static void rk818_irq_enable(struct irq_data *data)
117 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
119 rk818->irq_mask &= ~( 1 << irq_to_rk818_irq(rk818, data->irq));
122 static void rk818_irq_disable(struct irq_data *data)
124 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
126 rk818->irq_mask |= ( 1 << irq_to_rk818_irq(rk818, data->irq));
129 #ifdef CONFIG_PM_SLEEP
130 static int rk818_irq_set_wake(struct irq_data *data, unsigned int enable)
132 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
133 return irq_set_irq_wake(rk818->chip_irq, enable);
136 #define rk818_irq_set_wake NULL
139 static struct irq_chip rk818_irq_chip = {
141 .irq_bus_lock = rk818_irq_lock,
142 .irq_bus_sync_unlock = rk818_irq_sync_unlock,
143 .irq_disable = rk818_irq_disable,
144 .irq_enable = rk818_irq_enable,
145 .irq_set_wake = rk818_irq_set_wake,
148 static int rk818_irq_domain_map(struct irq_domain *d, unsigned int irq,
151 struct rk818 *rk818 = d->host_data;
153 irq_set_chip_data(irq, rk818);
154 irq_set_chip_and_handler(irq, &rk818_irq_chip, handle_edge_irq);
155 irq_set_nested_thread(irq, 1);
157 set_irq_flags(irq, IRQF_VALID);
159 irq_set_noprobe(irq);
164 static struct irq_domain_ops rk818_irq_domain_ops = {
165 .map = rk818_irq_domain_map,
168 int rk818_irq_init(struct rk818 *rk818, int irq,struct rk818_board *pdata)
170 struct irq_domain *domain;
171 int ret,val,irq_type,flags;
174 // printk("%s,line=%d\n", __func__,__LINE__);
176 dev_warn(rk818->dev, "No interrupt support, no core IRQ\n");
180 /* Clear unattended interrupts */
181 rk818_i2c_read(rk818, RK818_INT_STS_REG1, 1, ®);
182 rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
183 rk818_i2c_read(rk818, RK818_INT_STS_REG2, 1, ®);
184 rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
185 rk818_i2c_read(rk818, RK818_RTC_STATUS_REG, 1, ®);
186 rk818_i2c_write(rk818, RK818_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
188 /* Mask top level interrupts */
189 rk818->irq_mask = 0xFFFFFF;
190 mutex_init(&rk818->irq_lock);
191 wake_lock_init(&rk818->irq_wake, WAKE_LOCK_SUSPEND, "rk818_irq_wake");
192 rk818->irq_num = RK818_NUM_IRQ;
193 rk818->irq_gpio = pdata->irq_gpio;
194 if (rk818->irq_gpio && !rk818->chip_irq) {
195 rk818->chip_irq = gpio_to_irq(rk818->irq_gpio);
197 if (rk818->irq_gpio) {
198 ret = gpio_request(rk818->irq_gpio, "rk818_pmic_irq");
201 "Failed to request gpio %d with ret:"
202 "%d\n", rk818->irq_gpio, ret);
205 gpio_direction_input(rk818->irq_gpio);
206 val = gpio_get_value(rk818->irq_gpio);
208 irq_type = IRQ_TYPE_LEVEL_LOW;
209 flags = IRQF_TRIGGER_FALLING;
212 irq_type = IRQ_TYPE_LEVEL_HIGH;
213 flags = IRQF_TRIGGER_RISING;
215 gpio_free(rk818->irq_gpio);
216 pr_info("%s: rk818_pmic_irq=%x\n", __func__, val);
220 domain = irq_domain_add_linear(NULL, RK818_NUM_IRQ,
221 &rk818_irq_domain_ops, rk818);
223 dev_err(rk818->dev, "could not create irq domain\n");
226 rk818->irq_domain = domain;
228 ret = request_threaded_irq(rk818->chip_irq, NULL, rk818_irq, flags | IRQF_ONESHOT, "rk818", rk818);
230 irq_set_irq_type(rk818->chip_irq, irq_type);
231 enable_irq_wake(rk818->chip_irq);
233 dev_err(rk818->dev, "Failed to request IRQ: %d\n", ret);
238 int rk818_irq_exit(struct rk818 *rk818)
241 free_irq(rk818->chip_irq, rk818);