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