34 #include <linux/module.h>
35 #include <linux/i2c.h>
37 #include <linux/sched.h>
41 #include <linux/rtc.h>
43 #include <linux/slab.h>
50 #define DRIVER_NAME "menelaus"
52 #define MENELAUS_I2C_ADDRESS 0x72
54 #define MENELAUS_REV 0x01
55 #define MENELAUS_VCORE_CTRL1 0x02
56 #define MENELAUS_VCORE_CTRL2 0x03
57 #define MENELAUS_VCORE_CTRL3 0x04
58 #define MENELAUS_VCORE_CTRL4 0x05
59 #define MENELAUS_VCORE_CTRL5 0x06
60 #define MENELAUS_DCDC_CTRL1 0x07
61 #define MENELAUS_DCDC_CTRL2 0x08
62 #define MENELAUS_DCDC_CTRL3 0x09
63 #define MENELAUS_LDO_CTRL1 0x0A
64 #define MENELAUS_LDO_CTRL2 0x0B
65 #define MENELAUS_LDO_CTRL3 0x0C
66 #define MENELAUS_LDO_CTRL4 0x0D
67 #define MENELAUS_LDO_CTRL5 0x0E
68 #define MENELAUS_LDO_CTRL6 0x0F
69 #define MENELAUS_LDO_CTRL7 0x10
70 #define MENELAUS_LDO_CTRL8 0x11
71 #define MENELAUS_SLEEP_CTRL1 0x12
72 #define MENELAUS_SLEEP_CTRL2 0x13
73 #define MENELAUS_DEVICE_OFF 0x14
74 #define MENELAUS_OSC_CTRL 0x15
75 #define MENELAUS_DETECT_CTRL 0x16
76 #define MENELAUS_INT_MASK1 0x17
77 #define MENELAUS_INT_MASK2 0x18
78 #define MENELAUS_INT_STATUS1 0x19
79 #define MENELAUS_INT_STATUS2 0x1A
80 #define MENELAUS_INT_ACK1 0x1B
81 #define MENELAUS_INT_ACK2 0x1C
82 #define MENELAUS_GPIO_CTRL 0x1D
83 #define MENELAUS_GPIO_IN 0x1E
84 #define MENELAUS_GPIO_OUT 0x1F
85 #define MENELAUS_BBSMS 0x20
86 #define MENELAUS_RTC_CTRL 0x21
87 #define MENELAUS_RTC_UPDATE 0x22
88 #define MENELAUS_RTC_SEC 0x23
89 #define MENELAUS_RTC_MIN 0x24
90 #define MENELAUS_RTC_HR 0x25
91 #define MENELAUS_RTC_DAY 0x26
92 #define MENELAUS_RTC_MON 0x27
93 #define MENELAUS_RTC_YR 0x28
94 #define MENELAUS_RTC_WKDAY 0x29
95 #define MENELAUS_RTC_AL_SEC 0x2A
96 #define MENELAUS_RTC_AL_MIN 0x2B
97 #define MENELAUS_RTC_AL_HR 0x2C
98 #define MENELAUS_RTC_AL_DAY 0x2D
99 #define MENELAUS_RTC_AL_MON 0x2E
100 #define MENELAUS_RTC_AL_YR 0x2F
101 #define MENELAUS_RTC_COMP_MSB 0x30
102 #define MENELAUS_RTC_COMP_LSB 0x31
103 #define MENELAUS_S1_PULL_EN 0x32
104 #define MENELAUS_S1_PULL_DIR 0x33
105 #define MENELAUS_S2_PULL_EN 0x34
106 #define MENELAUS_S2_PULL_DIR 0x35
107 #define MENELAUS_MCT_CTRL1 0x36
108 #define MENELAUS_MCT_CTRL2 0x37
109 #define MENELAUS_MCT_CTRL3 0x38
110 #define MENELAUS_MCT_PIN_ST 0x39
111 #define MENELAUS_DEBOUNCE1 0x3A
113 #define IH_MENELAUS_IRQS 12
114 #define MENELAUS_MMC_S1CD_IRQ 0
115 #define MENELAUS_MMC_S2CD_IRQ 1
116 #define MENELAUS_MMC_S1D1_IRQ 2
117 #define MENELAUS_MMC_S2D1_IRQ 3
118 #define MENELAUS_LOWBAT_IRQ 4
119 #define MENELAUS_HOTDIE_IRQ 5
120 #define MENELAUS_UVLO_IRQ 6
121 #define MENELAUS_TSHUT_IRQ 7
122 #define MENELAUS_RTCTMR_IRQ 8
123 #define MENELAUS_RTCALM_IRQ 9
124 #define MENELAUS_RTCERR_IRQ 10
125 #define MENELAUS_PSHBTN_IRQ 11
126 #define MENELAUS_RESERVED12_IRQ 12
127 #define MENELAUS_RESERVED13_IRQ 13
128 #define MENELAUS_RESERVED14_IRQ 14
129 #define MENELAUS_RESERVED15_IRQ 15
132 #define VCORE_CTRL1_BYP_COMP (1 << 5)
133 #define VCORE_CTRL1_HW_NSW (1 << 7)
136 #define GPIO_CTRL_SLOTSELEN (1 << 5)
137 #define GPIO_CTRL_SLPCTLEN (1 << 6)
138 #define GPIO1_DIR_INPUT (1 << 0)
139 #define GPIO2_DIR_INPUT (1 << 1)
140 #define GPIO3_DIR_INPUT (1 << 2)
143 #define MCT_CTRL1_S1_CMD_OD (1 << 2)
144 #define MCT_CTRL1_S2_CMD_OD (1 << 3)
147 #define MCT_CTRL2_VS2_SEL_D0 (1 << 0)
148 #define MCT_CTRL2_VS2_SEL_D1 (1 << 1)
149 #define MCT_CTRL2_S1CD_BUFEN (1 << 4)
150 #define MCT_CTRL2_S2CD_BUFEN (1 << 5)
151 #define MCT_CTRL2_S1CD_DBEN (1 << 6)
152 #define MCT_CTRL2_S2CD_BEN (1 << 7)
155 #define MCT_CTRL3_SLOT1_EN (1 << 0)
156 #define MCT_CTRL3_SLOT2_EN (1 << 1)
157 #define MCT_CTRL3_S1_AUTO_EN (1 << 2)
158 #define MCT_CTRL3_S2_AUTO_EN (1 << 3)
161 #define MCT_PIN_ST_S1_CD_ST (1 << 0)
162 #define MCT_PIN_ST_S2_CD_ST (1 << 1)
164 static void menelaus_work(
struct work_struct *_menelaus);
170 #ifdef CONFIG_RTC_DRV_TWL92330
184 static int menelaus_write_reg(
int reg,
u8 value)
196 static int menelaus_read_reg(
int reg)
206 static int menelaus_enable_irq(
int irq)
210 the_menelaus->
mask2 &= ~(1 << irq);
212 the_menelaus->
mask2);
214 the_menelaus->
mask1 &= ~(1 << irq);
216 the_menelaus->
mask1);
220 static int menelaus_disable_irq(
int irq)
224 the_menelaus->
mask2 |= (1 << irq);
226 the_menelaus->
mask2);
228 the_menelaus->
mask1 |= (1 << irq);
230 the_menelaus->
mask1);
234 static int menelaus_ack_irq(
int irq)
243 static int menelaus_add_irq_work(
int irq,
250 ret = menelaus_enable_irq(irq);
257 static int menelaus_remove_irq_work(
int irq)
262 ret = menelaus_disable_irq(irq);
275 static void menelaus_mmc_cd_work(
struct menelaus_chip *menelaus_hw)
278 unsigned char card_mask = 0;
302 if (slot != 1 && slot != 2)
353 if (slot != 1 && slot != 2)
419 menelaus_mmc_cd_work);
423 menelaus_mmc_cd_work);
427 menelaus_mmc_cd_work);
431 menelaus_mmc_cd_work);
462 static int menelaus_set_voltage(
const struct menelaus_vtg *vtg,
int mV,
463 int vtg_val,
int mode)
472 ret = menelaus_read_reg(vtg->
vtg_reg);
479 "to %d mV (reg 0x%02x, val 0x%02x)\n",
482 ret = menelaus_write_reg(vtg->
vtg_reg, val);
486 ret = menelaus_write_reg(vtg->
mode_reg, mode);
501 for (i = 0; i <
n; i++, tbl++)
543 val = menelaus_get_vtg_value(mV, vcore_values,
548 dev_dbg(&c->
dev,
"Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
566 rval = menelaus_get_vtg_value(roof_mV, vcore_values,
570 fval = menelaus_get_vtg_value(floor_mV, vcore_values,
575 dev_dbg(&c->
dev,
"Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
618 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
620 val = menelaus_get_vtg_value(mV, vmem_values,
ARRAY_SIZE(vmem_values));
623 return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
647 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
649 val = menelaus_get_vtg_value(mV, vio_values,
ARRAY_SIZE(vio_values));
652 return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
688 if (dcdc != 2 && dcdc != 3)
696 return menelaus_set_voltage(vtg, 0, 0, 0);
698 val = menelaus_get_vtg_value(mV, vdcdc_values,
702 return menelaus_set_voltage(vtg, mV, val, 0x03);
725 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
727 val = menelaus_get_vtg_value(mV, vmmc_values,
ARRAY_SIZE(vmmc_values));
730 return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
755 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
757 val = menelaus_get_vtg_value(mV, vaux_values,
ARRAY_SIZE(vaux_values));
760 return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
780 dev_dbg(&c->
dev,
"regulator sleep configuration: %02x\n", val);
799 static void menelaus_work(
struct work_struct *_menelaus)
809 & ~menelaus->
mask2) << 8;
816 int irq = fls(isr) - 1;
820 menelaus_disable_irq(irq);
821 menelaus_ack_irq(irq);
825 menelaus_enable_irq(irq);
835 static irqreturn_t menelaus_irq(
int irq,
void *_menelaus)
852 #ifdef CONFIG_RTC_DRV_TWL92330
854 #define RTC_CTRL_RTC_EN (1 << 0)
855 #define RTC_CTRL_AL_EN (1 << 1)
856 #define RTC_CTRL_MODE12 (1 << 2)
857 #define RTC_CTRL_EVERY_MASK (3 << 3)
858 #define RTC_CTRL_EVERY_SEC (0 << 3)
859 #define RTC_CTRL_EVERY_MIN (1 << 3)
860 #define RTC_CTRL_EVERY_HR (2 << 3)
861 #define RTC_CTRL_EVERY_DAY (3 << 3)
863 #define RTC_UPDATE_EVERY 0x08
865 #define RTC_HR_PM (1 << 7)
871 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
873 if (regs[2] & RTC_HR_PM)
882 static int time_to_menelaus(
struct rtc_time *t,
int regnum)
894 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
897 hour = RTC_HR_PM |
bin2bcd(hour - 12);
902 status = menelaus_write_reg(regnum++, hour);
910 status = menelaus_write_reg(regnum++,
bin2bcd(t->
tm_mon + 1));
920 dev_err(&the_menelaus->
client->dev,
"rtc write reg %02x, err %d\n",
946 dev_err(dev,
"%s error %d\n",
"read", status);
950 menelaus_to_time(regs, t);
956 static int menelaus_set_time(
struct device *dev,
struct rtc_time *t)
974 dev_err(&the_menelaus->
client->dev,
"rtc commit time, err %d\n",
1001 dev_err(dev,
"%s error %d\n",
"alarm read", status);
1005 menelaus_to_time(regs, &w->
time);
1007 w->
enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
1023 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
1024 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1026 the_menelaus->rtc_control);
1038 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1040 the_menelaus->rtc_control);
1046 #ifdef CONFIG_RTC_INTF_DEV
1056 static int menelaus_ioctl(
struct device *dev,
unsigned cmd,
unsigned long arg)
1060 if (the_menelaus->
client->irq <= 0)
1066 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
1068 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1071 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
1073 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1077 if (the_menelaus->uie)
1081 menelaus_rtc_update_work);
1083 the_menelaus->uie = 1;
1086 if (!the_menelaus->uie)
1090 the_menelaus->uie = 0;
1099 #define menelaus_ioctl NULL
1105 .
ioctl = menelaus_ioctl,
1106 .read_time = menelaus_read_time,
1107 .set_time = menelaus_set_time,
1108 .read_alarm = menelaus_read_alarm,
1109 .set_alarm = menelaus_set_alarm,
1120 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1137 menelaus_rtc_alarm_work) < 0) {
1146 if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1147 || (m->rtc_control & RTC_CTRL_AL_EN)
1148 || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1149 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1151 m->rtc_control |= RTC_CTRL_RTC_EN;
1153 m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1154 m->rtc_control &= ~RTC_CTRL_AL_EN;
1161 if (IS_ERR(m->rtc)) {
1167 (
int) PTR_ERR(m->rtc));
1168 the_menelaus->rtc =
NULL;
1174 static inline void menelaus_rtc_init(
struct menelaus_chip *m)
1183 static struct i2c_driver menelaus_i2c_driver;
1192 client->
dev.platform_data;
1195 dev_dbg(&client->
dev,
"only one %s for now\n",
1200 menelaus = kzalloc(
sizeof *menelaus,
GFP_KERNEL);
1204 i2c_set_clientdata(client, menelaus);
1206 the_menelaus = menelaus;
1222 menelaus->
mask1 = 0xff;
1223 menelaus->
mask2 = 0xff;
1228 if (client->
irq > 0) {
1232 dev_dbg(&client->
dev,
"can't get IRQ %d, err %d\n",
1241 pr_info(
"Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1257 menelaus_rtc_init(menelaus);
1270 struct menelaus_chip *menelaus = i2c_get_clientdata(client);
1275 the_menelaus =
NULL;
1285 static struct i2c_driver menelaus_i2c_driver = {
1289 .probe = menelaus_probe,
1290 .remove =
__exit_p(menelaus_remove),
1291 .id_table = menelaus_id,
1294 static int __init menelaus_init(
void)
1298 res = i2c_add_driver(&menelaus_i2c_driver);
1307 static void __exit menelaus_exit(
void)