2 * twl4030_core.c - driver for TWL4030 PM and audio CODEC device
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
31 #include <linux/module.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/init.h>
34 #include <linux/time.h>
35 #include <linux/mutex.h>
36 #include <linux/interrupt.h>
37 #include <linux/random.h>
38 #include <linux/syscalls.h>
39 #include <linux/kthread.h>
40 #include <linux/platform_device.h>
42 #include <linux/i2c.h>
43 #include <linux/i2c/twl4030.h>
44 #include <linux/i2c/twl4030-gpio.h>
45 #include <linux/i2c/twl4030-madc.h>
46 #include <linux/i2c/twl4030-pwrirq.h>
47 #include <linux/slab.h>
48 #include <linux/clk.h>
49 #include <linux/device.h>
50 #include <linux/irq.h>
52 #include <asm/mach/irq.h>
54 #include <mach/gpio.h>
57 #define DRIVER_NAME "twl4030"
59 /* Macro Definitions */
60 #define TWL_CLIENT_STRING "TWL4030-ID"
61 #define TWL_CLIENT_USED 1
62 #define TWL_CLIENT_FREE 0
68 /* Primary Interrupt Handler on TWL4030 Registers */
70 /* Register Definitions */
72 #define REG_PIH_ISR_P1 (0x1)
73 #define REG_PIH_ISR_P2 (0x2)
74 #define REG_PIH_SIR (0x3)
76 /* Triton Core internal information (BEGIN) */
78 /* Last - for index max*/
79 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
82 #define TWL4030_NUM_SLAVES 0x04
83 #define TWL4030_SLAVENUM_NUM0 0x00
84 #define TWL4030_SLAVENUM_NUM1 0x01
85 #define TWL4030_SLAVENUM_NUM2 0x02
86 #define TWL4030_SLAVENUM_NUM3 0x03
87 #define TWL4030_SLAVEID_ID0 0x48
88 #define TWL4030_SLAVEID_ID1 0x49
89 #define TWL4030_SLAVEID_ID2 0x4A
90 #define TWL4030_SLAVEID_ID3 0x4B
92 /* Base Address defns */
94 #define TWL4030_BASEADD_USB 0x0000
96 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
97 #define TWL4030_BASEADD_GPIO 0x0098
99 #define TWL4030_BASEADD_INTBR 0x0085
100 #define TWL4030_BASEADD_PIH 0x0080
101 #define TWL4030_BASEADD_TEST 0x004C
103 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
104 #define TWL4030_BASEADD_LED 0x00EE
105 #define TWL4030_BASEADD_MADC 0x0000
106 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
107 #define TWL4030_BASEADD_PRECHARGE 0x00AA
108 #define TWL4030_BASEADD_PWM0 0x00F8
109 #define TWL4030_BASEADD_PWM1 0x00FB
110 #define TWL4030_BASEADD_PWMA 0x00EF
111 #define TWL4030_BASEADD_PWMB 0x00F1
112 #define TWL4030_BASEADD_KEYPAD 0x00D2
114 #define TWL4030_BASEADD_BACKUP 0x0014
115 #define TWL4030_BASEADD_INT 0x002E
116 #define TWL4030_BASEADD_PM_MASTER 0x0036
117 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
118 #define TWL4030_BASEADD_RTC 0x001C
119 #define TWL4030_BASEADD_SECURED_REG 0x0000
121 /* TWL4030 BCI registers */
122 #define TWL4030_INTERRUPTS_BCIIMR1A 0x2
123 #define TWL4030_INTERRUPTS_BCIIMR2A 0x3
124 #define TWL4030_INTERRUPTS_BCIIMR1B 0x6
125 #define TWL4030_INTERRUPTS_BCIIMR2B 0x7
126 #define TWL4030_INTERRUPTS_BCIISR1A 0x0
127 #define TWL4030_INTERRUPTS_BCIISR2A 0x1
128 #define TWL4030_INTERRUPTS_BCIISR1B 0x4
129 #define TWL4030_INTERRUPTS_BCIISR2B 0x5
131 /* TWL4030 keypad registers */
132 #define TWL4030_KEYPAD_KEYP_IMR1 0x12
133 #define TWL4030_KEYPAD_KEYP_IMR2 0x14
134 #define TWL4030_KEYPAD_KEYP_ISR1 0x11
135 #define TWL4030_KEYPAD_KEYP_ISR2 0x13
138 /* Triton Core internal information (END) */
140 /* Few power values */
141 #define R_CFG_BOOT 0x05
142 #define R_PROTECT_KEY 0x0E
145 #define KEY_UNLOCK1 0xce
146 #define KEY_UNLOCK2 0xec
147 #define KEY_LOCK 0x00
149 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
150 #define HFCLK_FREQ_26_MHZ (2 << 0)
151 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
152 #define HIGH_PERF_SQ (1 << 3)
154 /* on I2C-1 for 2430SDP */
155 #define CONFIG_I2C_TWL4030_ID 1
157 /* SIH_CTRL registers that aren't defined elsewhere */
158 #define TWL4030_INTERRUPTS_BCISIHCTRL 0x0d
159 #define TWL4030_MADC_MADC_SIH_CTRL 0x67
160 #define TWL4030_KEYPAD_KEYP_SIH_CTRL 0x17
162 #define TWL4030_SIH_CTRL_COR_MASK (1 << 2)
165 * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
166 * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
167 * @sih_ctrl: address of module SIH_CTRL register
168 * @reg_cnt: number of IMR/ISR regs
169 * @imrs: pointer to array of TWL module interrupt mask register indices
170 * @isrs: pointer to array of TWL module interrupt status register indices
172 * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
173 * during twl_init_irq().
175 struct twl4030_mod_iregs {
183 /* TWL4030 INT module interrupt mask registers */
184 static const u8 __initconst twl4030_int_imr_regs[] = {
185 TWL4030_INT_PWR_IMR1,
186 TWL4030_INT_PWR_IMR2,
189 /* TWL4030 INT module interrupt status registers */
190 static const u8 __initconst twl4030_int_isr_regs[] = {
191 TWL4030_INT_PWR_ISR1,
192 TWL4030_INT_PWR_ISR2,
195 /* TWL4030 INTERRUPTS module interrupt mask registers */
196 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
197 TWL4030_INTERRUPTS_BCIIMR1A,
198 TWL4030_INTERRUPTS_BCIIMR1B,
199 TWL4030_INTERRUPTS_BCIIMR2A,
200 TWL4030_INTERRUPTS_BCIIMR2B,
203 /* TWL4030 INTERRUPTS module interrupt status registers */
204 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
205 TWL4030_INTERRUPTS_BCIISR1A,
206 TWL4030_INTERRUPTS_BCIISR1B,
207 TWL4030_INTERRUPTS_BCIISR2A,
208 TWL4030_INTERRUPTS_BCIISR2B,
211 /* TWL4030 MADC module interrupt mask registers */
212 static const u8 __initconst twl4030_madc_imr_regs[] = {
217 /* TWL4030 MADC module interrupt status registers */
218 static const u8 __initconst twl4030_madc_isr_regs[] = {
223 /* TWL4030 keypad module interrupt mask registers */
224 static const u8 __initconst twl4030_keypad_imr_regs[] = {
225 TWL4030_KEYPAD_KEYP_IMR1,
226 TWL4030_KEYPAD_KEYP_IMR2,
229 /* TWL4030 keypad module interrupt status registers */
230 static const u8 __initconst twl4030_keypad_isr_regs[] = {
231 TWL4030_KEYPAD_KEYP_ISR1,
232 TWL4030_KEYPAD_KEYP_ISR2,
235 /* TWL4030 GPIO module interrupt mask registers */
236 static const u8 __initconst twl4030_gpio_imr_regs[] = {
245 /* TWL4030 GPIO module interrupt status registers */
246 static const u8 __initconst twl4030_gpio_isr_regs[] = {
255 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
256 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
258 .mod_no = TWL4030_MODULE_INT,
259 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
260 .reg_cnt = ARRAY_SIZE(twl4030_int_imr_regs),
261 .imrs = twl4030_int_imr_regs,
262 .isrs = twl4030_int_isr_regs,
265 .mod_no = TWL4030_MODULE_INTERRUPTS,
266 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
267 .reg_cnt = ARRAY_SIZE(twl4030_interrupts_imr_regs),
268 .imrs = twl4030_interrupts_imr_regs,
269 .isrs = twl4030_interrupts_isr_regs,
272 .mod_no = TWL4030_MODULE_MADC,
273 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
274 .reg_cnt = ARRAY_SIZE(twl4030_madc_imr_regs),
275 .imrs = twl4030_madc_imr_regs,
276 .isrs = twl4030_madc_isr_regs,
279 .mod_no = TWL4030_MODULE_KEYPAD,
280 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
281 .reg_cnt = ARRAY_SIZE(twl4030_keypad_imr_regs),
282 .imrs = twl4030_keypad_imr_regs,
283 .isrs = twl4030_keypad_isr_regs,
286 .mod_no = TWL4030_MODULE_GPIO,
287 .sih_ctrl = REG_GPIO_SIH_CTRL,
288 .reg_cnt = ARRAY_SIZE(twl4030_gpio_imr_regs),
289 .imrs = twl4030_gpio_imr_regs,
290 .isrs = twl4030_gpio_isr_regs,
295 /* Helper functions */
297 twl4030_detect_client(struct i2c_adapter *adapter, unsigned char sid);
298 static int twl4030_attach_adapter(struct i2c_adapter *adapter);
299 static int twl4030_detach_client(struct i2c_client *client);
300 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc);
302 static void twl_init_irq(void);
304 /* Data Structures */
305 /* To have info on T2 IRQ substem activated or not */
306 static unsigned char twl_irq_used = FREE;
307 static struct completion irq_event;
309 /* Structure to define on TWL4030 Slave ID */
310 struct twl4030_client {
311 struct i2c_client client;
312 const char client_name[sizeof(TWL_CLIENT_STRING) + 1];
313 const unsigned char address;
314 const char adapter_index;
317 /* max numb of i2c_msg required is for read =2 */
318 struct i2c_msg xfer_msg[2];
320 /* To lock access to xfer_msg */
321 struct mutex xfer_lock;
325 struct twl4030mapping {
326 unsigned char sid; /* Slave ID */
327 unsigned char base; /* base address */
330 /* mapping the module id to slave id and base address */
331 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
332 { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
333 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
334 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
335 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
336 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
337 { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
338 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
339 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
340 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
341 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
342 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
343 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
344 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
345 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
346 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
347 { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
348 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
349 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
350 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
351 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
352 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
353 { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
356 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES] = {
358 .address = TWL4030_SLAVEID_ID0,
359 .client_name = TWL_CLIENT_STRING "0",
360 .adapter_index = CONFIG_I2C_TWL4030_ID,
363 .address = TWL4030_SLAVEID_ID1,
364 .client_name = TWL_CLIENT_STRING "1",
365 .adapter_index = CONFIG_I2C_TWL4030_ID,
368 .address = TWL4030_SLAVEID_ID2,
369 .client_name = TWL_CLIENT_STRING "2",
370 .adapter_index = CONFIG_I2C_TWL4030_ID,
373 .address = TWL4030_SLAVEID_ID3,
374 .client_name = TWL_CLIENT_STRING "3",
375 .adapter_index = CONFIG_I2C_TWL4030_ID,
379 /* One Client Driver , 4 Clients */
380 static struct i2c_driver twl4030_driver = {
383 .owner = THIS_MODULE,
385 .attach_adapter = twl4030_attach_adapter,
386 .detach_client = twl4030_detach_client,
390 * TWL4030 doesn't have PIH mask, hence dummy function for mask
394 static void twl4030_i2c_ackirq(unsigned int irq) {}
395 static void twl4030_i2c_disableint(unsigned int irq) {}
396 static void twl4030_i2c_enableint(unsigned int irq) {}
398 /* information for processing in the Work Item */
399 static struct irq_chip twl4030_irq_chip = {
401 .ack = twl4030_i2c_ackirq,
402 .mask = twl4030_i2c_disableint,
403 .unmask = twl4030_i2c_enableint,
406 /* Global Functions */
409 * twl4030_i2c_write - Writes a n bit register in TWL4030
410 * @mod_no: module number
411 * @value: an array of num_bytes+1 containing data to write
412 * @reg: register address (just offset will do)
413 * @num_bytes: number of bytes to transfer
415 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
416 * valid data starts at Offset 1.
418 * Returns the result of operation - 0 is success
420 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
424 struct twl4030_client *twl;
427 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
428 pr_err("Invalid module Number\n");
431 sid = twl4030_map[mod_no].sid;
432 twl = &twl4030_modules[sid];
434 if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
435 pr_err("I2C Client[%d] is not initialized[%d]\n",
439 mutex_lock(&twl->xfer_lock);
441 * [MSG1]: fill the register address data
442 * fill the data Tx buffer
444 msg = &twl->xfer_msg[0];
445 msg->addr = twl->address;
446 msg->len = num_bytes + 1;
449 /* over write the first byte of buffer with the register address */
450 *value = twl4030_map[mod_no].base + reg;
451 ret = i2c_transfer(twl->client.adapter, twl->xfer_msg, 1);
452 mutex_unlock(&twl->xfer_lock);
454 /* i2cTransfer returns num messages.translate it pls.. */
459 EXPORT_SYMBOL(twl4030_i2c_write);
462 * twl4030_i2c_read - Reads a n bit register in TWL4030
463 * @mod_no: module number
464 * @value: an array of num_bytes containing data to be read
465 * @reg: register address (just offset will do)
466 * @num_bytes: number of bytes to transfer
468 * Returns result of operation - num_bytes is success else failure.
470 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
475 struct twl4030_client *twl;
478 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
479 pr_err("Invalid module Number\n");
482 sid = twl4030_map[mod_no].sid;
483 twl = &twl4030_modules[sid];
485 if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
486 pr_err("I2C Client[%d] is not initialized[%d]\n", sid,
490 mutex_lock(&twl->xfer_lock);
491 /* [MSG1] fill the register address data */
492 msg = &twl->xfer_msg[0];
493 msg->addr = twl->address;
495 msg->flags = 0; /* Read the register value */
496 val = twl4030_map[mod_no].base + reg;
498 /* [MSG2] fill the data rx buffer */
499 msg = &twl->xfer_msg[1];
500 msg->addr = twl->address;
501 msg->flags = I2C_M_RD; /* Read the register value */
502 msg->len = num_bytes; /* only n bytes */
504 ret = i2c_transfer(twl->client.adapter, twl->xfer_msg, 2);
505 mutex_unlock(&twl->xfer_lock);
507 /* i2cTransfer returns num messages.translate it pls.. */
512 EXPORT_SYMBOL(twl4030_i2c_read);
515 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
516 * @mod_no: module number
517 * @value: the value to be written 8 bit
518 * @reg: register address (just offset will do)
520 * Returns result of operation - 0 is success
522 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
525 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
526 u8 temp_buffer[2] = { 0 };
527 /* offset 1 contains the data */
528 temp_buffer[1] = value;
529 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
531 EXPORT_SYMBOL(twl4030_i2c_write_u8);
534 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
535 * @mod_no: module number
536 * @value: the value read 8 bit
537 * @reg: register address (just offset will do)
539 * Returns result of operation - 0 is success
541 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
543 return twl4030_i2c_read(mod_no, value, reg, 1);
545 EXPORT_SYMBOL(twl4030_i2c_read_u8);
547 /* Helper Functions */
550 * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
551 * module interrupts. It executes in kernel thread context.
552 * On entry, cpu interrupts are disabled.
554 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
556 struct irqaction *action;
557 const unsigned int cpu = smp_processor_id();
560 * Earlier this was desc->triggered = 1;
562 desc->status |= IRQ_LEVEL;
565 * The desc->handle method would normally call the desc->chip->ack
566 * method here, but we won't bother since our ack method is NULL.
570 kstat_cpu(cpu).irqs[irq]++;
572 action = desc->action;
581 /* Call the ISR with cpu interrupts enabled */
582 ret = action->handler(irq, action->dev_id);
583 if (ret == IRQ_HANDLED)
584 status |= action->flags;
586 action = action->next;
589 if (status & IRQF_SAMPLE_RANDOM)
590 add_interrupt_randomness(irq);
594 if (retval != IRQ_HANDLED)
595 printk(KERN_ERR "ISR for TWL4030 module"
596 " irq %d can't handle interrupt\n",
600 * Here is where we should call the unmask method, but
601 * again we won't bother since it is NULL.
604 printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
605 " but can't be masked!\n", irq);
607 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
608 " be masked!\n", irq);
612 * twl4030_irq_thread() runs as a kernel thread. It queries the twl4030
613 * interrupt controller to see which modules are generating interrupt requests
614 * and then calls the desc->handle method for each module requesting service.
616 static int twl4030_irq_thread(void *data)
619 irq_desc_t *desc = irq_desc + irq;
620 static unsigned i2c_errors;
621 const static unsigned max_i2c_errors = 100;
623 daemonize("twl4030-irq");
624 current->flags |= PF_NOFREEZE;
626 while (!kthread_should_stop()) {
631 wait_for_completion_interruptible(&irq_event);
633 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
636 printk(KERN_WARNING "I2C error %d while reading TWL4030"
637 " PIH ISR register.\n", ret);
638 if (++i2c_errors >= max_i2c_errors) {
639 printk(KERN_ERR "Maximum I2C error count"
640 " exceeded. Terminating %s.\n",
647 for (module_irq = TWL4030_IRQ_BASE; 0 != pih_isr;
648 pih_isr >>= 1, module_irq++) {
650 irq_desc_t *d = irq_desc + module_irq;
654 d->handle_irq(module_irq, d);
660 desc->chip->unmask(irq);
667 * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
668 * This is a chained interrupt, so there is no desc->action method for it.
669 * Now we need to query the interrupt controller in the twl4030 to determine
670 * which module is generating the interrupt request. However, we can't do i2c
671 * transactions in interrupt context, so we must defer that work to a kernel
672 * thread. All we do here is acknowledge and mask the interrupt and wakeup
675 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
677 const unsigned int cpu = smp_processor_id();
680 * Earlier this was desc->triggered = 1;
682 desc->status |= IRQ_LEVEL;
685 * Acknowledge, clear _AND_ disable the interrupt.
687 desc->chip->ack(irq);
690 kstat_cpu(cpu).irqs[irq]++;
692 complete(&irq_event);
696 /* attach a client to the adapter */
697 static int __init twl4030_detect_client(struct i2c_adapter *adapter,
701 struct twl4030_client *twl;
703 if (unlikely(sid >= TWL4030_NUM_SLAVES)) {
704 pr_err("sid[%d] > MOD_LAST[%d]\n", sid, TWL4030_NUM_SLAVES);
708 /* Check basic functionality */
709 err = i2c_check_functionality(adapter,
710 I2C_FUNC_SMBUS_WORD_DATA
711 | I2C_FUNC_SMBUS_WRITE_BYTE);
713 pr_err("SlaveID=%d functionality check failed\n", sid);
716 twl = &twl4030_modules[sid];
717 if (unlikely(twl->inuse)) {
718 pr_err("Client %s is already in use\n", twl->client_name);
722 memset(&twl->client, 0, sizeof(struct i2c_client));
724 twl->client.addr = twl->address;
725 twl->client.adapter = adapter;
726 twl->client.driver = &twl4030_driver;
728 memcpy(&twl->client.name, twl->client_name,
729 sizeof(TWL_CLIENT_STRING) + 1);
731 pr_info("TWL4030: TRY attach Slave %s on Adapter %s [%x]\n",
732 twl->client_name, adapter->name, err);
734 err = i2c_attach_client(&twl->client);
736 pr_err("Couldn't attach Slave %s on Adapter"
737 "%s [%x]\n", twl->client_name, adapter->name, err);
739 twl->inuse = TWL_CLIENT_USED;
740 mutex_init(&twl->xfer_lock);
746 static int add_children(void)
748 static bool children;
750 struct platform_device *pdev = NULL;
751 struct twl4030_client *twl = NULL;
754 /* FIXME this doesn't yet set up platform_data for anything;
755 * it can't be available until this becomes a "new style"
756 * I2C driver. Similarly, a new style driver will know it
757 * didn't already initialize its children.
763 #ifdef CONFIG_RTC_DRV_TWL4030
764 pdev = platform_device_alloc("twl4030_rtc", -1);
766 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
767 pdev->dev.parent = &twl->client.dev;
768 device_init_wakeup(&pdev->dev, 1);
771 * FIXME add the relevant IRQ resource, and make the
772 * rtc driver use it instead of hard-wiring ...
774 * REVISIT platform_data here currently only supports
775 * setting up the "msecure" line ... which actually
776 * violates the "princple of least privilege", since
777 * it's effectively always in "high trust" mode.
779 * For now, expect equivalent treatment at board init:
780 * setting msecure high. Eventually, Linux might
781 * become more aware of those HW security concerns.
784 status = platform_device_add(pdev);
786 platform_device_put(pdev);
795 /* adapter callback */
796 static int __init twl4030_attach_adapter(struct i2c_adapter *adapter)
800 static int twl_i2c_adapter = 1;
802 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
803 /* Check if I need to hook on to this adapter or not */
804 if (twl4030_modules[i].adapter_index == twl_i2c_adapter) {
805 ret = twl4030_detect_client(adapter, i);
815 * Check if the PIH module is initialized, if yes, then init
816 * the T2 Interrupt subsystem
818 if ((twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse ==
819 TWL_CLIENT_USED) && (twl_irq_used != USED)) {
826 pr_err("TWL_CLIENT(Idx=%d] registration failed[0x%x]\n", i, ret);
828 /* ignore current slave..it never got registered */
831 /* now remove all those from the current adapter... */
832 if (twl4030_modules[i].adapter_index == twl_i2c_adapter)
833 (void)twl4030_detach_client(&twl4030_modules[i].client);
839 /* adapter's callback */
840 static int twl4030_detach_client(struct i2c_client *client)
843 err = i2c_detach_client(client);
845 pr_err("Client detach failed\n");
851 static struct task_struct * __init start_twl4030_irq_thread(int irq)
853 struct task_struct *thread;
855 init_completion(&irq_event);
856 thread = kthread_run(twl4030_irq_thread, (void *)irq,
857 "twl4030 irq %d", irq);
859 pr_err("%s: could not create twl4030 irq %d thread!\n",
866 * These three functions should be part of Voltage frame work
867 * added here to complete the functionality for now.
869 static int __init protect_pm_master(void)
873 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
878 static int __init unprotect_pm_master(void)
882 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
884 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
889 static int __init power_companion_init(void)
893 u8 ctrl = HFCLK_FREQ_26_MHZ;
896 if (cpu_is_omap2430())
897 osc = clk_get(NULL, "osc_ck");
899 osc = clk_get(NULL, "osc_sys_ck");
901 printk(KERN_WARNING "Skipping twl3040 internal clock init and "
902 "using bootloader value (unknown osc rate)\n");
906 rate = clk_get_rate(osc);
910 case 19200000 : ctrl = HFCLK_FREQ_19p2_MHZ; break;
911 case 26000000 : ctrl = HFCLK_FREQ_26_MHZ; break;
912 case 38400000 : ctrl = HFCLK_FREQ_38p4_MHZ; break;
915 ctrl |= HIGH_PERF_SQ;
916 e |= unprotect_pm_master();
917 /* effect->MADC+USB ck en */
918 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
919 e |= protect_pm_master();
925 * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
926 * @mod_no: TWL4030 module number
927 * @reg: register index to clear
928 * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
930 * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
931 * status register to ensure that any prior interrupts are cleared.
932 * Returns the status from the I2C read operation.
934 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
938 return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
939 twl4030_i2c_write_u8(mod_no, 0xff, reg);
943 * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
944 * @mod_no: TWL4030 module number
945 * @reg: register index to clear
947 * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
948 * the specified TWL module are cleared by reads, or 0 if cleared by
951 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
955 WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
957 tmp &= TWL4030_SIH_CTRL_COR_MASK;
958 tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
964 * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
965 * @t: pointer to twl4030_mod_iregs array
966 * @t_sz: ARRAY_SIZE(t) (starting at 1)
968 * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
969 * interrupt status registers (ISRs). No return value, but will WARN if
970 * any I2C operations fail.
972 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
978 * N.B. - further efficiency is possible here. Eight I2C
979 * operations on BCI and GPIO modules are avoidable if I2C
980 * burst read/write transactions were implemented. Would
981 * probably save about 1ms of boot time and a small amount of
984 for (i = 0; i < t_sz; i++) {
985 const struct twl4030_mod_iregs tmr = t[i];
988 /* Are ISRs cleared by reads or writes? */
989 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
992 for (j = 0; j < tmr.reg_cnt; j++) {
994 /* Mask interrupts at the TWL4030 */
995 WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
998 /* Clear TWL4030 ISRs */
999 WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
1000 tmr.isrs[j], cor) < 0);
1008 static void twl_init_irq(void)
1012 char *msg = "Unable to register interrupt subsystem";
1013 unsigned int irq_num;
1016 * Mask and clear all TWL4030 interrupts since initially we do
1017 * not have any TWL4030 module interrupt handlers present
1019 twl4030_mask_clear_intrs(twl4030_mod_regs,
1020 ARRAY_SIZE(twl4030_mod_regs));
1022 /* install an irq handler for each of the PIH modules */
1023 for (i = TWL4030_IRQ_BASE; i < TWL4030_IRQ_END; i++) {
1024 set_irq_chip(i, &twl4030_irq_chip);
1025 set_irq_handler(i, do_twl4030_module_irq);
1026 set_irq_flags(i, IRQF_VALID);
1029 irq_num = (cpu_is_omap2430()) ? INT_24XX_SYS_NIRQ : INT_34XX_SYS_NIRQ;
1031 /* install an irq handler to demultiplex the TWL4030 interrupt */
1032 set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
1033 set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
1034 set_irq_chained_handler(irq_num, do_twl4030_irq);
1036 res = power_companion_init();
1038 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
1041 static int __init twl4030_init(void)
1043 return i2c_add_driver(&twl4030_driver);
1046 static void __exit twl4030_exit(void)
1048 i2c_del_driver(&twl4030_driver);
1049 twl_irq_used = FREE;
1052 subsys_initcall(twl4030_init);
1053 module_exit(twl4030_exit);
1055 MODULE_ALIAS("i2c:" DRIVER_NAME);
1056 MODULE_AUTHOR("Texas Instruments, Inc.");
1057 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1058 MODULE_LICENSE("GPL");