2 * An I2C driver for the Epson RX8581 RTC
4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
12 * Copyright 2005-06 Tower Technologies
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/bcd.h>
18 #include <linux/rtc.h>
19 #include <linux/log2.h>
21 #define RX8581_REG_SC 0x00 /* Second in BCD */
22 #define RX8581_REG_MN 0x01 /* Minute in BCD */
23 #define RX8581_REG_HR 0x02 /* Hour in BCD */
24 #define RX8581_REG_DW 0x03 /* Day of Week */
25 #define RX8581_REG_DM 0x04 /* Day of Month in BCD */
26 #define RX8581_REG_MO 0x05 /* Month in BCD */
27 #define RX8581_REG_YR 0x06 /* Year in BCD */
28 #define RX8581_REG_RAM 0x07 /* RAM */
29 #define RX8581_REG_AMN 0x08 /* Alarm Min in BCD*/
30 #define RX8581_REG_AHR 0x09 /* Alarm Hour in BCD */
31 #define RX8581_REG_ADM 0x0A
32 #define RX8581_REG_ADW 0x0A
33 #define RX8581_REG_TMR0 0x0B
34 #define RX8581_REG_TMR1 0x0C
35 #define RX8581_REG_EXT 0x0D /* Extension Register */
36 #define RX8581_REG_FLAG 0x0E /* Flag Register */
37 #define RX8581_REG_CTRL 0x0F /* Control Register */
40 /* Flag Register bit definitions */
41 #define RX8581_FLAG_UF 0x20 /* Update */
42 #define RX8581_FLAG_TF 0x10 /* Timer */
43 #define RX8581_FLAG_AF 0x08 /* Alarm */
44 #define RX8581_FLAG_VLF 0x02 /* Voltage Low */
46 /* Control Register bit definitions */
47 #define RX8581_CTRL_UIE 0x20 /* Update Interrupt Enable */
48 #define RX8581_CTRL_TIE 0x10 /* Timer Interrupt Enable */
49 #define RX8581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */
50 #define RX8581_CTRL_STOP 0x02 /* STOP bit */
51 #define RX8581_CTRL_RESET 0x01 /* RESET bit */
54 struct i2c_client *client;
55 struct rtc_device *rtc;
56 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
57 u8 length, u8 *values);
58 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
59 u8 length, const u8 *values);
62 static struct i2c_driver rx8581_driver;
64 static int rx8581_read_block_data(const struct i2c_client *client, u8 command,
65 u8 length, u8 *values)
69 for (i = 0; i < length; i++) {
70 data = i2c_smbus_read_byte_data(client, command + i);
78 static int rx8581_write_block_data(const struct i2c_client *client, u8 command,
79 u8 length, const u8 *values)
83 for (i = 0; i < length; i++) {
84 ret = i2c_smbus_write_byte_data(client, command + i,
93 * In the routines that deal directly with the rx8581 hardware, we use
94 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
96 static int rx8581_get_datetime(struct i2c_client *client, struct rtc_time *tm)
98 unsigned char date[7];
100 struct rx8581 *rx8581 = i2c_get_clientdata(client);
102 /* First we ensure that the "update flag" is not set, we read the
103 * time and date then re-read the "update flag". If the update flag
104 * has been set, we know that the time has changed during the read so
105 * we repeat the whole process again.
107 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
109 dev_err(&client->dev, "Unable to read device flags\n");
114 /* If update flag set, clear it */
115 if (data & RX8581_FLAG_UF) {
116 err = i2c_smbus_write_byte_data(client,
117 RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
119 dev_err(&client->dev, "Unable to write device flags\n");
124 /* Now read time and date */
125 err = rx8581->read_block_data(client, RX8581_REG_SC,
128 dev_err(&client->dev, "Unable to read date\n");
132 /* Check flag register */
133 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
135 dev_err(&client->dev, "Unable to read device flags\n");
138 } while (data & RX8581_FLAG_UF);
140 if (data & RX8581_FLAG_VLF)
141 dev_info(&client->dev,
142 "low voltage detected, date/time is not reliable.\n");
144 dev_dbg(&client->dev,
145 "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
146 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
148 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
150 tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
151 tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
152 tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
153 tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
154 tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
155 tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
156 tm->tm_year = bcd2bin(date[RX8581_REG_YR]);
157 if (tm->tm_year < 70)
158 tm->tm_year += 100; /* assume we are in 1970...2069 */
161 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
162 "mday=%d, mon=%d, year=%d, wday=%d\n",
164 tm->tm_sec, tm->tm_min, tm->tm_hour,
165 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
170 static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm)
173 unsigned char buf[7];
174 struct rx8581 *rx8581 = i2c_get_clientdata(client);
176 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
177 "mday=%d, mon=%d, year=%d, wday=%d\n",
179 tm->tm_sec, tm->tm_min, tm->tm_hour,
180 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
182 /* hours, minutes and seconds */
183 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
184 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
185 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
187 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
190 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
192 /* year and century */
193 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100);
194 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
197 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
199 dev_err(&client->dev, "Unable to read control register\n");
203 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
204 (data | RX8581_CTRL_STOP));
206 dev_err(&client->dev, "Unable to write control register\n");
210 /* write register's data */
211 err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
213 dev_err(&client->dev, "Unable to write to date registers\n");
217 /* get VLF and clear it */
218 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
220 dev_err(&client->dev, "Unable to read flag register\n");
224 err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
225 (data & ~(RX8581_FLAG_VLF)));
227 dev_err(&client->dev, "Unable to write flag register\n");
231 /* Restart the clock */
232 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
234 dev_err(&client->dev, "Unable to read control register\n");
238 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
239 (data & ~(RX8581_CTRL_STOP)));
241 dev_err(&client->dev, "Unable to write control register\n");
248 static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
250 return rx8581_get_datetime(to_i2c_client(dev), tm);
253 static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
255 return rx8581_set_datetime(to_i2c_client(dev), tm);
258 static const struct rtc_class_ops rx8581_rtc_ops = {
259 .read_time = rx8581_rtc_read_time,
260 .set_time = rx8581_rtc_set_time,
263 static int rx8581_probe(struct i2c_client *client,
264 const struct i2c_device_id *id)
266 struct rx8581 *rx8581;
268 dev_dbg(&client->dev, "%s\n", __func__);
270 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
271 && !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
274 rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
278 i2c_set_clientdata(client, rx8581);
279 rx8581->client = client;
281 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
282 rx8581->read_block_data = i2c_smbus_read_i2c_block_data;
283 rx8581->write_block_data = i2c_smbus_write_i2c_block_data;
285 rx8581->read_block_data = rx8581_read_block_data;
286 rx8581->write_block_data = rx8581_write_block_data;
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;
295 return rtc_register_device(rx8581->rtc);
298 static const struct i2c_device_id rx8581_id[] = {
302 MODULE_DEVICE_TABLE(i2c, rx8581_id);
304 static const struct of_device_id rx8581_of_match[] = {
305 { .compatible = "epson,rx8581" },
308 MODULE_DEVICE_TABLE(of, rx8581_of_match);
310 static struct i2c_driver rx8581_driver = {
312 .name = "rtc-rx8581",
313 .of_match_table = of_match_ptr(rx8581_of_match),
315 .probe = rx8581_probe,
316 .id_table = rx8581_id,
319 module_i2c_driver(rx8581_driver);
321 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
322 MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
323 MODULE_LICENSE("GPL");