Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * An I2C driver for the Epson RX8581 RTC
  3 *
  4 * Author: Martyn Welch <martyn.welch@ge.com>
  5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  6 *
  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
 21#define DRV_VERSION "0.1"
 22
 23#define RX8581_REG_SC		0x00 /* Second in BCD */
 24#define RX8581_REG_MN		0x01 /* Minute in BCD */
 25#define RX8581_REG_HR		0x02 /* Hour in BCD */
 26#define RX8581_REG_DW		0x03 /* Day of Week */
 27#define RX8581_REG_DM		0x04 /* Day of Month in BCD */
 28#define RX8581_REG_MO		0x05 /* Month in BCD */
 29#define RX8581_REG_YR		0x06 /* Year in BCD */
 30#define RX8581_REG_RAM		0x07 /* RAM */
 31#define RX8581_REG_AMN		0x08 /* Alarm Min in BCD*/
 32#define RX8581_REG_AHR		0x09 /* Alarm Hour in BCD */
 33#define RX8581_REG_ADM		0x0A
 34#define RX8581_REG_ADW		0x0A
 35#define RX8581_REG_TMR0		0x0B
 36#define RX8581_REG_TMR1		0x0C
 37#define RX8581_REG_EXT		0x0D /* Extension Register */
 38#define RX8581_REG_FLAG		0x0E /* Flag Register */
 39#define RX8581_REG_CTRL		0x0F /* Control Register */
 40
 41
 42/* Flag Register bit definitions */
 43#define RX8581_FLAG_UF		0x20 /* Update */
 44#define RX8581_FLAG_TF		0x10 /* Timer */
 45#define RX8581_FLAG_AF		0x08 /* Alarm */
 46#define RX8581_FLAG_VLF		0x02 /* Voltage Low */
 47
 48/* Control Register bit definitions */
 49#define RX8581_CTRL_UIE		0x20 /* Update Interrupt Enable */
 50#define RX8581_CTRL_TIE		0x10 /* Timer Interrupt Enable */
 51#define RX8581_CTRL_AIE		0x08 /* Alarm Interrupt Enable */
 52#define RX8581_CTRL_STOP	0x02 /* STOP bit */
 53#define RX8581_CTRL_RESET	0x01 /* RESET bit */
 54
 
 
 
 55struct rx8581 {
 56	struct i2c_client	*client;
 57	struct rtc_device	*rtc;
 58	s32 (*read_block_data)(const struct i2c_client *client, u8 command,
 59				u8 length, u8 *values);
 60	s32 (*write_block_data)(const struct i2c_client *client, u8 command,
 61				u8 length, const u8 *values);
 62};
 63
 64static struct i2c_driver rx8581_driver;
 65
 66static int rx8581_read_block_data(const struct i2c_client *client, u8 command,
 67					u8 length, u8 *values)
 68{
 69	s32 i, data;
 70
 71	for (i = 0; i < length; i++) {
 72		data = i2c_smbus_read_byte_data(client, command + i);
 73		if (data < 0)
 74			return data;
 75		values[i] = data;
 76	}
 77	return i;
 78}
 79
 80static int rx8581_write_block_data(const struct i2c_client *client, u8 command,
 81					u8 length, const u8 *values)
 82{
 83	s32 i, ret;
 84
 85	for (i = 0; i < length; i++) {
 86		ret = i2c_smbus_write_byte_data(client, command + i,
 87						values[i]);
 88		if (ret < 0)
 89			return ret;
 90	}
 91	return length;
 92}
 93
 94/*
 95 * In the routines that deal directly with the rx8581 hardware, we use
 96 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
 97 */
 98static int rx8581_get_datetime(struct i2c_client *client, struct rtc_time *tm)
 99{
 
100	unsigned char date[7];
101	int data, err;
 
102	struct rx8581 *rx8581 = i2c_get_clientdata(client);
103
104	/* First we ensure that the "update flag" is not set, we read the
105	 * time and date then re-read the "update flag". If the update flag
106	 * has been set, we know that the time has changed during the read so
107	 * we repeat the whole process again.
108	 */
109	data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
110	if (data < 0) {
111		dev_err(&client->dev, "Unable to read device flags\n");
112		return -EIO;
 
 
 
 
113	}
114
115	do {
116		/* If update flag set, clear it */
117		if (data & RX8581_FLAG_UF) {
118			err = i2c_smbus_write_byte_data(client,
119				RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
120			if (err != 0) {
121				dev_err(&client->dev, "Unable to write device flags\n");
122				return -EIO;
123			}
124		}
125
126		/* Now read time and date */
127		err = rx8581->read_block_data(client, RX8581_REG_SC,
128			7, date);
129		if (err < 0) {
130			dev_err(&client->dev, "Unable to read date\n");
131			return -EIO;
132		}
133
134		/* Check flag register */
135		data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
136		if (data < 0) {
137			dev_err(&client->dev, "Unable to read device flags\n");
138			return -EIO;
139		}
140	} while (data & RX8581_FLAG_UF);
141
142	if (data & RX8581_FLAG_VLF)
143		dev_info(&client->dev,
144			"low voltage detected, date/time is not reliable.\n");
145
146	dev_dbg(&client->dev,
147		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
148		"wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
149		__func__,
150		date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
151
152	tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
153	tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
154	tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
155	tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
156	tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
157	tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
158	tm->tm_year = bcd2bin(date[RX8581_REG_YR]);
159	if (tm->tm_year < 70)
160		tm->tm_year += 100;	/* assume we are in 1970...2069 */
161
162
163	dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
164		"mday=%d, mon=%d, year=%d, wday=%d\n",
165		__func__,
166		tm->tm_sec, tm->tm_min, tm->tm_hour,
167		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
168
169	err = rtc_valid_tm(tm);
170	if (err < 0)
171		dev_err(&client->dev, "retrieved date/time is not valid.\n");
172
173	return err;
174}
175
176static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm)
177{
178	int data, err;
 
179	unsigned char buf[7];
180	struct rx8581 *rx8581 = i2c_get_clientdata(client);
181
182	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
183		"mday=%d, mon=%d, year=%d, wday=%d\n",
184		__func__,
185		tm->tm_sec, tm->tm_min, tm->tm_hour,
186		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
187
188	/* hours, minutes and seconds */
189	buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
190	buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
191	buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
192
193	buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
194
195	/* month, 1 - 12 */
196	buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
197
198	/* year and century */
199	buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100);
200	buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
201
202	/* Stop the clock */
203	data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
204	if (data < 0) {
205		dev_err(&client->dev, "Unable to read control register\n");
206		return -EIO;
207	}
208
209	err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
210		(data | RX8581_CTRL_STOP));
211	if (err < 0) {
212		dev_err(&client->dev, "Unable to write control register\n");
213		return -EIO;
214	}
215
216	/* write register's data */
217	err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
218	if (err < 0) {
219		dev_err(&client->dev, "Unable to write to date registers\n");
220		return -EIO;
221	}
222
223	/* get VLF and clear it */
224	data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
225	if (data < 0) {
226		dev_err(&client->dev, "Unable to read flag register\n");
227		return -EIO;
228	}
229
230	err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
231		(data & ~(RX8581_FLAG_VLF)));
232	if (err != 0) {
233		dev_err(&client->dev, "Unable to write flag register\n");
234		return -EIO;
235	}
236
237	/* Restart the clock */
238	data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
239	if (data < 0) {
240		dev_err(&client->dev, "Unable to read control register\n");
241		return -EIO;
242	}
243
244	err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
245		(data & ~(RX8581_CTRL_STOP)));
246	if (err != 0) {
247		dev_err(&client->dev, "Unable to write control register\n");
248		return -EIO;
249	}
250
251	return 0;
 
 
 
 
 
 
252}
253
254static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
 
