Linux Audio

Check our new training course

Loading...
v6.2
  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/of_device.h>
 17#include <linux/regmap.h>
 18#include <linux/rtc.h>
 19#include <linux/log2.h>
 20
 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 */
 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
 53#define RX8571_USER_RAM		0x10
 54#define RX8571_NVRAM_SIZE	0x10
 55
 56struct rx8581 {
 57	struct regmap		*regmap;
 58	struct rtc_device	*rtc;
 
 
 
 
 59};
 60
 61struct rx85x1_config {
 62	struct regmap_config regmap;
 63	unsigned int num_nvram;
 64};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 65
 66/*
 67 * In the routines that deal directly with the rx8581 hardware, we use
 68 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
 69 */
 70static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
 71{
 72	struct i2c_client *client = to_i2c_client(dev);
 73	unsigned char date[7];
 74	unsigned int data;
 75	int err;
 76	struct rx8581 *rx8581 = i2c_get_clientdata(client);
 77
 78	/* First we ensure that the "update flag" is not set, we read the
 79	 * time and date then re-read the "update flag". If the update flag
 80	 * has been set, we know that the time has changed during the read so
 81	 * we repeat the whole process again.
 82	 */
 83	err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
 84	if (err < 0)
 85		return err;
 86
 87	if (data & RX8581_FLAG_VLF) {
 88		dev_warn(dev,
 89			 "low voltage detected, date/time is not reliable.\n");
 90		return -EINVAL;
 91	}
 92
 93	do {
 94		/* If update flag set, clear it */
 95		if (data & RX8581_FLAG_UF) {
 96			err = regmap_write(rx8581->regmap, RX8581_REG_FLAG,
 97					  data & ~RX8581_FLAG_UF);
 98			if (err < 0)
 99				return err;
 
 
100		}
101
102		/* Now read time and date */
103		err = regmap_bulk_read(rx8581->regmap, RX8581_REG_SC, date,
104				       sizeof(date));
105		if (err < 0)
106			return err;
 
 
107
108		/* Check flag register */
109		err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
110		if (err < 0)
111			return err;
 
 
112	} while (data & RX8581_FLAG_UF);
113
114	dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
 
 
 
 
 
115		"wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
116		__func__,
117		date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
118
119	tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
120	tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
121	tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
122	tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
123	tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
124	tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
125	tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
 
 
126
127	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
 
128		"mday=%d, mon=%d, year=%d, wday=%d\n",
129		__func__,
130		tm->tm_sec, tm->tm_min, tm->tm_hour,
131		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
132
133	return 0;
134}
135
136static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
137{
138	struct i2c_client *client = to_i2c_client(dev);
139	int err;
140	unsigned char buf[7];
141	struct rx8581 *rx8581 = i2c_get_clientdata(client);
142
143	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
144		"mday=%d, mon=%d, year=%d, wday=%d\n",
145		__func__,
146		tm->tm_sec, tm->tm_min, tm->tm_hour,
147		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
148
149	/* hours, minutes and seconds */
150	buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
151	buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
152	buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
153
154	buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
155
156	/* month, 1 - 12 */
157	buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
158
159	/* year and century */
160	buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
161	buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
162
163	/* Stop the clock */
164	err = regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
165				 RX8581_CTRL_STOP, RX8581_CTRL_STOP);
166	if (err < 0)
167		return err;
 
 
 
 
 
 
 
 
168
169	/* write register's data */
170	err = regmap_bulk_write(rx8581->regmap, RX8581_REG_SC,
171				buf, sizeof(buf));
172	if (err < 0)
173		return err;
 
174
175	/* get VLF and clear it */
176	err = regmap_update_bits(rx8581->regmap, RX8581_REG_FLAG,
177				 RX8581_FLAG_VLF, 0);
178	if (err < 0)
179		return err;
180
181	/* Restart the clock */
182	return regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
183				 RX8581_CTRL_STOP, 0);
184}
185
186static const struct rtc_class_ops rx8581_rtc_ops = {
187	.read_time	= rx8581_rtc_read_time,
188	.set_time	= rx8581_rtc_set_time,
189};
190
191static int rx8571_nvram_read(void *priv, unsigned int offset, void *val,
192			     size_t bytes)
193{
194	struct rx8581 *rx8581 = priv;
 
 
195
196	return regmap_bulk_read(rx8581->regmap, RX8571_USER_RAM + offset,
197				val, bytes);
198}
 
 
 
