blob: 54631a3ab65fdf418c55e1b6b3c17f8893f4a776 [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
Alexandre Belloni6e6111f2018-05-17 22:33:30 +0200112 if (data & RX8581_FLAG_VLF) {
113 dev_warn(dev,
114 "low voltage detected, date/time is not reliable.\n");
115 return -EINVAL;
116 }
117
Martyn Welcha7fa9852008-11-12 13:27:06 -0800118 do {
119 /* If update flag set, clear it */
120 if (data & RX8581_FLAG_UF) {
121 err = i2c_smbus_write_byte_data(client,
122 RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
123 if (err != 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200124 dev_err(dev, "Unable to write device flags\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800125 return -EIO;
126 }
127 }
128
129 /* Now read time and date */
Andreas Wernerd643a492014-01-23 15:55:20 -0800130 err = rx8581->read_block_data(client, RX8581_REG_SC,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800131 7, date);
132 if (err < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200133 dev_err(dev, "Unable to read date\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800134 return -EIO;
135 }
136
137 /* Check flag register */
138 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
139 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200140 dev_err(dev, "Unable to read device flags\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800141 return -EIO;
142 }
143 } while (data & RX8581_FLAG_UF);
144
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200145 dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
Martyn Welcha7fa9852008-11-12 13:27:06 -0800146 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
147 __func__,
148 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
149
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 */
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200156 tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800157
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200158 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Martyn Welcha7fa9852008-11-12 13:27:06 -0800159 "mday=%d, mon=%d, year=%d, wday=%d\n",
160 __func__,
161 tm->tm_sec, tm->tm_min, tm->tm_hour,
162 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
163
Alexandre Belloni24f421b2018-02-21 00:25:18 +0100164 return 0;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800165}
166
Alexandre Belloni2d2b3002018-05-17 22:33:28 +0200167static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
Martyn Welcha7fa9852008-11-12 13:27:06 -0800168{
Alexandre Belloni2d2b3002018-05-17 22:33:28 +0200169 struct i2c_client *client = to_i2c_client(dev);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800170 int data, err;
171 unsigned char buf[7];
Andreas Wernerd643a492014-01-23 15:55:20 -0800172 struct rx8581 *rx8581 = i2c_get_clientdata(client);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800173
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200174 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
Martyn Welcha7fa9852008-11-12 13:27:06 -0800175 "mday=%d, mon=%d, year=%d, wday=%d\n",
176 __func__,
177 tm->tm_sec, tm->tm_min, tm->tm_hour,
178 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
179
180 /* hours, minutes and seconds */
181 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
182 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
183 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
184
185 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
186
187 /* month, 1 - 12 */
188 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
189
190 /* year and century */
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200191 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800192 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
193
194 /* Stop the clock */
195 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
196 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200197 dev_err(dev, "Unable to read control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800198 return -EIO;
199 }
200
Rudolf Marek2884fce2010-07-27 13:18:02 -0700201 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800202 (data | RX8581_CTRL_STOP));
203 if (err < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200204 dev_err(dev, "Unable to write control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800205 return -EIO;
206 }
207
208 /* write register's data */
Andreas Wernerd643a492014-01-23 15:55:20 -0800209 err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800210 if (err < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200211 dev_err(dev, "Unable to write to date registers\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800212 return -EIO;
213 }
214
Rudolf Marek2884fce2010-07-27 13:18:02 -0700215 /* get VLF and clear it */
216 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
217 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200218 dev_err(dev, "Unable to read flag register\n");
Rudolf Marek2884fce2010-07-27 13:18:02 -0700219 return -EIO;
220 }
221
222 err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
223 (data & ~(RX8581_FLAG_VLF)));
224 if (err != 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200225 dev_err(dev, "Unable to write flag register\n");
Rudolf Marek2884fce2010-07-27 13:18:02 -0700226 return -EIO;
227 }
228
Martyn Welcha7fa9852008-11-12 13:27:06 -0800229 /* Restart the clock */
230 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
231 if (data < 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200232 dev_err(dev, "Unable to read control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800233 return -EIO;
234 }
235
Rudolf Marek2884fce2010-07-27 13:18:02 -0700236 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
237 (data & ~(RX8581_CTRL_STOP)));
Martyn Welcha7fa9852008-11-12 13:27:06 -0800238 if (err != 0) {
Alexandre Bellonied87c6d2018-05-17 22:33:29 +0200239 dev_err(dev, "Unable to write control register\n");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800240 return -EIO;
241 }
242
243 return 0;
244}
245
Martyn Welcha7fa9852008-11-12 13:27:06 -0800246static const struct rtc_class_ops rx8581_rtc_ops = {
247 .read_time = rx8581_rtc_read_time,
248 .set_time = rx8581_rtc_set_time,
249};
250
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800251static int rx8581_probe(struct i2c_client *client,
252 const struct i2c_device_id *id)
Martyn Welcha7fa9852008-11-12 13:27:06 -0800253{
Andreas Wernerd643a492014-01-23 15:55:20 -0800254 struct rx8581 *rx8581;
Martyn Welcha7fa9852008-11-12 13:27:06 -0800255
256 dev_dbg(&client->dev, "%s\n", __func__);
257
Andreas Wernerd643a492014-01-23 15:55:20 -0800258 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
259 && !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
260 return -EIO;
261
262 rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
263 if (!rx8581)
264 return -ENOMEM;
265
266 i2c_set_clientdata(client, rx8581);
267 rx8581->client = client;
268
269 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
270 rx8581->read_block_data = i2c_smbus_read_i2c_block_data;
271 rx8581->write_block_data = i2c_smbus_write_i2c_block_data;
272 } else {
273 rx8581->read_block_data = rx8581_read_block_data;
274 rx8581->write_block_data = rx8581_write_block_data;
275 }
Martyn Welcha7fa9852008-11-12 13:27:06 -0800276
Alexandre Bellonib8157162018-05-17 22:33:25 +0200277 rx8581->rtc = devm_rtc_allocate_device(&client->dev);
278 if (IS_ERR(rx8581->rtc))
Andreas Wernerd643a492014-01-23 15:55:20 -0800279 return PTR_ERR(rx8581->rtc);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800280
Alexandre Bellonib8157162018-05-17 22:33:25 +0200281 rx8581->rtc->ops = &rx8581_rtc_ops;
Alexandre Belloni86c54ef2018-05-17 22:33:26 +0200282 rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
283 rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Bellonic6e3c292018-05-17 22:33:27 +0200284 rx8581->rtc->start_secs = 0;
285 rx8581->rtc->set_start_time = true;
Alexandre Bellonib8157162018-05-17 22:33:25 +0200286
287 return rtc_register_device(rx8581->rtc);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800288}
289
Martyn Welcha7fa9852008-11-12 13:27:06 -0800290static const struct i2c_device_id rx8581_id[] = {
291 { "rx8581", 0 },
292 { }
293};
294MODULE_DEVICE_TABLE(i2c, rx8581_id);
295
Javier Martinez Canillasffbecfb2017-03-03 11:29:22 -0300296static const struct of_device_id rx8581_of_match[] = {
297 { .compatible = "epson,rx8581" },
298 { }
299};
300MODULE_DEVICE_TABLE(of, rx8581_of_match);
301
Martyn Welcha7fa9852008-11-12 13:27:06 -0800302static struct i2c_driver rx8581_driver = {
303 .driver = {
304 .name = "rtc-rx8581",
Javier Martinez Canillasffbecfb2017-03-03 11:29:22 -0300305 .of_match_table = of_match_ptr(rx8581_of_match),
Martyn Welcha7fa9852008-11-12 13:27:06 -0800306 },
307 .probe = rx8581_probe,
Martyn Welcha7fa9852008-11-12 13:27:06 -0800308 .id_table = rx8581_id,
309};
310
Axel Lin0abc9202012-03-23 15:02:31 -0700311module_i2c_driver(rx8581_driver);
Martyn Welcha7fa9852008-11-12 13:27:06 -0800312
Martyn Welch9756c4d2010-04-14 09:24:16 +0100313MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
Martyn Welcha7fa9852008-11-12 13:27:06 -0800314MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
315MODULE_LICENSE("GPL");