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>
44 #include <linux/i2c/menelaus.h>
46 #include <asm/mach-types.h>
47 #include <asm/mach/irq.h>
49 #include <mach/gpio.h>
51 #define DRIVER_NAME "menelaus"
54 static void menelaus_work(struct work_struct *_menelaus);
56 struct menelaus_chip {
58 struct i2c_client *client;
59 struct work_struct work;
60 #ifdef CONFIG_RTC_DRV_TWL92330
61 struct rtc_device *rtc;
65 unsigned vcore_hw_mode:1;
67 void (*handlers[16])(struct menelaus_chip *);
68 void (*mmc_callback)(void *data, u8 mask);
69 void *mmc_callback_data;
72 static struct menelaus_chip *the_menelaus;
74 static int menelaus_write_reg(int reg, u8 value)
76 int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
79 dev_err(&the_menelaus->client->dev, "write error");
86 static int menelaus_read_reg(int reg)
88 int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
91 dev_err(&the_menelaus->client->dev, "read error");
96 static int menelaus_enable_irq(int irq)
100 the_menelaus->mask2 &= ~(1 << irq);
101 return menelaus_write_reg(MENELAUS_INT_MASK2,
102 the_menelaus->mask2);
104 the_menelaus->mask1 &= ~(1 << irq);
105 return menelaus_write_reg(MENELAUS_INT_MASK1,
106 the_menelaus->mask1);
110 static int menelaus_disable_irq(int irq)
114 the_menelaus->mask2 |= (1 << irq);
115 return menelaus_write_reg(MENELAUS_INT_MASK2,
116 the_menelaus->mask2);
118 the_menelaus->mask1 |= (1 << irq);
119 return menelaus_write_reg(MENELAUS_INT_MASK1,
120 the_menelaus->mask1);
124 static int menelaus_ack_irq(int irq)
127 return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
129 return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
132 /* Adds a handler for an interrupt. Does not run in interrupt context */
133 static int menelaus_add_irq_work(int irq,
134 void (*handler)(struct menelaus_chip *))
138 mutex_lock(&the_menelaus->lock);
139 the_menelaus->handlers[irq] = handler;
140 ret = menelaus_enable_irq(irq);
141 mutex_unlock(&the_menelaus->lock);
146 /* Removes handler for an interrupt */
147 static int menelaus_remove_irq_work(int irq)
151 mutex_lock(&the_menelaus->lock);
152 ret = menelaus_disable_irq(irq);
153 the_menelaus->handlers[irq] = NULL;
154 mutex_unlock(&the_menelaus->lock);
160 * Gets scheduled when a card detect interrupt happens. Note that in some cases
161 * this line is wired to card cover switch rather than the card detect switch
162 * in each slot. In this case the cards are not seen by menelaus.
163 * FIXME: Add handling for D1 too
165 static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
168 unsigned char card_mask = 0;
170 reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
175 card_mask |= MCT_PIN_ST_S1_CD_ST;
178 card_mask |= MCT_PIN_ST_S2_CD_ST;
180 if (menelaus_hw->mmc_callback)
181 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
186 * Toggles the MMC slots between open-drain and push-pull mode.
188 int menelaus_set_mmc_opendrain(int slot, int enable)
192 if (slot != 1 && slot != 2)
194 mutex_lock(&the_menelaus->lock);
195 ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
197 mutex_unlock(&the_menelaus->lock);
203 val |= MCT_CTRL1_S1_CMD_OD;
205 val &= ~MCT_CTRL1_S1_CMD_OD;
208 val |= MCT_CTRL1_S2_CMD_OD;
210 val &= ~MCT_CTRL1_S2_CMD_OD;
212 ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
213 mutex_unlock(&the_menelaus->lock);
217 EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
219 int menelaus_set_slot_sel(int enable)
223 mutex_lock(&the_menelaus->lock);
224 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
227 ret |= GPIO2_DIR_INPUT;
229 ret |= GPIO_CTRL_SLOTSELEN;
231 ret &= ~GPIO_CTRL_SLOTSELEN;
232 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
234 mutex_unlock(&the_menelaus->lock);
237 EXPORT_SYMBOL(menelaus_set_slot_sel);
239 int menelaus_enable_slot(int slot, int enable)
243 mutex_lock(&the_menelaus->lock);
244 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
250 val |= MCT_CTRL3_SLOT1_EN;
252 val &= ~MCT_CTRL3_SLOT1_EN;
255 val |= MCT_CTRL3_SLOT2_EN;
257 val &= MCT_CTRL3_SLOT2_EN;
259 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
262 mutex_unlock(&the_menelaus->lock);
265 EXPORT_SYMBOL(menelaus_enable_slot);
267 int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
271 if (slot != 1 && slot != 2)
276 mutex_lock(&the_menelaus->lock);
278 ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
284 val |= MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN;
286 val &= ~(MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN);
289 val |= MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN;
291 val &= ~(MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN);
293 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
297 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
303 val |= MCT_CTRL3_SLOT1_EN;
305 val &= ~MCT_CTRL3_SLOT1_EN;
310 val |= MCT_CTRL3_SLOT2_EN;
312 val &= ~MCT_CTRL3_SLOT2_EN;
313 b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
314 b &= ~(MCT_CTRL2_VS2_SEL_D0 | MCT_CTRL2_VS2_SEL_D1);
316 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
320 /* Disable autonomous shutdown */
321 val &= ~(MCT_CTRL3_S1_AUTO_EN | MCT_CTRL3_S2_AUTO_EN);
322 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
324 mutex_unlock(&the_menelaus->lock);
327 EXPORT_SYMBOL(menelaus_set_mmc_slot);
329 int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
334 the_menelaus->mmc_callback_data = data;
335 the_menelaus->mmc_callback = callback;
336 ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
337 menelaus_mmc_cd_work);
340 ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
341 menelaus_mmc_cd_work);
344 ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
345 menelaus_mmc_cd_work);
348 ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
349 menelaus_mmc_cd_work);
353 EXPORT_SYMBOL(menelaus_register_mmc_callback);
355 void menelaus_unregister_mmc_callback(void)
357 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
358 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
359 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
360 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
362 the_menelaus->mmc_callback = NULL;
363 the_menelaus->mmc_callback_data = 0;
365 EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
367 struct menelaus_vtg {
375 struct menelaus_vtg_value {
380 static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
381 int vtg_val, int mode)
384 struct i2c_client *c = the_menelaus->client;
386 mutex_lock(&the_menelaus->lock);
390 ret = menelaus_read_reg(vtg->vtg_reg);
393 val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
394 val |= vtg_val << vtg->vtg_shift;
396 dev_dbg(&c->dev, "Setting voltage '%s'"
397 "to %d mV (reg 0x%02x, val 0x%02x)\n",
398 vtg->name, mV, vtg->vtg_reg, val);
400 ret = menelaus_write_reg(vtg->vtg_reg, val);
404 ret = menelaus_write_reg(vtg->mode_reg, mode);
406 mutex_unlock(&the_menelaus->lock);
408 /* Wait for voltage to stabilize */
414 static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
419 for (i = 0; i < n; i++, tbl++)
426 * Vcore can be programmed in two ways:
427 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
428 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
431 * Call correct 'set' function accordingly
434 static const struct menelaus_vtg_value vcore_values[] = {
456 int menelaus_set_vcore_sw(unsigned int mV)
459 struct i2c_client *c = the_menelaus->client;
461 val = menelaus_get_vtg_value(mV, vcore_values,
462 ARRAY_SIZE(vcore_values));
466 dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
468 /* Set SW mode and the voltage in one go. */
469 mutex_lock(&the_menelaus->lock);
470 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
472 the_menelaus->vcore_hw_mode = 0;
473 mutex_unlock(&the_menelaus->lock);
479 int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
481 int fval, rval, val, ret;
482 struct i2c_client *c = the_menelaus->client;
484 rval = menelaus_get_vtg_value(roof_mV, vcore_values,
485 ARRAY_SIZE(vcore_values));
488 fval = menelaus_get_vtg_value(floor_mV, vcore_values,
489 ARRAY_SIZE(vcore_values));
493 dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
496 mutex_lock(&the_menelaus->lock);
497 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
500 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
503 if (!the_menelaus->vcore_hw_mode) {
504 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
505 /* HW mode, turn OFF byte comparator */
506 val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP);
507 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
508 the_menelaus->vcore_hw_mode = 1;
512 mutex_unlock(&the_menelaus->lock);
516 static const struct menelaus_vtg vmem_vtg = {
518 .vtg_reg = MENELAUS_LDO_CTRL1,
521 .mode_reg = MENELAUS_LDO_CTRL3,
524 static const struct menelaus_vtg_value vmem_values[] = {
531 int menelaus_set_vmem(unsigned int mV)
536 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
538 val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
541 return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
543 EXPORT_SYMBOL(menelaus_set_vmem);
545 static const struct menelaus_vtg vio_vtg = {
547 .vtg_reg = MENELAUS_LDO_CTRL1,
550 .mode_reg = MENELAUS_LDO_CTRL4,
553 static const struct menelaus_vtg_value vio_values[] = {
560 int menelaus_set_vio(unsigned int mV)
565 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
567 val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
570 return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
572 EXPORT_SYMBOL(menelaus_set_vio);
574 static const struct menelaus_vtg_value vdcdc_values[] = {
585 static const struct menelaus_vtg vdcdc2_vtg = {
587 .vtg_reg = MENELAUS_DCDC_CTRL1,
590 .mode_reg = MENELAUS_DCDC_CTRL2,
593 static const struct menelaus_vtg vdcdc3_vtg = {
595 .vtg_reg = MENELAUS_DCDC_CTRL1,
598 .mode_reg = MENELAUS_DCDC_CTRL3,
601 int menelaus_set_vdcdc(int dcdc, unsigned int mV)
603 const struct menelaus_vtg *vtg;
606 if (dcdc != 2 && dcdc != 3)
614 return menelaus_set_voltage(vtg, 0, 0, 0);
616 val = menelaus_get_vtg_value(mV, vdcdc_values,
617 ARRAY_SIZE(vdcdc_values));
620 return menelaus_set_voltage(vtg, mV, val, 0x03);
623 static const struct menelaus_vtg_value vmmc_values[] = {
630 static const struct menelaus_vtg vmmc_vtg = {
632 .vtg_reg = MENELAUS_LDO_CTRL1,
635 .mode_reg = MENELAUS_LDO_CTRL7,
638 int menelaus_set_vmmc(unsigned int mV)
643 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
645 val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
648 return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
650 EXPORT_SYMBOL(menelaus_set_vmmc);
653 static const struct menelaus_vtg_value vaux_values[] = {
660 static const struct menelaus_vtg vaux_vtg = {
662 .vtg_reg = MENELAUS_LDO_CTRL1,
665 .mode_reg = MENELAUS_LDO_CTRL6,
668 int menelaus_set_vaux(unsigned int mV)
673 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
675 val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
678 return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
680 EXPORT_SYMBOL(menelaus_set_vaux);
682 int menelaus_get_slot_pin_states(void)
684 return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
686 EXPORT_SYMBOL(menelaus_get_slot_pin_states);
688 int menelaus_set_regulator_sleep(int enable, u32 val)
691 struct i2c_client *c = the_menelaus->client;
693 mutex_lock(&the_menelaus->lock);
694 ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
698 dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
700 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
703 t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT);
708 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
710 mutex_unlock(&the_menelaus->lock);
714 /*-----------------------------------------------------------------------*/
716 /* Handles Menelaus interrupts. Does not run in interrupt context */
717 static void menelaus_work(struct work_struct *_menelaus)
719 struct menelaus_chip *menelaus =
720 container_of(_menelaus, struct menelaus_chip, work);
721 void (*handler)(struct menelaus_chip *menelaus);
726 isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
727 & ~menelaus->mask2) << 8;
728 isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
734 int irq = fls(isr) - 1;
737 mutex_lock(&menelaus->lock);
738 menelaus_disable_irq(irq);
739 menelaus_ack_irq(irq);
740 handler = menelaus->handlers[irq];
743 menelaus_enable_irq(irq);
744 mutex_unlock(&menelaus->lock);
747 enable_irq(menelaus->client->irq);
751 * We cannot use I2C in interrupt context, so we just schedule work.
753 static irqreturn_t menelaus_irq(int irq, void *_menelaus)
755 struct menelaus_chip *menelaus = _menelaus;
757 disable_irq_nosync(irq);
758 (void)schedule_work(&menelaus->work);
763 /*-----------------------------------------------------------------------*/
766 * The RTC needs to be set once, then it runs on backup battery power.
767 * It supports alarms, including system wake alarms (from some modes);
768 * and 1/second IRQs if requested.
770 #ifdef CONFIG_RTC_DRV_TWL92330
772 #define RTC_CTRL_RTC_EN (1 << 0)
773 #define RTC_CTRL_AL_EN (1 << 1)
774 #define RTC_CTRL_MODE12 (1 << 2)
775 #define RTC_CTRL_EVERY_MASK (3 << 3)
776 #define RTC_CTRL_EVERY_SEC (0 << 3)
777 #define RTC_CTRL_EVERY_MIN (1 << 3)
778 #define RTC_CTRL_EVERY_HR (2 << 3)
779 #define RTC_CTRL_EVERY_DAY (3 << 3)
781 #define RTC_UPDATE_EVERY 0x08
783 #define RTC_HR_PM (1 << 7)
785 static void menelaus_to_time(char *regs, struct rtc_time *t)
787 t->tm_sec = BCD2BIN(regs[0]);
788 t->tm_min = BCD2BIN(regs[1]);
789 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
790 t->tm_hour = BCD2BIN(regs[2] & 0x1f) - 1;
791 if (regs[2] & RTC_HR_PM)
794 t->tm_hour = BCD2BIN(regs[2] & 0x3f);
795 t->tm_mday = BCD2BIN(regs[3]);
796 t->tm_mon = BCD2BIN(regs[4]) - 1;
797 t->tm_year = BCD2BIN(regs[5]) + 100;
800 static int time_to_menelaus(struct rtc_time *t, int regnum)
804 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_sec));
808 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_min));
812 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
813 hour = t->tm_hour + 1;
815 hour = RTC_HR_PM | BIN2BCD(hour - 12);
817 hour = BIN2BCD(hour);
819 hour = BIN2BCD(t->tm_hour);
820 status = menelaus_write_reg(regnum++, hour);
824 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mday));
828 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mon + 1));
832 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_year - 100));
838 dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
843 static int menelaus_read_time(struct device *dev, struct rtc_time *t)
845 struct i2c_msg msg[2];
849 /* block read date and time registers */
850 regs[0] = MENELAUS_RTC_SEC;
852 msg[0].addr = MENELAUS_I2C_ADDRESS;
857 msg[1].addr = MENELAUS_I2C_ADDRESS;
858 msg[1].flags = I2C_M_RD;
859 msg[1].len = sizeof(regs);
862 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
864 dev_err(dev, "%s error %d\n", "read", status);
868 menelaus_to_time(regs, t);
869 t->tm_wday = BCD2BIN(regs[6]);
874 static int menelaus_set_time(struct device *dev, struct rtc_time *t)
878 /* write date and time registers */
879 status = time_to_menelaus(t, MENELAUS_RTC_SEC);
882 status = menelaus_write_reg(MENELAUS_RTC_WKDAY, BIN2BCD(t->tm_wday));
884 dev_err(&the_menelaus->client->dev, "rtc write reg %02x"
885 "err %d\n", MENELAUS_RTC_WKDAY, status);
889 /* now commit the write */
890 status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
892 dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
898 static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
900 struct i2c_msg msg[2];
904 /* block read alarm registers */
905 regs[0] = MENELAUS_RTC_AL_SEC;
907 msg[0].addr = MENELAUS_I2C_ADDRESS;
912 msg[1].addr = MENELAUS_I2C_ADDRESS;
913 msg[1].flags = I2C_M_RD;
914 msg[1].len = sizeof(regs);
917 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
919 dev_err(dev, "%s error %d\n", "alarm read", status);
923 menelaus_to_time(regs, &w->time);
925 w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
927 /* NOTE we *could* check if actually pending... */
933 static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
937 if (the_menelaus->client->irq <= 0 && w->enabled)
940 /* clear previous alarm enable */
941 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
942 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
943 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
944 the_menelaus->rtc_control);
949 /* write alarm registers */
950 status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
954 /* enable alarm if requested */
956 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
957 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
958 the_menelaus->rtc_control);
964 #ifdef CONFIG_RTC_INTF_DEV
966 static void menelaus_rtc_update_work(struct menelaus_chip *m)
968 /* report 1/sec update */
970 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
974 static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
978 if (the_menelaus->client->irq <= 0)
984 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
986 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
989 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
991 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
993 /* 1/second "update" IRQ */
995 if (the_menelaus->uie)
997 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
998 status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
999 menelaus_rtc_update_work);
1001 the_menelaus->uie = 1;
1004 if (!the_menelaus->uie)
1006 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1008 the_menelaus->uie = 0;
1011 return -ENOIOCTLCMD;
1013 return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1017 #define menelaus_ioctl NULL
1020 /* REVISIT no compensation register support ... */
1022 static const struct rtc_class_ops menelaus_rtc_ops = {
1023 .ioctl = menelaus_ioctl,
1024 .read_time = menelaus_read_time,
1025 .set_time = menelaus_set_time,
1026 .read_alarm = menelaus_read_alarm,
1027 .set_alarm = menelaus_set_alarm,
1030 static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1033 local_irq_disable();
1034 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
1037 /* then disable it; alarms are oneshot */
1038 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1039 menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1042 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1044 int alarm = (m->client->irq > 0);
1046 /* assume 32KDETEN pin is pulled high */
1047 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1048 dev_dbg(&m->client->dev, "no 32k oscillator\n");
1052 /* support RTC alarm; it can issue wakeups */
1054 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1055 menelaus_rtc_alarm_work) < 0) {
1056 dev_err(&m->client->dev, "can't handle RTC alarm\n");
1059 device_init_wakeup(&m->client->dev, 1);
1062 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1063 m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1064 if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1065 || (m->rtc_control & RTC_CTRL_AL_EN)
1066 || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1067 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1068 dev_warn(&m->client->dev, "rtc clock needs setting\n");
1069 m->rtc_control |= RTC_CTRL_RTC_EN;
1071 m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1072 m->rtc_control &= ~RTC_CTRL_AL_EN;
1073 menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1076 m->rtc = rtc_device_register(DRIVER_NAME,
1078 &menelaus_rtc_ops, THIS_MODULE);
1079 if (IS_ERR(m->rtc)) {
1081 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1082 device_init_wakeup(&m->client->dev, 0);
1084 dev_err(&m->client->dev, "can't register RTC: %d\n",
1085 (int) PTR_ERR(m->rtc));
1086 the_menelaus->rtc = NULL;
1092 static inline void menelaus_rtc_init(struct menelaus_chip *m)
1099 /*-----------------------------------------------------------------------*/
1101 static struct i2c_driver menelaus_i2c_driver;
1103 static int menelaus_probe(struct i2c_client *client,
1104 const struct i2c_device_id *id)
1106 struct menelaus_chip *menelaus;
1109 struct menelaus_platform_data *menelaus_pdata =
1110 client->dev.platform_data;
1113 dev_dbg(&client->dev, "only one %s for now\n",
1118 menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL);
1122 i2c_set_clientdata(client, menelaus);
1124 the_menelaus = menelaus;
1125 menelaus->client = client;
1127 /* If a true probe check the device */
1128 rev = menelaus_read_reg(MENELAUS_REV);
1130 dev_err(&client->dev, "device not found");
1135 /* Ack and disable all Menelaus interrupts */
1136 menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1137 menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1138 menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1139 menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1140 menelaus->mask1 = 0xff;
1141 menelaus->mask2 = 0xff;
1143 /* Set output buffer strengths */
1144 menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1146 if (client->irq > 0) {
1147 err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED,
1148 DRIVER_NAME, menelaus);
1150 dev_dbg(&client->dev, "can't get IRQ %d, err %d",
1156 mutex_init(&menelaus->lock);
1157 INIT_WORK(&menelaus->work, menelaus_work);
1159 dev_info(&client->dev, "Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1161 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1165 menelaus->vcore_hw_mode = 1;
1167 menelaus->vcore_hw_mode = 0;
1169 if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1170 err = menelaus_pdata->late_init(&client->dev);
1175 menelaus_rtc_init(menelaus);
1179 free_irq(client->irq, menelaus);
1180 flush_scheduled_work();
1186 static int __exit menelaus_remove(struct i2c_client *client)
1188 struct menelaus_chip *menelaus = i2c_get_clientdata(client);
1190 free_irq(client->irq, menelaus);
1192 i2c_set_clientdata(client, NULL);
1193 the_menelaus = NULL;
1197 static const struct i2c_device_id menelaus_id[] = {
1201 MODULE_DEVICE_TABLE(i2c, menelaus_id);
1203 static struct i2c_driver menelaus_i2c_driver = {
1205 .name = DRIVER_NAME,
1207 .probe = menelaus_probe,
1208 .remove = __exit_p(menelaus_remove),
1209 .id_table = menelaus_id,
1212 static int __init menelaus_init(void)
1214 return i2c_add_driver(&menelaus_i2c_driver);
1217 static void __exit menelaus_exit(void)
1219 i2c_del_driver(&menelaus_i2c_driver);
1221 /* FIXME: Shutdown menelaus parts that can be shut down */
1224 MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1225 MODULE_DESCRIPTION("I2C interface for Menelaus.");
1226 MODULE_LICENSE("GPL");
1228 module_init(menelaus_init);
1229 module_exit(menelaus_exit);