]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-core.c
8d7c5efd07f6c55a7f6215292f8eb85a2c3ce8e3
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / twl4030-core.c
1 /*
2  * twl4030_core.c - driver for TWL4030 PM and audio CODEC device
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
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>
41
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>
51
52 #include <asm/mach/irq.h>
53
54 #include <mach/gpio.h>
55 #include <mach/mux.h>
56
57 #define DRIVER_NAME                     "twl4030"
58
59 /* Macro Definitions */
60 #define TWL_CLIENT_STRING               "TWL4030-ID"
61 #define TWL_CLIENT_USED                 1
62 #define TWL_CLIENT_FREE                 0
63
64 /* IRQ Flags */
65 #define FREE                            0
66 #define USED                            1
67
68 /* Primary Interrupt Handler on TWL4030 Registers */
69
70 /* Register Definitions */
71
72 #define REG_PIH_ISR_P1                  (0x1)
73 #define REG_PIH_ISR_P2                  (0x2)
74 #define REG_PIH_SIR                     (0x3)
75
76 /* Triton Core internal information (BEGIN) */
77
78 /* Last - for index max*/
79 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
80
81 /* Slave address */
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
91
92 /* Base Address defns */
93 /* USB ID */
94 #define TWL4030_BASEADD_USB             0x0000
95 /* AUD ID */
96 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
97 #define TWL4030_BASEADD_GPIO            0x0098
98
99 #define TWL4030_BASEADD_INTBR           0x0085
100 #define TWL4030_BASEADD_PIH             0x0080
101 #define TWL4030_BASEADD_TEST            0x004C
102 /* AUX ID */
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
113 /* POWER ID */
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
120
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
130
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
136
137
138 /* Triton Core internal information (END) */
139
140 /* Few power values */
141 #define R_CFG_BOOT                      0x05
142 #define R_PROTECT_KEY                   0x0E
143
144 /* access control */
145 #define KEY_UNLOCK1                     0xce
146 #define KEY_UNLOCK2                     0xec
147 #define KEY_LOCK                        0x00
148
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)
153
154 /* on I2C-1 for 2430SDP */
155 #define CONFIG_I2C_TWL4030_ID           1
156
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
161
162 #define TWL4030_SIH_CTRL_COR_MASK       (1 << 2)
163
164 /**
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
171  *
172  * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
173  * during twl_init_irq().
174  */
175 struct twl4030_mod_iregs {
176         const u8 mod_no;
177         const u8 sih_ctrl;
178         const u8 reg_cnt;
179         const u8 *imrs;
180         const u8 *isrs;
181 };
182
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,
187 };
188
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,
193 };
194
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,
201 };
202
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,
209 };
210
211 /* TWL4030 MADC module interrupt mask registers */
212 static const u8 __initconst twl4030_madc_imr_regs[] = {
213         TWL4030_MADC_IMR1,
214         TWL4030_MADC_IMR2,
215 };
216
217 /* TWL4030 MADC module interrupt status registers */
218 static const u8 __initconst twl4030_madc_isr_regs[] = {
219         TWL4030_MADC_ISR1,
220         TWL4030_MADC_ISR2,
221 };
222
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,
227 };
228
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,
233 };
234
235 /* TWL4030 GPIO module interrupt mask registers */
236 static const u8 __initconst twl4030_gpio_imr_regs[] = {
237         REG_GPIO_IMR1A,
238         REG_GPIO_IMR1B,
239         REG_GPIO_IMR2A,
240         REG_GPIO_IMR2B,
241         REG_GPIO_IMR3A,
242         REG_GPIO_IMR3B,
243 };
244
245 /* TWL4030 GPIO module interrupt status registers */
246 static const u8 __initconst twl4030_gpio_isr_regs[] = {
247         REG_GPIO_ISR1A,
248         REG_GPIO_ISR1B,
249         REG_GPIO_ISR2A,
250         REG_GPIO_ISR2B,
251         REG_GPIO_ISR3A,
252         REG_GPIO_ISR3B,
253 };
254
255 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
256 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
257         {
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,
263         },
264         {
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,
270         },
271         {
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,
277         },
278         {
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,
284         },
285         {
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,
291         },
292 };
293
294
295 /* Helper functions */
296 static int
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);
301
302 static void twl_init_irq(void);
303
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;
308
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;
315         unsigned char inuse;
316
317         /* max numb of i2c_msg required is for read =2 */
318         struct i2c_msg xfer_msg[2];
319
320         /* To lock access to xfer_msg */
321         struct mutex xfer_lock;
322 };
323
324 /* Module Mapping */
325 struct twl4030mapping {
326         unsigned char sid;      /* Slave ID */
327         unsigned char base;     /* base address */
328 };
329
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 },
354 };
355
356 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES] = {
357         {
358                 .address        = TWL4030_SLAVEID_ID0,
359                 .client_name    = TWL_CLIENT_STRING "0",
360                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
361         },
362         {
363                 .address        = TWL4030_SLAVEID_ID1,
364                 .client_name    = TWL_CLIENT_STRING "1",
365                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
366         },
367         {
368                 .address        = TWL4030_SLAVEID_ID2,
369                 .client_name    = TWL_CLIENT_STRING "2",
370                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
371         },
372         {
373                 .address        = TWL4030_SLAVEID_ID3,
374                 .client_name    = TWL_CLIENT_STRING "3",
375                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
376         },
377 };
378
379 /* One Client Driver , 4 Clients */
380 static struct i2c_driver twl4030_driver = {
381         .driver = {
382                 .name   = DRIVER_NAME,
383                 .owner  = THIS_MODULE,
384         },
385         .attach_adapter = twl4030_attach_adapter,
386         .detach_client  = twl4030_detach_client,
387 };
388
389 /*
390  * TWL4030 doesn't have PIH mask, hence dummy function for mask
391  * and unmask.
392  */
393
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) {}
397
398 /* information for processing in the Work Item */
399 static struct irq_chip twl4030_irq_chip = {
400         .name   = "twl4030",
401         .ack    = twl4030_i2c_ackirq,
402         .mask   = twl4030_i2c_disableint,
403         .unmask = twl4030_i2c_enableint,
404 };
405
406 /* Global Functions */
407
408 /**
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
414  *
415  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
416  * valid data starts at Offset 1.
417  *
418  * Returns the result of operation - 0 is success
419  */
420 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
421 {
422         int ret;
423         int sid;
424         struct twl4030_client *twl;
425         struct i2c_msg *msg;
426
427         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
428                 pr_err("Invalid module Number\n");
429                 return -EPERM;
430         }
431         sid = twl4030_map[mod_no].sid;
432         twl = &twl4030_modules[sid];
433
434         if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
435                 pr_err("I2C Client[%d] is not initialized[%d]\n",
436                        sid, __LINE__);
437                 return -EPERM;
438         }
439         mutex_lock(&twl->xfer_lock);
440         /*
441          * [MSG1]: fill the register address data
442          * fill the data Tx buffer
443          */
444         msg = &twl->xfer_msg[0];
445         msg->addr = twl->address;
446         msg->len = num_bytes + 1;
447         msg->flags = 0;
448         msg->buf = value;
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);
453
454         /* i2cTransfer returns num messages.translate it pls.. */
455         if (ret >= 0)
456                 ret = 0;
457         return ret;
458 }
459 EXPORT_SYMBOL(twl4030_i2c_write);
460
461 /**
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
467  *
468  * Returns result of operation - num_bytes is success else failure.
469  */
470 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
471 {
472         int ret;
473         u8 val;
474         int sid;
475         struct twl4030_client *twl;
476         struct i2c_msg *msg;
477
478         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
479                 pr_err("Invalid module Number\n");
480                 return -EPERM;
481         }
482         sid = twl4030_map[mod_no].sid;
483         twl = &twl4030_modules[sid];
484
485         if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
486                 pr_err("I2C Client[%d] is not initialized[%d]\n", sid,
487                        __LINE__);
488                 return -EPERM;
489         }
490         mutex_lock(&twl->xfer_lock);
491         /* [MSG1] fill the register address data */
492         msg = &twl->xfer_msg[0];
493         msg->addr = twl->address;
494         msg->len = 1;
495         msg->flags = 0; /* Read the register value */
496         val = twl4030_map[mod_no].base + reg;
497         msg->buf = &val;
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 */
503         msg->buf = value;
504         ret = i2c_transfer(twl->client.adapter, twl->xfer_msg, 2);
505         mutex_unlock(&twl->xfer_lock);
506
507         /* i2cTransfer returns num messages.translate it pls.. */
508         if (ret >= 0)
509                 ret = 0;
510         return ret;
511 }
512 EXPORT_SYMBOL(twl4030_i2c_read);
513
514 /**
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)
519  *
520  * Returns result of operation - 0 is success
521  */
522 int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
523 {
524
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);
530 }
531 EXPORT_SYMBOL(twl4030_i2c_write_u8);
532
533 /**
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)
538  *
539  * Returns result of operation - 0 is success
540  */
541 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
542 {
543         return twl4030_i2c_read(mod_no, value, reg, 1);
544 }
545 EXPORT_SYMBOL(twl4030_i2c_read_u8);
546
547 /* Helper Functions */
548
549 /*
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.
553  */
554 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
555 {
556         struct irqaction *action;
557         const unsigned int cpu = smp_processor_id();
558
559         /*
560          * Earlier this was desc->triggered = 1;
561          */
562         desc->status |= IRQ_LEVEL;
563
564         /*
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.
567          */
568
569         if (!desc->depth) {
570                 kstat_cpu(cpu).irqs[irq]++;
571
572                 action = desc->action;
573                 if (action) {
574                         int ret;
575                         int status = 0;
576                         int retval = 0;
577
578                         local_irq_enable();
579
580                         do {
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;
585                                 retval |= ret;
586                                 action = action->next;
587                         } while (action);
588
589                         if (status & IRQF_SAMPLE_RANDOM)
590                                 add_interrupt_randomness(irq);
591
592                         local_irq_disable();
593
594                         if (retval != IRQ_HANDLED)
595                                 printk(KERN_ERR "ISR for TWL4030 module"
596                                         " irq %d can't handle interrupt\n",
597                                         irq);
598
599                         /*
600                          * Here is where we should call the unmask method, but
601                          * again we won't bother since it is NULL.
602                          */
603                 } else
604                         printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
605                                         " but can't be masked!\n", irq);
606         } else
607                 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
608                                 " be masked!\n", irq);
609 }
610
611 /*
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.
615  */
616 static int twl4030_irq_thread(void *data)
617 {
618         int irq = (int)data;
619         irq_desc_t *desc = irq_desc + irq;
620         static unsigned i2c_errors;
621         const static unsigned max_i2c_errors = 100;
622
623         daemonize("twl4030-irq");
624         current->flags |= PF_NOFREEZE;
625
626         while (!kthread_should_stop()) {
627                 int ret;
628                 int module_irq;
629                 u8 pih_isr;
630
631                 wait_for_completion_interruptible(&irq_event);
632
633                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
634                                           REG_PIH_ISR_P1);
635                 if (ret) {
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",
641                                                 __func__);
642                                 break;
643                         }
644                         continue;
645                 }
646
647                 for (module_irq = TWL4030_IRQ_BASE; 0 != pih_isr;
648                          pih_isr >>= 1, module_irq++) {
649                         if (pih_isr & 0x1) {
650                                 irq_desc_t *d = irq_desc + module_irq;
651
652                                 local_irq_disable();
653
654                                 d->handle_irq(module_irq, d);
655
656                                 local_irq_enable();
657                         }
658                 }
659
660                 desc->chip->unmask(irq);
661         }
662
663         return 0;
664 }
665
666 /*
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
673  * the kernel thread.
674  */
675 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
676 {
677         const unsigned int cpu = smp_processor_id();
678
679         /*
680          * Earlier this was desc->triggered = 1;
681          */
682         desc->status |= IRQ_LEVEL;
683
684         /*
685          * Acknowledge, clear _AND_ disable the interrupt.
686          */
687         desc->chip->ack(irq);
688
689         if (!desc->depth) {
690                 kstat_cpu(cpu).irqs[irq]++;
691
692                 complete(&irq_event);
693         }
694 }
695
696 /* attach a client to the adapter */
697 static int __init twl4030_detect_client(struct i2c_adapter *adapter,
698                                         unsigned char sid)
699 {
700         int err = 0;
701         struct twl4030_client *twl;
702
703         if (unlikely(sid >= TWL4030_NUM_SLAVES)) {
704                 pr_err("sid[%d] > MOD_LAST[%d]\n", sid, TWL4030_NUM_SLAVES);
705                 return -EPERM;
706         }
707
708         /* Check basic functionality */
709         err = i2c_check_functionality(adapter,
710                         I2C_FUNC_SMBUS_WORD_DATA
711                         | I2C_FUNC_SMBUS_WRITE_BYTE);
712         if (!err) {
713                 pr_err("SlaveID=%d functionality check failed\n", sid);
714                 return err;
715         }
716         twl = &twl4030_modules[sid];
717         if (unlikely(twl->inuse)) {
718                 pr_err("Client %s is already in use\n", twl->client_name);
719                 return -EPERM;
720         }
721
722         memset(&twl->client, 0, sizeof(struct i2c_client));
723
724         twl->client.addr        = twl->address;
725         twl->client.adapter     = adapter;
726         twl->client.driver      = &twl4030_driver;
727
728         memcpy(&twl->client.name, twl->client_name,
729                         sizeof(TWL_CLIENT_STRING) + 1);
730
731         pr_info("TWL4030: TRY attach Slave %s on Adapter %s [%x]\n",
732                                 twl->client_name, adapter->name, err);
733
734         err = i2c_attach_client(&twl->client);
735         if (err) {
736                 pr_err("Couldn't attach Slave %s on Adapter"
737                        "%s [%x]\n", twl->client_name, adapter->name, err);
738         } else {
739                 twl->inuse = TWL_CLIENT_USED;
740                 mutex_init(&twl->xfer_lock);
741         }
742
743         return err;
744 }
745
746 static int add_children(void)
747 {
748         static bool             children;
749
750         struct platform_device  *pdev = NULL;
751         struct twl4030_client   *twl = NULL;
752         int                     status = 0;
753
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.
758          */
759
760         if (children)
761                 return 0;
762
763 #ifdef CONFIG_RTC_DRV_TWL4030
764         pdev = platform_device_alloc("twl4030_rtc", -1);
765         if (pdev) {
766                 twl = &twl4030_modules[TWL4030_SLAVENUM_NUM3];
767                 pdev->dev.parent = &twl->client.dev;
768                 device_init_wakeup(&pdev->dev, 1);
769
770                 /*
771                  * FIXME add the relevant IRQ resource, and make the
772                  * rtc driver use it instead of hard-wiring ...
773                  *
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.
778                  *
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.
782                  */
783
784                 status = platform_device_add(pdev);
785                 if (status < 0)
786                         platform_device_put(pdev);
787         } else
788                 status = -ENOMEM;
789 #endif
790
791         children = true;
792         return status;
793 }
794
795 /* adapter callback */
796 static int __init twl4030_attach_adapter(struct i2c_adapter *adapter)
797 {
798         int i;
799         int ret = 0;
800         static int twl_i2c_adapter = 1;
801
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);
806                         if (ret)
807                                 goto free_client;
808                 }
809         }
810         twl_i2c_adapter++;
811
812         add_children();
813
814         /*
815          * Check if the PIH module is initialized, if yes, then init
816          * the T2 Interrupt subsystem
817          */
818         if ((twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse ==
819                 TWL_CLIENT_USED) && (twl_irq_used != USED)) {
820                 twl_init_irq();
821                 twl_irq_used = USED;
822         }
823         return 0;
824
825 free_client:
826         pr_err("TWL_CLIENT(Idx=%d] registration failed[0x%x]\n", i, ret);
827
828         /* ignore current slave..it never got registered */
829         i--;
830         while (i >= 0) {
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);
834                 i--;
835         }
836         return ret;
837 }
838
839 /* adapter's callback */
840 static int twl4030_detach_client(struct i2c_client *client)
841 {
842         int err;
843         err = i2c_detach_client(client);
844         if (err) {
845                 pr_err("Client detach failed\n");
846                 return err;
847         }
848         return 0;
849 }
850
851 static struct task_struct * __init start_twl4030_irq_thread(int irq)
852 {
853         struct task_struct *thread;
854
855         init_completion(&irq_event);
856         thread = kthread_run(twl4030_irq_thread, (void *)irq,
857                              "twl4030 irq %d", irq);
858         if (!thread)
859                 pr_err("%s: could not create twl4030 irq %d thread!\n",
860                        __func__, irq);
861
862         return thread;
863 }
864
865 /*
866  * These three functions should be part of Voltage frame work
867  * added here to complete the functionality for now.
868  */
869 static int __init protect_pm_master(void)
870 {
871         int e = 0;
872
873         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
874                         R_PROTECT_KEY);
875         return e;
876 }
877
878 static int __init unprotect_pm_master(void)
879 {
880         int e = 0;
881
882         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
883                         R_PROTECT_KEY);
884         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
885                         R_PROTECT_KEY);
886         return e;
887 }
888
889 static int __init power_companion_init(void)
890 {
891         struct clk *osc;
892         u32 rate;
893         u8 ctrl = HFCLK_FREQ_26_MHZ;
894         int e = 0;
895
896         if (cpu_is_omap2430())
897                 osc = clk_get(NULL, "osc_ck");
898         else
899                 osc = clk_get(NULL, "osc_sys_ck");
900         if (IS_ERR(osc)) {
901                 printk(KERN_WARNING "Skipping twl3040 internal clock init and "
902                                 "using bootloader value (unknown osc rate)\n");
903                 return 0;
904         }
905
906         rate = clk_get_rate(osc);
907         clk_put(osc);
908
909         switch (rate) {
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;
913         }
914
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();
920
921         return e;
922 }
923
924 /**
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)
929  *
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.
933  */
934 static int __init twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
935 {
936         u8 tmp;
937
938         return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
939                 twl4030_i2c_write_u8(mod_no, 0xff, reg);
940 }
941
942 /**
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
946  *
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
949  * writes.
950  */
951 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
952 {
953         u8 tmp = 0;
954
955         WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
956
957         tmp &= TWL4030_SIH_CTRL_COR_MASK;
958         tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
959
960         return tmp;
961 }
962
963 /**
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)
967  *
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.
971  */
972 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
973                                             const u8 t_sz)
974 {
975         int i, j;
976
977         /*
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
982          * power.
983          */
984         for (i = 0; i < t_sz; i++) {
985                 const struct twl4030_mod_iregs tmr = t[i];
986                 int cor;
987
988                 /* Are ISRs cleared by reads or writes? */
989                 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
990                 WARN_ON(cor < 0);
991
992                 for (j = 0; j < tmr.reg_cnt; j++) {
993
994                         /* Mask interrupts at the TWL4030 */
995                         WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
996                                                      tmr.imrs[j]) < 0);
997
998                         /* Clear TWL4030 ISRs */
999                         WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
1000                                                       tmr.isrs[j], cor) < 0);
1001                 }
1002         }
1003
1004         return;
1005 }
1006
1007
1008 static void twl_init_irq(void)
1009 {
1010         int     i;
1011         int     res = 0;
1012         char    *msg = "Unable to register interrupt subsystem";
1013         unsigned int irq_num;
1014
1015         /*
1016          * Mask and clear all TWL4030 interrupts since initially we do
1017          * not have any TWL4030 module interrupt handlers present
1018          */
1019         twl4030_mask_clear_intrs(twl4030_mod_regs,
1020                                  ARRAY_SIZE(twl4030_mod_regs));
1021
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);
1027         }
1028
1029         irq_num = (cpu_is_omap2430()) ? INT_24XX_SYS_NIRQ : INT_34XX_SYS_NIRQ;
1030
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);
1035
1036         res = power_companion_init();
1037         if (res < 0)
1038                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
1039 }
1040
1041 static int __init twl4030_init(void)
1042 {
1043         return i2c_add_driver(&twl4030_driver);
1044 }
1045
1046 static void __exit twl4030_exit(void)
1047 {
1048         i2c_del_driver(&twl4030_driver);
1049         twl_irq_used = FREE;
1050 }
1051
1052 subsys_initcall(twl4030_init);
1053 module_exit(twl4030_exit);
1054
1055 MODULE_ALIAS("i2c:" DRIVER_NAME);
1056 MODULE_AUTHOR("Texas Instruments, Inc.");
1057 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1058 MODULE_LICENSE("GPL");