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