]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-pwrirq.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.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 #include <linux/i2c/twl4030-pwrirq.h>
31
32 #define PWR_SIH_CTRL_COR (1<<2)
33
34 static u8 twl4030_pwrirq_mask;
35 static u8 twl4030_pwrirq_pending_unmask;
36
37 static struct task_struct *twl4030_pwrirq_unmask_thread;
38
39 static void twl4030_pwrirq_ack(unsigned int irq) {}
40
41 static void twl4030_pwrirq_disableint(unsigned int irq) {}
42
43 static void twl4030_pwrirq_enableint(unsigned int irq)
44 {
45         twl4030_pwrirq_pending_unmask |= 1 << (irq - TWL4030_PWR_IRQ_BASE);
46         if (twl4030_pwrirq_unmask_thread &&
47                 twl4030_pwrirq_unmask_thread->state != TASK_RUNNING)
48                 wake_up_process(twl4030_pwrirq_unmask_thread);
49 }
50
51 static struct irq_chip twl4030_pwrirq_chip = {
52         .name   = "twl4030-pwr",
53         .ack    = twl4030_pwrirq_ack,
54         .mask   = twl4030_pwrirq_disableint,
55         .unmask = twl4030_pwrirq_enableint,
56 };
57
58 static void do_twl4030_pwrmodule_irq(unsigned int irq, irq_desc_t *desc)
59 {
60         struct irqaction *action;
61         const unsigned int cpu = smp_processor_id();
62
63         desc->status |= IRQ_LEVEL;
64
65         if (!desc->depth) {
66                 kstat_cpu(cpu).irqs[irq]++;
67
68                 action = desc->action;
69                 if (action) {
70                         int ret;
71                         int status = 0;
72                         int retval = 0;
73
74                         do {
75                                 ret = action->handler(irq, action->dev_id);
76                                 if (ret == IRQ_HANDLED)
77                                         status |= action->flags;
78                                 retval |= ret;
79                                 action = action->next;
80                         } while (action);
81
82                         if (status & IRQF_SAMPLE_RANDOM)
83                                 add_interrupt_randomness(irq);
84
85                         if (retval != IRQ_HANDLED)
86                                 printk(KERN_ERR "ISR for TWL4030 power module"
87                                         " irq %d can't handle interrupt\n",
88                                         irq);
89                 } else {
90                         local_irq_disable();
91                         twl4030_pwrirq_mask |= 1 << (irq - TWL4030_PWR_IRQ_BASE);
92                         local_irq_enable();
93                         twl4030_i2c_write_u8(TWL4030_MODULE_INT,
94                                              twl4030_pwrirq_mask,
95                                              TWL4030_INT_PWR_IMR1);
96                 }
97         }
98 }
99
100 static void do_twl4030_pwrirq(unsigned int irq, irq_desc_t *desc)
101 {
102         const unsigned int cpu = smp_processor_id();
103
104         desc->status |= IRQ_LEVEL;
105
106         desc->chip->ack(irq);
107
108         if (!desc->depth) {
109                 int ret;
110                 int module_irq;
111                 u8 pwr_isr;
112
113                 kstat_cpu(cpu).irqs[irq]++;
114
115                 local_irq_enable();
116                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &pwr_isr,
117                                           TWL4030_INT_PWR_ISR1);
118                 if (ret) {
119                         printk(KERN_WARNING
120                                 "I2C error %d while reading TWL4030"
121                                 "INT PWR_ISR1 register\n", ret);
122                         return;
123                 }
124
125                 for (module_irq = TWL4030_PWR_IRQ_BASE; pwr_isr != 0;
126                         module_irq++, pwr_isr >>= 1) {
127                         if (pwr_isr & 1) {
128                                 irq_desc_t *d = irq_desc + module_irq;
129                                 d->handle_irq(module_irq, d);
130                         }
131                 }
132
133                 local_irq_disable();
134                 desc->chip->unmask(irq);
135         }
136 }
137
138 static int twl4030_pwrirq_thread(void *data)
139 {
140         current->flags |= PF_NOFREEZE;
141
142         while (!kthread_should_stop()) {
143                 u8 local_unmask;
144
145                 local_irq_disable();
146                 local_unmask = twl4030_pwrirq_pending_unmask;
147                 twl4030_pwrirq_pending_unmask = 0;
148                 local_irq_enable();
149
150                 twl4030_pwrirq_mask &= ~local_unmask;
151
152                 twl4030_i2c_write_u8(TWL4030_MODULE_INT, twl4030_pwrirq_mask,
153                                      TWL4030_INT_PWR_IMR1);
154
155                 local_irq_disable();
156                 if (!twl4030_pwrirq_pending_unmask)
157                         set_current_state(TASK_INTERRUPTIBLE);
158                 local_irq_enable();
159
160                 schedule();
161         }
162         set_current_state(TASK_RUNNING);
163         return 0;
164 }
165
166 static int __init twl4030_pwrirq_init(void)
167 {
168         int i, err;
169
170         twl4030_pwrirq_mask = 0xff;
171         twl4030_pwrirq_pending_unmask = 0;
172
173         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT, twl4030_pwrirq_mask,
174                                         TWL4030_INT_PWR_IMR1);
175         if (err)
176                 return err;
177
178         /* Enable clear on read */
179
180         err = twl4030_i2c_write_u8(TWL4030_MODULE_INT, PWR_SIH_CTRL_COR,
181                                    TWL4030_INT_PWR_SIH_CTRL);
182         if (err)
183                 return err;
184
185         twl4030_pwrirq_unmask_thread = kthread_create(twl4030_pwrirq_thread,
186                 NULL, "twl4030 pwrirq");
187         if (!twl4030_pwrirq_unmask_thread) {
188                 printk(KERN_ERR
189                         "%s: could not create twl4030 pwrirq unmask thread!\n",
190                                 __func__);
191                 return -ENOMEM;
192         }
193
194         for (i = TWL4030_PWR_IRQ_BASE; i < TWL4030_PWR_IRQ_END; i++) {
195                 set_irq_chip(i, &twl4030_pwrirq_chip);
196                 set_irq_handler(i, do_twl4030_pwrmodule_irq);
197                 set_irq_flags(i, IRQF_VALID);
198         }
199
200         set_irq_chained_handler(TWL4030_MODIRQ_PWR, do_twl4030_pwrirq);
201
202         return 0;
203 }
204
205 static void __exit twl4030_pwrirq_exit(void)
206 {
207
208         int i;
209
210         set_irq_handler(TWL4030_MODIRQ_PWR, NULL);
211         set_irq_flags(TWL4030_MODIRQ_PWR, 0);
212
213         for (i = TWL4030_PWR_IRQ_BASE; i < TWL4030_PWR_IRQ_END; i++) {
214                 set_irq_handler(i, NULL);
215                 set_irq_flags(i, 0);
216         }
217
218         if (twl4030_pwrirq_unmask_thread) {
219                 kthread_stop(twl4030_pwrirq_unmask_thread);
220                 twl4030_pwrirq_unmask_thread = NULL;
221         }
222 }
223
224 MODULE_ALIAS("i2c:twl4030-pwrirq");
225 subsys_initcall(twl4030_pwrirq_init);
226 module_exit(twl4030_pwrirq_exit);