Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / drivers / i2c / chips / menelaus.c
1
2 /*
3  * Copyright (C) 2004 Texas Instruments, Inc.
4  *
5  * Some parts based tps65010.c:
6  * Copyright (C) 2004 Texas Instruments and
7  * Copyright (C) 2004-2005 David Brownell
8  *
9  * Some parts based on tlv320aic24.c:
10  * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
11  *
12  * Changes for interrupt handling and clean-up by
13  * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
14  * Cleanup and generalized support for voltage setting by
15  * Juha Yrjola
16  * Added support for controlling VCORE and regulator sleep states,
17  * Amit Kucheria <amit.kucheria@nokia.com>
18  * Copyright (C) 2005, 2006 Nokia Corporation
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  */
34
35 #include <linux/module.h>
36 #include <linux/i2c.h>
37 #include <linux/interrupt.h>
38 #include <linux/sched.h>
39 #include <linux/mutex.h>
40 #include <linux/workqueue.h>
41 #include <linux/delay.h>
42 #include <linux/rtc.h>
43 #include <linux/bcd.h>
44 #include <linux/i2c/menelaus.h>
45
46 #include <asm/mach-types.h>
47 #include <asm/mach/irq.h>
48
49 #include <mach/gpio.h>
50
51 #define DRIVER_NAME                     "menelaus"
52
53
54 static void menelaus_work(struct work_struct *_menelaus);
55
56 struct menelaus_chip {
57         struct mutex            lock;
58         struct i2c_client       *client;
59         struct work_struct      work;
60 #ifdef CONFIG_RTC_DRV_TWL92330
61         struct rtc_device       *rtc;
62         u8                      rtc_control;
63         unsigned                uie:1;
64 #endif
65         unsigned                vcore_hw_mode:1;
66         u8                      mask1, mask2;
67         void                    (*handlers[16])(struct menelaus_chip *);
68         void                    (*mmc_callback)(void *data, u8 mask);
69         void                    *mmc_callback_data;
70 };
71
72 static struct menelaus_chip *the_menelaus;
73
74 static int menelaus_write_reg(int reg, u8 value)
75 {
76         int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
77
78         if (val < 0) {
79                 dev_err(&the_menelaus->client->dev, "write error");
80                 return val;
81         }
82
83         return 0;
84 }
85
86 static int menelaus_read_reg(int reg)
87 {
88         int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
89
90         if (val < 0)
91                 dev_err(&the_menelaus->client->dev, "read error");
92
93         return val;
94 }
95
96 static int menelaus_enable_irq(int irq)
97 {
98         if (irq > 7) {
99                 irq -= 8;
100                 the_menelaus->mask2 &= ~(1 << irq);
101                 return menelaus_write_reg(MENELAUS_INT_MASK2,
102                                 the_menelaus->mask2);
103         } else {
104                 the_menelaus->mask1 &= ~(1 << irq);
105                 return menelaus_write_reg(MENELAUS_INT_MASK1,
106                                 the_menelaus->mask1);
107         }
108 }
109
110 static int menelaus_disable_irq(int irq)
111 {
112         if (irq > 7) {
113                 irq -= 8;
114                 the_menelaus->mask2 |= (1 << irq);
115                 return menelaus_write_reg(MENELAUS_INT_MASK2,
116                                 the_menelaus->mask2);
117         } else {
118                 the_menelaus->mask1 |= (1 << irq);
119                 return menelaus_write_reg(MENELAUS_INT_MASK1,
120                                 the_menelaus->mask1);
121         }
122 }
123
124 static int menelaus_ack_irq(int irq)
125 {
126         if (irq > 7)
127                 return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
128         else
129                 return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
130 }
131
132 /* Adds a handler for an interrupt. Does not run in interrupt context */
133 static int menelaus_add_irq_work(int irq,
134                 void (*handler)(struct menelaus_chip *))
135 {
136         int ret = 0;
137
138         mutex_lock(&the_menelaus->lock);
139         the_menelaus->handlers[irq] = handler;
140         ret = menelaus_enable_irq(irq);
141         mutex_unlock(&the_menelaus->lock);
142
143         return ret;
144 }
145
146 /* Removes handler for an interrupt */
147 static int menelaus_remove_irq_work(int irq)
148 {
149         int ret = 0;
150
151         mutex_lock(&the_menelaus->lock);
152         ret = menelaus_disable_irq(irq);
153         the_menelaus->handlers[irq] = NULL;
154         mutex_unlock(&the_menelaus->lock);
155
156         return ret;
157 }
158
159 /*
160  * Gets scheduled when a card detect interrupt happens. Note that in some cases
161  * this line is wired to card cover switch rather than the card detect switch
162  * in each slot. In this case the cards are not seen by menelaus.
163  * FIXME: Add handling for D1 too
164  */
165 static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
166 {
167         int reg;
168         unsigned char card_mask = 0;
169
170         reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
171         if (reg < 0)
172                 return;
173
174         if (!(reg & 0x1))
175                 card_mask |= MCT_PIN_ST_S1_CD_ST;
176
177         if (!(reg & 0x2))
178                 card_mask |= MCT_PIN_ST_S2_CD_ST;
179
180         if (menelaus_hw->mmc_callback)
181                 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
182                                           card_mask);
183 }
184
185 /*
186  * Toggles the MMC slots between open-drain and push-pull mode.
187  */
188 int menelaus_set_mmc_opendrain(int slot, int enable)
189 {
190         int ret, val;
191
192         if (slot != 1 && slot != 2)
193                 return -EINVAL;
194         mutex_lock(&the_menelaus->lock);
195         ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
196         if (ret < 0) {
197                 mutex_unlock(&the_menelaus->lock);
198                 return ret;
199         }
200         val = ret;
201         if (slot == 1) {
202                 if (enable)
203                         val |= MCT_CTRL1_S1_CMD_OD;
204                 else
205                         val &= ~MCT_CTRL1_S1_CMD_OD;
206         } else {
207                 if (enable)
208                         val |= MCT_CTRL1_S2_CMD_OD;
209                 else
210                         val &= ~MCT_CTRL1_S2_CMD_OD;
211         }
212         ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
213         mutex_unlock(&the_menelaus->lock);
214
215         return ret;
216 }
217 EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
218
219 int menelaus_set_slot_sel(int enable)
220 {
221         int ret;
222
223         mutex_lock(&the_menelaus->lock);
224         ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
225         if (ret < 0)
226                 goto out;
227         ret |= GPIO2_DIR_INPUT;
228         if (enable)
229                 ret |= GPIO_CTRL_SLOTSELEN;
230         else
231                 ret &= ~GPIO_CTRL_SLOTSELEN;
232         ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
233 out:
234         mutex_unlock(&the_menelaus->lock);
235         return ret;
236 }
237 EXPORT_SYMBOL(menelaus_set_slot_sel);
238
239 int menelaus_enable_slot(int slot, int enable)
240 {
241         int ret, val;
242
243         mutex_lock(&the_menelaus->lock);
244         ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
245         if (ret < 0)
246                 goto out;
247         val = ret;
248         if (slot == 1) {
249                 if (enable)
250                         val |= MCT_CTRL3_SLOT1_EN;
251                 else
252                         val &= ~MCT_CTRL3_SLOT1_EN;
253         } else {
254                 if (enable)
255                         val |= MCT_CTRL3_SLOT2_EN;
256                 else
257                         val &= MCT_CTRL3_SLOT2_EN;
258         }
259         ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
260
261 out:
262         mutex_unlock(&the_menelaus->lock);
263         return ret;
264 }
265 EXPORT_SYMBOL(menelaus_enable_slot);
266
267 int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
268 {
269         int ret, val;
270
271         if (slot != 1 && slot != 2)
272                 return -EINVAL;
273         if (power >= 3)
274                 return -EINVAL;
275
276         mutex_lock(&the_menelaus->lock);
277
278         ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
279         if (ret < 0)
280                 goto out;
281         val = ret;
282         if (slot == 1) {
283                 if (cd_en)
284                         val |= MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN;
285                 else
286                         val &= ~(MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN);
287         } else {
288                 if (cd_en)
289                         val |= MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN;
290                 else
291                         val &= ~(MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN);
292         }
293         ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
294         if (ret < 0)
295                 goto out;
296
297         ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
298         if (ret < 0)
299                 goto out;
300         val = ret;
301         if (slot == 1) {
302                 if (enable)
303                         val |= MCT_CTRL3_SLOT1_EN;
304                 else
305                         val &= ~MCT_CTRL3_SLOT1_EN;
306         } else {
307                 int b;
308
309                 if (enable)
310                         val |= MCT_CTRL3_SLOT2_EN;
311                 else
312                         val &= ~MCT_CTRL3_SLOT2_EN;
313                 b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
314                 b &= ~(MCT_CTRL2_VS2_SEL_D0 | MCT_CTRL2_VS2_SEL_D1);
315                 b |= power;
316                 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
317                 if (ret < 0)
318                         goto out;
319         }
320         /* Disable autonomous shutdown */
321         val &= ~(MCT_CTRL3_S1_AUTO_EN | MCT_CTRL3_S2_AUTO_EN);
322         ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
323 out:
324         mutex_unlock(&the_menelaus->lock);
325         return ret;
326 }
327 EXPORT_SYMBOL(menelaus_set_mmc_slot);
328
329 int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
330                                    void *data)
331 {
332         int ret = 0;
333
334         the_menelaus->mmc_callback_data = data;
335         the_menelaus->mmc_callback = callback;
336         ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
337                                     menelaus_mmc_cd_work);
338         if (ret < 0)
339                 return ret;
340         ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
341                                     menelaus_mmc_cd_work);
342         if (ret < 0)
343                 return ret;
344         ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
345                                     menelaus_mmc_cd_work);
346         if (ret < 0)
347                 return ret;
348         ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
349                                     menelaus_mmc_cd_work);
350
351         return ret;
352 }
353 EXPORT_SYMBOL(menelaus_register_mmc_callback);
354
355 void menelaus_unregister_mmc_callback(void)
356 {
357         menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
358         menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
359         menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
360         menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
361
362         the_menelaus->mmc_callback = NULL;
363         the_menelaus->mmc_callback_data = 0;
364 }
365 EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
366
367 struct menelaus_vtg {
368         const char *name;
369         u8 vtg_reg;
370         u8 vtg_shift;
371         u8 vtg_bits;
372         u8 mode_reg;
373 };
374
375 struct menelaus_vtg_value {
376         u16 vtg;
377         u16 val;
378 };
379
380 static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
381                                 int vtg_val, int mode)
382 {
383         int val, ret;
384         struct i2c_client *c = the_menelaus->client;
385
386         mutex_lock(&the_menelaus->lock);
387         if (vtg == 0)
388                 goto set_voltage;
389
390         ret = menelaus_read_reg(vtg->vtg_reg);
391         if (ret < 0)
392                 goto out;
393         val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
394         val |= vtg_val << vtg->vtg_shift;
395
396         dev_dbg(&c->dev, "Setting voltage '%s'"
397                          "to %d mV (reg 0x%02x, val 0x%02x)\n",
398                         vtg->name, mV, vtg->vtg_reg, val);
399
400         ret = menelaus_write_reg(vtg->vtg_reg, val);
401         if (ret < 0)
402                 goto out;
403 set_voltage:
404         ret = menelaus_write_reg(vtg->mode_reg, mode);
405 out:
406         mutex_unlock(&the_menelaus->lock);
407         if (ret == 0) {
408                 /* Wait for voltage to stabilize */
409                 msleep(1);
410         }
411         return ret;
412 }
413
414 static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
415                                   int n)
416 {
417         int i;
418
419         for (i = 0; i < n; i++, tbl++)
420                 if (tbl->vtg == vtg)
421                         return tbl->val;
422         return -EINVAL;
423 }
424
425 /*
426  * Vcore can be programmed in two ways:
427  * SW-controlled: Required voltage is programmed into VCORE_CTRL1
428  * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
429  * and VCORE_CTRL4
430  *
431  * Call correct 'set' function accordingly
432  */
433
434 static const struct menelaus_vtg_value vcore_values[] = {
435         { 1000, 0 },
436         { 1025, 1 },
437         { 1050, 2 },
438         { 1075, 3 },
439         { 1100, 4 },
440         { 1125, 5 },
441         { 1150, 6 },
442         { 1175, 7 },
443         { 1200, 8 },
444         { 1225, 9 },
445         { 1250, 10 },
446         { 1275, 11 },
447         { 1300, 12 },
448         { 1325, 13 },
449         { 1350, 14 },
450         { 1375, 15 },
451         { 1400, 16 },
452         { 1425, 17 },
453         { 1450, 18 },
454 };
455
456 int menelaus_set_vcore_sw(unsigned int mV)
457 {
458         int val, ret;
459         struct i2c_client *c = the_menelaus->client;
460
461         val = menelaus_get_vtg_value(mV, vcore_values,
462                                      ARRAY_SIZE(vcore_values));
463         if (val < 0)
464                 return -EINVAL;
465
466         dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
467
468         /* Set SW mode and the voltage in one go. */
469         mutex_lock(&the_menelaus->lock);
470         ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
471         if (ret == 0)
472                 the_menelaus->vcore_hw_mode = 0;
473         mutex_unlock(&the_menelaus->lock);
474         msleep(1);
475
476         return ret;
477 }
478
479 int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
480 {
481         int fval, rval, val, ret;
482         struct i2c_client *c = the_menelaus->client;
483
484         rval = menelaus_get_vtg_value(roof_mV, vcore_values,
485                                       ARRAY_SIZE(vcore_values));
486         if (rval < 0)
487                 return -EINVAL;
488         fval = menelaus_get_vtg_value(floor_mV, vcore_values,
489                                       ARRAY_SIZE(vcore_values));
490         if (fval < 0)
491                 return -EINVAL;
492
493         dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
494                floor_mV, roof_mV);
495
496         mutex_lock(&the_menelaus->lock);
497         ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
498         if (ret < 0)
499                 goto out;
500         ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
501         if (ret < 0)
502                 goto out;
503         if (!the_menelaus->vcore_hw_mode) {
504                 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
505                 /* HW mode, turn OFF byte comparator */
506                 val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP);
507                 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
508                 the_menelaus->vcore_hw_mode = 1;
509         }
510         msleep(1);
511 out:
512         mutex_unlock(&the_menelaus->lock);
513         return ret;
514 }
515
516 static const struct menelaus_vtg vmem_vtg = {
517         .name = "VMEM",
518         .vtg_reg = MENELAUS_LDO_CTRL1,
519         .vtg_shift = 0,
520         .vtg_bits = 2,
521         .mode_reg = MENELAUS_LDO_CTRL3,
522 };
523
524 static const struct menelaus_vtg_value vmem_values[] = {
525         { 1500, 0 },
526         { 1800, 1 },
527         { 1900, 2 },
528         { 2500, 3 },
529 };
530
531 int menelaus_set_vmem(unsigned int mV)
532 {
533         int val;
534
535         if (mV == 0)
536                 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
537
538         val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
539         if (val < 0)
540                 return -EINVAL;
541         return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
542 }
543 EXPORT_SYMBOL(menelaus_set_vmem);
544
545 static const struct menelaus_vtg vio_vtg = {
546         .name = "VIO",
547         .vtg_reg = MENELAUS_LDO_CTRL1,
548         .vtg_shift = 2,
549         .vtg_bits = 2,
550         .mode_reg = MENELAUS_LDO_CTRL4,
551 };
552
553 static const struct menelaus_vtg_value vio_values[] = {
554         { 1500, 0 },
555         { 1800, 1 },
556         { 2500, 2 },
557         { 2800, 3 },
558 };
559
560 int menelaus_set_vio(unsigned int mV)
561 {
562         int val;
563
564         if (mV == 0)
565                 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
566
567         val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
568         if (val < 0)
569                 return -EINVAL;
570         return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
571 }
572 EXPORT_SYMBOL(menelaus_set_vio);
573
574 static const struct menelaus_vtg_value vdcdc_values[] = {
575         { 1500, 0 },
576         { 1800, 1 },
577         { 2000, 2 },
578         { 2200, 3 },
579         { 2400, 4 },
580         { 2800, 5 },
581         { 3000, 6 },
582         { 3300, 7 },
583 };
584
585 static const struct menelaus_vtg vdcdc2_vtg = {
586         .name = "VDCDC2",
587         .vtg_reg = MENELAUS_DCDC_CTRL1,
588         .vtg_shift = 0,
589         .vtg_bits = 3,
590         .mode_reg = MENELAUS_DCDC_CTRL2,
591 };
592
593 static const struct menelaus_vtg vdcdc3_vtg = {
594         .name = "VDCDC3",
595         .vtg_reg = MENELAUS_DCDC_CTRL1,
596         .vtg_shift = 3,
597         .vtg_bits = 3,
598         .mode_reg = MENELAUS_DCDC_CTRL3,
599 };
600
601 int menelaus_set_vdcdc(int dcdc, unsigned int mV)
602 {
603         const struct menelaus_vtg *vtg;
604         int val;
605
606         if (dcdc != 2 && dcdc != 3)
607                 return -EINVAL;
608         if (dcdc == 2)
609                 vtg = &vdcdc2_vtg;
610         else
611                 vtg = &vdcdc3_vtg;
612
613         if (mV == 0)
614                 return menelaus_set_voltage(vtg, 0, 0, 0);
615
616         val = menelaus_get_vtg_value(mV, vdcdc_values,
617                                      ARRAY_SIZE(vdcdc_values));
618         if (val < 0)
619                 return -EINVAL;
620         return menelaus_set_voltage(vtg, mV, val, 0x03);
621 }
622
623 static const struct menelaus_vtg_value vmmc_values[] = {
624         { 1850, 0 },
625         { 2800, 1 },
626         { 3000, 2 },
627         { 3100, 3 },
628 };
629
630 static const struct menelaus_vtg vmmc_vtg = {
631         .name = "VMMC",
632         .vtg_reg = MENELAUS_LDO_CTRL1,
633         .vtg_shift = 6,
634         .vtg_bits = 2,
635         .mode_reg = MENELAUS_LDO_CTRL7,
636 };
637
638 int menelaus_set_vmmc(unsigned int mV)
639 {
640         int val;
641
642         if (mV == 0)
643                 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
644
645         val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
646         if (val < 0)
647                 return -EINVAL;
648         return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
649 }
650 EXPORT_SYMBOL(menelaus_set_vmmc);
651
652
653 static const struct menelaus_vtg_value vaux_values[] = {
654         { 1500, 0 },
655         { 1800, 1 },
656         { 2500, 2 },
657         { 2800, 3 },
658 };
659
660 static const struct menelaus_vtg vaux_vtg = {
661         .name = "VAUX",
662         .vtg_reg = MENELAUS_LDO_CTRL1,
663         .vtg_shift = 4,
664         .vtg_bits = 2,
665         .mode_reg = MENELAUS_LDO_CTRL6,
666 };
667
668 int menelaus_set_vaux(unsigned int mV)
669 {
670         int val;
671
672         if (mV == 0)
673                 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
674
675         val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
676         if (val < 0)
677                 return -EINVAL;
678         return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
679 }
680 EXPORT_SYMBOL(menelaus_set_vaux);
681
682 int menelaus_get_slot_pin_states(void)
683 {
684         return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
685 }
686 EXPORT_SYMBOL(menelaus_get_slot_pin_states);
687
688 int menelaus_set_regulator_sleep(int enable, u32 val)
689 {
690         int t, ret;
691         struct i2c_client *c = the_menelaus->client;
692
693         mutex_lock(&the_menelaus->lock);
694         ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
695         if (ret < 0)
696                 goto out;
697
698         dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
699
700         ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
701         if (ret < 0)
702                 goto out;
703         t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT);
704         if (enable)
705                 ret |= t;
706         else
707                 ret &= ~t;
708         ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
709 out:
710         mutex_unlock(&the_menelaus->lock);
711         return ret;
712 }
713
714 /*-----------------------------------------------------------------------*/
715
716 /* Handles Menelaus interrupts. Does not run in interrupt context */
717 static void menelaus_work(struct work_struct *_menelaus)
718 {
719         struct menelaus_chip *menelaus =
720                         container_of(_menelaus, struct menelaus_chip, work);
721         void (*handler)(struct menelaus_chip *menelaus);
722
723         while (1) {
724                 unsigned isr;
725
726                 isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
727                                 & ~menelaus->mask2) << 8;
728                 isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
729                                 & ~menelaus->mask1;
730                 if (!isr)
731                         break;
732
733                 while (isr) {
734                         int irq = fls(isr) - 1;
735                         isr &= ~(1 << irq);
736
737                         mutex_lock(&menelaus->lock);
738                         menelaus_disable_irq(irq);
739                         menelaus_ack_irq(irq);
740                         handler = menelaus->handlers[irq];
741                         if (handler)
742                                 handler(menelaus);
743                         menelaus_enable_irq(irq);
744                         mutex_unlock(&menelaus->lock);
745                 }
746         }
747         enable_irq(menelaus->client->irq);
748 }
749
750 /*
751  * We cannot use I2C in interrupt context, so we just schedule work.
752  */
753 static irqreturn_t menelaus_irq(int irq, void *_menelaus)
754 {
755         struct menelaus_chip *menelaus = _menelaus;
756
757         disable_irq_nosync(irq);
758         (void)schedule_work(&menelaus->work);
759
760         return IRQ_HANDLED;
761 }
762
763 /*-----------------------------------------------------------------------*/
764
765 /*
766  * The RTC needs to be set once, then it runs on backup battery power.
767  * It supports alarms, including system wake alarms (from some modes);
768  * and 1/second IRQs if requested.
769  */
770 #ifdef CONFIG_RTC_DRV_TWL92330
771
772 #define RTC_CTRL_RTC_EN         (1 << 0)
773 #define RTC_CTRL_AL_EN          (1 << 1)
774 #define RTC_CTRL_MODE12         (1 << 2)
775 #define RTC_CTRL_EVERY_MASK     (3 << 3)
776 #define RTC_CTRL_EVERY_SEC      (0 << 3)
777 #define RTC_CTRL_EVERY_MIN      (1 << 3)
778 #define RTC_CTRL_EVERY_HR       (2 << 3)
779 #define RTC_CTRL_EVERY_DAY      (3 << 3)
780
781 #define RTC_UPDATE_EVERY        0x08
782
783 #define RTC_HR_PM               (1 << 7)
784
785 static void menelaus_to_time(char *regs, struct rtc_time *t)
786 {
787         t->tm_sec = BCD2BIN(regs[0]);
788         t->tm_min = BCD2BIN(regs[1]);
789         if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
790                 t->tm_hour = BCD2BIN(regs[2] & 0x1f) - 1;
791                 if (regs[2] & RTC_HR_PM)
792                         t->tm_hour += 12;
793         } else
794                 t->tm_hour = BCD2BIN(regs[2] & 0x3f);
795         t->tm_mday = BCD2BIN(regs[3]);
796         t->tm_mon = BCD2BIN(regs[4]) - 1;
797         t->tm_year = BCD2BIN(regs[5]) + 100;
798 }
799
800 static int time_to_menelaus(struct rtc_time *t, int regnum)
801 {
802         int     hour, status;
803
804         status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_sec));
805         if (status < 0)
806                 goto fail;
807
808         status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_min));
809         if (status < 0)
810                 goto fail;
811
812         if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
813                 hour = t->tm_hour + 1;
814                 if (hour > 12)
815                         hour = RTC_HR_PM | BIN2BCD(hour - 12);
816                 else
817                         hour = BIN2BCD(hour);
818         } else
819                 hour = BIN2BCD(t->tm_hour);
820         status = menelaus_write_reg(regnum++, hour);
821         if (status < 0)
822                 goto fail;
823
824         status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mday));
825         if (status < 0)
826                 goto fail;
827
828         status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mon + 1));
829         if (status < 0)
830                 goto fail;
831
832         status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_year - 100));
833         if (status < 0)
834                 goto fail;
835
836         return 0;
837 fail:
838         dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
839                         --regnum, status);
840         return status;
841 }
842
843 static int menelaus_read_time(struct device *dev, struct rtc_time *t)
844 {
845         struct i2c_msg  msg[2];
846         char            regs[7];
847         int             status;
848
849         /* block read date and time registers */
850         regs[0] = MENELAUS_RTC_SEC;
851
852         msg[0].addr = MENELAUS_I2C_ADDRESS;
853         msg[0].flags = 0;
854         msg[0].len = 1;
855         msg[0].buf = regs;
856
857         msg[1].addr = MENELAUS_I2C_ADDRESS;
858         msg[1].flags = I2C_M_RD;
859         msg[1].len = sizeof(regs);
860         msg[1].buf = regs;
861
862         status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
863         if (status != 2) {
864                 dev_err(dev, "%s error %d\n", "read", status);
865                 return -EIO;
866         }
867
868         menelaus_to_time(regs, t);
869         t->tm_wday = BCD2BIN(regs[6]);
870
871         return 0;
872 }
873
874 static int menelaus_set_time(struct device *dev, struct rtc_time *t)
875 {
876         int             status;
877
878         /* write date and time registers */
879         status = time_to_menelaus(t, MENELAUS_RTC_SEC);
880         if (status < 0)
881                 return status;
882         status = menelaus_write_reg(MENELAUS_RTC_WKDAY, BIN2BCD(t->tm_wday));
883         if (status < 0) {
884                 dev_err(&the_menelaus->client->dev, "rtc write reg %02x"
885                                 "err %d\n", MENELAUS_RTC_WKDAY, status);
886                 return status;
887         }
888
889         /* now commit the write */
890         status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
891         if (status < 0)
892                 dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
893                                 status);
894
895         return 0;
896 }
897
898 static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
899 {
900         struct i2c_msg  msg[2];
901         char            regs[6];
902         int             status;
903
904         /* block read alarm registers */
905         regs[0] = MENELAUS_RTC_AL_SEC;
906
907         msg[0].addr = MENELAUS_I2C_ADDRESS;
908         msg[0].flags = 0;
909         msg[0].len = 1;
910         msg[0].buf = regs;
911
912         msg[1].addr = MENELAUS_I2C_ADDRESS;
913         msg[1].flags = I2C_M_RD;
914         msg[1].len = sizeof(regs);
915         msg[1].buf = regs;
916
917         status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
918         if (status != 2) {
919                 dev_err(dev, "%s error %d\n", "alarm read", status);
920                 return -EIO;
921         }
922
923         menelaus_to_time(regs, &w->time);
924
925         w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
926
927         /* NOTE we *could* check if actually pending... */
928         w->pending = 0;
929
930         return 0;
931 }
932
933 static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
934 {
935         int             status;
936
937         if (the_menelaus->client->irq <= 0 && w->enabled)
938                 return -ENODEV;
939
940         /* clear previous alarm enable */
941         if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
942                 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
943                 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
944                                 the_menelaus->rtc_control);
945                 if (status < 0)
946                         return status;
947         }
948
949         /* write alarm registers */
950         status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
951         if (status < 0)
952                 return status;
953
954         /* enable alarm if requested */
955         if (w->enabled) {
956                 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
957                 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
958                                 the_menelaus->rtc_control);
959         }
960
961         return status;
962 }
963
964 #ifdef CONFIG_RTC_INTF_DEV
965
966 static void menelaus_rtc_update_work(struct menelaus_chip *m)
967 {
968         /* report 1/sec update */
969         local_irq_disable();
970         rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
971         local_irq_enable();
972 }
973
974 static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
975 {
976         int     status;
977
978         if (the_menelaus->client->irq <= 0)
979                 return -ENOIOCTLCMD;
980
981         switch (cmd) {
982         /* alarm IRQ */
983         case RTC_AIE_ON:
984                 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
985                         return 0;
986                 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
987                 break;
988         case RTC_AIE_OFF:
989                 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
990                         return 0;
991                 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
992                 break;
993         /* 1/second "update" IRQ */
994         case RTC_UIE_ON:
995                 if (the_menelaus->uie)
996                         return 0;
997                 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
998                 status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
999                                 menelaus_rtc_update_work);
1000                 if (status == 0)
1001                         the_menelaus->uie = 1;
1002                 return status;
1003         case RTC_UIE_OFF:
1004                 if (!the_menelaus->uie)
1005                         return 0;
1006                 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1007                 if (status == 0)
1008                         the_menelaus->uie = 0;
1009                 return status;
1010         default:
1011                 return -ENOIOCTLCMD;
1012         }
1013         return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1014 }
1015
1016 #else
1017 #define menelaus_ioctl  NULL
1018 #endif
1019
1020 /* REVISIT no compensation register support ... */
1021
1022 static const struct rtc_class_ops menelaus_rtc_ops = {
1023         .ioctl                  = menelaus_ioctl,
1024         .read_time              = menelaus_read_time,
1025         .set_time               = menelaus_set_time,
1026         .read_alarm             = menelaus_read_alarm,
1027         .set_alarm              = menelaus_set_alarm,
1028 };
1029
1030 static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1031 {
1032         /* report alarm */
1033         local_irq_disable();
1034         rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
1035         local_irq_enable();
1036
1037         /* then disable it; alarms are oneshot */
1038         the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1039         menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1040 }
1041
1042 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1043 {
1044         int     alarm = (m->client->irq > 0);
1045
1046         /* assume 32KDETEN pin is pulled high */
1047         if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1048                 dev_dbg(&m->client->dev, "no 32k oscillator\n");
1049                 return;
1050         }
1051
1052         /* support RTC alarm; it can issue wakeups */
1053         if (alarm) {
1054                 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1055                                 menelaus_rtc_alarm_work) < 0) {
1056                         dev_err(&m->client->dev, "can't handle RTC alarm\n");
1057                         return;
1058                 }
1059                 device_init_wakeup(&m->client->dev, 1);
1060         }
1061
1062         /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1063         m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1064         if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1065                         || (m->rtc_control & RTC_CTRL_AL_EN)
1066                         || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1067                 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1068                         dev_warn(&m->client->dev, "rtc clock needs setting\n");
1069                         m->rtc_control |= RTC_CTRL_RTC_EN;
1070                 }
1071                 m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1072                 m->rtc_control &= ~RTC_CTRL_AL_EN;
1073                 menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1074         }
1075
1076         m->rtc = rtc_device_register(DRIVER_NAME,
1077                         &m->client->dev,
1078                         &menelaus_rtc_ops, THIS_MODULE);
1079         if (IS_ERR(m->rtc)) {
1080                 if (alarm) {
1081                         menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1082                         device_init_wakeup(&m->client->dev, 0);
1083                 }
1084                 dev_err(&m->client->dev, "can't register RTC: %d\n",
1085                                 (int) PTR_ERR(m->rtc));
1086                 the_menelaus->rtc = NULL;
1087         }
1088 }
1089
1090 #else
1091
1092 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1093 {
1094         /* nothing */
1095 }
1096
1097 #endif
1098
1099 /*-----------------------------------------------------------------------*/
1100
1101 static struct i2c_driver menelaus_i2c_driver;
1102
1103 static int menelaus_probe(struct i2c_client *client,
1104                           const struct i2c_device_id *id)
1105 {
1106         struct menelaus_chip    *menelaus;
1107         int                     rev = 0, val;
1108         int                     err = 0;
1109         struct menelaus_platform_data *menelaus_pdata =
1110                                         client->dev.platform_data;
1111
1112         if (the_menelaus) {
1113                 dev_dbg(&client->dev, "only one %s for now\n",
1114                                 DRIVER_NAME);
1115                 return -ENODEV;
1116         }
1117
1118         menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL);
1119         if (!menelaus)
1120                 return -ENOMEM;
1121
1122         i2c_set_clientdata(client, menelaus);
1123
1124         the_menelaus = menelaus;
1125         menelaus->client = client;
1126
1127         /* If a true probe check the device */
1128         rev = menelaus_read_reg(MENELAUS_REV);
1129         if (rev < 0) {
1130                 dev_err(&client->dev, "device not found");
1131                 err = -ENODEV;
1132                 goto fail1;
1133         }
1134
1135         /* Ack and disable all Menelaus interrupts */
1136         menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1137         menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1138         menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1139         menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1140         menelaus->mask1 = 0xff;
1141         menelaus->mask2 = 0xff;
1142
1143         /* Set output buffer strengths */
1144         menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1145
1146         if (client->irq > 0) {
1147                 err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED,
1148                                   DRIVER_NAME, menelaus);
1149                 if (err) {
1150                         dev_dbg(&client->dev,  "can't get IRQ %d, err %d",
1151                                         client->irq, err);
1152                         goto fail1;
1153                 }
1154         }
1155
1156         mutex_init(&menelaus->lock);
1157         INIT_WORK(&menelaus->work, menelaus_work);
1158
1159         dev_info(&client->dev, "Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1160
1161         val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1162         if (val < 0)
1163                 goto fail2;
1164         if (val & (1 << 7))
1165                 menelaus->vcore_hw_mode = 1;
1166         else
1167                 menelaus->vcore_hw_mode = 0;
1168
1169         if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1170                 err = menelaus_pdata->late_init(&client->dev);
1171                 if (err < 0)
1172                         goto fail2;
1173         }
1174
1175         menelaus_rtc_init(menelaus);
1176
1177         return 0;
1178 fail2:
1179         free_irq(client->irq, menelaus);
1180         flush_scheduled_work();
1181 fail1:
1182         kfree(menelaus);
1183         return err;
1184 }
1185
1186 static int __exit menelaus_remove(struct i2c_client *client)
1187 {
1188         struct menelaus_chip    *menelaus = i2c_get_clientdata(client);
1189
1190         free_irq(client->irq, menelaus);
1191         kfree(menelaus);
1192         i2c_set_clientdata(client, NULL);
1193         the_menelaus = NULL;
1194         return 0;
1195 }
1196
1197 static const struct i2c_device_id menelaus_id[] = {
1198         { "menelaus", 0 },
1199         { }
1200 };
1201 MODULE_DEVICE_TABLE(i2c, menelaus_id);
1202
1203 static struct i2c_driver menelaus_i2c_driver = {
1204         .driver = {
1205                 .name           = DRIVER_NAME,
1206         },
1207         .probe          = menelaus_probe,
1208         .remove         = __exit_p(menelaus_remove),
1209         .id_table       = menelaus_id,
1210 };
1211
1212 static int __init menelaus_init(void)
1213 {
1214         return i2c_add_driver(&menelaus_i2c_driver);
1215 }
1216
1217 static void __exit menelaus_exit(void)
1218 {
1219         i2c_del_driver(&menelaus_i2c_driver);
1220
1221         /* FIXME: Shutdown menelaus parts that can be shut down */
1222 }
1223
1224 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1225 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1226 MODULE_LICENSE("GPL");
1227
1228 module_init(menelaus_init);
1229 module_exit(menelaus_exit);