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>
33 #include <asm/mach/irq.h>
36 #define DRIVER_NAME "menelaus"
38 #define MENELAUS_I2C_ADDRESS 0x72
40 #define MENELAUS_REV 0x01
41 #define MENELAUS_VCORE_CTRL1 0x02
42 #define MENELAUS_VCORE_CTRL2 0x03
43 #define MENELAUS_VCORE_CTRL3 0x04
44 #define MENELAUS_VCORE_CTRL4 0x05
45 #define MENELAUS_VCORE_CTRL5 0x06
46 #define MENELAUS_DCDC_CTRL1 0x07
47 #define MENELAUS_DCDC_CTRL2 0x08
48 #define MENELAUS_DCDC_CTRL3 0x09
49 #define MENELAUS_LDO_CTRL1 0x0A
50 #define MENELAUS_LDO_CTRL2 0x0B
51 #define MENELAUS_LDO_CTRL3 0x0C
52 #define MENELAUS_LDO_CTRL4 0x0D
53 #define MENELAUS_LDO_CTRL5 0x0E
54 #define MENELAUS_LDO_CTRL6 0x0F
55 #define MENELAUS_LDO_CTRL7 0x10
56 #define MENELAUS_LDO_CTRL8 0x11
57 #define MENELAUS_SLEEP_CTRL1 0x12
58 #define MENELAUS_SLEEP_CTRL2 0x13
59 #define MENELAUS_DEVICE_OFF 0x14
60 #define MENELAUS_OSC_CTRL 0x15
61 #define MENELAUS_DETECT_CTRL 0x16
62 #define MENELAUS_INT_MASK1 0x17
63 #define MENELAUS_INT_MASK2 0x18
64 #define MENELAUS_INT_STATUS1 0x19
65 #define MENELAUS_INT_STATUS2 0x1A
66 #define MENELAUS_INT_ACK1 0x1B
67 #define MENELAUS_INT_ACK2 0x1C
68 #define MENELAUS_GPIO_CTRL 0x1D
69 #define MENELAUS_GPIO_IN 0x1E
70 #define MENELAUS_GPIO_OUT 0x1F
71 #define MENELAUS_BBSMS 0x20
72 #define MENELAUS_RTC_CTRL 0x21
73 #define MENELAUS_RTC_UPDATE 0x22
74 #define MENELAUS_RTC_SEC 0x23
75 #define MENELAUS_RTC_MIN 0x24
76 #define MENELAUS_RTC_HR 0x25
77 #define MENELAUS_RTC_DAY 0x26
78 #define MENELAUS_RTC_MON 0x27
79 #define MENELAUS_RTC_YR 0x28
80 #define MENELAUS_RTC_WKDAY 0x29
81 #define MENELAUS_RTC_AL_SEC 0x2A
82 #define MENELAUS_RTC_AL_MIN 0x2B
83 #define MENELAUS_RTC_AL_HR 0x2C
84 #define MENELAUS_RTC_AL_DAY 0x2D
85 #define MENELAUS_RTC_AL_MON 0x2E
86 #define MENELAUS_RTC_AL_YR 0x2F
87 #define MENELAUS_RTC_COMP_MSB 0x30
88 #define MENELAUS_RTC_COMP_LSB 0x31
89 #define MENELAUS_S1_PULL_EN 0x32
90 #define MENELAUS_S1_PULL_DIR 0x33
91 #define MENELAUS_S2_PULL_EN 0x34
92 #define MENELAUS_S2_PULL_DIR 0x35
93 #define MENELAUS_MCT_CTRL1 0x36
94 #define MENELAUS_MCT_CTRL2 0x37
95 #define MENELAUS_MCT_CTRL3 0x38
96 #define MENELAUS_MCT_PIN_ST 0x39
97 #define MENELAUS_DEBOUNCE1 0x3A
99 #define IH_MENELAUS_IRQS 12
100 #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
101 #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
102 #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
103 #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
104 #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
105 #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
106 #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
107 #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
108 #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
109 #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
110 #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
111 #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
112 #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
113 #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
114 #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
115 #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
117 /* VCORE_CTRL1 register */
118 #define VCORE_CTRL1_BYP_COMP (1 << 5)
119 #define VCORE_CTRL1_HW_NSW (1 << 7)
121 /* GPIO_CTRL register */
122 #define GPIO_CTRL_SLOTSELEN (1 << 5)
123 #define GPIO_CTRL_SLPCTLEN (1 << 6)
124 #define GPIO1_DIR_INPUT (1 << 0)
125 #define GPIO2_DIR_INPUT (1 << 1)
126 #define GPIO3_DIR_INPUT (1 << 2)
128 /* MCT_CTRL1 register */
129 #define MCT_CTRL1_S1_CMD_OD (1 << 2)
130 #define MCT_CTRL1_S2_CMD_OD (1 << 3)
132 /* MCT_CTRL2 register */
133 #define MCT_CTRL2_VS2_SEL_D0 (1 << 0)
134 #define MCT_CTRL2_VS2_SEL_D1 (1 << 1)
135 #define MCT_CTRL2_S1CD_BUFEN (1 << 4)
136 #define MCT_CTRL2_S2CD_BUFEN (1 << 5)
137 #define MCT_CTRL2_S1CD_DBEN (1 << 6)
138 #define MCT_CTRL2_S2CD_BEN (1 << 7)
140 /* MCT_CTRL3 register */
141 #define MCT_CTRL3_SLOT1_EN (1 << 0)
142 #define MCT_CTRL3_SLOT2_EN (1 << 1)
143 #define MCT_CTRL3_S1_AUTO_EN (1 << 2)
144 #define MCT_CTRL3_S2_AUTO_EN (1 << 3)
146 /* MCT_PIN_ST register */
147 #define MCT_PIN_ST_S1_CD_ST (1 << 0)
148 #define MCT_PIN_ST_S2_CD_ST (1 << 1)
150 static void menelaus_work(struct work_struct
*_menelaus
);
152 struct menelaus_chip
{
154 struct i2c_client
*client
;
155 struct work_struct work
;
156 #ifdef CONFIG_RTC_DRV_TWL92330
157 struct rtc_device
*rtc
;
161 unsigned vcore_hw_mode
:1;
163 void (*handlers
[16])(struct menelaus_chip
*);
164 void (*mmc_callback
)(void *data
, u8 mask
);
165 void *mmc_callback_data
;
168 static struct menelaus_chip
*the_menelaus
;
170 static int menelaus_write_reg(int reg
, u8 value
)
172 int val
= i2c_smbus_write_byte_data(the_menelaus
->client
, reg
, value
);
175 pr_err(DRIVER_NAME
": write error");
182 static int menelaus_read_reg(int reg
)
184 int val
= i2c_smbus_read_byte_data(the_menelaus
->client
, reg
);
187 pr_err(DRIVER_NAME
": read error");
192 static int menelaus_enable_irq(int irq
)
196 the_menelaus
->mask2
&= ~(1 << irq
);
197 return menelaus_write_reg(MENELAUS_INT_MASK2
,
198 the_menelaus
->mask2
);
200 the_menelaus
->mask1
&= ~(1 << irq
);
201 return menelaus_write_reg(MENELAUS_INT_MASK1
,
202 the_menelaus
->mask1
);
206 static int menelaus_disable_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_ack_irq(int irq
)
223 return menelaus_write_reg(MENELAUS_INT_ACK2
, 1 << (irq
- 8));
225 return menelaus_write_reg(MENELAUS_INT_ACK1
, 1 << irq
);
228 /* Adds a handler for an interrupt. Does not run in interrupt context */
229 static int menelaus_add_irq_work(int irq
,
230 void (*handler
)(struct menelaus_chip
*))
234 mutex_lock(&the_menelaus
->lock
);
235 the_menelaus
->handlers
[irq
] = handler
;
236 ret
= menelaus_enable_irq(irq
);
237 mutex_unlock(&the_menelaus
->lock
);
242 /* Removes handler for an interrupt */
243 static int menelaus_remove_irq_work(int irq
)
247 mutex_lock(&the_menelaus
->lock
);
248 ret
= menelaus_disable_irq(irq
);
249 the_menelaus
->handlers
[irq
] = NULL
;
250 mutex_unlock(&the_menelaus
->lock
);
256 * Gets scheduled when a card detect interrupt happens. Note that in some cases
257 * this line is wired to card cover switch rather than the card detect switch
258 * in each slot. In this case the cards are not seen by menelaus.
259 * FIXME: Add handling for D1 too
261 static void menelaus_mmc_cd_work(struct menelaus_chip
*menelaus_hw
)
264 unsigned char card_mask
= 0;
266 reg
= menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
271 card_mask
|= MCT_PIN_ST_S1_CD_ST
;
274 card_mask
|= MCT_PIN_ST_S2_CD_ST
;
276 if (menelaus_hw
->mmc_callback
)
277 menelaus_hw
->mmc_callback(menelaus_hw
->mmc_callback_data
,
282 * Toggles the MMC slots between open-drain and push-pull mode.
284 int menelaus_set_mmc_opendrain(int slot
, int enable
)
288 if (slot
!= 1 && slot
!= 2)
290 mutex_lock(&the_menelaus
->lock
);
291 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL1
);
293 mutex_unlock(&the_menelaus
->lock
);
299 val
|= MCT_CTRL1_S1_CMD_OD
;
301 val
&= ~MCT_CTRL1_S1_CMD_OD
;
304 val
|= MCT_CTRL1_S2_CMD_OD
;
306 val
&= ~MCT_CTRL1_S2_CMD_OD
;
308 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL1
, val
);
309 mutex_unlock(&the_menelaus
->lock
);
313 EXPORT_SYMBOL(menelaus_set_mmc_opendrain
);
315 int menelaus_set_slot_sel(int enable
)
319 mutex_lock(&the_menelaus
->lock
);
320 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
323 ret
|= GPIO2_DIR_INPUT
;
325 ret
|= GPIO_CTRL_SLOTSELEN
;
327 ret
&= ~GPIO_CTRL_SLOTSELEN
;
328 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
330 mutex_unlock(&the_menelaus
->lock
);
333 EXPORT_SYMBOL(menelaus_set_slot_sel
);
335 int menelaus_set_mmc_slot(int slot
, int enable
, int power
, int cd_en
)
339 if (slot
!= 1 && slot
!= 2)
344 mutex_lock(&the_menelaus
->lock
);
346 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
352 val
|= MCT_CTRL2_S1CD_BUFEN
| MCT_CTRL2_S1CD_DBEN
;
354 val
&= ~(MCT_CTRL2_S1CD_BUFEN
| MCT_CTRL2_S1CD_DBEN
);
357 val
|= MCT_CTRL2_S2CD_BUFEN
| MCT_CTRL2_S2CD_BEN
;
359 val
&= ~(MCT_CTRL2_S2CD_BUFEN
| MCT_CTRL2_S2CD_BEN
);
361 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, val
);
365 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL3
);
371 val
|= MCT_CTRL3_SLOT1_EN
;
373 val
&= ~MCT_CTRL3_SLOT1_EN
;
378 val
|= MCT_CTRL3_SLOT2_EN
;
380 val
&= ~MCT_CTRL3_SLOT2_EN
;
381 b
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
382 b
&= ~(MCT_CTRL2_VS2_SEL_D0
| MCT_CTRL2_VS2_SEL_D1
);
384 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, b
);
388 /* Disable autonomous shutdown */
389 val
&= ~(MCT_CTRL3_S1_AUTO_EN
| MCT_CTRL3_S2_AUTO_EN
);
390 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL3
, val
);
392 mutex_unlock(&the_menelaus
->lock
);
395 EXPORT_SYMBOL(menelaus_set_mmc_slot
);
397 int menelaus_register_mmc_callback(void (*callback
)(void *data
, u8 card_mask
),
402 the_menelaus
->mmc_callback_data
= data
;
403 the_menelaus
->mmc_callback
= callback
;
404 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ
,
405 menelaus_mmc_cd_work
);
408 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ
,
409 menelaus_mmc_cd_work
);
412 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ
,
413 menelaus_mmc_cd_work
);
416 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ
,
417 menelaus_mmc_cd_work
);
421 EXPORT_SYMBOL(menelaus_register_mmc_callback
);
423 void menelaus_unregister_mmc_callback(void)
425 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ
);
426 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ
);
427 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ
);
428 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ
);
430 the_menelaus
->mmc_callback
= NULL
;
431 the_menelaus
->mmc_callback_data
= NULL
;
433 EXPORT_SYMBOL(menelaus_unregister_mmc_callback
);
435 struct menelaus_vtg
{
443 struct menelaus_vtg_value
{
448 static int menelaus_set_voltage(const struct menelaus_vtg
*vtg
, int mV
,
449 int vtg_val
, int mode
)
452 struct i2c_client
*c
= the_menelaus
->client
;
454 mutex_lock(&the_menelaus
->lock
);
456 ret
= menelaus_read_reg(vtg
->vtg_reg
);
459 val
= ret
& ~(((1 << vtg
->vtg_bits
) - 1) << vtg
->vtg_shift
);
460 val
|= vtg_val
<< vtg
->vtg_shift
;
462 dev_dbg(&c
->dev
, "Setting voltage '%s'"
463 "to %d mV (reg 0x%02x, val 0x%02x)\n",
464 vtg
->name
, mV
, vtg
->vtg_reg
, val
);
466 ret
= menelaus_write_reg(vtg
->vtg_reg
, val
);
469 ret
= menelaus_write_reg(vtg
->mode_reg
, mode
);
471 mutex_unlock(&the_menelaus
->lock
);
473 /* Wait for voltage to stabilize */
479 static int menelaus_get_vtg_value(int vtg
, const struct menelaus_vtg_value
*tbl
,
484 for (i
= 0; i
< n
; i
++, tbl
++)
491 * Vcore can be programmed in two ways:
492 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
493 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
496 * Call correct 'set' function accordingly
499 static const struct menelaus_vtg_value vcore_values
[] = {
521 int menelaus_set_vcore_hw(unsigned int roof_mV
, unsigned int floor_mV
)
523 int fval
, rval
, val
, ret
;
524 struct i2c_client
*c
= the_menelaus
->client
;
526 rval
= menelaus_get_vtg_value(roof_mV
, vcore_values
,
527 ARRAY_SIZE(vcore_values
));
530 fval
= menelaus_get_vtg_value(floor_mV
, vcore_values
,
531 ARRAY_SIZE(vcore_values
));
535 dev_dbg(&c
->dev
, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
538 mutex_lock(&the_menelaus
->lock
);
539 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL3
, fval
);
542 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL4
, rval
);
545 if (!the_menelaus
->vcore_hw_mode
) {
546 val
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
547 /* HW mode, turn OFF byte comparator */
548 val
|= (VCORE_CTRL1_HW_NSW
| VCORE_CTRL1_BYP_COMP
);
549 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL1
, val
);
550 the_menelaus
->vcore_hw_mode
= 1;
554 mutex_unlock(&the_menelaus
->lock
);
558 static const struct menelaus_vtg vmem_vtg
= {
560 .vtg_reg
= MENELAUS_LDO_CTRL1
,
563 .mode_reg
= MENELAUS_LDO_CTRL3
,
566 static const struct menelaus_vtg_value vmem_values
[] = {
573 int menelaus_set_vmem(unsigned int mV
)
578 return menelaus_set_voltage(&vmem_vtg
, 0, 0, 0);
580 val
= menelaus_get_vtg_value(mV
, vmem_values
, ARRAY_SIZE(vmem_values
));
583 return menelaus_set_voltage(&vmem_vtg
, mV
, val
, 0x02);
585 EXPORT_SYMBOL(menelaus_set_vmem
);
587 static const struct menelaus_vtg vio_vtg
= {
589 .vtg_reg
= MENELAUS_LDO_CTRL1
,
592 .mode_reg
= MENELAUS_LDO_CTRL4
,
595 static const struct menelaus_vtg_value vio_values
[] = {
602 int menelaus_set_vio(unsigned int mV
)
607 return menelaus_set_voltage(&vio_vtg
, 0, 0, 0);
609 val
= menelaus_get_vtg_value(mV
, vio_values
, ARRAY_SIZE(vio_values
));
612 return menelaus_set_voltage(&vio_vtg
, mV
, val
, 0x02);
614 EXPORT_SYMBOL(menelaus_set_vio
);
616 static const struct menelaus_vtg_value vdcdc_values
[] = {
627 static const struct menelaus_vtg vdcdc2_vtg
= {
629 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
632 .mode_reg
= MENELAUS_DCDC_CTRL2
,
635 static const struct menelaus_vtg vdcdc3_vtg
= {
637 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
640 .mode_reg
= MENELAUS_DCDC_CTRL3
,
643 int menelaus_set_vdcdc(int dcdc
, unsigned int mV
)
645 const struct menelaus_vtg
*vtg
;
648 if (dcdc
!= 2 && dcdc
!= 3)
656 return menelaus_set_voltage(vtg
, 0, 0, 0);
658 val
= menelaus_get_vtg_value(mV
, vdcdc_values
,
659 ARRAY_SIZE(vdcdc_values
));
662 return menelaus_set_voltage(vtg
, mV
, val
, 0x03);
665 static const struct menelaus_vtg_value vmmc_values
[] = {
672 static const struct menelaus_vtg vmmc_vtg
= {
674 .vtg_reg
= MENELAUS_LDO_CTRL1
,
677 .mode_reg
= MENELAUS_LDO_CTRL7
,
680 int menelaus_set_vmmc(unsigned int mV
)
685 return menelaus_set_voltage(&vmmc_vtg
, 0, 0, 0);
687 val
= menelaus_get_vtg_value(mV
, vmmc_values
, ARRAY_SIZE(vmmc_values
));
690 return menelaus_set_voltage(&vmmc_vtg
, mV
, val
, 0x02);
692 EXPORT_SYMBOL(menelaus_set_vmmc
);
695 static const struct menelaus_vtg_value vaux_values
[] = {
702 static const struct menelaus_vtg vaux_vtg
= {
704 .vtg_reg
= MENELAUS_LDO_CTRL1
,
707 .mode_reg
= MENELAUS_LDO_CTRL6
,
710 int menelaus_set_vaux(unsigned int mV
)
715 return menelaus_set_voltage(&vaux_vtg
, 0, 0, 0);
717 val
= menelaus_get_vtg_value(mV
, vaux_values
, ARRAY_SIZE(vaux_values
));
720 return menelaus_set_voltage(&vaux_vtg
, mV
, val
, 0x02);
722 EXPORT_SYMBOL(menelaus_set_vaux
);
724 int menelaus_get_slot_pin_states(void)
726 return menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
728 EXPORT_SYMBOL(menelaus_get_slot_pin_states
);
730 int menelaus_set_regulator_sleep(int enable
, u32 val
)
733 struct i2c_client
*c
= the_menelaus
->client
;
735 mutex_lock(&the_menelaus
->lock
);
736 ret
= menelaus_write_reg(MENELAUS_SLEEP_CTRL2
, val
);
740 dev_dbg(&c
->dev
, "regulator sleep configuration: %02x\n", val
);
742 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
745 t
= (GPIO_CTRL_SLPCTLEN
| GPIO3_DIR_INPUT
);
750 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
752 mutex_unlock(&the_menelaus
->lock
);
756 /*-----------------------------------------------------------------------*/
758 /* Handles Menelaus interrupts. Does not run in interrupt context */
759 static void menelaus_work(struct work_struct
*_menelaus
)
761 struct menelaus_chip
*menelaus
=
762 container_of(_menelaus
, struct menelaus_chip
, work
);
763 void (*handler
)(struct menelaus_chip
*menelaus
);
768 isr
= (menelaus_read_reg(MENELAUS_INT_STATUS2
)
769 & ~menelaus
->mask2
) << 8;
770 isr
|= menelaus_read_reg(MENELAUS_INT_STATUS1
)
776 int irq
= fls(isr
) - 1;
779 mutex_lock(&menelaus
->lock
);
780 menelaus_disable_irq(irq
);
781 menelaus_ack_irq(irq
);
782 handler
= menelaus
->handlers
[irq
];
785 menelaus_enable_irq(irq
);
786 mutex_unlock(&menelaus
->lock
);
789 enable_irq(menelaus
->client
->irq
);
793 * We cannot use I2C in interrupt context, so we just schedule work.
795 static irqreturn_t
menelaus_irq(int irq
, void *_menelaus
)
797 struct menelaus_chip
*menelaus
= _menelaus
;
799 disable_irq_nosync(irq
);
800 (void)schedule_work(&menelaus
->work
);
805 /*-----------------------------------------------------------------------*/
808 * The RTC needs to be set once, then it runs on backup battery power.
809 * It supports alarms, including system wake alarms (from some modes);
810 * and 1/second IRQs if requested.
812 #ifdef CONFIG_RTC_DRV_TWL92330
814 #define RTC_CTRL_RTC_EN (1 << 0)
815 #define RTC_CTRL_AL_EN (1 << 1)
816 #define RTC_CTRL_MODE12 (1 << 2)
817 #define RTC_CTRL_EVERY_MASK (3 << 3)
818 #define RTC_CTRL_EVERY_SEC (0 << 3)
819 #define RTC_CTRL_EVERY_MIN (1 << 3)
820 #define RTC_CTRL_EVERY_HR (2 << 3)
821 #define RTC_CTRL_EVERY_DAY (3 << 3)
823 #define RTC_UPDATE_EVERY 0x08
825 #define RTC_HR_PM (1 << 7)
827 static void menelaus_to_time(char *regs
, struct rtc_time
*t
)
829 t
->tm_sec
= bcd2bin(regs
[0]);
830 t
->tm_min
= bcd2bin(regs
[1]);
831 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
832 t
->tm_hour
= bcd2bin(regs
[2] & 0x1f) - 1;
833 if (regs
[2] & RTC_HR_PM
)
836 t
->tm_hour
= bcd2bin(regs
[2] & 0x3f);
837 t
->tm_mday
= bcd2bin(regs
[3]);
838 t
->tm_mon
= bcd2bin(regs
[4]) - 1;
839 t
->tm_year
= bcd2bin(regs
[5]) + 100;
842 static int time_to_menelaus(struct rtc_time
*t
, int regnum
)
846 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_sec
));
850 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_min
));
854 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
855 hour
= t
->tm_hour
+ 1;
857 hour
= RTC_HR_PM
| bin2bcd(hour
- 12);
859 hour
= bin2bcd(hour
);
861 hour
= bin2bcd(t
->tm_hour
);
862 status
= menelaus_write_reg(regnum
++, hour
);
866 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_mday
));
870 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_mon
+ 1));
874 status
= menelaus_write_reg(regnum
++, bin2bcd(t
->tm_year
- 100));
880 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x, err %d\n",
885 static int menelaus_read_time(struct device
*dev
, struct rtc_time
*t
)
887 struct i2c_msg msg
[2];
891 /* block read date and time registers */
892 regs
[0] = MENELAUS_RTC_SEC
;
894 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
899 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
900 msg
[1].flags
= I2C_M_RD
;
901 msg
[1].len
= sizeof(regs
);
904 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
906 dev_err(dev
, "%s error %d\n", "read", status
);
910 menelaus_to_time(regs
, t
);
911 t
->tm_wday
= bcd2bin(regs
[6]);
916 static int menelaus_set_time(struct device
*dev
, struct rtc_time
*t
)
920 /* write date and time registers */
921 status
= time_to_menelaus(t
, MENELAUS_RTC_SEC
);
924 status
= menelaus_write_reg(MENELAUS_RTC_WKDAY
, bin2bcd(t
->tm_wday
));
926 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x "
927 "err %d\n", MENELAUS_RTC_WKDAY
, status
);
931 /* now commit the write */
932 status
= menelaus_write_reg(MENELAUS_RTC_UPDATE
, RTC_UPDATE_EVERY
);
934 dev_err(&the_menelaus
->client
->dev
, "rtc commit time, err %d\n",
940 static int menelaus_read_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
942 struct i2c_msg msg
[2];
946 /* block read alarm registers */
947 regs
[0] = MENELAUS_RTC_AL_SEC
;
949 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
954 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
955 msg
[1].flags
= I2C_M_RD
;
956 msg
[1].len
= sizeof(regs
);
959 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
961 dev_err(dev
, "%s error %d\n", "alarm read", status
);
965 menelaus_to_time(regs
, &w
->time
);
967 w
->enabled
= !!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
);
969 /* NOTE we *could* check if actually pending... */
975 static int menelaus_set_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
979 if (the_menelaus
->client
->irq
<= 0 && w
->enabled
)
982 /* clear previous alarm enable */
983 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
) {
984 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
985 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
986 the_menelaus
->rtc_control
);
991 /* write alarm registers */
992 status
= time_to_menelaus(&w
->time
, MENELAUS_RTC_AL_SEC
);
996 /* enable alarm if requested */
998 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
999 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
1000 the_menelaus
->rtc_control
);
1006 #ifdef CONFIG_RTC_INTF_DEV
1008 static void menelaus_rtc_update_work(struct menelaus_chip
*m
)
1010 /* report 1/sec update */
1011 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_UF
);
1014 static int menelaus_ioctl(struct device
*dev
, unsigned cmd
, unsigned long arg
)
1018 if (the_menelaus
->client
->irq
<= 0)
1019 return -ENOIOCTLCMD
;
1024 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
)
1026 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
1029 if (!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
))
1031 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1033 /* 1/second "update" IRQ */
1035 if (the_menelaus
->uie
)
1037 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1038 status
= menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ
,
1039 menelaus_rtc_update_work
);
1041 the_menelaus
->uie
= 1;
1044 if (!the_menelaus
->uie
)
1046 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1048 the_menelaus
->uie
= 0;
1051 return -ENOIOCTLCMD
;
1053 return menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1057 #define menelaus_ioctl NULL
1060 /* REVISIT no compensation register support ... */
1062 static const struct rtc_class_ops menelaus_rtc_ops
= {
1063 .ioctl
= menelaus_ioctl
,
1064 .read_time
= menelaus_read_time
,
1065 .set_time
= menelaus_set_time
,
1066 .read_alarm
= menelaus_read_alarm
,
1067 .set_alarm
= menelaus_set_alarm
,
1070 static void menelaus_rtc_alarm_work(struct menelaus_chip
*m
)
1073 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_AF
);
1075 /* then disable it; alarms are oneshot */
1076 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1077 menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1080 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1082 int alarm
= (m
->client
->irq
> 0);
1085 /* assume 32KDETEN pin is pulled high */
1086 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL
) & 0x80)) {
1087 dev_dbg(&m
->client
->dev
, "no 32k oscillator\n");
1091 m
->rtc
= devm_rtc_allocate_device(&m
->client
->dev
);
1095 m
->rtc
->ops
= &menelaus_rtc_ops
;
1097 /* support RTC alarm; it can issue wakeups */
1099 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ
,
1100 menelaus_rtc_alarm_work
) < 0) {
1101 dev_err(&m
->client
->dev
, "can't handle RTC alarm\n");
1104 device_init_wakeup(&m
->client
->dev
, 1);
1107 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1108 m
->rtc_control
= menelaus_read_reg(MENELAUS_RTC_CTRL
);
1109 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)
1110 || (m
->rtc_control
& RTC_CTRL_AL_EN
)
1111 || (m
->rtc_control
& RTC_CTRL_EVERY_MASK
)) {
1112 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)) {
1113 dev_warn(&m
->client
->dev
, "rtc clock needs setting\n");
1114 m
->rtc_control
|= RTC_CTRL_RTC_EN
;
1116 m
->rtc_control
&= ~RTC_CTRL_EVERY_MASK
;
1117 m
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1118 menelaus_write_reg(MENELAUS_RTC_CTRL
, m
->rtc_control
);
1121 err
= devm_rtc_register_device(m
->rtc
);
1124 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ
);
1125 device_init_wakeup(&m
->client
->dev
, 0);
1127 the_menelaus
->rtc
= NULL
;
1133 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1140 /*-----------------------------------------------------------------------*/
1142 static struct i2c_driver menelaus_i2c_driver
;
1144 static int menelaus_probe(struct i2c_client
*client
)
1146 struct menelaus_chip
*menelaus
;
1149 struct menelaus_platform_data
*menelaus_pdata
=
1150 dev_get_platdata(&client
->dev
);
1153 dev_dbg(&client
->dev
, "only one %s for now\n",
1158 menelaus
= devm_kzalloc(&client
->dev
, sizeof(*menelaus
), GFP_KERNEL
);
1162 i2c_set_clientdata(client
, menelaus
);
1164 the_menelaus
= menelaus
;
1165 menelaus
->client
= client
;
1167 /* If a true probe check the device */
1168 rev
= menelaus_read_reg(MENELAUS_REV
);
1170 pr_err(DRIVER_NAME
": device not found");
1174 /* Ack and disable all Menelaus interrupts */
1175 menelaus_write_reg(MENELAUS_INT_ACK1
, 0xff);
1176 menelaus_write_reg(MENELAUS_INT_ACK2
, 0xff);
1177 menelaus_write_reg(MENELAUS_INT_MASK1
, 0xff);
1178 menelaus_write_reg(MENELAUS_INT_MASK2
, 0xff);
1179 menelaus
->mask1
= 0xff;
1180 menelaus
->mask2
= 0xff;
1182 /* Set output buffer strengths */
1183 menelaus_write_reg(MENELAUS_MCT_CTRL1
, 0x73);
1185 if (client
->irq
> 0) {
1186 err
= request_irq(client
->irq
, menelaus_irq
, 0,
1187 DRIVER_NAME
, menelaus
);
1189 dev_dbg(&client
->dev
, "can't get IRQ %d, err %d\n",
1195 mutex_init(&menelaus
->lock
);
1196 INIT_WORK(&menelaus
->work
, menelaus_work
);
1198 pr_info("Menelaus rev %d.%d\n", rev
>> 4, rev
& 0x0f);
1200 err
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
1203 if (err
& VCORE_CTRL1_HW_NSW
)
1204 menelaus
->vcore_hw_mode
= 1;
1206 menelaus
->vcore_hw_mode
= 0;
1208 if (menelaus_pdata
!= NULL
&& menelaus_pdata
->late_init
!= NULL
) {
1209 err
= menelaus_pdata
->late_init(&client
->dev
);
1214 menelaus_rtc_init(menelaus
);
1218 free_irq(client
->irq
, menelaus
);
1219 flush_work(&menelaus
->work
);
1223 static void menelaus_remove(struct i2c_client
*client
)
1225 struct menelaus_chip
*menelaus
= i2c_get_clientdata(client
);
1227 free_irq(client
->irq
, menelaus
);
1228 flush_work(&menelaus
->work
);
1229 the_menelaus
= NULL
;
1232 static const struct i2c_device_id menelaus_id
[] = {
1236 MODULE_DEVICE_TABLE(i2c
, menelaus_id
);
1238 static struct i2c_driver menelaus_i2c_driver
= {
1240 .name
= DRIVER_NAME
,
1242 .probe
= menelaus_probe
,
1243 .remove
= menelaus_remove
,
1244 .id_table
= menelaus_id
,
1247 module_i2c_driver(menelaus_i2c_driver
);
1249 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1250 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1251 MODULE_LICENSE("GPL");