2 * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
4 * Copyright (C) 2005-2006 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 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/kernel_stat.h>
31 #include <linux/init.h>
32 #include <linux/mutex.h>
33 #include <linux/interrupt.h>
34 #include <linux/irq.h>
35 #include <linux/random.h>
36 #include <linux/kthread.h>
37 #include <linux/platform_device.h>
38 #include <linux/clk.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c/twl4030.h>
42 #include <linux/i2c/twl4030-gpio.h>
43 #include <linux/i2c/twl4030-madc.h>
44 #include <linux/i2c/twl4030-pwrirq.h>
46 #define DRIVER_NAME "twl4030"
48 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
49 #define twl_has_rtc() true
51 #define twl_has_rtc() false
54 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
55 #define twl_has_keypad() true
57 #define twl_has_keypad() false
60 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
61 #define twl_has_usb() true
63 #define twl_has_usb() false
66 /* Primary Interrupt Handler on TWL4030 Registers */
68 /* Register Definitions */
70 #define REG_PIH_ISR_P1 (0x1)
71 #define REG_PIH_ISR_P2 (0x2)
72 #define REG_PIH_SIR (0x3)
74 /* Triton Core internal information (BEGIN) */
76 /* Last - for index max*/
77 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
79 #define TWL4030_NUM_SLAVES 4
82 #define TWL4030_SLAVENUM_NUM0 0x00
83 #define TWL4030_SLAVENUM_NUM1 0x01
84 #define TWL4030_SLAVENUM_NUM2 0x02
85 #define TWL4030_SLAVENUM_NUM3 0x03
87 /* Base Address defns */
89 #define TWL4030_BASEADD_USB 0x0000
91 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
92 #define TWL4030_BASEADD_GPIO 0x0098
94 #define TWL4030_BASEADD_INTBR 0x0085
95 #define TWL4030_BASEADD_PIH 0x0080
96 #define TWL4030_BASEADD_TEST 0x004C
98 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
99 #define TWL4030_BASEADD_LED 0x00EE
100 #define TWL4030_BASEADD_MADC 0x0000
101 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
102 #define TWL4030_BASEADD_PRECHARGE 0x00AA
103 #define TWL4030_BASEADD_PWM0 0x00F8
104 #define TWL4030_BASEADD_PWM1 0x00FB
105 #define TWL4030_BASEADD_PWMA 0x00EF
106 #define TWL4030_BASEADD_PWMB 0x00F1
107 #define TWL4030_BASEADD_KEYPAD 0x00D2
109 #define TWL4030_BASEADD_BACKUP 0x0014
110 #define TWL4030_BASEADD_INT 0x002E
111 #define TWL4030_BASEADD_PM_MASTER 0x0036
112 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
113 #define TWL4030_BASEADD_RTC 0x001C
114 #define TWL4030_BASEADD_SECURED_REG 0x0000
116 /* TWL4030 BCI registers */
117 #define TWL4030_INTERRUPTS_BCIIMR1A 0x2
118 #define TWL4030_INTERRUPTS_BCIIMR2A 0x3
119 #define TWL4030_INTERRUPTS_BCIIMR1B 0x6
120 #define TWL4030_INTERRUPTS_BCIIMR2B 0x7
121 #define TWL4030_INTERRUPTS_BCIISR1A 0x0
122 #define TWL4030_INTERRUPTS_BCIISR2A 0x1
123 #define TWL4030_INTERRUPTS_BCIISR1B 0x4
124 #define TWL4030_INTERRUPTS_BCIISR2B 0x5
126 /* TWL4030 keypad registers */
127 #define TWL4030_KEYPAD_KEYP_IMR1 0x12
128 #define TWL4030_KEYPAD_KEYP_IMR2 0x14
129 #define TWL4030_KEYPAD_KEYP_ISR1 0x11
130 #define TWL4030_KEYPAD_KEYP_ISR2 0x13
133 /* Triton Core internal information (END) */
135 /* Few power values */
136 #define R_CFG_BOOT 0x05
137 #define R_PROTECT_KEY 0x0E
140 #define KEY_UNLOCK1 0xce
141 #define KEY_UNLOCK2 0xec
142 #define KEY_LOCK 0x00
144 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
145 #define HFCLK_FREQ_26_MHZ (2 << 0)
146 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
147 #define HIGH_PERF_SQ (1 << 3)
149 /* SIH_CTRL registers that aren't defined elsewhere */
150 #define TWL4030_INTERRUPTS_BCISIHCTRL 0x0d
151 #define TWL4030_MADC_MADC_SIH_CTRL 0x67
152 #define TWL4030_KEYPAD_KEYP_SIH_CTRL 0x17
154 #define TWL4030_SIH_CTRL_COR_MASK (1 << 2)
157 * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
158 * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
159 * @sih_ctrl: address of module SIH_CTRL register
160 * @reg_cnt: number of IMR/ISR regs
161 * @imrs: pointer to array of TWL module interrupt mask register indices
162 * @isrs: pointer to array of TWL module interrupt status register indices
164 * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
165 * during twl_init_irq().
167 struct twl4030_mod_iregs {
175 /* TWL4030 INT module interrupt mask registers */
176 static const u8 __initconst twl4030_int_imr_regs[] = {
177 TWL4030_INT_PWR_IMR1,
178 TWL4030_INT_PWR_IMR2,
181 /* TWL4030 INT module interrupt status registers */
182 static const u8 __initconst twl4030_int_isr_regs[] = {
183 TWL4030_INT_PWR_ISR1,
184 TWL4030_INT_PWR_ISR2,
187 /* TWL4030 INTERRUPTS module interrupt mask registers */
188 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
189 TWL4030_INTERRUPTS_BCIIMR1A,
190 TWL4030_INTERRUPTS_BCIIMR1B,
191 TWL4030_INTERRUPTS_BCIIMR2A,
192 TWL4030_INTERRUPTS_BCIIMR2B,
195 /* TWL4030 INTERRUPTS module interrupt status registers */
196 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
197 TWL4030_INTERRUPTS_BCIISR1A,
198 TWL4030_INTERRUPTS_BCIISR1B,
199 TWL4030_INTERRUPTS_BCIISR2A,
200 TWL4030_INTERRUPTS_BCIISR2B,
203 /* TWL4030 MADC module interrupt mask registers */
204 static const u8 __initconst twl4030_madc_imr_regs[] = {
209 /* TWL4030 MADC module interrupt status registers */
210 static const u8 __initconst twl4030_madc_isr_regs[] = {
215 /* TWL4030 keypad module interrupt mask registers */
216 static const u8 __initconst twl4030_keypad_imr_regs[] = {
217 TWL4030_KEYPAD_KEYP_IMR1,
218 TWL4030_KEYPAD_KEYP_IMR2,
221 /* TWL4030 keypad module interrupt status registers */
222 static const u8 __initconst twl4030_keypad_isr_regs[] = {
223 TWL4030_KEYPAD_KEYP_ISR1,
224 TWL4030_KEYPAD_KEYP_ISR2,
227 /* TWL4030 GPIO module interrupt mask registers */
228 static const u8 __initconst twl4030_gpio_imr_regs[] = {
237 /* TWL4030 GPIO module interrupt status registers */
238 static const u8 __initconst twl4030_gpio_isr_regs[] = {
247 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
248 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
250 .mod_no = TWL4030_MODULE_INT,
251 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
252 .reg_cnt = ARRAY_SIZE(twl4030_int_imr_regs),
253 .imrs = twl4030_int_imr_regs,
254 .isrs = twl4030_int_isr_regs,
257 .mod_no = TWL4030_MODULE_INTERRUPTS,
258 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
259 .reg_cnt = ARRAY_SIZE(twl4030_interrupts_imr_regs),
260 .imrs = twl4030_interrupts_imr_regs,
261 .isrs = twl4030_interrupts_isr_regs,
264 .mod_no = TWL4030_MODULE_MADC,
265 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
266 .reg_cnt = ARRAY_SIZE(twl4030_madc_imr_regs),
267 .imrs = twl4030_madc_imr_regs,
268 .isrs = twl4030_madc_isr_regs,
271 .mod_no = TWL4030_MODULE_KEYPAD,
272 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
273 .reg_cnt = ARRAY_SIZE(twl4030_keypad_imr_regs),
274 .imrs = twl4030_keypad_imr_regs,
275 .isrs = twl4030_keypad_isr_regs,
278 .mod_no = TWL4030_MODULE_GPIO,
279 .sih_ctrl = REG_GPIO_SIH_CTRL,
280 .reg_cnt = ARRAY_SIZE(twl4030_gpio_imr_regs),
281 .imrs = twl4030_gpio_imr_regs,
282 .isrs = twl4030_gpio_isr_regs,
287 /* Data Structures */
288 /* To have info on T2 IRQ substem activated or not */
289 static struct completion irq_event;
291 /* Structure to define on TWL4030 Slave ID */
292 struct twl4030_client {
293 struct i2c_client *client;
297 /* max numb of i2c_msg required is for read =2 */
298 struct i2c_msg xfer_msg[2];
300 /* To lock access to xfer_msg */
301 struct mutex xfer_lock;
305 struct twl4030mapping {
306 unsigned char sid; /* Slave ID */
307 unsigned char base; /* base address */
310 /* mapping the module id to slave id and base address */
311 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
312 { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
313 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
314 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
315 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
316 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
317 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
318 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
319 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
320 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
321 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
322 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
323 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
324 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
325 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
326 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
327 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
328 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
329 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
330 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
331 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
332 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
333 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
336 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
339 * TWL4030 doesn't have PIH mask, hence dummy function for mask
343 static void twl4030_i2c_ackirq(unsigned int irq)
347 static void twl4030_i2c_disableint(unsigned int irq)
351 static void twl4030_i2c_enableint(unsigned int irq)
355 /* information for processing in the Work Item */
356 static struct irq_chip twl4030_irq_chip = {
358 .ack = twl4030_i2c_ackirq,
359 .mask = twl4030_i2c_disableint,
360 .unmask = twl4030_i2c_enableint,
363 /* Global Functions */
366 * twl4030_i2c_write - Writes a n bit register in TWL4030
367 * @mod_no: module number
368 * @value: an array of num_bytes+1 containing data to write
369 * @reg: register address (just offset will do)
370 * @num_bytes: number of bytes to transfer
372 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
373 * valid data starts at Offset 1.
375 * Returns the result of operation - 0 is success
377 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
381 struct twl4030_client *twl;
384 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
385 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
388 sid = twl4030_map[mod_no].sid;
389 twl = &twl4030_modules[sid];
391 if (unlikely(!twl->inuse)) {
392 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
395 mutex_lock(&twl->xfer_lock);
397 * [MSG1]: fill the register address data
398 * fill the data Tx buffer
400 msg = &twl->xfer_msg[0];
401 msg->addr = twl->address;
402 msg->len = num_bytes + 1;
405 /* over write the first byte of buffer with the register address */
406 *value = twl4030_map[mod_no].base + reg;
407 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
408 mutex_unlock(&twl->xfer_lock);
410 /* i2cTransfer returns num messages.translate it pls.. */
415 EXPORT_SYMBOL(twl4030_i2c_write);
418 * twl4030_i2c_read - Reads a n bit register in TWL4030
419 * @mod_no: module number
420 * @value: an array of num_bytes containing data to be read
421 * @reg: register address (just offset will do)
422 * @num_bytes: number of bytes to transfer
424 * Returns result of operation - num_bytes is success else failure.
426 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
431 struct twl4030_client *twl;
434 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
435 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
438 sid = twl4030_map[mod_no].sid;
439 twl = &twl4030_modules[sid];
441 if (unlikely(!twl->inuse)) {
442 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
445 mutex_lock(&twl->xfer_lock);
446 /* [MSG1] fill the register address data */
447 msg = &twl->xfer_msg[0];
448 msg->addr = twl->address;
450 msg->flags = 0; /* Read the register value */
451 val = twl4030_map[mod_no].base + reg;
453 /* [MSG2] fill the data rx buffer */
454 msg = &twl->xfer_msg[1];
455 msg->addr = twl->address;
456 msg->flags = I2C_M_RD; /* Read the register value */
457 msg->len = num_bytes; /* only n bytes */
459 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
460 mutex_unlock(&twl->xfer_lock);
462 /* i2cTransfer returns num messages.translate it pls.. */
467 EXPORT_SYMBOL(twl4030_i2c_read);
470 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
471 * @mod_no: module number
472 * @value: the value to be written 8 bit
473 * @reg: register address (just offset will do)
475 * Returns result of operation - 0 is success
477 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
480 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
481 u8 temp_buffer[2] = { 0 };
482 /* offset 1 contains the data */
483 temp_buffer[1] = value;
484 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
486 EXPORT_SYMBOL(twl4030_i2c_write_u8);
489 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
490 * @mod_no: module number
491 * @value: the value read 8 bit
492 * @reg: register address (just offset will do)
494 * Returns result of operation - 0 is success
496 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
498 return twl4030_i2c_read(mod_no, value, reg, 1);
500 EXPORT_SYMBOL(twl4030_i2c_read_u8);
502 /* Helper Functions */
505 * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
506 * module interrupts. It executes in kernel thread context.
507 * On entry, cpu interrupts are disabled.
509 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
511 struct irqaction *action;
512 const unsigned int cpu = smp_processor_id();
515 * Earlier this was desc->triggered = 1;
517 desc->status |= IRQ_LEVEL;
520 * The desc->handle method would normally call the desc->chip->ack
521 * method here, but we won't bother since our ack method is NULL.
525 kstat_cpu(cpu).irqs[irq]++;
527 action = desc->action;
536 /* Call the ISR with cpu interrupts enabled */
537 ret = action->handler(irq, action->dev_id);
538 if (ret == IRQ_HANDLED)
539 status |= action->flags;
541 action = action->next;
544 if (status & IRQF_SAMPLE_RANDOM)
545 add_interrupt_randomness(irq);
549 if (retval != IRQ_HANDLED)
550 printk(KERN_ERR "ISR for TWL4030 module"
551 " irq %d can't handle interrupt\n",
555 * Here is where we should call the unmask method, but
556 * again we won't bother since it is NULL.
559 printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
560 " but can't be masked!\n", irq);
562 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
563 " be masked!\n", irq);
566 static unsigned twl4030_irq_base;
569 * twl4030_irq_thread() runs as a kernel thread. It queries the twl4030
570 * interrupt controller to see which modules are generating interrupt requests
571 * and then calls the desc->handle method for each module requesting service.
573 static int twl4030_irq_thread(void *data)
576 irq_desc_t *desc = irq_desc + irq;
577 static unsigned i2c_errors;
578 const static unsigned max_i2c_errors = 100;
580 daemonize("twl4030-irq");
581 current->flags |= PF_NOFREEZE;
583 while (!kthread_should_stop()) {
588 wait_for_completion_interruptible(&irq_event);
590 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
593 printk(KERN_WARNING "I2C error %d while reading TWL4030"
594 " PIH ISR register.\n", ret);
595 if (++i2c_errors >= max_i2c_errors) {
596 printk(KERN_ERR "Maximum I2C error count"
597 " exceeded. Terminating %s.\n",
604 for (module_irq = twl4030_irq_base; 0 != pih_isr;
605 pih_isr >>= 1, module_irq++) {
607 irq_desc_t *d = irq_desc + module_irq;
611 d->handle_irq(module_irq, d);
617 desc->chip->unmask(irq);
624 * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
625 * This is a chained interrupt, so there is no desc->action method for it.
626 * Now we need to query the interrupt controller in the twl4030 to determine
627 * which module is generating the interrupt request. However, we can't do i2c
628 * transactions in interrupt context, so we must defer that work to a kernel
629 * thread. All we do here is acknowledge and mask the interrupt and wakeup
632 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
634 const unsigned int cpu = smp_processor_id();
637 * Earlier this was desc->triggered = 1;
639 desc->status |= IRQ_LEVEL;
642 * Acknowledge, clear _AND_ disable the interrupt.
644 desc->chip->ack(irq);
647 kstat_cpu(cpu).irqs[irq]++;
649 complete(&irq_event);
653 static int add_children(struct twl4030_platform_data *pdata)
655 struct platform_device *pdev = NULL;
656 struct twl4030_client *twl = NULL;
660 pdev = platform_device_alloc("twl4030_rtc", -1);
662 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
663 pdev->dev.parent = &twl->client->dev;
664 device_init_wakeup(&pdev->dev, 1);
667 * FIXME add the relevant IRQ resource, and make the
668 * rtc driver use it instead of hard-wiring ...
670 * REVISIT platform_data here currently only supports
671 * setting up the "msecure" line ... which actually
672 * violates the "princple of least privilege", since
673 * it's effectively always in "high trust" mode.
675 * For now, expect equivalent treatment at board init:
676 * setting msecure high. Eventually, Linux might
677 * become more aware of those HW security concerns.
680 status = platform_device_add(pdev);
682 platform_device_put(pdev);
689 if (twl_has_keypad() && pdata->keypad) {
690 pdev = platform_device_alloc("twl4030_keypad", -1);
692 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
693 pdev->dev.parent = &twl->client->dev;
694 device_init_wakeup(&pdev->dev, 1);
695 status = platform_device_add_data(pdev, pdata->keypad,
696 sizeof(*pdata->keypad));
698 platform_device_put(pdev);
701 status = platform_device_add(pdev);
703 platform_device_put(pdev);
710 if (twl_has_usb() && pdata->usb) {
711 pdev = platform_device_alloc("twl4030_usb", -1);
713 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM0];
714 pdev->dev.parent = &twl->client->dev;
715 device_init_wakeup(&pdev->dev, 1);
716 status = platform_device_add_data(pdev, pdata->usb,
717 sizeof(*pdata->usb));
719 platform_device_put(pdev);
722 status = platform_device_add(pdev);
724 platform_device_put(pdev);
732 pr_err("failed to add twl4030's children\n");
736 static struct task_struct * __init start_twl4030_irq_thread(int irq)
738 struct task_struct *thread;
740 init_completion(&irq_event);
741 thread = kthread_run(twl4030_irq_thread, (void *)irq,
742 "twl4030 irq %d", irq);
744 pr_err("%s: could not create twl4030 irq %d thread!\n",
751 * These three functions should be part of Voltage frame work
752 * added here to complete the functionality for now.
754 static int __init protect_pm_master(void)
758 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
763 static int __init unprotect_pm_master(void)
767 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
769 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
774 static int __init power_companion_init(void)
778 u8 ctrl = HFCLK_FREQ_26_MHZ;
781 if (cpu_is_omap2430())
782 osc = clk_get(NULL, "osc_ck");
784 osc = clk_get(NULL, "osc_sys_ck");
786 printk(KERN_WARNING "Skipping twl3040 internal clock init and "
787 "using bootloader value (unknown osc rate)\n");
791 rate = clk_get_rate(osc);
796 ctrl = HFCLK_FREQ_19p2_MHZ;
799 ctrl = HFCLK_FREQ_26_MHZ;
802 ctrl = HFCLK_FREQ_38p4_MHZ;
806 ctrl |= HIGH_PERF_SQ;
807 e |= unprotect_pm_master();
808 /* effect->MADC+USB ck en */
809 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
810 e |= protect_pm_master();
816 * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
817 * @mod_no: TWL4030 module number
818 * @reg: register index to clear
819 * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
821 * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
822 * status register to ensure that any prior interrupts are cleared.
823 * Returns the status from the I2C read operation.
825 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
829 return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
830 twl4030_i2c_write_u8(mod_no, 0xff, reg);
834 * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
835 * @mod_no: TWL4030 module number
836 * @reg: register index to clear
838 * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
839 * the specified TWL module are cleared by reads, or 0 if cleared by
842 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
846 WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
848 tmp &= TWL4030_SIH_CTRL_COR_MASK;
849 tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
855 * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
856 * @t: pointer to twl4030_mod_iregs array
857 * @t_sz: ARRAY_SIZE(t) (starting at 1)
859 * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
860 * interrupt status registers (ISRs). No return value, but will WARN if
861 * any I2C operations fail.
863 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
869 * N.B. - further efficiency is possible here. Eight I2C
870 * operations on BCI and GPIO modules are avoidable if I2C
871 * burst read/write transactions were implemented. Would
872 * probably save about 1ms of boot time and a small amount of
875 for (i = 0; i < t_sz; i++) {
876 const struct twl4030_mod_iregs tmr = t[i];
879 /* Are ISRs cleared by reads or writes? */
880 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
883 for (j = 0; j < tmr.reg_cnt; j++) {
885 /* Mask interrupts at the TWL4030 */
886 WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
889 /* Clear TWL4030 ISRs */
890 WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
891 tmr.isrs[j], cor) < 0);
899 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
903 char *msg = "Unable to register interrupt subsystem";
906 * Mask and clear all TWL4030 interrupts since initially we do
907 * not have any TWL4030 module interrupt handlers present
909 twl4030_mask_clear_intrs(twl4030_mod_regs,
910 ARRAY_SIZE(twl4030_mod_regs));
912 twl4030_irq_base = irq_base;
914 /* install an irq handler for each of the PIH modules */
915 for (i = irq_base; i < irq_end; i++) {
916 set_irq_chip(i, &twl4030_irq_chip);
917 set_irq_handler(i, do_twl4030_module_irq);
918 set_irq_flags(i, IRQF_VALID);
921 /* install an irq handler to demultiplex the TWL4030 interrupt */
922 set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
923 set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
924 set_irq_chained_handler(irq_num, do_twl4030_irq);
926 res = power_companion_init();
928 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
931 /*----------------------------------------------------------------------*/
933 static int twl4030_remove(struct i2c_client *client)
937 /* FIXME undo twl_init_irq() */
938 if (twl4030_irq_base) {
939 dev_err(&client->dev, "can't yet clean up IRQs?\n");
943 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
944 struct twl4030_client *twl = &twl4030_modules[i];
946 if (twl->client && twl->client != client)
947 i2c_unregister_device(twl->client);
948 twl4030_modules[i].client = NULL;
949 twl4030_modules[i].inuse = false;
954 /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
956 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
960 struct twl4030_platform_data *pdata = client->dev.platform_data;
963 dev_dbg(&client->dev, "no platform data?\n");
967 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
968 dev_dbg(&client->dev, "can't talk I2C?\n");
972 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
973 if (twl4030_modules[i].inuse || twl4030_irq_base) {
974 dev_dbg(&client->dev, "driver is already in use\n");
979 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
980 struct twl4030_client *twl = &twl4030_modules[i];
982 twl->address = client->addr + i;
984 twl->client = client;
986 twl->client = i2c_new_dummy(client->adapter,
989 dev_err(&twl->client->dev,
990 "can't attach client %d\n", i);
994 strlcpy(twl->client->name, id->name,
995 sizeof(twl->client->name));
998 mutex_init(&twl->xfer_lock);
1001 status = add_children(pdata);
1006 * Check if the PIH module is initialized, if yes, then init
1007 * the T2 Interrupt subsystem
1009 if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
1010 && twl4030_irq_base == 0
1013 && pdata->irq_end > pdata->irq_base)
1014 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
1016 dev_info(&client->dev, "chaining %d irqs\n",
1018 ? (pdata->irq_end - pdata->irq_base)
1023 twl4030_remove(client);
1027 static const struct i2c_device_id twl4030_ids[] = {
1028 { "twl4030", 0 }, /* "Triton 2" */
1029 { "tps65950", 0 }, /* catalog version of twl4030 */
1030 { "tps65930", 0 }, /* fewer LDOs and DACs; no charger */
1031 { "tps65920", 0 }, /* fewer LDOs; no codec or charger */
1032 { /* end of list */ },
1034 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1036 /* One Client Driver , 4 Clients */
1037 static struct i2c_driver twl4030_driver = {
1038 .driver.name = DRIVER_NAME,
1039 .id_table = twl4030_ids,
1040 .probe = twl4030_probe,
1041 .remove = twl4030_remove,
1044 static int __init twl4030_init(void)
1046 return i2c_add_driver(&twl4030_driver);
1048 subsys_initcall(twl4030_init);
1050 static void __exit twl4030_exit(void)
1052 i2c_del_driver(&twl4030_driver);
1054 module_exit(twl4030_exit);
1056 MODULE_AUTHOR("Texas Instruments, Inc.");
1057 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1058 MODULE_LICENSE("GPL");