]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/twl4030-core.c
TWL4030: convert early interrupt mask/clear funcs to use array
[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
41 #include <linux/i2c.h>
42 #include <linux/i2c/twl4030.h>
43 #include <linux/i2c/twl4030-gpio.h>
44 #include <linux/i2c/twl4030-madc.h>
45 #include <linux/i2c/twl4030-pwrirq.h>
46 #include <linux/slab.h>
47 #include <linux/clk.h>
48 #include <linux/device.h>
49 #include <linux/irq.h>
50
51 #include <asm/mach/irq.h>
52
53 #include <asm/arch/gpio.h>
54 #include <asm/arch/mux.h>
55
56 #define DRIVER_NAME                     "twl4030"
57
58 /* Macro Definitions */
59 #define TWL_CLIENT_STRING               "TWL4030-ID"
60 #define TWL_CLIENT_USED                 1
61 #define TWL_CLIENT_FREE                 0
62
63 /* IRQ Flags */
64 #define FREE                            0
65 #define USED                            1
66
67 /* Primary Interrupt Handler on TWL4030 Registers */
68
69 /* Register Definitions */
70
71 #define REG_PIH_ISR_P1                  (0x1)
72 #define REG_PIH_ISR_P2                  (0x2)
73 #define REG_PIH_SIR                     (0x3)
74
75 /* Triton Core internal information (BEGIN) */
76
77 /* Last - for index max*/
78 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
79
80 /* Slave address */
81 #define TWL4030_NUM_SLAVES              0x04
82 #define TWL4030_SLAVENUM_NUM0           0x00
83 #define TWL4030_SLAVENUM_NUM1           0x01
84 #define TWL4030_SLAVENUM_NUM2           0x02
85 #define TWL4030_SLAVENUM_NUM3           0x03
86 #define TWL4030_SLAVEID_ID0             0x48
87 #define TWL4030_SLAVEID_ID1             0x49
88 #define TWL4030_SLAVEID_ID2             0x4A
89 #define TWL4030_SLAVEID_ID3             0x4B
90
91 /* Base Address defns */
92 /* USB ID */
93 #define TWL4030_BASEADD_USB             0x0000
94 /* AUD ID */
95 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
96 #define TWL4030_BASEADD_GPIO            0x0098
97
98 #define TWL4030_BASEADD_INTBR           0x0085
99 #define TWL4030_BASEADD_PIH             0x0080
100 #define TWL4030_BASEADD_TEST            0x004C
101 /* AUX ID */
102 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
103 #define TWL4030_BASEADD_LED             0x00EE
104 #define TWL4030_BASEADD_MADC            0x0000
105 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
106 #define TWL4030_BASEADD_PRECHARGE       0x00AA
107 #define TWL4030_BASEADD_PWM0            0x00F8
108 #define TWL4030_BASEADD_PWM1            0x00FB
109 #define TWL4030_BASEADD_PWMA            0x00EF
110 #define TWL4030_BASEADD_PWMB            0x00F1
111 #define TWL4030_BASEADD_KEYPAD          0x00D2
112 /* POWER ID */
113 #define TWL4030_BASEADD_BACKUP          0x0014
114 #define TWL4030_BASEADD_INT             0x002E
115 #define TWL4030_BASEADD_PM_MASTER       0x0036
116 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
117 #define TWL4030_BASEADD_RTC             0x001C
118 #define TWL4030_BASEADD_SECURED_REG     0x0000
119
120 /* TWL4030 BCI registers */
121 #define TWL4030_INTERRUPTS_BCIIMR1A     0x2
122 #define TWL4030_INTERRUPTS_BCIIMR2A     0x3
123 #define TWL4030_INTERRUPTS_BCIIMR1B     0x6
124 #define TWL4030_INTERRUPTS_BCIIMR2B     0x7
125 #define TWL4030_INTERRUPTS_BCIISR1A     0x0
126 #define TWL4030_INTERRUPTS_BCIISR2A     0x1
127 #define TWL4030_INTERRUPTS_BCIISR1B     0x4
128 #define TWL4030_INTERRUPTS_BCIISR2B     0x5
129
130 /* TWL4030 keypad registers */
131 #define TWL4030_KEYPAD_KEYP_IMR1        0x12
132 #define TWL4030_KEYPAD_KEYP_IMR2        0x14
133 #define TWL4030_KEYPAD_KEYP_ISR1        0x11
134 #define TWL4030_KEYPAD_KEYP_ISR2        0x13
135
136
137 /* Triton Core internal information (END) */
138
139 /* Few power values */
140 #define R_CFG_BOOT                      0x05
141 #define R_PROTECT_KEY                   0x0E
142
143 /* access control */
144 #define KEY_UNLOCK1                     0xce
145 #define KEY_UNLOCK2                     0xec
146 #define KEY_LOCK                        0x00
147
148 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
149 #define HFCLK_FREQ_26_MHZ               (2 << 0)
150 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
151 #define HIGH_PERF_SQ                    (1 << 3)
152
153 /* on I2C-1 for 2430SDP */
154 #define CONFIG_I2C_TWL4030_ID           1
155
156 /* SIH_CTRL registers that aren't defined elsewhere */
157 #define TWL4030_INTERRUPTS_BCISIHCTRL   0x0d
158 #define TWL4030_MADC_MADC_SIH_CTRL      0x67
159 #define TWL4030_KEYPAD_KEYP_SIH_CTRL    0x17
160
161 #define TWL4030_SIH_CTRL_COR_MASK       (1 << 2)
162
163 /**
164  * struct twl4030_mod_iregs - TWL module IMR/ISR regs to mask/clear at init
165  * @mod_no: TWL4030 module number (e.g., TWL4030_MODULE_GPIO)
166  * @sih_ctrl: address of module SIH_CTRL register
167  * @reg_cnt: number of IMR/ISR regs
168  * @imrs: pointer to array of TWL module interrupt mask register indices
169  * @isrs: pointer to array of TWL module interrupt status register indices
170  *
171  * Ties together TWL4030 modules and lists of IMR/ISR registers to mask/clear
172  * during twl_init_irq().
173  */
174 struct twl4030_mod_iregs {
175         const u8 mod_no;
176         const u8 sih_ctrl;
177         const u8 reg_cnt;
178         const u8 *imrs;
179         const u8 *isrs;
180 };
181
182 /* TWL4030 INT module interrupt mask registers */
183 static const u8 __initconst twl4030_int_imr_regs[] = {
184         TWL4030_INT_PWR_IMR1,
185         TWL4030_INT_PWR_IMR2,
186 };
187
188 /* TWL4030 INT module interrupt status registers */
189 static const u8 __initconst twl4030_int_isr_regs[] = {
190         TWL4030_INT_PWR_ISR1,
191         TWL4030_INT_PWR_ISR2,
192 };
193
194 /* TWL4030 INTERRUPTS module interrupt mask registers */
195 static const u8 __initconst twl4030_interrupts_imr_regs[] = {
196         TWL4030_INTERRUPTS_BCIIMR1A,
197         TWL4030_INTERRUPTS_BCIIMR1B,
198         TWL4030_INTERRUPTS_BCIIMR2A,
199         TWL4030_INTERRUPTS_BCIIMR2B,
200 };
201
202 /* TWL4030 INTERRUPTS module interrupt status registers */
203 static const u8 __initconst twl4030_interrupts_isr_regs[] = {
204         TWL4030_INTERRUPTS_BCIISR1A,
205         TWL4030_INTERRUPTS_BCIISR1B,
206         TWL4030_INTERRUPTS_BCIISR2A,
207         TWL4030_INTERRUPTS_BCIISR2B,
208 };
209
210 /* TWL4030 MADC module interrupt mask registers */
211 static const u8 __initconst twl4030_madc_imr_regs[] = {
212         TWL4030_MADC_IMR1,
213         TWL4030_MADC_IMR2,
214 };
215
216 /* TWL4030 MADC module interrupt status registers */
217 static const u8 __initconst twl4030_madc_isr_regs[] = {
218         TWL4030_MADC_ISR1,
219         TWL4030_MADC_ISR2,
220 };
221
222 /* TWL4030 keypad module interrupt mask registers */
223 static const u8 __initconst twl4030_keypad_imr_regs[] = {
224         TWL4030_KEYPAD_KEYP_IMR1,
225         TWL4030_KEYPAD_KEYP_IMR2,
226 };
227
228 /* TWL4030 keypad module interrupt status registers */
229 static const u8 __initconst twl4030_keypad_isr_regs[] = {
230         TWL4030_KEYPAD_KEYP_ISR1,
231         TWL4030_KEYPAD_KEYP_ISR2,
232 };
233
234 /* TWL4030 GPIO module interrupt mask registers */
235 static const u8 __initconst twl4030_gpio_imr_regs[] = {
236         REG_GPIO_IMR1A,
237         REG_GPIO_IMR1B,
238         REG_GPIO_IMR2A,
239         REG_GPIO_IMR2B,
240         REG_GPIO_IMR3A,
241         REG_GPIO_IMR3B,
242 };
243
244 /* TWL4030 GPIO module interrupt status registers */
245 static const u8 __initconst twl4030_gpio_isr_regs[] = {
246         REG_GPIO_ISR1A,
247         REG_GPIO_ISR1B,
248         REG_GPIO_ISR2A,
249         REG_GPIO_ISR2B,
250         REG_GPIO_ISR3A,
251         REG_GPIO_ISR3B,
252 };
253
254 /* TWL4030 modules that have IMR/ISR registers that must be masked/cleared */
255 static const struct twl4030_mod_iregs __initconst twl4030_mod_regs[] = {
256         {
257                 .mod_no   = TWL4030_MODULE_INT,
258                 .sih_ctrl = TWL4030_INT_PWR_SIH_CTRL,
259                 .reg_cnt  = ARRAY_SIZE(twl4030_int_imr_regs),
260                 .imrs     = twl4030_int_imr_regs,
261                 .isrs     = twl4030_int_isr_regs,
262         },
263         {
264                 .mod_no   = TWL4030_MODULE_INTERRUPTS,
265                 .sih_ctrl = TWL4030_INTERRUPTS_BCISIHCTRL,
266                 .reg_cnt  = ARRAY_SIZE(twl4030_interrupts_imr_regs),
267                 .imrs     = twl4030_interrupts_imr_regs,
268                 .isrs     = twl4030_interrupts_isr_regs,
269         },
270         {
271                 .mod_no   = TWL4030_MODULE_MADC,
272                 .sih_ctrl = TWL4030_MADC_MADC_SIH_CTRL,
273                 .reg_cnt  = ARRAY_SIZE(twl4030_madc_imr_regs),
274                 .imrs     = twl4030_madc_imr_regs,
275                 .isrs     = twl4030_madc_isr_regs,
276         },
277         {
278                 .mod_no   = TWL4030_MODULE_KEYPAD,
279                 .sih_ctrl = TWL4030_KEYPAD_KEYP_SIH_CTRL,
280                 .reg_cnt  = ARRAY_SIZE(twl4030_keypad_imr_regs),
281                 .imrs     = twl4030_keypad_imr_regs,
282                 .isrs     = twl4030_keypad_isr_regs,
283         },
284         {
285                 .mod_no   = TWL4030_MODULE_GPIO,
286                 .sih_ctrl = REG_GPIO_SIH_CTRL,
287                 .reg_cnt  = ARRAY_SIZE(twl4030_gpio_imr_regs),
288                 .imrs     = twl4030_gpio_imr_regs,
289                 .isrs     = twl4030_gpio_isr_regs,
290         },
291 };
292
293
294 /* Helper functions */
295 static int
296 twl4030_detect_client(struct i2c_adapter *adapter, unsigned char sid);
297 static int twl4030_attach_adapter(struct i2c_adapter *adapter);
298 static int twl4030_detach_client(struct i2c_client *client);
299 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc);
300
301 static void twl_init_irq(void);
302
303 /* Data Structures */
304 /* To have info on T2 IRQ substem activated or not */
305 static unsigned char twl_irq_used = FREE;
306 static struct completion irq_event;
307
308 /* Structure to define on TWL4030 Slave ID */
309 struct twl4030_client {
310         struct i2c_client client;
311         const char client_name[sizeof(TWL_CLIENT_STRING) + 1];
312         const unsigned char address;
313         const char adapter_index;
314         unsigned char inuse;
315
316         /* max numb of i2c_msg required is for read =2 */
317         struct i2c_msg xfer_msg[2];
318
319         /* To lock access to xfer_msg */
320         struct mutex xfer_lock;
321 };
322
323 /* Module Mapping */
324 struct twl4030mapping {
325         unsigned char sid;      /* Slave ID */
326         unsigned char base;     /* base address */
327 };
328
329 /* mapping the module id to slave id and base address */
330 static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
331         { TWL4030_SLAVENUM_NUM0, TWL4030_BASEADD_USB },
332         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_AUDIO_VOICE },
333         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_GPIO },
334         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_INTBR },
335         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_PIH },
336         { TWL4030_SLAVENUM_NUM1, TWL4030_BASEADD_TEST },
337         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_KEYPAD },
338         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MADC },
339         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_INTERRUPTS },
340         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_LED },
341         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_MAIN_CHARGE },
342         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PRECHARGE },
343         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM0 },
344         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWM1 },
345         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMA },
346         { TWL4030_SLAVENUM_NUM2, TWL4030_BASEADD_PWMB },
347         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_BACKUP },
348         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_INT },
349         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_MASTER },
350         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_PM_RECEIVER },
351         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_RTC },
352         { TWL4030_SLAVENUM_NUM3, TWL4030_BASEADD_SECURED_REG },
353 };
354
355 static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES] = {
356         {
357                 .address        = TWL4030_SLAVEID_ID0,
358                 .client_name    = TWL_CLIENT_STRING "0",
359                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
360         },
361         {
362                 .address        = TWL4030_SLAVEID_ID1,
363                 .client_name    = TWL_CLIENT_STRING "1",
364                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
365         },
366         {
367                 .address        = TWL4030_SLAVEID_ID2,
368                 .client_name    = TWL_CLIENT_STRING "2",
369                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
370         },
371         {
372                 .address        = TWL4030_SLAVEID_ID3,
373                 .client_name    = TWL_CLIENT_STRING "3",
374                 .adapter_index  = CONFIG_I2C_TWL4030_ID,
375         },
376 };
377
378 /* One Client Driver , 4 Clients */
379 static struct i2c_driver twl4030_driver = {
380         .driver = {
381                 .name   = DRIVER_NAME,
382                 .owner  = THIS_MODULE,
383         },
384         .attach_adapter = twl4030_attach_adapter,
385         .detach_client  = twl4030_detach_client,
386 };
387
388 /*
389  * TWL4030 doesn't have PIH mask, hence dummy function for mask
390  * and unmask.
391  */
392
393 static void twl4030_i2c_ackirq(unsigned int irq) {}
394 static void twl4030_i2c_disableint(unsigned int irq) {}
395 static void twl4030_i2c_enableint(unsigned int irq) {}
396
397 /* information for processing in the Work Item */
398 static struct irq_chip twl4030_irq_chip = {
399         .name   = "twl4030",
400         .ack    = twl4030_i2c_ackirq,
401         .mask   = twl4030_i2c_disableint,
402         .unmask = twl4030_i2c_enableint,
403 };
404
405 /* Global Functions */
406 /*
407  * @brief twl4030_i2c_write - Writes a n bit register in TWL4030
408  *
409  * @param mod_no - module number
410  * @param *value - an array of num_bytes+1 containing data to write
411  * IMPORTANT - Allocate value num_bytes+1 and valid data starts at
412  *               Offset 1.
413  * @param reg - register address (just offset will do)
414  * @param num_bytes - number of bytes to transfer
415  *
416  * @return result of operation - 0 is success
417  */
418 int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
419 {
420         int ret;
421         int sid;
422         struct twl4030_client *twl;
423         struct i2c_msg *msg;
424
425         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
426                 pr_err("Invalid module Number\n");
427                 return -EPERM;
428         }
429         sid = twl4030_map[mod_no].sid;
430         twl = &twl4030_modules[sid];
431
432         if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
433                 pr_err("I2C Client[%d] is not initialized[%d]\n",
434                        sid, __LINE__);
435                 return -EPERM;
436         }
437         mutex_lock(&twl->xfer_lock);
438         /*
439          * [MSG1]: fill the register address data
440          * fill the data Tx buffer
441          */
442         msg = &twl->xfer_msg[0];
443         msg->addr = twl->address;
444         msg->len = num_bytes + 1;
445         msg->flags = 0;
446         msg->buf = value;
447         /* over write the first byte of buffer with the register address */
448         *value = twl4030_map[mod_no].base + reg;
449         ret = i2c_transfer(twl->client.adapter, twl->xfer_msg, 1);
450         mutex_unlock(&twl->xfer_lock);
451
452         /* i2cTransfer returns num messages.translate it pls.. */
453         if (ret >= 0)
454                 ret = 0;
455         return ret;
456 }
457 EXPORT_SYMBOL(twl4030_i2c_write);
458
459 /**
460  * @brief twl4030_i2c_read - Reads a n bit register in TWL4030
461  *
462  * @param mod_no - module number
463  * @param *value - an array of num_bytes containing data to be read
464  * @param reg - register address (just offset will do)
465  * @param num_bytes - number of bytes to transfer
466  *
467  * @return result of operation - num_bytes is success else failure.
468  */
469 int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
470 {
471         int ret;
472         u8 val;
473         int sid;
474         struct twl4030_client *twl;
475         struct i2c_msg *msg;
476
477         if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
478                 pr_err("Invalid module Number\n");
479                 return -EPERM;
480         }
481         sid = twl4030_map[mod_no].sid;
482         twl = &twl4030_modules[sid];
483
484         if (unlikely(twl->inuse != TWL_CLIENT_USED)) {
485                 pr_err("I2C Client[%d] is not initialized[%d]\n", sid,
486                        __LINE__);
487                 return -EPERM;
488         }
489         mutex_lock(&twl->xfer_lock);
490         /* [MSG1] fill the register address data */
491         msg = &twl->xfer_msg[0];
492         msg->addr = twl->address;
493         msg->len = 1;
494         msg->flags = 0; /* Read the register value */
495         val = twl4030_map[mod_no].base + reg;
496         msg->buf = &val;
497         /* [MSG2] fill the data rx buffer */
498         msg = &twl->xfer_msg[1];
499         msg->addr = twl->address;
500         msg->flags = I2C_M_RD;  /* Read the register value */
501         msg->len = num_bytes;   /* only n bytes */
502         msg->buf = value;
503         ret = i2c_transfer(twl->client.adapter, twl->xfer_msg, 2);
504         mutex_unlock(&twl->xfer_lock);
505
506         /* i2cTransfer returns num messages.translate it pls.. */
507         if (ret >= 0)
508                 ret = 0;
509         return ret;
510 }
511 EXPORT_SYMBOL(twl4030_i2c_read);
512
513 /**
514  * @brief twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
515  *
516  * @param mod_no - module number
517  * @param value - the value to be written 8 bit
518  * @param reg - register address (just offset will do)
519  *
520  * @return 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  * @brief twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
535  *
536  * @param mod_no - module number
537  * @param *value - the value read 8 bit
538  * @param reg - register address (just offset will do)
539  *
540  * @return result of operation - 0 is success
541  */
542 int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
543 {
544         return twl4030_i2c_read(mod_no, value, reg, 1);
545 }
546 EXPORT_SYMBOL(twl4030_i2c_read_u8);
547
548 /* Helper Functions */
549
550 /*
551  * do_twl4030_module_irq() is the desc->handle method for each of the twl4030
552  * module interrupts.  It executes in kernel thread context.
553  * On entry, cpu interrupts are disabled.
554  */
555 static void do_twl4030_module_irq(unsigned int irq, irq_desc_t *desc)
556 {
557         struct irqaction *action;
558         const unsigned int cpu = smp_processor_id();
559
560         /*
561          * Earlier this was desc->triggered = 1;
562          */
563         desc->status |= IRQ_LEVEL;
564
565         /*
566          * The desc->handle method would normally call the desc->chip->ack
567          * method here, but we won't bother since our ack method is NULL.
568          */
569
570         if (!desc->depth) {
571                 kstat_cpu(cpu).irqs[irq]++;
572
573                 action = desc->action;
574                 if (action) {
575                         int ret;
576                         int status = 0;
577                         int retval = 0;
578
579                         local_irq_enable();
580
581                         do {
582                                 /* Call the ISR with cpu interrupts enabled */
583                                 ret = action->handler(irq, action->dev_id);
584                                 if (ret == IRQ_HANDLED)
585                                         status |= action->flags;
586                                 retval |= ret;
587                                 action = action->next;
588                         } while (action);
589
590                         if (status & IRQF_SAMPLE_RANDOM)
591                                 add_interrupt_randomness(irq);
592
593                         local_irq_disable();
594
595                         if (retval != IRQ_HANDLED)
596                                 printk(KERN_ERR "ISR for TWL4030 module"
597                                         " irq %d can't handle interrupt\n",
598                                         irq);
599
600                         /*
601                          * Here is where we should call the unmask method, but
602                          * again we won't bother since it is NULL.
603                          */
604                 } else
605                         printk(KERN_CRIT "TWL4030 module irq %d has no ISR"
606                                         " but can't be masked!\n", irq);
607         } else
608                 printk(KERN_CRIT "TWL4030 module irq %d is disabled but can't"
609                                 " be masked!\n", irq);
610 }
611
612 /*
613  * twl4030_irq_thread() runs as a kernel thread.  It queries the twl4030
614  * interrupt controller to see which modules are generating interrupt requests
615  * and then calls the desc->handle method for each module requesting service.
616  */
617 static int twl4030_irq_thread(void *data)
618 {
619         int irq = (int)data;
620         irq_desc_t *desc = irq_desc + irq;
621         static unsigned i2c_errors;
622         const static unsigned max_i2c_errors = 100;
623
624         daemonize("twl4030-irq");
625         current->flags |= PF_NOFREEZE;
626
627         while (!kthread_should_stop()) {
628                 int ret;
629                 int module_irq;
630                 u8 pih_isr;
631
632                 wait_for_completion_interruptible(&irq_event);
633
634                 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
635                                           REG_PIH_ISR_P1);
636                 if (ret) {
637                         printk(KERN_WARNING "I2C error %d while reading TWL4030"
638                                         " PIH ISR register.\n", ret);
639                         if (++i2c_errors >= max_i2c_errors) {
640                                 printk(KERN_ERR "Maximum I2C error count"
641                                                 " exceeded.  Terminating %s.\n",
642                                                 __func__);
643                                 break;
644                         }
645                         continue;
646                 }
647
648                 for (module_irq = TWL4030_IRQ_BASE; 0 != pih_isr;
649                          pih_isr >>= 1, module_irq++) {
650                         if (pih_isr & 0x1) {
651                                 irq_desc_t *d = irq_desc + module_irq;
652
653                                 local_irq_disable();
654
655                                 d->handle_irq(module_irq, d);
656
657                                 local_irq_enable();
658                         }
659                 }
660
661                 desc->chip->unmask(irq);
662         }
663
664         return 0;
665 }
666
667 /*
668  * do_twl4030_irq() is the desc->handle method for the twl4030 interrupt.
669  * This is a chained interrupt, so there is no desc->action method for it.
670  * Now we need to query the interrupt controller in the twl4030 to determine
671  * which module is generating the interrupt request.  However, we can't do i2c
672  * transactions in interrupt context, so we must defer that work to a kernel
673  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
674  * the kernel thread.
675  */
676 static void do_twl4030_irq(unsigned int irq, irq_desc_t *desc)
677 {
678         const unsigned int cpu = smp_processor_id();
679
680         /*
681          * Earlier this was desc->triggered = 1;
682          */
683         desc->status |= IRQ_LEVEL;
684
685         /*
686          * Acknowledge, clear _AND_ disable the interrupt.
687          */
688         desc->chip->ack(irq);
689
690         if (!desc->depth) {
691                 kstat_cpu(cpu).irqs[irq]++;
692
693                 complete(&irq_event);
694         }
695 }
696
697 /* attach a client to the adapter */
698 static int twl4030_detect_client(struct i2c_adapter *adapter, 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 /* adapter callback */
747 static int twl4030_attach_adapter(struct i2c_adapter *adapter)
748 {
749         int i;
750         int ret = 0;
751         static int twl_i2c_adapter = 1;
752
753         for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
754                 /* Check if I need to hook on to this adapter or not */
755                 if (twl4030_modules[i].adapter_index == twl_i2c_adapter) {
756                         ret = twl4030_detect_client(adapter, i);
757                         if (ret)
758                                 goto free_client;
759                 }
760         }
761         twl_i2c_adapter++;
762
763         /*
764          * Check if the PIH module is initialized, if yes, then init
765          * the T2 Interrupt subsystem
766          */
767         if ((twl4030_modules[twl4030_map[TWL4030_MODULE_PIH].sid].inuse ==
768                 TWL_CLIENT_USED) && (twl_irq_used != USED)) {
769                 twl_init_irq();
770                 twl_irq_used = USED;
771         }
772         return 0;
773
774 free_client:
775         pr_err("TWL_CLIENT(Idx=%d] registration failed[0x%x]\n", i, ret);
776
777         /* ignore current slave..it never got registered */
778         i--;
779         while (i >= 0) {
780                 /* now remove all those from the current adapter... */
781                 if (twl4030_modules[i].adapter_index == twl_i2c_adapter)
782                         (void)twl4030_detach_client(&twl4030_modules[i].client);
783                 i--;
784         }
785         return ret;
786 }
787
788 /* adapter's callback */
789 static int twl4030_detach_client(struct i2c_client *client)
790 {
791         int err;
792         err = i2c_detach_client(client);
793         if (err) {
794                 pr_err("Client detach failed\n");
795                 return err;
796         }
797         return 0;
798 }
799
800 static struct task_struct *start_twl4030_irq_thread(int irq)
801 {
802         struct task_struct *thread;
803
804         init_completion(&irq_event);
805         thread = kthread_run(twl4030_irq_thread, (void *)irq,
806                              "twl4030 irq %d", irq);
807         if (!thread)
808                 pr_err("%s: could not create twl4030 irq %d thread!\n",
809                        __func__, irq);
810
811         return thread;
812 }
813
814 /*
815  * These three functions should be part of Voltage frame work
816  * added here to complete the functionality for now.
817  */
818 static int protect_pm_master(void)
819 {
820         int e = 0;
821
822         e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
823                         R_PROTECT_KEY);
824         return e;
825 }
826
827 static int unprotect_pm_master(void)
828 {
829         int e = 0;
830
831         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
832                         R_PROTECT_KEY);
833         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
834                         R_PROTECT_KEY);
835         return e;
836 }
837
838 static int power_companion_init(void)
839 {
840         struct clk *osc;
841         u32 rate;
842         u8 ctrl = HFCLK_FREQ_26_MHZ;
843         int e = 0;
844
845         if (cpu_is_omap2430())
846                 osc = clk_get(NULL, "osc_ck");
847         else
848                 osc = clk_get(NULL, "osc_sys_ck");
849         if (IS_ERR(osc)) {
850                 printk(KERN_WARNING "Skipping twl3040 internal clock init and "
851                                 "using bootloader value (unknown osc rate)\n");
852                 return 0;
853         }
854
855         rate = clk_get_rate(osc);
856         clk_put(osc);
857
858         switch (rate) {
859         case 19200000 : ctrl = HFCLK_FREQ_19p2_MHZ; break;
860         case 26000000 : ctrl = HFCLK_FREQ_26_MHZ; break;
861         case 38400000 : ctrl = HFCLK_FREQ_38p4_MHZ; break;
862         }
863
864         ctrl |= HIGH_PERF_SQ;
865         e |= unprotect_pm_master();
866         /* effect->MADC+USB ck en */
867         e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
868         e |= protect_pm_master();
869
870         return e;
871 }
872
873 /**
874  * twl4030_i2c_clear_isr - clear TWL4030 SIH ISR regs via read + write
875  * @mod_no: TWL4030 module number
876  * @reg: register index to clear
877  * @cor: value of the <module>_SIH_CTRL.COR bit (1 or 0)
878  *
879  * Either reads (cor == 1) or writes (cor == 0) to a TWL4030 interrupt
880  * status register to ensure that any prior interrupts are cleared.
881  * Returns the status from the I2C read operation.
882  */
883 static int twl4030_i2c_clear_isr(u8 mod_no, u8 reg, u8 cor)
884 {
885         u8 tmp;
886
887         return (cor) ? twl4030_i2c_read_u8(mod_no, &tmp, reg) :
888                 twl4030_i2c_write_u8(mod_no, 0xff, reg);
889 }
890
891 /**
892  * twl4030_read_cor_bit - are TWL module ISRs cleared by reads or writes?
893  * @mod_no: TWL4030 module number
894  * @reg: register index to clear
895  *
896  * Returns 1 if the TWL4030 SIH interrupt status registers (ISRs) for
897  * the specified TWL module are cleared by reads, or 0 if cleared by
898  * writes.
899  */
900 static int twl4030_read_cor_bit(u8 mod_no, u8 reg)
901 {
902         u8 tmp = 0;
903
904         WARN_ON(twl4030_i2c_read_u8(mod_no, &tmp, reg) < 0);
905
906         tmp &= TWL4030_SIH_CTRL_COR_MASK;
907         tmp >>= __ffs(TWL4030_SIH_CTRL_COR_MASK);
908
909         return tmp;
910 }
911
912 /**
913  * twl4030_mask_clear_intrs - mask and clear all TWL4030 interrupts
914  * @t: pointer to twl4030_mod_iregs array
915  * @t_sz: ARRAY_SIZE(t) (starting at 1)
916  *
917  * Mask all TWL4030 interrupt mask registers (IMRs) and clear all
918  * interrupt status registers (ISRs).  No return value, but will WARN if
919  * any I2C operations fail.
920  */
921 static void __init twl4030_mask_clear_intrs(const struct twl4030_mod_iregs *t,
922                                             const u8 t_sz)
923 {
924         int i, j;
925
926         /*
927          * N.B. - further efficiency is possible here.  Eight I2C
928          * operations on BCI and GPIO modules are avoidable if I2C
929          * burst read/write transactions were implemented.  Would
930          * probably save about 1ms of boot time and a small amount of
931          * power.
932          */
933         for (i = 0; i < t_sz; i++) {
934                 const struct twl4030_mod_iregs tmr = t[i];
935                 int cor;
936
937                 /* Are ISRs cleared by reads or writes? */
938                 cor = twl4030_read_cor_bit(tmr.mod_no, tmr.sih_ctrl);
939                 WARN_ON(cor < 0);
940
941                 for (j = 0; j < tmr.reg_cnt; j++) {
942
943                         /* Mask interrupts at the TWL4030 */
944                         WARN_ON(twl4030_i2c_write_u8(tmr.mod_no, 0xff,
945                                                      tmr.imrs[j]) < 0);
946
947                         /* Clear TWL4030 ISRs */
948                         WARN_ON(twl4030_i2c_clear_isr(tmr.mod_no,
949                                                       tmr.isrs[j], cor) < 0);
950                 }
951         }
952
953         return;
954 }
955
956
957 static void twl_init_irq(void)
958 {
959         int     i;
960         int     res = 0;
961         char    *msg = "Unable to register interrupt subsystem";
962         unsigned int irq_num;
963
964         /*
965          * Mask and clear all TWL4030 interrupts since initially we do
966          * not have any TWL4030 module interrupt handlers present
967          */
968         twl4030_mask_clear_intrs(twl4030_mod_regs,
969                                  ARRAY_SIZE(twl4030_mod_regs));
970
971         /* install an irq handler for each of the PIH modules */
972         for (i = TWL4030_IRQ_BASE; i < TWL4030_IRQ_END; i++) {
973                 set_irq_chip(i, &twl4030_irq_chip);
974                 set_irq_handler(i, do_twl4030_module_irq);
975                 set_irq_flags(i, IRQF_VALID);
976         }
977
978         irq_num = (cpu_is_omap2430()) ? INT_24XX_SYS_NIRQ : INT_34XX_SYS_NIRQ;
979
980         /* install an irq handler to demultiplex the TWL4030 interrupt */
981         set_irq_data(irq_num, start_twl4030_irq_thread(irq_num));
982         set_irq_type(irq_num, IRQT_FALLING);
983         set_irq_chained_handler(irq_num, do_twl4030_irq);
984
985         res = power_companion_init();
986         if (res < 0)
987                 pr_err("%s[%d][%d]\n", msg, res, __LINE__);
988 }
989
990 static int __init twl4030_init(void)
991 {
992         return i2c_add_driver(&twl4030_driver);
993 }
994
995 static void __exit twl4030_exit(void)
996 {
997         i2c_del_driver(&twl4030_driver);
998         twl_irq_used = FREE;
999 }
1000
1001 subsys_initcall(twl4030_init);
1002 module_exit(twl4030_exit);
1003
1004 MODULE_ALIAS("i2c:" DRIVER_NAME);
1005 MODULE_AUTHOR("Texas Instruments, Inc.");
1006 MODULE_DESCRIPTION("I2C Core interface for TWL4030");
1007 MODULE_LICENSE("GPL");