199
200static int rx8571_nvram_write(void *priv, unsigned int offset, void *val,
201			      size_t bytes)
202{
203	struct rx8581 *rx8581 = priv;
 
 
204
205	return regmap_bulk_write(rx8581->regmap, RX8571_USER_RAM + offset,
206				 val, bytes);
207}
208
209static int rx85x1_nvram_read(void *priv, unsigned int offset, void *val,
210			     size_t bytes)
211{
212	struct rx8581 *rx8581 = priv;
213	unsigned int tmp_val;
214	int ret;
215
216	ret = regmap_read(rx8581->regmap, RX8581_REG_RAM, &tmp_val);
217	(*(unsigned char *)val) = (unsigned char) tmp_val;
218
219	return ret;
220}
221
222static int rx85x1_nvram_write(void *priv, unsigned int offset, void *val,
223			      size_t bytes)
224{
225	struct rx8581 *rx8581 = priv;
226	unsigned char tmp_val;
227
228	tmp_val = *((unsigned char *)val);
229	return regmap_write(rx8581->regmap, RX8581_REG_RAM,
230				(unsigned int)tmp_val);
231}
232
233static const struct rx85x1_config rx8581_config = {
234	.regmap = {
235		.reg_bits = 8,
236		.val_bits = 8,
237		.max_register = 0xf,
238	},
239	.num_nvram = 1
240};
241
242static const struct rx85x1_config rx8571_config = {
243	.regmap = {
244		.reg_bits = 8,
245		.val_bits = 8,
246		.max_register = 0x1f,
247	},
248	.num_nvram = 2
249};
250
251static int rx8581_probe(struct i2c_client *client)
 
252{
253	struct rx8581 *rx8581;
254	const struct rx85x1_config *config = &rx8581_config;
255	const void *data = of_device_get_match_data(&client->dev);
256	static struct nvmem_config nvmem_cfg[] = {
257		{
258			.name = "rx85x1-",
259			.word_size = 1,
260			.stride = 1,
261			.size = 1,
262			.reg_read = rx85x1_nvram_read,
263			.reg_write = rx85x1_nvram_write,
264		}, {
265			.name = "rx8571-",
266			.word_size = 1,
267			.stride = 1,
268			.size = RX8571_NVRAM_SIZE,
269			.reg_read = rx8571_nvram_read,
270			.reg_write = rx8571_nvram_write,
271		},
272	};
273	int ret, i;
274
275	dev_dbg(&client->dev, "%s\n", __func__);
276
277	if (data)
278		config = data;
 
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
286	rx8581->regmap = devm_regmap_init_i2c(client, &config->regmap);
287	if (IS_ERR(rx8581->regmap))
288		return PTR_ERR(rx8581->regmap);
289
290	rx8581->rtc = devm_rtc_allocate_device(&client->dev);
291	if (IS_ERR(rx8581->rtc))
292		return PTR_ERR(rx8581->rtc);
293
294	rx8581->rtc->ops = &rx8581_rtc_ops;
295	rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
296	rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
297	rx8581->rtc->start_secs = 0;
298	rx8581->rtc->set_start_time = true;
299
300	ret = devm_rtc_register_device(rx8581->rtc);
 
301
302	for (i = 0; i < config->num_nvram; i++) {
303		nvmem_cfg[i].priv = rx8581;
304		devm_rtc_nvmem_register(rx8581->rtc, &nvmem_cfg[i]);
 
305	}
306
307	return ret;
308}
309
310static const struct i2c_device_id rx8581_id[] = {
311	{ "rx8581", 0 },
312	{ }
313};
314MODULE_DEVICE_TABLE(i2c, rx8581_id);
315
316static const __maybe_unused struct of_device_id rx8581_of_match[] = {
317	{ .compatible = "epson,rx8571", .data = &rx8571_config },
318	{ .compatible = "epson,rx8581", .data = &rx8581_config },
319	{ /* sentinel */ }
320};
321MODULE_DEVICE_TABLE(of, rx8581_of_match);
322
323static struct i2c_driver rx8581_driver = {
324	.driver		= {
325		.name	= "rtc-rx8581",
326		.of_match_table = of_match_ptr(rx8581_of_match),
327	},
328	.probe_new	= rx8581_probe,
329	.id_table	= rx8581_id,
330};
331
332module_i2c_driver(rx8581_driver);
333
334MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
335MODULE_DESCRIPTION("Epson RX-8571/RX-8581 RTC driver");
336MODULE_LICENSE("GPL");
v4.17
 
  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 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
 
 
 
 53struct 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
 62static struct i2c_driver rx8581_driver;
 63
 64static 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
 92/*
 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 */
 96static int rx8581_get_datetime(struct i2c_client *client, struct rtc_time *tm)
 97{
 
 98	unsigned char date[7];
 99	int data, err;
 
100	struct rx8581 *rx8581 = i2c_get_clientdata(client);
101
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.
106	 */
107	data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
108	if (data < 0) {
109		dev_err(&client->dev, "Unable to read device flags\n");
110		return -EIO;
 
 
 
 
111	}
112
113	do {
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));
118			if (err != 0) {
119				dev_err(&client->dev, "Unable to write device flags\n");
120				return -EIO;
121			}
122		}
123
124		/* Now read time and date */
125		err = rx8581->read_block_data(client, RX8581_REG_SC,
126			7, date);
127		if (err < 0) {
128			dev_err(&client->dev, "Unable to read date\n");
129			return -EIO;
130		}
131
132		/* Check flag register */
133		data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
134		if (data < 0) {
135			dev_err(&client->dev, "Unable to read device flags\n");
136			return -EIO;
137		}
138	} while (data & RX8581_FLAG_UF);
139
140	if (data & RX8581_FLAG_VLF)
141		dev_info(&client->dev,
142			"low voltage detected, date/time is not reliable.\n");
143
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",
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 */
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 */
159
160
161	dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
162		"mday=%d, mon=%d, year=%d, wday=%d\n",
163		__func__,
164		tm->tm_sec, tm->tm_min, tm->tm_hour,
165		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
166
167	return 0;
168}
169
170static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm)
171{
172	int data, err;
 
173	unsigned char buf[7];
174	struct rx8581 *rx8581 = i2c_get_clientdata(client);
175
176	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
177		"mday=%d, mon=%d, year=%d, wday=%d\n",
178		__func__,
179		tm->tm_sec, tm->tm_min, tm->tm_hour,
180		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
181
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);
186
187	buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
188
189	/* month, 1 - 12 */
190	buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
191
192	/* year and century */
193	buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100);
194	buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
195
196	/* Stop the clock */
197	data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
198	if (data < 0) {
199		dev_err(&client->dev, "Unable to read control register\n");
200		return -EIO;
201	}
202
203	err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
204		(data | RX8581_CTRL_STOP));
205	if (err < 0) {
206		dev_err(&client->dev, "Unable to write control register\n");
207		return -EIO;
208	}
209
210	/* write register's data */
211	err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
212	if (err < 0) {
213		dev_err(&client->dev, "Unable to write to date registers\n");
214		return -EIO;
215	}
216
217	/* get VLF and clear it */
218	data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
219	if (data < 0) {
220		dev_err(&client->dev, "Unable to read flag register\n");
221		return -EIO;
222	}
 
 
 
 
 
 
 
 
 
