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