blob: 0a70f1ac5cc242aacbb8e997537cd3732e9c2e34 [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
Andreas Wernerd643a492014-01-23 15:55:20 -080062static int rx8581_read_block_data(const struct i2c_client *client, u8 command,
63 u8 length, u8 *values)
64{
65 s32 i, data;
66
67 for (i = 0; i < length; i++) {
68 data = i2c_smbus_read_byte_data(client, command + i);
69 if (data < 0)
70 return data;
71 values[i] = data;
72 }
73 return i;
74}
75
76static int rx8581_write_block_data(const struct i2c_client *client, u8 command,
77 u8 length, const u8 *values)
78{
79 s32 i, ret;
80
81 for (i = 0; i < length; i++) {
82 ret = i2c_smbus_write_byte_data(client, command + i,
83 values[i]);
84 if (ret < 0)
85 return ret;
86 }
87 return length;
88}
89
Martyn Welcha7fa9852008-11-12 13:27:06 -080090/*
91 * In the routines that deal directly with the rx8581 hardware, we use
92 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
93 */
Alexandre Belloni2d2b3002018-05-17 22:33:28 +020094static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
Martyn Welcha7fa9852008-11-12 13:27:06 -080095{
Alexandre Belloni2d2b3002018-05-17 22:33:28 +020096 struct i2c_client *client = to_i2c_client(dev);
Martyn Welcha7fa9852008-11-12 13:27:06 -080097 unsigned char date[7];
98 int data, err;
Andreas Wernerd643a492014-01-23 15:55:20 -080099 struct rx8581 *rx8581 = i2c_get_clientdata(client);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800100
101 /* First we ensure that the "update flag" is not set, we read the
102 * time and date then re-read the "update flag". If the update flag
103 * has been set, we know that the time has changed during the read so
104 * we repeat the whole process again.
105 */
106 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
107 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200108 dev_err(dev, "Unable to read device flags\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800109 return -EIO;
110 }
111
112 do {
113 /* If update flag set, clear it */
114 if (data & RX8581_FLAG_UF) {
115 err = i2c_smbus_write_byte_data(client,
116 RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
117 if (err != 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200118 dev_err(dev, "Unable to write device flags\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800119 return -EIO;
120 }
121 }
122
123 /* Now read time and date */
Andreas Wernerd643a492014-01-23 15:55:20 -0800124 err = rx8581->read_block_data(client, RX8581_REG_SC,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800125 7, date);
126 if (err < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200127 dev_err(dev, "Unable to read date\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800128 return -EIO;
129 }
130
131 /* Check flag register */
132 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
133 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200134 dev_err(dev, "Unable to read device flags\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800135 return -EIO;
136 }
137 } while (data & RX8581_FLAG_UF);
138
139 if (data & RX8581_FLAG_VLF)
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200140 dev_info(dev,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800141 "low voltage detected, date/time is not reliable.\n");
142
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200143 dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
Martyn Welcha7fa9852008-11-12 13:27:06 -0800144 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
145 __func__,
146 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
147
148 tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
149 tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
150 tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
151 tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
152 tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
153 tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200154 tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800155
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200156 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Martyn Welcha7fa9852008-11-12 13:27:06 -0800157 "mday=%d, mon=%d, year=%d, wday=%d\n",
158 __func__,
159 tm->tm_sec, tm->tm_min, tm->tm_hour,
160 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
161
Alexandre Belloni24f421b2018-02-21 00:25:18 +0100162 return 0;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800163}
164
Alexandre Belloni2d2b3002018-05-17 22:33:28 +0200165static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
Martyn Welcha7fa9852008-11-12 13:27:06 -0800166{
Alexandre Belloni2d2b3002018-05-17 22:33:28 +0200167 struct i2c_client *client = to_i2c_client(dev);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800168 int data, err;
169 unsigned char buf[7];
Andreas Wernerd643a492014-01-23 15:55:20 -0800170 struct rx8581 *rx8581 = i2c_get_clientdata(client);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800171
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200172 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
Martyn Welcha7fa9852008-11-12 13:27:06 -0800173 "mday=%d, mon=%d, year=%d, wday=%d\n",
174 __func__,
175 tm->tm_sec, tm->tm_min, tm->tm_hour,
176 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
177
178 /* hours, minutes and seconds */
179 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
180 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
181 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
182
183 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
184
185 /* month, 1 - 12 */
186 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
187
188 /* year and century */
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200189 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800190 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
191
192 /* Stop the clock */
193 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
194 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200195 dev_err(dev, "Unable to read control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800196 return -EIO;
197 }
198
Rudolf Marek2884fce2010-07-27 13:18:02 -0700199 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800200 (data | RX8581_CTRL_STOP));
201 if (err < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200202 dev_err(dev, "Unable to write control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800203 return -EIO;
204 }
205
206 /* write register's data */
Andreas Wernerd643a492014-01-23 15:55:20 -0800207 err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800208 if (err < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200209 dev_err(dev, "Unable to write to date registers\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800210 return -EIO;
211 }
212
Rudolf Marek2884fce2010-07-27 13:18:02 -0700213 /* get VLF and clear it */
214 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
215 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200216 dev_err(dev, "Unable to read flag register\n");
Rudolf Marek2884fce2010-07-27 13:18:02 -0700217 return -EIO;
218 }
219
220 err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
221 (data & ~(RX8581_FLAG_VLF)));
222 if (err != 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200223 dev_err(dev, "Unable to write flag register\n");
Rudolf Marek2884fce2010-07-27 13:18:02 -0700224 return -EIO;
225 }
226
Martyn Welcha7fa9852008-11-12 13:27:06 -0800227 /* Restart the clock */
228 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
229 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200230 dev_err(dev, "Unable to read control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800231 return -EIO;
232 }
233
Rudolf Marek2884fce2010-07-27 13:18:02 -0700234 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
235 (data & ~(RX8581_CTRL_STOP)));
Martyn Welcha7fa9852008-11-12 13:27:06 -0800236 if (err != 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200237 dev_err(dev, "Unable to write control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800238 return -EIO;
239 }
240
241 return 0;
242}
243
Martyn Welcha7fa9852008-11-12 13:27:06 -0800244static const struct rtc_class_ops rx8581_rtc_ops = {
245 .read_time = rx8581_rtc_read_time,
246 .set_time = rx8581_rtc_set_time,
247};
248
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800249static int rx8581_probe(struct i2c_client *client,
250 const struct i2c_device_id *id)
Martyn Welcha7fa9852008-11-12 13:27:06 -0800251{
Andreas Wernerd643a492014-01-23 15:55:20 -0800252 struct rx8581 *rx8581;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800253
254 dev_dbg(&client->dev, "%s\n", __func__);
255
Andreas Wernerd643a492014-01-23 15:55:20 -0800256 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
257 && !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
258 return -EIO;
259
260 rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
261 if (!rx8581)
262 return -ENOMEM;
263
264 i2c_set_clientdata(client, rx8581);
265 rx8581->client = client;
266
267 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
268 rx8581->read_block_data = i2c_smbus_read_i2c_block_data;
269 rx8581->write_block_data = i2c_smbus_write_i2c_block_data;
270 } else {
271 rx8581->read_block_data = rx8581_read_block_data;
272 rx8581->write_block_data = rx8581_write_block_data;
273 }
Martyn Welcha7fa9852008-11-12 13:27:06 -0800274
Alexandre Bellonib8157162018-05-17 22:33:25 +0200275 rx8581->rtc = devm_rtc_allocate_device(&client->dev);
276 if (IS_ERR(rx8581->rtc))
Andreas Wernerd643a492014-01-23 15:55:20 -0800277 return PTR_ERR(rx8581->rtc);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800278
Alexandre Bellonib8157162018-05-17 22:33:25 +0200279 rx8581->rtc->ops = &rx8581_rtc_ops;
Alexandre Belloni86c54ef2018-05-17 22:33:26 +0200280 rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
281 rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200282 rx8581->rtc->start_secs = 0;
283 rx8581->rtc->set_start_time = true;
Alexandre Bellonib8157162018-05-17 22:33:25 +0200284
285 return rtc_register_device(rx8581->rtc);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800286}
287
Martyn Welcha7fa9852008-11-12 13:27:06 -0800288static const struct i2c_device_id rx8581_id[] = {
289 { "rx8581", 0 },
290 { }
291};
292MODULE_DEVICE_TABLE(i2c, rx8581_id);
293
Javier Martinez Canillasffbecfb2017-03-03 11:29:22 -0300294static const struct of_device_id rx8581_of_match[] = {
295 { .compatible = "epson,rx8581" },
296 { }
297};
298MODULE_DEVICE_TABLE(of, rx8581_of_match);
299
Martyn Welcha7fa9852008-11-12 13:27:06 -0800300static struct i2c_driver rx8581_driver = {
301 .driver = {
302 .name = "rtc-rx8581",
Javier Martinez Canillasffbecfb2017-03-03 11:29:22 -0300303 .of_match_table = of_match_ptr(rx8581_of_match),
Martyn Welcha7fa9852008-11-12 13:27:06 -0800304 },
305 .probe = rx8581_probe,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800306 .id_table = rx8581_id,
307};
308
Axel Lin0abc9202012-03-23 15:02:31 -0700309module_i2c_driver(rx8581_driver);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800310
Martyn Welch9756c4d2010-04-14 09:24:16 +0100311MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800312MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
313MODULE_LICENSE("GPL");