blob: f7da2614de80343c3c97acd848db3053557d74ea [file] [log] [blame]
Balaji T Ke8deb282009-12-14 00:25:31 +01001/*
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>
Paul Gortmaker5d4a3572011-07-10 12:41:10 -040035#include <linux/export.h>
Balaji T Ke8deb282009-12-14 00:25:31 +010036#include <linux/interrupt.h>
37#include <linux/irq.h>
38#include <linux/kthread.h>
39#include <linux/i2c/twl.h>
kishore kadiyala72f2e2c2010-09-24 17:13:20 +000040#include <linux/platform_device.h>
Todd Poynorab2b9262011-10-04 11:52:29 +020041#include <linux/suspend.h>
Benoit Cousson78518ff2012-02-29 19:40:31 +010042#include <linux/of.h>
43#include <linux/irqdomain.h>
Balaji T Ke8deb282009-12-14 00:25:31 +010044
G, Manjunath Kondaiahb0b4a7c2010-10-19 11:02:48 +020045#include "twl-core.h"
46
Balaji T Ke8deb282009-12-14 00:25:31 +010047/*
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.
Balaji T Ke8deb282009-12-14 00:25:31 +010056 */
Benoit Cousson78518ff2012-02-29 19:40:31 +010057#define TWL6030_NR_IRQS 20
Balaji T Ke8deb282009-12-14 00:25:31 +010058
59static 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 */
Hema HK77b1d3f2010-12-10 17:55:37 +053081 USB_PRES_INTR_OFFSET, /* Bit 19 VBUS */
Balaji T Ke8deb282009-12-14 00:25:31 +010082 CHARGER_INTR_OFFSET, /* Bit 20 CHRG_CTRL */
Graeme Gregory6523b142011-05-12 14:27:56 +010083 CHARGERFAULT_INTR_OFFSET, /* Bit 21 EXT_CHRG */
84 CHARGERFAULT_INTR_OFFSET, /* Bit 22 INT_CHRG */
Balaji T Ke8deb282009-12-14 00:25:31 +010085 RSV_INTR_OFFSET, /* Bit 23 Reserved */
86};
87/*----------------------------------------------------------------------*/
88
89static unsigned twl6030_irq_base;
Todd Poynorab2b9262011-10-04 11:52:29 +020090static int twl_irq;
91static bool twl_irq_wake_enabled;
Balaji T Ke8deb282009-12-14 00:25:31 +010092
Todd Poynorab2b9262011-10-04 11:52:29 +020093static atomic_t twl6030_wakeirqs = ATOMIC_INIT(0);
94
95static 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
Todd Poynor782baa22011-09-26 16:44:24 -0700114 disable_irq(twl_irq);
Todd Poynorab2b9262011-10-04 11:52:29 +0200115 break;
Todd Poynor782baa22011-09-26 16:44:24 -0700116
117 case PM_POST_SUSPEND:
118 enable_irq(twl_irq);
119 break;
120
Todd Poynorab2b9262011-10-04 11:52:29 +0200121 default:
122 break;
123 }
124
125 return NOTIFY_DONE;
126}
127
128static struct notifier_block twl6030_irq_pm_notifier_block = {
129 .notifier_call = twl6030_irq_pm_notifier,
130};
Balaji T Ke8deb282009-12-14 00:25:31 +0100131
132/*
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300133* 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*/
138static irqreturn_t twl6030_irq_thread(int irq, void *data)
Balaji T Ke8deb282009-12-14 00:25:31 +0100139{
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300140 int i, ret;
141 union {
Balaji T Ke8deb282009-12-14 00:25:31 +0100142 u8 bytes[4];
143 u32 int_sts;
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300144 } sts;
Balaji T Ke8deb282009-12-14 00:25:31 +0100145
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300146 /* read INT_STS_A, B and C in one shot using a burst read */
147 ret = twl_i2c_read(TWL_MODULE_PIH, sts.bytes, REG_INT_STS_A, 3);
148 if (ret) {
149 pr_warn("twl6030_irq: I2C error %d reading PIH ISR\n", ret);
150 return IRQ_HANDLED;
Balaji T Ke8deb282009-12-14 00:25:31 +0100151 }
152
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300153 sts.bytes[3] = 0; /* Only 24 bits are valid*/
Balaji T Ke8deb282009-12-14 00:25:31 +0100154
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300155 /*
156 * Since VBUS status bit is not reliable for VBUS disconnect
157 * use CHARGER VBUS detection status bit instead.
158 */
159 if (sts.bytes[2] & 0x10)
160 sts.bytes[2] |= 0x08;
161
162 for (i = 0; sts.int_sts; sts.int_sts >>= 1, i++)
163 if (sts.int_sts & 0x1) {
164 int module_irq = twl6030_irq_base +
165 twl6030_interrupt_mapping[i];
166 handle_nested_irq(module_irq);
167 pr_debug("twl6030_irq: PIH ISR %u, virq%u\n",
168 i, module_irq);
169 }
170
171 /*
172 * NOTE:
173 * Simulation confirms that documentation is wrong w.r.t the
174 * interrupt status clear operation. A single *byte* write to
175 * any one of STS_A to STS_C register results in all three
176 * STS registers being reset. Since it does not matter which
177 * value is written, all three registers are cleared on a
178 * single byte write, so we just use 0x0 to clear.
179 */
180 ret = twl_i2c_write_u8(TWL_MODULE_PIH, 0x00, REG_INT_STS_A);
181 if (ret)
182 pr_warn("twl6030_irq: I2C error in clearing PIH ISR\n");
183
Balaji T Ke8deb282009-12-14 00:25:31 +0100184 return IRQ_HANDLED;
185}
186
187/*----------------------------------------------------------------------*/
188
189static inline void activate_irq(int irq)
190{
191#ifdef CONFIG_ARM
192 /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
193 * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
194 */
195 set_irq_flags(irq, IRQF_VALID);
196#else
197 /* same effect on other architectures */
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000198 irq_set_noprobe(irq);
Balaji T Ke8deb282009-12-14 00:25:31 +0100199#endif
200}
201
Nishanth Menonb8b8d792012-02-22 20:03:59 -0600202static int twl6030_irq_set_wake(struct irq_data *d, unsigned int on)
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530203{
Todd Poynorab2b9262011-10-04 11:52:29 +0200204 if (on)
205 atomic_inc(&twl6030_wakeirqs);
206 else
207 atomic_dec(&twl6030_wakeirqs);
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530208
Todd Poynorab2b9262011-10-04 11:52:29 +0200209 return 0;
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530210}
211
Balaji T Ke8deb282009-12-14 00:25:31 +0100212int twl6030_interrupt_unmask(u8 bit_mask, u8 offset)
213{
214 int ret;
215 u8 unmask_value;
216 ret = twl_i2c_read_u8(TWL_MODULE_PIH, &unmask_value,
217 REG_INT_STS_A + offset);
218 unmask_value &= (~(bit_mask));
219 ret |= twl_i2c_write_u8(TWL_MODULE_PIH, unmask_value,
220 REG_INT_STS_A + offset); /* unmask INT_MSK_A/B/C */
221 return ret;
222}
223EXPORT_SYMBOL(twl6030_interrupt_unmask);
224
225int twl6030_interrupt_mask(u8 bit_mask, u8 offset)
226{
227 int ret;
228 u8 mask_value;
229 ret = twl_i2c_read_u8(TWL_MODULE_PIH, &mask_value,
230 REG_INT_STS_A + offset);
231 mask_value |= (bit_mask);
232 ret |= twl_i2c_write_u8(TWL_MODULE_PIH, mask_value,
233 REG_INT_STS_A + offset); /* mask INT_MSK_A/B/C */
234 return ret;
235}
236EXPORT_SYMBOL(twl6030_interrupt_mask);
237
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000238int twl6030_mmc_card_detect_config(void)
239{
240 int ret;
241 u8 reg_val = 0;
242
243 /* Unmasking the Card detect Interrupt line for MMC1 from Phoenix */
244 twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
245 REG_INT_MSK_LINE_B);
246 twl6030_interrupt_unmask(TWL6030_MMCDETECT_INT_MASK,
247 REG_INT_MSK_STS_B);
248 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300249 * Initially Configuring MMC_CTRL for receiving interrupts &
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000250 * Card status on TWL6030 for MMC1
251 */
252 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val, TWL6030_MMCCTRL);
253 if (ret < 0) {
254 pr_err("twl6030: Failed to read MMCCTRL, error %d\n", ret);
255 return ret;
256 }
257 reg_val &= ~VMMC_AUTO_OFF;
258 reg_val |= SW_FC;
259 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val, TWL6030_MMCCTRL);
260 if (ret < 0) {
261 pr_err("twl6030: Failed to write MMCCTRL, error %d\n", ret);
262 return ret;
263 }
264
265 /* Configuring PullUp-PullDown register */
266 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &reg_val,
267 TWL6030_CFG_INPUT_PUPD3);
268 if (ret < 0) {
269 pr_err("twl6030: Failed to read CFG_INPUT_PUPD3, error %d\n",
270 ret);
271 return ret;
272 }
273 reg_val &= ~(MMC_PU | MMC_PD);
274 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, reg_val,
275 TWL6030_CFG_INPUT_PUPD3);
276 if (ret < 0) {
277 pr_err("twl6030: Failed to write CFG_INPUT_PUPD3, error %d\n",
278 ret);
279 return ret;
280 }
Benoit Coussonbdd61bc2012-03-02 16:15:22 +0100281
282 return twl6030_irq_base + MMCDETECT_INTR_OFFSET;
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000283}
284EXPORT_SYMBOL(twl6030_mmc_card_detect_config);
285
286int twl6030_mmc_card_detect(struct device *dev, int slot)
287{
288 int ret = -EIO;
289 u8 read_reg = 0;
290 struct platform_device *pdev = to_platform_device(dev);
291
292 if (pdev->id) {
293 /* TWL6030 provide's Card detect support for
294 * only MMC1 controller.
295 */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300296 pr_err("Unknown MMC controller %d in %s\n", pdev->id, __func__);
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000297 return ret;
298 }
299 /*
300 * BIT0 of MMC_CTRL on TWL6030 provides card status for MMC1
301 * 0 - Card not present ,1 - Card present
302 */
303 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &read_reg,
304 TWL6030_MMCCTRL);
305 if (ret >= 0)
306 ret = read_reg & STS_MMC;
307 return ret;
308}
309EXPORT_SYMBOL(twl6030_mmc_card_detect);
310
Benoit Cousson78518ff2012-02-29 19:40:31 +0100311int twl6030_init_irq(struct device *dev, int irq_num)
Balaji T Ke8deb282009-12-14 00:25:31 +0100312{
Benoit Cousson78518ff2012-02-29 19:40:31 +0100313 struct device_node *node = dev->of_node;
314 int nr_irqs, irq_base, irq_end;
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100315 static struct irq_chip twl6030_irq_chip;
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300316 int status;
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100317 int i;
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100318 u8 mask[3];
Benoit Cousson78518ff2012-02-29 19:40:31 +0100319
320 nr_irqs = TWL6030_NR_IRQS;
321
322 irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
323 if (IS_ERR_VALUE(irq_base)) {
324 dev_err(dev, "Fail to allocate IRQ descs\n");
325 return irq_base;
326 }
327
328 irq_domain_add_legacy(node, nr_irqs, irq_base, 0,
329 &irq_domain_simple_ops, NULL);
330
331 irq_end = irq_base + nr_irqs;
332
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100333 mask[0] = 0xFF;
Balaji T Ke8deb282009-12-14 00:25:31 +0100334 mask[1] = 0xFF;
335 mask[2] = 0xFF;
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100336
337 /* mask all int lines */
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300338 status = twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_LINE_A, 3);
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100339 /* mask all int sts */
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300340 status |= twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_STS_A, 3);
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100341 /* clear INT_STS_A,B,C */
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300342 status |= twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_STS_A, 3);
343
344 if (status < 0) {
345 dev_err(dev, "I2C err writing TWL_MODULE_PIH: %d\n", status);
346 return status;
347 }
Balaji T Ke8deb282009-12-14 00:25:31 +0100348
349 twl6030_irq_base = irq_base;
350
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100351 /*
352 * install an irq handler for each of the modules;
Balaji T Ke8deb282009-12-14 00:25:31 +0100353 * clone dummy irq_chip since PIH can't *do* anything
354 */
355 twl6030_irq_chip = dummy_irq_chip;
356 twl6030_irq_chip.name = "twl6030";
Lennert Buytenhekc45c6852010-12-13 13:31:18 +0100357 twl6030_irq_chip.irq_set_type = NULL;
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530358 twl6030_irq_chip.irq_set_wake = twl6030_irq_set_wake;
Balaji T Ke8deb282009-12-14 00:25:31 +0100359
360 for (i = irq_base; i < irq_end; i++) {
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000361 irq_set_chip_and_handler(i, &twl6030_irq_chip,
362 handle_simple_irq);
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530363 irq_set_chip_data(i, (void *)irq_num);
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300364 irq_set_nested_thread(i, true);
365 irq_set_parent(i, irq_num);
Balaji T Ke8deb282009-12-14 00:25:31 +0100366 activate_irq(i);
367 }
368
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300369 dev_info(dev, "PIH (irq %d) nested IRQs %d..%d\n",
370 irq_num, irq_base, irq_end);
Balaji T Ke8deb282009-12-14 00:25:31 +0100371
372 /* install an irq handler to demultiplex the TWL6030 interrupt */
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300373 status = request_threaded_irq(irq_num, NULL, twl6030_irq_thread,
374 IRQF_ONESHOT, "TWL6030-PIH", NULL);
Balaji T Ke8deb282009-12-14 00:25:31 +0100375 if (status < 0) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100376 dev_err(dev, "could not claim irq %d: %d\n", irq_num, status);
Balaji T Ke8deb282009-12-14 00:25:31 +0100377 goto fail_irq;
378 }
Axel Lin862de702011-08-11 15:21:00 +0800379
Todd Poynorab2b9262011-10-04 11:52:29 +0200380 twl_irq = irq_num;
381 register_pm_notifier(&twl6030_irq_pm_notifier_block);
Benoit Cousson78518ff2012-02-29 19:40:31 +0100382 return irq_base;
Balaji T Ke8deb282009-12-14 00:25:31 +0100383
Axel Lin862de702011-08-11 15:21:00 +0800384fail_irq:
Balaji T Ke8deb282009-12-14 00:25:31 +0100385 for (i = irq_base; i < irq_end; i++)
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000386 irq_set_chip_and_handler(i, NULL, NULL);
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100387
Balaji T Ke8deb282009-12-14 00:25:31 +0100388 return status;
389}
390
391int twl6030_exit_irq(void)
392{
Balaji T Ke8deb282009-12-14 00:25:31 +0100393
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300394 if (twl_irq) {
395 unregister_pm_notifier(&twl6030_irq_pm_notifier_block);
396 free_irq(twl_irq, NULL);
Balaji T Ke8deb282009-12-14 00:25:31 +0100397 }
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300398
Balaji T Ke8deb282009-12-14 00:25:31 +0100399 return 0;
400}
401