2 * Copyright (C) 2004 Texas Instruments, Inc.
4 * Some parts based tps65010.c:
5 * Copyright (C) 2004 Texas Instruments and
6 * Copyright (C) 2004-2005 David Brownell
8 * Some parts based on tlv320aic24.c:
9 * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
11 * Changes for interrupt handling and clean-up by
12 * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
13 * Cleanup and generalized support for voltage setting by
15 * Added support for controlling VCORE and regulator sleep states,
16 * Amit Kucheria <amit.kucheria@nokia.com>
17 * Copyright (C) 2005, 2006 Nokia Corporation
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <linux/module.h>
35 #include <linux/i2c.h>
36 #include <linux/interrupt.h>
37 #include <linux/sched.h>
38 #include <linux/mutex.h>
39 #include <linux/workqueue.h>
40 #include <linux/delay.h>
41 #include <linux/rtc.h>
42 #include <linux/bcd.h>
43 #include <linux/slab.h>
45 #include <asm/mach/irq.h>
47 #include <mach/gpio.h>
48 #include <plat/menelaus.h>
50 #define DRIVER_NAME "menelaus"
52 #define MENELAUS_I2C_ADDRESS 0x72
54 #define MENELAUS_REV 0x01
55 #define MENELAUS_VCORE_CTRL1 0x02
56 #define MENELAUS_VCORE_CTRL2 0x03
57 #define MENELAUS_VCORE_CTRL3 0x04
58 #define MENELAUS_VCORE_CTRL4 0x05
59 #define MENELAUS_VCORE_CTRL5 0x06
60 #define MENELAUS_DCDC_CTRL1 0x07
61 #define MENELAUS_DCDC_CTRL2 0x08
62 #define MENELAUS_DCDC_CTRL3 0x09
63 #define MENELAUS_LDO_CTRL1 0x0A
64 #define MENELAUS_LDO_CTRL2 0x0B
65 #define MENELAUS_LDO_CTRL3 0x0C
66 #define MENELAUS_LDO_CTRL4 0x0D
67 #define MENELAUS_LDO_CTRL5 0x0E
68 #define MENELAUS_LDO_CTRL6 0x0F
69 #define MENELAUS_LDO_CTRL7 0x10
70 #define MENELAUS_LDO_CTRL8 0x11
71 #define MENELAUS_SLEEP_CTRL1 0x12
72 #define MENELAUS_SLEEP_CTRL2 0x13
73 #define MENELAUS_DEVICE_OFF 0x14
74 #define MENELAUS_OSC_CTRL 0x15
75 #define MENELAUS_DETECT_CTRL 0x16
76 #define MENELAUS_INT_MASK1 0x17
77 #define MENELAUS_INT_MASK2 0x18
78 #define MENELAUS_INT_STATUS1 0x19
79 #define MENELAUS_INT_STATUS2 0x1A
80 #define MENELAUS_INT_ACK1 0x1B
81 #define MENELAUS_INT_ACK2 0x1C
82 #define MENELAUS_GPIO_CTRL 0x1D
83 #define MENELAUS_GPIO_IN 0x1E
84 #define MENELAUS_GPIO_OUT 0x1F
85 #define MENELAUS_BBSMS 0x20
86 #define MENELAUS_RTC_CTRL 0x21
87 #define MENELAUS_RTC_UPDATE 0x22
88 #define MENELAUS_RTC_SEC 0x23
89 #define MENELAUS_RTC_MIN 0x24
90 #define MENELAUS_RTC_HR 0x25
91 #define MENELAUS_RTC_DAY 0x26
92 #define MENELAUS_RTC_MON 0x27
93 #define MENELAUS_RTC_YR 0x28
94 #define MENELAUS_RTC_WKDAY 0x29
95 #define MENELAUS_RTC_AL_SEC 0x2A
96 #define MENELAUS_RTC_AL_MIN 0x2B
97 #define MENELAUS_RTC_AL_HR 0x2C
98 #define MENELAUS_RTC_AL_DAY 0x2D
99 #define MENELAUS_RTC_AL_MON 0x2E
100 #define MENELAUS_RTC_AL_YR 0x2F
101 #define MENELAUS_RTC_COMP_MSB 0x30
102 #define MENELAUS_RTC_COMP_LSB 0x31
103 #define MENELAUS_S1_PULL_EN 0x32
104 #define MENELAUS_S1_PULL_DIR 0x33
105 #define MENELAUS_S2_PULL_EN 0x34
106 #define MENELAUS_S2_PULL_DIR 0x35
107 #define MENELAUS_MCT_CTRL1 0x36
108 #define MENELAUS_MCT_CTRL2 0x37
109 #define MENELAUS_MCT_CTRL3 0x38
110 #define MENELAUS_MCT_PIN_ST 0x39
111 #define MENELAUS_DEBOUNCE1 0x3A
113 #define IH_MENELAUS_IRQS 12
114 #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
115 #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
116 #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
117 #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
118 #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
119 #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
120 #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
121 #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
122 #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
123 #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
124 #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
125 #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
126 #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
127 #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
128 #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
129 #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
131 /* VCORE_CTRL1 register */
132 #define VCORE_CTRL1_BYP_COMP (1 << 5)
133 #define VCORE_CTRL1_HW_NSW (1 << 7)
135 /* GPIO_CTRL register */
136 #define GPIO_CTRL_SLOTSELEN (1 << 5)
137 #define GPIO_CTRL_SLPCTLEN (1 << 6)
138 #define GPIO1_DIR_INPUT (1 << 0)
139 #define GPIO2_DIR_INPUT (1 << 1)
140 #define GPIO3_DIR_INPUT (1 << 2)
142 /* MCT_CTRL1 register */
143 #define MCT_CTRL1_S1_CMD_OD (1 << 2)
144 #define MCT_CTRL1_S2_CMD_OD (1 << 3)
146 /* MCT_CTRL2 register */
147 #define MCT_CTRL2_VS2_SEL_D0 (1 << 0)
148 #define MCT_CTRL2_VS2_SEL_D1 (1 << 1)
149 #define MCT_CTRL2_S1CD_BUFEN (1 << 4)
150 #define MCT_CTRL2_S2CD_BUFEN (1 << 5)
151 #define MCT_CTRL2_S1CD_DBEN (1 << 6)
152 #define MCT_CTRL2_S2CD_BEN (1 << 7)
154 /* MCT_CTRL3 register */
155 #define MCT_CTRL3_SLOT1_EN (1 << 0)
156 #define MCT_CTRL3_SLOT2_EN (1 << 1)
157 #define MCT_CTRL3_S1_AUTO_EN (1 << 2)
158 #define MCT_CTRL3_S2_AUTO_EN (1 << 3)
160 /* MCT_PIN_ST register */
161 #define MCT_PIN_ST_S1_CD_ST (1 << 0)
162 #define MCT_PIN_ST_S2_CD_ST (1 << 1)
164 static void menelaus_work(struct work_struct
*_menelaus
);
166 struct menelaus_chip
{
168 struct i2c_client
*client
;
169 struct work_struct work
;
170 #ifdef CONFIG_RTC_DRV_TWL92330
171 struct rtc_device
*rtc
;
175 unsigned vcore_hw_mode
:1;
177 void (*handlers
[16])(struct menelaus_chip
*);
178 void (*mmc_callback
)(void *data
, u8 mask
);
179 void *mmc_callback_data
;
182 static struct menelaus_chip
*the_menelaus
;
184 static int menelaus_write_reg(int reg
, u8 value
)
186 int val
= i2c_smbus_write_byte_data(the_menelaus
->client
, reg
, value
);
189 pr_err(DRIVER_NAME
": write error");
196 static int menelaus_read_reg(int reg
)
198 int val
= i2c_smbus_read_byte_data(the_menelaus
->client
, reg
);
201 pr_err(DRIVER_NAME
": read error");
206 static int menelaus_enable_irq(int irq
)
210 the_menelaus
->mask2
&= ~(1 << irq
);
211 return menelaus_write_reg(MENELAUS_INT_MASK2
,
212 the_menelaus
->mask2
);
214 the_menelaus
->mask1
&= ~(1 << irq
);
215 return menelaus_write_reg(MENELAUS_INT_MASK1
,
216 the_menelaus
->mask1
);
220 static int menelaus_disable_irq(int irq
)
224 the_menelaus
->mask2
|= (1 << irq
);
225 return menelaus_write_reg(MENELAUS_INT_MASK2
,
226 the_menelaus
->mask2
);
228 the_menelaus
->mask1
|= (1 << irq
);
229 return menelaus_write_reg(MENELAUS_INT_MASK1
,
230 the_menelaus
->mask1
);
234 static int menelaus_ack_irq(int irq
)
237 return menelaus_write_reg(MENELAUS_INT_ACK2
, 1 << (irq
- 8));
239 return menelaus_write_reg(MENELAUS_INT_ACK1
, 1 << irq
);
242 /* Adds a handler for an interrupt. Does not run in interrupt context */
243 static int menelaus_add_irq_work(int irq
,
244 void (*handler
)(struct menelaus_chip
*))
248 mutex_lock(&the_menelaus
->lock
);
249 the_menelaus
->handlers
[irq
] = handler
;
250 ret
= menelaus_enable_irq(irq
);
251 mutex_unlock(&the_menelaus
->lock
);
256 /* Removes handler for an interrupt */
257 static int menelaus_remove_irq_work(int irq
)
261 mutex_lock(&the_menelaus
->lock
);
262 ret
= menelaus_disable_irq(irq
);
263 the_menelaus
->handlers
[irq
] = NULL
;
264 mutex_unlock(&the_menelaus
->lock
);
270 * Gets scheduled when a card detect interrupt happens. Note that in some cases
271 * this line is wired to card cover switch rather than the card detect switch
272 * in each slot. In this case the cards are not seen by menelaus.
273 * FIXME: Add handling for D1 too
275 static void menelaus_mmc_cd_work(struct menelaus_chip
*menelaus_hw
)
278 unsigned char card_mask
= 0;
280 reg
= menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
285 card_mask
|= MCT_PIN_ST_S1_CD_ST
;
288 card_mask
|= MCT_PIN_ST_S2_CD_ST
;
290 if (menelaus_hw
->mmc_callback
)
291 menelaus_hw
->mmc_callback(menelaus_hw
->mmc_callback_data
,
296 * Toggles the MMC slots between open-drain and push-pull mode.
298 int menelaus_set_mmc_opendrain(int slot
, int enable
)
302 if (slot
!= 1 && slot
!= 2)
304 mutex_lock(&the_menelaus
->lock
);
305 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL1
);
307 mutex_unlock(&the_menelaus
->lock
);
313 val
|= MCT_CTRL1_S1_CMD_OD
;
315 val
&= ~MCT_CTRL1_S1_CMD_OD
;
318 val
|= MCT_CTRL1_S2_CMD_OD
;
320 val
&= ~MCT_CTRL1_S2_CMD_OD
;
322 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL1
, val
);
323 mutex_unlock(&the_menelaus
->lock
);
327 EXPORT_SYMBOL(menelaus_set_mmc_opendrain
);
329 int menelaus_set_slot_sel(int enable
)
333 mutex_lock(&the_menelaus
->lock
);
334 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
337 ret
|= GPIO2_DIR_INPUT
;
339 ret
|= GPIO_CTRL_SLOTSELEN
;
341 ret
&= ~GPIO_CTRL_SLOTSELEN
;
342 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
344 mutex_unlock(&the_menelaus
->lock
);
347 EXPORT_SYMBOL(menelaus_set_slot_sel
);
349 int menelaus_set_mmc_slot(int slot
, int enable
, int power
, int cd_en
)
353 if (slot
!= 1 && slot
!= 2)
358 mutex_lock(&the_menelaus
->lock
);
360 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
366 val
|= MCT_CTRL2_S1CD_BUFEN
| MCT_CTRL2_S1CD_DBEN
;
368 val
&= ~(MCT_CTRL2_S1CD_BUFEN
| MCT_CTRL2_S1CD_DBEN
);
371 val
|= MCT_CTRL2_S2CD_BUFEN
| MCT_CTRL2_S2CD_BEN
;
373 val
&= ~(MCT_CTRL2_S2CD_BUFEN
| MCT_CTRL2_S2CD_BEN
);
375 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, val
);
379 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL3
);
385 val
|= MCT_CTRL3_SLOT1_EN
;
387 val
&= ~MCT_CTRL3_SLOT1_EN
;
392 val
|= MCT_CTRL3_SLOT2_EN
;
394 val
&= ~MCT_CTRL3_SLOT2_EN
;
395 b
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
396 b
&= ~(MCT_CTRL2_VS2_SEL_D0
| MCT_CTRL2_VS2_SEL_D1
);
398 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, b
);
402 /* Disable autonomous shutdown */
403 val
&= ~(MCT_CTRL3_S1_AUTO_EN
| MCT_CTRL3_S2_AUTO_EN
);
404 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL3
, val
);
406 mutex_unlock(&the_menelaus
->lock
);
409 EXPORT_SYMBOL(menelaus_set_mmc_slot
);
411 int menelaus_register_mmc_callback(void (*callback
)(void *data
, u8 card_mask
),
416 the_menelaus
->mmc_callback_data
= data
;
417 the_menelaus
->mmc_callback
= callback
;
418 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ
,
419 menelaus_mmc_cd_work
);
422 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ
,
423 menelaus_mmc_cd_work
);
426 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ
,
427 menelaus_mmc_cd_work
);
430 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ
,
431 menelaus_mmc_cd_work
);
435 EXPORT_SYMBOL(menelaus_register_mmc_callback
);
437 void menelaus_unregister_mmc_callback(void)
439 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ
);
440 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ
);
441 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ
);
442 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ
);
444 the_menelaus
->mmc_callback
= NULL
;
445 the_menelaus
->mmc_callback_data
= 0;
447 EXPORT_SYMBOL(menelaus_unregister_mmc_callback
);
449 struct menelaus_vtg
{
457 struct menelaus_vtg_value
{
462 static int menelaus_set_voltage(const struct menelaus_vtg
*vtg
, int mV
,
463 int vtg_val
, int mode
)
466 struct i2c_client
*c
= the_menelaus
->client
;
468 mutex_lock(&the_menelaus
->lock
);
472 ret
= menelaus_read_reg(vtg
->vtg_reg
);
475 val
= ret
& ~(((1 << vtg
->vtg_bits
) - 1) << vtg
->vtg_shift
);
476 val
|= vtg_val
<< vtg
->vtg_shift
;
478 dev_dbg(&c
->dev
, "Setting voltage '%s'"
479 "to %d mV (reg 0x%02x, val 0x%02x)\n",
480 vtg
->name
, mV
, vtg
->vtg_reg
, val
);
482 ret
= menelaus_write_reg(vtg
->vtg_reg
, val
);
486 ret
= menelaus_write_reg(vtg
->mode_reg
, mode
);
488 mutex_unlock(&the_menelaus
->lock
);
490 /* Wait for voltage to stabilize */
496 static int menelaus_get_vtg_value(int vtg
, const struct menelaus_vtg_value
*tbl
,
501 for (i
= 0; i
< n
; i
++, tbl
++)
508 * Vcore can be programmed in two ways:
509 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
510 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
513 * Call correct 'set' function accordingly
516 static const struct menelaus_vtg_value vcore_values
[] = {
538 int menelaus_set_vcore_sw(unsigned int mV
)
541 struct i2c_client
*c
= the_menelaus
->client
;
543 val
= menelaus_get_vtg_value(mV
, vcore_values
,
544 ARRAY_SIZE(vcore_values
));
548 dev_dbg(&c
->dev
, "Setting VCORE to %d mV (val 0x%02x)\n", mV
, val
);
550 /* Set SW mode and the voltage in one go. */
551 mutex_lock(&the_menelaus
->lock
);
552 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL1
, val
);
554 the_menelaus
->vcore_hw_mode
= 0;
555 mutex_unlock(&the_menelaus
->lock
);
561 int menelaus_set_vcore_hw(unsigned int roof_mV
, unsigned int floor_mV
)
563 int fval
, rval
, val
, ret
;
564 struct i2c_client
*c
= the_menelaus
->client
;
566 rval
= menelaus_get_vtg_value(roof_mV
, vcore_values
,
567 ARRAY_SIZE(vcore_values
));
570 fval
= menelaus_get_vtg_value(floor_mV
, vcore_values
,
571 ARRAY_SIZE(vcore_values
));
575 dev_dbg(&c
->dev
, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
578 mutex_lock(&the_menelaus
->lock
);
579 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL3
, fval
);
582 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL4
, rval
);
585 if (!the_menelaus
->vcore_hw_mode
) {
586 val
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
587 /* HW mode, turn OFF byte comparator */
588 val
|= (VCORE_CTRL1_HW_NSW
| VCORE_CTRL1_BYP_COMP
);
589 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL1
, val
);
590 the_menelaus
->vcore_hw_mode
= 1;
594 mutex_unlock(&the_menelaus
->lock
);
598 static const struct menelaus_vtg vmem_vtg
= {
600 .vtg_reg
= MENELAUS_LDO_CTRL1
,
603 .mode_reg
= MENELAUS_LDO_CTRL3
,
606 static const struct menelaus_vtg_value vmem_values
[] = {
613 int menelaus_set_vmem(unsigned int mV
)
618 return menelaus_set_voltage(&vmem_vtg
, 0, 0, 0);
620 val
= menelaus_get_vtg_value(mV
, vmem_values
, ARRAY_SIZE(vmem_values
));
623 return menelaus_set_voltage(&vmem_vtg
, mV
, val
, 0x02);
625 EXPORT_SYMBOL(menelaus_set_vmem
);
627 static const struct menelaus_vtg vio_vtg
= {
629 .vtg_reg
= MENELAUS_LDO_CTRL1
,
632 .mode_reg
= MENELAUS_LDO_CTRL4
,
635 static const struct menelaus_vtg_value vio_values
[] = {
642 int menelaus_set_vio(unsigned int mV
)
647 return menelaus_set_voltage(&vio_vtg
, 0, 0, 0);
649 val
= menelaus_get_vtg_value(mV
, vio_values
, ARRAY_SIZE(vio_values
));
652 return menelaus_set_voltage(&vio_vtg
, mV
, val
, 0x02);
654 EXPORT_SYMBOL(menelaus_set_vio
);
656 static const struct menelaus_vtg_value vdcdc_values
[] = {
667 static const struct menelaus_vtg vdcdc2_vtg
= {
669 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
672 .mode_reg
= MENELAUS_DCDC_CTRL2
,
675 static const struct menelaus_vtg vdcdc3_vtg
= {
677 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
680 .mode_reg
= MENELAUS_DCDC_CTRL3
,
683 int menelaus_set_vdcdc(int dcdc
, unsigned int mV
)
685 const struct menelaus_vtg
*vtg
;
688 if (dcdc
!= 2 && dcdc
!= 3)
696 return menelaus_set_voltage(vtg
, 0, 0, 0);
698 val
= menelaus_get_vtg_value(mV
, vdcdc_values
,
699 ARRAY_SIZE(vdcdc_values
));
702 return menelaus_set_voltage(vtg
, mV
, val
, 0x03);
705 static const struct menelaus_vtg_value vmmc_values
[] = {
712 static const struct menelaus_vtg vmmc_vtg
= {
714 .vtg_reg
= MENELAUS_LDO_CTRL1
,
717 .mode_reg
= MENELAUS_LDO_CTRL7
,
720 int menelaus_set_vmmc(unsigned int mV
)
725 return menelaus_set_voltage(&vmmc_vtg
, 0, 0, 0);
727 val
= menelaus_get_vtg_value(mV
, vmmc_values
, ARRAY_SIZE(vmmc_values
));
730 return menelaus_set_voltage(&vmmc_vtg
, mV
, val
, 0x02);
732 EXPORT_SYMBOL(menelaus_set_vmmc
);
735 static const struct menelaus_vtg_value vaux_values
[] = {
742 static const struct menelaus_vtg vaux_vtg
= {
744 .vtg_reg
= MENELAUS_LDO_CTRL1
,
747 .mode_reg
= MENELAUS_LDO_CTRL6
,
750 int menelaus_set_vaux(unsigned int mV
)
755 return menelaus_set_voltage(&vaux_vtg
, 0, 0, 0);
757 val
= menelaus_get_vtg_value(mV
, vaux_values
, ARRAY_SIZE(vaux_values
));
760 return menelaus_set_voltage(&vaux_vtg
, mV
, val
, 0x02);
762 EXPORT_SYMBOL(menelaus_set_vaux
);
764 int menelaus_get_slot_pin_states(void)
766 return menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
768 EXPORT_SYMBOL(menelaus_get_slot_pin_states
);
770 int menelaus_set_regulator_sleep(int enable
, u32 val
)
773 struct i2c_client
*c
= the_menelaus
->client
;
775 mutex_lock(&the_menelaus
->lock
);
776 ret
= menelaus_write_reg(MENELAUS_SLEEP_CTRL2
, val
);
780 dev_dbg(&c
->dev
, "regulator sleep configuration: %02x\n", val
);
782 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
785 t
= (GPIO_CTRL_SLPCTLEN
| GPIO3_DIR_INPUT
);
790 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
792 mutex_unlock(&the_menelaus
->lock
);
796 /*-----------------------------------------------------------------------*/
798 /* Handles Menelaus interrupts. Does not run in interrupt context */
799 static void menelaus_work(struct work_struct
*_menelaus
)
801 struct menelaus_chip
*menelaus
=
802 container_of(_menelaus
, struct menelaus_chip
, work
);
803 void (*handler
)(struct menelaus_chip
*menelaus
);
808 isr
= (menelaus_read_reg(MENELAUS_INT_STATUS2
)
809 & ~menelaus
->mask2
) << 8;
810 isr
|= menelaus_read_reg(MENELAUS_INT_STATUS1
)
816 int irq
= fls(isr
) - 1;
819 mutex_lock(&menelaus
->lock
);
820 menelaus_disable_irq(irq
);
821 menelaus_ack_irq(irq
);
822 handler
= menelaus
->handlers
[irq
];
825 menelaus_enable_irq(irq
);
826 mutex_unlock(&menelaus
->lock
);
829 enable_irq(menelaus
->client
->irq
);
833 * We cannot use I2C in interrupt context, so we just schedule work.
835 static irqreturn_t
menelaus_irq(int irq
, void *_menelaus
)
837 struct menelaus_chip
*menelaus
= _menelaus
;
839 disable_irq_nosync(irq
);
840 (void)schedule_work(&menelaus
->work
);
845 /*-----------------------------------------------------------------------*/
848 * The RTC needs to be set once, then it runs on backup battery power.
849 * It supports alarms, including system wake alarms (from some modes);
850 * and 1/second IRQs if requested.
852 #ifdef CONFIG_RTC_DRV_TWL92330
854 #define RTC_CTRL_RTC_EN (1 << 0)
855 #define RTC_CTRL_AL_EN (1 << 1)
856 #define RTC_CTRL_MODE12 (1 << 2)
857 #define RTC_CTRL_EVERY_MASK (3 << 3)
858 #define RTC_CTRL_EVERY_SEC (0 << 3)
859 #define RTC_CTRL_EVERY_MIN (1 << 3)
860 #define RTC_CTRL_EVERY_HR (2 << 3)
861 #define RTC_CTRL_EVERY_DAY (3 << 3)
863 #define RTC_UPDATE_EVERY 0x08
865 #define RTC_HR_PM (1 << 7)
867 static void menelaus_to_time(char *regs
, struct rtc_time
*t
)
869 t
->tm_sec
= bcd2bin(regs
[0]);
870 t
->tm_min
= bcd2bin(regs
[1]);
871 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
872 t
->tm_hour
= bcd2bin(regs
[2] & 0x1f) - 1;
873 if (regs
[2] & RTC_HR_PM
)
876 t
->tm_hour
= bcd2bin(regs
[2] & 0x3f);
877 t
->tm_mday
= bcd2bin(regs
[3]);
878 t
->tm_mon
= bcd2bin(regs
[4]) - 1;
879 t
->tm_year
= bcd2bin(regs
[5]) + 100;
882 static int time_to_menelaus(struct rtc_time
*t
, int regnum
)
886 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_sec
));
890 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_min
));
894 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
895 hour
= t
->tm_hour
+ 1;
897 hour
= RTC_HR_PM
| bin2bcd(hour
- 12);
899 hour
= bin2bcd(hour
);
901 hour
= bin2bcd(t
->tm_hour
);
902 status
= menelaus_write_reg(regnum
++, hour
);
906 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_mday
));
910 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_mon
+ 1));
914 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_year
- 100));
920 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x, err %d\n",
925 static int menelaus_read_time(struct device
*dev
, struct rtc_time
*t
)
927 struct i2c_msg msg
[2];
931 /* block read date and time registers */
932 regs
[0] = MENELAUS_RTC_SEC
;
934 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
939 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
940 msg
[1].flags
= I2C_M_RD
;
941 msg
[1].len
= sizeof(regs
);
944 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
946 dev_err(dev
, "%s error %d\n", "read", status
);
950 menelaus_to_time(regs
, t
);
951 t
->tm_wday
= bcd2bin(regs
[6]);
956 static int menelaus_set_time(struct device
*dev
, struct rtc_time
*t
)
960 /* write date and time registers */
961 status
= time_to_menelaus(t
, MENELAUS_RTC_SEC
);
964 status
= menelaus_write_reg(MENELAUS_RTC_WKDAY
, bin2bcd(t
->tm_wday
));
966 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x "
967 "err %d\n", MENELAUS_RTC_WKDAY
, status
);
971 /* now commit the write */
972 status
= menelaus_write_reg(MENELAUS_RTC_UPDATE
, RTC_UPDATE_EVERY
);
974 dev_err(&the_menelaus
->client
->dev
, "rtc commit time, err %d\n",
980 static int menelaus_read_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
982 struct i2c_msg msg
[2];
986 /* block read alarm registers */
987 regs
[0] = MENELAUS_RTC_AL_SEC
;
989 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
994 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
995 msg
[1].flags
= I2C_M_RD
;
996 msg
[1].len
= sizeof(regs
);
999 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
1001 dev_err(dev
, "%s error %d\n", "alarm read", status
);
1005 menelaus_to_time(regs
, &w
->time
);
1007 w
->enabled
= !!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
);
1009 /* NOTE we *could* check if actually pending... */
1015 static int menelaus_set_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
1019 if (the_menelaus
->client
->irq
<= 0 && w
->enabled
)
1022 /* clear previous alarm enable */
1023 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
) {
1024 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1025 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
1026 the_menelaus
->rtc_control
);
1031 /* write alarm registers */
1032 status
= time_to_menelaus(&w
->time
, MENELAUS_RTC_AL_SEC
);
1036 /* enable alarm if requested */
1038 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
1039 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
1040 the_menelaus
->rtc_control
);
1046 #ifdef CONFIG_RTC_INTF_DEV
1048 static void menelaus_rtc_update_work(struct menelaus_chip
*m
)
1050 /* report 1/sec update */
1051 local_irq_disable();
1052 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_UF
);
1056 static int menelaus_ioctl(struct device
*dev
, unsigned cmd
, unsigned long arg
)
1060 if (the_menelaus
->client
->irq
<= 0)
1061 return -ENOIOCTLCMD
;
1066 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
)
1068 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
1071 if (!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
))
1073 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1075 /* 1/second "update" IRQ */
1077 if (the_menelaus
->uie
)
1079 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1080 status
= menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ
,
1081 menelaus_rtc_update_work
);
1083 the_menelaus
->uie
= 1;
1086 if (!the_menelaus
->uie
)
1088 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1090 the_menelaus
->uie
= 0;
1093 return -ENOIOCTLCMD
;
1095 return menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1099 #define menelaus_ioctl NULL
1102 /* REVISIT no compensation register support ... */
1104 static const struct rtc_class_ops menelaus_rtc_ops
= {
1105 .ioctl
= menelaus_ioctl
,
1106 .read_time
= menelaus_read_time
,
1107 .set_time
= menelaus_set_time
,
1108 .read_alarm
= menelaus_read_alarm
,
1109 .set_alarm
= menelaus_set_alarm
,
1112 static void menelaus_rtc_alarm_work(struct menelaus_chip
*m
)
1115 local_irq_disable();
1116 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_AF
);
1119 /* then disable it; alarms are oneshot */
1120 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1121 menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1124 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1126 int alarm
= (m
->client
->irq
> 0);
1128 /* assume 32KDETEN pin is pulled high */
1129 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL
) & 0x80)) {
1130 dev_dbg(&m
->client
->dev
, "no 32k oscillator\n");
1134 /* support RTC alarm; it can issue wakeups */
1136 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ
,
1137 menelaus_rtc_alarm_work
) < 0) {
1138 dev_err(&m
->client
->dev
, "can't handle RTC alarm\n");
1141 device_init_wakeup(&m
->client
->dev
, 1);
1144 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1145 m
->rtc_control
= menelaus_read_reg(MENELAUS_RTC_CTRL
);
1146 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)
1147 || (m
->rtc_control
& RTC_CTRL_AL_EN
)
1148 || (m
->rtc_control
& RTC_CTRL_EVERY_MASK
)) {
1149 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)) {
1150 dev_warn(&m
->client
->dev
, "rtc clock needs setting\n");
1151 m
->rtc_control
|= RTC_CTRL_RTC_EN
;
1153 m
->rtc_control
&= ~RTC_CTRL_EVERY_MASK
;
1154 m
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1155 menelaus_write_reg(MENELAUS_RTC_CTRL
, m
->rtc_control
);
1158 m
->rtc
= rtc_device_register(DRIVER_NAME
,
1160 &menelaus_rtc_ops
, THIS_MODULE
);
1161 if (IS_ERR(m
->rtc
)) {
1163 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ
);
1164 device_init_wakeup(&m
->client
->dev
, 0);
1166 dev_err(&m
->client
->dev
, "can't register RTC: %d\n",
1167 (int) PTR_ERR(m
->rtc
));
1168 the_menelaus
->rtc
= NULL
;
1174 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1181 /*-----------------------------------------------------------------------*/
1183 static struct i2c_driver menelaus_i2c_driver
;
1185 static int menelaus_probe(struct i2c_client
*client
,
1186 const struct i2c_device_id
*id
)
1188 struct menelaus_chip
*menelaus
;
1191 struct menelaus_platform_data
*menelaus_pdata
=
1192 client
->dev
.platform_data
;
1195 dev_dbg(&client
->dev
, "only one %s for now\n",
1200 menelaus
= kzalloc(sizeof *menelaus
, GFP_KERNEL
);
1204 i2c_set_clientdata(client
, menelaus
);
1206 the_menelaus
= menelaus
;
1207 menelaus
->client
= client
;
1209 /* If a true probe check the device */
1210 rev
= menelaus_read_reg(MENELAUS_REV
);
1212 pr_err(DRIVER_NAME
": device not found");
1217 /* Ack and disable all Menelaus interrupts */
1218 menelaus_write_reg(MENELAUS_INT_ACK1
, 0xff);
1219 menelaus_write_reg(MENELAUS_INT_ACK2
, 0xff);
1220 menelaus_write_reg(MENELAUS_INT_MASK1
, 0xff);
1221 menelaus_write_reg(MENELAUS_INT_MASK2
, 0xff);
1222 menelaus
->mask1
= 0xff;
1223 menelaus
->mask2
= 0xff;
1225 /* Set output buffer strengths */
1226 menelaus_write_reg(MENELAUS_MCT_CTRL1
, 0x73);
1228 if (client
->irq
> 0) {
1229 err
= request_irq(client
->irq
, menelaus_irq
, IRQF_DISABLED
,
1230 DRIVER_NAME
, menelaus
);
1232 dev_dbg(&client
->dev
, "can't get IRQ %d, err %d\n",
1238 mutex_init(&menelaus
->lock
);
1239 INIT_WORK(&menelaus
->work
, menelaus_work
);
1241 pr_info("Menelaus rev %d.%d\n", rev
>> 4, rev
& 0x0f);
1243 val
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
1247 menelaus
->vcore_hw_mode
= 1;
1249 menelaus
->vcore_hw_mode
= 0;
1251 if (menelaus_pdata
!= NULL
&& menelaus_pdata
->late_init
!= NULL
) {
1252 err
= menelaus_pdata
->late_init(&client
->dev
);
1257 menelaus_rtc_init(menelaus
);
1261 free_irq(client
->irq
, menelaus
);
1262 flush_work_sync(&menelaus
->work
);
1268 static int __exit
menelaus_remove(struct i2c_client
*client
)
1270 struct menelaus_chip
*menelaus
= i2c_get_clientdata(client
);
1272 free_irq(client
->irq
, menelaus
);
1273 flush_work_sync(&menelaus
->work
);
1275 the_menelaus
= NULL
;
1279 static const struct i2c_device_id menelaus_id
[] = {
1283 MODULE_DEVICE_TABLE(i2c
, menelaus_id
);
1285 static struct i2c_driver menelaus_i2c_driver
= {
1287 .name
= DRIVER_NAME
,
1289 .probe
= menelaus_probe
,
1290 .remove
= __exit_p(menelaus_remove
),
1291 .id_table
= menelaus_id
,
1294 static int __init
menelaus_init(void)
1298 res
= i2c_add_driver(&menelaus_i2c_driver
);
1300 pr_err(DRIVER_NAME
": driver registration failed\n");
1307 static void __exit
menelaus_exit(void)
1309 i2c_del_driver(&menelaus_i2c_driver
);
1311 /* FIXME: Shutdown menelaus parts that can be shut down */
1314 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1315 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1316 MODULE_LICENSE("GPL");
1318 module_init(menelaus_init
);
1319 module_exit(menelaus_exit
);