remove <linux/i2c/twl4030-pwrirq.h>
[pandora-kernel.git] / drivers / i2c / chips / twl4030-pwrirq.c
1 /*
2  * twl4030-pwrirq.c - handle power interrupts from TWL3040
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Written by Peter De Schrijver <peter.de-schrijver@nokia.com>
7  *
8  * This file is subject to the terms and conditions of the GNU General
9  * Public License. See the file "COPYING" in the main directory of this
10  * archive for more details.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/kernel_stat.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/i2c.h>
27 #include <linux/random.h>
28 #include <linux/kthread.h>
29 #include <linux/i2c/twl4030.h>
30
31
32 static u8 twl4030_pwrirq_mask;
33 static u8 twl4030_pwrirq_pending_unmask;
34
35 static struct task_struct *twl4030_pwrirq_unmask_thread;
36
37 static void twl4030_pwrirq_ack(unsigned int irq) {}
38
39 static void twl4030_pwrirq_disableint(unsigned int irq) {}
40
41 static void twl4030_pwrirq_enableint(unsigned int irq)
42 {
43         twl4030_pwrirq_pending_unmask |= 1 << (irq - TWL4030_PWR_IRQ_BASE);
44         if (twl4030_pwrirq_unmask_thread &&
45                 twl4030_pwrirq_unmask_thread->state != TASK_RUNNING)
46                 wake_up_process(twl4030_pwrirq_unmask_thread);
47 }
48
49 static struct irq_chip twl4030_pwrirq_chip = {
50         .name   = "twl4030-pwr",
51         .ack    = twl4030_pwrirq_ack,
52         .mask   = twl4030_pwrirq_disableint,
53         .unmask = twl4030_pwrirq_enableint,
54 };
55
56 static void do_twl4030_pwrmodule_irq(unsigned int irq, irq_desc_t *desc)
57 {
58         struct irqaction *action;
59         const unsigned int cpu = smp_processor_id();
60
61         desc->status |= IRQ_LEVEL;
62
63         if (!desc->depth) {
64                 kstat_cpu(cpu).irqs[irq]++;
65
66                 action = desc->action;
67                 if (action) {
68                         int ret;
69                         int status = 0;
70                         int retval = 0;
71
72                         do {
73                                 ret = action->handler(irq, action->dev_id);
74                                 if (ret == IRQ_HANDLED)
75                                         status |= action->flags;
76                                 retval |= ret;
77                                 action = action->next;
78                         } while (action);
79
80                         if (status & IRQF_SAMPLE_RANDOM)
81                                 add_interrupt_randomness(irq);
82
83                         if (retval != IRQ_HANDLED)
84                                 printk(KERN_ERR "ISR for TWL4030 power module"
85                                         " irq %d can't handle interrupt\n",
86                                         irq);
87                 } else {
88                         local_irq_disable();
89                         twl4030_pwrirq_mask |= 1 << (irq - TWL4030_PWR_IRQ_BASE);
90                         local_irq_enable();
91                         twl4030_i2c_write_u8(TWL4030_MODULE_INT,
92                                              twl4030_pwrirq_mask,
93                                              TWL4030_INT_PWR_IMR1);
94                 }
95         }
96 }
97
98 static void do_twl4030_pwrirq(unsigned int irq, irq_desc_t *desc)
99 {
100         const unsigned int cpu = smp_processor_id();
101
102         desc->status |= IRQ_LEVEL;
103
104         desc->chip->ack(irq);
105
106         if (!desc->depth) {
107                 int ret;
108                 int module_irq;
109                 u8 pwr_isr;
110
111                 kstat_cpu(cpu).irqs[irq]++;
112
113                 local_irq_enable();
114                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &pwr_isr,
115                                           TWL4030_INT_PWR_ISR1);
116                 if (ret) {
117                         printk(KERN_WARNING
118                                 "I2C error %d while reading TWL4030"
119                                 "INT PWR_ISR1 register\n", ret);
120                         return;
121                 }
122
123                 for (module_irq = TWL4030_PWR_IRQ_BASE; pwr_isr != 0;
124                         module_irq++, pwr_isr >>= 1) {
125                         if (pwr_isr & 1) {
126                                 irq_desc_t *d = irq_desc + module_irq;
127                                 d->handle_irq(module_irq, d);
128                         }
129                 }
130
131                 local_irq_disable();
132                 desc->chip->unmask(irq);
133         }
134 }
135
136 static int twl4030_pwrirq_thread(void *data)
137 {
138         current->flags |= PF_NOFREEZE;
139
140         while (!kthread_should_stop()) {
141                 u8 local_unmask;
142
143                 local_irq_disable();
144                 local_unmask = twl4030_pwrirq_pending_unmask;
145                 twl4030_pwrirq_pending_unmask = 0;
146                 local_irq_enable();
147
148                 twl4030_pwrirq_mask &= ~local_unmask;
149
150                 twl4030_i2c_write_u8(TWL4030_MODULE_INT, twl4030_pwrirq_mask,
151                                      TWL4030_INT_PWR_IMR1);
152
153                 local_irq_disable();
154                 if (!twl4030_pwrirq_pending_unmask)
155                         set_current_state(TASK_INTERRUPTIBLE);
156                 local_irq_enable();
157
158                 schedule();
159         }
160         set_current_state(TASK_RUNNING);
161         return 0;
162 }
163
164 static int __init twl4030_pwrirq_init(void)
165 {
166         int i, err;
167
168         twl4030_pwrirq_mask = 0xff;
169         twl4030_pwrirq_pending_unmask = 0;
170
171         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT, twl4030_pwrirq_mask,
172                                         TWL4030_INT_PWR_IMR1);
173         if (err)
174                 return err;
175
176         /* Enable clear on read */
177
178         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT,
179                                 TWL4030_SIH_CTRL_COR_MASK,
180                                 TWL4030_INT_PWR_SIH_CTRL);
181         if (err)
182                 return err;
183
184         twl4030_pwrirq_unmask_thread = kthread_create(twl4030_pwrirq_thread,
185                 NULL, "twl4030 pwrirq");
186         if (!twl4030_pwrirq_unmask_thread) {
187                 printk(KERN_ERR
188                         "%s: could not create twl4030 pwrirq unmask thread!\n",
189                                 __func__);
190                 return -ENOMEM;
191         }
192
193         for (i = TWL4030_PWR_IRQ_BASE; i < TWL4030_PWR_IRQ_END; i++) {
194                 set_irq_chip(i, &twl4030_pwrirq_chip);
195                 set_irq_handler(i, do_twl4030_pwrmodule_irq);
196                 set_irq_flags(i, IRQF_VALID);
197         }
198
199         set_irq_chained_handler(TWL4030_MODIRQ_PWR, do_twl4030_pwrirq);
200
201         return 0;
202 }
203 subsys_initcall(twl4030_pwrirq_init);
204
205 static void __exit twl4030_pwrirq_exit(void)
206 {
207
208         int i;
209
210         /* FIXME the irqs are left enabled; trouble when they arrive... */
211
212         set_irq_handler(TWL4030_MODIRQ_PWR, NULL);
213         set_irq_flags(TWL4030_MODIRQ_PWR, 0);
214
215         for (i = TWL4030_PWR_IRQ_BASE; i < TWL4030_PWR_IRQ_END; i++) {
216                 set_irq_handler(i, NULL);
217                 set_irq_flags(i, 0);
218         }
219
220         if (twl4030_pwrirq_unmask_thread) {
221                 kthread_stop(twl4030_pwrirq_unmask_thread);
222                 twl4030_pwrirq_unmask_thread = NULL;
223         }
224 }
225 module_exit(twl4030_pwrirq_exit);