223
224	err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
225		(data & ~(RX8581_FLAG_VLF)));
226	if (err != 0) {
227		dev_err(&client->dev, "Unable to write flag register\n");
228		return -EIO;
229	}
230
231	/* Restart the clock */
232	data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
233	if (data < 0) {
234		dev_err(&client->dev, "Unable to read control register\n");
235		return -EIO;
236	}
237
238	err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
239		(data & ~(RX8581_CTRL_STOP)));
240	if (err != 0) {
241		dev_err(&client->dev, "Unable to write control register\n");
242		return -EIO;
243	}
244
245	return 0;
 
246}
247
248static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
 
249{
250	return rx8581_get_datetime(to_i2c_client(dev), tm);
 
 
 
 
 
 
 
251}
252
253static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
 
254{
255	return rx8581_set_datetime(to_i2c_client(dev), tm);
 
 
 
 
 
256}
257
258static const struct rtc_class_ops rx8581_rtc_ops = {
259	.read_time	= rx8581_rtc_read_time,
260	.set_time	= rx8581_rtc_set_time,
 
 
 
 
 
 
 
 
 
 
 
 
 
261};
262
263static int rx8581_probe(struct i2c_client *client,
264			const struct i2c_device_id *id)
265{
266	struct rx8581	  *rx8581;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
268	dev_dbg(&client->dev, "%s\n", __func__);
269
270	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
271		&& !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
272		return -EIO;
273
274	rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
275	if (!rx8581)
276		return -ENOMEM;
277
278	i2c_set_clientdata(client, rx8581);
279	rx8581->client = client;
280
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;
284	} else {
285		rx8581->read_block_data = rx8581_read_block_data;
286		rx8581->write_block_data = rx8581_write_block_data;
287	}
 
 
 
 
 
 
288
289	rx8581->rtc = devm_rtc_device_register(&client->dev,
290		rx8581_driver.driver.name, &rx8581_rtc_ops, THIS_MODULE);
291
292	if (IS_ERR(rx8581->rtc)) {
293		dev_err(&client->dev,
294			"unable to register the class device\n");
295		return PTR_ERR(rx8581->rtc);
296	}
297
298	return 0;
299}
300
301static const struct i2c_device_id rx8581_id[] = {
302	{ "rx8581", 0 },
303	{ }
304};
305MODULE_DEVICE_TABLE(i2c, rx8581_id);
306
307static const struct of_device_id rx8581_of_match[] = {
308	{ .compatible = "epson,rx8581" },
309	{ }
 
310};
311MODULE_DEVICE_TABLE(of, rx8581_of_match);
312
313static struct i2c_driver rx8581_driver = {
314	.driver		= {
315		.name	= "rtc-rx8581",
316		.of_match_table = of_match_ptr(rx8581_of_match),
317	},
318	.probe		= rx8581_probe,
319	.id_table	= rx8581_id,
320};
321
322module_i2c_driver(rx8581_driver);
323
324MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
325MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
326MODULE_LICENSE("GPL");