2 * twl6030-irq.c - TWL6030 irq support
4 * Copyright (C) 2005-2009 Texas Instruments, Inc.
6 * Modifications to defer interrupt handling to a kernel thread:
7 * Copyright (C) 2006 MontaVista Software, Inc.
9 * Based on tlv320aic23.c:
10 * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
12 * Code cleanup and modifications to IRQ handler.
13 * by syed khasim <x0khasim@ti.com>
15 * TWL6030 specific code and IRQ handling changes by
16 * Jagadeesh Bhaskar Pakaravoor <j-pakaravoor@ti.com>
17 * Balaji T K <balajitk@ti.com>
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.
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.
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
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>
43 #include <linux/irqdomain.h>
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.
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.
57 #define TWL6030_NR_IRQS 20
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 */
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 */
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 */
87 /*----------------------------------------------------------------------*/
90 static bool twl_irq_wake_enabled;
92 static atomic_t twl6030_wakeirqs = ATOMIC_INIT(0);
93 struct irq_domain *irq_domain;
95 static int twl6030_irq_pm_notifier(struct notifier_block *notifier,
96 unsigned long pm_event, void *unused)
101 case PM_SUSPEND_PREPARE:
102 chained_wakeups = atomic_read(&twl6030_wakeirqs);
104 if (chained_wakeups && !twl_irq_wake_enabled) {
105 if (enable_irq_wake(twl_irq))
106 pr_err("twl6030 IRQ wake enable failed\n");
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;
114 disable_irq(twl_irq);
117 case PM_POST_SUSPEND:
128 static struct notifier_block twl6030_irq_pm_notifier_block = {
129 .notifier_call = twl6030_irq_pm_notifier,
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.
138 static irqreturn_t twl6030_irq_thread(int irq, void *data)
141 struct irq_domain *irq_domain = (struct irq_domain *)data;
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);
150 pr_warn("twl6030_irq: I2C error %d reading PIH ISR\n", ret);
154 sts.bytes[3] = 0; /* Only 24 bits are valid*/
157 * Since VBUS status bit is not reliable for VBUS disconnect
158 * use CHARGER VBUS detection status bit instead.
160 if (sts.bytes[2] & 0x10)
161 sts.bytes[2] |= 0x08;
163 for (i = 0; sts.int_sts; sts.int_sts >>= 1, i++)
164 if (sts.int_sts & 0x1) {
166 irq_find_mapping(irq_domain,
167 twl6030_interrupt_mapping[i]);
169 handle_nested_irq(module_irq);
171 pr_err("twl6030_irq: Unmapped PIH ISR %u detected\n",
173 pr_debug("twl6030_irq: PIH ISR %u, virq%u\n",
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.
186 ret = twl_i2c_write_u8(TWL_MODULE_PIH, 0x00, REG_INT_STS_A);
188 pr_warn("twl6030_irq: I2C error in clearing PIH ISR\n");
193 /*----------------------------------------------------------------------*/
195 static int twl6030_irq_set_wake(struct irq_data *d, unsigned int on)
198 atomic_inc(&twl6030_wakeirqs);
200 atomic_dec(&twl6030_wakeirqs);
205 int twl6030_interrupt_unmask(u8 bit_mask, u8 offset)
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 */
216 EXPORT_SYMBOL(twl6030_interrupt_unmask);
218 int twl6030_interrupt_mask(u8 bit_mask, u8 offset)
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 */
229 EXPORT_SYMBOL(twl6030_interrupt_mask);
231 int twl6030_mmc_card_detect_config(void)
236 /* Unmasking the Card detect Interrupt line for MMC1 from Phoenix */
237 twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
239 twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
242 * Initially Configuring MMC_CTRL for receiving interrupts &
243 * Card status on TWL6030 for MMC1
245 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, ®_val, TWL6030_MMCCTRL);
247 pr_err("twl6030: Failed to read MMCCTRL, error %d\n", ret);
250 reg_val &= ~VMMC_AUTO_OFF;
252 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val, TWL6030_MMCCTRL);
254 pr_err("twl6030: Failed to write MMCCTRL, error %d\n", ret);
258 /* Configuring PullUp-PullDown register */
259 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, ®_val,
260 TWL6030_CFG_INPUT_PUPD3);
262 pr_err("twl6030: Failed to read CFG_INPUT_PUPD3, error %d\n",
266 reg_val &= ~(MMC_PU | MMC_PD);
267 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val,
268 TWL6030_CFG_INPUT_PUPD3);
270 pr_err("twl6030: Failed to write CFG_INPUT_PUPD3, error %d\n",
275 return irq_find_mapping(irq_domain, MMCDETECT_INTR_OFFSET);
277 EXPORT_SYMBOL(twl6030_mmc_card_detect_config);
279 int twl6030_mmc_card_detect(struct device *dev, int slot)
283 struct platform_device *pdev = to_platform_device(dev);
286 /* TWL6030 provide's Card detect support for
287 * only MMC1 controller.
289 pr_err("Unknown MMC controller %d in %s\n", pdev->id, __func__);
293 * BIT0 of MMC_CTRL on TWL6030 provides card status for MMC1
294 * 0 - Card not present ,1 - Card present
296 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &read_reg,
299 ret = read_reg & STS_MMC;
302 EXPORT_SYMBOL(twl6030_mmc_card_detect);
304 static struct irq_chip twl6030_irq_chip;
306 static int twl6030_irq_map(struct irq_domain *d, unsigned int virq,
307 irq_hw_number_t hwirq)
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);
316 * ARM requires an extra step to clear IRQ_NOREQUEST, which it
317 * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
319 set_irq_flags(virq, IRQF_VALID);
321 /* same effect on other architectures */
322 irq_set_noprobe(virq);
328 static void twl6030_irq_unmap(struct irq_domain *d, unsigned int virq)
331 set_irq_flags(virq, 0);
333 irq_set_chip_and_handler(virq, NULL, NULL);
334 irq_set_chip_data(virq, NULL);
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,
343 int twl6030_init_irq(struct device *dev, int irq_num)
345 struct device_node *node = dev->of_node;
350 nr_irqs = TWL6030_NR_IRQS;
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);
364 dev_err(dev, "I2C err writing TWL_MODULE_PIH: %d\n", status);
369 * install an irq handler for each of the modules;
370 * clone dummy irq_chip since PIH can't *do* anything
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;
377 irq_domain = irq_domain_add_linear(node, nr_irqs,
378 &twl6030_irq_domain_ops, NULL);
380 dev_err(dev, "Can't add irq_domain\n");
384 dev_info(dev, "PIH (irq %d) nested IRQs\n", irq_num);
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);
390 dev_err(dev, "could not claim irq %d: %d\n", irq_num, status);
395 register_pm_notifier(&twl6030_irq_pm_notifier_block);
399 irq_domain_remove(irq_domain);
403 int twl6030_exit_irq(void)
406 unregister_pm_notifier(&twl6030_irq_pm_notifier_block);
407 free_irq(twl_irq, NULL);
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()