1/*
2 * An I2C driver for the Intersil ISL 12022
3 *
4 * Author: Roman Fietze <roman.fietze@telemotive.de>
5 *
6 * Based on the Philips PCF8563 RTC
7 * by Alessandro Zummo <a.zummo@towertech.it>.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
12 */
13
14#include <linux/i2c.h>
15#include <linux/bcd.h>
16#include <linux/rtc.h>
17#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22
23#define DRV_VERSION "0.1"
24
25/* ISL register offsets */
26#define ISL12022_REG_SC		0x00
27#define ISL12022_REG_MN		0x01
28#define ISL12022_REG_HR		0x02
29#define ISL12022_REG_DT		0x03
30#define ISL12022_REG_MO		0x04
31#define ISL12022_REG_YR		0x05
32#define ISL12022_REG_DW		0x06
33
34#define ISL12022_REG_SR		0x07
35#define ISL12022_REG_INT	0x08
36
37/* ISL register bits */
38#define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
39
40#define ISL12022_SR_LBAT85	(1 << 2)
41#define ISL12022_SR_LBAT75	(1 << 1)
42
43#define ISL12022_INT_WRTC	(1 << 6)
44
45
46static struct i2c_driver isl12022_driver;
47
48struct isl12022 {
49	struct rtc_device *rtc;
50
51	bool write_enabled;	/* true if write enable is set */
52};
53
54
55static int isl12022_read_regs(struct i2c_client *client, uint8_t reg,
56			      uint8_t *data, size_t n)
57{
58	struct i2c_msg msgs[] = {
59		{
60			.addr	= client->addr,
61			.flags	= 0,
62			.len	= 1,
63			.buf	= data
64		},		/* setup read ptr */
65		{
66			.addr	= client->addr,
67			.flags	= I2C_M_RD,
68			.len	= n,
69			.buf	= data
70		}
71	};
72
73	int ret;
74
75	data[0] = reg;
76	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
77	if (ret != ARRAY_SIZE(msgs)) {
78		dev_err(&client->dev, "%s: read error, ret=%d\n",
79			__func__, ret);
80		return -EIO;
81	}
82
83	return 0;
84}
85
86
87static int isl12022_write_reg(struct i2c_client *client,
88			      uint8_t reg, uint8_t val)
89{
90	uint8_t data[2] = { reg, val };
91	int err;
92
93	err = i2c_master_send(client, data, sizeof(data));
94	if (err != sizeof(data)) {
95		dev_err(&client->dev,
96			"%s: err=%d addr=%02x, data=%02x\n",
97			__func__, err, data[0], data[1]);
98		return -EIO;
99	}
100
101	return 0;
102}
103
104
105/*
106 * In the routines that deal directly with the isl12022 hardware, we use
107 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
108 */
109static int isl12022_get_datetime(struct i2c_client *client, struct rtc_time *tm)
110{
111	uint8_t buf[ISL12022_REG_INT + 1];
112	int ret;
113
114	ret = isl12022_read_regs(client, ISL12022_REG_SC, buf, sizeof(buf));
115	if (ret)
116		return ret;
117
118	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
119		dev_warn(&client->dev,
120			 "voltage dropped below %u%%, "
121			 "date and time is not reliable.\n",
122			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
123	}
124
125	dev_dbg(&client->dev,
126		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
127		"mday=%02x, mon=%02x, year=%02x, wday=%02x, "
128		"sr=%02x, int=%02x",
129		__func__,
130		buf[ISL12022_REG_SC],
131		buf[ISL12022_REG_MN],
132		buf[ISL12022_REG_HR],
133		buf[ISL12022_REG_DT],
134		buf[ISL12022_REG_MO],
135		buf[ISL12022_REG_YR],
136		buf[ISL12022_REG_DW],
137		buf[ISL12022_REG_SR],
138		buf[ISL12022_REG_INT]);
139
140	tm->tm_sec = bcd2bin(buf[ISL12022_REG_SC] & 0x7F);
141	tm->tm_min = bcd2bin(buf[ISL12022_REG_MN] & 0x7F);
142	tm->tm_hour = bcd2bin(buf[ISL12022_REG_HR] & 0x3F);
143	tm->tm_mday = bcd2bin(buf[ISL12022_REG_DT] & 0x3F);
144	tm->tm_wday = buf[ISL12022_REG_DW] & 0x07;
145	tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
146	tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
147
148	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
149		"mday=%d, mon=%d, year=%d, wday=%d\n",
150		__func__,
151		tm->tm_sec, tm->tm_min, tm->tm_hour,
152		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
153
154	/* The clock can give out invalid datetime, but we cannot return
155	 * -EINVAL otherwise hwclock will refuse to set the time on bootup. */
156	if (rtc_valid_tm(tm) < 0)
157		dev_err(&client->dev, "retrieved date and time is invalid.\n");
158
159	return 0;
160}
161
162static int isl12022_set_datetime(struct i2c_client *client, struct rtc_time *tm)
163{
164	struct isl12022 *isl12022 = i2c_get_clientdata(client);
165	size_t i;
166	int ret;
167	uint8_t buf[ISL12022_REG_DW + 1];
168
169	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
170		"mday=%d, mon=%d, year=%d, wday=%d\n",
171		__func__,
172		tm->tm_sec, tm->tm_min, tm->tm_hour,
173		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
174
175	if (!isl12022->write_enabled) {
176
177		ret = isl12022_read_regs(client, ISL12022_REG_INT, buf, 1);
178		if (ret)
179			return ret;
180
181		/* Check if WRTC (write rtc enable) is set factory default is
182		 * 0 (not set) */
183		if (!(buf[0] & ISL12022_INT_WRTC)) {
184			dev_info(&client->dev,
185				 "init write enable and 24 hour format\n");
186
187			/* Set the write enable bit. */
188			ret = isl12022_write_reg(client,
189						 ISL12022_REG_INT,
190						 buf[0] | ISL12022_INT_WRTC);
191			if (ret)
192				return ret;
193
194			/* Write to any RTC register to start RTC, we use the
195			 * HR register, setting the MIL bit to use the 24 hour
196			 * format. */
197			ret = isl12022_read_regs(client, ISL12022_REG_HR,
198						 buf, 1);
199			if (ret)
200				return ret;
201
202			ret = isl12022_write_reg(client,
203						 ISL12022_REG_HR,
204						 buf[0] | ISL12022_HR_MIL);
205			if (ret)
206				return ret;
207		}
208
209		isl12022->write_enabled = 1;
210	}
211
212	/* hours, minutes and seconds */
213	buf[ISL12022_REG_SC] = bin2bcd(tm->tm_sec);
214	buf[ISL12022_REG_MN] = bin2bcd(tm->tm_min);
215	buf[ISL12022_REG_HR] = bin2bcd(tm->tm_hour) | ISL12022_HR_MIL;
216
217	buf[ISL12022_REG_DT] = bin2bcd(tm->tm_mday);
218
219	/* month, 1 - 12 */
220	buf[ISL12022_REG_MO] = bin2bcd(tm->tm_mon + 1);
221
222	/* year and century */
223	buf[ISL12022_REG_YR] = bin2bcd(tm->tm_year % 100);
224
225	buf[ISL12022_REG_DW] = tm->tm_wday & 0x07;
226
227	/* write register's data */
228	for (i = 0; i < ARRAY_SIZE(buf); i++) {
229		ret = isl12022_write_reg(client, ISL12022_REG_SC + i,
230					 buf[ISL12022_REG_SC + i]);
231		if (ret)
232			return -EIO;
233	}
234
235	return 0;
236}
237
238static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
239{
240	return isl12022_get_datetime(to_i2c_client(dev), tm);
241}
242
243static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
244{
245	return isl12022_set_datetime(to_i2c_client(dev), tm);
246}
247
248static const struct rtc_class_ops isl12022_rtc_ops = {
249	.read_time	= isl12022_rtc_read_time,
250	.set_time	= isl12022_rtc_set_time,
251};
252
253static int isl12022_probe(struct i2c_client *client,
254			  const struct i2c_device_id *id)
255{
256	struct isl12022 *isl12022;
257
258	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
259		return -ENODEV;
260
261	isl12022 = devm_kzalloc(&client->dev, sizeof(struct isl12022),
262				GFP_KERNEL);
263	if (!isl12022)
264		return -ENOMEM;
265
266	dev_dbg(&client->dev, "chip found, driver version " DRV_VERSION "\n");
267
268	i2c_set_clientdata(client, isl12022);
269
270	isl12022->rtc = devm_rtc_device_register(&client->dev,
271					isl12022_driver.driver.name,
272					&isl12022_rtc_ops, THIS_MODULE);
273	return PTR_ERR_OR_ZERO(isl12022->rtc);
274}
275
276#ifdef CONFIG_OF
277static const struct of_device_id isl12022_dt_match[] = {
278	{ .compatible = "isl,isl12022" },
279	{ },
280};
281#endif
282
283static const struct i2c_device_id isl12022_id[] = {
284	{ "isl12022", 0 },
285	{ }
286};
287MODULE_DEVICE_TABLE(i2c, isl12022_id);
288
289static struct i2c_driver isl12022_driver = {
290	.driver		= {
291		.name	= "rtc-isl12022",
292#ifdef CONFIG_OF
293		.of_match_table = of_match_ptr(isl12022_dt_match),
294#endif
295	},
296	.probe		= isl12022_probe,
297	.id_table	= isl12022_id,
298};
299
300module_i2c_driver(isl12022_driver);
301
302MODULE_AUTHOR("roman.fietze@telemotive.de");
303MODULE_DESCRIPTION("ISL 12022 RTC driver");
304MODULE_LICENSE("GPL");
305MODULE_VERSION(DRV_VERSION);
306