rk808:support pmu rk808
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / rk808-irq.c
1 /*
2  * rk808-irq.c  --  TI TPS6591x
3  *
4  * Copyright 2010 Texas Instruments Inc.
5  *
6  * Author: Graeme Gregory <gg@slimlogic.co.uk>
7  * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
8  *
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.
13  *
14  */
15
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>
27
28 static inline int irq_to_rk808_irq(struct rk808 *rk808,
29                                                         int irq)
30 {
31         return (irq - rk808->irq_base);
32 }
33
34 /*
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
41  * minimise I/O.
42  */
43 static irqreturn_t rk808_irq(int irq, void *irq_data)
44 {
45         struct rk808 *rk808 = irq_data;
46         u32 irq_sts;
47         u32 irq_mask;
48         u8 reg;
49         int i;
50         //printk(" rk808 irq %d \n",irq);       
51         wake_lock(&rk808->irq_wake);    
52         rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, &reg);
53         irq_sts = reg;
54         rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, &reg);
55         irq_sts |= reg << 8;
56         
57         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, &reg);
58         irq_mask = reg;
59         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, &reg);
60         irq_mask |= reg << 8;
61
62         irq_sts &= ~irq_mask;
63
64         if (!irq_sts)
65         {
66                 wake_unlock(&rk808->irq_wake);
67                 return IRQ_NONE;
68         }
69
70         for (i = 0; i < rk808->irq_num; i++) {
71
72                 if (!(irq_sts & (1 << i)))
73                         continue;
74
75                 handle_nested_irq(rk808->irq_base + i);
76         }
77
78         /* Write the STS register back to clear IRQs we handled */
79         reg = irq_sts & 0xFF;
80         irq_sts >>= 8;
81         rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
82         reg = irq_sts & 0xFF;
83         rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
84         wake_unlock(&rk808->irq_wake);
85         return IRQ_HANDLED;
86 }
87
88 static void rk808_irq_lock(struct irq_data *data)
89 {
90         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
91
92         mutex_lock(&rk808->irq_lock);
93 }
94
95 static void rk808_irq_sync_unlock(struct irq_data *data)
96 {
97         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
98         u32 reg_mask;
99         u8 reg;
100
101         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, &reg);
102         reg_mask = reg;
103         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, &reg);
104         reg_mask |= reg << 8;
105
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);
111         }
112         mutex_unlock(&rk808->irq_lock);
113 }
114
115 static void rk808_irq_enable(struct irq_data *data)
116 {
117         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
118
119         rk808->irq_mask &= ~( 1 << irq_to_rk808_irq(rk808, data->irq));
120 }
121
122 static void rk808_irq_disable(struct irq_data *data)
123 {
124         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
125
126         rk808->irq_mask |= ( 1 << irq_to_rk808_irq(rk808, data->irq));
127 }
128
129 #ifdef CONFIG_PM_SLEEP
130 static int rk808_irq_set_wake(struct irq_data *data, unsigned int enable)
131 {
132         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
133         return irq_set_irq_wake(rk808->chip_irq, enable);
134 }
135 #else
136 #define rk808_irq_set_wake NULL
137 #endif
138
139 static struct irq_chip rk808_irq_chip = {
140         .name = "rk808",
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,
146 };
147
148 int rk808_irq_init(struct rk808 *rk808, int irq,struct rk808_platform_data *pdata)
149 {
150         int ret, cur_irq;
151         int flags = IRQF_ONESHOT;
152         u8 reg;
153
154         printk("%s,line=%d\n", __func__,__LINE__);
155
156
157         if (!irq) {
158                 dev_warn(rk808->dev, "No interrupt support, no core IRQ\n");
159                 return 0;
160         }
161
162         if (!pdata || !pdata->irq_base) {
163                 dev_warn(rk808->dev, "No interrupt support, no IRQ base\n");
164                 return 0;
165         }
166
167         /* Clear unattended interrupts */
168         rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, &reg);
169         rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
170         rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, &reg);
171         rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
172         rk808_i2c_read(rk808, RK808_RTC_STATUS_REG, 1, &reg);   
173         rk808_i2c_write(rk808, RK808_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
174
175         /* Mask top level interrupts */
176         rk808->irq_mask = 0xFFFFFF;
177
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;
182
183         rk808->irq_num = RK808_NUM_IRQ;
184
185         /* Register with genirq */
186         for (cur_irq = rk808->irq_base;
187              cur_irq < rk808->irq_num + rk808->irq_base;
188              cur_irq++) {
189                 irq_set_chip_data(cur_irq, rk808);
190                 irq_set_chip_and_handler(cur_irq, &rk808_irq_chip,
191                                          handle_edge_irq);
192                 irq_set_nested_thread(cur_irq, 1);
193
194                 /* ARM needs us to explicitly flag the IRQ as valid
195                  * and will set them noprobe when we do so. */
196 #ifdef CONFIG_ARM
197                 set_irq_flags(cur_irq, IRQF_VALID);
198 #else
199                 irq_set_noprobe(cur_irq);
200 #endif
201         }
202
203         ret = request_threaded_irq(irq, NULL, rk808_irq, flags, "rk808", rk808);
204
205         irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
206
207         if (ret != 0)
208                 dev_err(rk808->dev, "Failed to request IRQ: %d\n", ret);
209         
210         return ret;
211 }
212
213 int rk808_irq_exit(struct rk808 *rk808)
214 {
215         if (rk808->chip_irq)
216                 free_irq(rk808->chip_irq, rk808);
217         return 0;
218 }