1 // SPDX-License-Identifier: GPL-2.0-or-later
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
16 * Added support for controlling VCORE and regulator sleep states,
17 * Amit Kucheria <amit.kucheria@nokia.com>
18 * Copyright (C) 2005, 2006 Nokia Corporation
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h>
25 #include <linux/mutex.h>
26 #include <linux/workqueue.h>
27 #include <linux/delay.h>
28 #include <linux/rtc.h>
29 #include <linux/bcd.h>
30 #include <linux/slab.h>
31 #include <linux/mfd/menelaus.h>
32 #include <linux/gpio.h>
34 #include <asm/mach/irq.h>
37 #define DRIVER_NAME "menelaus"
39 #define MENELAUS_I2C_ADDRESS 0x72
41 #define MENELAUS_REV 0x01
42 #define MENELAUS_VCORE_CTRL1 0x02
43 #define MENELAUS_VCORE_CTRL2 0x03
44 #define MENELAUS_VCORE_CTRL3 0x04
45 #define MENELAUS_VCORE_CTRL4 0x05
46 #define MENELAUS_VCORE_CTRL5 0x06
47 #define MENELAUS_DCDC_CTRL1 0x07
48 #define MENELAUS_DCDC_CTRL2 0x08
49 #define MENELAUS_DCDC_CTRL3 0x09
50 #define MENELAUS_LDO_CTRL1 0x0A
51 #define MENELAUS_LDO_CTRL2 0x0B
52 #define MENELAUS_LDO_CTRL3 0x0C
53 #define MENELAUS_LDO_CTRL4 0x0D
54 #define MENELAUS_LDO_CTRL5 0x0E
55 #define MENELAUS_LDO_CTRL6 0x0F
56 #define MENELAUS_LDO_CTRL7 0x10
57 #define MENELAUS_LDO_CTRL8 0x11
58 #define MENELAUS_SLEEP_CTRL1 0x12
59 #define MENELAUS_SLEEP_CTRL2 0x13
60 #define MENELAUS_DEVICE_OFF 0x14
61 #define MENELAUS_OSC_CTRL 0x15
62 #define MENELAUS_DETECT_CTRL 0x16
63 #define MENELAUS_INT_MASK1 0x17
64 #define MENELAUS_INT_MASK2 0x18
65 #define MENELAUS_INT_STATUS1 0x19
66 #define MENELAUS_INT_STATUS2 0x1A
67 #define MENELAUS_INT_ACK1 0x1B
68 #define MENELAUS_INT_ACK2 0x1C
69 #define MENELAUS_GPIO_CTRL 0x1D
70 #define MENELAUS_GPIO_IN 0x1E
71 #define MENELAUS_GPIO_OUT 0x1F
72 #define MENELAUS_BBSMS 0x20
73 #define MENELAUS_RTC_CTRL 0x21
74 #define MENELAUS_RTC_UPDATE 0x22
75 #define MENELAUS_RTC_SEC 0x23
76 #define MENELAUS_RTC_MIN 0x24
77 #define MENELAUS_RTC_HR 0x25
78 #define MENELAUS_RTC_DAY 0x26
79 #define MENELAUS_RTC_MON 0x27
80 #define MENELAUS_RTC_YR 0x28
81 #define MENELAUS_RTC_WKDAY 0x29
82 #define MENELAUS_RTC_AL_SEC 0x2A
83 #define MENELAUS_RTC_AL_MIN 0x2B
84 #define MENELAUS_RTC_AL_HR 0x2C
85 #define MENELAUS_RTC_AL_DAY 0x2D
86 #define MENELAUS_RTC_AL_MON 0x2E
87 #define MENELAUS_RTC_AL_YR 0x2F
88 #define MENELAUS_RTC_COMP_MSB 0x30
89 #define MENELAUS_RTC_COMP_LSB 0x31
90 #define MENELAUS_S1_PULL_EN 0x32
91 #define MENELAUS_S1_PULL_DIR 0x33
92 #define MENELAUS_S2_PULL_EN 0x34
93 #define MENELAUS_S2_PULL_DIR 0x35
94 #define MENELAUS_MCT_CTRL1 0x36
95 #define MENELAUS_MCT_CTRL2 0x37
96 #define MENELAUS_MCT_CTRL3 0x38
97 #define MENELAUS_MCT_PIN_ST 0x39
98 #define MENELAUS_DEBOUNCE1 0x3A
100 #define IH_MENELAUS_IRQS 12
101 #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
102 #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
103 #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
104 #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
105 #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
106 #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
107 #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
108 #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
109 #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
110 #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
111 #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
112 #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
113 #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
114 #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
115 #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
116 #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
118 /* VCORE_CTRL1 register */
119 #define VCORE_CTRL1_BYP_COMP (1 << 5)
120 #define VCORE_CTRL1_HW_NSW (1 << 7)
122 /* GPIO_CTRL register */
123 #define GPIO_CTRL_SLOTSELEN (1 << 5)
124 #define GPIO_CTRL_SLPCTLEN (1 << 6)
125 #define GPIO1_DIR_INPUT (1 << 0)
126 #define GPIO2_DIR_INPUT (1 << 1)
127 #define GPIO3_DIR_INPUT (1 << 2)
129 /* MCT_CTRL1 register */
130 #define MCT_CTRL1_S1_CMD_OD (1 << 2)
131 #define MCT_CTRL1_S2_CMD_OD (1 << 3)
133 /* MCT_CTRL2 register */
134 #define MCT_CTRL2_VS2_SEL_D0 (1 << 0)
135 #define MCT_CTRL2_VS2_SEL_D1 (1 << 1)
136 #define MCT_CTRL2_S1CD_BUFEN (1 << 4)
137 #define MCT_CTRL2_S2CD_BUFEN (1 << 5)
138 #define MCT_CTRL2_S1CD_DBEN (1 << 6)
139 #define MCT_CTRL2_S2CD_BEN (1 << 7)
141 /* MCT_CTRL3 register */
142 #define MCT_CTRL3_SLOT1_EN (1 << 0)
143 #define MCT_CTRL3_SLOT2_EN (1 << 1)
144 #define MCT_CTRL3_S1_AUTO_EN (1 << 2)
145 #define MCT_CTRL3_S2_AUTO_EN (1 << 3)
147 /* MCT_PIN_ST register */
148 #define MCT_PIN_ST_S1_CD_ST (1 << 0)
149 #define MCT_PIN_ST_S2_CD_ST (1 << 1)
151 static void menelaus_work(struct work_struct
*_menelaus
);
153 struct menelaus_chip
{
155 struct i2c_client
*client
;
156 struct work_struct work
;
157 #ifdef CONFIG_RTC_DRV_TWL92330
158 struct rtc_device
*rtc
;
162 unsigned vcore_hw_mode
:1;
164 void (*handlers
[16])(struct menelaus_chip
*);
165 void (*mmc_callback
)(void *data
, u8 mask
);
166 void *mmc_callback_data
;
169 static struct menelaus_chip
*the_menelaus
;
171 static int menelaus_write_reg(int reg
, u8 value
)
173 int val
= i2c_smbus_write_byte_data(the_menelaus
->client
, reg
, value
);
176 pr_err(DRIVER_NAME
": write error");
183 static int menelaus_read_reg(int reg
)
185 int val
= i2c_smbus_read_byte_data(the_menelaus
->client
, reg
);
188 pr_err(DRIVER_NAME
": read error");
193 static int menelaus_enable_irq(int irq
)
197 the_menelaus
->mask2
&= ~(1 << irq
);
198 return menelaus_write_reg(MENELAUS_INT_MASK2
,
199 the_menelaus
->mask2
);
201 the_menelaus
->mask1
&= ~(1 << irq
);
202 return menelaus_write_reg(MENELAUS_INT_MASK1
,
203 the_menelaus
->mask1
);
207 static int menelaus_disable_irq(int irq
)
211 the_menelaus
->mask2
|= (1 << irq
);
212 return menelaus_write_reg(MENELAUS_INT_MASK2
,
213 the_menelaus
->mask2
);
215 the_menelaus
->mask1
|= (1 << irq
);
216 return menelaus_write_reg(MENELAUS_INT_MASK1
,
217 the_menelaus
->mask1
);
221 static int menelaus_ack_irq(int irq
)
224 return menelaus_write_reg(MENELAUS_INT_ACK2
, 1 << (irq
- 8));
226 return menelaus_write_reg(MENELAUS_INT_ACK1
, 1 << irq
);
229 /* Adds a handler for an interrupt. Does not run in interrupt context */
230 static int menelaus_add_irq_work(int irq
,
231 void (*handler
)(struct menelaus_chip
*))
235 mutex_lock(&the_menelaus
->lock
);
236 the_menelaus
->handlers
[irq
] = handler
;
237 ret
= menelaus_enable_irq(irq
);
238 mutex_unlock(&the_menelaus
->lock
);
243 /* Removes handler for an interrupt */
244 static int menelaus_remove_irq_work(int irq
)
248 mutex_lock(&the_menelaus
->lock
);
249 ret
= menelaus_disable_irq(irq
);
250 the_menelaus
->handlers
[irq
] = NULL
;
251 mutex_unlock(&the_menelaus
->lock
);
257 * Gets scheduled when a card detect interrupt happens. Note that in some cases
258 * this line is wired to card cover switch rather than the card detect switch
259 * in each slot. In this case the cards are not seen by menelaus.
260 * FIXME: Add handling for D1 too
262 static void menelaus_mmc_cd_work(struct menelaus_chip
*menelaus_hw
)
265 unsigned char card_mask
= 0;
267 reg
= menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
272 card_mask
|= MCT_PIN_ST_S1_CD_ST
;
275 card_mask
|= MCT_PIN_ST_S2_CD_ST
;
277 if (menelaus_hw
->mmc_callback
)
278 menelaus_hw
->mmc_callback(menelaus_hw
->mmc_callback_data
,
283 * Toggles the MMC slots between open-drain and push-pull mode.
285 int menelaus_set_mmc_opendrain(int slot
, int enable
)
289 if (slot
!= 1 && slot
!= 2)
291 mutex_lock(&the_menelaus
->lock
);
292 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL1
);
294 mutex_unlock(&the_menelaus
->lock
);
300 val
|= MCT_CTRL1_S1_CMD_OD
;
302 val
&= ~MCT_CTRL1_S1_CMD_OD
;
305 val
|= MCT_CTRL1_S2_CMD_OD
;
307 val
&= ~MCT_CTRL1_S2_CMD_OD
;
309 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL1
, val
);
310 mutex_unlock(&the_menelaus
->lock
);
314 EXPORT_SYMBOL(menelaus_set_mmc_opendrain
);
316 int menelaus_set_slot_sel(int enable
)
320 mutex_lock(&the_menelaus
->lock
);
321 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
324 ret
|= GPIO2_DIR_INPUT
;
326 ret
|= GPIO_CTRL_SLOTSELEN
;
328 ret
&= ~GPIO_CTRL_SLOTSELEN
;
329 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
331 mutex_unlock(&the_menelaus
->lock
);
334 EXPORT_SYMBOL(menelaus_set_slot_sel
);
336 int menelaus_set_mmc_slot(int slot
, int enable
, int power
, int cd_en
)
340 if (slot
!= 1 && slot
!= 2)
345 mutex_lock(&the_menelaus
->lock
);
347 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
353 val
|= MCT_CTRL2_S1CD_BUFEN
| MCT_CTRL2_S1CD_DBEN
;
355 val
&= ~(MCT_CTRL2_S1CD_BUFEN
| MCT_CTRL2_S1CD_DBEN
);
358 val
|= MCT_CTRL2_S2CD_BUFEN
| MCT_CTRL2_S2CD_BEN
;
360 val
&= ~(MCT_CTRL2_S2CD_BUFEN
| MCT_CTRL2_S2CD_BEN
);
362 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, val
);
366 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL3
);
372 val
|= MCT_CTRL3_SLOT1_EN
;
374 val
&= ~MCT_CTRL3_SLOT1_EN
;
379 val
|= MCT_CTRL3_SLOT2_EN
;
381 val
&= ~MCT_CTRL3_SLOT2_EN
;
382 b
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
383 b
&= ~(MCT_CTRL2_VS2_SEL_D0
| MCT_CTRL2_VS2_SEL_D1
);
385 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, b
);
389 /* Disable autonomous shutdown */
390 val
&= ~(MCT_CTRL3_S1_AUTO_EN
| MCT_CTRL3_S2_AUTO_EN
);
391 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL3
, val
);
393 mutex_unlock(&the_menelaus
->lock
);
396 EXPORT_SYMBOL(menelaus_set_mmc_slot
);
398 int menelaus_register_mmc_callback(void (*callback
)(void *data
, u8 card_mask
),
403 the_menelaus
->mmc_callback_data
= data
;
404 the_menelaus
->mmc_callback
= callback
;
405 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ
,
406 menelaus_mmc_cd_work
);
409 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ
,
410 menelaus_mmc_cd_work
);
413 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ
,
414 menelaus_mmc_cd_work
);
417 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ
,
418 menelaus_mmc_cd_work
);
422 EXPORT_SYMBOL(menelaus_register_mmc_callback
);
424 void menelaus_unregister_mmc_callback(void)
426 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ
);
427 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ
);
428 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ
);
429 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ
);
431 the_menelaus
->mmc_callback
= NULL
;
432 the_menelaus
->mmc_callback_data
= NULL
;
434 EXPORT_SYMBOL(menelaus_unregister_mmc_callback
);
436 struct menelaus_vtg
{
444 struct menelaus_vtg_value
{
449 static int menelaus_set_voltage(const struct menelaus_vtg
*vtg
, int mV
,
450 int vtg_val
, int mode
)
453 struct i2c_client
*c
= the_menelaus
->client
;
455 mutex_lock(&the_menelaus
->lock
);
457 ret
= menelaus_read_reg(vtg
->vtg_reg
);
460 val
= ret
& ~(((1 << vtg
->vtg_bits
) - 1) << vtg
->vtg_shift
);
461 val
|= vtg_val
<< vtg
->vtg_shift
;
463 dev_dbg(&c
->dev
, "Setting voltage '%s'"
464 "to %d mV (reg 0x%02x, val 0x%02x)\n",
465 vtg
->name
, mV
, vtg
->vtg_reg
, val
);
467 ret
= menelaus_write_reg(vtg
->vtg_reg
, val
);
470 ret
= menelaus_write_reg(vtg
->mode_reg
, mode
);
472 mutex_unlock(&the_menelaus
->lock
);
474 /* Wait for voltage to stabilize */
480 static int menelaus_get_vtg_value(int vtg
, const struct menelaus_vtg_value
*tbl
,
485 for (i
= 0; i
< n
; i
++, tbl
++)
492 * Vcore can be programmed in two ways:
493 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
494 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
497 * Call correct 'set' function accordingly
500 static const struct menelaus_vtg_value vcore_values
[] = {
522 int menelaus_set_vcore_hw(unsigned int roof_mV
, unsigned int floor_mV
)
524 int fval
, rval
, val
, ret
;
525 struct i2c_client
*c
= the_menelaus
->client
;
527 rval
= menelaus_get_vtg_value(roof_mV
, vcore_values
,
528 ARRAY_SIZE(vcore_values
));
531 fval
= menelaus_get_vtg_value(floor_mV
, vcore_values
,
532 ARRAY_SIZE(vcore_values
));
536 dev_dbg(&c
->dev
, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
539 mutex_lock(&the_menelaus
->lock
);
540 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL3
, fval
);
543 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL4
, rval
);
546 if (!the_menelaus
->vcore_hw_mode
) {
547 val
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
548 /* HW mode, turn OFF byte comparator */
549 val
|= (VCORE_CTRL1_HW_NSW
| VCORE_CTRL1_BYP_COMP
);
550 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL1
, val
);
551 the_menelaus
->vcore_hw_mode
= 1;
555 mutex_unlock(&the_menelaus
->lock
);
559 static const struct menelaus_vtg vmem_vtg
= {
561 .vtg_reg
= MENELAUS_LDO_CTRL1
,
564 .mode_reg
= MENELAUS_LDO_CTRL3
,
567 static const struct menelaus_vtg_value vmem_values
[] = {
574 int menelaus_set_vmem(unsigned int mV
)
579 return menelaus_set_voltage(&vmem_vtg
, 0, 0, 0);
581 val
= menelaus_get_vtg_value(mV
, vmem_values
, ARRAY_SIZE(vmem_values
));
584 return menelaus_set_voltage(&vmem_vtg
, mV
, val
, 0x02);
586 EXPORT_SYMBOL(menelaus_set_vmem
);
588 static const struct menelaus_vtg vio_vtg
= {
590 .vtg_reg
= MENELAUS_LDO_CTRL1
,
593 .mode_reg
= MENELAUS_LDO_CTRL4
,
596 static const struct menelaus_vtg_value vio_values
[] = {
603 int menelaus_set_vio(unsigned int mV
)
608 return menelaus_set_voltage(&vio_vtg
, 0, 0, 0);
610 val
= menelaus_get_vtg_value(mV
, vio_values
, ARRAY_SIZE(vio_values
));
613 return menelaus_set_voltage(&vio_vtg
, mV
, val
, 0x02);
615 EXPORT_SYMBOL(menelaus_set_vio
);
617 static const struct menelaus_vtg_value vdcdc_values
[] = {
628 static const struct menelaus_vtg vdcdc2_vtg
= {
630 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
633 .mode_reg
= MENELAUS_DCDC_CTRL2
,
636 static const struct menelaus_vtg vdcdc3_vtg
= {
638 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
641 .mode_reg
= MENELAUS_DCDC_CTRL3
,
644 int menelaus_set_vdcdc(int dcdc
, unsigned int mV
)
646 const struct menelaus_vtg
*vtg
;
649 if (dcdc
!= 2 && dcdc
!= 3)
657 return menelaus_set_voltage(vtg
, 0, 0, 0);
659 val
= menelaus_get_vtg_value(mV
, vdcdc_values
,
660 ARRAY_SIZE(vdcdc_values
));
663 return menelaus_set_voltage(vtg
, mV
, val
, 0x03);
666 static const struct menelaus_vtg_value vmmc_values
[] = {
673 static const struct menelaus_vtg vmmc_vtg
= {
675 .vtg_reg
= MENELAUS_LDO_CTRL1
,
678 .mode_reg
= MENELAUS_LDO_CTRL7
,
681 int menelaus_set_vmmc(unsigned int mV
)
686 return menelaus_set_voltage(&vmmc_vtg
, 0, 0, 0);
688 val
= menelaus_get_vtg_value(mV
, vmmc_values
, ARRAY_SIZE(vmmc_values
));
691 return menelaus_set_voltage(&vmmc_vtg
, mV
, val
, 0x02);
693 EXPORT_SYMBOL(menelaus_set_vmmc
);
696 static const struct menelaus_vtg_value vaux_values
[] = {
703 static const struct menelaus_vtg vaux_vtg
= {
705 .vtg_reg
= MENELAUS_LDO_CTRL1
,
708 .mode_reg
= MENELAUS_LDO_CTRL6
,
711 int menelaus_set_vaux(unsigned int mV
)
716 return menelaus_set_voltage(&vaux_vtg
, 0, 0, 0);
718 val
= menelaus_get_vtg_value(mV
, vaux_values
, ARRAY_SIZE(vaux_values
));
721 return menelaus_set_voltage(&vaux_vtg
, mV
, val
, 0x02);
723 EXPORT_SYMBOL(menelaus_set_vaux
);
725 int menelaus_get_slot_pin_states(void)
727 return menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
729 EXPORT_SYMBOL(menelaus_get_slot_pin_states
);
731 int menelaus_set_regulator_sleep(int enable
, u32 val
)
734 struct i2c_client
*c
= the_menelaus
->client
;
736 mutex_lock(&the_menelaus
->lock
);
737 ret
= menelaus_write_reg(MENELAUS_SLEEP_CTRL2
, val
);
741 dev_dbg(&c
->dev
, "regulator sleep configuration: %02x\n", val
);
743 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
746 t
= (GPIO_CTRL_SLPCTLEN
| GPIO3_DIR_INPUT
);
751 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
753 mutex_unlock(&the_menelaus
->lock
);
757 /*-----------------------------------------------------------------------*/
759 /* Handles Menelaus interrupts. Does not run in interrupt context */
760 static void menelaus_work(struct work_struct
*_menelaus
)
762 struct menelaus_chip
*menelaus
=
763 container_of(_menelaus
, struct menelaus_chip
, work
);
764 void (*handler
)(struct menelaus_chip
*menelaus
);
769 isr
= (menelaus_read_reg(MENELAUS_INT_STATUS2
)
770 & ~menelaus
->mask2
) << 8;
771 isr
|= menelaus_read_reg(MENELAUS_INT_STATUS1
)
777 int irq
= fls(isr
) - 1;
780 mutex_lock(&menelaus
->lock
);
781 menelaus_disable_irq(irq
);
782 menelaus_ack_irq(irq
);
783 handler
= menelaus
->handlers
[irq
];
786 menelaus_enable_irq(irq
);
787 mutex_unlock(&menelaus
->lock
);
790 enable_irq(menelaus
->client
->irq
);
794 * We cannot use I2C in interrupt context, so we just schedule work.
796 static irqreturn_t
menelaus_irq(int irq
, void *_menelaus
)
798 struct menelaus_chip
*menelaus
= _menelaus
;
800 disable_irq_nosync(irq
);
801 (void)schedule_work(&menelaus
->work
);
806 /*-----------------------------------------------------------------------*/
809 * The RTC needs to be set once, then it runs on backup battery power.
810 * It supports alarms, including system wake alarms (from some modes);
811 * and 1/second IRQs if requested.
813 #ifdef CONFIG_RTC_DRV_TWL92330
815 #define RTC_CTRL_RTC_EN (1 << 0)
816 #define RTC_CTRL_AL_EN (1 << 1)
817 #define RTC_CTRL_MODE12 (1 << 2)
818 #define RTC_CTRL_EVERY_MASK (3 << 3)
819 #define RTC_CTRL_EVERY_SEC (0 << 3)
820 #define RTC_CTRL_EVERY_MIN (1 << 3)
821 #define RTC_CTRL_EVERY_HR (2 << 3)
822 #define RTC_CTRL_EVERY_DAY (3 << 3)
824 #define RTC_UPDATE_EVERY 0x08
826 #define RTC_HR_PM (1 << 7)
828 static void menelaus_to_time(char *regs
, struct rtc_time
*t
)
830 t
->tm_sec
= bcd2bin(regs
[0]);
831 t
->tm_min
= bcd2bin(regs
[1]);
832 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
833 t
->tm_hour
= bcd2bin(regs
[2] & 0x1f) - 1;
834 if (regs
[2] & RTC_HR_PM
)
837 t
->tm_hour
= bcd2bin(regs
[2] & 0x3f);
838 t
->tm_mday
= bcd2bin(regs
[3]);
839 t
->tm_mon
= bcd2bin(regs
[4]) - 1;
840 t
->tm_year
= bcd2bin(regs
[5]) + 100;
843 static int time_to_menelaus(struct rtc_time
*t
, int regnum
)
847 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_sec
));
851 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_min
));
855 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
856 hour
= t
->tm_hour
+ 1;
858 hour
= RTC_HR_PM
| bin2bcd(hour
- 12);
860 hour
= bin2bcd(hour
);
862 hour
= bin2bcd(t
->tm_hour
);
863 status
= menelaus_write_reg(regnum
++, hour
);
867 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_mday
));
871 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_mon
+ 1));
875 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_year
- 100));
881 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x, err %d\n",
886 static int menelaus_read_time(struct device
*dev
, struct rtc_time
*t
)
888 struct i2c_msg msg
[2];
892 /* block read date and time registers */
893 regs
[0] = MENELAUS_RTC_SEC
;
895 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
900 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
901 msg
[1].flags
= I2C_M_RD
;
902 msg
[1].len
= sizeof(regs
);
905 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
907 dev_err(dev
, "%s error %d\n", "read", status
);
911 menelaus_to_time(regs
, t
);
912 t
->tm_wday
= bcd2bin(regs
[6]);
917 static int menelaus_set_time(struct device
*dev
, struct rtc_time
*t
)
921 /* write date and time registers */
922 status
= time_to_menelaus(t
, MENELAUS_RTC_SEC
);
925 status
= menelaus_write_reg(MENELAUS_RTC_WKDAY
, bin2bcd(t
->tm_wday
));
927 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x "
928 "err %d\n", MENELAUS_RTC_WKDAY
, status
);
932 /* now commit the write */
933 status
= menelaus_write_reg(MENELAUS_RTC_UPDATE
, RTC_UPDATE_EVERY
);
935 dev_err(&the_menelaus
->client
->dev
, "rtc commit time, err %d\n",
941 static int menelaus_read_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
943 struct i2c_msg msg
[2];
947 /* block read alarm registers */
948 regs
[0] = MENELAUS_RTC_AL_SEC
;
950 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
955 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
956 msg
[1].flags
= I2C_M_RD
;
957 msg
[1].len
= sizeof(regs
);
960 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
962 dev_err(dev
, "%s error %d\n", "alarm read", status
);
966 menelaus_to_time(regs
, &w
->time
);
968 w
->enabled
= !!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
);
970 /* NOTE we *could* check if actually pending... */
976 static int menelaus_set_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
980 if (the_menelaus
->client
->irq
<= 0 && w
->enabled
)
983 /* clear previous alarm enable */
984 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
) {
985 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
986 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
987 the_menelaus
->rtc_control
);
992 /* write alarm registers */
993 status
= time_to_menelaus(&w
->time
, MENELAUS_RTC_AL_SEC
);
997 /* enable alarm if requested */
999 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
1000 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
1001 the_menelaus
->rtc_control
);
1007 #ifdef CONFIG_RTC_INTF_DEV
1009 static void menelaus_rtc_update_work(struct menelaus_chip
*m
)
1011 /* report 1/sec update */
1012 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_UF
);
1015 static int menelaus_ioctl(struct device
*dev
, unsigned cmd
, unsigned long arg
)
1019 if (the_menelaus
->client
->irq
<= 0)
1020 return -ENOIOCTLCMD
;
1025 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
)
1027 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
1030 if (!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
))
1032 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1034 /* 1/second "update" IRQ */
1036 if (the_menelaus
->uie
)
1038 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1039 status
= menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ
,
1040 menelaus_rtc_update_work
);
1042 the_menelaus
->uie
= 1;
1045 if (!the_menelaus
->uie
)
1047 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1049 the_menelaus
->uie
= 0;
1052 return -ENOIOCTLCMD
;
1054 return menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1058 #define menelaus_ioctl NULL
1061 /* REVISIT no compensation register support ... */
1063 static const struct rtc_class_ops menelaus_rtc_ops
= {
1064 .ioctl
= menelaus_ioctl
,
1065 .read_time
= menelaus_read_time
,
1066 .set_time
= menelaus_set_time
,
1067 .read_alarm
= menelaus_read_alarm
,
1068 .set_alarm
= menelaus_set_alarm
,
1071 static void menelaus_rtc_alarm_work(struct menelaus_chip
*m
)
1074 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_AF
);
1076 /* then disable it; alarms are oneshot */
1077 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1078 menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1081 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1083 int alarm
= (m
->client
->irq
> 0);
1086 /* assume 32KDETEN pin is pulled high */
1087 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL
) & 0x80)) {
1088 dev_dbg(&m
->client
->dev
, "no 32k oscillator\n");
1092 m
->rtc
= devm_rtc_allocate_device(&m
->client
->dev
);
1096 m
->rtc
->ops
= &menelaus_rtc_ops
;
1098 /* support RTC alarm; it can issue wakeups */
1100 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ
,
1101 menelaus_rtc_alarm_work
) < 0) {
1102 dev_err(&m
->client
->dev
, "can't handle RTC alarm\n");
1105 device_init_wakeup(&m
->client
->dev
, 1);
1108 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1109 m
->rtc_control
= menelaus_read_reg(MENELAUS_RTC_CTRL
);
1110 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)
1111 || (m
->rtc_control
& RTC_CTRL_AL_EN
)
1112 || (m
->rtc_control
& RTC_CTRL_EVERY_MASK
)) {
1113 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)) {
1114 dev_warn(&m
->client
->dev
, "rtc clock needs setting\n");
1115 m
->rtc_control
|= RTC_CTRL_RTC_EN
;
1117 m
->rtc_control
&= ~RTC_CTRL_EVERY_MASK
;
1118 m
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1119 menelaus_write_reg(MENELAUS_RTC_CTRL
, m
->rtc_control
);
1122 err
= rtc_register_device(m
->rtc
);
1125 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ
);
1126 device_init_wakeup(&m
->client
->dev
, 0);
1128 the_menelaus
->rtc
= NULL
;
1134 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1141 /*-----------------------------------------------------------------------*/
1143 static struct i2c_driver menelaus_i2c_driver
;
1145 static int menelaus_probe(struct i2c_client
*client
,
1146 const struct i2c_device_id
*id
)
1148 struct menelaus_chip
*menelaus
;
1151 struct menelaus_platform_data
*menelaus_pdata
=
1152 dev_get_platdata(&client
->dev
);
1155 dev_dbg(&client
->dev
, "only one %s for now\n",
1160 menelaus
= devm_kzalloc(&client
->dev
, sizeof(*menelaus
), GFP_KERNEL
);
1164 i2c_set_clientdata(client
, menelaus
);
1166 the_menelaus
= menelaus
;
1167 menelaus
->client
= client
;
1169 /* If a true probe check the device */
1170 rev
= menelaus_read_reg(MENELAUS_REV
);
1172 pr_err(DRIVER_NAME
": device not found");
1176 /* Ack and disable all Menelaus interrupts */
1177 menelaus_write_reg(MENELAUS_INT_ACK1
, 0xff);
1178 menelaus_write_reg(MENELAUS_INT_ACK2
, 0xff);
1179 menelaus_write_reg(MENELAUS_INT_MASK1
, 0xff);
1180 menelaus_write_reg(MENELAUS_INT_MASK2
, 0xff);
1181 menelaus
->mask1
= 0xff;
1182 menelaus
->mask2
= 0xff;
1184 /* Set output buffer strengths */
1185 menelaus_write_reg(MENELAUS_MCT_CTRL1
, 0x73);
1187 if (client
->irq
> 0) {
1188 err
= request_irq(client
->irq
, menelaus_irq
, 0,
1189 DRIVER_NAME
, menelaus
);
1191 dev_dbg(&client
->dev
, "can't get IRQ %d, err %d\n",
1197 mutex_init(&menelaus
->lock
);
1198 INIT_WORK(&menelaus
->work
, menelaus_work
);
1200 pr_info("Menelaus rev %d.%d\n", rev
>> 4, rev
& 0x0f);
1202 err
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
1205 if (err
& VCORE_CTRL1_HW_NSW
)
1206 menelaus
->vcore_hw_mode
= 1;
1208 menelaus
->vcore_hw_mode
= 0;
1210 if (menelaus_pdata
!= NULL
&& menelaus_pdata
->late_init
!= NULL
) {
1211 err
= menelaus_pdata
->late_init(&client
->dev
);
1216 menelaus_rtc_init(menelaus
);
1220 free_irq(client
->irq
, menelaus
);
1221 flush_work(&menelaus
->work
);
1225 static int menelaus_remove(struct i2c_client
*client
)
1227 struct menelaus_chip
*menelaus
= i2c_get_clientdata(client
);
1229 free_irq(client
->irq
, menelaus
);
1230 flush_work(&menelaus
->work
);
1231 the_menelaus
= NULL
;
1235 static const struct i2c_device_id menelaus_id
[] = {
1239 MODULE_DEVICE_TABLE(i2c
, menelaus_id
);
1241 static struct i2c_driver menelaus_i2c_driver
= {
1243 .name
= DRIVER_NAME
,
1245 .probe
= menelaus_probe
,
1246 .remove
= menelaus_remove
,
1247 .id_table
= menelaus_id
,
1250 module_i2c_driver(menelaus_i2c_driver
);
1252 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1253 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1254 MODULE_LICENSE("GPL");