]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/menelaus.c
Merge branch 'master' of /home/tmlind/src/kernel/linux-2.6/
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / menelaus.c
1 /*
2  * drivers/i2c/chips/menelaus.c
3  *
4  * Copyright (C) 2004 Texas Instruments, Inc.
5  *
6  * Some parts based tps65010.c:
7  * Copyright (C) 2004 Texas Instruments and
8  * Copyright (C) 2004-2005 David Brownell
9  *
10  * Some parts based on tlv320aic24.c:
11  * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
12  *
13  * Changes for interrupt handling and clean-up by
14  * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
15  * Cleanup and generalized support for voltage setting by
16  * Juha Yrjola
17  * Copyright (C) 2005-2006 Nokia Corporation
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32  */
33
34 #include <linux/module.h>
35 #include <linux/i2c.h>
36 #include <linux/interrupt.h>
37 #include <linux/sched.h>
38 #include <linux/mutex.h>
39 #include <linux/workqueue.h>
40
41 #include <asm/mach-types.h>
42 #include <asm/mach/irq.h>
43
44 #include <asm/arch/mux.h>
45 #include <asm/arch/gpio.h>
46 #include <asm/arch/menelaus.h>
47
48 #define DEBUG
49
50 #define DRIVER_NAME                     "menelaus"
51
52 #define pr_err(fmt, arg...)     printk(KERN_ERR DRIVER_NAME ": ", ## arg);
53
54 #define MENELAUS_I2C_ADDRESS            0x72
55
56 #define MENELAUS_REV                    0x01
57 #define MENELAUS_VCORE_CTRL1            0x02
58 #define MENELAUS_VCORE_CTRL2            0x03
59 #define MENELAUS_VCORE_CTRL3            0x04
60 #define MENELAUS_VCORE_CTRL4            0x05
61 #define MENELAUS_VCORE_CTRL5            0x06
62 #define MENELAUS_DCDC_CTRL1             0x07
63 #define MENELAUS_DCDC_CTRL2             0x08
64 #define MENELAUS_DCDC_CTRL3             0x09
65 #define MENELAUS_LDO_CTRL1              0x0A
66 #define MENELAUS_LDO_CTRL2              0x0B
67 #define MENELAUS_LDO_CTRL3              0x0C
68 #define MENELAUS_LDO_CTRL4              0x0D
69 #define MENELAUS_LDO_CTRL5              0x0E
70 #define MENELAUS_LDO_CTRL6              0x0F
71 #define MENELAUS_LDO_CTRL7              0x10
72 #define MENELAUS_LDO_CTRL8              0x11
73 #define MENELAUS_SLEEP_CTRL1            0x12
74 #define MENELAUS_SLEEP_CTRL2            0x13
75 #define MENELAUS_DEVICE_OFF             0x14
76 #define MENELAUS_OSC_CTRL               0x15
77 #define MENELAUS_DETECT_CTRL            0x16
78 #define MENELAUS_INT_MASK1              0x17
79 #define MENELAUS_INT_MASK2              0x18
80 #define MENELAUS_INT_STATUS1            0x19
81 #define MENELAUS_INT_STATUS2            0x1A
82 #define MENELAUS_INT_ACK1               0x1B
83 #define MENELAUS_INT_ACK2               0x1C
84 #define MENELAUS_GPIO_CTRL              0x1D
85 #define MENELAUS_GPIO_IN                0x1E
86 #define MENELAUS_GPIO_OUT               0x1F
87 #define MENELAUS_BBSMS                  0x20
88 #define MENELAUS_RTC_CTRL               0x21
89 #define MENELAUS_RTC_UPDATE             0x22
90 #define MENELAUS_RTC_SEC                0x23
91 #define MENELAUS_RTC_MIN                0x24
92 #define MENELAUS_RTC_HR                 0x25
93 #define MENELAUS_RTC_DAY                0x26
94 #define MENELAUS_RTC_MON                0x27
95 #define MENELAUS_RTC_YR                 0x28
96 #define MENELAUS_RTC_WKDAY              0x29
97 #define MENELAUS_RTC_AL_SEC             0x2A
98 #define MENELAUS_RTC_AL_MIN             0x2B
99 #define MENELAUS_RTC_AL_HR              0x2C
100 #define MENELAUS_RTC_AL_DAY             0x2D
101 #define MENELAUS_RTC_AL_MON             0x2E
102 #define MENELAUS_RTC_AL_YR              0x2F
103 #define MENELAUS_RTC_COMP_MSB           0x30
104 #define MENELAUS_RTC_COMP_LSB           0x31
105 #define MENELAUS_S1_PULL_EN             0x32
106 #define MENELAUS_S1_PULL_DIR            0x33
107 #define MENELAUS_S2_PULL_EN             0x34
108 #define MENELAUS_S2_PULL_DIR            0x35
109 #define MENELAUS_MCT_CTRL1              0x36
110 #define MENELAUS_MCT_CTRL2              0x37
111 #define MENELAUS_MCT_CTRL3              0x38
112 #define MENELAUS_MCT_PIN_ST             0x39
113 #define MENELAUS_DEBOUNCE1              0x3A
114
115 #define IH_MENELAUS_IRQS                12
116 #define MENELAUS_MMC_S1CD_IRQ           0       /* MMC slot 1 card change */
117 #define MENELAUS_MMC_S2CD_IRQ           1       /* MMC slot 2 card change */
118 #define MENELAUS_MMC_S1D1_IRQ           2       /* MMC DAT1 low in slot 1 */
119 #define MENELAUS_MMC_S2D1_IRQ           3       /* MMC DAT1 low in slot 2 */
120 #define MENELAUS_LOWBAT_IRQ             4       /* Low battery */
121 #define MENELAUS_HOTDIE_IRQ             5       /* Hot die detect */
122 #define MENELAUS_UVLO_IRQ               6       /* UVLO detect */
123 #define MENELAUS_TSHUT_IRQ              7       /* Thermal shutdown */
124 #define MENELAUS_RTCTMR_IRQ             8       /* RTC timer */
125 #define MENELAUS_RTCALM_IRQ             9       /* RTC alarm */
126 #define MENELAUS_RTCERR_IRQ             10      /* RTC error */
127 #define MENELAUS_PSHBTN_IRQ             11      /* Push button */
128 #define MENELAUS_RESERVED12_IRQ         12      /* Reserved */
129 #define MENELAUS_RESERVED13_IRQ         13      /* Reserved */
130 #define MENELAUS_RESERVED14_IRQ         14      /* Reserved */
131 #define MENELAUS_RESERVED15_IRQ         15      /* Reserved */
132
133 static void menelaus_work(struct work_struct *_menelaus);
134
135 /* Initialized by menelaus_init */
136 static unsigned short normal_i2c[] = { MENELAUS_I2C_ADDRESS, I2C_CLIENT_END };
137
138 I2C_CLIENT_INSMOD;
139
140 struct menelaus_chip {
141         unsigned long           initialized;
142         struct mutex            lock;
143         struct i2c_client       client;
144         struct work_struct      work;
145         int                     irq;
146         void                    *handlers[16];
147         void                    (*mmc_callback)(void *data, u8 mask);
148         void                    *mmc_callback_data;
149 };
150
151 static struct menelaus_chip menelaus;
152
153 static int menelaus_write_reg(int reg, u8 value)
154 {
155         int val = i2c_smbus_write_byte_data(&menelaus.client, reg, value);
156
157         if (val < 0) {
158                 pr_err("write error");
159                 return val;
160         }
161
162         return 0;
163 }
164
165 static int menelaus_read_reg(int reg)
166 {
167         int val = i2c_smbus_read_byte_data(&menelaus.client, reg);
168
169         if (val < 0)
170                 pr_err("read error");
171
172         return val;
173 }
174
175 static int menelaus_enable_irq(int irq)
176 {
177         if (irq > 7)
178                 return menelaus_write_reg(MENELAUS_INT_MASK2,
179                                           menelaus_read_reg(MENELAUS_INT_MASK2)
180                                           & ~(1 << (irq - 8)));
181         else
182                 return menelaus_write_reg(MENELAUS_INT_MASK1,
183                                           menelaus_read_reg(MENELAUS_INT_MASK1)
184                                           & ~(1 << irq));
185 }
186
187 static int menelaus_disable_irq(int irq)
188 {
189         if (irq > 7)
190                 return menelaus_write_reg(menelaus_read_reg(MENELAUS_INT_MASK2)
191                                           | (1 << (irq - 8)),
192                                           MENELAUS_INT_MASK2);
193         else
194                 return menelaus_write_reg(MENELAUS_INT_MASK1,
195                                           menelaus_read_reg(MENELAUS_INT_MASK1)
196                                           | (1 << irq));
197 }
198
199 static int menelaus_ack_irq(int irq)
200 {
201         if (irq > 7)
202                 return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
203         else
204                 return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq); 
205 }
206
207 /* Adds a handler for an interrupt. Does not run in interrupt context */
208 static int menelaus_add_irq_work(int irq, void * handler)
209 {
210         int ret = 0;
211
212         mutex_lock(&menelaus.lock);
213         menelaus.handlers[irq] = handler;
214         ret = menelaus_enable_irq(irq);
215         mutex_unlock(&menelaus.lock);
216
217         return ret;
218 }
219
220 /* Removes handler for an interrupt */
221 static int menelaus_remove_irq_work(int irq)
222 {
223         int ret = 0;
224
225         mutex_lock(&menelaus.lock);
226         ret = menelaus_disable_irq(irq);
227         menelaus.handlers[irq] = NULL;
228         mutex_unlock(&menelaus.lock);
229
230         return ret;
231 }
232
233 /*
234  * Gets scheduled when a card detect interrupt happens. Note that in some cases
235  * this line is wired to card cover switch rather than the card detect switch
236  * in each slot. In this case the cards are not seen by menelaus.
237  * FIXME: Add handling for D1 too
238  */
239 static int menelaus_mmc_cd_work(struct menelaus_chip * menelaus_hw)
240 {
241         int reg;
242         unsigned char card_mask = 0;
243
244         reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
245         if (reg < 0)
246                 return reg;
247
248         if (!(reg & 0x1))
249                 card_mask |= (1 << 0);
250
251         if (!(reg & 0x2))
252                 card_mask |= (1 << 1);
253
254         if (menelaus_hw->mmc_callback)
255                 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
256                                           card_mask);
257
258         return 0;
259 }
260
261 /*
262  * Toggles the MMC slots between open-drain and push-pull mode.
263  */
264 int menelaus_set_mmc_opendrain(int slot, int enable)
265 {
266         int ret, val;
267
268         if (slot != 1 && slot != 2)
269                 return -EINVAL;
270         mutex_lock(&menelaus.lock);
271         ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
272         if (ret < 0) {
273                 mutex_unlock(&menelaus.lock);
274                 return ret;
275         }
276         val = ret;
277         if (slot == 1) {
278                 if (enable)
279                         val |= 1 << 2;
280                 else
281                         val &= ~(1 << 2);
282         } else {
283                 if (enable)
284                         val |= 1 << 3;
285                 else
286                         val &= ~(1 << 3);
287         }
288         ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
289         mutex_unlock(&menelaus.lock);
290
291         return ret;
292 }
293 EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
294
295 int menelaus_set_slot_sel(int enable)
296 {
297         int ret;
298
299         mutex_lock(&menelaus.lock);
300         ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
301         if (ret < 0)
302                 goto out;
303         ret |= 0x02;
304         if (enable)
305                 ret |= 1 << 5;
306         else
307                 ret &= ~(1 << 5);
308         ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
309 out:
310         mutex_unlock(&menelaus.lock);
311         return ret;
312 }
313 EXPORT_SYMBOL(menelaus_set_slot_sel);
314
315 int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
316 {
317         int ret, val;
318
319         if (slot != 1 && slot != 2)
320                 return -EINVAL;
321         if (power >= 3)
322                 return -EINVAL;
323
324         mutex_lock(&menelaus.lock);
325
326         ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
327         if (ret < 0)
328                 goto out;
329         val = ret;
330         if (slot == 1) {
331                 if (cd_en)
332                         val |= (1 << 4) | (1 << 6);
333                 else
334                         val &= ~((1 << 4) | (1 << 6));
335         } else {
336                 if (cd_en)
337                         val |= (1 << 5) | (1 << 7);
338                 else
339                         val &= ~((1 << 5) | (1 << 7));
340         }
341         ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
342         if (ret < 0)
343                 goto out;
344
345         ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
346         if (ret < 0)
347                 goto out;
348         val = ret;
349         if (slot == 1) {
350                 if (enable)
351                         val |= 1 << 0;
352                 else
353                         val &= ~(1 << 0);
354         } else {
355                 int b;
356
357                 if (enable)
358                         ret |= 1 << 1;
359                 else
360                         ret &= ~(1 << 1);
361                 b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
362                 b &= ~0x03;
363                 b |= power;
364                 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
365                 if (ret < 0)
366                         goto out;
367         }
368         /* Disable autonomous shutdown */
369         val &= ~(0x03 << 2);
370         ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
371 out:
372         mutex_unlock(&menelaus.lock);
373         return ret;
374 }
375 EXPORT_SYMBOL(menelaus_set_mmc_slot);
376
377 #include <linux/delay.h>
378
379 int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
380                                    void *data)
381 {
382         int ret = 0;
383
384         menelaus.mmc_callback_data = data;
385         menelaus.mmc_callback = callback;
386         ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
387                                     menelaus_mmc_cd_work);
388         if (ret < 0)
389                 return ret;
390         ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
391                                     menelaus_mmc_cd_work);
392         if (ret < 0)
393                 return ret;
394         ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
395                                     menelaus_mmc_cd_work);
396         if (ret < 0)
397                 return ret;
398         ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
399                                     menelaus_mmc_cd_work);
400
401         return ret;
402 }
403 EXPORT_SYMBOL(menelaus_register_mmc_callback);
404
405 void menelaus_unregister_mmc_callback(void)
406 {
407         menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
408         menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
409         menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
410         menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
411
412         menelaus.mmc_callback = NULL;
413         menelaus.mmc_callback_data = 0;
414 }
415 EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
416
417 struct menelaus_vtg {
418         const char *name;
419         u8 vtg_reg;
420         u8 vtg_shift;
421         u8 vtg_bits;
422         u8 mode_reg;
423 };
424
425 struct menelaus_vtg_value {
426         u16 vtg;
427         u16 val;
428 };
429
430 static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
431                                 int vtg_val, int mode)
432 {
433         int val, ret;
434
435         mutex_lock(&menelaus.lock);
436         if (vtg == 0)
437                 goto set_voltage;
438
439         ret = menelaus_read_reg(vtg->vtg_reg);
440         if (ret < 0)
441                 goto out;
442         val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
443         val |= vtg_val << vtg->vtg_shift;
444 #ifdef DEBUG
445         printk("menelaus: Setting voltage '%s' to %d mV (reg 0x%02x, val 0x%02x)\n",
446                vtg->name, mV, vtg->vtg_reg, val);
447 #endif
448         ret = menelaus_write_reg(vtg->vtg_reg, val);
449         if (ret < 0)
450                 goto out;
451 set_voltage:
452         ret = menelaus_write_reg(vtg->mode_reg, mode);
453 out:
454         mutex_unlock(&menelaus.lock);
455         if (ret == 0) {
456                 /* Wait for voltage to stabilize */
457                 msleep(1);
458         }
459         return ret;
460 }
461
462 static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
463                                   int n)
464 {
465         int i;
466
467         for (i = 0; i < n; i++, tbl++)
468                 if (tbl->vtg == vtg)
469                         return tbl->val;
470         return -EINVAL;
471 }
472
473 static const struct menelaus_vtg vmem_vtg = {
474         .name = "VMEM",
475         .vtg_reg = MENELAUS_LDO_CTRL1,
476         .vtg_shift = 0,
477         .vtg_bits = 2,
478         .mode_reg = MENELAUS_LDO_CTRL3,
479 };
480
481 static const struct menelaus_vtg_value vmem_values[] = {
482         { 1500, 0 },
483         { 1800, 1 },
484         { 1900, 2 },
485         { 2500, 3 },
486 };
487
488 int menelaus_set_vmem(unsigned int mV)
489 {
490         int val;
491
492         if (mV == 0)
493                 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
494
495         val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
496         if (val < 0)
497                 return -EINVAL;
498         return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
499 }
500 EXPORT_SYMBOL(menelaus_set_vmem);
501
502 static const struct menelaus_vtg vio_vtg = {
503         .name = "VIO",
504         .vtg_reg = MENELAUS_LDO_CTRL1,
505         .vtg_shift = 2,
506         .vtg_bits = 2,
507         .mode_reg = MENELAUS_LDO_CTRL4,
508 };
509
510 static const struct menelaus_vtg_value vio_values[] = {
511         { 1500, 0 },
512         { 1800, 1 },
513         { 2500, 2 },
514         { 2800, 3 },
515 };
516
517 int menelaus_set_vio(unsigned int mV)
518 {
519         int val;
520
521         if (mV == 0)
522                 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
523
524         val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
525         if (val < 0)
526                 return -EINVAL;
527         return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
528 }
529 EXPORT_SYMBOL(menelaus_set_vio);
530
531 static const struct menelaus_vtg_value vdcdc_values[] = {
532         { 1500, 0 },
533         { 1800, 1 },
534         { 2000, 2 },
535         { 2200, 3 },
536         { 2400, 4 },
537         { 2800, 5 },
538         { 3000, 6 },
539         { 3300, 7 },
540 };
541
542 static const struct menelaus_vtg vdcdc2_vtg = {
543         .name = "VDCDC2",
544         .vtg_reg = MENELAUS_DCDC_CTRL1,
545         .vtg_shift = 0,
546         .vtg_bits = 3,
547         .mode_reg = MENELAUS_DCDC_CTRL2,
548 };
549
550 static const struct menelaus_vtg vdcdc3_vtg = {
551         .name = "VDCDC3",
552         .vtg_reg = MENELAUS_DCDC_CTRL1,
553         .vtg_shift = 3,
554         .vtg_bits = 3,
555         .mode_reg = MENELAUS_DCDC_CTRL3,
556 };
557
558 int menelaus_set_vdcdc(int dcdc, unsigned int mV)
559 {
560         const struct menelaus_vtg *vtg;
561         int val;
562
563         if (dcdc != 2 && dcdc != 3)
564                 return -EINVAL;
565         if (dcdc == 2)
566                 vtg = &vdcdc2_vtg;
567         else
568                 vtg = &vdcdc3_vtg;
569
570         if (mV == 0)
571                 return menelaus_set_voltage(vtg, 0, 0, 0);
572
573         val = menelaus_get_vtg_value(mV, vdcdc_values, ARRAY_SIZE(vdcdc_values));
574         if (val < 0)
575                 return -EINVAL;
576         return menelaus_set_voltage(vtg, mV, val, 0x03);
577 }
578
579 static const struct menelaus_vtg_value vmmc_values[] = {
580         { 1850, 0 },
581         { 2800, 1 },
582         { 3000, 2 },
583         { 3100, 3 },
584 };
585
586 static const struct menelaus_vtg vmmc_vtg = {
587         .name = "VMMC",
588         .vtg_reg = MENELAUS_LDO_CTRL1,
589         .vtg_shift = 6,
590         .vtg_bits = 2,
591         .mode_reg = MENELAUS_LDO_CTRL7,
592 };
593
594 int menelaus_set_vmmc(unsigned int mV)
595 {
596         int val;
597
598         if (mV == 0)
599                 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
600
601         val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
602         if (val < 0)
603                 return -EINVAL;
604         return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
605 }
606 EXPORT_SYMBOL(menelaus_set_vmmc);
607
608
609 static const struct menelaus_vtg_value vaux_values[] = {
610         { 1500, 0 },
611         { 1800, 1 },
612         { 2500, 2 },
613         { 2800, 3 },
614 };
615
616 static const struct menelaus_vtg vaux_vtg = {
617         .name = "VAUX",
618         .vtg_reg = MENELAUS_LDO_CTRL1,
619         .vtg_shift = 4,
620         .vtg_bits = 2,
621         .mode_reg = MENELAUS_LDO_CTRL6,
622 };
623
624 int menelaus_set_vaux(unsigned int mV)
625 {
626         int val;
627
628         if (mV == 0)
629                 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
630
631         val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
632         if (val < 0)
633                 return -EINVAL;
634         return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
635 }
636 EXPORT_SYMBOL(menelaus_set_vaux);
637
638 int menelaus_get_slot_pin_states(void)
639 {
640         return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
641 }
642 EXPORT_SYMBOL(menelaus_get_slot_pin_states);
643
644 /*-----------------------------------------------------------------------*/
645
646 /* Handles Menelaus interrupts. Does not run in interrupt context */
647 static void menelaus_work(struct work_struct *_menelaus)
648 {
649         struct menelaus_chip *menelaus =
650                         container_of(_menelaus, struct menelaus_chip, work);
651         int (*handler)(struct menelaus_chip *menelaus);
652
653         while (1) {
654                 int i;
655                 unsigned char isr;
656
657                 isr = menelaus_read_reg(MENELAUS_INT_STATUS1) |
658                       (menelaus_read_reg(MENELAUS_INT_STATUS2) << 8);
659
660                 if (!isr)
661                         break;
662
663                 for (i = 0; i < IH_MENELAUS_IRQS; i++) {
664                         if (isr & (1 << i)) {
665                                 mutex_lock(&menelaus->lock);
666                                 menelaus_disable_irq(i);
667                                 menelaus_ack_irq(i);
668                                 if (menelaus->handlers[i]) {
669                                         handler = menelaus->handlers[i];
670                                         handler(menelaus);
671                                 }
672                                 menelaus_enable_irq(i);
673                                 mutex_unlock(&menelaus->lock);
674                         }
675                 }
676         }
677         enable_irq(menelaus->irq);
678 }
679
680 /*
681  * We cannot use I2C in interrupt context, so we just schedule work.
682  */
683 static irqreturn_t menelaus_irq(int irq, void *_menelaus)
684 {
685         struct menelaus_chip *menelaus = _menelaus;
686
687         disable_irq_nosync(irq);
688         (void)schedule_work(&menelaus->work);
689
690         return IRQ_HANDLED;
691 }
692
693 static struct i2c_driver menelaus_i2c_driver;
694
695 static int menelaus_probe(struct i2c_adapter *adapter, int address, int kind)
696 {
697         struct i2c_client       *c;
698         int                     rev = 0;
699         int                     err = 0;
700
701         if (test_and_set_bit(0, &menelaus.initialized))
702                 return -EBUSY;
703
704         c = &menelaus.client;
705         strncpy(c->name, DRIVER_NAME, sizeof(c->name));
706         c->addr         = address;
707         c->adapter      = adapter;
708         c->driver       = &menelaus_i2c_driver;
709         c->flags        = 0;
710
711         if ((err = i2c_attach_client(c)) < 0) {
712                 pr_err("couldn't attach\n");
713                 goto fail1;
714         }
715
716         /* If a true probe check the device */
717         if (kind < 0 && (rev = menelaus_read_reg(MENELAUS_REV)) < 0) {
718                 pr_err("device not found");
719                 err = -ENODEV;
720                 goto fail2;
721         }
722
723         /* Most likely Menelaus interrupt is at SYS_NIRQ */
724         omap_cfg_reg(W19_24XX_SYS_NIRQ);
725         menelaus.irq = INT_24XX_SYS_NIRQ;
726
727         /* Ack and disable all Menelaus interrupts */
728         menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
729         menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
730         menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
731         menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
732
733         /* Set output buffer strengths */
734         menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
735
736         err = request_irq(menelaus.irq, menelaus_irq, SA_INTERRUPT,
737                           DRIVER_NAME, &menelaus);
738         if (err) {
739                 printk(KERN_ERR "Could not get Menelaus IRQ\n");
740                 goto fail2;
741         }
742
743         mutex_init(&menelaus.lock);
744         INIT_WORK(&menelaus.work, menelaus_work);
745
746         if (kind < 0)
747                 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
748
749         return 0;
750
751 fail2:
752         i2c_detach_client(c);
753 fail1:
754         clear_bit(0, &menelaus.initialized);
755         return err;
756 }
757
758 static int menelaus_remove(struct i2c_client *client)
759 {
760         int err;
761
762         free_irq(menelaus.irq, &menelaus);
763
764         if ((err = i2c_detach_client(client))) {
765                 pr_err("client deregistration failed\n");
766                 return err;
767         }
768
769         clear_bit(0, &menelaus.initialized);
770
771         return 0;
772 }
773
774 /*-----------------------------------------------------------------------*/
775
776 static int menelaus_scan_bus(struct i2c_adapter *bus)
777 {
778         if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA |
779                                           I2C_FUNC_SMBUS_WRITE_BYTE)) {
780                 pr_err("invalid i2c bus functionality\n");
781                 return -EINVAL;
782         }
783
784         return i2c_probe(bus, &addr_data, menelaus_probe);
785 }
786
787 static struct i2c_driver menelaus_i2c_driver = {
788         .driver = {
789                 .name           = DRIVER_NAME,
790         },
791         .id             = I2C_DRIVERID_MISC, /*FIXME:accroding to i2c-ids.h */
792         .class          = I2C_CLASS_HWMON,
793         .attach_adapter = menelaus_scan_bus,
794         .detach_client  = menelaus_remove,
795 };
796
797 static int __init menelaus_init(void)
798 {
799         int res;
800
801         if ((res = i2c_add_driver(&menelaus_i2c_driver)) < 0) {
802                 pr_err("driver registration failed\n");
803                 return res;
804         }
805
806         return 0;
807 }
808
809 static void __exit menelaus_exit(void)
810 {
811         if (i2c_del_driver(&menelaus_i2c_driver) < 0)
812                 pr_err("driver remove failed\n");
813
814         /* FIXME: Shutdown menelaus parts that can be shut down */
815 }
816
817 MODULE_AUTHOR("Texas Instruments, Inc.");
818 MODULE_DESCRIPTION("I2C interface for Menelaus.");
819 MODULE_LICENSE("GPL");
820
821 module_init(menelaus_init);
822 module_exit(menelaus_exit);