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
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 pr_err(fmt, arg...) printk(KERN_ERR DRIVER_NAME ": ", ## arg);
55 #define MENELAUS_I2C_ADDRESS 0x72
57 #define MENELAUS_REV 0x01
58 #define MENELAUS_VCORE_CTRL1 0x02
59 #define MENELAUS_VCORE_CTRL2 0x03
60 #define MENELAUS_VCORE_CTRL3 0x04
61 #define MENELAUS_VCORE_CTRL4 0x05
62 #define MENELAUS_VCORE_CTRL5 0x06
63 #define MENELAUS_DCDC_CTRL1 0x07
64 #define MENELAUS_DCDC_CTRL2 0x08
65 #define MENELAUS_DCDC_CTRL3 0x09
66 #define MENELAUS_LDO_CTRL1 0x0A
67 #define MENELAUS_LDO_CTRL2 0x0B
68 #define MENELAUS_LDO_CTRL3 0x0C
69 #define MENELAUS_LDO_CTRL4 0x0D
70 #define MENELAUS_LDO_CTRL5 0x0E
71 #define MENELAUS_LDO_CTRL6 0x0F
72 #define MENELAUS_LDO_CTRL7 0x10
73 #define MENELAUS_LDO_CTRL8 0x11
74 #define MENELAUS_SLEEP_CTRL1 0x12
75 #define MENELAUS_SLEEP_CTRL2 0x13
76 #define MENELAUS_DEVICE_OFF 0x14
77 #define MENELAUS_OSC_CTRL 0x15
78 #define MENELAUS_DETECT_CTRL 0x16
79 #define MENELAUS_INT_MASK1 0x17
80 #define MENELAUS_INT_MASK2 0x18
81 #define MENELAUS_INT_STATUS1 0x19
82 #define MENELAUS_INT_STATUS2 0x1A
83 #define MENELAUS_INT_ACK1 0x1B
84 #define MENELAUS_INT_ACK2 0x1C
85 #define MENELAUS_GPIO_CTRL 0x1D
86 #define MENELAUS_GPIO_IN 0x1E
87 #define MENELAUS_GPIO_OUT 0x1F
88 #define MENELAUS_BBSMS 0x20
89 #define MENELAUS_RTC_CTRL 0x21
90 #define MENELAUS_RTC_UPDATE 0x22
91 #define MENELAUS_RTC_SEC 0x23
92 #define MENELAUS_RTC_MIN 0x24
93 #define MENELAUS_RTC_HR 0x25
94 #define MENELAUS_RTC_DAY 0x26
95 #define MENELAUS_RTC_MON 0x27
96 #define MENELAUS_RTC_YR 0x28
97 #define MENELAUS_RTC_WKDAY 0x29
98 #define MENELAUS_RTC_AL_SEC 0x2A
99 #define MENELAUS_RTC_AL_MIN 0x2B
100 #define MENELAUS_RTC_AL_HR 0x2C
101 #define MENELAUS_RTC_AL_DAY 0x2D
102 #define MENELAUS_RTC_AL_MON 0x2E
103 #define MENELAUS_RTC_AL_YR 0x2F
104 #define MENELAUS_RTC_COMP_MSB 0x30
105 #define MENELAUS_RTC_COMP_LSB 0x31
106 #define MENELAUS_S1_PULL_EN 0x32
107 #define MENELAUS_S1_PULL_DIR 0x33
108 #define MENELAUS_S2_PULL_EN 0x34
109 #define MENELAUS_S2_PULL_DIR 0x35
110 #define MENELAUS_MCT_CTRL1 0x36
111 #define MENELAUS_MCT_CTRL2 0x37
112 #define MENELAUS_MCT_CTRL3 0x38
113 #define MENELAUS_MCT_PIN_ST 0x39
114 #define MENELAUS_DEBOUNCE1 0x3A
116 #define IH_MENELAUS_IRQS 12
117 #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
118 #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
119 #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
120 #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
121 #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
122 #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
123 #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
124 #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
125 #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
126 #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
127 #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
128 #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
129 #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
130 #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
131 #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
132 #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
134 static void menelaus_work(struct work_struct
*_menelaus
);
136 struct menelaus_chip
{
138 struct i2c_client
*client
;
139 struct work_struct work
;
140 #ifdef CONFIG_RTC_DRV_TWL92330
141 struct rtc_device
*rtc
;
145 unsigned vcore_hw_mode
:1;
147 void (*handlers
[16])(struct menelaus_chip
*);
148 void (*mmc_callback
)(void *data
, u8 mask
);
149 void *mmc_callback_data
;
152 static struct menelaus_chip
*the_menelaus
;
154 static int menelaus_write_reg(int reg
, u8 value
)
156 int val
= i2c_smbus_write_byte_data(the_menelaus
->client
, reg
, value
);
159 pr_err("write error");
166 static int menelaus_read_reg(int reg
)
168 int val
= i2c_smbus_read_byte_data(the_menelaus
->client
, reg
);
171 pr_err("read error");
176 static int menelaus_enable_irq(int irq
)
180 the_menelaus
->mask2
&= ~(1 << irq
);
181 return menelaus_write_reg(MENELAUS_INT_MASK2
,
182 the_menelaus
->mask2
);
184 the_menelaus
->mask1
&= ~(1 << irq
);
185 return menelaus_write_reg(MENELAUS_INT_MASK1
,
186 the_menelaus
->mask1
);
190 static int menelaus_disable_irq(int irq
)
194 the_menelaus
->mask2
|= (1 << irq
);
195 return menelaus_write_reg(MENELAUS_INT_MASK2
,
196 the_menelaus
->mask2
);
198 the_menelaus
->mask1
|= (1 << irq
);
199 return menelaus_write_reg(MENELAUS_INT_MASK1
,
200 the_menelaus
->mask1
);
204 static int menelaus_ack_irq(int irq
)
207 return menelaus_write_reg(MENELAUS_INT_ACK2
, 1 << (irq
- 8));
209 return menelaus_write_reg(MENELAUS_INT_ACK1
, 1 << irq
);
212 /* Adds a handler for an interrupt. Does not run in interrupt context */
213 static int menelaus_add_irq_work(int irq
,
214 void (*handler
)(struct menelaus_chip
*))
218 mutex_lock(&the_menelaus
->lock
);
219 the_menelaus
->handlers
[irq
] = handler
;
220 ret
= menelaus_enable_irq(irq
);
221 mutex_unlock(&the_menelaus
->lock
);
226 /* Removes handler for an interrupt */
227 static int menelaus_remove_irq_work(int irq
)
231 mutex_lock(&the_menelaus
->lock
);
232 ret
= menelaus_disable_irq(irq
);
233 the_menelaus
->handlers
[irq
] = NULL
;
234 mutex_unlock(&the_menelaus
->lock
);
240 * Gets scheduled when a card detect interrupt happens. Note that in some cases
241 * this line is wired to card cover switch rather than the card detect switch
242 * in each slot. In this case the cards are not seen by menelaus.
243 * FIXME: Add handling for D1 too
245 static void menelaus_mmc_cd_work(struct menelaus_chip
*menelaus_hw
)
248 unsigned char card_mask
= 0;
250 reg
= menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
255 card_mask
|= (1 << 0);
258 card_mask
|= (1 << 1);
260 if (menelaus_hw
->mmc_callback
)
261 menelaus_hw
->mmc_callback(menelaus_hw
->mmc_callback_data
,
266 * Toggles the MMC slots between open-drain and push-pull mode.
268 int menelaus_set_mmc_opendrain(int slot
, int enable
)
272 if (slot
!= 1 && slot
!= 2)
274 mutex_lock(&the_menelaus
->lock
);
275 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL1
);
277 mutex_unlock(&the_menelaus
->lock
);
292 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL1
, val
);
293 mutex_unlock(&the_menelaus
->lock
);
297 EXPORT_SYMBOL(menelaus_set_mmc_opendrain
);
299 int menelaus_set_slot_sel(int enable
)
303 mutex_lock(&the_menelaus
->lock
);
304 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
312 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
314 mutex_unlock(&the_menelaus
->lock
);
317 EXPORT_SYMBOL(menelaus_set_slot_sel
);
319 int menelaus_set_mmc_slot(int slot
, int enable
, int power
, int cd_en
)
323 if (slot
!= 1 && slot
!= 2)
328 mutex_lock(&the_menelaus
->lock
);
330 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
336 val
|= (1 << 4) | (1 << 6);
338 val
&= ~((1 << 4) | (1 << 6));
341 val
|= (1 << 5) | (1 << 7);
343 val
&= ~((1 << 5) | (1 << 7));
345 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, val
);
349 ret
= menelaus_read_reg(MENELAUS_MCT_CTRL3
);
365 b
= menelaus_read_reg(MENELAUS_MCT_CTRL2
);
368 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL2
, b
);
372 /* Disable autonomous shutdown */
374 ret
= menelaus_write_reg(MENELAUS_MCT_CTRL3
, val
);
376 mutex_unlock(&the_menelaus
->lock
);
379 EXPORT_SYMBOL(menelaus_set_mmc_slot
);
381 int menelaus_register_mmc_callback(void (*callback
)(void *data
, u8 card_mask
),
386 the_menelaus
->mmc_callback_data
= data
;
387 the_menelaus
->mmc_callback
= callback
;
388 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ
,
389 menelaus_mmc_cd_work
);
392 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ
,
393 menelaus_mmc_cd_work
);
396 ret
= menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ
,
397 menelaus_mmc_cd_work
);
400 ret
= menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ
,
401 menelaus_mmc_cd_work
);
405 EXPORT_SYMBOL(menelaus_register_mmc_callback
);
407 void menelaus_unregister_mmc_callback(void)
409 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ
);
410 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ
);
411 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ
);
412 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ
);
414 the_menelaus
->mmc_callback
= NULL
;
415 the_menelaus
->mmc_callback_data
= 0;
417 EXPORT_SYMBOL(menelaus_unregister_mmc_callback
);
419 struct menelaus_vtg
{
427 struct menelaus_vtg_value
{
432 static int menelaus_set_voltage(const struct menelaus_vtg
*vtg
, int mV
,
433 int vtg_val
, int mode
)
436 struct i2c_client
*c
= the_menelaus
->client
;
438 mutex_lock(&the_menelaus
->lock
);
442 ret
= menelaus_read_reg(vtg
->vtg_reg
);
445 val
= ret
& ~(((1 << vtg
->vtg_bits
) - 1) << vtg
->vtg_shift
);
446 val
|= vtg_val
<< vtg
->vtg_shift
;
448 dev_dbg(&c
->dev
, "Setting voltage '%s'"
449 "to %d mV (reg 0x%02x, val 0x%02x)\n",
450 vtg
->name
, mV
, vtg
->vtg_reg
, val
);
452 ret
= menelaus_write_reg(vtg
->vtg_reg
, val
);
456 ret
= menelaus_write_reg(vtg
->mode_reg
, mode
);
458 mutex_unlock(&the_menelaus
->lock
);
460 /* Wait for voltage to stabilize */
466 static int menelaus_get_vtg_value(int vtg
, const struct menelaus_vtg_value
*tbl
,
471 for (i
= 0; i
< n
; i
++, tbl
++)
478 * Vcore can be programmed in two ways:
479 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
480 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
483 * Call correct 'set' function accordingly
486 static const struct menelaus_vtg_value vcore_values
[] = {
508 int menelaus_set_vcore_sw(unsigned int mV
)
511 struct i2c_client
*c
= the_menelaus
->client
;
513 val
= menelaus_get_vtg_value(mV
, vcore_values
,
514 ARRAY_SIZE(vcore_values
));
518 dev_dbg(&c
->dev
, "Setting VCORE to %d mV (val 0x%02x)\n", mV
, val
);
520 /* Set SW mode and the voltage in one go. */
521 mutex_lock(&the_menelaus
->lock
);
522 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL1
, val
);
524 the_menelaus
->vcore_hw_mode
= 0;
525 mutex_unlock(&the_menelaus
->lock
);
531 int menelaus_set_vcore_hw(unsigned int roof_mV
, unsigned int floor_mV
)
533 int fval
, rval
, val
, ret
;
534 struct i2c_client
*c
= the_menelaus
->client
;
536 rval
= menelaus_get_vtg_value(roof_mV
, vcore_values
,
537 ARRAY_SIZE(vcore_values
));
540 fval
= menelaus_get_vtg_value(floor_mV
, vcore_values
,
541 ARRAY_SIZE(vcore_values
));
545 dev_dbg(&c
->dev
, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
548 mutex_lock(&the_menelaus
->lock
);
549 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL3
, fval
);
552 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL4
, rval
);
555 if (!the_menelaus
->vcore_hw_mode
) {
556 val
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
557 /* HW mode, turn OFF byte comparator */
558 val
|= ((1 << 7) | (1 << 5));
559 ret
= menelaus_write_reg(MENELAUS_VCORE_CTRL1
, val
);
560 the_menelaus
->vcore_hw_mode
= 1;
564 mutex_unlock(&the_menelaus
->lock
);
568 static const struct menelaus_vtg vmem_vtg
= {
570 .vtg_reg
= MENELAUS_LDO_CTRL1
,
573 .mode_reg
= MENELAUS_LDO_CTRL3
,
576 static const struct menelaus_vtg_value vmem_values
[] = {
583 int menelaus_set_vmem(unsigned int mV
)
588 return menelaus_set_voltage(&vmem_vtg
, 0, 0, 0);
590 val
= menelaus_get_vtg_value(mV
, vmem_values
, ARRAY_SIZE(vmem_values
));
593 return menelaus_set_voltage(&vmem_vtg
, mV
, val
, 0x02);
595 EXPORT_SYMBOL(menelaus_set_vmem
);
597 static const struct menelaus_vtg vio_vtg
= {
599 .vtg_reg
= MENELAUS_LDO_CTRL1
,
602 .mode_reg
= MENELAUS_LDO_CTRL4
,
605 static const struct menelaus_vtg_value vio_values
[] = {
612 int menelaus_set_vio(unsigned int mV
)
617 return menelaus_set_voltage(&vio_vtg
, 0, 0, 0);
619 val
= menelaus_get_vtg_value(mV
, vio_values
, ARRAY_SIZE(vio_values
));
622 return menelaus_set_voltage(&vio_vtg
, mV
, val
, 0x02);
624 EXPORT_SYMBOL(menelaus_set_vio
);
626 static const struct menelaus_vtg_value vdcdc_values
[] = {
637 static const struct menelaus_vtg vdcdc2_vtg
= {
639 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
642 .mode_reg
= MENELAUS_DCDC_CTRL2
,
645 static const struct menelaus_vtg vdcdc3_vtg
= {
647 .vtg_reg
= MENELAUS_DCDC_CTRL1
,
650 .mode_reg
= MENELAUS_DCDC_CTRL3
,
653 int menelaus_set_vdcdc(int dcdc
, unsigned int mV
)
655 const struct menelaus_vtg
*vtg
;
658 if (dcdc
!= 2 && dcdc
!= 3)
666 return menelaus_set_voltage(vtg
, 0, 0, 0);
668 val
= menelaus_get_vtg_value(mV
, vdcdc_values
,
669 ARRAY_SIZE(vdcdc_values
));
672 return menelaus_set_voltage(vtg
, mV
, val
, 0x03);
675 static const struct menelaus_vtg_value vmmc_values
[] = {
682 static const struct menelaus_vtg vmmc_vtg
= {
684 .vtg_reg
= MENELAUS_LDO_CTRL1
,
687 .mode_reg
= MENELAUS_LDO_CTRL7
,
690 int menelaus_set_vmmc(unsigned int mV
)
695 return menelaus_set_voltage(&vmmc_vtg
, 0, 0, 0);
697 val
= menelaus_get_vtg_value(mV
, vmmc_values
, ARRAY_SIZE(vmmc_values
));
700 return menelaus_set_voltage(&vmmc_vtg
, mV
, val
, 0x02);
702 EXPORT_SYMBOL(menelaus_set_vmmc
);
705 static const struct menelaus_vtg_value vaux_values
[] = {
712 static const struct menelaus_vtg vaux_vtg
= {
714 .vtg_reg
= MENELAUS_LDO_CTRL1
,
717 .mode_reg
= MENELAUS_LDO_CTRL6
,
720 int menelaus_set_vaux(unsigned int mV
)
725 return menelaus_set_voltage(&vaux_vtg
, 0, 0, 0);
727 val
= menelaus_get_vtg_value(mV
, vaux_values
, ARRAY_SIZE(vaux_values
));
730 return menelaus_set_voltage(&vaux_vtg
, mV
, val
, 0x02);
732 EXPORT_SYMBOL(menelaus_set_vaux
);
734 int menelaus_get_slot_pin_states(void)
736 return menelaus_read_reg(MENELAUS_MCT_PIN_ST
);
738 EXPORT_SYMBOL(menelaus_get_slot_pin_states
);
740 int menelaus_set_regulator_sleep(int enable
, u32 val
)
743 struct i2c_client
*c
= the_menelaus
->client
;
745 mutex_lock(&the_menelaus
->lock
);
746 ret
= menelaus_write_reg(MENELAUS_SLEEP_CTRL2
, val
);
750 dev_dbg(&c
->dev
, "regulator sleep configuration: %02x\n", val
);
752 ret
= menelaus_read_reg(MENELAUS_GPIO_CTRL
);
755 t
= ((1 << 6) | 0x04);
760 ret
= menelaus_write_reg(MENELAUS_GPIO_CTRL
, ret
);
762 mutex_unlock(&the_menelaus
->lock
);
766 /*-----------------------------------------------------------------------*/
768 /* Handles Menelaus interrupts. Does not run in interrupt context */
769 static void menelaus_work(struct work_struct
*_menelaus
)
771 struct menelaus_chip
*menelaus
=
772 container_of(_menelaus
, struct menelaus_chip
, work
);
773 void (*handler
)(struct menelaus_chip
*menelaus
);
778 isr
= (menelaus_read_reg(MENELAUS_INT_STATUS2
)
779 & ~menelaus
->mask2
) << 8;
780 isr
|= menelaus_read_reg(MENELAUS_INT_STATUS1
)
786 int irq
= fls(isr
) - 1;
789 mutex_lock(&menelaus
->lock
);
790 menelaus_disable_irq(irq
);
791 menelaus_ack_irq(irq
);
792 handler
= menelaus
->handlers
[irq
];
795 menelaus_enable_irq(irq
);
796 mutex_unlock(&menelaus
->lock
);
799 enable_irq(menelaus
->client
->irq
);
803 * We cannot use I2C in interrupt context, so we just schedule work.
805 static irqreturn_t
menelaus_irq(int irq
, void *_menelaus
)
807 struct menelaus_chip
*menelaus
= _menelaus
;
809 disable_irq_nosync(irq
);
810 (void)schedule_work(&menelaus
->work
);
815 /*-----------------------------------------------------------------------*/
818 * The RTC needs to be set once, then it runs on backup battery power.
819 * It supports alarms, including system wake alarms (from some modes);
820 * and 1/second IRQs if requested.
822 #ifdef CONFIG_RTC_DRV_TWL92330
824 #define RTC_CTRL_RTC_EN (1 << 0)
825 #define RTC_CTRL_AL_EN (1 << 1)
826 #define RTC_CTRL_MODE12 (1 << 2)
827 #define RTC_CTRL_EVERY_MASK (3 << 3)
828 #define RTC_CTRL_EVERY_SEC (0 << 3)
829 #define RTC_CTRL_EVERY_MIN (1 << 3)
830 #define RTC_CTRL_EVERY_HR (2 << 3)
831 #define RTC_CTRL_EVERY_DAY (3 << 3)
833 #define RTC_UPDATE_EVERY 0x08
835 #define RTC_HR_PM (1 << 7)
837 static void menelaus_to_time(char *regs
, struct rtc_time
*t
)
839 t
->tm_sec
= BCD2BIN(regs
[0]);
840 t
->tm_min
= BCD2BIN(regs
[1]);
841 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
842 t
->tm_hour
= BCD2BIN(regs
[2] & 0x1f) - 1;
843 if (regs
[2] & RTC_HR_PM
)
846 t
->tm_hour
= BCD2BIN(regs
[2] & 0x3f);
847 t
->tm_mday
= BCD2BIN(regs
[3]);
848 t
->tm_mon
= BCD2BIN(regs
[4]) - 1;
849 t
->tm_year
= BCD2BIN(regs
[5]) + 100;
852 static int time_to_menelaus(struct rtc_time
*t
, int regnum
)
856 status
= menelaus_write_reg(regnum
++, BIN2BCD(t
->tm_sec
));
860 status
= menelaus_write_reg(regnum
++, BIN2BCD(t
->tm_min
));
864 if (the_menelaus
->rtc_control
& RTC_CTRL_MODE12
) {
865 hour
= t
->tm_hour
+ 1;
867 hour
= RTC_HR_PM
| BIN2BCD(hour
- 12);
869 hour
= BIN2BCD(hour
);
871 hour
= BIN2BCD(t
->tm_hour
);
872 status
= menelaus_write_reg(regnum
++, hour
);
876 status
= menelaus_write_reg(regnum
++, BIN2BCD(t
->tm_mday
));
880 status
= menelaus_write_reg(regnum
++, BIN2BCD(t
->tm_mon
+ 1));
884 status
= menelaus_write_reg(regnum
++, BIN2BCD(t
->tm_year
- 100));
890 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x, err %d\n",
895 static int menelaus_read_time(struct device
*dev
, struct rtc_time
*t
)
897 struct i2c_msg msg
[2];
901 /* block read date and time registers */
902 regs
[0] = MENELAUS_RTC_SEC
;
904 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
909 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
910 msg
[1].flags
= I2C_M_RD
;
911 msg
[1].len
= sizeof(regs
);
914 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
916 dev_err(dev
, "%s error %d\n", "read", status
);
920 menelaus_to_time(regs
, t
);
921 t
->tm_wday
= BCD2BIN(regs
[6]);
926 static int menelaus_set_time(struct device
*dev
, struct rtc_time
*t
)
930 /* write date and time registers */
931 status
= time_to_menelaus(t
, MENELAUS_RTC_SEC
);
934 status
= menelaus_write_reg(MENELAUS_RTC_WKDAY
, BIN2BCD(t
->tm_wday
));
936 dev_err(&the_menelaus
->client
->dev
, "rtc write reg %02x",
937 "err %d\n", MENELAUS_RTC_WKDAY
, status
);
941 /* now commit the write */
942 status
= menelaus_write_reg(MENELAUS_RTC_UPDATE
, RTC_UPDATE_EVERY
);
944 dev_err(&the_menelaus
->client
->dev
, "rtc commit time, err %d\n",
950 static int menelaus_read_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
952 struct i2c_msg msg
[2];
956 /* block read alarm registers */
957 regs
[0] = MENELAUS_RTC_AL_SEC
;
959 msg
[0].addr
= MENELAUS_I2C_ADDRESS
;
964 msg
[1].addr
= MENELAUS_I2C_ADDRESS
;
965 msg
[1].flags
= I2C_M_RD
;
966 msg
[1].len
= sizeof(regs
);
969 status
= i2c_transfer(the_menelaus
->client
->adapter
, msg
, 2);
971 dev_err(dev
, "%s error %d\n", "alarm read", status
);
975 menelaus_to_time(regs
, &w
->time
);
977 w
->enabled
= !!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
);
979 /* NOTE we *could* check if actually pending... */
985 static int menelaus_set_alarm(struct device
*dev
, struct rtc_wkalrm
*w
)
989 if (the_menelaus
->client
->irq
<= 0 && w
->enabled
)
992 /* clear previous alarm enable */
993 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
) {
994 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
995 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
996 the_menelaus
->rtc_control
);
1001 /* write alarm registers */
1002 status
= time_to_menelaus(&w
->time
, MENELAUS_RTC_AL_SEC
);
1006 /* enable alarm if requested */
1008 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
1009 status
= menelaus_write_reg(MENELAUS_RTC_CTRL
,
1010 the_menelaus
->rtc_control
);
1016 #ifdef CONFIG_RTC_INTF_DEV
1018 static void menelaus_rtc_update_work(struct menelaus_chip
*m
)
1020 /* report 1/sec update */
1021 local_irq_disable();
1022 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_UF
);
1026 static int menelaus_ioctl(struct device
*dev
, unsigned cmd
, unsigned long arg
)
1030 if (the_menelaus
->client
->irq
<= 0)
1031 return -ENOIOCTLCMD
;
1036 if (the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
)
1038 the_menelaus
->rtc_control
|= RTC_CTRL_AL_EN
;
1041 if (!(the_menelaus
->rtc_control
& RTC_CTRL_AL_EN
))
1043 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1045 /* 1/second "update" IRQ */
1047 if (the_menelaus
->uie
)
1049 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1050 status
= menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ
,
1051 menelaus_rtc_update_work
);
1053 the_menelaus
->uie
= 1;
1056 if (!the_menelaus
->uie
)
1058 status
= menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ
);
1060 the_menelaus
->uie
= 0;
1063 return -ENOIOCTLCMD
;
1065 return menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1069 #define menelaus_ioctl NULL
1072 /* REVISIT no compensation register support ... */
1074 static const struct rtc_class_ops menelaus_rtc_ops
= {
1075 .ioctl
= menelaus_ioctl
,
1076 .read_time
= menelaus_read_time
,
1077 .set_time
= menelaus_set_time
,
1078 .read_alarm
= menelaus_read_alarm
,
1079 .set_alarm
= menelaus_set_alarm
,
1082 static void menelaus_rtc_alarm_work(struct menelaus_chip
*m
)
1085 local_irq_disable();
1086 rtc_update_irq(m
->rtc
, 1, RTC_IRQF
| RTC_AF
);
1089 /* then disable it; alarms are oneshot */
1090 the_menelaus
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1091 menelaus_write_reg(MENELAUS_RTC_CTRL
, the_menelaus
->rtc_control
);
1094 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1096 int alarm
= (m
->client
->irq
> 0);
1098 /* assume 32KDETEN pin is pulled high */
1099 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL
) & 0x80)) {
1100 dev_dbg(&m
->client
->dev
, "no 32k oscillator\n");
1104 /* support RTC alarm; it can issue wakeups */
1106 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ
,
1107 menelaus_rtc_alarm_work
) < 0) {
1108 dev_err(&m
->client
->dev
, "can't handle RTC alarm\n");
1111 device_init_wakeup(&m
->client
->dev
, 1);
1114 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1115 m
->rtc_control
= menelaus_read_reg(MENELAUS_RTC_CTRL
);
1116 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)
1117 || (m
->rtc_control
& RTC_CTRL_AL_EN
)
1118 || (m
->rtc_control
& RTC_CTRL_EVERY_MASK
)) {
1119 if (!(m
->rtc_control
& RTC_CTRL_RTC_EN
)) {
1120 dev_warn(&m
->client
->dev
, "rtc clock needs setting\n");
1121 m
->rtc_control
|= RTC_CTRL_RTC_EN
;
1123 m
->rtc_control
&= ~RTC_CTRL_EVERY_MASK
;
1124 m
->rtc_control
&= ~RTC_CTRL_AL_EN
;
1125 menelaus_write_reg(MENELAUS_RTC_CTRL
, m
->rtc_control
);
1128 m
->rtc
= rtc_device_register(DRIVER_NAME
,
1130 &menelaus_rtc_ops
, THIS_MODULE
);
1131 if (IS_ERR(m
->rtc
)) {
1133 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ
);
1134 device_init_wakeup(&m
->client
->dev
, 0);
1136 dev_err(&m
->client
->dev
, "can't register RTC: %d\n",
1137 (int) PTR_ERR(m
->rtc
));
1138 the_menelaus
->rtc
= NULL
;
1144 static inline void menelaus_rtc_init(struct menelaus_chip
*m
)
1151 /*-----------------------------------------------------------------------*/
1153 static struct i2c_driver menelaus_i2c_driver
;
1155 static int menelaus_probe(struct i2c_client
*client
)
1157 struct menelaus_chip
*menelaus
;
1160 struct menelaus_platform_data
*menelaus_pdata
=
1161 client
->dev
.platform_data
;
1164 dev_dbg(&client
->dev
, "only one %s for now\n",
1169 menelaus
= kzalloc(sizeof *menelaus
, GFP_KERNEL
);
1173 i2c_set_clientdata(client
, menelaus
);
1175 the_menelaus
= menelaus
;
1176 menelaus
->client
= client
;
1178 /* If a true probe check the device */
1179 rev
= menelaus_read_reg(MENELAUS_REV
);
1181 pr_err("device not found");
1186 /* Ack and disable all Menelaus interrupts */
1187 menelaus_write_reg(MENELAUS_INT_ACK1
, 0xff);
1188 menelaus_write_reg(MENELAUS_INT_ACK2
, 0xff);
1189 menelaus_write_reg(MENELAUS_INT_MASK1
, 0xff);
1190 menelaus_write_reg(MENELAUS_INT_MASK2
, 0xff);
1191 menelaus
->mask1
= 0xff;
1192 menelaus
->mask2
= 0xff;
1194 /* Set output buffer strengths */
1195 menelaus_write_reg(MENELAUS_MCT_CTRL1
, 0x73);
1197 if (client
->irq
> 0) {
1198 err
= request_irq(client
->irq
, menelaus_irq
, IRQF_DISABLED
,
1199 DRIVER_NAME
, menelaus
);
1201 dev_dbg(&client
->dev
, "can't get IRQ %d, err %d",
1207 mutex_init(&menelaus
->lock
);
1208 INIT_WORK(&menelaus
->work
, menelaus_work
);
1210 pr_info("Menelaus rev %d.%d\n", rev
>> 4, rev
& 0x0f);
1212 val
= menelaus_read_reg(MENELAUS_VCORE_CTRL1
);
1216 menelaus
->vcore_hw_mode
= 1;
1218 menelaus
->vcore_hw_mode
= 0;
1220 if (menelaus_pdata
!= NULL
&& menelaus_pdata
->late_init
!= NULL
) {
1221 err
= menelaus_pdata
->late_init(&client
->dev
);
1226 menelaus_rtc_init(menelaus
);
1230 free_irq(client
->irq
, menelaus
);
1231 flush_scheduled_work();
1237 static int __exit
menelaus_remove(struct i2c_client
*client
)
1239 struct menelaus_chip
*menelaus
= i2c_get_clientdata(client
);
1241 free_irq(client
->irq
, menelaus
);
1243 i2c_set_clientdata(client
, NULL
);
1244 the_menelaus
= NULL
;
1248 static struct i2c_driver menelaus_i2c_driver
= {
1250 .name
= DRIVER_NAME
,
1252 .probe
= menelaus_probe
,
1253 .remove
= __exit_p(menelaus_remove
),
1256 static int __init
menelaus_init(void)
1260 res
= i2c_add_driver(&menelaus_i2c_driver
);
1262 pr_err("driver registration failed\n");
1269 static void __exit
menelaus_exit(void)
1271 i2c_del_driver(&menelaus_i2c_driver
);
1273 /* FIXME: Shutdown menelaus parts that can be shut down */
1276 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1277 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1278 MODULE_LICENSE("GPL");
1280 module_init(menelaus_init
);
1281 module_exit(menelaus_exit
);