blob: a91fbb86a1106b6bd59030270d95584e81c4a919 [file] [log] [blame]
Martyn Welcha7fa9852008-11-12 13:27:06 -08001/*
2 * An I2C driver for the Epson RX8581 RTC
3 *
Martyn Welch9756c4d2010-04-14 09:24:16 +01004 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
Martyn Welcha7fa9852008-11-12 13:27:06 -08006 *
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.
10 *
11 * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
12 * Copyright 2005-06 Tower Technologies
13 */
14
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>
20
Martyn Welcha7fa9852008-11-12 13:27:06 -080021#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 */
38
39
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 */
45
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 */
52
Andreas Wernerd643a492014-01-23 15:55:20 -080053struct rx8581 {
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);
60};
61
Martyn Welcha7fa9852008-11-12 13:27:06 -080062static struct i2c_driver rx8581_driver;
63
Andreas Wernerd643a492014-01-23 15:55:20 -080064static int rx8581_read_block_data(const struct i2c_client *client, u8 command,
65 u8 length, u8 *values)
66{
67 s32 i, data;
68
69 for (i = 0; i < length; i++) {
70 data = i2c_smbus_read_byte_data(client, command + i);
71 if (data < 0)
72 return data;
73 values[i] = data;
74 }
75 return i;
76}
77
78static int rx8581_write_block_data(const struct i2c_client *client, u8 command,
79 u8 length, const u8 *values)
80{
81 s32 i, ret;
82
83 for (i = 0; i < length; i++) {
84 ret = i2c_smbus_write_byte_data(client, command + i,
85 values[i]);
86 if (ret < 0)
87 return ret;
88 }
89 return length;
90}
91
Martyn Welcha7fa9852008-11-12 13:27:06 -080092/*
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.
95 */
Alexandre Belloni2d2b3002018-05-17 22:33:28 +020096static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
Martyn Welcha7fa9852008-11-12 13:27:06 -080097{
Alexandre Belloni2d2b3002018-05-17 22:33:28 +020098 struct i2c_client *client = to_i2c_client(dev);
Martyn Welcha7fa9852008-11-12 13:27:06 -080099 unsigned char date[7];
100 int data, err;
Andreas Wernerd643a492014-01-23 15:55:20 -0800101 struct rx8581 *rx8581 = i2c_get_clientdata(client);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800102
103 /* First we ensure that the "update flag" is not set, we read the
104 * time and date then re-read the "update flag". If the update flag
105 * has been set, we know that the time has changed during the read so
106 * we repeat the whole process again.
107 */
108 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
109 if (data < 0) {
110 dev_err(&client->dev, "Unable to read device flags\n");
111 return -EIO;
112 }
113
114 do {
115 /* If update flag set, clear it */
116 if (data & RX8581_FLAG_UF) {
117 err = i2c_smbus_write_byte_data(client,
118 RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
119 if (err != 0) {
Andreas Wernerd643a492014-01-23 15:55:20 -0800120 dev_err(&client->dev, "Unable to write device flags\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800121 return -EIO;
122 }
123 }
124
125 /* Now read time and date */
Andreas Wernerd643a492014-01-23 15:55:20 -0800126 err = rx8581->read_block_data(client, RX8581_REG_SC,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800127 7, date);
128 if (err < 0) {
129 dev_err(&client->dev, "Unable to read date\n");
130 return -EIO;
131 }
132
133 /* Check flag register */
134 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
135 if (data < 0) {
136 dev_err(&client->dev, "Unable to read device flags\n");
137 return -EIO;
138 }
139 } while (data & RX8581_FLAG_UF);
140
141 if (data & RX8581_FLAG_VLF)
142 dev_info(&client->dev,
143 "low voltage detected, date/time is not reliable.\n");
144
145 dev_dbg(&client->dev,
146 "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
147 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
148 __func__,
149 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
150
151 tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
152 tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
153 tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
154 tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
155 tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
156 tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200157 tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800158
159 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
160 "mday=%d, mon=%d, year=%d, wday=%d\n",
161 __func__,
162 tm->tm_sec, tm->tm_min, tm->tm_hour,
163 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
164
Alexandre Belloni24f421b2018-02-21 00:25:18 +0100165 return 0;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800166}
167
Alexandre Belloni2d2b3002018-05-17 22:33:28 +0200168static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
Martyn Welcha7fa9852008-11-12 13:27:06 -0800169{
Alexandre Belloni2d2b3002018-05-17 22:33:28 +0200170 struct i2c_client *client = to_i2c_client(dev);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800171 int data, err;
172 unsigned char buf[7];
Andreas Wernerd643a492014-01-23 15:55:20 -0800173 struct rx8581 *rx8581 = i2c_get_clientdata(client);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800174
175 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
176 "mday=%d, mon=%d, year=%d, wday=%d\n",
177 __func__,
178 tm->tm_sec, tm->tm_min, tm->tm_hour,
179 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
180
181 /* hours, minutes and seconds */
182 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
183 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
184 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
185
186 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
187
188 /* month, 1 - 12 */
189 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
190
191 /* year and century */
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200192 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800193 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
194
195 /* Stop the clock */
196 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
197 if (data < 0) {
198 dev_err(&client->dev, "Unable to read control register\n");
199 return -EIO;
200 }
201
Rudolf Marek2884fce2010-07-27 13:18:02 -0700202 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800203 (data | RX8581_CTRL_STOP));
204 if (err < 0) {
205 dev_err(&client->dev, "Unable to write control register\n");
206 return -EIO;
207 }
208
209 /* write register's data */
Andreas Wernerd643a492014-01-23 15:55:20 -0800210 err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800211 if (err < 0) {
212 dev_err(&client->dev, "Unable to write to date registers\n");
213 return -EIO;
214 }
215
Rudolf Marek2884fce2010-07-27 13:18:02 -0700216 /* get VLF and clear it */
217 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
218 if (data < 0) {
219 dev_err(&client->dev, "Unable to read flag register\n");
220 return -EIO;
221 }
222
223 err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
224 (data & ~(RX8581_FLAG_VLF)));
225 if (err != 0) {
226 dev_err(&client->dev, "Unable to write flag register\n");
227 return -EIO;
228 }
229
Martyn Welcha7fa9852008-11-12 13:27:06 -0800230 /* Restart the clock */
231 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
232 if (data < 0) {
233 dev_err(&client->dev, "Unable to read control register\n");
234 return -EIO;
235 }
236
Rudolf Marek2884fce2010-07-27 13:18:02 -0700237 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
238 (data & ~(RX8581_CTRL_STOP)));
Martyn Welcha7fa9852008-11-12 13:27:06 -0800239 if (err != 0) {
240 dev_err(&client->dev, "Unable to write control register\n");
241 return -EIO;
242 }
243
244 return 0;
245}
246
Martyn Welcha7fa9852008-11-12 13:27:06 -0800247static const struct rtc_class_ops rx8581_rtc_ops = {
248 .read_time = rx8581_rtc_read_time,
249 .set_time = rx8581_rtc_set_time,
250};
251
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800252static int rx8581_probe(struct i2c_client *client,
253 const struct i2c_device_id *id)
Martyn Welcha7fa9852008-11-12 13:27:06 -0800254{
Andreas Wernerd643a492014-01-23 15:55:20 -0800255 struct rx8581 *rx8581;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800256
257 dev_dbg(&client->dev, "%s\n", __func__);
258
Andreas Wernerd643a492014-01-23 15:55:20 -0800259 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
260 && !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
261 return -EIO;
262
263 rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
264 if (!rx8581)
265 return -ENOMEM;
266
267 i2c_set_clientdata(client, rx8581);
268 rx8581->client = client;
269
270 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
271 rx8581->read_block_data = i2c_smbus_read_i2c_block_data;
272 rx8581->write_block_data = i2c_smbus_write_i2c_block_data;
273 } else {
274 rx8581->read_block_data = rx8581_read_block_data;
275 rx8581->write_block_data = rx8581_write_block_data;
276 }
Martyn Welcha7fa9852008-11-12 13:27:06 -0800277
Alexandre Bellonib8157162018-05-17 22:33:25 +0200278 rx8581->rtc = devm_rtc_allocate_device(&client->dev);
279 if (IS_ERR(rx8581->rtc))
Andreas Wernerd643a492014-01-23 15:55:20 -0800280 return PTR_ERR(rx8581->rtc);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800281
Alexandre Bellonib8157162018-05-17 22:33:25 +0200282 rx8581->rtc->ops = &rx8581_rtc_ops;
Alexandre Belloni86c54ef2018-05-17 22:33:26 +0200283 rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
284 rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200285 rx8581->rtc->start_secs = 0;
286 rx8581->rtc->set_start_time = true;
Alexandre Bellonib8157162018-05-17 22:33:25 +0200287
288 return rtc_register_device(rx8581->rtc);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800289}
290
Martyn Welcha7fa9852008-11-12 13:27:06 -0800291static const struct i2c_device_id rx8581_id[] = {
292 { "rx8581", 0 },
293 { }
294};
295MODULE_DEVICE_TABLE(i2c, rx8581_id);
296
Javier Martinez Canillasffbecfb2017-03-03 11:29:22 -0300297static const struct of_device_id rx8581_of_match[] = {
298 { .compatible = "epson,rx8581" },
299 { }
300};
301MODULE_DEVICE_TABLE(of, rx8581_of_match);
302
Martyn Welcha7fa9852008-11-12 13:27:06 -0800303static struct i2c_driver rx8581_driver = {
304 .driver = {
305 .name = "rtc-rx8581",
Javier Martinez Canillasffbecfb2017-03-03 11:29:22 -0300306 .of_match_table = of_match_ptr(rx8581_of_match),
Martyn Welcha7fa9852008-11-12 13:27:06 -0800307 },
308 .probe = rx8581_probe,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800309 .id_table = rx8581_id,
310};
311
Axel Lin0abc9202012-03-23 15:02:31 -0700312module_i2c_driver(rx8581_driver);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800313
Martyn Welch9756c4d2010-04-14 09:24:16 +0100314MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800315MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
316MODULE_LICENSE("GPL");