1 // SPDX-License-Identifier: GPL-2.0-only
3 * An I2C driver for the Epson RX8581 RTC
5 * Author: Martyn Welch <martyn.welch@ge.com>
6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
8 * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
9 * Copyright 2005-06 Tower Technologies
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/bcd.h>
16 #include <linux/regmap.h>
17 #include <linux/rtc.h>
18 #include <linux/log2.h>
20 #define RX8581_REG_SC 0x00 /* Second in BCD */
21 #define RX8581_REG_MN 0x01 /* Minute in BCD */
22 #define RX8581_REG_HR 0x02 /* Hour in BCD */
23 #define RX8581_REG_DW 0x03 /* Day of Week */
24 #define RX8581_REG_DM 0x04 /* Day of Month in BCD */
25 #define RX8581_REG_MO 0x05 /* Month in BCD */
26 #define RX8581_REG_YR 0x06 /* Year in BCD */
27 #define RX8581_REG_RAM 0x07 /* RAM */
28 #define RX8581_REG_AMN 0x08 /* Alarm Min in BCD*/
29 #define RX8581_REG_AHR 0x09 /* Alarm Hour in BCD */
30 #define RX8581_REG_ADM 0x0A
31 #define RX8581_REG_ADW 0x0A
32 #define RX8581_REG_TMR0 0x0B
33 #define RX8581_REG_TMR1 0x0C
34 #define RX8581_REG_EXT 0x0D /* Extension Register */
35 #define RX8581_REG_FLAG 0x0E /* Flag Register */
36 #define RX8581_REG_CTRL 0x0F /* Control Register */
39 /* Flag Register bit definitions */
40 #define RX8581_FLAG_UF 0x20 /* Update */
41 #define RX8581_FLAG_TF 0x10 /* Timer */
42 #define RX8581_FLAG_AF 0x08 /* Alarm */
43 #define RX8581_FLAG_VLF 0x02 /* Voltage Low */
45 /* Control Register bit definitions */
46 #define RX8581_CTRL_UIE 0x20 /* Update Interrupt Enable */
47 #define RX8581_CTRL_TIE 0x10 /* Timer Interrupt Enable */
48 #define RX8581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */
49 #define RX8581_CTRL_STOP 0x02 /* STOP bit */
50 #define RX8581_CTRL_RESET 0x01 /* RESET bit */
52 #define RX8571_USER_RAM 0x10
53 #define RX8571_NVRAM_SIZE 0x10
56 struct regmap *regmap;
57 struct rtc_device *rtc;
60 struct rx85x1_config {
61 struct regmap_config regmap;
62 unsigned int num_nvram;
66 * In the routines that deal directly with the rx8581 hardware, we use
67 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
69 static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
71 struct i2c_client *client = to_i2c_client(dev);
72 unsigned char date[7];
75 struct rx8581 *rx8581 = i2c_get_clientdata(client);
77 /* First we ensure that the "update flag" is not set, we read the
78 * time and date then re-read the "update flag". If the update flag
79 * has been set, we know that the time has changed during the read so
80 * we repeat the whole process again.
82 err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
86 if (data & RX8581_FLAG_VLF) {
88 "low voltage detected, date/time is not reliable.\n");
93 /* If update flag set, clear it */
94 if (data & RX8581_FLAG_UF) {
95 err = regmap_write(rx8581->regmap, RX8581_REG_FLAG,
96 data & ~RX8581_FLAG_UF);
101 /* Now read time and date */
102 err = regmap_bulk_read(rx8581->regmap, RX8581_REG_SC, date,
107 /* Check flag register */
108 err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
111 } while (data & RX8581_FLAG_UF);
113 dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
114 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
116 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
118 tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
119 tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
120 tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
121 tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
122 tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
123 tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
124 tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
126 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
127 "mday=%d, mon=%d, year=%d, wday=%d\n",
129 tm->tm_sec, tm->tm_min, tm->tm_hour,
130 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
135 static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
137 struct i2c_client *client = to_i2c_client(dev);
139 unsigned char buf[7];
140 struct rx8581 *rx8581 = i2c_get_clientdata(client);
142 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
143 "mday=%d, mon=%d, year=%d, wday=%d\n",
145 tm->tm_sec, tm->tm_min, tm->tm_hour,
146 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
148 /* hours, minutes and seconds */
149 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
150 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
151 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
153 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
156 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
158 /* year and century */
159 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
160 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
163 err = regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
164 RX8581_CTRL_STOP, RX8581_CTRL_STOP);
168 /* write register's data */
169 err = regmap_bulk_write(rx8581->regmap, RX8581_REG_SC,
174 /* get VLF and clear it */
175 err = regmap_update_bits(rx8581->regmap, RX8581_REG_FLAG,
180 /* Restart the clock */
181 return regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
182 RX8581_CTRL_STOP, 0);
185 static const struct rtc_class_ops rx8581_rtc_ops = {
186 .read_time = rx8581_rtc_read_time,
187 .set_time = rx8581_rtc_set_time,
190 static int rx8571_nvram_read(void *priv, unsigned int offset, void *val,
193 struct rx8581 *rx8581 = priv;
195 return regmap_bulk_read(rx8581->regmap, RX8571_USER_RAM + offset,
199 static int rx8571_nvram_write(void *priv, unsigned int offset, void *val,
202 struct rx8581 *rx8581 = priv;
204 return regmap_bulk_write(rx8581->regmap, RX8571_USER_RAM + offset,
208 static int rx85x1_nvram_read(void *priv, unsigned int offset, void *val,
211 struct rx8581 *rx8581 = priv;
212 unsigned int tmp_val;
215 ret = regmap_read(rx8581->regmap, RX8581_REG_RAM, &tmp_val);
216 (*(unsigned char *)val) = (unsigned char) tmp_val;
221 static int rx85x1_nvram_write(void *priv, unsigned int offset, void *val,
224 struct rx8581 *rx8581 = priv;
225 unsigned char tmp_val;
227 tmp_val = *((unsigned char *)val);
228 return regmap_write(rx8581->regmap, RX8581_REG_RAM,
229 (unsigned int)tmp_val);
232 static const struct rx85x1_config rx8581_config = {
241 static const struct rx85x1_config rx8571_config = {
245 .max_register = 0x1f,
250 static int rx8581_probe(struct i2c_client *client)
252 struct rx8581 *rx8581;
253 const struct rx85x1_config *config = &rx8581_config;
254 const void *data = of_device_get_match_data(&client->dev);
255 static struct nvmem_config nvmem_cfg[] = {
261 .reg_read = rx85x1_nvram_read,
262 .reg_write = rx85x1_nvram_write,
267 .size = RX8571_NVRAM_SIZE,
268 .reg_read = rx8571_nvram_read,
269 .reg_write = rx8571_nvram_write,
274 dev_dbg(&client->dev, "%s\n", __func__);
279 rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
283 i2c_set_clientdata(client, rx8581);
285 rx8581->regmap = devm_regmap_init_i2c(client, &config->regmap);
286 if (IS_ERR(rx8581->regmap))
287 return PTR_ERR(rx8581->regmap);
289 rx8581->rtc = devm_rtc_allocate_device(&client->dev);
290 if (IS_ERR(rx8581->rtc))
291 return PTR_ERR(rx8581->rtc);
293 rx8581->rtc->ops = &rx8581_rtc_ops;
294 rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
295 rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
296 rx8581->rtc->start_secs = 0;
297 rx8581->rtc->set_start_time = true;
299 ret = devm_rtc_register_device(rx8581->rtc);
301 for (i = 0; i < config->num_nvram; i++) {
302 nvmem_cfg[i].priv = rx8581;
303 devm_rtc_nvmem_register(rx8581->rtc, &nvmem_cfg[i]);
309 static const struct i2c_device_id rx8581_id[] = {
313 MODULE_DEVICE_TABLE(i2c, rx8581_id);
315 static const __maybe_unused struct of_device_id rx8581_of_match[] = {
316 { .compatible = "epson,rx8571", .data = &rx8571_config },
317 { .compatible = "epson,rx8581", .data = &rx8581_config },
320 MODULE_DEVICE_TABLE(of, rx8581_of_match);
322 static struct i2c_driver rx8581_driver = {
324 .name = "rtc-rx8581",
325 .of_match_table = of_match_ptr(rx8581_of_match),
327 .probe = rx8581_probe,
328 .id_table = rx8581_id,
331 module_i2c_driver(rx8581_driver);
333 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
334 MODULE_DESCRIPTION("Epson RX-8571/RX-8581 RTC driver");
335 MODULE_LICENSE("GPL");