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 cur_irq = irq_find_mapping(rk818->irq_domain, i);
78 handle_nested_irq(cur_irq);
81 /* Write the STS register back to clear IRQs we handled */
84 rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
86 rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
87 wake_unlock(&rk818->irq_wake);
91 static void rk818_irq_lock(struct irq_data *data)
93 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
95 mutex_lock(&rk818->irq_lock);
98 static void rk818_irq_sync_unlock(struct irq_data *data)
100 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
104 rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG1, 1, ®);
106 rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG2, 1, ®);
107 reg_mask |= reg << 8;
109 if (rk818->irq_mask != reg_mask) {
110 reg = rk818->irq_mask & 0xff;
111 // rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG1, 1, reg);
112 reg = rk818->irq_mask >> 8 & 0xff;
113 // rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG2, 1, reg);
115 mutex_unlock(&rk818->irq_lock);
118 static void rk818_irq_enable(struct irq_data *data)
120 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
122 rk818->irq_mask &= ~( 1 << irq_to_rk818_irq(rk818, data->irq));
125 static void rk818_irq_disable(struct irq_data *data)
127 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
129 rk818->irq_mask |= ( 1 << irq_to_rk818_irq(rk818, data->irq));
132 #ifdef CONFIG_PM_SLEEP
133 static int rk818_irq_set_wake(struct irq_data *data, unsigned int enable)
135 struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
136 return irq_set_irq_wake(rk818->chip_irq, enable);
139 #define rk818_irq_set_wake NULL
142 static struct irq_chip rk818_irq_chip = {
144 .irq_bus_lock = rk818_irq_lock,
145 .irq_bus_sync_unlock = rk818_irq_sync_unlock,
146 .irq_disable = rk818_irq_disable,
147 .irq_enable = rk818_irq_enable,
148 .irq_set_wake = rk818_irq_set_wake,
151 static int rk818_irq_domain_map(struct irq_domain *d, unsigned int irq,
154 struct rk818 *rk818 = d->host_data;
156 irq_set_chip_data(irq, rk818);
157 irq_set_chip_and_handler(irq, &rk818_irq_chip, handle_edge_irq);
158 irq_set_nested_thread(irq, 1);
160 set_irq_flags(irq, IRQF_VALID);
162 irq_set_noprobe(irq);
167 static struct irq_domain_ops rk818_irq_domain_ops = {
168 .map = rk818_irq_domain_map,
171 int rk818_irq_init(struct rk818 *rk818, int irq,struct rk818_board *pdata)
173 struct irq_domain *domain;
174 int ret,val,irq_type,flags;
177 // printk("%s,line=%d\n", __func__,__LINE__);
179 dev_warn(rk818->dev, "No interrupt support, no core IRQ\n");
183 /* Clear unattended interrupts */
184 rk818_i2c_read(rk818, RK818_INT_STS_REG1, 1, ®);
185 rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
186 rk818_i2c_read(rk818, RK818_INT_STS_REG2, 1, ®);
187 rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
188 rk818_i2c_read(rk818, RK818_RTC_STATUS_REG, 1, ®);
189 rk818_i2c_write(rk818, RK818_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
191 /* Mask top level interrupts */
192 rk818->irq_mask = 0xFFFFFF;
193 mutex_init(&rk818->irq_lock);
194 wake_lock_init(&rk818->irq_wake, WAKE_LOCK_SUSPEND, "rk818_irq_wake");
195 rk818->irq_num = RK818_NUM_IRQ;
196 rk818->irq_gpio = pdata->irq_gpio;
197 if (rk818->irq_gpio && !rk818->chip_irq) {
198 rk818->chip_irq = gpio_to_irq(rk818->irq_gpio);
200 if (rk818->irq_gpio) {
201 ret = gpio_request(rk818->irq_gpio, "rk818_pmic_irq");
204 "Failed to request gpio %d with ret:"
205 "%d\n", rk818->irq_gpio, ret);
208 gpio_direction_input(rk818->irq_gpio);
209 val = gpio_get_value(rk818->irq_gpio);
211 irq_type = IRQ_TYPE_LEVEL_LOW;
212 flags = IRQF_TRIGGER_FALLING;
215 irq_type = IRQ_TYPE_LEVEL_HIGH;
216 flags = IRQF_TRIGGER_RISING;
218 gpio_free(rk818->irq_gpio);
219 pr_info("%s: rk818_pmic_irq=%x\n", __func__, val);
223 domain = irq_domain_add_linear(NULL, RK818_NUM_IRQ,
224 &rk818_irq_domain_ops, rk818);
226 dev_err(rk818->dev, "could not create irq domain\n");
229 rk818->irq_domain = domain;
231 ret = request_threaded_irq(rk818->chip_irq, NULL, rk818_irq, flags | IRQF_ONESHOT, "rk818", rk818);
233 irq_set_irq_type(rk818->chip_irq, irq_type);
234 enable_irq_wake(rk818->chip_irq);
236 dev_err(rk818->dev, "Failed to request IRQ: %d\n", ret);
241 int rk818_irq_exit(struct rk818 *rk818)
244 free_irq(rk818->chip_irq, rk818);