PRCM: OMAP3: Fix to wrongly modified omap2_clk_wait_ready
[linux-ginger.git] / drivers / i2c / chips / menelaus.c
blobf02ce23ca0ea2d4b501cf9d69f7ae7e34b58ca3f
2 /*
3 * Copyright (C) 2004 Texas Instruments, Inc.
5 * Some parts based tps65010.c:
6 * Copyright (C) 2004 Texas Instruments and
7 * Copyright (C) 2004-2005 David Brownell
9 * Some parts based on tlv320aic24.c:
10 * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
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
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.
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.
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
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>
45 #include <asm/mach-types.h>
46 #include <asm/mach/irq.h>
48 #include <asm/arch/gpio.h>
49 #include <asm/arch/menelaus.h>
51 #define DRIVER_NAME "menelaus"
53 #define MENELAUS_I2C_ADDRESS 0x72
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
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 */
132 static void menelaus_work(struct work_struct *_menelaus);
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;
150 static struct menelaus_chip *the_menelaus;
152 static int menelaus_write_reg(int reg, u8 value)
154 int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
156 if (val < 0) {
157 dev_err(&the_menelaus->client->dev, "write error");
158 return val;
161 return 0;
164 static int menelaus_read_reg(int reg)
166 int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
168 if (val < 0)
169 dev_err(&the_menelaus->client->dev, "read error");
171 return val;
174 static int menelaus_enable_irq(int irq)
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);
188 static int menelaus_disable_irq(int irq)
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);
202 static int menelaus_ack_irq(int irq)
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);
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 *))
214 int ret = 0;
216 mutex_lock(&the_menelaus->lock);
217 the_menelaus->handlers[irq] = handler;
218 ret = menelaus_enable_irq(irq);
219 mutex_unlock(&the_menelaus->lock);
221 return ret;
224 /* Removes handler for an interrupt */
225 static int menelaus_remove_irq_work(int irq)
227 int ret = 0;
229 mutex_lock(&the_menelaus->lock);
230 ret = menelaus_disable_irq(irq);
231 the_menelaus->handlers[irq] = NULL;
232 mutex_unlock(&the_menelaus->lock);
234 return ret;
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
243 static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
245 int reg;
246 unsigned char card_mask = 0;
248 reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
249 if (reg < 0)
250 return;
252 if (!(reg & 0x1))
253 card_mask |= (1 << 0);
255 if (!(reg & 0x2))
256 card_mask |= (1 << 1);
258 if (menelaus_hw->mmc_callback)
259 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
260 card_mask);
264 * Toggles the MMC slots between open-drain and push-pull mode.
266 int menelaus_set_mmc_opendrain(int slot, int enable)
268 int ret, val;
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;
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);
290 ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
291 mutex_unlock(&the_menelaus->lock);
293 return ret;
295 EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
297 int menelaus_set_slot_sel(int enable)
299 int ret;
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;
315 EXPORT_SYMBOL(menelaus_set_slot_sel);
317 int menelaus_enable_slot(int slot, int enable)
319 int ret, val;
321 mutex_lock(&the_menelaus->lock);
322 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
323 if (ret < 0)
324 goto out;
325 val = ret;
326 if (slot == 1) {
327 if (enable)
328 val |= 1 << 0;
329 else
330 val &= ~(1 << 0);
331 } else {
332 if (enable)
333 val |= 1 << 1;
334 else
335 val &= ~(1 << 1);
337 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
339 out:
340 mutex_unlock(&the_menelaus->lock);
341 return ret;
343 EXPORT_SYMBOL(menelaus_enable_slot);
345 int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
347 int ret, val;
349 if (slot != 1 && slot != 2)
350 return -EINVAL;
351 if (power >= 3)
352 return -EINVAL;
354 mutex_lock(&the_menelaus->lock);
356 ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
357 if (ret < 0)
358 goto out;
359 val = ret;
360 if (slot == 1) {
361 if (cd_en)
362 val |= (1 << 4) | (1 << 6);
363 else
364 val &= ~((1 << 4) | (1 << 6));
365 } else {
366 if (cd_en)
367 val |= (1 << 5) | (1 << 7);
368 else
369 val &= ~((1 << 5) | (1 << 7));
371 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
372 if (ret < 0)
373 goto out;
375 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
376 if (ret < 0)
377 goto out;
378 val = ret;
379 if (slot == 1) {
380 if (enable)
381 val |= 1 << 0;
382 else
383 val &= ~(1 << 0);
384 } else {
385 int b;
387 if (enable)
388 val |= 1 << 1;
389 else
390 val &= ~(1 << 1);
391 b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
392 b &= ~0x03;
393 b |= power;
394 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
395 if (ret < 0)
396 goto out;
398 /* Disable autonomous shutdown */
399 val &= ~(0x03 << 2);
400 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
401 out:
402 mutex_unlock(&the_menelaus->lock);
403 return ret;
405 EXPORT_SYMBOL(menelaus_set_mmc_slot);
407 int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
408 void *data)
410 int ret = 0;
412 the_menelaus->mmc_callback_data = data;
413 the_menelaus->mmc_callback = callback;
414 ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
415 menelaus_mmc_cd_work);
416 if (ret < 0)
417 return ret;
418 ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
419 menelaus_mmc_cd_work);
420 if (ret < 0)
421 return ret;
422 ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
423 menelaus_mmc_cd_work);
424 if (ret < 0)
425 return ret;
426 ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
427 menelaus_mmc_cd_work);
429 return ret;
431 EXPORT_SYMBOL(menelaus_register_mmc_callback);
433 void menelaus_unregister_mmc_callback(void)
435 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
436 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
437 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
438 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
440 the_menelaus->mmc_callback = NULL;
441 the_menelaus->mmc_callback_data = 0;
443 EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
445 struct menelaus_vtg {
446 const char *name;
447 u8 vtg_reg;
448 u8 vtg_shift;
449 u8 vtg_bits;
450 u8 mode_reg;
453 struct menelaus_vtg_value {
454 u16 vtg;
455 u16 val;
458 static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
459 int vtg_val, int mode)
461 int val, ret;
462 struct i2c_client *c = the_menelaus->client;
464 mutex_lock(&the_menelaus->lock);
465 if (vtg == 0)
466 goto set_voltage;
468 ret = menelaus_read_reg(vtg->vtg_reg);
469 if (ret < 0)
470 goto out;
471 val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
472 val |= vtg_val << vtg->vtg_shift;
474 dev_dbg(&c->dev, "Setting voltage '%s'"
475 "to %d mV (reg 0x%02x, val 0x%02x)\n",
476 vtg->name, mV, vtg->vtg_reg, val);
478 ret = menelaus_write_reg(vtg->vtg_reg, val);
479 if (ret < 0)
480 goto out;
481 set_voltage:
482 ret = menelaus_write_reg(vtg->mode_reg, mode);
483 out:
484 mutex_unlock(&the_menelaus->lock);
485 if (ret == 0) {
486 /* Wait for voltage to stabilize */
487 msleep(1);
489 return ret;
492 static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
493 int n)
495 int i;
497 for (i = 0; i < n; i++, tbl++)
498 if (tbl->vtg == vtg)
499 return tbl->val;
500 return -EINVAL;
504 * Vcore can be programmed in two ways:
505 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
506 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
507 * and VCORE_CTRL4
509 * Call correct 'set' function accordingly
512 static const struct menelaus_vtg_value vcore_values[] = {
513 { 1000, 0 },
514 { 1025, 1 },
515 { 1050, 2 },
516 { 1075, 3 },
517 { 1100, 4 },
518 { 1125, 5 },
519 { 1150, 6 },
520 { 1175, 7 },
521 { 1200, 8 },
522 { 1225, 9 },
523 { 1250, 10 },
524 { 1275, 11 },
525 { 1300, 12 },
526 { 1325, 13 },
527 { 1350, 14 },
528 { 1375, 15 },
529 { 1400, 16 },
530 { 1425, 17 },
531 { 1450, 18 },
534 int menelaus_set_vcore_sw(unsigned int mV)
536 int val, ret;
537 struct i2c_client *c = the_menelaus->client;
539 val = menelaus_get_vtg_value(mV, vcore_values,
540 ARRAY_SIZE(vcore_values));
541 if (val < 0)
542 return -EINVAL;
544 dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
546 /* Set SW mode and the voltage in one go. */
547 mutex_lock(&the_menelaus->lock);
548 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
549 if (ret == 0)
550 the_menelaus->vcore_hw_mode = 0;
551 mutex_unlock(&the_menelaus->lock);
552 msleep(1);
554 return ret;
557 int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
559 int fval, rval, val, ret;
560 struct i2c_client *c = the_menelaus->client;
562 rval = menelaus_get_vtg_value(roof_mV, vcore_values,
563 ARRAY_SIZE(vcore_values));
564 if (rval < 0)
565 return -EINVAL;
566 fval = menelaus_get_vtg_value(floor_mV, vcore_values,
567 ARRAY_SIZE(vcore_values));
568 if (fval < 0)
569 return -EINVAL;
571 dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
572 floor_mV, roof_mV);
574 mutex_lock(&the_menelaus->lock);
575 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
576 if (ret < 0)
577 goto out;
578 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
579 if (ret < 0)
580 goto out;
581 if (!the_menelaus->vcore_hw_mode) {
582 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
583 /* HW mode, turn OFF byte comparator */
584 val |= ((1 << 7) | (1 << 5));
585 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
586 the_menelaus->vcore_hw_mode = 1;
588 msleep(1);
589 out:
590 mutex_unlock(&the_menelaus->lock);
591 return ret;
594 static const struct menelaus_vtg vmem_vtg = {
595 .name = "VMEM",
596 .vtg_reg = MENELAUS_LDO_CTRL1,
597 .vtg_shift = 0,
598 .vtg_bits = 2,
599 .mode_reg = MENELAUS_LDO_CTRL3,
602 static const struct menelaus_vtg_value vmem_values[] = {
603 { 1500, 0 },
604 { 1800, 1 },
605 { 1900, 2 },
606 { 2500, 3 },
609 int menelaus_set_vmem(unsigned int mV)
611 int val;
613 if (mV == 0)
614 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
616 val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
617 if (val < 0)
618 return -EINVAL;
619 return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
621 EXPORT_SYMBOL(menelaus_set_vmem);
623 static const struct menelaus_vtg vio_vtg = {
624 .name = "VIO",
625 .vtg_reg = MENELAUS_LDO_CTRL1,
626 .vtg_shift = 2,
627 .vtg_bits = 2,
628 .mode_reg = MENELAUS_LDO_CTRL4,
631 static const struct menelaus_vtg_value vio_values[] = {
632 { 1500, 0 },
633 { 1800, 1 },
634 { 2500, 2 },
635 { 2800, 3 },
638 int menelaus_set_vio(unsigned int mV)
640 int val;
642 if (mV == 0)
643 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
645 val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
646 if (val < 0)
647 return -EINVAL;
648 return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
650 EXPORT_SYMBOL(menelaus_set_vio);
652 static const struct menelaus_vtg_value vdcdc_values[] = {
653 { 1500, 0 },
654 { 1800, 1 },
655 { 2000, 2 },
656 { 2200, 3 },
657 { 2400, 4 },
658 { 2800, 5 },
659 { 3000, 6 },
660 { 3300, 7 },
663 static const struct menelaus_vtg vdcdc2_vtg = {
664 .name = "VDCDC2",
665 .vtg_reg = MENELAUS_DCDC_CTRL1,
666 .vtg_shift = 0,
667 .vtg_bits = 3,
668 .mode_reg = MENELAUS_DCDC_CTRL2,
671 static const struct menelaus_vtg vdcdc3_vtg = {
672 .name = "VDCDC3",
673 .vtg_reg = MENELAUS_DCDC_CTRL1,
674 .vtg_shift = 3,
675 .vtg_bits = 3,
676 .mode_reg = MENELAUS_DCDC_CTRL3,
679 int menelaus_set_vdcdc(int dcdc, unsigned int mV)
681 const struct menelaus_vtg *vtg;
682 int val;
684 if (dcdc != 2 && dcdc != 3)
685 return -EINVAL;
686 if (dcdc == 2)
687 vtg = &vdcdc2_vtg;
688 else
689 vtg = &vdcdc3_vtg;
691 if (mV == 0)
692 return menelaus_set_voltage(vtg, 0, 0, 0);
694 val = menelaus_get_vtg_value(mV, vdcdc_values,
695 ARRAY_SIZE(vdcdc_values));
696 if (val < 0)
697 return -EINVAL;
698 return menelaus_set_voltage(vtg, mV, val, 0x03);
701 static const struct menelaus_vtg_value vmmc_values[] = {
702 { 1850, 0 },
703 { 2800, 1 },
704 { 3000, 2 },
705 { 3100, 3 },
708 static const struct menelaus_vtg vmmc_vtg = {
709 .name = "VMMC",
710 .vtg_reg = MENELAUS_LDO_CTRL1,
711 .vtg_shift = 6,
712 .vtg_bits = 2,
713 .mode_reg = MENELAUS_LDO_CTRL7,
716 int menelaus_set_vmmc(unsigned int mV)
718 int val;
720 if (mV == 0)
721 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
723 val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
724 if (val < 0)
725 return -EINVAL;
726 return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
728 EXPORT_SYMBOL(menelaus_set_vmmc);
731 static const struct menelaus_vtg_value vaux_values[] = {
732 { 1500, 0 },
733 { 1800, 1 },
734 { 2500, 2 },
735 { 2800, 3 },
738 static const struct menelaus_vtg vaux_vtg = {
739 .name = "VAUX",
740 .vtg_reg = MENELAUS_LDO_CTRL1,
741 .vtg_shift = 4,
742 .vtg_bits = 2,
743 .mode_reg = MENELAUS_LDO_CTRL6,
746 int menelaus_set_vaux(unsigned int mV)
748 int val;
750 if (mV == 0)
751 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
753 val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
754 if (val < 0)
755 return -EINVAL;
756 return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
758 EXPORT_SYMBOL(menelaus_set_vaux);
760 int menelaus_get_slot_pin_states(void)
762 return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
764 EXPORT_SYMBOL(menelaus_get_slot_pin_states);
766 int menelaus_set_regulator_sleep(int enable, u32 val)
768 int t, ret;
769 struct i2c_client *c = the_menelaus->client;
771 mutex_lock(&the_menelaus->lock);
772 ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
773 if (ret < 0)
774 goto out;
776 dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
778 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
779 if (ret < 0)
780 goto out;
781 t = ((1 << 6) | 0x04);
782 if (enable)
783 ret |= t;
784 else
785 ret &= ~t;
786 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
787 out:
788 mutex_unlock(&the_menelaus->lock);
789 return ret;
792 /*-----------------------------------------------------------------------*/
794 /* Handles Menelaus interrupts. Does not run in interrupt context */
795 static void menelaus_work(struct work_struct *_menelaus)
797 struct menelaus_chip *menelaus =
798 container_of(_menelaus, struct menelaus_chip, work);
799 void (*handler)(struct menelaus_chip *menelaus);
801 while (1) {
802 unsigned isr;
804 isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
805 & ~menelaus->mask2) << 8;
806 isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
807 & ~menelaus->mask1;
808 if (!isr)
809 break;
811 while (isr) {
812 int irq = fls(isr) - 1;
813 isr &= ~(1 << irq);
815 mutex_lock(&menelaus->lock);
816 menelaus_disable_irq(irq);
817 menelaus_ack_irq(irq);
818 handler = menelaus->handlers[irq];
819 if (handler)
820 handler(menelaus);
821 menelaus_enable_irq(irq);
822 mutex_unlock(&menelaus->lock);
825 enable_irq(menelaus->client->irq);
829 * We cannot use I2C in interrupt context, so we just schedule work.
831 static irqreturn_t menelaus_irq(int irq, void *_menelaus)
833 struct menelaus_chip *menelaus = _menelaus;
835 disable_irq_nosync(irq);
836 (void)schedule_work(&menelaus->work);
838 return IRQ_HANDLED;
841 /*-----------------------------------------------------------------------*/
844 * The RTC needs to be set once, then it runs on backup battery power.
845 * It supports alarms, including system wake alarms (from some modes);
846 * and 1/second IRQs if requested.
848 #ifdef CONFIG_RTC_DRV_TWL92330
850 #define RTC_CTRL_RTC_EN (1 << 0)
851 #define RTC_CTRL_AL_EN (1 << 1)
852 #define RTC_CTRL_MODE12 (1 << 2)
853 #define RTC_CTRL_EVERY_MASK (3 << 3)
854 #define RTC_CTRL_EVERY_SEC (0 << 3)
855 #define RTC_CTRL_EVERY_MIN (1 << 3)
856 #define RTC_CTRL_EVERY_HR (2 << 3)
857 #define RTC_CTRL_EVERY_DAY (3 << 3)
859 #define RTC_UPDATE_EVERY 0x08
861 #define RTC_HR_PM (1 << 7)
863 static void menelaus_to_time(char *regs, struct rtc_time *t)
865 t->tm_sec = BCD2BIN(regs[0]);
866 t->tm_min = BCD2BIN(regs[1]);
867 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
868 t->tm_hour = BCD2BIN(regs[2] & 0x1f) - 1;
869 if (regs[2] & RTC_HR_PM)
870 t->tm_hour += 12;
871 } else
872 t->tm_hour = BCD2BIN(regs[2] & 0x3f);
873 t->tm_mday = BCD2BIN(regs[3]);
874 t->tm_mon = BCD2BIN(regs[4]) - 1;
875 t->tm_year = BCD2BIN(regs[5]) + 100;
878 static int time_to_menelaus(struct rtc_time *t, int regnum)
880 int hour, status;
882 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_sec));
883 if (status < 0)
884 goto fail;
886 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_min));
887 if (status < 0)
888 goto fail;
890 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
891 hour = t->tm_hour + 1;
892 if (hour > 12)
893 hour = RTC_HR_PM | BIN2BCD(hour - 12);
894 else
895 hour = BIN2BCD(hour);
896 } else
897 hour = BIN2BCD(t->tm_hour);
898 status = menelaus_write_reg(regnum++, hour);
899 if (status < 0)
900 goto fail;
902 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mday));
903 if (status < 0)
904 goto fail;
906 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mon + 1));
907 if (status < 0)
908 goto fail;
910 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_year - 100));
911 if (status < 0)
912 goto fail;
914 return 0;
915 fail:
916 dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
917 --regnum, status);
918 return status;
921 static int menelaus_read_time(struct device *dev, struct rtc_time *t)
923 struct i2c_msg msg[2];
924 char regs[7];
925 int status;
927 /* block read date and time registers */
928 regs[0] = MENELAUS_RTC_SEC;
930 msg[0].addr = MENELAUS_I2C_ADDRESS;
931 msg[0].flags = 0;
932 msg[0].len = 1;
933 msg[0].buf = regs;
935 msg[1].addr = MENELAUS_I2C_ADDRESS;
936 msg[1].flags = I2C_M_RD;
937 msg[1].len = sizeof(regs);
938 msg[1].buf = regs;
940 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
941 if (status != 2) {
942 dev_err(dev, "%s error %d\n", "read", status);
943 return -EIO;
946 menelaus_to_time(regs, t);
947 t->tm_wday = BCD2BIN(regs[6]);
949 return 0;
952 static int menelaus_set_time(struct device *dev, struct rtc_time *t)
954 int status;
956 /* write date and time registers */
957 status = time_to_menelaus(t, MENELAUS_RTC_SEC);
958 if (status < 0)
959 return status;
960 status = menelaus_write_reg(MENELAUS_RTC_WKDAY, BIN2BCD(t->tm_wday));
961 if (status < 0) {
962 dev_err(&the_menelaus->client->dev, "rtc write reg %02x"
963 "err %d\n", MENELAUS_RTC_WKDAY, status);
964 return status;
967 /* now commit the write */
968 status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
969 if (status < 0)
970 dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
971 status);
973 return 0;
976 static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
978 struct i2c_msg msg[2];
979 char regs[6];
980 int status;
982 /* block read alarm registers */
983 regs[0] = MENELAUS_RTC_AL_SEC;
985 msg[0].addr = MENELAUS_I2C_ADDRESS;
986 msg[0].flags = 0;
987 msg[0].len = 1;
988 msg[0].buf = regs;
990 msg[1].addr = MENELAUS_I2C_ADDRESS;
991 msg[1].flags = I2C_M_RD;
992 msg[1].len = sizeof(regs);
993 msg[1].buf = regs;
995 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
996 if (status != 2) {
997 dev_err(dev, "%s error %d\n", "alarm read", status);
998 return -EIO;
1001 menelaus_to_time(regs, &w->time);
1003 w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
1005 /* NOTE we *could* check if actually pending... */
1006 w->pending = 0;
1008 return 0;
1011 static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
1013 int status;
1015 if (the_menelaus->client->irq <= 0 && w->enabled)
1016 return -ENODEV;
1018 /* clear previous alarm enable */
1019 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
1020 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1021 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1022 the_menelaus->rtc_control);
1023 if (status < 0)
1024 return status;
1027 /* write alarm registers */
1028 status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
1029 if (status < 0)
1030 return status;
1032 /* enable alarm if requested */
1033 if (w->enabled) {
1034 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1035 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1036 the_menelaus->rtc_control);
1039 return status;
1042 #ifdef CONFIG_RTC_INTF_DEV
1044 static void menelaus_rtc_update_work(struct menelaus_chip *m)
1046 /* report 1/sec update */
1047 local_irq_disable();
1048 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
1049 local_irq_enable();
1052 static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
1054 int status;
1056 if (the_menelaus->client->irq <= 0)
1057 return -ENOIOCTLCMD;
1059 switch (cmd) {
1060 /* alarm IRQ */
1061 case RTC_AIE_ON:
1062 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
1063 return 0;
1064 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1065 break;
1066 case RTC_AIE_OFF:
1067 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
1068 return 0;
1069 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1070 break;
1071 /* 1/second "update" IRQ */
1072 case RTC_UIE_ON:
1073 if (the_menelaus->uie)
1074 return 0;
1075 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1076 status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
1077 menelaus_rtc_update_work);
1078 if (status == 0)
1079 the_menelaus->uie = 1;
1080 return status;
1081 case RTC_UIE_OFF:
1082 if (!the_menelaus->uie)
1083 return 0;
1084 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1085 if (status == 0)
1086 the_menelaus->uie = 0;
1087 return status;
1088 default:
1089 return -ENOIOCTLCMD;
1091 return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1094 #else
1095 #define menelaus_ioctl NULL
1096 #endif
1098 /* REVISIT no compensation register support ... */
1100 static const struct rtc_class_ops menelaus_rtc_ops = {
1101 .ioctl = menelaus_ioctl,
1102 .read_time = menelaus_read_time,
1103 .set_time = menelaus_set_time,
1104 .read_alarm = menelaus_read_alarm,
1105 .set_alarm = menelaus_set_alarm,
1108 static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1110 /* report alarm */
1111 local_irq_disable();
1112 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
1113 local_irq_enable();
1115 /* then disable it; alarms are oneshot */
1116 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1117 menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1120 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1122 int alarm = (m->client->irq > 0);
1124 /* assume 32KDETEN pin is pulled high */
1125 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1126 dev_dbg(&m->client->dev, "no 32k oscillator\n");
1127 return;
1130 /* support RTC alarm; it can issue wakeups */
1131 if (alarm) {
1132 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1133 menelaus_rtc_alarm_work) < 0) {
1134 dev_err(&m->client->dev, "can't handle RTC alarm\n");
1135 return;
1137 device_init_wakeup(&m->client->dev, 1);
1140 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1141 m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1142 if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1143 || (m->rtc_control & RTC_CTRL_AL_EN)
1144 || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1145 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1146 dev_warn(&m->client->dev, "rtc clock needs setting\n");
1147 m->rtc_control |= RTC_CTRL_RTC_EN;
1149 m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1150 m->rtc_control &= ~RTC_CTRL_AL_EN;
1151 menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1154 m->rtc = rtc_device_register(DRIVER_NAME,
1155 &m->client->dev,
1156 &menelaus_rtc_ops, THIS_MODULE);
1157 if (IS_ERR(m->rtc)) {
1158 if (alarm) {
1159 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1160 device_init_wakeup(&m->client->dev, 0);
1162 dev_err(&m->client->dev, "can't register RTC: %d\n",
1163 (int) PTR_ERR(m->rtc));
1164 the_menelaus->rtc = NULL;
1168 #else
1170 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1172 /* nothing */
1175 #endif
1177 /*-----------------------------------------------------------------------*/
1179 static struct i2c_driver menelaus_i2c_driver;
1181 static int menelaus_probe(struct i2c_client *client,
1182 const struct i2c_device_id *id)
1184 struct menelaus_chip *menelaus;
1185 int rev = 0, val;
1186 int err = 0;
1187 struct menelaus_platform_data *menelaus_pdata =
1188 client->dev.platform_data;
1190 if (the_menelaus) {
1191 dev_dbg(&client->dev, "only one %s for now\n",
1192 DRIVER_NAME);
1193 return -ENODEV;
1196 menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL);
1197 if (!menelaus)
1198 return -ENOMEM;
1200 i2c_set_clientdata(client, menelaus);
1202 the_menelaus = menelaus;
1203 menelaus->client = client;
1205 /* If a true probe check the device */
1206 rev = menelaus_read_reg(MENELAUS_REV);
1207 if (rev < 0) {
1208 dev_err(&client->dev, "device not found");
1209 err = -ENODEV;
1210 goto fail1;
1213 /* Ack and disable all Menelaus interrupts */
1214 menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1215 menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1216 menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1217 menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1218 menelaus->mask1 = 0xff;
1219 menelaus->mask2 = 0xff;
1221 /* Set output buffer strengths */
1222 menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1224 if (client->irq > 0) {
1225 err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED,
1226 DRIVER_NAME, menelaus);
1227 if (err) {
1228 dev_dbg(&client->dev, "can't get IRQ %d, err %d",
1229 client->irq, err);
1230 goto fail1;
1234 mutex_init(&menelaus->lock);
1235 INIT_WORK(&menelaus->work, menelaus_work);
1237 dev_info(&client->dev, "Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1239 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1240 if (val < 0)
1241 goto fail2;
1242 if (val & (1 << 7))
1243 menelaus->vcore_hw_mode = 1;
1244 else
1245 menelaus->vcore_hw_mode = 0;
1247 if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1248 err = menelaus_pdata->late_init(&client->dev);
1249 if (err < 0)
1250 goto fail2;
1253 menelaus_rtc_init(menelaus);
1255 return 0;
1256 fail2:
1257 free_irq(client->irq, menelaus);
1258 flush_scheduled_work();
1259 fail1:
1260 kfree(menelaus);
1261 return err;
1264 static int __exit menelaus_remove(struct i2c_client *client)
1266 struct menelaus_chip *menelaus = i2c_get_clientdata(client);
1268 free_irq(client->irq, menelaus);
1269 kfree(menelaus);
1270 i2c_set_clientdata(client, NULL);
1271 the_menelaus = NULL;
1272 return 0;
1275 static const struct i2c_device_id menelaus_id[] = {
1276 { "menelaus", 0 },
1279 MODULE_DEVICE_TABLE(i2c, menelaus_id);
1281 static struct i2c_driver menelaus_i2c_driver = {
1282 .driver = {
1283 .name = DRIVER_NAME,
1285 .probe = menelaus_probe,
1286 .remove = __exit_p(menelaus_remove),
1287 .id_table = menelaus_id,
1290 static int __init menelaus_init(void)
1292 return i2c_add_driver(&menelaus_i2c_driver);
1295 static void __exit menelaus_exit(void)
1297 i2c_del_driver(&menelaus_i2c_driver);
1299 /* FIXME: Shutdown menelaus parts that can be shut down */
1302 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1303 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1304 MODULE_LICENSE("GPL");
1306 module_init(menelaus_init);
1307 module_exit(menelaus_exit);