blob: 1b03ce9e9f1535535aa20b26926c32decbf2f9be [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
Todd Poynorab2b9262011-10-04 11:52:29 +020089static int twl_irq;
90static bool twl_irq_wake_enabled;
Balaji T Ke8deb282009-12-14 00:25:31 +010091
Todd Poynorab2b9262011-10-04 11:52:29 +020092static atomic_t twl6030_wakeirqs = ATOMIC_INIT(0);
Grygorii Strashkob32408f2013-07-25 16:15:49 +030093struct irq_domain *irq_domain;
Todd Poynorab2b9262011-10-04 11:52:29 +020094
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;
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300141 struct irq_domain *irq_domain = (struct irq_domain *)data;
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300142 union {
Balaji T Ke8deb282009-12-14 00:25:31 +0100143 u8 bytes[4];
144 u32 int_sts;
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300145 } sts;
Balaji T Ke8deb282009-12-14 00:25:31 +0100146
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300147 /* 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;
Balaji T Ke8deb282009-12-14 00:25:31 +0100152 }
153
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300154 sts.bytes[3] = 0; /* Only 24 bits are valid*/
Balaji T Ke8deb282009-12-14 00:25:31 +0100155
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300156 /*
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) {
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300165 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);
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300173 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
Balaji T Ke8deb282009-12-14 00:25:31 +0100190 return IRQ_HANDLED;
191}
192
193/*----------------------------------------------------------------------*/
194
Nishanth Menonb8b8d792012-02-22 20:03:59 -0600195static int twl6030_irq_set_wake(struct irq_data *d, unsigned int on)
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530196{
Todd Poynorab2b9262011-10-04 11:52:29 +0200197 if (on)
198 atomic_inc(&twl6030_wakeirqs);
199 else
200 atomic_dec(&twl6030_wakeirqs);
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530201
Todd Poynorab2b9262011-10-04 11:52:29 +0200202 return 0;
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530203}
204
Balaji T Ke8deb282009-12-14 00:25:31 +0100205int 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}
216EXPORT_SYMBOL(twl6030_interrupt_unmask);
217
218int 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}
229EXPORT_SYMBOL(twl6030_interrupt_mask);
230
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000231int 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 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300242 * Initially Configuring MMC_CTRL for receiving interrupts &
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000243 * 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 }
Benoit Coussonbdd61bc2012-03-02 16:15:22 +0100274
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300275 return irq_find_mapping(irq_domain, MMCDETECT_INTR_OFFSET);
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000276}
277EXPORT_SYMBOL(twl6030_mmc_card_detect_config);
278
279int 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 */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300289 pr_err("Unknown MMC controller %d in %s\n", pdev->id, __func__);
kishore kadiyala72f2e2c2010-09-24 17:13:20 +0000290 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}
302EXPORT_SYMBOL(twl6030_mmc_card_detect);
303
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300304static struct irq_chip twl6030_irq_chip;
305
306static 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
328static 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
337static 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
Benoit Cousson78518ff2012-02-29 19:40:31 +0100343int twl6030_init_irq(struct device *dev, int irq_num)
Balaji T Ke8deb282009-12-14 00:25:31 +0100344{
Benoit Cousson78518ff2012-02-29 19:40:31 +0100345 struct device_node *node = dev->of_node;
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300346 int nr_irqs;
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300347 int status;
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100348 u8 mask[3];
Benoit Cousson78518ff2012-02-29 19:40:31 +0100349
350 nr_irqs = TWL6030_NR_IRQS;
351
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100352 mask[0] = 0xFF;
Balaji T Ke8deb282009-12-14 00:25:31 +0100353 mask[1] = 0xFF;
354 mask[2] = 0xFF;
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100355
356 /* mask all int lines */
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300357 status = twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_LINE_A, 3);
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100358 /* mask all int sts */
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300359 status |= twl_i2c_write(TWL_MODULE_PIH, &mask[0], REG_INT_MSK_STS_A, 3);
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100360 /* clear INT_STS_A,B,C */
Grygorii Strashkoa820e562013-07-25 16:15:48 +0300361 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 }
Balaji T Ke8deb282009-12-14 00:25:31 +0100367
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100368 /*
369 * install an irq handler for each of the modules;
Balaji T Ke8deb282009-12-14 00:25:31 +0100370 * clone dummy irq_chip since PIH can't *do* anything
371 */
372 twl6030_irq_chip = dummy_irq_chip;
373 twl6030_irq_chip.name = "twl6030";
Lennert Buytenhekc45c6852010-12-13 13:31:18 +0100374 twl6030_irq_chip.irq_set_type = NULL;
Santosh Shilimkar49dcd072011-09-06 21:29:30 +0530375 twl6030_irq_chip.irq_set_wake = twl6030_irq_set_wake;
Balaji T Ke8deb282009-12-14 00:25:31 +0100376
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300377 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;
Balaji T Ke8deb282009-12-14 00:25:31 +0100382 }
383
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300384 dev_info(dev, "PIH (irq %d) nested IRQs\n", irq_num);
Balaji T Ke8deb282009-12-14 00:25:31 +0100385
386 /* install an irq handler to demultiplex the TWL6030 interrupt */
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300387 status = request_threaded_irq(irq_num, NULL, twl6030_irq_thread,
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300388 IRQF_ONESHOT, "TWL6030-PIH", irq_domain);
Balaji T Ke8deb282009-12-14 00:25:31 +0100389 if (status < 0) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +0100390 dev_err(dev, "could not claim irq %d: %d\n", irq_num, status);
Balaji T Ke8deb282009-12-14 00:25:31 +0100391 goto fail_irq;
392 }
Axel Lin862de702011-08-11 15:21:00 +0800393
Todd Poynorab2b9262011-10-04 11:52:29 +0200394 twl_irq = irq_num;
395 register_pm_notifier(&twl6030_irq_pm_notifier_block);
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300396 return 0;
Balaji T Ke8deb282009-12-14 00:25:31 +0100397
Axel Lin862de702011-08-11 15:21:00 +0800398fail_irq:
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300399 irq_domain_remove(irq_domain);
Balaji T Ke8deb282009-12-14 00:25:31 +0100400 return status;
401}
402
403int twl6030_exit_irq(void)
404{
Naga Venkata Srikanth V87343e52013-07-25 16:15:47 +0300405 if (twl_irq) {
406 unregister_pm_notifier(&twl6030_irq_pm_notifier_block);
407 free_irq(twl_irq, NULL);
Grygorii Strashkob32408f2013-07-25 16:15:49 +0300408 /*
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 */
Balaji T Ke8deb282009-12-14 00:25:31 +0100417 }
Balaji T Ke8deb282009-12-14 00:25:31 +0100418 return 0;
419}
420