255{
256	return rx8581_get_datetime(to_i2c_client(dev), tm);
 
 
 
257}
258
259static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
 
260{
261	return rx8581_set_datetime(to_i2c_client(dev), tm);
 
 
 
 
 
 
 
262}
263
264static const struct rtc_class_ops rx8581_rtc_ops = {
265	.read_time	= rx8581_rtc_read_time,
266	.set_time	= rx8581_rtc_set_time,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267};
268
269static int rx8581_probe(struct i2c_client *client,
270			const struct i2c_device_id *id)
 
 
 
 
 
 
 
 
271{
272	struct rx8581	  *rx8581;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
274	dev_dbg(&client->dev, "%s\n", __func__);
275
276	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
277		&& !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
278		return -EIO;
279
280	rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
281	if (!rx8581)
282		return -ENOMEM;
283
284	i2c_set_clientdata(client, rx8581);
285	rx8581->client = client;
286
287	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
288		rx8581->read_block_data = i2c_smbus_read_i2c_block_data;
289		rx8581->write_block_data = i2c_smbus_write_i2c_block_data;
290	} else {
291		rx8581->read_block_data = rx8581_read_block_data;
292		rx8581->write_block_data = rx8581_write_block_data;
293	}
294
295	dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
 
 
296
297	rx8581->rtc = devm_rtc_device_register(&client->dev,
298		rx8581_driver.driver.name, &rx8581_rtc_ops, THIS_MODULE);
 
 
 
