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>
44 #include <asm/mach/irq.h>
46 #include <mach/gpio.h>
47 #include <mach/menelaus.h>
49 #define DRIVER_NAME "menelaus"
51 #define MENELAUS_I2C_ADDRESS 0x72
53 #define MENELAUS_REV 0x01
54 #define MENELAUS_VCORE_CTRL1 0x02
55 #define MENELAUS_VCORE_CTRL2 0x03
56 #define MENELAUS_VCORE_CTRL3 0x04
57 #define MENELAUS_VCORE_CTRL4 0x05
58 #define MENELAUS_VCORE_CTRL5 0x06
59 #define MENELAUS_DCDC_CTRL1 0x07
60 #define MENELAUS_DCDC_CTRL2 0x08
61 #define MENELAUS_DCDC_CTRL3 0x09
62 #define MENELAUS_LDO_CTRL1 0x0A
63 #define MENELAUS_LDO_CTRL2 0x0B
64 #define MENELAUS_LDO_CTRL3 0x0C
65 #define MENELAUS_LDO_CTRL4 0x0D
66 #define MENELAUS_LDO_CTRL5 0x0E
67 #define MENELAUS_LDO_CTRL6 0x0F
68 #define MENELAUS_LDO_CTRL7 0x10
69 #define MENELAUS_LDO_CTRL8 0x11
70 #define MENELAUS_SLEEP_CTRL1 0x12
71 #define MENELAUS_SLEEP_CTRL2 0x13
72 #define MENELAUS_DEVICE_OFF 0x14
73 #define MENELAUS_OSC_CTRL 0x15
74 #define MENELAUS_DETECT_CTRL 0x16
75 #define MENELAUS_INT_MASK1 0x17
76 #define MENELAUS_INT_MASK2 0x18
77 #define MENELAUS_INT_STATUS1 0x19
78 #define MENELAUS_INT_STATUS2 0x1A
79 #define MENELAUS_INT_ACK1 0x1B
80 #define MENELAUS_INT_ACK2 0x1C
81 #define MENELAUS_GPIO_CTRL 0x1D
82 #define MENELAUS_GPIO_IN 0x1E
83 #define MENELAUS_GPIO_OUT 0x1F
84 #define MENELAUS_BBSMS 0x20
85 #define MENELAUS_RTC_CTRL 0x21
86 #define MENELAUS_RTC_UPDATE 0x22
87 #define MENELAUS_RTC_SEC 0x23
88 #define MENELAUS_RTC_MIN 0x24
89 #define MENELAUS_RTC_HR 0x25
90 #define MENELAUS_RTC_DAY 0x26
91 #define MENELAUS_RTC_MON 0x27
92 #define MENELAUS_RTC_YR 0x28
93 #define MENELAUS_RTC_WKDAY 0x29
94 #define MENELAUS_RTC_AL_SEC 0x2A
95 #define MENELAUS_RTC_AL_MIN 0x2B
96 #define MENELAUS_RTC_AL_HR 0x2C
97 #define MENELAUS_RTC_AL_DAY 0x2D
98 #define MENELAUS_RTC_AL_MON 0x2E
99 #define MENELAUS_RTC_AL_YR 0x2F
100 #define MENELAUS_RTC_COMP_MSB 0x30
101 #define MENELAUS_RTC_COMP_LSB 0x31
102 #define MENELAUS_S1_PULL_EN 0x32
103 #define MENELAUS_S1_PULL_DIR 0x33
104 #define MENELAUS_S2_PULL_EN 0x34
105 #define MENELAUS_S2_PULL_DIR 0x35
106 #define MENELAUS_MCT_CTRL1 0x36
107 #define MENELAUS_MCT_CTRL2 0x37
108 #define MENELAUS_MCT_CTRL3 0x38
109 #define MENELAUS_MCT_PIN_ST 0x39
110 #define MENELAUS_DEBOUNCE1 0x3A
112 #define IH_MENELAUS_IRQS 12
113 #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
114 #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
115 #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
116 #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
117 #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
118 #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
119 #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
120 #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
121 #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
122 #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
123 #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
124 #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
125 #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
126 #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
127 #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
128 #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
130 static void menelaus_work(struct work_struct *_menelaus);
132 struct menelaus_chip {
134 struct i2c_client *client;
135 struct work_struct work;
136 #ifdef CONFIG_RTC_DRV_TWL92330
137 struct rtc_device *rtc;
141 unsigned vcore_hw_mode:1;
143 void (*handlers[16])(struct menelaus_chip *);
144 void (*mmc_callback)(void *data, u8 mask);
145 void *mmc_callback_data;
148 static struct menelaus_chip *the_menelaus;
150 static int menelaus_write_reg(int reg, u8 value)
152 int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
155 pr_err(DRIVER_NAME ": write error");
162 static int menelaus_read_reg(int reg)
164 int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
167 pr_err(DRIVER_NAME ": read error");
172 static int menelaus_enable_irq(int irq)
176 the_menelaus->mask2 &= ~(1 << irq);
177 return menelaus_write_reg(MENELAUS_INT_MASK2,
178 the_menelaus->mask2);
180 the_menelaus->mask1 &= ~(1 << irq);
181 return menelaus_write_reg(MENELAUS_INT_MASK1,
182 the_menelaus->mask1);
186 static int menelaus_disable_irq(int irq)
190 the_menelaus->mask2 |= (1 << irq);
191 return menelaus_write_reg(MENELAUS_INT_MASK2,
192 the_menelaus->mask2);
194 the_menelaus->mask1 |= (1 << irq);
195 return menelaus_write_reg(MENELAUS_INT_MASK1,
196 the_menelaus->mask1);
200 static int menelaus_ack_irq(int irq)
203 return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
205 return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
208 /* Adds a handler for an interrupt. Does not run in interrupt context */
209 static int menelaus_add_irq_work(int irq,
210 void (*handler)(struct menelaus_chip *))
214 mutex_lock(&the_menelaus->lock);
215 the_menelaus->handlers[irq] = handler;
216 ret = menelaus_enable_irq(irq);
217 mutex_unlock(&the_menelaus->lock);
222 /* Removes handler for an interrupt */
223 static int menelaus_remove_irq_work(int irq)
227 mutex_lock(&the_menelaus->lock);
228 ret = menelaus_disable_irq(irq);
229 the_menelaus->handlers[irq] = NULL;
230 mutex_unlock(&the_menelaus->lock);
236 * Gets scheduled when a card detect interrupt happens. Note that in some cases
237 * this line is wired to card cover switch rather than the card detect switch
238 * in each slot. In this case the cards are not seen by menelaus.
239 * FIXME: Add handling for D1 too
241 static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
244 unsigned char card_mask = 0;
246 reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
251 card_mask |= (1 << 0);
254 card_mask |= (1 << 1);
256 if (menelaus_hw->mmc_callback)
257 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
262 * Toggles the MMC slots between open-drain and push-pull mode.
264 int menelaus_set_mmc_opendrain(int slot, int enable)
268 if (slot != 1 && slot != 2)
270 mutex_lock(&the_menelaus->lock);
271 ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
273 mutex_unlock(&the_menelaus->lock);
288 ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
289 mutex_unlock(&the_menelaus->lock);
293 EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
295 int menelaus_set_slot_sel(int enable)
299 mutex_lock(&the_menelaus->lock);
300 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
308 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
310 mutex_unlock(&the_menelaus->lock);
313 EXPORT_SYMBOL(menelaus_set_slot_sel);
315 int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
319 if (slot != 1 && slot != 2)
324 mutex_lock(&the_menelaus->lock);
326 ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
332 val |= (1 << 4) | (1 << 6);
334 val &= ~((1 << 4) | (1 << 6));
337 val |= (1 << 5) | (1 << 7);
339 val &= ~((1 << 5) | (1 << 7));
341 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
345 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
361 b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
364 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
368 /* Disable autonomous shutdown */
370 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
372 mutex_unlock(&the_menelaus->lock);
375 EXPORT_SYMBOL(menelaus_set_mmc_slot);
377 int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
382 the_menelaus->mmc_callback_data = data;
383 the_menelaus->mmc_callback = callback;
384 ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
385 menelaus_mmc_cd_work);
388 ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
389 menelaus_mmc_cd_work);
392 ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
393 menelaus_mmc_cd_work);
396 ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
397 menelaus_mmc_cd_work);
401 EXPORT_SYMBOL(menelaus_register_mmc_callback);
403 void menelaus_unregister_mmc_callback(void)
405 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
406 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
407 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
408 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
410 the_menelaus->mmc_callback = NULL;
411 the_menelaus->mmc_callback_data = 0;
413 EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
415 struct menelaus_vtg {
423 struct menelaus_vtg_value {
428 static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
429 int vtg_val, int mode)
432 struct i2c_client *c = the_menelaus->client;
434 mutex_lock(&the_menelaus->lock);
438 ret = menelaus_read_reg(vtg->vtg_reg);
441 val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
442 val |= vtg_val << vtg->vtg_shift;
444 dev_dbg(&c->dev, "Setting voltage '%s'"
445 "to %d mV (reg 0x%02x, val 0x%02x)\n",
446 vtg->name, mV, vtg->vtg_reg, val);
448 ret = menelaus_write_reg(vtg->vtg_reg, val);
452 ret = menelaus_write_reg(vtg->mode_reg, mode);
454 mutex_unlock(&the_menelaus->lock);
456 /* Wait for voltage to stabilize */
462 static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
467 for (i = 0; i < n; i++, tbl++)
474 * Vcore can be programmed in two ways:
475 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
476 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
479 * Call correct 'set' function accordingly
482 static const struct menelaus_vtg_value vcore_values[] = {
504 int menelaus_set_vcore_sw(unsigned int mV)
507 struct i2c_client *c = the_menelaus->client;
509 val = menelaus_get_vtg_value(mV, vcore_values,
510 ARRAY_SIZE(vcore_values));
514 dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
516 /* Set SW mode and the voltage in one go. */
517 mutex_lock(&the_menelaus->lock);
518 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
520 the_menelaus->vcore_hw_mode = 0;
521 mutex_unlock(&the_menelaus->lock);
527 int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
529 int fval, rval, val, ret;
530 struct i2c_client *c = the_menelaus->client;
532 rval = menelaus_get_vtg_value(roof_mV, vcore_values,
533 ARRAY_SIZE(vcore_values));
536 fval = menelaus_get_vtg_value(floor_mV, vcore_values,
537 ARRAY_SIZE(vcore_values));
541 dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
544 mutex_lock(&the_menelaus->lock);
545 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
548 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
551 if (!the_menelaus->vcore_hw_mode) {
552 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
553 /* HW mode, turn OFF byte comparator */
554 val |= ((1 << 7) | (1 << 5));
555 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
556 the_menelaus->vcore_hw_mode = 1;
560 mutex_unlock(&the_menelaus->lock);
564 static const struct menelaus_vtg vmem_vtg = {
566 .vtg_reg = MENELAUS_LDO_CTRL1,
569 .mode_reg = MENELAUS_LDO_CTRL3,
572 static const struct menelaus_vtg_value vmem_values[] = {
579 int menelaus_set_vmem(unsigned int mV)
584 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
586 val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
589 return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
591 EXPORT_SYMBOL(menelaus_set_vmem);
593 static const struct menelaus_vtg vio_vtg = {
595 .vtg_reg = MENELAUS_LDO_CTRL1,
598 .mode_reg = MENELAUS_LDO_CTRL4,
601 static const struct menelaus_vtg_value vio_values[] = {
608 int menelaus_set_vio(unsigned int mV)
613 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
615 val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
618 return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
620 EXPORT_SYMBOL(menelaus_set_vio);
622 static const struct menelaus_vtg_value vdcdc_values[] = {
633 static const struct menelaus_vtg vdcdc2_vtg = {
635 .vtg_reg = MENELAUS_DCDC_CTRL1,
638 .mode_reg = MENELAUS_DCDC_CTRL2,
641 static const struct menelaus_vtg vdcdc3_vtg = {
643 .vtg_reg = MENELAUS_DCDC_CTRL1,
646 .mode_reg = MENELAUS_DCDC_CTRL3,
649 int menelaus_set_vdcdc(int dcdc, unsigned int mV)
651 const struct menelaus_vtg *vtg;
654 if (dcdc != 2 && dcdc != 3)
662 return menelaus_set_voltage(vtg, 0, 0, 0);
664 val = menelaus_get_vtg_value(mV, vdcdc_values,
665 ARRAY_SIZE(vdcdc_values));
668 return menelaus_set_voltage(vtg, mV, val, 0x03);
671 static const struct menelaus_vtg_value vmmc_values[] = {
678 static const struct menelaus_vtg vmmc_vtg = {
680 .vtg_reg = MENELAUS_LDO_CTRL1,
683 .mode_reg = MENELAUS_LDO_CTRL7,
686 int menelaus_set_vmmc(unsigned int mV)
691 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
693 val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
696 return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
698 EXPORT_SYMBOL(menelaus_set_vmmc);
701 static const struct menelaus_vtg_value vaux_values[] = {
708 static const struct menelaus_vtg vaux_vtg = {
710 .vtg_reg = MENELAUS_LDO_CTRL1,
713 .mode_reg = MENELAUS_LDO_CTRL6,
716 int menelaus_set_vaux(unsigned int mV)
721 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
723 val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
726 return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
728 EXPORT_SYMBOL(menelaus_set_vaux);
730 int menelaus_get_slot_pin_states(void)
732 return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
734 EXPORT_SYMBOL(menelaus_get_slot_pin_states);
736 int menelaus_set_regulator_sleep(int enable, u32 val)
739 struct i2c_client *c = the_menelaus->client;
741 mutex_lock(&the_menelaus->lock);
742 ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
746 dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
748 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
751 t = ((1 << 6) | 0x04);
756 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
758 mutex_unlock(&the_menelaus->lock);
762 /*-----------------------------------------------------------------------*/
764 /* Handles Menelaus interrupts. Does not run in interrupt context */
765 static void menelaus_work(struct work_struct *_menelaus)
767 struct menelaus_chip *menelaus =
768 container_of(_menelaus, struct menelaus_chip, work);
769 void (*handler)(struct menelaus_chip *menelaus);
774 isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
775 & ~menelaus->mask2) << 8;
776 isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
782 int irq = fls(isr) - 1;
785 mutex_lock(&menelaus->lock);
786 menelaus_disable_irq(irq);
787 menelaus_ack_irq(irq);
788 handler = menelaus->handlers[irq];
791 menelaus_enable_irq(irq);
792 mutex_unlock(&menelaus->lock);
795 enable_irq(menelaus->client->irq);
799 * We cannot use I2C in interrupt context, so we just schedule work.
801 static irqreturn_t menelaus_irq(int irq, void *_menelaus)
803 struct menelaus_chip *menelaus = _menelaus;
805 disable_irq_nosync(irq);
806 (void)schedule_work(&menelaus->work);
811 /*-----------------------------------------------------------------------*/
814 * The RTC needs to be set once, then it runs on backup battery power.
815 * It supports alarms, including system wake alarms (from some modes);
816 * and 1/second IRQs if requested.
818 #ifdef CONFIG_RTC_DRV_TWL92330
820 #define RTC_CTRL_RTC_EN (1 << 0)
821 #define RTC_CTRL_AL_EN (1 << 1)
822 #define RTC_CTRL_MODE12 (1 << 2)
823 #define RTC_CTRL_EVERY_MASK (3 << 3)
824 #define RTC_CTRL_EVERY_SEC (0 << 3)
825 #define RTC_CTRL_EVERY_MIN (1 << 3)
826 #define RTC_CTRL_EVERY_HR (2 << 3)
827 #define RTC_CTRL_EVERY_DAY (3 << 3)
829 #define RTC_UPDATE_EVERY 0x08
831 #define RTC_HR_PM (1 << 7)
833 static void menelaus_to_time(char *regs, struct rtc_time *t)
835 t->tm_sec = BCD2BIN(regs[0]);
836 t->tm_min = BCD2BIN(regs[1]);
837 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
838 t->tm_hour = BCD2BIN(regs[2] & 0x1f) - 1;
839 if (regs[2] & RTC_HR_PM)
842 t->tm_hour = BCD2BIN(regs[2] & 0x3f);
843 t->tm_mday = BCD2BIN(regs[3]);
844 t->tm_mon = BCD2BIN(regs[4]) - 1;
845 t->tm_year = BCD2BIN(regs[5]) + 100;
848 static int time_to_menelaus(struct rtc_time *t, int regnum)
852 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_sec));
856 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_min));
860 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
861 hour = t->tm_hour + 1;
863 hour = RTC_HR_PM | BIN2BCD(hour - 12);
865 hour = BIN2BCD(hour);
867 hour = BIN2BCD(t->tm_hour);
868 status = menelaus_write_reg(regnum++, hour);
872 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mday));
876 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mon + 1));
880 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_year - 100));
886 dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
891 static int menelaus_read_time(struct device *dev, struct rtc_time *t)
893 struct i2c_msg msg[2];
897 /* block read date and time registers */
898 regs[0] = MENELAUS_RTC_SEC;
900 msg[0].addr = MENELAUS_I2C_ADDRESS;
905 msg[1].addr = MENELAUS_I2C_ADDRESS;
906 msg[1].flags = I2C_M_RD;
907 msg[1].len = sizeof(regs);
910 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
912 dev_err(dev, "%s error %d\n", "read", status);
916 menelaus_to_time(regs, t);
917 t->tm_wday = BCD2BIN(regs[6]);
922 static int menelaus_set_time(struct device *dev, struct rtc_time *t)
926 /* write date and time registers */
927 status = time_to_menelaus(t, MENELAUS_RTC_SEC);
930 status = menelaus_write_reg(MENELAUS_RTC_WKDAY, BIN2BCD(t->tm_wday));
932 dev_err(&the_menelaus->client->dev, "rtc write reg %02x "
933 "err %d\n", MENELAUS_RTC_WKDAY, status);
937 /* now commit the write */
938 status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
940 dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
946 static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
948 struct i2c_msg msg[2];
952 /* block read alarm registers */
953 regs[0] = MENELAUS_RTC_AL_SEC;
955 msg[0].addr = MENELAUS_I2C_ADDRESS;
960 msg[1].addr = MENELAUS_I2C_ADDRESS;
961 msg[1].flags = I2C_M_RD;
962 msg[1].len = sizeof(regs);
965 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
967 dev_err(dev, "%s error %d\n", "alarm read", status);
971 menelaus_to_time(regs, &w->time);
973 w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
975 /* NOTE we *could* check if actually pending... */
981 static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
985 if (the_menelaus->client->irq <= 0 && w->enabled)
988 /* clear previous alarm enable */
989 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
990 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
991 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
992 the_menelaus->rtc_control);
997 /* write alarm registers */
998 status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
1002 /* enable alarm if requested */
1004 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1005 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1006 the_menelaus->rtc_control);
1012 #ifdef CONFIG_RTC_INTF_DEV
1014 static void menelaus_rtc_update_work(struct menelaus_chip *m)
1016 /* report 1/sec update */
1017 local_irq_disable();
1018 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
1022 static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
1026 if (the_menelaus->client->irq <= 0)
1027 return -ENOIOCTLCMD;
1032 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
1034 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1037 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
1039 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1041 /* 1/second "update" IRQ */
1043 if (the_menelaus->uie)
1045 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1046 status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
1047 menelaus_rtc_update_work);
1049 the_menelaus->uie = 1;
1052 if (!the_menelaus->uie)
1054 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1056 the_menelaus->uie = 0;
1059 return -ENOIOCTLCMD;
1061 return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1065 #define menelaus_ioctl NULL
1068 /* REVISIT no compensation register support ... */
1070 static const struct rtc_class_ops menelaus_rtc_ops = {
1071 .ioctl = menelaus_ioctl,
1072 .read_time = menelaus_read_time,
1073 .set_time = menelaus_set_time,
1074 .read_alarm = menelaus_read_alarm,
1075 .set_alarm = menelaus_set_alarm,
1078 static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1081 local_irq_disable();
1082 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
1085 /* then disable it; alarms are oneshot */
1086 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1087 menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1090 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1092 int alarm = (m->client->irq > 0);
1094 /* assume 32KDETEN pin is pulled high */
1095 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1096 dev_dbg(&m->client->dev, "no 32k oscillator\n");
1100 /* support RTC alarm; it can issue wakeups */
1102 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1103 menelaus_rtc_alarm_work) < 0) {
1104 dev_err(&m->client->dev, "can't handle RTC alarm\n");
1107 device_init_wakeup(&m->client->dev, 1);
1110 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1111 m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1112 if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1113 || (m->rtc_control & RTC_CTRL_AL_EN)
1114 || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1115 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1116 dev_warn(&m->client->dev, "rtc clock needs setting\n");
1117 m->rtc_control |= RTC_CTRL_RTC_EN;
1119 m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1120 m->rtc_control &= ~RTC_CTRL_AL_EN;
1121 menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1124 m->rtc = rtc_device_register(DRIVER_NAME,
1126 &menelaus_rtc_ops, THIS_MODULE);
1127 if (IS_ERR(m->rtc)) {
1129 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1130 device_init_wakeup(&m->client->dev, 0);
1132 dev_err(&m->client->dev, "can't register RTC: %d\n",
1133 (int) PTR_ERR(m->rtc));
1134 the_menelaus->rtc = NULL;
1140 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1147 /*-----------------------------------------------------------------------*/
1149 static struct i2c_driver menelaus_i2c_driver;
1151 static int menelaus_probe(struct i2c_client *client,
1152 const struct i2c_device_id *id)
1154 struct menelaus_chip *menelaus;
1157 struct menelaus_platform_data *menelaus_pdata =
1158 client->dev.platform_data;
1161 dev_dbg(&client->dev, "only one %s for now\n",
1166 menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL);
1170 i2c_set_clientdata(client, menelaus);
1172 the_menelaus = menelaus;
1173 menelaus->client = client;
1175 /* If a true probe check the device */
1176 rev = menelaus_read_reg(MENELAUS_REV);
1178 pr_err(DRIVER_NAME ": device not found");
1183 /* Ack and disable all Menelaus interrupts */
1184 menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1185 menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1186 menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1187 menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1188 menelaus->mask1 = 0xff;
1189 menelaus->mask2 = 0xff;
1191 /* Set output buffer strengths */
1192 menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1194 if (client->irq > 0) {
1195 err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED,
1196 DRIVER_NAME, menelaus);
1198 dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
1204 mutex_init(&menelaus->lock);
1205 INIT_WORK(&menelaus->work, menelaus_work);
1207 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1209 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1213 menelaus->vcore_hw_mode = 1;
1215 menelaus->vcore_hw_mode = 0;
1217 if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1218 err = menelaus_pdata->late_init(&client->dev);
1223 menelaus_rtc_init(menelaus);
1227 free_irq(client->irq, menelaus);
1228 flush_scheduled_work();
1234 static int __exit menelaus_remove(struct i2c_client *client)
1236 struct menelaus_chip *menelaus = i2c_get_clientdata(client);
1238 free_irq(client->irq, menelaus);
1240 i2c_set_clientdata(client, NULL);
1241 the_menelaus = NULL;
1245 static const struct i2c_device_id menelaus_id[] = {
1249 MODULE_DEVICE_TABLE(i2c, menelaus_id);
1251 static struct i2c_driver menelaus_i2c_driver = {
1253 .name = DRIVER_NAME,
1255 .probe = menelaus_probe,
1256 .remove = __exit_p(menelaus_remove),
1257 .id_table = menelaus_id,
1260 static int __init menelaus_init(void)
1264 res = i2c_add_driver(&menelaus_i2c_driver);
1266 pr_err(DRIVER_NAME ": driver registration failed\n");
1273 static void __exit menelaus_exit(void)
1275 i2c_del_driver(&menelaus_i2c_driver);
1277 /* FIXME: Shutdown menelaus parts that can be shut down */
1280 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1281 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1282 MODULE_LICENSE("GPL");
1284 module_init(menelaus_init);
1285 module_exit(menelaus_exit);