]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/mfd/twl6030-irq.c
mfd: twl6030-irq: Convert to use linear irq_domain
[karo-tx-linux.git] / drivers / mfd / twl6030-irq.c
1 /*
2  * twl6030-irq.c - TWL6030 irq support
3  *
4  * Copyright (C) 2005-2009 Texas Instruments, Inc.
5  *
6  * Modifications to defer interrupt handling to a kernel thread:
7  * Copyright (C) 2006 MontaVista Software, Inc.
8  *
9  * Based on tlv320aic23.c:
10  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
11  *
12  * Code cleanup and modifications to IRQ handler.
13  * by syed khasim <x0khasim@ti.com>
14  *
15  * TWL6030 specific code and IRQ handling changes by
16  * Jagadeesh Bhaskar Pakaravoor <j-pakaravoor@ti.com>
17  * Balaji T K <balajitk@ti.com>
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
32  */
33
34 #include <linux/init.h>
35 #include <linux/export.h>
36 #include <linux/interrupt.h>
37 #include <linux/irq.h>
38 #include <linux/kthread.h>
39 #include <linux/i2c/twl.h>
40 #include <linux/platform_device.h>
41 #include <linux/suspend.h>
42 #include <linux/of.h>
43 #include <linux/irqdomain.h>
44
45 #include "twl-core.h"
46
47 /*
48  * TWL6030 (unlike its predecessors, which had two level interrupt handling)
49  * three interrupt registers INT_STS_A, INT_STS_B and INT_STS_C.
50  * It exposes status bits saying who has raised an interrupt. There are
51  * three mask registers that corresponds to these status registers, that
52  * enables/disables these interrupts.
53  *
54  * We set up IRQs starting at a platform-specified base. An interrupt map table,
55  * specifies mapping between interrupt number and the associated module.
56  */
57 #define TWL6030_NR_IRQS    20
58
59 static int twl6030_interrupt_mapping[24] = {
60         PWR_INTR_OFFSET,        /* Bit 0        PWRON                   */
61         PWR_INTR_OFFSET,        /* Bit 1        RPWRON                  */
62         PWR_INTR_OFFSET,        /* Bit 2        BAT_VLOW                */
63         RTC_INTR_OFFSET,        /* Bit 3        RTC_ALARM               */
64         RTC_INTR_OFFSET,        /* Bit 4        RTC_PERIOD              */
65         HOTDIE_INTR_OFFSET,     /* Bit 5        HOT_DIE                 */
66         SMPSLDO_INTR_OFFSET,    /* Bit 6        VXXX_SHORT              */
67         SMPSLDO_INTR_OFFSET,    /* Bit 7        VMMC_SHORT              */
68
69         SMPSLDO_INTR_OFFSET,    /* Bit 8        VUSIM_SHORT             */
70         BATDETECT_INTR_OFFSET,  /* Bit 9        BAT                     */
71         SIMDETECT_INTR_OFFSET,  /* Bit 10       SIM                     */
72         MMCDETECT_INTR_OFFSET,  /* Bit 11       MMC                     */
73         RSV_INTR_OFFSET,        /* Bit 12       Reserved                */
74         MADC_INTR_OFFSET,       /* Bit 13       GPADC_RT_EOC            */
75         MADC_INTR_OFFSET,       /* Bit 14       GPADC_SW_EOC            */
76         GASGAUGE_INTR_OFFSET,   /* Bit 15       CC_AUTOCAL              */
77
78         USBOTG_INTR_OFFSET,     /* Bit 16       ID_WKUP                 */
79         USBOTG_INTR_OFFSET,     /* Bit 17       VBUS_WKUP               */
80         USBOTG_INTR_OFFSET,     /* Bit 18       ID                      */
81         USB_PRES_INTR_OFFSET,   /* Bit 19       VBUS                    */
82         CHARGER_INTR_OFFSET,    /* Bit 20       CHRG_CTRL               */
83         CHARGERFAULT_INTR_OFFSET,       /* Bit 21       EXT_CHRG        */
84         CHARGERFAULT_INTR_OFFSET,       /* Bit 22       INT_CHRG        */
85         RSV_INTR_OFFSET,        /* Bit 23       Reserved                */
86 };
87 /*----------------------------------------------------------------------*/
88
89 static int twl_irq;
90 static bool twl_irq_wake_enabled;
91
92 static atomic_t twl6030_wakeirqs = ATOMIC_INIT(0);
93 struct irq_domain       *irq_domain;
94
95 static int twl6030_irq_pm_notifier(struct notifier_block *notifier,
96                                    unsigned long pm_event, void *unused)
97 {
98         int chained_wakeups;
99
100         switch (pm_event) {
101         case PM_SUSPEND_PREPARE:
102                 chained_wakeups = atomic_read(&twl6030_wakeirqs);
103
104                 if (chained_wakeups && !twl_irq_wake_enabled) {
105                         if (enable_irq_wake(twl_irq))
106                                 pr_err("twl6030 IRQ wake enable failed\n");
107                         else
108                                 twl_irq_wake_enabled = true;
109                 } else if (!chained_wakeups && twl_irq_wake_enabled) {
110                         disable_irq_wake(twl_irq);
111                         twl_irq_wake_enabled = false;
112                 }
113
114                 disable_irq(twl_irq);
115                 break;
116
117         case PM_POST_SUSPEND:
118                 enable_irq(twl_irq);
119                 break;
120
121         default:
122                 break;
123         }
124
125         return NOTIFY_DONE;
126 }
127
128 static struct notifier_block twl6030_irq_pm_notifier_block = {
129         .notifier_call = twl6030_irq_pm_notifier,
130 };
131
132 /*
133 * Threaded irq handler for the twl6030 interrupt.
134 * We query the interrupt controller in the twl6030 to determine
135 * which module is generating the interrupt request and call
136 * handle_nested_irq for that module.
137 */
138 static irqreturn_t twl6030_irq_thread(int irq, void *data)
139 {
140         int i, ret;
141         struct irq_domain *irq_domain = (struct irq_domain *)data;
142         union {
143                 u8 bytes[4];
144                 u32 int_sts;
145         } sts;
146
147         /* read INT_STS_A, B and C in one shot using a burst read */
148         ret = twl_i2c_read(TWL_MODULE_PIH, sts.bytes, REG_INT_STS_A, 3);
149         if (ret) {
150                 pr_warn("twl6030_irq: I2C error %d reading PIH ISR\n", ret);
151                 return IRQ_HANDLED;
152         }
153
154         sts.bytes[3] = 0; /* Only 24 bits are valid*/
155
156         /*
157          * Since VBUS status bit is not reliable for VBUS disconnect
158          * use CHARGER VBUS detection status bit instead.
159          */
160         if (sts.bytes[2] & 0x10)
161                 sts.bytes[2] |= 0x08;
162
163         for (i = 0; sts.int_sts; sts.int_sts >>= 1, i++)
164                 if (sts.int_sts & 0x1) {
165                         int module_irq =
166                                 irq_find_mapping(irq_domain,
167                                                  twl6030_interrupt_mapping[i]);
168                         if (module_irq)
169                                 handle_nested_irq(module_irq);
170                         else
171                                 pr_err("twl6030_irq: Unmapped PIH ISR %u detected\n",
172                                        i);
173                         pr_debug("twl6030_irq: PIH ISR %u, virq%u\n",
174                                  i, module_irq);
175                 }
176
177         /*
178          * NOTE:
179          * Simulation confirms that documentation is wrong w.r.t the
180          * interrupt status clear operation. A single *byte* write to
181          * any one of STS_A to STS_C register results in all three
182          * STS registers being reset. Since it does not matter which
183          * value is written, all three registers are cleared on a
184          * single byte write, so we just use 0x0 to clear.
185          */
186         ret = twl_i2c_write_u8(TWL_MODULE_PIH, 0x00, REG_INT_STS_A);
187         if (ret)
188                 pr_warn("twl6030_irq: I2C error in clearing PIH ISR\n");
189
190         return IRQ_HANDLED;
191 }
192
193 /*----------------------------------------------------------------------*/
194
195 static int twl6030_irq_set_wake(struct irq_data *d, unsigned int on)
196 {
197         if (on)
198                 atomic_inc(&twl6030_wakeirqs);
199         else
200                 atomic_dec(&twl6030_wakeirqs);
201
202         return 0;
203 }
204
205 int twl6030_interrupt_unmask(u8 bit_mask, u8 offset)
206 {
207         int ret;
208         u8 unmask_value;
209         ret = twl_i2c_read_u8(TWL_MODULE_PIH, &unmask_value,
210                         REG_INT_STS_A + offset);
211         unmask_value &= (~(bit_mask));
212         ret |= twl_i2c_write_u8(TWL_MODULE_PIH, unmask_value,
213                         REG_INT_STS_A + offset); /* unmask INT_MSK_A/B/C */
214         return ret;
215 }
216 EXPORT_SYMBOL(twl6030_interrupt_unmask);
217
218 int twl6030_interrupt_mask(u8 bit_mask, u8 offset)
219 {
220         int ret;
221         u8 mask_value;
222         ret = twl_i2c_read_u8(TWL_MODULE_PIH, &mask_value,
223                         REG_INT_STS_A + offset);
224         mask_value |= (bit_mask);
225         ret |= twl_i2c_write_u8(TWL_MODULE_PIH, mask_value,
226                         REG_INT_STS_A + offset); /* mask INT_MSK_A/B/C */
227         return ret;
228 }
229 EXPORT_SYMBOL(twl6030_interrupt_mask);
230
231 int twl6030_mmc_card_detect_config(void)
232 {
233         int ret;
234         u8 reg_val = 0;
235
236         /* Unmasking the Card detect Interrupt line for MMC1 from Phoenix */
237         twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
238                                                 REG_INT_MSK_LINE_B);
239         twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
240                                                 REG_INT_MSK_STS_B);
241         /*
242          * Initially Configuring MMC_CTRL for receiving interrupts &
243          * Card status on TWL6030 for MMC1
244          */
245         ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val, TWL6030_MMCCTRL);
246         if (ret < 0) {
247                 pr_err("twl6030: Failed to read MMCCTRL, error %d\n", ret);
248                 return ret;
249         }
250         reg_val &= ~VMMC_AUTO_OFF;
251         reg_val |= SW_FC;
252         ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val, TWL6030_MMCCTRL);
253         if (ret < 0) {
254                 pr_err("twl6030: Failed to write MMCCTRL, error %d\n", ret);
255                 return ret;
256         }
257
258         /* Configuring PullUp-PullDown register */
259         ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val,
260                                                 TWL6030_CFG_INPUT_PUPD3);
261         if (ret < 0) {
262                 pr_err("twl6030: Failed to read CFG_INPUT_PUPD3, error %d\n",
263                                                                         ret);
264                 return ret;
265         }
266         reg_val &= ~(MMC_PU | MMC_PD);
267         ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val,
268                                                 TWL6030_CFG_INPUT_PUPD3);
269         if (ret < 0) {
270                 pr_err("twl6030: Failed to write CFG_INPUT_PUPD3, error %d\n",
271                                                                         ret);
272                 return ret;
273         }
274
275         return irq_find_mapping(irq_domain, MMCDETECT_INTR_OFFSET);
276 }
277 EXPORT_SYMBOL(twl6030_mmc_card_detect_config);
278
279 int twl6030_mmc_card_detect(struct device *dev, int slot)
280 {
281         int ret = -EIO;
282         u8 read_reg = 0;
283         struct platform_device *pdev = to_platform_device(dev);
284
285         if (pdev->id) {
286                 /* TWL6030 provide's Card detect support for
287                  * only MMC1 controller.
288                  */
289                 pr_err("Unknown MMC controller %d in %s\n", pdev->id, __func__);
290                 return ret;
291         }
292         /*
293          * BIT0 of MMC_CTRL on TWL6030 provides card status for MMC1
294          * 0 - Card not present ,1 - Card present
295          */
296         ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &read_reg,
297                                                 TWL6030_MMCCTRL);
298         if (ret >= 0)
299                 ret = read_reg & STS_MMC;
300         return ret;
301 }
302 EXPORT_SYMBOL(twl6030_mmc_card_detect);
303
304 static struct irq_chip twl6030_irq_chip;
305
306 static int twl6030_irq_map(struct irq_domain *d, unsigned int virq,
307                               irq_hw_number_t hwirq)
308 {
309         irq_set_chip_data(virq, &twl6030_irq_chip);
310         irq_set_chip_and_handler(virq,  &twl6030_irq_chip, handle_simple_irq);
311         irq_set_nested_thread(virq, true);
312         irq_set_parent(virq, twl_irq);
313
314 #ifdef CONFIG_ARM
315         /*
316          * ARM requires an extra step to clear IRQ_NOREQUEST, which it
317          * sets on behalf of every irq_chip.  Also sets IRQ_NOPROBE.
318          */
319         set_irq_flags(virq, IRQF_VALID);
320 #else
321         /* same effect on other architectures */
322         irq_set_noprobe(virq);
323 #endif
324
325         return 0;
326 }
327
328 static void twl6030_irq_unmap(struct irq_domain *d, unsigned int virq)
329 {
330 #ifdef CONFIG_ARM
331         set_irq_flags(virq, 0);
332 #endif
333         irq_set_chip_and_handler(virq, NULL, NULL);
334         irq_set_chip_data(virq, NULL);
335 }
336
337 static struct irq_domain_ops twl6030_irq_domain_ops = {
338         .map    = twl6030_irq_map,
339         .unmap  = twl6030_irq_unmap,
340         .xlate  = irq_domain_xlate_onetwocell,
341 };
342
343 int twl6030_init_irq(struct device *dev, int irq_num)
344 {
345         struct                  device_node *node = dev->of_node;
346         int                     nr_irqs;
347         int                     status;
348         u8                      mask[3];
349
350         nr_irqs = TWL6030_NR_IRQS;
351
352         mask[0] = 0xFF;
353         mask[1] = 0xFF;
354         mask[2] = 0xFF;
355
356         /* mask all int lines */
357         status = twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_LINE_A, 3);
358         /* mask all int sts */
359         status |= twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_STS_A, 3);
360         /* clear INT_STS_A,B,C */
361         status |= twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_STS_A, 3);
362
363         if (status < 0) {
364                 dev_err(dev, "I2C err writing TWL_MODULE_PIH: %d\n", status);
365                 return status;
366         }
367
368         /*
369          * install an irq handler for each of the modules;
370          * clone dummy irq_chip since PIH can't *do* anything
371          */
372         twl6030_irq_chip = dummy_irq_chip;
373         twl6030_irq_chip.name = "twl6030";
374         twl6030_irq_chip.irq_set_type = NULL;
375         twl6030_irq_chip.irq_set_wake = twl6030_irq_set_wake;
376
377         irq_domain = irq_domain_add_linear(node, nr_irqs,
378                                            &twl6030_irq_domain_ops, NULL);
379         if (!irq_domain) {
380                 dev_err(dev, "Can't add irq_domain\n");
381                 return -ENOMEM;
382         }
383
384         dev_info(dev, "PIH (irq %d) nested IRQs\n", irq_num);
385
386         /* install an irq handler to demultiplex the TWL6030 interrupt */
387         status = request_threaded_irq(irq_num, NULL, twl6030_irq_thread,
388                                       IRQF_ONESHOT, "TWL6030-PIH", irq_domain);
389         if (status < 0) {
390                 dev_err(dev, "could not claim irq %d: %d\n", irq_num, status);
391                 goto fail_irq;
392         }
393
394         twl_irq = irq_num;
395         register_pm_notifier(&twl6030_irq_pm_notifier_block);
396         return 0;
397
398 fail_irq:
399         irq_domain_remove(irq_domain);
400         return status;
401 }
402
403 int twl6030_exit_irq(void)
404 {
405         if (twl_irq) {
406                 unregister_pm_notifier(&twl6030_irq_pm_notifier_block);
407                 free_irq(twl_irq, NULL);
408                 /*
409                  * TODO: IRQ domain and allocated nested IRQ descriptors
410                  * should be freed somehow here. Now It can't be done, because
411                  * child devices will not be deleted during removing of
412                  * TWL Core driver and they will still contain allocated
413                  * virt IRQs in their Resources tables.
414                  * The same prevents us from using devm_request_threaded_irq()
415                  * in this module.
416                  */
417         }
418         return 0;
419 }
420