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-madc.h>
44 #define DRIVER_NAME "twl4030"
46 #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
47 defined(CONFIG_TWL4030_BCI_BATTERY_MODUEL)
48 #define twl_has_bci() true
50 #define twl_has_bci() false
53 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
54 #define twl_has_keypad() true
56 #define twl_has_keypad() false
59 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
60 #define twl_has_gpio() true
62 #define twl_has_gpio() false
65 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
66 #define twl_has_madc() true
68 #define twl_has_madc() false
71 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
72 #define twl_has_rtc() true
74 #define twl_has_rtc() false
77 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
78 #define twl_has_usb() true
80 #define twl_has_usb() false
83 static inline void activate_irq(int irq)
86 /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
87 * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
89 set_irq_flags(irq, IRQF_VALID);
91 /* same effect on other architectures */
96 /* Primary Interrupt Handler on TWL4030 Registers */
98 /* Register Definitions */
100 #define REG_PIH_ISR_P1 (0x1)
101 #define REG_PIH_ISR_P2 (0x2)
102 #define REG_PIH_SIR (0x3)
104 /* Triton Core internal information (BEGIN) */
106 /* Last - for index max*/
107 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
109 #define TWL4030_NUM_SLAVES 4
112 #define TWL4030_SLAVENUM_NUM0 0x00
113 #define TWL4030_SLAVENUM_NUM1 0x01
114 #define TWL4030_SLAVENUM_NUM2 0x02
115 #define TWL4030_SLAVENUM_NUM3 0x03
117 /* Base Address defns */
119 #define TWL4030_BASEADD_USB 0x0000
121 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
122 #define TWL4030_BASEADD_GPIO 0x0098
124 #define TWL4030_BASEADD_INTBR 0x0085
125 #define TWL4030_BASEADD_PIH 0x0080
126 #define TWL4030_BASEADD_TEST 0x004C
128 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
129 #define TWL4030_BASEADD_LED 0x00EE
130 #define TWL4030_BASEADD_MADC 0x0000
131 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
132 #define TWL4030_BASEADD_PRECHARGE 0x00AA
133 #define TWL4030_BASEADD_PWM0 0x00F8
134 #define TWL4030_BASEADD_PWM1 0x00FB
135 #define TWL4030_BASEADD_PWMA 0x00EF
136 #define TWL4030_BASEADD_PWMB 0x00F1
137 #define TWL4030_BASEADD_KEYPAD 0x00D2
139 #define TWL4030_BASEADD_BACKUP 0x0014
140 #define TWL4030_BASEADD_INT 0x002E
141 #define TWL4030_BASEADD_PM_MASTER 0x0036
142 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
143 #define TWL4030_BASEADD_RTC 0x001C
144 #define TWL4030_BASEADD_SECURED_REG 0x0000
146 /* TWL4030 BCI registers */
147 #define TWL4030_INTERRUPTS_BCIIMR1A 0x2
148 #define TWL4030_INTERRUPTS_BCIIMR2A 0x3
149 #define TWL4030_INTERRUPTS_BCIIMR1B 0x6
150 #define TWL4030_INTERRUPTS_BCIIMR2B 0x7
151 #define TWL4030_INTERRUPTS_BCIISR1A 0x0
152 #define TWL4030_INTERRUPTS_BCIISR2A 0x1
153 #define TWL4030_INTERRUPTS_BCIISR1B 0x4
154 #define TWL4030_INTERRUPTS_BCIISR2B 0x5
156 /* TWL4030 keypad registers */
157 #define TWL4030_KEYPAD_KEYP_IMR1 0x12
158 #define TWL4030_KEYPAD_KEYP_IMR2 0x14
159 #define TWL4030_KEYPAD_KEYP_ISR1 0x11
160 #define TWL4030_KEYPAD_KEYP_ISR2 0x13
163 /* Triton Core internal information (END) */
165 /* Few power values */
166 #define R_CFG_BOOT 0x05
167 #define R_PROTECT_KEY 0x0E
170 #define KEY_UNLOCK1 0xce
171 #define KEY_UNLOCK2 0xec
172 #define KEY_LOCK 0x00
174 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
175 #define HFCLK_FREQ_26_MHZ (2 << 0)
176 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
177 #define HIGH_PERF_SQ (1 << 3)
179 /* SIH_CTRL registers that aren't defined elsewhere */
180 #define TWL4030_INTERRUPTS_BCISIHCTRL 0x0d
181 #define TWL4030_MADC_MADC_SIH_CTRL 0x67
182 #define TWL4030_KEYPAD_KEYP_SIH_CTRL 0x17
186 * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
187 * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
188 * @sih_ctrl: address of module SIH_CTRL register
189 * @reg_cnt: number of IMR/ISR regs
190 * @imrs: pointer to array of TWL module interrupt mask register indices
191 * @isrs: pointer to array of TWL module interrupt status register indices
193 * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
194 * during twl_init_irq().
196 struct twl4030_mod_iregs {
204 /* TWL4030 INT module interrupt mask registers */
205 static const u8 __initconst twl4030_int_imr_regs[] = {
206 TWL4030_INT_PWR_IMR1,
207 TWL4030_INT_PWR_IMR2,
210 /* TWL4030 INT module interrupt status registers */
211 static const u8 __initconst twl4030_int_isr_regs[] = {
212 TWL4030_INT_PWR_ISR1,
213 TWL4030_INT_PWR_ISR2,
216 /* TWL4030 INTERRUPTS module interrupt mask registers */
217 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
218 TWL4030_INTERRUPTS_BCIIMR1A,
219 TWL4030_INTERRUPTS_BCIIMR1B,
220 TWL4030_INTERRUPTS_BCIIMR2A,
221 TWL4030_INTERRUPTS_BCIIMR2B,
224 /* TWL4030 INTERRUPTS module interrupt status registers */
225 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
226 TWL4030_INTERRUPTS_BCIISR1A,
227 TWL4030_INTERRUPTS_BCIISR1B,
228 TWL4030_INTERRUPTS_BCIISR2A,
229 TWL4030_INTERRUPTS_BCIISR2B,
232 /* TWL4030 MADC module interrupt mask registers */
233 static const u8 __initconst twl4030_madc_imr_regs[] = {
238 /* TWL4030 MADC module interrupt status registers */
239 static const u8 __initconst twl4030_madc_isr_regs[] = {
244 /* TWL4030 keypad module interrupt mask registers */
245 static const u8 __initconst twl4030_keypad_imr_regs[] = {
246 TWL4030_KEYPAD_KEYP_IMR1,
247 TWL4030_KEYPAD_KEYP_IMR2,
250 /* TWL4030 keypad module interrupt status registers */
251 static const u8 __initconst twl4030_keypad_isr_regs[] = {
252 TWL4030_KEYPAD_KEYP_ISR1,
253 TWL4030_KEYPAD_KEYP_ISR2,
256 /* TWL4030 GPIO module interrupt mask registers */
257 static const u8 __initconst twl4030_gpio_imr_regs[] = {
266 /* TWL4030 GPIO module interrupt status registers */
267 static const u8 __initconst twl4030_gpio_isr_regs[] = {
276 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
277 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
279 .mod_no = TWL4030_MODULE_INT,
280 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
281 .reg_cnt = ARRAY_SIZE(twl4030_int_imr_regs),
282 .imrs = twl4030_int_imr_regs,
283 .isrs = twl4030_int_isr_regs,
286 .mod_no = TWL4030_MODULE_INTERRUPTS,
287 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
288 .reg_cnt = ARRAY_SIZE(twl4030_interrupts_imr_regs),
289 .imrs = twl4030_interrupts_imr_regs,
290 .isrs = twl4030_interrupts_isr_regs,
293 .mod_no = TWL4030_MODULE_MADC,
294 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
295 .reg_cnt = ARRAY_SIZE(twl4030_madc_imr_regs),
296 .imrs = twl4030_madc_imr_regs,
297 .isrs = twl4030_madc_isr_regs,
300 .mod_no = TWL4030_MODULE_KEYPAD,
301 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
302 .reg_cnt = ARRAY_SIZE(twl4030_keypad_imr_regs),
303 .imrs = twl4030_keypad_imr_regs,
304 .isrs = twl4030_keypad_isr_regs,
307 .mod_no = TWL4030_MODULE_GPIO,
308 .sih_ctrl = REG_GPIO_SIH_CTRL,
309 .reg_cnt = ARRAY_SIZE(twl4030_gpio_imr_regs),
310 .imrs = twl4030_gpio_imr_regs,
311 .isrs = twl4030_gpio_isr_regs,
316 /* Data Structures */
317 /* To have info on T2 IRQ substem activated or not */
318 static struct completion irq_event;
320 /* Structure to define on TWL4030 Slave ID */
321 struct twl4030_client {
322 struct i2c_client *client;
326 /* max numb of i2c_msg required is for read =2 */
327 struct i2c_msg xfer_msg[2];
329 /* To lock access to xfer_msg */
330 struct mutex xfer_lock;
334 struct twl4030mapping {
335 unsigned char sid; /* Slave ID */
336 unsigned char base; /* base address */
339 /* mapping the module id to slave id and base address */
340 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
342 * NOTE: don't change this table without updating the
343 * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
344 * so they continue to match the order in this table.
347 { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
349 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
350 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
351 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
352 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
353 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
355 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
356 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
357 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
358 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
359 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
360 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
361 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
362 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
363 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
364 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
366 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
367 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
368 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
369 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
370 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
371 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
374 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
377 * TWL4030 doesn't have PIH mask, hence dummy function for mask
381 static void twl4030_i2c_ackirq(unsigned int irq)
385 static void twl4030_i2c_disableint(unsigned int irq)
389 static void twl4030_i2c_enableint(unsigned int irq)
393 /* information for processing in the Work Item */
394 static struct irq_chip twl4030_irq_chip = {
396 .ack = twl4030_i2c_ackirq,
397 .mask = twl4030_i2c_disableint,
398 .unmask = twl4030_i2c_enableint,
401 /* Global Functions */
404 * twl4030_i2c_write - Writes a n bit register in TWL4030
405 * @mod_no: module number
406 * @value: an array of num_bytes+1 containing data to write
407 * @reg: register address (just offset will do)
408 * @num_bytes: number of bytes to transfer
410 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
411 * valid data starts at Offset 1.
413 * Returns the result of operation - 0 is success
415 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
419 struct twl4030_client *twl;
422 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
423 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
426 sid = twl4030_map[mod_no].sid;
427 twl = &twl4030_modules[sid];
429 if (unlikely(!twl->inuse)) {
430 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
433 mutex_lock(&twl->xfer_lock);
435 * [MSG1]: fill the register address data
436 * fill the data Tx buffer
438 msg = &twl->xfer_msg[0];
439 msg->addr = twl->address;
440 msg->len = num_bytes + 1;
443 /* over write the first byte of buffer with the register address */
444 *value = twl4030_map[mod_no].base + reg;
445 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
446 mutex_unlock(&twl->xfer_lock);
448 /* i2cTransfer returns num messages.translate it pls.. */
453 EXPORT_SYMBOL(twl4030_i2c_write);
456 * twl4030_i2c_read - Reads a n bit register in TWL4030
457 * @mod_no: module number
458 * @value: an array of num_bytes containing data to be read
459 * @reg: register address (just offset will do)
460 * @num_bytes: number of bytes to transfer
462 * Returns result of operation - num_bytes is success else failure.
464 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
469 struct twl4030_client *twl;
472 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
473 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
476 sid = twl4030_map[mod_no].sid;
477 twl = &twl4030_modules[sid];
479 if (unlikely(!twl->inuse)) {
480 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
483 mutex_lock(&twl->xfer_lock);
484 /* [MSG1] fill the register address data */
485 msg = &twl->xfer_msg[0];
486 msg->addr = twl->address;
488 msg->flags = 0; /* Read the register value */
489 val = twl4030_map[mod_no].base + reg;
491 /* [MSG2] fill the data rx buffer */
492 msg = &twl->xfer_msg[1];
493 msg->addr = twl->address;
494 msg->flags = I2C_M_RD; /* Read the register value */
495 msg->len = num_bytes; /* only n bytes */
497 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
498 mutex_unlock(&twl->xfer_lock);
500 /* i2cTransfer returns num messages.translate it pls.. */
505 EXPORT_SYMBOL(twl4030_i2c_read);
508 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
509 * @mod_no: module number
510 * @value: the value to be written 8 bit
511 * @reg: register address (just offset will do)
513 * Returns result of operation - 0 is success
515 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
518 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
519 u8 temp_buffer[2] = { 0 };
520 /* offset 1 contains the data */
521 temp_buffer[1] = value;
522 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
524 EXPORT_SYMBOL(twl4030_i2c_write_u8);
527 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
528 * @mod_no: module number
529 * @value: the value read 8 bit
530 * @reg: register address (just offset will do)
532 * Returns result of operation - 0 is success
534 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
536 return twl4030_i2c_read(mod_no, value, reg, 1);
538 EXPORT_SYMBOL(twl4030_i2c_read_u8);
540 /* Helper Functions */
543 * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
544 * module interrupts. It executes in kernel thread context.
545 * On entry, cpu interrupts are disabled.
547 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
549 struct irqaction *action;
550 const unsigned int cpu = smp_processor_id();
553 * Earlier this was desc->triggered = 1;
555 desc->status |= IRQ_LEVEL;
558 * The desc->handle method would normally call the desc->chip->ack
559 * method here, but we won't bother since our ack method is NULL.
563 kstat_cpu(cpu).irqs[irq]++;
565 action = desc->action;
574 /* Call the ISR with cpu interrupts enabled */
575 ret = action->handler(irq, action->dev_id);
576 if (ret == IRQ_HANDLED)
577 status |= action->flags;
579 action = action->next;
582 if (status & IRQF_SAMPLE_RANDOM)
583 add_interrupt_randomness(irq);
587 if (retval != IRQ_HANDLED)
588 printk(KERN_ERR "ISR for TWL4030 module"
589 " irq %d can't handle interrupt\n",
593 * Here is where we should call the unmask method, but
594 * again we won't bother since it is NULL.
597 printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
598 " but can't be masked!\n", irq);
600 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
601 " be masked!\n", irq);
604 static unsigned twl4030_irq_base;
607 * twl4030_irq_thread() runs as a kernel thread. It queries the twl4030
608 * interrupt controller to see which modules are generating interrupt requests
609 * and then calls the desc->handle method for each module requesting service.
611 static int twl4030_irq_thread(void *data)
613 long irq = (long)data;
614 irq_desc_t *desc = irq_desc + irq;
615 static unsigned i2c_errors;
616 const static unsigned max_i2c_errors = 100;
618 daemonize("twl4030-irq");
619 current->flags |= PF_NOFREEZE;
621 while (!kthread_should_stop()) {
626 wait_for_completion_interruptible(&irq_event);
628 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
631 printk(KERN_WARNING "I2C error %d while reading TWL4030"
632 " PIH ISR register.\n", ret);
633 if (++i2c_errors >= max_i2c_errors) {
634 printk(KERN_ERR "Maximum I2C error count"
635 " exceeded. Terminating %s.\n",
642 for (module_irq = twl4030_irq_base; 0 != pih_isr;
643 pih_isr >>= 1, module_irq++) {
645 irq_desc_t *d = irq_desc + module_irq;
649 d->handle_irq(module_irq, d);
655 desc->chip->unmask(irq);
662 * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
663 * This is a chained interrupt, so there is no desc->action method for it.
664 * Now we need to query the interrupt controller in the twl4030 to determine
665 * which module is generating the interrupt request. However, we can't do i2c
666 * transactions in interrupt context, so we must defer that work to a kernel
667 * thread. All we do here is acknowledge and mask the interrupt and wakeup
670 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
672 const unsigned int cpu = smp_processor_id();
675 * Earlier this was desc->triggered = 1;
677 desc->status |= IRQ_LEVEL;
680 * Acknowledge, clear _AND_ disable the interrupt.
682 desc->chip->ack(irq);
685 kstat_cpu(cpu).irqs[irq]++;
687 complete(&irq_event);
691 static int add_children(struct twl4030_platform_data *pdata)
693 struct platform_device *pdev = NULL;
694 struct twl4030_client *twl = NULL;
697 if (twl_has_bci() && pdata->bci) {
698 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
700 pdev = platform_device_alloc("twl4030_bci", -1);
702 pr_debug("%s: can't alloc bci dev\n", DRIVER_NAME);
708 pdev->dev.parent = &twl->client->dev;
709 status = platform_device_add_data(pdev, pdata->bci,
710 sizeof(*pdata->bci));
712 dev_dbg(&twl->client->dev,
713 "can't add bci data, %d\n",
720 struct resource r = {
721 .start = TWL4030_PWRIRQ_CHG_PRES,
722 .flags = IORESOURCE_IRQ,
725 status = platform_device_add_resources(pdev, &r, 1);
729 status = platform_device_add(pdev);
732 platform_device_put(pdev);
733 dev_dbg(&twl->client->dev,
734 "can't create bci dev, %d\n",
740 if (twl_has_gpio() && pdata->gpio) {
741 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM1];
743 pdev = platform_device_alloc("twl4030_gpio", -1);
745 pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
750 /* more driver model init */
752 pdev->dev.parent = &twl->client->dev;
753 /* device_init_wakeup(&pdev->dev, 1); */
755 status = platform_device_add_data(pdev, pdata->gpio,
756 sizeof(*pdata->gpio));
758 dev_dbg(&twl->client->dev,
759 "can't add gpio data, %d\n",
765 /* GPIO module IRQ */
767 struct resource r = {
768 .start = pdata->irq_base + 0,
769 .flags = IORESOURCE_IRQ,
772 status = platform_device_add_resources(pdev, &r, 1);
776 status = platform_device_add(pdev);
779 platform_device_put(pdev);
780 dev_dbg(&twl->client->dev,
781 "can't create gpio dev, %d\n",
787 if (twl_has_keypad() && pdata->keypad) {
788 pdev = platform_device_alloc("twl4030_keypad", -1);
790 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
791 pdev->dev.parent = &twl->client->dev;
792 device_init_wakeup(&pdev->dev, 1);
793 status = platform_device_add_data(pdev, pdata->keypad,
794 sizeof(*pdata->keypad));
796 dev_dbg(&twl->client->dev,
797 "can't add keypad data, %d\n",
799 platform_device_put(pdev);
802 status = platform_device_add(pdev);
804 platform_device_put(pdev);
805 dev_dbg(&twl->client->dev,
806 "can't create keypad dev, %d\n",
811 pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
817 if (twl_has_madc() && pdata->madc) {
818 pdev = platform_device_alloc("twl4030_madc", -1);
820 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
821 pdev->dev.parent = &twl->client->dev;
822 device_init_wakeup(&pdev->dev, 1);
823 status = platform_device_add_data(pdev, pdata->madc,
824 sizeof(*pdata->madc));
826 platform_device_put(pdev);
827 dev_dbg(&twl->client->dev,
828 "can't add madc data, %d\n",
832 status = platform_device_add(pdev);
834 platform_device_put(pdev);
835 dev_dbg(&twl->client->dev,
836 "can't create madc dev, %d\n",
841 pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
848 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
850 pdev = platform_device_alloc("twl4030_rtc", -1);
852 pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
855 pdev->dev.parent = &twl->client->dev;
856 device_init_wakeup(&pdev->dev, 1);
860 * REVISIT platform_data here currently might use of
861 * "msecure" line ... but for now we just expect board
862 * setup to tell the chip "we are secure" at all times.
863 * Eventually, Linux might become more aware of such
864 * HW security concerns, and "least privilege".
869 struct resource r = {
870 /* REVISIT don't hard-wire this stuff */
871 .start = TWL4030_PWRIRQ_RTC,
872 .flags = IORESOURCE_IRQ,
875 status = platform_device_add_resources(pdev, &r, 1);
879 status = platform_device_add(pdev);
882 platform_device_put(pdev);
883 dev_dbg(&twl->client->dev,
884 "can't create rtc dev, %d\n",
890 if (twl_has_usb() && pdata->usb) {
891 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM0];
893 pdev = platform_device_alloc("twl4030_usb", -1);
895 pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
901 pdev->dev.parent = &twl->client->dev;
902 device_init_wakeup(&pdev->dev, 1);
903 status = platform_device_add_data(pdev, pdata->usb,
904 sizeof(*pdata->usb));
906 platform_device_put(pdev);
907 dev_dbg(&twl->client->dev,
908 "can't add usb data, %d\n",
915 struct resource r = {
916 .start = TWL4030_PWRIRQ_USB_PRES,
917 .flags = IORESOURCE_IRQ,
920 status = platform_device_add_resources(pdev, &r, 1);
924 status = platform_device_add(pdev);
927 platform_device_put(pdev);
928 dev_dbg(&twl->client->dev,
929 "can't create usb dev, %d\n",
936 pr_err("failed to add twl4030's children (status %d)\n", status);
940 static struct task_struct * __init start_twl4030_irq_thread(long irq)
942 struct task_struct *thread;
944 init_completion(&irq_event);
945 thread = kthread_run(twl4030_irq_thread, (void *)irq,
946 "twl4030 irq %ld", irq);
948 pr_err("%s: could not create twl4030 irq %ld thread!\n",
955 * These three functions should be part of Voltage frame work
956 * added here to complete the functionality for now.
958 static int __init protect_pm_master(void)
962 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
967 static int __init unprotect_pm_master(void)
971 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
973 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
978 static int __init power_companion_init(void)
983 u8 ctrl = HFCLK_FREQ_26_MHZ;
985 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
986 if (cpu_is_omap2430())
987 osc = clk_get(NULL, "osc_ck");
989 osc = clk_get(NULL, "osc_sys_ck");
994 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
995 "using bootloader value (unknown osc rate)\n");
999 rate = clk_get_rate(osc);
1004 ctrl = HFCLK_FREQ_19p2_MHZ;
1007 ctrl = HFCLK_FREQ_26_MHZ;
1010 ctrl = HFCLK_FREQ_38p4_MHZ;
1014 ctrl |= HIGH_PERF_SQ;
1015 e |= unprotect_pm_master();
1016 /* effect->MADC+USB ck en */
1017 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
1018 e |= protect_pm_master();
1024 * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
1025 * @mod_no: TWL4030 module number
1026 * @reg: register index to clear
1027 * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
1029 * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
1030 * status register to ensure that any prior interrupts are cleared.
1031 * Returns the status from the I2C read operation.
1033 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
1037 return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
1038 twl4030_i2c_write_u8(mod_no, 0xff, reg);
1042 * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
1043 * @mod_no: TWL4030 module number
1044 * @reg: register index to clear
1046 * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
1047 * the specified TWL module are cleared by reads, or 0 if cleared by
1050 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
1054 WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
1056 tmp &= TWL4030_SIH_CTRL_COR_MASK;
1057 tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
1063 * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
1064 * @t: pointer to twl4030_mod_iregs array
1065 * @t_sz: ARRAY_SIZE(t) (starting at 1)
1067 * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
1068 * interrupt status registers (ISRs). No return value, but will WARN if
1069 * any I2C operations fail.
1071 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
1077 * N.B. - further efficiency is possible here. Eight I2C
1078 * operations on BCI and GPIO modules are avoidable if I2C
1079 * burst read/write transactions were implemented. Would
1080 * probably save about 1ms of boot time and a small amount of
1083 for (i = 0; i < t_sz; i++) {
1084 const struct twl4030_mod_iregs tmr = t[i];
1087 /* Are ISRs cleared by reads or writes? */
1088 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
1090 for (j = 0; j < tmr.reg_cnt; j++) {
1092 /* Mask interrupts at the TWL4030 */
1093 WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
1096 /* Clear TWL4030 ISRs */
1097 WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
1098 tmr.isrs[j], cor) < 0);
1106 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
1110 char *msg = "Unable to register interrupt subsystem";
1113 * Mask and clear all TWL4030 interrupts since initially we do
1114 * not have any TWL4030 module interrupt handlers present
1116 twl4030_mask_clear_intrs(twl4030_mod_regs,
1117 ARRAY_SIZE(twl4030_mod_regs));
1119 twl4030_irq_base = irq_base;
1121 /* install an irq handler for each of the PIH modules */
1122 for (i = irq_base; i < irq_end; i++) {
1123 set_irq_chip_and_handler(i, &twl4030_irq_chip,
1124 do_twl4030_module_irq);
1128 /* install an irq handler to demultiplex the TWL4030 interrupt */
1129 set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
1130 set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
1131 set_irq_chained_handler(irq_num, do_twl4030_irq);
1133 res = power_companion_init();
1135 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
1138 /*----------------------------------------------------------------------*/
1140 static int twl4030_remove(struct i2c_client *client)
1144 /* FIXME undo twl_init_irq() */
1145 if (twl4030_irq_base) {
1146 dev_err(&client->dev, "can't yet clean up IRQs?\n");
1150 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1151 struct twl4030_client *twl = &twl4030_modules[i];
1153 if (twl->client && twl->client != client)
1154 i2c_unregister_device(twl->client);
1155 twl4030_modules[i].client = NULL;
1156 twl4030_modules[i].inuse = false;
1161 /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
1163 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
1167 struct twl4030_platform_data *pdata = client->dev.platform_data;
1170 dev_dbg(&client->dev, "no platform data?\n");
1174 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1175 dev_dbg(&client->dev, "can't talk I2C?\n");
1179 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1180 if (twl4030_modules[i].inuse || twl4030_irq_base) {
1181 dev_dbg(&client->dev, "driver is already in use\n");
1186 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1187 struct twl4030_client *twl = &twl4030_modules[i];
1189 twl->address = client->addr + i;
1191 twl->client = client;
1193 twl->client = i2c_new_dummy(client->adapter,
1196 dev_err(&twl->client->dev,
1197 "can't attach client %d\n", i);
1201 strlcpy(twl->client->name, id->name,
1202 sizeof(twl->client->name));
1205 mutex_init(&twl->xfer_lock);
1209 * Check if the PIH module is initialized, if yes, then init
1210 * the T2 Interrupt subsystem
1212 if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
1213 && twl4030_irq_base == 0
1216 && pdata->irq_end > pdata->irq_base) {
1217 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
1218 dev_info(&client->dev, "IRQ %d chains IRQs %d..%d\n",
1219 client->irq, pdata->irq_base, pdata->irq_end - 1);
1222 status = add_children(pdata);
1225 twl4030_remove(client);
1229 static const struct i2c_device_id twl4030_ids[] = {
1230 { "twl4030", 0 }, /* "Triton 2" */
1231 { "tps65950", 0 }, /* catalog version of twl4030 */
1232 { "tps65930", 0 }, /* fewer LDOs and DACs; no charger */
1233 { "tps65920", 0 }, /* fewer LDOs; no codec or charger */
1234 { /* end of list */ },
1236 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1238 /* One Client Driver , 4 Clients */
1239 static struct i2c_driver twl4030_driver = {
1240 .driver.name = DRIVER_NAME,
1241 .id_table = twl4030_ids,
1242 .probe = twl4030_probe,
1243 .remove = twl4030_remove,
1246 static int __init twl4030_init(void)
1248 return i2c_add_driver(&twl4030_driver);
1250 subsys_initcall(twl4030_init);
1252 static void __exit twl4030_exit(void)
1254 i2c_del_driver(&twl4030_driver);
1256 module_exit(twl4030_exit);
1258 MODULE_AUTHOR("Texas Instruments, Inc.");
1259 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1260 MODULE_LICENSE("GPL");