]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-core.c
i2c: move twl4030-madc to new registration style
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / twl4030-core.c
1 /*
2  * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
3  *
4  * Copyright (C) 2005-2006 Texas Instruments, Inc.
5  *
6  * Modifications to defer interrupt handling to a kernel thread:
7  * Copyright (C) 2006 MontaVista Software, Inc.
8  *
9  * Based on tlv320aic23.c:
10  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
11  *
12  * Code cleanup and modifications to IRQ handler.
13  * by syed khasim <x0khasim@ti.com>
14  *
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.
19  *
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.
24  *
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
28  */
29
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>
39
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>
45
46 #define DRIVER_NAME                     "twl4030"
47
48 #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
49 #define twl_has_rtc()   true
50 #else
51 #define twl_has_rtc()   false
52 #endif
53
54 #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
55 #define twl_has_keypad()        true
56 #else
57 #define twl_has_keypad()        false
58 #endif
59
60 #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
61 #define twl_has_usb()   true
62 #else
63 #define twl_has_usb()   false
64 #endif
65
66 #ifdef CONFIG_TWL4030_GPIO
67 #define twl_has_gpio()  true
68 #else
69 #define twl_has_gpio()  false
70 #endif
71
72 #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
73 #define twl_has_madc()  true
74 #else
75 #define twl_has_madc()  false
76 #endif
77
78 /* Primary Interrupt Handler on TWL4030 Registers */
79
80 /* Register Definitions */
81
82 #define REG_PIH_ISR_P1                  (0x1)
83 #define REG_PIH_ISR_P2                  (0x2)
84 #define REG_PIH_SIR                     (0x3)
85
86 /* Triton Core internal information (BEGIN) */
87
88 /* Last - for index max*/
89 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
90
91 #define TWL4030_NUM_SLAVES              4
92
93 /* Slave address */
94 #define TWL4030_SLAVENUM_NUM0           0x00
95 #define TWL4030_SLAVENUM_NUM1           0x01
96 #define TWL4030_SLAVENUM_NUM2           0x02
97 #define TWL4030_SLAVENUM_NUM3           0x03
98
99 /* Base Address defns */
100 /* USB ID */
101 #define TWL4030_BASEADD_USB             0x0000
102 /* AUD ID */
103 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
104 #define TWL4030_BASEADD_GPIO            0x0098
105
106 #define TWL4030_BASEADD_INTBR           0x0085
107 #define TWL4030_BASEADD_PIH             0x0080
108 #define TWL4030_BASEADD_TEST            0x004C
109 /* AUX ID */
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
120 /* POWER ID */
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
127
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
137
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
143
144
145 /* Triton Core internal information (END) */
146
147 /* Few power values */
148 #define R_CFG_BOOT                      0x05
149 #define R_PROTECT_KEY                   0x0E
150
151 /* access control */
152 #define KEY_UNLOCK1                     0xce
153 #define KEY_UNLOCK2                     0xec
154 #define KEY_LOCK                        0x00
155
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)
160
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
165
166 #define TWL4030_SIH_CTRL_COR_MASK       (1 << 2)
167
168 /**
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
175  *
176  * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
177  * during twl_init_irq().
178  */
179 struct twl4030_mod_iregs {
180         const u8 mod_no;
181         const u8 sih_ctrl;
182         const u8 reg_cnt;
183         const u8 *imrs;
184         const u8 *isrs;
185 };
186
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,
191 };
192
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,
197 };
198
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,
205 };
206
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,
213 };
214
215 /* TWL4030 MADC module interrupt mask registers */
216 static const u8 __initconst twl4030_madc_imr_regs[] = {
217         TWL4030_MADC_IMR1,
218         TWL4030_MADC_IMR2,
219 };
220
221 /* TWL4030 MADC module interrupt status registers */
222 static const u8 __initconst twl4030_madc_isr_regs[] = {
223         TWL4030_MADC_ISR1,
224         TWL4030_MADC_ISR2,
225 };
226
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,
231 };
232
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,
237 };
238
239 /* TWL4030 GPIO module interrupt mask registers */
240 static const u8 __initconst twl4030_gpio_imr_regs[] = {
241         REG_GPIO_IMR1A,
242         REG_GPIO_IMR1B,
243         REG_GPIO_IMR2A,
244         REG_GPIO_IMR2B,
245         REG_GPIO_IMR3A,
246         REG_GPIO_IMR3B,
247 };
248
249 /* TWL4030 GPIO module interrupt status registers */
250 static const u8 __initconst twl4030_gpio_isr_regs[] = {
251         REG_GPIO_ISR1A,
252         REG_GPIO_ISR1B,
253         REG_GPIO_ISR2A,
254         REG_GPIO_ISR2B,
255         REG_GPIO_ISR3A,
256         REG_GPIO_ISR3B,
257 };
258
259 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
260 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
261         {
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,
267         },
268         {
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,
274         },
275         {
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,
281         },
282         {
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,
288         },
289         {
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,
295         },
296 };
297
298
299 /* Data Structures */
300 /* To have info on T2 IRQ substem activated or not */
301 static struct completion irq_event;
302
303 /* Structure to define on TWL4030 Slave ID */
304 struct twl4030_client {
305         struct i2c_client *client;
306         u8 address;
307         bool inuse;
308
309         /* max numb of i2c_msg required is for read =2 */
310         struct i2c_msg xfer_msg[2];
311
312         /* To lock access to xfer_msg */
313         struct mutex xfer_lock;
314 };
315
316 /* Module Mapping */
317 struct twl4030mapping {
318         unsigned char sid;      /* Slave ID */
319         unsigned char base;     /* base address */
320 };
321
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 },
346 };
347
348 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
349
350 /*
351  * TWL4030 doesn't have PIH mask, hence dummy function for mask
352  * and unmask.
353  */
354
355 static void twl4030_i2c_ackirq(unsigned int irq)
356 {
357 }
358
359 static void twl4030_i2c_disableint(unsigned int irq)
360 {
361 }
362
363 static void twl4030_i2c_enableint(unsigned int irq)
364 {
365 }
366
367 /* information for processing in the Work Item */
368 static struct irq_chip twl4030_irq_chip = {
369         .name   = "twl4030",
370         .ack    = twl4030_i2c_ackirq,
371         .mask   = twl4030_i2c_disableint,
372         .unmask = twl4030_i2c_enableint,
373 };
374
375 /* Global Functions */
376
377 /**
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
383  *
384  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
385  * valid data starts at Offset 1.
386  *
387  * Returns the result of operation - 0 is success
388  */
389 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
390 {
391         int ret;
392         int sid;
393         struct twl4030_client *twl;
394         struct i2c_msg *msg;
395
396         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
397                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
398                 return -EPERM;
399         }
400         sid = twl4030_map[mod_no].sid;
401         twl = &twl4030_modules[sid];
402
403         if (unlikely(!twl->inuse)) {
404                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
405                 return -EPERM;
406         }
407         mutex_lock(&twl->xfer_lock);
408         /*
409          * [MSG1]: fill the register address data
410          * fill the data Tx buffer
411          */
412         msg = &twl->xfer_msg[0];
413         msg->addr = twl->address;
414         msg->len = num_bytes + 1;
415         msg->flags = 0;
416         msg->buf = value;
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);
421
422         /* i2cTransfer returns num messages.translate it pls.. */
423         if (ret >= 0)
424                 ret = 0;
425         return ret;
426 }
427 EXPORT_SYMBOL(twl4030_i2c_write);
428
429 /**
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
435  *
436  * Returns result of operation - num_bytes is success else failure.
437  */
438 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
439 {
440         int ret;
441         u8 val;
442         int sid;
443         struct twl4030_client *twl;
444         struct i2c_msg *msg;
445
446         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
447                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
448                 return -EPERM;
449         }
450         sid = twl4030_map[mod_no].sid;
451         twl = &twl4030_modules[sid];
452
453         if (unlikely(!twl->inuse)) {
454                 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
455                 return -EPERM;
456         }
457         mutex_lock(&twl->xfer_lock);
458         /* [MSG1] fill the register address data */
459         msg = &twl->xfer_msg[0];
460         msg->addr = twl->address;
461         msg->len = 1;
462         msg->flags = 0; /* Read the register value */
463         val = twl4030_map[mod_no].base + reg;
464         msg->buf = &val;
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 */
470         msg->buf = value;
471         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
472         mutex_unlock(&twl->xfer_lock);
473
474         /* i2cTransfer returns num messages.translate it pls.. */
475         if (ret >= 0)
476                 ret = 0;
477         return ret;
478 }
479 EXPORT_SYMBOL(twl4030_i2c_read);
480
481 /**
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)
486  *
487  * Returns result of operation - 0 is success
488  */
489 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
490 {
491
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);
497 }
498 EXPORT_SYMBOL(twl4030_i2c_write_u8);
499
500 /**
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)
505  *
506  * Returns result of operation - 0 is success
507  */
508 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
509 {
510         return twl4030_i2c_read(mod_no, value, reg, 1);
511 }
512 EXPORT_SYMBOL(twl4030_i2c_read_u8);
513
514 /* Helper Functions */
515
516 /*
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.
520  */
521 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
522 {
523         struct irqaction *action;
524         const unsigned int cpu = smp_processor_id();
525
526         /*
527          * Earlier this was desc->triggered = 1;
528          */
529         desc->status |= IRQ_LEVEL;
530
531         /*
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.
534          */
535
536         if (!desc->depth) {
537                 kstat_cpu(cpu).irqs[irq]++;
538
539                 action = desc->action;
540                 if (action) {
541                         int ret;
542                         int status = 0;
543                         int retval = 0;
544
545                         local_irq_enable();
546
547                         do {
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;
552                                 retval |= ret;
553                                 action = action->next;
554                         } while (action);
555
556                         if (status & IRQF_SAMPLE_RANDOM)
557                                 add_interrupt_randomness(irq);
558
559                         local_irq_disable();
560
561                         if (retval != IRQ_HANDLED)
562                                 printk(KERN_ERR "ISR for TWL4030 module"
563                                         " irq %d can't handle interrupt\n",
564                                         irq);
565
566                         /*
567                          * Here is where we should call the unmask method, but
568                          * again we won't bother since it is NULL.
569                          */
570                 } else
571                         printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
572                                         " but can't be masked!\n", irq);
573         } else
574                 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
575                                 " be masked!\n", irq);
576 }
577
578 static unsigned twl4030_irq_base;
579
580 /*
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.
584  */
585 static int twl4030_irq_thread(void *data)
586 {
587         int irq = (int)data;
588         irq_desc_t *desc = irq_desc + irq;
589         static unsigned i2c_errors;
590         const static unsigned max_i2c_errors = 100;
591
592         daemonize("twl4030-irq");
593         current->flags |= PF_NOFREEZE;
594
595         while (!kthread_should_stop()) {
596                 int ret;
597                 int module_irq;
598                 u8 pih_isr;
599
600                 wait_for_completion_interruptible(&irq_event);
601
602                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
603                                           REG_PIH_ISR_P1);
604                 if (ret) {
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",
610                                                 __func__);
611                                 break;
612                         }
613                         continue;
614                 }
615
616                 for (module_irq = twl4030_irq_base; 0 != pih_isr;
617                          pih_isr >>= 1, module_irq++) {
618                         if (pih_isr & 0x1) {
619                                 irq_desc_t *d = irq_desc + module_irq;
620
621                                 local_irq_disable();
622
623                                 d->handle_irq(module_irq, d);
624
625                                 local_irq_enable();
626                         }
627                 }
628
629                 desc->chip->unmask(irq);
630         }
631
632         return 0;
633 }
634
635 /*
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
642  * the kernel thread.
643  */
644 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
645 {
646         const unsigned int cpu = smp_processor_id();
647
648         /*
649          * Earlier this was desc->triggered = 1;
650          */
651         desc->status |= IRQ_LEVEL;
652
653         /*
654          * Acknowledge, clear _AND_ disable the interrupt.
655          */
656         desc->chip->ack(irq);
657
658         if (!desc->depth) {
659                 kstat_cpu(cpu).irqs[irq]++;
660
661                 complete(&irq_event);
662         }
663 }
664
665 static int add_children(struct twl4030_platform_data *pdata)
666 {
667         struct platform_device  *pdev = NULL;
668         struct twl4030_client   *twl = NULL;
669         int                     status = 0;
670
671         if (twl_has_gpio() && pdata->gpio) {
672                 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM1];
673
674                 pdev = platform_device_alloc("twl4030_gpio", -1);
675                 if (!pdev)
676                         status = -ENOMEM;
677
678                 /* more driver model init */
679                 if (status == 0) {
680                         pdev->dev.parent = &twl->client->dev;
681                         /* device_init_wakeup(&pdev->dev, 1); */
682
683                         status = platform_device_add_data(pdev, pdata->gpio,
684                                         sizeof(*pdata->gpio));
685                 }
686
687                 /* GPIO module IRQ */
688                 if (status == 0) {
689                         struct resource r = {
690                                 .start = pdata->irq_base + 0,
691                                 .flags = IORESOURCE_IRQ,
692                         };
693
694                         status = platform_device_add_resources(pdev, &r, 1);
695                 }
696
697                 if (status == 0)
698                         status = platform_device_add(pdev);
699
700                 if (status < 0) {
701                         platform_device_put(pdev);
702                         dev_dbg(&twl->client->dev,
703                                         "can't create gpio dev, %d\n",
704                                         status);
705                         goto err;
706                 }
707         }
708
709         if (twl_has_rtc()) {
710                 pdev = platform_device_alloc("twl4030_rtc", -1);
711                 if (pdev) {
712                         twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
713                         pdev->dev.parent = &twl->client->dev;
714                         device_init_wakeup(&pdev->dev, 1);
715
716                         /*
717                          * FIXME add the relevant IRQ resource, and make the
718                          * rtc driver use it instead of hard-wiring ...
719                          *
720                          * REVISIT platform_data here currently only supports
721                          * setting up the "msecure" line ... which actually
722                          * violates the "princple of least privilege", since
723                          * it's effectively always in "high trust" mode.
724                          *
725                          * For now, expect equivalent treatment at board init:
726                          * setting msecure high.  Eventually, Linux might
727                          * become more aware of those HW security concerns.
728                          */
729
730                         status = platform_device_add(pdev);
731                         if (status < 0)
732                                 platform_device_put(pdev);
733                 } else {
734                         status = -ENOMEM;
735                         goto err;
736                 }
737         }
738
739         if (twl_has_keypad() && pdata->keypad) {
740                 pdev = platform_device_alloc("twl4030_keypad", -1);
741                 if (pdev) {
742                         twl = &twl4030_modules[TWL4030_SLAVENUM_NUM2];
743                         pdev->dev.parent = &twl->client->dev;
744                         device_init_wakeup(&pdev->dev, 1);
745                         status = platform_device_add_data(pdev, pdata->keypad,
746                                         sizeof(*pdata->keypad));
747                         if (status < 0) {
748                                 platform_device_put(pdev);
749                                 goto err;
750                         }
751                         status = platform_device_add(pdev);
752                         if (status < 0)
753                                 platform_device_put(pdev);
754                 } else {
755                         status = -ENOMEM;
756                         goto err;
757                 }
758         }
759
760         if (twl_has_usb() && pdata->usb) {
761                 pdev = platform_device_alloc("twl4030_usb", -1);
762                 if (pdev) {
763                         twl = &twl4030_modules[TWL4030_SLAVENUM_NUM0];
764                         pdev->dev.parent = &twl->client->dev;
765                         device_init_wakeup(&pdev->dev, 1);
766                         status = platform_device_add_data(pdev, pdata->usb,
767                                         sizeof(*pdata->usb));
768                         if (status < 0) {
769                                 platform_device_put(pdev);
770                                 goto err;
771                         }
772                         status = platform_device_add(pdev);
773                         if (status < 0)
774                                 platform_device_put(pdev);
775                 } else {
776                         status = -ENOMEM;
777                         goto err;
778                 }
779         }
780
781         if (twl_has_madc() && pdata->madc) {
782                 pdev = platform_device_alloc("twl4030_madc", -1);
783                 if (pdev) {
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->madc,
788                                         sizeof(*pdata->madc));
789                         if (status < 0) {
790                                 platform_device_put(pdev);
791                                 goto err;
792                         }
793                         status = platform_device_add(pdev);
794                         if (status < 0)
795                                 platform_device_put(pdev);
796                 } else {
797                         status = -ENOMEM;
798                         goto err;
799                 }
800         }
801
802 err:
803         pr_err("failed to add twl4030's children\n");
804         return status;
805 }
806
807 static struct task_struct * __init start_twl4030_irq_thread(int irq)
808 {
809         struct task_struct *thread;
810
811         init_completion(&irq_event);
812         thread = kthread_run(twl4030_irq_thread, (void *)irq,
813                              "twl4030 irq %d", irq);
814         if (!thread)
815                 pr_err("%s: could not create twl4030 irq %d thread!\n",
816                        DRIVER_NAME, irq);
817
818         return thread;
819 }
820
821 /*
822  * These three functions should be part of Voltage frame work
823  * added here to complete the functionality for now.
824  */
825 static int __init protect_pm_master(void)
826 {
827         int e = 0;
828
829         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
830                         R_PROTECT_KEY);
831         return e;
832 }
833
834 static int __init unprotect_pm_master(void)
835 {
836         int e = 0;
837
838         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
839                         R_PROTECT_KEY);
840         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
841                         R_PROTECT_KEY);
842         return e;
843 }
844
845 static int __init power_companion_init(void)
846 {
847         struct clk *osc;
848         u32 rate;
849         u8 ctrl = HFCLK_FREQ_26_MHZ;
850         int e = 0;
851
852         if (cpu_is_omap2430())
853                 osc = clk_get(NULL, "osc_ck");
854         else
855                 osc = clk_get(NULL, "osc_sys_ck");
856         if (IS_ERR(osc)) {
857                 printk(KERN_WARNING "Skipping twl3040 internal clock init and "
858                                 "using bootloader value (unknown osc rate)\n");
859                 return 0;
860         }
861
862         rate = clk_get_rate(osc);
863         clk_put(osc);
864
865         switch (rate) {
866         case 19200000:
867                 ctrl = HFCLK_FREQ_19p2_MHZ;
868                 break;
869         case 26000000:
870                 ctrl = HFCLK_FREQ_26_MHZ;
871                 break;
872         case 38400000:
873                 ctrl = HFCLK_FREQ_38p4_MHZ;
874                 break;
875         }
876
877         ctrl |= HIGH_PERF_SQ;
878         e |= unprotect_pm_master();
879         /* effect->MADC+USB ck en */
880         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
881         e |= protect_pm_master();
882
883         return e;
884 }
885
886 /**
887  * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
888  * @mod_no: TWL4030 module number
889  * @reg: register index to clear
890  * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
891  *
892  * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
893  * status register to ensure that any prior interrupts are cleared.
894  * Returns the status from the I2C read operation.
895  */
896 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
897 {
898         u8 tmp;
899
900         return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
901                 twl4030_i2c_write_u8(mod_no, 0xff, reg);
902 }
903
904 /**
905  * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
906  * @mod_no: TWL4030 module number
907  * @reg: register index to clear
908  *
909  * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
910  * the specified TWL module are cleared by reads, or 0 if cleared by
911  * writes.
912  */
913 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
914 {
915         u8 tmp = 0;
916
917         WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
918
919         tmp &= TWL4030_SIH_CTRL_COR_MASK;
920         tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
921
922         return tmp;
923 }
924
925 /**
926  * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
927  * @t: pointer to twl4030_mod_iregs array
928  * @t_sz: ARRAY_SIZE(t) (starting at 1)
929  *
930  * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
931  * interrupt status registers (ISRs).  No return value, but will WARN if
932  * any I2C operations fail.
933  */
934 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
935                                             const u8 t_sz)
936 {
937         int i, j;
938
939         /*
940          * N.B. - further efficiency is possible here.  Eight I2C
941          * operations on BCI and GPIO modules are avoidable if I2C
942          * burst read/write transactions were implemented.  Would
943          * probably save about 1ms of boot time and a small amount of
944          * power.
945          */
946         for (i = 0; i < t_sz; i++) {
947                 const struct twl4030_mod_iregs tmr = t[i];
948                 int cor;
949
950                 /* Are ISRs cleared by reads or writes? */
951                 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
952                 WARN_ON(cor < 0);
953
954                 for (j = 0; j < tmr.reg_cnt; j++) {
955
956                         /* Mask interrupts at the TWL4030 */
957                         WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
958                                                      tmr.imrs[j]) < 0);
959
960                         /* Clear TWL4030 ISRs */
961                         WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
962                                                       tmr.isrs[j], cor) < 0);
963                 }
964         }
965
966         return;
967 }
968
969
970 static void twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
971 {
972         int     i;
973         int     res = 0;
974         char    *msg = "Unable to register interrupt subsystem";
975
976         /*
977          * Mask and clear all TWL4030 interrupts since initially we do
978          * not have any TWL4030 module interrupt handlers present
979          */
980         twl4030_mask_clear_intrs(twl4030_mod_regs,
981                                  ARRAY_SIZE(twl4030_mod_regs));
982
983         twl4030_irq_base = irq_base;
984
985         /* install an irq handler for each of the PIH modules */
986         for (i = irq_base; i < irq_end; i++) {
987                 set_irq_chip(i, &twl4030_irq_chip);
988                 set_irq_handler(i, do_twl4030_module_irq);
989                 set_irq_flags(i, IRQF_VALID);
990         }
991
992         /* install an irq handler to demultiplex the TWL4030 interrupt */
993         set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
994         set_irq_type(irq_num, IRQ_TYPE_EDGE_FALLING);
995         set_irq_chained_handler(irq_num, do_twl4030_irq);
996
997         res = power_companion_init();
998         if (res < 0)
999                 pr_err("%s: %s[%d]\n", DRIVER_NAME, msg, res);
1000 }
1001
1002 /*----------------------------------------------------------------------*/
1003
1004 static int twl4030_remove(struct i2c_client *client)
1005 {
1006         unsigned i;
1007
1008         /* FIXME undo twl_init_irq() */
1009         if (twl4030_irq_base) {
1010                 dev_err(&client->dev, "can't yet clean up IRQs?\n");
1011                 return -ENOSYS;
1012         }
1013
1014         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1015                 struct twl4030_client   *twl = &twl4030_modules[i];
1016
1017                 if (twl->client && twl->client != client)
1018                         i2c_unregister_device(twl->client);
1019                 twl4030_modules[i].client = NULL;
1020                 twl4030_modules[i].inuse = false;
1021         }
1022         return 0;
1023 }
1024
1025 /* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
1026 static int
1027 twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
1028 {
1029         int                             status;
1030         unsigned                        i;
1031         struct twl4030_platform_data    *pdata = client->dev.platform_data;
1032
1033         if (!pdata) {
1034                 dev_dbg(&client->dev, "no platform data?\n");
1035                 return -EINVAL;
1036         }
1037
1038         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1039                 dev_dbg(&client->dev, "can't talk I2C?\n");
1040                 return -EIO;
1041         }
1042
1043         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1044                 if (twl4030_modules[i].inuse || twl4030_irq_base) {
1045                         dev_dbg(&client->dev, "driver is already in use\n");
1046                         return -EBUSY;
1047                 }
1048         }
1049
1050         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
1051                 struct twl4030_client   *twl = &twl4030_modules[i];
1052
1053                 twl->address = client->addr + i;
1054                 if (i == 0)
1055                         twl->client = client;
1056                 else {
1057                         twl->client = i2c_new_dummy(client->adapter,
1058                                         twl->address);
1059                         if (!twl->client) {
1060                                 dev_err(&twl->client->dev,
1061                                         "can't attach client %d\n", i);
1062                                 status = -ENOMEM;
1063                                 goto fail;
1064                         }
1065                         strlcpy(twl->client->name, id->name,
1066                                         sizeof(twl->client->name));
1067                 }
1068                 twl->inuse = true;
1069                 mutex_init(&twl->xfer_lock);
1070         }
1071
1072         status = add_children(pdata);
1073         if (status < 0)
1074                 goto fail;
1075
1076         /*
1077          * Check if the PIH module is initialized, if yes, then init
1078          * the T2 Interrupt subsystem
1079          */
1080         if (twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse
1081                         && twl4030_irq_base == 0
1082                         && client->irq
1083                         && pdata->irq_base
1084                         && pdata->irq_end > pdata->irq_base)
1085                 twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
1086
1087         dev_info(&client->dev, "chaining %d irqs\n",
1088                         twl4030_irq_base
1089                                 ? (pdata->irq_end - pdata->irq_base)
1090                                 : 0);
1091         return 0;
1092
1093 fail:
1094         twl4030_remove(client);
1095         return status;
1096 }
1097
1098 static const struct i2c_device_id twl4030_ids[] = {
1099         { "twl4030", 0 },       /* "Triton 2" */
1100         { "tps65950", 0 },      /* catalog version of twl4030 */
1101         { "tps65930", 0 },      /* fewer LDOs and DACs; no charger */
1102         { "tps65920", 0 },      /* fewer LDOs; no codec or charger */
1103         { /* end of list */ },
1104 };
1105 MODULE_DEVICE_TABLE(i2c, twl4030_ids);
1106
1107 /* One Client Driver , 4 Clients */
1108 static struct i2c_driver twl4030_driver = {
1109         .driver.name    = DRIVER_NAME,
1110         .id_table       = twl4030_ids,
1111         .probe          = twl4030_probe,
1112         .remove         = twl4030_remove,
1113 };
1114
1115 static int __init twl4030_init(void)
1116 {
1117         return i2c_add_driver(&twl4030_driver);
1118 }
1119 subsys_initcall(twl4030_init);
1120
1121 static void __exit twl4030_exit(void)
1122 {
1123         i2c_del_driver(&twl4030_driver);
1124 }
1125 module_exit(twl4030_exit);
1126
1127 MODULE_AUTHOR("Texas Instruments, Inc.");
1128 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1129 MODULE_LICENSE("GPL");