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>
43 #include <linux/i2c/twl4030-pwrirq.h>
45 #define DRIVER_NAME "twl4030"
47 #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
48 defined(CONFIG_TWL4030_BCI_BATTERY_MODUEL)
49 #define twl_has_bci() true
51 #define twl_has_bci() 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_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
61 #define twl_has_gpio() true
63 #define twl_has_gpio() false
66 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
67 #define twl_has_madc() true
69 #define twl_has_madc() false
72 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
73 #define twl_has_rtc() true
75 #define twl_has_rtc() false
78 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
79 #define twl_has_usb() true
81 #define twl_has_usb() false
84 static inline void activate_irq(int irq)
87 /* ARM requires an extra step to clear IRQ_NOREQUEST, which it
88 * sets on behalf of every irq_chip. Also sets IRQ_NOPROBE.
90 set_irq_flags(irq, IRQF_VALID);
92 /* same effect on other architectures */
97 /* Primary Interrupt Handler on TWL4030 Registers */
99 /* Register Definitions */
101 #define REG_PIH_ISR_P1 (0x1)
102 #define REG_PIH_ISR_P2 (0x2)
103 #define REG_PIH_SIR (0x3)
105 /* Triton Core internal information (BEGIN) */
107 /* Last - for index max*/
108 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
110 #define TWL4030_NUM_SLAVES 4
113 #define TWL4030_SLAVENUM_NUM0 0x00
114 #define TWL4030_SLAVENUM_NUM1 0x01
115 #define TWL4030_SLAVENUM_NUM2 0x02
116 #define TWL4030_SLAVENUM_NUM3 0x03
118 /* Base Address defns */
120 #define TWL4030_BASEADD_USB 0x0000
122 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
123 #define TWL4030_BASEADD_GPIO 0x0098
125 #define TWL4030_BASEADD_INTBR 0x0085
126 #define TWL4030_BASEADD_PIH 0x0080
127 #define TWL4030_BASEADD_TEST 0x004C
129 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
130 #define TWL4030_BASEADD_LED 0x00EE
131 #define TWL4030_BASEADD_MADC 0x0000
132 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
133 #define TWL4030_BASEADD_PRECHARGE 0x00AA
134 #define TWL4030_BASEADD_PWM0 0x00F8
135 #define TWL4030_BASEADD_PWM1 0x00FB
136 #define TWL4030_BASEADD_PWMA 0x00EF
137 #define TWL4030_BASEADD_PWMB 0x00F1
138 #define TWL4030_BASEADD_KEYPAD 0x00D2
140 #define TWL4030_BASEADD_BACKUP 0x0014
141 #define TWL4030_BASEADD_INT 0x002E
142 #define TWL4030_BASEADD_PM_MASTER 0x0036
143 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
144 #define TWL4030_BASEADD_RTC 0x001C
145 #define TWL4030_BASEADD_SECURED_REG 0x0000
147 /* TWL4030 BCI registers */
148 #define TWL4030_INTERRUPTS_BCIIMR1A 0x2
149 #define TWL4030_INTERRUPTS_BCIIMR2A 0x3
150 #define TWL4030_INTERRUPTS_BCIIMR1B 0x6
151 #define TWL4030_INTERRUPTS_BCIIMR2B 0x7
152 #define TWL4030_INTERRUPTS_BCIISR1A 0x0
153 #define TWL4030_INTERRUPTS_BCIISR2A 0x1
154 #define TWL4030_INTERRUPTS_BCIISR1B 0x4
155 #define TWL4030_INTERRUPTS_BCIISR2B 0x5
157 /* TWL4030 keypad registers */
158 #define TWL4030_KEYPAD_KEYP_IMR1 0x12
159 #define TWL4030_KEYPAD_KEYP_IMR2 0x14
160 #define TWL4030_KEYPAD_KEYP_ISR1 0x11
161 #define TWL4030_KEYPAD_KEYP_ISR2 0x13
164 /* Triton Core internal information (END) */
166 /* Few power values */
167 #define R_CFG_BOOT 0x05
168 #define R_PROTECT_KEY 0x0E
171 #define KEY_UNLOCK1 0xce
172 #define KEY_UNLOCK2 0xec
173 #define KEY_LOCK 0x00
175 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
176 #define HFCLK_FREQ_26_MHZ (2 << 0)
177 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
178 #define HIGH_PERF_SQ (1 << 3)
180 /* SIH_CTRL registers that aren't defined elsewhere */
181 #define TWL4030_INTERRUPTS_BCISIHCTRL 0x0d
182 #define TWL4030_MADC_MADC_SIH_CTRL 0x67
183 #define TWL4030_KEYPAD_KEYP_SIH_CTRL 0x17
187 * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
188 * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
189 * @sih_ctrl: address of module SIH_CTRL register
190 * @reg_cnt: number of IMR/ISR regs
191 * @imrs: pointer to array of TWL module interrupt mask register indices
192 * @isrs: pointer to array of TWL module interrupt status register indices
194 * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
195 * during twl_init_irq().
197 struct twl4030_mod_iregs {
205 /* TWL4030 INT module interrupt mask registers */
206 static const u8 __initconst twl4030_int_imr_regs[] = {
207 TWL4030_INT_PWR_IMR1,
208 TWL4030_INT_PWR_IMR2,
211 /* TWL4030 INT module interrupt status registers */
212 static const u8 __initconst twl4030_int_isr_regs[] = {
213 TWL4030_INT_PWR_ISR1,
214 TWL4030_INT_PWR_ISR2,
217 /* TWL4030 INTERRUPTS module interrupt mask registers */
218 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
219 TWL4030_INTERRUPTS_BCIIMR1A,
220 TWL4030_INTERRUPTS_BCIIMR1B,
221 TWL4030_INTERRUPTS_BCIIMR2A,
222 TWL4030_INTERRUPTS_BCIIMR2B,
225 /* TWL4030 INTERRUPTS module interrupt status registers */
226 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
227 TWL4030_INTERRUPTS_BCIISR1A,
228 TWL4030_INTERRUPTS_BCIISR1B,
229 TWL4030_INTERRUPTS_BCIISR2A,
230 TWL4030_INTERRUPTS_BCIISR2B,
233 /* TWL4030 MADC module interrupt mask registers */
234 static const u8 __initconst twl4030_madc_imr_regs[] = {
239 /* TWL4030 MADC module interrupt status registers */
240 static const u8 __initconst twl4030_madc_isr_regs[] = {
245 /* TWL4030 keypad module interrupt mask registers */
246 static const u8 __initconst twl4030_keypad_imr_regs[] = {
247 TWL4030_KEYPAD_KEYP_IMR1,
248 TWL4030_KEYPAD_KEYP_IMR2,
251 /* TWL4030 keypad module interrupt status registers */
252 static const u8 __initconst twl4030_keypad_isr_regs[] = {
253 TWL4030_KEYPAD_KEYP_ISR1,
254 TWL4030_KEYPAD_KEYP_ISR2,
257 /* TWL4030 GPIO module interrupt mask registers */
258 static const u8 __initconst twl4030_gpio_imr_regs[] = {
267 /* TWL4030 GPIO module interrupt status registers */
268 static const u8 __initconst twl4030_gpio_isr_regs[] = {
277 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
278 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
280 .mod_no = TWL4030_MODULE_INT,
281 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
282 .reg_cnt = ARRAY_SIZE(twl4030_int_imr_regs),
283 .imrs = twl4030_int_imr_regs,
284 .isrs = twl4030_int_isr_regs,
287 .mod_no = TWL4030_MODULE_INTERRUPTS,
288 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
289 .reg_cnt = ARRAY_SIZE(twl4030_interrupts_imr_regs),
290 .imrs = twl4030_interrupts_imr_regs,
291 .isrs = twl4030_interrupts_isr_regs,
294 .mod_no = TWL4030_MODULE_MADC,
295 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
296 .reg_cnt = ARRAY_SIZE(twl4030_madc_imr_regs),
297 .imrs = twl4030_madc_imr_regs,
298 .isrs = twl4030_madc_isr_regs,
301 .mod_no = TWL4030_MODULE_KEYPAD,
302 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
303 .reg_cnt = ARRAY_SIZE(twl4030_keypad_imr_regs),
304 .imrs = twl4030_keypad_imr_regs,
305 .isrs = twl4030_keypad_isr_regs,
308 .mod_no = TWL4030_MODULE_GPIO,
309 .sih_ctrl = REG_GPIO_SIH_CTRL,
310 .reg_cnt = ARRAY_SIZE(twl4030_gpio_imr_regs),
311 .imrs = twl4030_gpio_imr_regs,
312 .isrs = twl4030_gpio_isr_regs,
317 /* Data Structures */
318 /* To have info on T2 IRQ substem activated or not */
319 static struct completion irq_event;
321 /* Structure to define on TWL4030 Slave ID */
322 struct twl4030_client {
323 struct i2c_client *client;
327 /* max numb of i2c_msg required is for read =2 */
328 struct i2c_msg xfer_msg[2];
330 /* To lock access to xfer_msg */
331 struct mutex xfer_lock;
335 struct twl4030mapping {
336 unsigned char sid; /* Slave ID */
337 unsigned char base; /* base address */
340 /* mapping the module id to slave id and base address */
341 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
343 * NOTE: don't change this table without updating the
344 * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
345 * so they continue to match the order in this table.
348 { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
350 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
351 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
352 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
353 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
354 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
356 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
357 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
358 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
359 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
360 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
361 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
362 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
363 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
364 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
365 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
367 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
368 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
369 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
370 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
371 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
372 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
375 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
378 * TWL4030 doesn't have PIH mask, hence dummy function for mask
382 static void twl4030_i2c_ackirq(unsigned int irq)
386 static void twl4030_i2c_disableint(unsigned int irq)
390 static void twl4030_i2c_enableint(unsigned int irq)
394 /* information for processing in the Work Item */
395 static struct irq_chip twl4030_irq_chip = {
397 .ack = twl4030_i2c_ackirq,
398 .mask = twl4030_i2c_disableint,
399 .unmask = twl4030_i2c_enableint,
402 /* Global Functions */
405 * twl4030_i2c_write - Writes a n bit register in TWL4030
406 * @mod_no: module number
407 * @value: an array of num_bytes+1 containing data to write
408 * @reg: register address (just offset will do)
409 * @num_bytes: number of bytes to transfer
411 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
412 * valid data starts at Offset 1.
414 * Returns the result of operation - 0 is success
416 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
420 struct twl4030_client *twl;
423 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
424 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
427 sid = twl4030_map[mod_no].sid;
428 twl = &twl4030_modules[sid];
430 if (unlikely(!twl->inuse)) {
431 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
434 mutex_lock(&twl->xfer_lock);
436 * [MSG1]: fill the register address data
437 * fill the data Tx buffer
439 msg = &twl->xfer_msg[0];
440 msg->addr = twl->address;
441 msg->len = num_bytes + 1;
444 /* over write the first byte of buffer with the register address */
445 *value = twl4030_map[mod_no].base + reg;
446 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
447 mutex_unlock(&twl->xfer_lock);
449 /* i2cTransfer returns num messages.translate it pls.. */
454 EXPORT_SYMBOL(twl4030_i2c_write);
457 * twl4030_i2c_read - Reads a n bit register in TWL4030
458 * @mod_no: module number
459 * @value: an array of num_bytes containing data to be read
460 * @reg: register address (just offset will do)
461 * @num_bytes: number of bytes to transfer
463 * Returns result of operation - num_bytes is success else failure.
465 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
470 struct twl4030_client *twl;
473 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
474 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
477 sid = twl4030_map[mod_no].sid;
478 twl = &twl4030_modules[sid];
480 if (unlikely(!twl->inuse)) {
481 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
484 mutex_lock(&twl->xfer_lock);
485 /* [MSG1] fill the register address data */
486 msg = &twl->xfer_msg[0];
487 msg->addr = twl->address;
489 msg->flags = 0; /* Read the register value */
490 val = twl4030_map[mod_no].base + reg;
492 /* [MSG2] fill the data rx buffer */
493 msg = &twl->xfer_msg[1];
494 msg->addr = twl->address;
495 msg->flags = I2C_M_RD; /* Read the register value */
496 msg->len = num_bytes; /* only n bytes */
498 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
499 mutex_unlock(&twl->xfer_lock);
501 /* i2cTransfer returns num messages.translate it pls.. */
506 EXPORT_SYMBOL(twl4030_i2c_read);
509 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
510 * @mod_no: module number
511 * @value: the value to be written 8 bit
512 * @reg: register address (just offset will do)
514 * Returns result of operation - 0 is success
516 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
519 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
520 u8 temp_buffer[2] = { 0 };
521 /* offset 1 contains the data */
522 temp_buffer[1] = value;
523 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
525 EXPORT_SYMBOL(twl4030_i2c_write_u8);
528 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
529 * @mod_no: module number
530 * @value: the value read 8 bit
531 * @reg: register address (just offset will do)
533 * Returns result of operation - 0 is success
535 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
537 return twl4030_i2c_read(mod_no, value, reg, 1);
539 EXPORT_SYMBOL(twl4030_i2c_read_u8);
541 /* Helper Functions */
544 * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
545 * module interrupts. It executes in kernel thread context.
546 * On entry, cpu interrupts are disabled.
548 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
550 struct irqaction *action;
551 const unsigned int cpu = smp_processor_id();
554 * Earlier this was desc->triggered = 1;
556 desc->status |= IRQ_LEVEL;
559 * The desc->handle method would normally call the desc->chip->ack
560 * method here, but we won't bother since our ack method is NULL.
564 kstat_cpu(cpu).irqs[irq]++;
566 action = desc->action;
575 /* Call the ISR with cpu interrupts enabled */
576 ret = action->handler(irq, action->dev_id);
577 if (ret == IRQ_HANDLED)
578 status |= action->flags;
580 action = action->next;
583 if (status & IRQF_SAMPLE_RANDOM)
584 add_interrupt_randomness(irq);
588 if (retval != IRQ_HANDLED)
589 printk(KERN_ERR "ISR for TWL4030 module"
590 " irq %d can't handle interrupt\n",
594 * Here is where we should call the unmask method, but
595 * again we won't bother since it is NULL.
598 printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
599 " but can't be masked!\n", irq);
601 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
602 " be masked!\n", irq);
605 static unsigned twl4030_irq_base;
608 * twl4030_irq_thread() runs as a kernel thread. It queries the twl4030
609 * interrupt controller to see which modules are generating interrupt requests
610 * and then calls the desc->handle method for each module requesting service.
612 static int twl4030_irq_thread(void *data)
614 long irq = (long)data;
615 irq_desc_t *desc = irq_desc + irq;
616 static unsigned i2c_errors;
617 const static unsigned max_i2c_errors = 100;
619 daemonize("twl4030-irq");
620 current->flags |= PF_NOFREEZE;
622 while (!kthread_should_stop()) {
627 wait_for_completion_interruptible(&irq_event);
629 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
632 printk(KERN_WARNING "I2C error %d while reading TWL4030"
633 " PIH ISR register.\n", ret);
634 if (++i2c_errors >= max_i2c_errors) {
635 printk(KERN_ERR "Maximum I2C error count"
636 " exceeded. Terminating %s.\n",
643 for (module_irq = twl4030_irq_base; 0 != pih_isr;
644 pih_isr >>= 1, module_irq++) {
646 irq_desc_t *d = irq_desc + module_irq;
650 d->handle_irq(module_irq, d);
656 desc->chip->unmask(irq);
663 * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
664 * This is a chained interrupt, so there is no desc->action method for it.
665 * Now we need to query the interrupt controller in the twl4030 to determine
666 * which module is generating the interrupt request. However, we can't do i2c
667 * transactions in interrupt context, so we must defer that work to a kernel
668 * thread. All we do here is acknowledge and mask the interrupt and wakeup
671 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
673 const unsigned int cpu = smp_processor_id();
676 * Earlier this was desc->triggered = 1;
678 desc->status |= IRQ_LEVEL;
681 * Acknowledge, clear _AND_ disable the interrupt.
683 desc->chip->ack(irq);
686 kstat_cpu(cpu).irqs[irq]++;
688 complete(&irq_event);
692 static int add_children(struct twl4030_platform_data *pdata)
694 struct platform_device *pdev = NULL;
695 struct twl4030_client *twl = NULL;
698 if (twl_has_bci() && pdata->bci) {
699 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
701 pdev = platform_device_alloc("twl4030_bci", -1);
703 pr_debug("%s: can't alloc bci dev\n", DRIVER_NAME);
709 pdev->dev.parent = &twl->client->dev;
710 status = platform_device_add_data(pdev, pdata->bci,
711 sizeof(*pdata->bci));
713 dev_dbg(&twl->client->dev,
714 "can't add bci data, %d\n",
721 struct resource r = {
722 .start = TWL4030_PWRIRQ_CHG_PRES,
723 .flags = IORESOURCE_IRQ,
726 status = platform_device_add_resources(pdev, &r, 1);
730 status = platform_device_add(pdev);
733 platform_device_put(pdev);
734 dev_dbg(&twl->client->dev,
735 "can't create bci dev, %d\n",
741 if (twl_has_gpio() && pdata->gpio) {
742 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM1];
744 pdev = platform_device_alloc("twl4030_gpio", -1);
746 pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
751 /* more driver model init */
753 pdev->dev.parent = &twl->client->dev;
754 /* device_init_wakeup(&pdev->dev, 1); */
756 status = platform_device_add_data(pdev, pdata->gpio,
757 sizeof(*pdata->gpio));
759 dev_dbg(&twl->client->dev,
760 "can't add gpio data, %d\n",
766 /* GPIO module IRQ */
768 struct resource r = {
769 .start = pdata->irq_base + 0,
770 .flags = IORESOURCE_IRQ,
773 status = platform_device_add_resources(pdev, &r, 1);
777 status = platform_device_add(pdev);
780 platform_device_put(pdev);
781 dev_dbg(&twl->client->dev,
782 "can't create gpio dev, %d\n",
788 if (twl_has_keypad() && pdata->keypad) {
789 pdev = platform_device_alloc("twl4030_keypad", -1);
791 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
792 pdev->dev.parent = &twl->client->dev;
793 device_init_wakeup(&pdev->dev, 1);
794 status = platform_device_add_data(pdev, pdata->keypad,
795 sizeof(*pdata->keypad));
797 dev_dbg(&twl->client->dev,
798 "can't add keypad data, %d\n",
800 platform_device_put(pdev);
803 status = platform_device_add(pdev);
805 platform_device_put(pdev);
806 dev_dbg(&twl->client->dev,
807 "can't create keypad dev, %d\n",
812 pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
818 if (twl_has_madc() && pdata->madc) {
819 pdev = platform_device_alloc("twl4030_madc", -1);
821 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
822 pdev->dev.parent = &twl->client->dev;
823 device_init_wakeup(&pdev->dev, 1);
824 status = platform_device_add_data(pdev, pdata->madc,
825 sizeof(*pdata->madc));
827 platform_device_put(pdev);
828 dev_dbg(&twl->client->dev,
829 "can't add madc data, %d\n",
833 status = platform_device_add(pdev);
835 platform_device_put(pdev);
836 dev_dbg(&twl->client->dev,
837 "can't create madc dev, %d\n",
842 pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
849 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
851 pdev = platform_device_alloc("twl4030_rtc", -1);
853 pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
856 pdev->dev.parent = &twl->client->dev;
857 device_init_wakeup(&pdev->dev, 1);
861 * REVISIT platform_data here currently might use of
862 * "msecure" line ... but for now we just expect board
863 * setup to tell the chip "we are secure" at all times.
864 * Eventually, Linux might become more aware of such
865 * HW security concerns, and "least privilege".
870 struct resource r = {
871 /* REVISIT don't hard-wire this stuff */
872 .start = TWL4030_PWRIRQ_RTC,
873 .flags = IORESOURCE_IRQ,
876 status = platform_device_add_resources(pdev, &r, 1);
880 status = platform_device_add(pdev);
883 platform_device_put(pdev);
884 dev_dbg(&twl->client->dev,
885 "can't create rtc dev, %d\n",
891 if (twl_has_usb() && pdata->usb) {
892 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM0];
894 pdev = platform_device_alloc("twl4030_usb", -1);
896 pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
902 pdev->dev.parent = &twl->client->dev;
903 device_init_wakeup(&pdev->dev, 1);
904 status = platform_device_add_data(pdev, pdata->usb,
905 sizeof(*pdata->usb));
907 platform_device_put(pdev);
908 dev_dbg(&twl->client->dev,
909 "can't add usb data, %d\n",
916 struct resource r = {
917 .start = TWL4030_PWRIRQ_USB_PRES,
918 .flags = IORESOURCE_IRQ,
921 status = platform_device_add_resources(pdev, &r, 1);
925 status = platform_device_add(pdev);
928 platform_device_put(pdev);
929 dev_dbg(&twl->client->dev,
930 "can't create usb dev, %d\n",
937 pr_err("failed to add twl4030's children (status %d)\n", status);
941 static struct task_struct * __init start_twl4030_irq_thread(long irq)
943 struct task_struct *thread;
945 init_completion(&irq_event);
946 thread = kthread_run(twl4030_irq_thread, (void *)irq,
947 "twl4030 irq %ld", irq);
949 pr_err("%s: could not create twl4030 irq %ld thread!\n",
956 * These three functions should be part of Voltage frame work
957 * added here to complete the functionality for now.
959 static int __init protect_pm_master(void)
963 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
968 static int __init unprotect_pm_master(void)
972 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
974 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
979 static int __init power_companion_init(void)
984 u8 ctrl = HFCLK_FREQ_26_MHZ;
986 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
987 if (cpu_is_omap2430())
988 osc = clk_get(NULL, "osc_ck");
990 osc = clk_get(NULL, "osc_sys_ck");
995 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
996 "using bootloader value (unknown osc rate)\n");
1000 rate = clk_get_rate(osc);
1005 ctrl = HFCLK_FREQ_19p2_MHZ;
1008 ctrl = HFCLK_FREQ_26_MHZ;
1011 ctrl = HFCLK_FREQ_38p4_MHZ;
1015 ctrl |= HIGH_PERF_SQ;
1016 e |= unprotect_pm_master();
1017 /* effect->MADC+USB ck en */
1018 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
1019 e |= protect_pm_master();
1025 * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
1026 * @mod_no: TWL4030 module number
1027 * @reg: register index to clear
1028 * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
1030 * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
1031 * status register to ensure that any prior interrupts are cleared.
1032 * Returns the status from the I2C read operation.
1034 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
1038 return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
1039 twl4030_i2c_write_u8(mod_no, 0xff, reg);
1043 * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
1044 * @mod_no: TWL4030 module number
1045 * @reg: register index to clear
1047 * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
1048 * the specified TWL module are cleared by reads, or 0 if cleared by
1051 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
1055 WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
1057 tmp &= TWL4030_SIH_CTRL_COR_MASK;
1058 tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
1064 * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
1065 * @t: pointer to twl4030_mod_iregs array
1066 * @t_sz: ARRAY_SIZE(t) (starting at 1)
1068 * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
1069 * interrupt status registers (ISRs). No return value, but will WARN if
1070 * any I2C operations fail.
1072 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
1078 * N.B. - further efficiency is possible here. Eight I2C
1079 * operations on BCI and GPIO modules are avoidable if I2C
1080 * burst read/write transactions were implemented. Would
1081 * probably save about 1ms of boot time and a small amount of
1084 for (i = 0; i < t_sz; i++) {
1085 const struct twl4030_mod_iregs tmr = t[i];
1088 /* Are ISRs cleared by reads or writes? */
1089 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
1091 for (j = 0; j < tmr.reg_cnt; j++) {
1093 /* Mask interrupts at the TWL4030 */
1094 WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
1097 /* Clear TWL4030 ISRs */
1098 WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
1099 tmr.isrs[j], cor) < 0);
1107 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
1111 char *msg = "Unable to register interrupt subsystem";
1114 * Mask and clear all TWL4030 interrupts since initially we do
1115 * not have any TWL4030 module interrupt handlers present
1117 twl4030_mask_clear_intrs(twl4030_mod_regs,
1118 ARRAY_SIZE(twl4030_mod_regs));
1120 twl4030_irq_base = irq_base;
1122 /* install an irq handler for each of the PIH modules */
1123 for (i = irq_base; i < irq_end; i++) {
1124 set_irq_chip_and_handler(i, &twl4030_irq_chip,
1125 do_twl4030_module_irq);
1129 /* install an irq handler to demultiplex the TWL4030 interrupt */
1130 set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
1131 set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
1132 set_irq_chained_handler(irq_num, do_twl4030_irq);
1134 res = power_companion_init();
1136 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
1139 /*----------------------------------------------------------------------*/
1141 static int twl4030_remove(struct i2c_client *client)
1145 /* FIXME undo twl_init_irq() */
1146 if (twl4030_irq_base) {
1147 dev_err(&client->dev, "can't yet clean up IRQs?\n");
1151 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1152 struct twl4030_client *twl = &twl4030_modules[i];
1154 if (twl->client && twl->client != client)
1155 i2c_unregister_device(twl->client);
1156 twl4030_modules[i].client = NULL;
1157 twl4030_modules[i].inuse = false;
1162 /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
1164 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
1168 struct twl4030_platform_data *pdata = client->dev.platform_data;
1171 dev_dbg(&client->dev, "no platform data?\n");
1175 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1176 dev_dbg(&client->dev, "can't talk I2C?\n");
1180 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1181 if (twl4030_modules[i].inuse || twl4030_irq_base) {
1182 dev_dbg(&client->dev, "driver is already in use\n");
1187 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1188 struct twl4030_client *twl = &twl4030_modules[i];
1190 twl->address = client->addr + i;
1192 twl->client = client;
1194 twl->client = i2c_new_dummy(client->adapter,
1197 dev_err(&twl->client->dev,
1198 "can't attach client %d\n", i);
1202 strlcpy(twl->client->name, id->name,
1203 sizeof(twl->client->name));
1206 mutex_init(&twl->xfer_lock);
1210 * Check if the PIH module is initialized, if yes, then init
1211 * the T2 Interrupt subsystem
1213 if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
1214 && twl4030_irq_base == 0
1217 && pdata->irq_end > pdata->irq_base) {
1218 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
1219 dev_info(&client->dev, "IRQ %d chains IRQs %d..%d\n",
1220 client->irq, pdata->irq_base, pdata->irq_end - 1);
1223 status = add_children(pdata);
1226 twl4030_remove(client);
1230 static const struct i2c_device_id twl4030_ids[] = {
1231 { "twl4030", 0 }, /* "Triton 2" */
1232 { "tps65950", 0 }, /* catalog version of twl4030 */
1233 { "tps65930", 0 }, /* fewer LDOs and DACs; no charger */
1234 { "tps65920", 0 }, /* fewer LDOs; no codec or charger */
1235 { /* end of list */ },
1237 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1239 /* One Client Driver , 4 Clients */
1240 static struct i2c_driver twl4030_driver = {
1241 .driver.name = DRIVER_NAME,
1242 .id_table = twl4030_ids,
1243 .probe = twl4030_probe,
1244 .remove = twl4030_remove,
1247 static int __init twl4030_init(void)
1249 return i2c_add_driver(&twl4030_driver);
1251 subsys_initcall(twl4030_init);
1253 static void __exit twl4030_exit(void)
1255 i2c_del_driver(&twl4030_driver);
1257 module_exit(twl4030_exit);
1259 MODULE_AUTHOR("Texas Instruments, Inc.");
1260 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1261 MODULE_LICENSE("GPL");