2 * Micro Crystal RV-3029C2 rtc class driver
4 * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
6 * based on previously existing rtc class drivers
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * NOTE: Currently this driver only supports the bare minimum for read
13 * and write the RTC and alarms. The extra features provided by this chip
14 * (trickle charger, eeprom, T° compensation) are unavailable.
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/bcd.h>
20 #include <linux/rtc.h>
24 #define RV3029C2_ONOFF_CTRL 0x00
25 #define RV3029C2_IRQ_CTRL 0x01
26 #define RV3029C2_IRQ_CTRL_AIE (1 << 0)
27 #define RV3029C2_IRQ_FLAGS 0x02
28 #define RV3029C2_IRQ_FLAGS_AF (1 << 0)
29 #define RV3029C2_STATUS 0x03
30 #define RV3029C2_STATUS_VLOW1 (1 << 2)
31 #define RV3029C2_STATUS_VLOW2 (1 << 3)
32 #define RV3029C2_STATUS_SR (1 << 4)
33 #define RV3029C2_STATUS_PON (1 << 5)
34 #define RV3029C2_STATUS_EEBUSY (1 << 7)
35 #define RV3029C2_RST_CTRL 0x04
36 #define RV3029C2_CONTROL_SECTION_LEN 0x05
39 #define RV3029C2_W_SEC 0x08
40 #define RV3029C2_W_MINUTES 0x09
41 #define RV3029C2_W_HOURS 0x0A
42 #define RV3029C2_REG_HR_12_24 (1<<6) /* 24h/12h mode */
43 #define RV3029C2_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
44 #define RV3029C2_W_DATE 0x0B
45 #define RV3029C2_W_DAYS 0x0C
46 #define RV3029C2_W_MONTHS 0x0D
47 #define RV3029C2_W_YEARS 0x0E
48 #define RV3029C2_WATCH_SECTION_LEN 0x07
51 #define RV3029C2_A_SC 0x10
52 #define RV3029C2_A_MN 0x11
53 #define RV3029C2_A_HR 0x12
54 #define RV3029C2_A_DT 0x13
55 #define RV3029C2_A_DW 0x14
56 #define RV3029C2_A_MO 0x15
57 #define RV3029C2_A_YR 0x16
58 #define RV3029C2_ALARM_SECTION_LEN 0x07
61 #define RV3029C2_TIMER_LOW 0x18
62 #define RV3029C2_TIMER_HIGH 0x19
64 /* temperature section */
65 #define RV3029C2_TEMP_PAGE 0x20
67 /* eeprom data section */
68 #define RV3029C2_E2P_EEDATA1 0x28
69 #define RV3029C2_E2P_EEDATA2 0x29
71 /* eeprom control section */
72 #define RV3029C2_CONTROL_E2P_EECTRL 0x30
73 #define RV3029C2_TRICKLE_1K (1<<0) /* 1K resistance */
74 #define RV3029C2_TRICKLE_5K (1<<1) /* 5K resistance */
75 #define RV3029C2_TRICKLE_20K (1<<2) /* 20K resistance */
76 #define RV3029C2_TRICKLE_80K (1<<3) /* 80K resistance */
77 #define RV3029C2_CONTROL_E2P_XTALOFFSET 0x31
78 #define RV3029C2_CONTROL_E2P_QCOEF 0x32
79 #define RV3029C2_CONTROL_E2P_TURNOVER 0x33
81 /* user ram section */
82 #define RV3029C2_USR1_RAM_PAGE 0x38
83 #define RV3029C2_USR1_SECTION_LEN 0x04
84 #define RV3029C2_USR2_RAM_PAGE 0x3C
85 #define RV3029C2_USR2_SECTION_LEN 0x04
88 rv3029c2_i2c_read_regs(struct i2c_client
*client
, u8 reg
, u8
*buf
,
93 if ((reg
> RV3029C2_USR1_RAM_PAGE
+ 7) ||
94 (reg
+ len
> RV3029C2_USR1_RAM_PAGE
+ 8))
97 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, len
, buf
);
106 rv3029c2_i2c_write_regs(struct i2c_client
*client
, u8 reg
, u8
const buf
[],
109 if ((reg
> RV3029C2_USR1_RAM_PAGE
+ 7) ||
110 (reg
+ len
> RV3029C2_USR1_RAM_PAGE
+ 8))
113 return i2c_smbus_write_i2c_block_data(client
, reg
, len
, buf
);
117 rv3029c2_i2c_get_sr(struct i2c_client
*client
, u8
*buf
)
119 int ret
= rv3029c2_i2c_read_regs(client
, RV3029C2_STATUS
, buf
, 1);
123 dev_dbg(&client
->dev
, "status = 0x%.2x (%d)\n", buf
[0], buf
[0]);
128 rv3029c2_i2c_set_sr(struct i2c_client
*client
, u8 val
)
134 sr
= rv3029c2_i2c_write_regs(client
, RV3029C2_STATUS
, buf
, 1);
135 dev_dbg(&client
->dev
, "status = 0x%.2x (%d)\n", buf
[0], buf
[0]);
142 rv3029c2_i2c_read_time(struct i2c_client
*client
, struct rtc_time
*tm
)
146 u8 regs
[RV3029C2_WATCH_SECTION_LEN
] = { 0, };
148 ret
= rv3029c2_i2c_get_sr(client
, buf
);
150 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
154 ret
= rv3029c2_i2c_read_regs(client
, RV3029C2_W_SEC
, regs
,
155 RV3029C2_WATCH_SECTION_LEN
);
157 dev_err(&client
->dev
, "%s: reading RTC section failed\n",
162 tm
->tm_sec
= bcd2bin(regs
[RV3029C2_W_SEC
-RV3029C2_W_SEC
]);
163 tm
->tm_min
= bcd2bin(regs
[RV3029C2_W_MINUTES
-RV3029C2_W_SEC
]);
165 /* HR field has a more complex interpretation */
167 const u8 _hr
= regs
[RV3029C2_W_HOURS
-RV3029C2_W_SEC
];
168 if (_hr
& RV3029C2_REG_HR_12_24
) {
170 tm
->tm_hour
= bcd2bin(_hr
& 0x1f);
171 if (_hr
& RV3029C2_REG_HR_PM
) /* PM flag set */
173 } else /* 24h format */
174 tm
->tm_hour
= bcd2bin(_hr
& 0x3f);
177 tm
->tm_mday
= bcd2bin(regs
[RV3029C2_W_DATE
-RV3029C2_W_SEC
]);
178 tm
->tm_mon
= bcd2bin(regs
[RV3029C2_W_MONTHS
-RV3029C2_W_SEC
]) - 1;
179 tm
->tm_year
= bcd2bin(regs
[RV3029C2_W_YEARS
-RV3029C2_W_SEC
]) + 100;
180 tm
->tm_wday
= bcd2bin(regs
[RV3029C2_W_DAYS
-RV3029C2_W_SEC
]) - 1;
185 static int rv3029c2_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
187 return rv3029c2_i2c_read_time(to_i2c_client(dev
), tm
);
191 rv3029c2_i2c_read_alarm(struct i2c_client
*client
, struct rtc_wkalrm
*alarm
)
193 struct rtc_time
*const tm
= &alarm
->time
;
197 ret
= rv3029c2_i2c_get_sr(client
, regs
);
199 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
203 ret
= rv3029c2_i2c_read_regs(client
, RV3029C2_A_SC
, regs
,
204 RV3029C2_ALARM_SECTION_LEN
);
207 dev_err(&client
->dev
, "%s: reading alarm section failed\n",
212 tm
->tm_sec
= bcd2bin(regs
[RV3029C2_A_SC
-RV3029C2_A_SC
] & 0x7f);
213 tm
->tm_min
= bcd2bin(regs
[RV3029C2_A_MN
-RV3029C2_A_SC
] & 0x7f);
214 tm
->tm_hour
= bcd2bin(regs
[RV3029C2_A_HR
-RV3029C2_A_SC
] & 0x3f);
215 tm
->tm_mday
= bcd2bin(regs
[RV3029C2_A_DT
-RV3029C2_A_SC
] & 0x3f);
216 tm
->tm_mon
= bcd2bin(regs
[RV3029C2_A_MO
-RV3029C2_A_SC
] & 0x1f) - 1;
217 tm
->tm_year
= bcd2bin(regs
[RV3029C2_A_YR
-RV3029C2_A_SC
] & 0x7f) + 100;
218 tm
->tm_wday
= bcd2bin(regs
[RV3029C2_A_DW
-RV3029C2_A_SC
] & 0x07) - 1;
224 rv3029c2_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
226 return rv3029c2_i2c_read_alarm(to_i2c_client(dev
), alarm
);
229 static int rv3029c2_rtc_i2c_alarm_set_irq(struct i2c_client
*client
,
236 ret
= rv3029c2_i2c_read_regs(client
, RV3029C2_IRQ_CTRL
, buf
, 1);
238 dev_err(&client
->dev
, "can't read INT reg\n");
242 buf
[0] |= RV3029C2_IRQ_CTRL_AIE
;
244 buf
[0] &= ~RV3029C2_IRQ_CTRL_AIE
;
246 ret
= rv3029c2_i2c_write_regs(client
, RV3029C2_IRQ_CTRL
, buf
, 1);
248 dev_err(&client
->dev
, "can't set INT reg\n");
255 static int rv3029c2_rtc_i2c_set_alarm(struct i2c_client
*client
,
256 struct rtc_wkalrm
*alarm
)
258 struct rtc_time
*const tm
= &alarm
->time
;
263 * The clock has an 8 bit wide bcd-coded register (they never learn)
264 * for the year. tm_year is an offset from 1900 and we are interested
265 * in the 2000-2099 range, so any value less than 100 is invalid.
267 if (tm
->tm_year
< 100)
270 ret
= rv3029c2_i2c_get_sr(client
, regs
);
272 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
275 regs
[RV3029C2_A_SC
-RV3029C2_A_SC
] = bin2bcd(tm
->tm_sec
& 0x7f);
276 regs
[RV3029C2_A_MN
-RV3029C2_A_SC
] = bin2bcd(tm
->tm_min
& 0x7f);
277 regs
[RV3029C2_A_HR
-RV3029C2_A_SC
] = bin2bcd(tm
->tm_hour
& 0x3f);
278 regs
[RV3029C2_A_DT
-RV3029C2_A_SC
] = bin2bcd(tm
->tm_mday
& 0x3f);
279 regs
[RV3029C2_A_MO
-RV3029C2_A_SC
] = bin2bcd((tm
->tm_mon
& 0x1f) - 1);
280 regs
[RV3029C2_A_DW
-RV3029C2_A_SC
] = bin2bcd((tm
->tm_wday
& 7) - 1);
281 regs
[RV3029C2_A_YR
-RV3029C2_A_SC
] = bin2bcd((tm
->tm_year
& 0x7f) - 100);
283 ret
= rv3029c2_i2c_write_regs(client
, RV3029C2_A_SC
, regs
,
284 RV3029C2_ALARM_SECTION_LEN
);
288 if (alarm
->enabled
) {
292 ret
= rv3029c2_i2c_read_regs(client
, RV3029C2_IRQ_FLAGS
,
295 dev_err(&client
->dev
, "can't read alarm flag\n");
298 buf
[0] &= ~RV3029C2_IRQ_FLAGS_AF
;
299 ret
= rv3029c2_i2c_write_regs(client
, RV3029C2_IRQ_FLAGS
,
302 dev_err(&client
->dev
, "can't set alarm flag\n");
306 ret
= rv3029c2_rtc_i2c_alarm_set_irq(client
, 1);
310 dev_dbg(&client
->dev
, "alarm IRQ armed\n");
312 /* disable AIE irq */
313 ret
= rv3029c2_rtc_i2c_alarm_set_irq(client
, 1);
317 dev_dbg(&client
->dev
, "alarm IRQ disabled\n");
323 static int rv3029c2_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
325 return rv3029c2_rtc_i2c_set_alarm(to_i2c_client(dev
), alarm
);
329 rv3029c2_i2c_set_time(struct i2c_client
*client
, struct rtc_time
const *tm
)
335 * The clock has an 8 bit wide bcd-coded register (they never learn)
336 * for the year. tm_year is an offset from 1900 and we are interested
337 * in the 2000-2099 range, so any value less than 100 is invalid.
339 if (tm
->tm_year
< 100)
342 regs
[RV3029C2_W_SEC
-RV3029C2_W_SEC
] = bin2bcd(tm
->tm_sec
);
343 regs
[RV3029C2_W_MINUTES
-RV3029C2_W_SEC
] = bin2bcd(tm
->tm_min
);
344 regs
[RV3029C2_W_HOURS
-RV3029C2_W_SEC
] = bin2bcd(tm
->tm_hour
);
345 regs
[RV3029C2_W_DATE
-RV3029C2_W_SEC
] = bin2bcd(tm
->tm_mday
);
346 regs
[RV3029C2_W_MONTHS
-RV3029C2_W_SEC
] = bin2bcd(tm
->tm_mon
+1);
347 regs
[RV3029C2_W_DAYS
-RV3029C2_W_SEC
] = bin2bcd((tm
->tm_wday
& 7)+1);
348 regs
[RV3029C2_W_YEARS
-RV3029C2_W_SEC
] = bin2bcd(tm
->tm_year
- 100);
350 ret
= rv3029c2_i2c_write_regs(client
, RV3029C2_W_SEC
, regs
,
351 RV3029C2_WATCH_SECTION_LEN
);
355 ret
= rv3029c2_i2c_get_sr(client
, regs
);
357 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
361 ret
= rv3029c2_i2c_set_sr(client
, (regs
[0] & ~RV3029C2_STATUS_PON
));
363 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
370 static int rv3029c2_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
372 return rv3029c2_i2c_set_time(to_i2c_client(dev
), tm
);
375 static const struct rtc_class_ops rv3029c2_rtc_ops
= {
376 .read_time
= rv3029c2_rtc_read_time
,
377 .set_time
= rv3029c2_rtc_set_time
,
378 .read_alarm
= rv3029c2_rtc_read_alarm
,
379 .set_alarm
= rv3029c2_rtc_set_alarm
,
382 static struct i2c_device_id rv3029c2_id
[] = {
386 MODULE_DEVICE_TABLE(i2c
, rv3029c2_id
);
388 static int rv3029c2_probe(struct i2c_client
*client
,
389 const struct i2c_device_id
*id
)
391 struct rtc_device
*rtc
;
395 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_EMUL
))
398 rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
399 &rv3029c2_rtc_ops
, THIS_MODULE
);
404 i2c_set_clientdata(client
, rtc
);
406 rc
= rv3029c2_i2c_get_sr(client
, buf
);
408 dev_err(&client
->dev
, "reading status failed\n");
415 static int rv3029c2_remove(struct i2c_client
*client
)
420 static struct i2c_driver rv3029c2_driver
= {
422 .name
= "rtc-rv3029c2",
424 .probe
= rv3029c2_probe
,
425 .remove
= rv3029c2_remove
,
426 .id_table
= rv3029c2_id
,
429 module_i2c_driver(rv3029c2_driver
);
431 MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
432 MODULE_DESCRIPTION("Micro Crystal RV3029C2 RTC driver");
433 MODULE_LICENSE("GPL");