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