299
300	if (IS_ERR(rx8581->rtc)) {
301		dev_err(&client->dev,
302			"unable to register the class device\n");
303		return PTR_ERR(rx8581->rtc);
 
304	}
305
306	return 0;
307}
308
309static const struct i2c_device_id rx8581_id[] = {
310	{ "rx8581", 0 },
311	{ }
312};
313MODULE_DEVICE_TABLE(i2c, rx8581_id);
314
 
 
 
 
 
 
 
315static struct i2c_driver rx8581_driver = {
316	.driver		= {
317		.name	= "rtc-rx8581",
 
318	},
319	.probe		= rx8581_probe,
320	.id_table	= rx8581_id,
321};
322
323module_i2c_driver(rx8581_driver);
324
325MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
326MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
327MODULE_LICENSE("GPL");
328MODULE_VERSION(DRV_VERSION);
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * An I2C driver for the Epson RX8581 RTC
  4 *
  5 * Author: Martyn Welch <martyn.welch@ge.com>
  6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  7 *
 
 
 
 
  8 * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
  9 * Copyright 2005-06 Tower Technologies
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/i2c.h>
 14#include <linux/bcd.h>
 15#include <linux/of.h>
 16#include <linux/regmap.h>
 17#include <linux/rtc.h>
 18#include <linux/log2.h>
 19
 
 
 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 */
 37
 38
 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 */
 44
 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 */
 51
 52#define RX8571_USER_RAM		0x10
 53#define RX8571_NVRAM_SIZE	0x10
 54
 55struct rx8581 {
 56	struct regmap		*regmap;
 57	struct rtc_device	*rtc;
 
 
 
 
 58};
 59
 60struct rx85x1_config {
 61	struct regmap_config regmap;
 62	unsigned int num_nvram;
 63};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 64
 65/*
 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.
 68 */
 69static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
 70{
 71	struct i2c_client *client = to_i2c_client(dev);
 72	unsigned char date[7];
 73	unsigned int data;
 74	int err;
 75	struct rx8581 *rx8581 = i2c_get_clientdata(client);
 76
 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.
 81	 */
 82	err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
 83	if (err < 0)
 84		return err;
 85
 86	if (data & RX8581_FLAG_VLF) {
 87		dev_warn(dev,
 88			 "low voltage detected, date/time is not reliable.\n");
 89		return -EINVAL;
 90	}
 91
 92	do {
 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);
 97			if (err < 0)
 98				return err;
 
 
 99		}
100
101		/* Now read time and date */
102		err = regmap_bulk_read(rx8581->regmap, RX8581_REG_SC, date,
103				       sizeof(date));
104		if (err < 0)
105			return err;
 
 
106
107		/* Check flag register */
108		err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
109		if (err < 0)
110			return err;
 
 
111	} while (data & RX8581_FLAG_UF);
112
113	dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
 
 
 
 
 
114		"wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
115		__func__,
116		date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
117
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;
 
 
125
126	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
 
127		"mday=%d, mon=%d, year=%d, wday=%d\n",
128		__func__,
129		tm->tm_sec, tm->tm_min, tm->tm_hour,
130		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
131
132	return 0;
 
 
 
 
133}
134
135static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
136{
137	struct i2c_client *client = to_i2c_client(dev);
138	int err;
139	unsigned char buf[7];
140	struct rx8581 *rx8581 = i2c_get_clientdata(client);
141
142	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
143		"mday=%d, mon=%d, year=%d, wday=%d\n",
144		__func__,
145		tm->tm_sec, tm->tm_min, tm->tm_hour,
146		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
147
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);
152
153	buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
154
155	/* month, 1 - 12 */
156	buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
157
158	/* year and century */
159	buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
160	buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
161
162	/* Stop the clock */
163	err = regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
164				 RX8581_CTRL_STOP, RX8581_CTRL_STOP);
165	if (err < 0)
166		return err;
 
 
 
 
 
 
 
 
167
168	/* write register's data */
169	err = regmap_bulk_write(rx8581->regmap, RX8581_REG_SC,
170				buf, sizeof(buf));
171	if (err < 0)
172		return err;
 
