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_TWL4030_BCI_BATTERY) || \
49 defined(CONFIG_TWL4030_BCI_BATTERY_MODUEL)
50 #define twl_has_bci() true
52 #define twl_has_bci() false
55 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
56 #define twl_has_keypad() true
58 #define twl_has_keypad() false
61 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
62 #define twl_has_gpio() true
64 #define twl_has_gpio() false
67 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
68 #define twl_has_madc() true
70 #define twl_has_madc() false
73 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
74 #define twl_has_rtc() true
76 #define twl_has_rtc() false
79 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
80 #define twl_has_usb() true
82 #define twl_has_usb() false
85 static inline void activate_irq(int irq)
88 /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
89 * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
91 set_irq_flags(irq, IRQF_VALID);
93 /* same effect on other architectures */
98 /* Primary Interrupt Handler on TWL4030 Registers */
100 /* Register Definitions */
102 #define REG_PIH_ISR_P1 (0x1)
103 #define REG_PIH_ISR_P2 (0x2)
104 #define REG_PIH_SIR (0x3)
106 /* Triton Core internal information (BEGIN) */
108 /* Last - for index max*/
109 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
111 #define TWL4030_NUM_SLAVES 4
114 #define TWL4030_SLAVENUM_NUM0 0x00
115 #define TWL4030_SLAVENUM_NUM1 0x01
116 #define TWL4030_SLAVENUM_NUM2 0x02
117 #define TWL4030_SLAVENUM_NUM3 0x03
119 /* Base Address defns */
121 #define TWL4030_BASEADD_USB 0x0000
123 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
124 #define TWL4030_BASEADD_GPIO 0x0098
126 #define TWL4030_BASEADD_INTBR 0x0085
127 #define TWL4030_BASEADD_PIH 0x0080
128 #define TWL4030_BASEADD_TEST 0x004C
130 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
131 #define TWL4030_BASEADD_LED 0x00EE
132 #define TWL4030_BASEADD_MADC 0x0000
133 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
134 #define TWL4030_BASEADD_PRECHARGE 0x00AA
135 #define TWL4030_BASEADD_PWM0 0x00F8
136 #define TWL4030_BASEADD_PWM1 0x00FB
137 #define TWL4030_BASEADD_PWMA 0x00EF
138 #define TWL4030_BASEADD_PWMB 0x00F1
139 #define TWL4030_BASEADD_KEYPAD 0x00D2
141 #define TWL4030_BASEADD_BACKUP 0x0014
142 #define TWL4030_BASEADD_INT 0x002E
143 #define TWL4030_BASEADD_PM_MASTER 0x0036
144 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
145 #define TWL4030_BASEADD_RTC 0x001C
146 #define TWL4030_BASEADD_SECURED_REG 0x0000
148 /* TWL4030 BCI registers */
149 #define TWL4030_INTERRUPTS_BCIIMR1A 0x2
150 #define TWL4030_INTERRUPTS_BCIIMR2A 0x3
151 #define TWL4030_INTERRUPTS_BCIIMR1B 0x6
152 #define TWL4030_INTERRUPTS_BCIIMR2B 0x7
153 #define TWL4030_INTERRUPTS_BCIISR1A 0x0
154 #define TWL4030_INTERRUPTS_BCIISR2A 0x1
155 #define TWL4030_INTERRUPTS_BCIISR1B 0x4
156 #define TWL4030_INTERRUPTS_BCIISR2B 0x5
158 /* TWL4030 keypad registers */
159 #define TWL4030_KEYPAD_KEYP_IMR1 0x12
160 #define TWL4030_KEYPAD_KEYP_IMR2 0x14
161 #define TWL4030_KEYPAD_KEYP_ISR1 0x11
162 #define TWL4030_KEYPAD_KEYP_ISR2 0x13
165 /* Triton Core internal information (END) */
167 /* Few power values */
168 #define R_CFG_BOOT 0x05
169 #define R_PROTECT_KEY 0x0E
172 #define KEY_UNLOCK1 0xce
173 #define KEY_UNLOCK2 0xec
174 #define KEY_LOCK 0x00
176 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
177 #define HFCLK_FREQ_26_MHZ (2 << 0)
178 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
179 #define HIGH_PERF_SQ (1 << 3)
181 /* SIH_CTRL registers that aren't defined elsewhere */
182 #define TWL4030_INTERRUPTS_BCISIHCTRL 0x0d
183 #define TWL4030_MADC_MADC_SIH_CTRL 0x67
184 #define TWL4030_KEYPAD_KEYP_SIH_CTRL 0x17
186 #define TWL4030_SIH_CTRL_COR_MASK (1 << 2)
189 * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
190 * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
191 * @sih_ctrl: address of module SIH_CTRL register
192 * @reg_cnt: number of IMR/ISR regs
193 * @imrs: pointer to array of TWL module interrupt mask register indices
194 * @isrs: pointer to array of TWL module interrupt status register indices
196 * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
197 * during twl_init_irq().
199 struct twl4030_mod_iregs {
207 /* TWL4030 INT module interrupt mask registers */
208 static const u8 __initconst twl4030_int_imr_regs[] = {
209 TWL4030_INT_PWR_IMR1,
210 TWL4030_INT_PWR_IMR2,
213 /* TWL4030 INT module interrupt status registers */
214 static const u8 __initconst twl4030_int_isr_regs[] = {
215 TWL4030_INT_PWR_ISR1,
216 TWL4030_INT_PWR_ISR2,
219 /* TWL4030 INTERRUPTS module interrupt mask registers */
220 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
221 TWL4030_INTERRUPTS_BCIIMR1A,
222 TWL4030_INTERRUPTS_BCIIMR1B,
223 TWL4030_INTERRUPTS_BCIIMR2A,
224 TWL4030_INTERRUPTS_BCIIMR2B,
227 /* TWL4030 INTERRUPTS module interrupt status registers */
228 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
229 TWL4030_INTERRUPTS_BCIISR1A,
230 TWL4030_INTERRUPTS_BCIISR1B,
231 TWL4030_INTERRUPTS_BCIISR2A,
232 TWL4030_INTERRUPTS_BCIISR2B,
235 /* TWL4030 MADC module interrupt mask registers */
236 static const u8 __initconst twl4030_madc_imr_regs[] = {
241 /* TWL4030 MADC module interrupt status registers */
242 static const u8 __initconst twl4030_madc_isr_regs[] = {
247 /* TWL4030 keypad module interrupt mask registers */
248 static const u8 __initconst twl4030_keypad_imr_regs[] = {
249 TWL4030_KEYPAD_KEYP_IMR1,
250 TWL4030_KEYPAD_KEYP_IMR2,
253 /* TWL4030 keypad module interrupt status registers */
254 static const u8 __initconst twl4030_keypad_isr_regs[] = {
255 TWL4030_KEYPAD_KEYP_ISR1,
256 TWL4030_KEYPAD_KEYP_ISR2,
259 /* TWL4030 GPIO module interrupt mask registers */
260 static const u8 __initconst twl4030_gpio_imr_regs[] = {
269 /* TWL4030 GPIO module interrupt status registers */
270 static const u8 __initconst twl4030_gpio_isr_regs[] = {
279 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
280 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
282 .mod_no = TWL4030_MODULE_INT,
283 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
284 .reg_cnt = ARRAY_SIZE(twl4030_int_imr_regs),
285 .imrs = twl4030_int_imr_regs,
286 .isrs = twl4030_int_isr_regs,
289 .mod_no = TWL4030_MODULE_INTERRUPTS,
290 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
291 .reg_cnt = ARRAY_SIZE(twl4030_interrupts_imr_regs),
292 .imrs = twl4030_interrupts_imr_regs,
293 .isrs = twl4030_interrupts_isr_regs,
296 .mod_no = TWL4030_MODULE_MADC,
297 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
298 .reg_cnt = ARRAY_SIZE(twl4030_madc_imr_regs),
299 .imrs = twl4030_madc_imr_regs,
300 .isrs = twl4030_madc_isr_regs,
303 .mod_no = TWL4030_MODULE_KEYPAD,
304 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
305 .reg_cnt = ARRAY_SIZE(twl4030_keypad_imr_regs),
306 .imrs = twl4030_keypad_imr_regs,
307 .isrs = twl4030_keypad_isr_regs,
310 .mod_no = TWL4030_MODULE_GPIO,
311 .sih_ctrl = REG_GPIO_SIH_CTRL,
312 .reg_cnt = ARRAY_SIZE(twl4030_gpio_imr_regs),
313 .imrs = twl4030_gpio_imr_regs,
314 .isrs = twl4030_gpio_isr_regs,
319 /* Data Structures */
320 /* To have info on T2 IRQ substem activated or not */
321 static struct completion irq_event;
323 /* Structure to define on TWL4030 Slave ID */
324 struct twl4030_client {
325 struct i2c_client *client;
329 /* max numb of i2c_msg required is for read =2 */
330 struct i2c_msg xfer_msg[2];
332 /* To lock access to xfer_msg */
333 struct mutex xfer_lock;
337 struct twl4030mapping {
338 unsigned char sid; /* Slave ID */
339 unsigned char base; /* base address */
342 /* mapping the module id to slave id and base address */
343 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
344 { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
345 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
346 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
347 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
348 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
349 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
350 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
351 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
352 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
353 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
354 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
355 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
356 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
357 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
358 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
359 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
360 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
361 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
362 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
363 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
364 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
365 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
368 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
371 * TWL4030 doesn't have PIH mask, hence dummy function for mask
375 static void twl4030_i2c_ackirq(unsigned int irq)
379 static void twl4030_i2c_disableint(unsigned int irq)
383 static void twl4030_i2c_enableint(unsigned int irq)
387 /* information for processing in the Work Item */
388 static struct irq_chip twl4030_irq_chip = {
390 .ack = twl4030_i2c_ackirq,
391 .mask = twl4030_i2c_disableint,
392 .unmask = twl4030_i2c_enableint,
395 /* Global Functions */
398 * twl4030_i2c_write - Writes a n bit register in TWL4030
399 * @mod_no: module number
400 * @value: an array of num_bytes+1 containing data to write
401 * @reg: register address (just offset will do)
402 * @num_bytes: number of bytes to transfer
404 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
405 * valid data starts at Offset 1.
407 * Returns the result of operation - 0 is success
409 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
413 struct twl4030_client *twl;
416 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
417 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
420 sid = twl4030_map[mod_no].sid;
421 twl = &twl4030_modules[sid];
423 if (unlikely(!twl->inuse)) {
424 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
427 mutex_lock(&twl->xfer_lock);
429 * [MSG1]: fill the register address data
430 * fill the data Tx buffer
432 msg = &twl->xfer_msg[0];
433 msg->addr = twl->address;
434 msg->len = num_bytes + 1;
437 /* over write the first byte of buffer with the register address */
438 *value = twl4030_map[mod_no].base + reg;
439 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
440 mutex_unlock(&twl->xfer_lock);
442 /* i2cTransfer returns num messages.translate it pls.. */
447 EXPORT_SYMBOL(twl4030_i2c_write);
450 * twl4030_i2c_read - Reads a n bit register in TWL4030
451 * @mod_no: module number
452 * @value: an array of num_bytes containing data to be read
453 * @reg: register address (just offset will do)
454 * @num_bytes: number of bytes to transfer
456 * Returns result of operation - num_bytes is success else failure.
458 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
463 struct twl4030_client *twl;
466 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
467 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
470 sid = twl4030_map[mod_no].sid;
471 twl = &twl4030_modules[sid];
473 if (unlikely(!twl->inuse)) {
474 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
477 mutex_lock(&twl->xfer_lock);
478 /* [MSG1] fill the register address data */
479 msg = &twl->xfer_msg[0];
480 msg->addr = twl->address;
482 msg->flags = 0; /* Read the register value */
483 val = twl4030_map[mod_no].base + reg;
485 /* [MSG2] fill the data rx buffer */
486 msg = &twl->xfer_msg[1];
487 msg->addr = twl->address;
488 msg->flags = I2C_M_RD; /* Read the register value */
489 msg->len = num_bytes; /* only n bytes */
491 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
492 mutex_unlock(&twl->xfer_lock);
494 /* i2cTransfer returns num messages.translate it pls.. */
499 EXPORT_SYMBOL(twl4030_i2c_read);
502 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
503 * @mod_no: module number
504 * @value: the value to be written 8 bit
505 * @reg: register address (just offset will do)
507 * Returns result of operation - 0 is success
509 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
512 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
513 u8 temp_buffer[2] = { 0 };
514 /* offset 1 contains the data */
515 temp_buffer[1] = value;
516 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
518 EXPORT_SYMBOL(twl4030_i2c_write_u8);
521 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
522 * @mod_no: module number
523 * @value: the value read 8 bit
524 * @reg: register address (just offset will do)
526 * Returns result of operation - 0 is success
528 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
530 return twl4030_i2c_read(mod_no, value, reg, 1);
532 EXPORT_SYMBOL(twl4030_i2c_read_u8);
534 /* Helper Functions */
537 * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
538 * module interrupts. It executes in kernel thread context.
539 * On entry, cpu interrupts are disabled.
541 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
543 struct irqaction *action;
544 const unsigned int cpu = smp_processor_id();
547 * Earlier this was desc->triggered = 1;
549 desc->status |= IRQ_LEVEL;
552 * The desc->handle method would normally call the desc->chip->ack
553 * method here, but we won't bother since our ack method is NULL.
557 kstat_cpu(cpu).irqs[irq]++;
559 action = desc->action;
568 /* Call the ISR with cpu interrupts enabled */
569 ret = action->handler(irq, action->dev_id);
570 if (ret == IRQ_HANDLED)
571 status |= action->flags;
573 action = action->next;
576 if (status & IRQF_SAMPLE_RANDOM)
577 add_interrupt_randomness(irq);
581 if (retval != IRQ_HANDLED)
582 printk(KERN_ERR "ISR for TWL4030 module"
583 " irq %d can't handle interrupt\n",
587 * Here is where we should call the unmask method, but
588 * again we won't bother since it is NULL.
591 printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
592 " but can't be masked!\n", irq);
594 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
595 " be masked!\n", irq);
598 static unsigned twl4030_irq_base;
601 * twl4030_irq_thread() runs as a kernel thread. It queries the twl4030
602 * interrupt controller to see which modules are generating interrupt requests
603 * and then calls the desc->handle method for each module requesting service.
605 static int twl4030_irq_thread(void *data)
607 long irq = (long)data;
608 irq_desc_t *desc = irq_desc + irq;
609 static unsigned i2c_errors;
610 const static unsigned max_i2c_errors = 100;
612 daemonize("twl4030-irq");
613 current->flags |= PF_NOFREEZE;
615 while (!kthread_should_stop()) {
620 wait_for_completion_interruptible(&irq_event);
622 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
625 printk(KERN_WARNING "I2C error %d while reading TWL4030"
626 " PIH ISR register.\n", ret);
627 if (++i2c_errors >= max_i2c_errors) {
628 printk(KERN_ERR "Maximum I2C error count"
629 " exceeded. Terminating %s.\n",
636 for (module_irq = twl4030_irq_base; 0 != pih_isr;
637 pih_isr >>= 1, module_irq++) {
639 irq_desc_t *d = irq_desc + module_irq;
643 d->handle_irq(module_irq, d);
649 desc->chip->unmask(irq);
656 * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
657 * This is a chained interrupt, so there is no desc->action method for it.
658 * Now we need to query the interrupt controller in the twl4030 to determine
659 * which module is generating the interrupt request. However, we can't do i2c
660 * transactions in interrupt context, so we must defer that work to a kernel
661 * thread. All we do here is acknowledge and mask the interrupt and wakeup
664 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
666 const unsigned int cpu = smp_processor_id();
669 * Earlier this was desc->triggered = 1;
671 desc->status |= IRQ_LEVEL;
674 * Acknowledge, clear _AND_ disable the interrupt.
676 desc->chip->ack(irq);
679 kstat_cpu(cpu).irqs[irq]++;
681 complete(&irq_event);
685 static int add_children(struct twl4030_platform_data *pdata)
687 struct platform_device *pdev = NULL;
688 struct twl4030_client *twl = NULL;
691 if (twl_has_bci() && pdata->bci) {
692 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
694 pdev = platform_device_alloc("twl4030_bci", -1);
696 pr_debug("%s: can't alloc bci dev\n", DRIVER_NAME);
702 pdev->dev.parent = &twl->client->dev;
703 status = platform_device_add_data(pdev, pdata->bci,
704 sizeof(*pdata->bci));
706 dev_dbg(&twl->client->dev,
707 "can't add bci data, %d\n",
714 struct resource r = {
715 .start = TWL4030_PWRIRQ_CHG_PRES,
716 .flags = IORESOURCE_IRQ,
719 status = platform_device_add_resources(pdev, &r, 1);
723 status = platform_device_add(pdev);
726 platform_device_put(pdev);
727 dev_dbg(&twl->client->dev,
728 "can't create bci dev, %d\n",
734 if (twl_has_gpio() && pdata->gpio) {
735 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM1];
737 pdev = platform_device_alloc("twl4030_gpio", -1);
739 pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
744 /* more driver model init */
746 pdev->dev.parent = &twl->client->dev;
747 /* device_init_wakeup(&pdev->dev, 1); */
749 status = platform_device_add_data(pdev, pdata->gpio,
750 sizeof(*pdata->gpio));
752 dev_dbg(&twl->client->dev,
753 "can't add gpio data, %d\n",
759 /* GPIO module IRQ */
761 struct resource r = {
762 .start = pdata->irq_base + 0,
763 .flags = IORESOURCE_IRQ,
766 status = platform_device_add_resources(pdev, &r, 1);
770 status = platform_device_add(pdev);
773 platform_device_put(pdev);
774 dev_dbg(&twl->client->dev,
775 "can't create gpio dev, %d\n",
781 if (twl_has_keypad() && pdata->keypad) {
782 pdev = platform_device_alloc("twl4030_keypad", -1);
784 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
785 pdev->dev.parent = &twl->client->dev;
786 device_init_wakeup(&pdev->dev, 1);
787 status = platform_device_add_data(pdev, pdata->keypad,
788 sizeof(*pdata->keypad));
790 dev_dbg(&twl->client->dev,
791 "can't add keypad data, %d\n",
793 platform_device_put(pdev);
796 status = platform_device_add(pdev);
798 platform_device_put(pdev);
799 dev_dbg(&twl->client->dev,
800 "can't create keypad dev, %d\n",
805 pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
811 if (twl_has_madc() && pdata->madc) {
812 pdev = platform_device_alloc("twl4030_madc", -1);
814 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
815 pdev->dev.parent = &twl->client->dev;
816 device_init_wakeup(&pdev->dev, 1);
817 status = platform_device_add_data(pdev, pdata->madc,
818 sizeof(*pdata->madc));
820 platform_device_put(pdev);
821 dev_dbg(&twl->client->dev,
822 "can't add madc data, %d\n",
826 status = platform_device_add(pdev);
828 platform_device_put(pdev);
829 dev_dbg(&twl->client->dev,
830 "can't create madc dev, %d\n",
835 pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
842 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
844 pdev = platform_device_alloc("twl4030_rtc", -1);
846 pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
849 pdev->dev.parent = &twl->client->dev;
850 device_init_wakeup(&pdev->dev, 1);
854 * REVISIT platform_data here currently might use of
855 * "msecure" line ... but for now we just expect board
856 * setup to tell the chip "we are secure" at all times.
857 * Eventually, Linux might become more aware of such
858 * HW security concerns, and "least privilege".
863 struct resource r = {
864 /* REVISIT don't hard-wire this stuff */
865 .start = TWL4030_PWRIRQ_RTC,
866 .flags = IORESOURCE_IRQ,
869 status = platform_device_add_resources(pdev, &r, 1);
873 status = platform_device_add(pdev);
876 platform_device_put(pdev);
877 dev_dbg(&twl->client->dev,
878 "can't create rtc dev, %d\n",
884 if (twl_has_usb() && pdata->usb) {
885 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM0];
887 pdev = platform_device_alloc("twl4030_usb", -1);
889 pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
895 pdev->dev.parent = &twl->client->dev;
896 device_init_wakeup(&pdev->dev, 1);
897 status = platform_device_add_data(pdev, pdata->usb,
898 sizeof(*pdata->usb));
900 platform_device_put(pdev);
901 dev_dbg(&twl->client->dev,
902 "can't add usb data, %d\n",
909 struct resource r = {
910 .start = TWL4030_PWRIRQ_USB_PRES,
911 .flags = IORESOURCE_IRQ,
914 status = platform_device_add_resources(pdev, &r, 1);
918 status = platform_device_add(pdev);
921 platform_device_put(pdev);
922 dev_dbg(&twl->client->dev,
923 "can't create usb dev, %d\n",
930 pr_err("failed to add twl4030's children (status %d)\n", status);
934 static struct task_struct * __init start_twl4030_irq_thread(long irq)
936 struct task_struct *thread;
938 init_completion(&irq_event);
939 thread = kthread_run(twl4030_irq_thread, (void *)irq,
940 "twl4030 irq %ld", irq);
942 pr_err("%s: could not create twl4030 irq %ld thread!\n",
949 * These three functions should be part of Voltage frame work
950 * added here to complete the functionality for now.
952 static int __init protect_pm_master(void)
956 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
961 static int __init unprotect_pm_master(void)
965 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
967 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
972 static int __init power_companion_init(void)
977 u8 ctrl = HFCLK_FREQ_26_MHZ;
979 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
980 if (cpu_is_omap2430())
981 osc = clk_get(NULL, "osc_ck");
983 osc = clk_get(NULL, "osc_sys_ck");
988 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
989 "using bootloader value (unknown osc rate)\n");
993 rate = clk_get_rate(osc);
998 ctrl = HFCLK_FREQ_19p2_MHZ;
1001 ctrl = HFCLK_FREQ_26_MHZ;
1004 ctrl = HFCLK_FREQ_38p4_MHZ;
1008 ctrl |= HIGH_PERF_SQ;
1009 e |= unprotect_pm_master();
1010 /* effect->MADC+USB ck en */
1011 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
1012 e |= protect_pm_master();
1018 * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
1019 * @mod_no: TWL4030 module number
1020 * @reg: register index to clear
1021 * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
1023 * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
1024 * status register to ensure that any prior interrupts are cleared.
1025 * Returns the status from the I2C read operation.
1027 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
1031 return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
1032 twl4030_i2c_write_u8(mod_no, 0xff, reg);
1036 * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
1037 * @mod_no: TWL4030 module number
1038 * @reg: register index to clear
1040 * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
1041 * the specified TWL module are cleared by reads, or 0 if cleared by
1044 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
1048 WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
1050 tmp &= TWL4030_SIH_CTRL_COR_MASK;
1051 tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
1057 * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
1058 * @t: pointer to twl4030_mod_iregs array
1059 * @t_sz: ARRAY_SIZE(t) (starting at 1)
1061 * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
1062 * interrupt status registers (ISRs). No return value, but will WARN if
1063 * any I2C operations fail.
1065 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
1071 * N.B. - further efficiency is possible here. Eight I2C
1072 * operations on BCI and GPIO modules are avoidable if I2C
1073 * burst read/write transactions were implemented. Would
1074 * probably save about 1ms of boot time and a small amount of
1077 for (i = 0; i < t_sz; i++) {
1078 const struct twl4030_mod_iregs tmr = t[i];
1081 /* Are ISRs cleared by reads or writes? */
1082 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
1085 for (j = 0; j < tmr.reg_cnt; j++) {
1087 /* Mask interrupts at the TWL4030 */
1088 WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
1091 /* Clear TWL4030 ISRs */
1092 WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
1093 tmr.isrs[j], cor) < 0);
1101 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
1105 char *msg = "Unable to register interrupt subsystem";
1108 * Mask and clear all TWL4030 interrupts since initially we do
1109 * not have any TWL4030 module interrupt handlers present
1111 twl4030_mask_clear_intrs(twl4030_mod_regs,
1112 ARRAY_SIZE(twl4030_mod_regs));
1114 twl4030_irq_base = irq_base;
1116 /* install an irq handler for each of the PIH modules */
1117 for (i = irq_base; i < irq_end; i++) {
1118 set_irq_chip_and_handler(i, &twl4030_irq_chip,
1119 do_twl4030_module_irq);
1123 /* install an irq handler to demultiplex the TWL4030 interrupt */
1124 set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
1125 set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
1126 set_irq_chained_handler(irq_num, do_twl4030_irq);
1128 res = power_companion_init();
1130 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
1133 /*----------------------------------------------------------------------*/
1135 static int twl4030_remove(struct i2c_client *client)
1139 /* FIXME undo twl_init_irq() */
1140 if (twl4030_irq_base) {
1141 dev_err(&client->dev, "can't yet clean up IRQs?\n");
1145 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1146 struct twl4030_client *twl = &twl4030_modules[i];
1148 if (twl->client && twl->client != client)
1149 i2c_unregister_device(twl->client);
1150 twl4030_modules[i].client = NULL;
1151 twl4030_modules[i].inuse = false;
1156 /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
1158 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
1162 struct twl4030_platform_data *pdata = client->dev.platform_data;
1165 dev_dbg(&client->dev, "no platform data?\n");
1169 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1170 dev_dbg(&client->dev, "can't talk I2C?\n");
1174 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1175 if (twl4030_modules[i].inuse || twl4030_irq_base) {
1176 dev_dbg(&client->dev, "driver is already in use\n");
1181 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1182 struct twl4030_client *twl = &twl4030_modules[i];
1184 twl->address = client->addr + i;
1186 twl->client = client;
1188 twl->client = i2c_new_dummy(client->adapter,
1191 dev_err(&twl->client->dev,
1192 "can't attach client %d\n", i);
1196 strlcpy(twl->client->name, id->name,
1197 sizeof(twl->client->name));
1200 mutex_init(&twl->xfer_lock);
1204 * Check if the PIH module is initialized, if yes, then init
1205 * the T2 Interrupt subsystem
1207 if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
1208 && twl4030_irq_base == 0
1211 && pdata->irq_end > pdata->irq_base) {
1212 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
1213 dev_info(&client->dev, "IRQ %d chains IRQs %d..%d\n",
1214 client->irq, pdata->irq_base, pdata->irq_end - 1);
1217 status = add_children(pdata);
1220 twl4030_remove(client);
1224 static const struct i2c_device_id twl4030_ids[] = {
1225 { "twl4030", 0 }, /* "Triton 2" */
1226 { "tps65950", 0 }, /* catalog version of twl4030 */
1227 { "tps65930", 0 }, /* fewer LDOs and DACs; no charger */
1228 { "tps65920", 0 }, /* fewer LDOs; no codec or charger */
1229 { /* end of list */ },
1231 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1233 /* One Client Driver , 4 Clients */
1234 static struct i2c_driver twl4030_driver = {
1235 .driver.name = DRIVER_NAME,
1236 .id_table = twl4030_ids,
1237 .probe = twl4030_probe,
1238 .remove = twl4030_remove,
1241 static int __init twl4030_init(void)
1243 return i2c_add_driver(&twl4030_driver);
1245 subsys_initcall(twl4030_init);
1247 static void __exit twl4030_exit(void)
1249 i2c_del_driver(&twl4030_driver);
1251 module_exit(twl4030_exit);
1253 MODULE_AUTHOR("Texas Instruments, Inc.");
1254 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1255 MODULE_LICENSE("GPL");