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_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
49 #define twl_has_keypad() true
51 #define twl_has_keypad() false
54 #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
55 #define twl_has_gpio() true
57 #define twl_has_gpio() false
60 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
61 #define twl_has_madc() true
63 #define twl_has_madc() false
66 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
67 #define twl_has_rtc() true
69 #define twl_has_rtc() false
72 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
73 #define twl_has_usb() true
75 #define twl_has_usb() false
78 /* Primary Interrupt Handler on TWL4030 Registers */
80 /* Register Definitions */
82 #define REG_PIH_ISR_P1 (0x1)
83 #define REG_PIH_ISR_P2 (0x2)
84 #define REG_PIH_SIR (0x3)
86 /* Triton Core internal information (BEGIN) */
88 /* Last - for index max*/
89 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
91 #define TWL4030_NUM_SLAVES 4
94 #define TWL4030_SLAVENUM_NUM0 0x00
95 #define TWL4030_SLAVENUM_NUM1 0x01
96 #define TWL4030_SLAVENUM_NUM2 0x02
97 #define TWL4030_SLAVENUM_NUM3 0x03
99 /* Base Address defns */
101 #define TWL4030_BASEADD_USB 0x0000
103 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
104 #define TWL4030_BASEADD_GPIO 0x0098
106 #define TWL4030_BASEADD_INTBR 0x0085
107 #define TWL4030_BASEADD_PIH 0x0080
108 #define TWL4030_BASEADD_TEST 0x004C
110 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
111 #define TWL4030_BASEADD_LED 0x00EE
112 #define TWL4030_BASEADD_MADC 0x0000
113 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
114 #define TWL4030_BASEADD_PRECHARGE 0x00AA
115 #define TWL4030_BASEADD_PWM0 0x00F8
116 #define TWL4030_BASEADD_PWM1 0x00FB
117 #define TWL4030_BASEADD_PWMA 0x00EF
118 #define TWL4030_BASEADD_PWMB 0x00F1
119 #define TWL4030_BASEADD_KEYPAD 0x00D2
121 #define TWL4030_BASEADD_BACKUP 0x0014
122 #define TWL4030_BASEADD_INT 0x002E
123 #define TWL4030_BASEADD_PM_MASTER 0x0036
124 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
125 #define TWL4030_BASEADD_RTC 0x001C
126 #define TWL4030_BASEADD_SECURED_REG 0x0000
128 /* TWL4030 BCI registers */
129 #define TWL4030_INTERRUPTS_BCIIMR1A 0x2
130 #define TWL4030_INTERRUPTS_BCIIMR2A 0x3
131 #define TWL4030_INTERRUPTS_BCIIMR1B 0x6
132 #define TWL4030_INTERRUPTS_BCIIMR2B 0x7
133 #define TWL4030_INTERRUPTS_BCIISR1A 0x0
134 #define TWL4030_INTERRUPTS_BCIISR2A 0x1
135 #define TWL4030_INTERRUPTS_BCIISR1B 0x4
136 #define TWL4030_INTERRUPTS_BCIISR2B 0x5
138 /* TWL4030 keypad registers */
139 #define TWL4030_KEYPAD_KEYP_IMR1 0x12
140 #define TWL4030_KEYPAD_KEYP_IMR2 0x14
141 #define TWL4030_KEYPAD_KEYP_ISR1 0x11
142 #define TWL4030_KEYPAD_KEYP_ISR2 0x13
145 /* Triton Core internal information (END) */
147 /* Few power values */
148 #define R_CFG_BOOT 0x05
149 #define R_PROTECT_KEY 0x0E
152 #define KEY_UNLOCK1 0xce
153 #define KEY_UNLOCK2 0xec
154 #define KEY_LOCK 0x00
156 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
157 #define HFCLK_FREQ_26_MHZ (2 << 0)
158 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
159 #define HIGH_PERF_SQ (1 << 3)
161 /* SIH_CTRL registers that aren't defined elsewhere */
162 #define TWL4030_INTERRUPTS_BCISIHCTRL 0x0d
163 #define TWL4030_MADC_MADC_SIH_CTRL 0x67
164 #define TWL4030_KEYPAD_KEYP_SIH_CTRL 0x17
166 #define TWL4030_SIH_CTRL_COR_MASK (1 << 2)
169 * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
170 * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
171 * @sih_ctrl: address of module SIH_CTRL register
172 * @reg_cnt: number of IMR/ISR regs
173 * @imrs: pointer to array of TWL module interrupt mask register indices
174 * @isrs: pointer to array of TWL module interrupt status register indices
176 * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
177 * during twl_init_irq().
179 struct twl4030_mod_iregs {
187 /* TWL4030 INT module interrupt mask registers */
188 static const u8 __initconst twl4030_int_imr_regs[] = {
189 TWL4030_INT_PWR_IMR1,
190 TWL4030_INT_PWR_IMR2,
193 /* TWL4030 INT module interrupt status registers */
194 static const u8 __initconst twl4030_int_isr_regs[] = {
195 TWL4030_INT_PWR_ISR1,
196 TWL4030_INT_PWR_ISR2,
199 /* TWL4030 INTERRUPTS module interrupt mask registers */
200 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
201 TWL4030_INTERRUPTS_BCIIMR1A,
202 TWL4030_INTERRUPTS_BCIIMR1B,
203 TWL4030_INTERRUPTS_BCIIMR2A,
204 TWL4030_INTERRUPTS_BCIIMR2B,
207 /* TWL4030 INTERRUPTS module interrupt status registers */
208 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
209 TWL4030_INTERRUPTS_BCIISR1A,
210 TWL4030_INTERRUPTS_BCIISR1B,
211 TWL4030_INTERRUPTS_BCIISR2A,
212 TWL4030_INTERRUPTS_BCIISR2B,
215 /* TWL4030 MADC module interrupt mask registers */
216 static const u8 __initconst twl4030_madc_imr_regs[] = {
221 /* TWL4030 MADC module interrupt status registers */
222 static const u8 __initconst twl4030_madc_isr_regs[] = {
227 /* TWL4030 keypad module interrupt mask registers */
228 static const u8 __initconst twl4030_keypad_imr_regs[] = {
229 TWL4030_KEYPAD_KEYP_IMR1,
230 TWL4030_KEYPAD_KEYP_IMR2,
233 /* TWL4030 keypad module interrupt status registers */
234 static const u8 __initconst twl4030_keypad_isr_regs[] = {
235 TWL4030_KEYPAD_KEYP_ISR1,
236 TWL4030_KEYPAD_KEYP_ISR2,
239 /* TWL4030 GPIO module interrupt mask registers */
240 static const u8 __initconst twl4030_gpio_imr_regs[] = {
249 /* TWL4030 GPIO module interrupt status registers */
250 static const u8 __initconst twl4030_gpio_isr_regs[] = {
259 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
260 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
262 .mod_no = TWL4030_MODULE_INT,
263 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
264 .reg_cnt = ARRAY_SIZE(twl4030_int_imr_regs),
265 .imrs = twl4030_int_imr_regs,
266 .isrs = twl4030_int_isr_regs,
269 .mod_no = TWL4030_MODULE_INTERRUPTS,
270 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
271 .reg_cnt = ARRAY_SIZE(twl4030_interrupts_imr_regs),
272 .imrs = twl4030_interrupts_imr_regs,
273 .isrs = twl4030_interrupts_isr_regs,
276 .mod_no = TWL4030_MODULE_MADC,
277 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
278 .reg_cnt = ARRAY_SIZE(twl4030_madc_imr_regs),
279 .imrs = twl4030_madc_imr_regs,
280 .isrs = twl4030_madc_isr_regs,
283 .mod_no = TWL4030_MODULE_KEYPAD,
284 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
285 .reg_cnt = ARRAY_SIZE(twl4030_keypad_imr_regs),
286 .imrs = twl4030_keypad_imr_regs,
287 .isrs = twl4030_keypad_isr_regs,
290 .mod_no = TWL4030_MODULE_GPIO,
291 .sih_ctrl = REG_GPIO_SIH_CTRL,
292 .reg_cnt = ARRAY_SIZE(twl4030_gpio_imr_regs),
293 .imrs = twl4030_gpio_imr_regs,
294 .isrs = twl4030_gpio_isr_regs,
299 /* Data Structures */
300 /* To have info on T2 IRQ substem activated or not */
301 static struct completion irq_event;
303 /* Structure to define on TWL4030 Slave ID */
304 struct twl4030_client {
305 struct i2c_client *client;
309 /* max numb of i2c_msg required is for read =2 */
310 struct i2c_msg xfer_msg[2];
312 /* To lock access to xfer_msg */
313 struct mutex xfer_lock;
317 struct twl4030mapping {
318 unsigned char sid; /* Slave ID */
319 unsigned char base; /* base address */
322 /* mapping the module id to slave id and base address */
323 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
324 { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
325 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
326 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
327 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
328 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
329 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
330 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
331 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
332 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
333 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
334 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
335 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
336 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
337 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
338 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
339 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
340 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
341 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
342 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
343 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
344 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
345 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
348 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
351 * TWL4030 doesn't have PIH mask, hence dummy function for mask
355 static void twl4030_i2c_ackirq(unsigned int irq)
359 static void twl4030_i2c_disableint(unsigned int irq)
363 static void twl4030_i2c_enableint(unsigned int irq)
367 /* information for processing in the Work Item */
368 static struct irq_chip twl4030_irq_chip = {
370 .ack = twl4030_i2c_ackirq,
371 .mask = twl4030_i2c_disableint,
372 .unmask = twl4030_i2c_enableint,
375 /* Global Functions */
378 * twl4030_i2c_write - Writes a n bit register in TWL4030
379 * @mod_no: module number
380 * @value: an array of num_bytes+1 containing data to write
381 * @reg: register address (just offset will do)
382 * @num_bytes: number of bytes to transfer
384 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
385 * valid data starts at Offset 1.
387 * Returns the result of operation - 0 is success
389 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
393 struct twl4030_client *twl;
396 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
397 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
400 sid = twl4030_map[mod_no].sid;
401 twl = &twl4030_modules[sid];
403 if (unlikely(!twl->inuse)) {
404 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
407 mutex_lock(&twl->xfer_lock);
409 * [MSG1]: fill the register address data
410 * fill the data Tx buffer
412 msg = &twl->xfer_msg[0];
413 msg->addr = twl->address;
414 msg->len = num_bytes + 1;
417 /* over write the first byte of buffer with the register address */
418 *value = twl4030_map[mod_no].base + reg;
419 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
420 mutex_unlock(&twl->xfer_lock);
422 /* i2cTransfer returns num messages.translate it pls.. */
427 EXPORT_SYMBOL(twl4030_i2c_write);
430 * twl4030_i2c_read - Reads a n bit register in TWL4030
431 * @mod_no: module number
432 * @value: an array of num_bytes containing data to be read
433 * @reg: register address (just offset will do)
434 * @num_bytes: number of bytes to transfer
436 * Returns result of operation - num_bytes is success else failure.
438 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
443 struct twl4030_client *twl;
446 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
447 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
450 sid = twl4030_map[mod_no].sid;
451 twl = &twl4030_modules[sid];
453 if (unlikely(!twl->inuse)) {
454 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
457 mutex_lock(&twl->xfer_lock);
458 /* [MSG1] fill the register address data */
459 msg = &twl->xfer_msg[0];
460 msg->addr = twl->address;
462 msg->flags = 0; /* Read the register value */
463 val = twl4030_map[mod_no].base + reg;
465 /* [MSG2] fill the data rx buffer */
466 msg = &twl->xfer_msg[1];
467 msg->addr = twl->address;
468 msg->flags = I2C_M_RD; /* Read the register value */
469 msg->len = num_bytes; /* only n bytes */
471 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
472 mutex_unlock(&twl->xfer_lock);
474 /* i2cTransfer returns num messages.translate it pls.. */
479 EXPORT_SYMBOL(twl4030_i2c_read);
482 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
483 * @mod_no: module number
484 * @value: the value to be written 8 bit
485 * @reg: register address (just offset will do)
487 * Returns result of operation - 0 is success
489 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
492 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
493 u8 temp_buffer[2] = { 0 };
494 /* offset 1 contains the data */
495 temp_buffer[1] = value;
496 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
498 EXPORT_SYMBOL(twl4030_i2c_write_u8);
501 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
502 * @mod_no: module number
503 * @value: the value read 8 bit
504 * @reg: register address (just offset will do)
506 * Returns result of operation - 0 is success
508 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
510 return twl4030_i2c_read(mod_no, value, reg, 1);
512 EXPORT_SYMBOL(twl4030_i2c_read_u8);
514 /* Helper Functions */
517 * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
518 * module interrupts. It executes in kernel thread context.
519 * On entry, cpu interrupts are disabled.
521 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
523 struct irqaction *action;
524 const unsigned int cpu = smp_processor_id();
527 * Earlier this was desc->triggered = 1;
529 desc->status |= IRQ_LEVEL;
532 * The desc->handle method would normally call the desc->chip->ack
533 * method here, but we won't bother since our ack method is NULL.
537 kstat_cpu(cpu).irqs[irq]++;
539 action = desc->action;
548 /* Call the ISR with cpu interrupts enabled */
549 ret = action->handler(irq, action->dev_id);
550 if (ret == IRQ_HANDLED)
551 status |= action->flags;
553 action = action->next;
556 if (status & IRQF_SAMPLE_RANDOM)
557 add_interrupt_randomness(irq);
561 if (retval != IRQ_HANDLED)
562 printk(KERN_ERR "ISR for TWL4030 module"
563 " irq %d can't handle interrupt\n",
567 * Here is where we should call the unmask method, but
568 * again we won't bother since it is NULL.
571 printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
572 " but can't be masked!\n", irq);
574 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
575 " be masked!\n", irq);
578 static unsigned twl4030_irq_base;
581 * twl4030_irq_thread() runs as a kernel thread. It queries the twl4030
582 * interrupt controller to see which modules are generating interrupt requests
583 * and then calls the desc->handle method for each module requesting service.
585 static int twl4030_irq_thread(void *data)
588 irq_desc_t *desc = irq_desc + irq;
589 static unsigned i2c_errors;
590 const static unsigned max_i2c_errors = 100;
592 daemonize("twl4030-irq");
593 current->flags |= PF_NOFREEZE;
595 while (!kthread_should_stop()) {
600 wait_for_completion_interruptible(&irq_event);
602 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
605 printk(KERN_WARNING "I2C error %d while reading TWL4030"
606 " PIH ISR register.\n", ret);
607 if (++i2c_errors >= max_i2c_errors) {
608 printk(KERN_ERR "Maximum I2C error count"
609 " exceeded. Terminating %s.\n",
616 for (module_irq = twl4030_irq_base; 0 != pih_isr;
617 pih_isr >>= 1, module_irq++) {
619 irq_desc_t *d = irq_desc + module_irq;
623 d->handle_irq(module_irq, d);
629 desc->chip->unmask(irq);
636 * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
637 * This is a chained interrupt, so there is no desc->action method for it.
638 * Now we need to query the interrupt controller in the twl4030 to determine
639 * which module is generating the interrupt request. However, we can't do i2c
640 * transactions in interrupt context, so we must defer that work to a kernel
641 * thread. All we do here is acknowledge and mask the interrupt and wakeup
644 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
646 const unsigned int cpu = smp_processor_id();
649 * Earlier this was desc->triggered = 1;
651 desc->status |= IRQ_LEVEL;
654 * Acknowledge, clear _AND_ disable the interrupt.
656 desc->chip->ack(irq);
659 kstat_cpu(cpu).irqs[irq]++;
661 complete(&irq_event);
665 static int add_children(struct twl4030_platform_data *pdata)
667 struct platform_device *pdev = NULL;
668 struct twl4030_client *twl = NULL;
671 if (twl_has_gpio() && pdata->gpio) {
672 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM1];
674 pdev = platform_device_alloc("twl4030_gpio", -1);
676 pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
681 /* more driver model init */
683 pdev->dev.parent = &twl->client->dev;
684 /* device_init_wakeup(&pdev->dev, 1); */
686 status = platform_device_add_data(pdev, pdata->gpio,
687 sizeof(*pdata->gpio));
689 dev_dbg(&twl->client->dev,
690 "can't add gpio data, %d\n",
696 /* GPIO module IRQ */
698 struct resource r = {
699 .start = pdata->irq_base + 0,
700 .flags = IORESOURCE_IRQ,
703 status = platform_device_add_resources(pdev, &r, 1);
707 status = platform_device_add(pdev);
710 platform_device_put(pdev);
711 dev_dbg(&twl->client->dev,
712 "can't create gpio dev, %d\n",
718 if (twl_has_keypad() && pdata->keypad) {
719 pdev = platform_device_alloc("twl4030_keypad", -1);
721 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
722 pdev->dev.parent = &twl->client->dev;
723 device_init_wakeup(&pdev->dev, 1);
724 status = platform_device_add_data(pdev, pdata->keypad,
725 sizeof(*pdata->keypad));
727 dev_dbg(&twl->client->dev,
728 "can't add keypad data, %d\n",
730 platform_device_put(pdev);
733 status = platform_device_add(pdev);
735 platform_device_put(pdev);
736 dev_dbg(&twl->client->dev,
737 "can't create keypad dev, %d\n",
742 pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
748 if (twl_has_madc() && pdata->madc) {
749 pdev = platform_device_alloc("twl4030_madc", -1);
751 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
752 pdev->dev.parent = &twl->client->dev;
753 device_init_wakeup(&pdev->dev, 1);
754 status = platform_device_add_data(pdev, pdata->madc,
755 sizeof(*pdata->madc));
757 platform_device_put(pdev);
758 dev_dbg(&twl->client->dev,
759 "can't add madc data, %d\n",
763 status = platform_device_add(pdev);
765 platform_device_put(pdev);
766 dev_dbg(&twl->client->dev,
767 "can't create madc dev, %d\n",
772 pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
779 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
781 pdev = platform_device_alloc("twl4030_rtc", -1);
783 pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
786 pdev->dev.parent = &twl->client->dev;
787 device_init_wakeup(&pdev->dev, 1);
791 * REVISIT platform_data here currently might use of
792 * "msecure" line ... but for now we just expect board
793 * setup to tell the chip "we are secure" at all times.
794 * Eventually, Linux might become more aware of such
795 * HW security concerns, and "least privilege".
800 struct resource r = {
801 /* REVISIT don't hard-wire this stuff */
802 .start = TWL4030_PWRIRQ_RTC,
803 .flags = IORESOURCE_IRQ,
806 status = platform_device_add_resources(pdev, &r, 1);
810 status = platform_device_add(pdev);
813 platform_device_put(pdev);
814 dev_dbg(&twl->client->dev,
815 "can't create rtc dev, %d\n",
821 if (twl_has_usb() && pdata->usb) {
822 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM0];
824 pdev = platform_device_alloc("twl4030_usb", -1);
826 pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
832 pdev->dev.parent = &twl->client->dev;
833 device_init_wakeup(&pdev->dev, 1);
834 status = platform_device_add_data(pdev, pdata->usb,
835 sizeof(*pdata->usb));
837 platform_device_put(pdev);
838 dev_dbg(&twl->client->dev,
839 "can't add usb data, %d\n",
846 struct resource r = {
847 .start = TWL4030_PWRIRQ_USB_PRES,
848 .flags = IORESOURCE_IRQ,
851 status = platform_device_add_resources(pdev, &r, 1);
855 status = platform_device_add(pdev);
858 platform_device_put(pdev);
859 dev_dbg(&twl->client->dev,
860 "can't create usb dev, %d\n",
867 pr_err("failed to add twl4030's children (status %d)\n", status);
871 static struct task_struct * __init start_twl4030_irq_thread(int irq)
873 struct task_struct *thread;
875 init_completion(&irq_event);
876 thread = kthread_run(twl4030_irq_thread, (void *)irq,
877 "twl4030 irq %d", irq);
879 pr_err("%s: could not create twl4030 irq %d thread!\n",
886 * These three functions should be part of Voltage frame work
887 * added here to complete the functionality for now.
889 static int __init protect_pm_master(void)
893 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
898 static int __init unprotect_pm_master(void)
902 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
904 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
909 static int __init power_companion_init(void)
913 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
916 u8 ctrl = HFCLK_FREQ_26_MHZ;
918 if (cpu_is_omap2430())
919 osc = clk_get(NULL, "osc_ck");
921 osc = clk_get(NULL, "osc_sys_ck");
923 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
924 "using bootloader value (unknown osc rate)\n");
928 rate = clk_get_rate(osc);
933 ctrl = HFCLK_FREQ_19p2_MHZ;
936 ctrl = HFCLK_FREQ_26_MHZ;
939 ctrl = HFCLK_FREQ_38p4_MHZ;
943 ctrl |= HIGH_PERF_SQ;
944 e |= unprotect_pm_master();
945 /* effect->MADC+USB ck en */
946 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
947 e |= protect_pm_master();
954 * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
955 * @mod_no: TWL4030 module number
956 * @reg: register index to clear
957 * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
959 * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
960 * status register to ensure that any prior interrupts are cleared.
961 * Returns the status from the I2C read operation.
963 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
967 return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
968 twl4030_i2c_write_u8(mod_no, 0xff, reg);
972 * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
973 * @mod_no: TWL4030 module number
974 * @reg: register index to clear
976 * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
977 * the specified TWL module are cleared by reads, or 0 if cleared by
980 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
984 WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
986 tmp &= TWL4030_SIH_CTRL_COR_MASK;
987 tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
993 * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
994 * @t: pointer to twl4030_mod_iregs array
995 * @t_sz: ARRAY_SIZE(t) (starting at 1)
997 * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
998 * interrupt status registers (ISRs). No return value, but will WARN if
999 * any I2C operations fail.
1001 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
1007 * N.B. - further efficiency is possible here. Eight I2C
1008 * operations on BCI and GPIO modules are avoidable if I2C
1009 * burst read/write transactions were implemented. Would
1010 * probably save about 1ms of boot time and a small amount of
1013 for (i = 0; i < t_sz; i++) {
1014 const struct twl4030_mod_iregs tmr = t[i];
1017 /* Are ISRs cleared by reads or writes? */
1018 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
1021 for (j = 0; j < tmr.reg_cnt; j++) {
1023 /* Mask interrupts at the TWL4030 */
1024 WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
1027 /* Clear TWL4030 ISRs */
1028 WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
1029 tmr.isrs[j], cor) < 0);
1037 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
1041 char *msg = "Unable to register interrupt subsystem";
1044 * Mask and clear all TWL4030 interrupts since initially we do
1045 * not have any TWL4030 module interrupt handlers present
1047 twl4030_mask_clear_intrs(twl4030_mod_regs,
1048 ARRAY_SIZE(twl4030_mod_regs));
1050 twl4030_irq_base = irq_base;
1052 /* install an irq handler for each of the PIH modules */
1053 for (i = irq_base; i < irq_end; i++) {
1054 set_irq_chip(i, &twl4030_irq_chip);
1055 set_irq_handler(i, do_twl4030_module_irq);
1056 set_irq_flags(i, IRQF_VALID);
1059 /* install an irq handler to demultiplex the TWL4030 interrupt */
1060 set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
1061 set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
1062 set_irq_chained_handler(irq_num, do_twl4030_irq);
1064 res = power_companion_init();
1066 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
1069 /*----------------------------------------------------------------------*/
1071 static int twl4030_remove(struct i2c_client *client)
1075 /* FIXME undo twl_init_irq() */
1076 if (twl4030_irq_base) {
1077 dev_err(&client->dev, "can't yet clean up IRQs?\n");
1081 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1082 struct twl4030_client *twl = &twl4030_modules[i];
1084 if (twl->client && twl->client != client)
1085 i2c_unregister_device(twl->client);
1086 twl4030_modules[i].client = NULL;
1087 twl4030_modules[i].inuse = false;
1092 /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
1094 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
1098 struct twl4030_platform_data *pdata = client->dev.platform_data;
1101 dev_dbg(&client->dev, "no platform data?\n");
1105 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1106 dev_dbg(&client->dev, "can't talk I2C?\n");
1110 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1111 if (twl4030_modules[i].inuse || twl4030_irq_base) {
1112 dev_dbg(&client->dev, "driver is already in use\n");
1117 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1118 struct twl4030_client *twl = &twl4030_modules[i];
1120 twl->address = client->addr + i;
1122 twl->client = client;
1124 twl->client = i2c_new_dummy(client->adapter,
1127 dev_err(&twl->client->dev,
1128 "can't attach client %d\n", i);
1132 strlcpy(twl->client->name, id->name,
1133 sizeof(twl->client->name));
1136 mutex_init(&twl->xfer_lock);
1140 * Check if the PIH module is initialized, if yes, then init
1141 * the T2 Interrupt subsystem
1143 if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
1144 && twl4030_irq_base == 0
1147 && pdata->irq_end > pdata->irq_base) {
1148 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
1149 dev_info(&client->dev, "IRQ %d chains IRQs %d..%d\n",
1150 client->irq, pdata->irq_base, pdata->irq_end - 1);
1153 status = add_children(pdata);
1156 twl4030_remove(client);
1160 static const struct i2c_device_id twl4030_ids[] = {
1161 { "twl4030", 0 }, /* "Triton 2" */
1162 { "tps65950", 0 }, /* catalog version of twl4030 */
1163 { "tps65930", 0 }, /* fewer LDOs and DACs; no charger */
1164 { "tps65920", 0 }, /* fewer LDOs; no codec or charger */
1165 { /* end of list */ },
1167 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1169 /* One Client Driver , 4 Clients */
1170 static struct i2c_driver twl4030_driver = {
1171 .driver.name = DRIVER_NAME,
1172 .id_table = twl4030_ids,
1173 .probe = twl4030_probe,
1174 .remove = twl4030_remove,
1177 static int __init twl4030_init(void)
1179 return i2c_add_driver(&twl4030_driver);
1181 subsys_initcall(twl4030_init);
1183 static void __exit twl4030_exit(void)
1185 i2c_del_driver(&twl4030_driver);
1187 module_exit(twl4030_exit);
1189 MODULE_AUTHOR("Texas Instruments, Inc.");
1190 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1191 MODULE_LICENSE("GPL");