173
174	/* get VLF and clear it */
175	err = regmap_update_bits(rx8581->regmap, RX8581_REG_FLAG,
176				 RX8581_FLAG_VLF, 0);
177	if (err < 0)
178		return err;
 
 
 
 
 
 
 
 
179
180	/* Restart the clock */
181	return regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
182				 RX8581_CTRL_STOP, 0);
183}
 
 
184
185static const struct rtc_class_ops rx8581_rtc_ops = {
186	.read_time	= rx8581_rtc_read_time,
187	.set_time	= rx8581_rtc_set_time,
188};
 
 
189
190static int rx8571_nvram_read(void *priv, unsigned int offset, void *val,
191			     size_t bytes)
192{
193	struct rx8581 *rx8581 = priv;
194
195	return regmap_bulk_read(rx8581->regmap, RX8571_USER_RAM + offset,
196				val, bytes);
197}
198
199static int rx8571_nvram_write(void *priv, unsigned int offset, void *val,
200			      size_t bytes)
201{
202	struct rx8581 *rx8581 = priv;
203
204	return regmap_bulk_write(rx8581->regmap, RX8571_USER_RAM + offset,
205				 val, bytes);
206}
207
208static int rx85x1_nvram_read(void *priv, unsigned int offset, void *val,
209			     size_t bytes)
210{
211	struct rx8581 *rx8581 = priv;
212	unsigned int tmp_val;
213	int ret;
214
215	ret = regmap_read(rx8581->regmap, RX8581_REG_RAM, &tmp_val);
216	(*(unsigned char *)val) = (unsigned char) tmp_val;
217
218	return ret;
219}
220
221static int rx85x1_nvram_write(void *priv, unsigned int offset, void *val,
222			      size_t bytes)
223{
224	struct rx8581 *rx8581 = priv;
225	unsigned char tmp_val;
226
227	tmp_val = *((unsigned char *)val);
228	return regmap_write(rx8581->regmap, RX8581_REG_RAM,
229				(unsigned int)tmp_val);
230}
231
232static const struct rx85x1_config rx8581_config = {
233	.regmap = {
234		.reg_bits = 8,
235		.val_bits = 8,
236		.max_register = 0xf,
237	},
238	.num_nvram = 1
239};
240
241static const struct rx85x1_config rx8571_config = {
242	.regmap = {
243		.reg_bits = 8,
244		.val_bits = 8,
245		.max_register = 0x1f,
246	},
247	.num_nvram = 2
248};
249
250static int rx8581_probe(struct i2c_client *client)
251{
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[] = {
256		{
257			.name = "rx85x1-",
258			.word_size = 1,
259			.stride = 1,
260			.size = 1,
261			.reg_read = rx85x1_nvram_read,
262			.reg_write = rx85x1_nvram_write,
263		}, {
264			.name = "rx8571-",
265			.word_size = 1,
266			.stride = 1,
267			.size = RX8571_NVRAM_SIZE,
268			.reg_read = rx8571_nvram_read,
269			.reg_write = rx8571_nvram_write,
270		},
271	};
272	int ret, i;
273
274	dev_dbg(&client->dev, "%s\n", __func__);
275
276	if (data)
277		config = data;
 
278
279	rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
280	if (!rx8581)
281		return -ENOMEM;
282
283	i2c_set_clientdata(client, rx8581);
 
284
285	rx8581->regmap = devm_regmap_init_i2c(client, &config->regmap);
286	if (IS_ERR(rx8581->regmap))
287		return PTR_ERR(rx8581->regmap);
 
 
 
 
288
289	rx8581->rtc = devm_rtc_allocate_device(&client->dev);
290	if (IS_ERR(rx8581->rtc))
291		return PTR_ERR(rx8581->rtc);
292
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;
298
299	ret = devm_rtc_register_device(rx8581->rtc);
300
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]);
304	}
305
306	return ret;
307}
308
309static const struct i2c_device_id rx8581_id[] = {
310	{ "rx8581" },
311	{ }
312};
313MODULE_DEVICE_TABLE(i2c, rx8581_id);
314
315static const __maybe_unused struct of_device_id rx8581_of_match[] = {
316	{ .compatible = "epson,rx8571", .data = &rx8571_config },
317	{ .compatible = "epson,rx8581", .data = &rx8581_config },
318	{ /* sentinel */ }
319};
320MODULE_DEVICE_TABLE(of, rx8581_of_match);
321
322static struct i2c_driver rx8581_driver = {
323	.driver		= {
324		.name	= "rtc-rx8581",
325		.of_match_table = of_match_ptr(rx8581_of_match),
326	},
327	.probe		= rx8581_probe,
328	.id_table	= rx8581_id,
329};
330
331module_i2c_driver(rx8581_driver);
332
333MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
334MODULE_DESCRIPTION("Epson RX-8571/RX-8581 RTC driver");
335MODULE_LICENSE("GPL");