Blame view

drivers/rtc/rtc-isl12022.c 6.77 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
d6c7428f9   Roman Fietze   rtc: add Intersil...
2
3
4
5
6
7
8
  /*
   * An I2C driver for the Intersil ISL 12022
   *
   * Author: Roman Fietze <roman.fietze@telemotive.de>
   *
   * Based on the Philips PCF8563 RTC
   * by Alessandro Zummo <a.zummo@towertech.it>.
d6c7428f9   Roman Fietze   rtc: add Intersil...
9
10
11
12
13
14
   */
  
  #include <linux/i2c.h>
  #include <linux/bcd.h>
  #include <linux/rtc.h>
  #include <linux/slab.h>
2113852b2   Paul Gortmaker   rtc: Add module.h...
15
  #include <linux/module.h>
17cc54b20   Sachin Kamat   drivers/rtc/rtc-i...
16
  #include <linux/err.h>
db04d6284   Stuart Longland   drivers/rtc/rtc-i...
17
18
  #include <linux/of.h>
  #include <linux/of_device.h>
d6c7428f9   Roman Fietze   rtc: add Intersil...
19

d6c7428f9   Roman Fietze   rtc: add Intersil...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  /* ISL register offsets */
  #define ISL12022_REG_SC		0x00
  #define ISL12022_REG_MN		0x01
  #define ISL12022_REG_HR		0x02
  #define ISL12022_REG_DT		0x03
  #define ISL12022_REG_MO		0x04
  #define ISL12022_REG_YR		0x05
  #define ISL12022_REG_DW		0x06
  
  #define ISL12022_REG_SR		0x07
  #define ISL12022_REG_INT	0x08
  
  /* ISL register bits */
  #define ISL12022_HR_MIL		(1 << 7)	/* military or 24 hour time */
  
  #define ISL12022_SR_LBAT85	(1 << 2)
  #define ISL12022_SR_LBAT75	(1 << 1)
  
  #define ISL12022_INT_WRTC	(1 << 6)
  
  
  static struct i2c_driver isl12022_driver;
  
  struct isl12022 {
  	struct rtc_device *rtc;
  
  	bool write_enabled;	/* true if write enable is set */
  };
  
  
  static int isl12022_read_regs(struct i2c_client *client, uint8_t reg,
  			      uint8_t *data, size_t n)
  {
  	struct i2c_msg msgs[] = {
  		{
  			.addr	= client->addr,
  			.flags	= 0,
  			.len	= 1,
  			.buf	= data
  		},		/* setup read ptr */
  		{
  			.addr	= client->addr,
  			.flags	= I2C_M_RD,
  			.len	= n,
  			.buf	= data
  		}
  	};
  
  	int ret;
  
  	data[0] = reg;
  	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  	if (ret != ARRAY_SIZE(msgs)) {
  		dev_err(&client->dev, "%s: read error, ret=%d
  ",
  			__func__, ret);
  		return -EIO;
  	}
  
  	return 0;
  }
  
  
  static int isl12022_write_reg(struct i2c_client *client,
  			      uint8_t reg, uint8_t val)
  {
  	uint8_t data[2] = { reg, val };
  	int err;
  
  	err = i2c_master_send(client, data, sizeof(data));
  	if (err != sizeof(data)) {
  		dev_err(&client->dev,
  			"%s: err=%d addr=%02x, data=%02x
  ",
  			__func__, err, data[0], data[1]);
  		return -EIO;
  	}
  
  	return 0;
  }
  
  
  /*
   * In the routines that deal directly with the isl12022 hardware, we use
   * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
   */
143d92bea   Alexandre Belloni   rtc: isl12022: re...
106
  static int isl12022_rtc_read_time(struct device *dev, struct rtc_time *tm)
d6c7428f9   Roman Fietze   rtc: add Intersil...
107
  {
143d92bea   Alexandre Belloni   rtc: isl12022: re...
108
  	struct i2c_client *client = to_i2c_client(dev);
d6c7428f9   Roman Fietze   rtc: add Intersil...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	uint8_t buf[ISL12022_REG_INT + 1];
  	int ret;
  
  	ret = isl12022_read_regs(client, ISL12022_REG_SC, buf, sizeof(buf));
  	if (ret)
  		return ret;
  
  	if (buf[ISL12022_REG_SR] & (ISL12022_SR_LBAT85 | ISL12022_SR_LBAT75)) {
  		dev_warn(&client->dev,
  			 "voltage dropped below %u%%, "
  			 "date and time is not reliable.
  ",
  			 buf[ISL12022_REG_SR] & ISL12022_SR_LBAT85 ? 85 : 75);
  	}
  
  	dev_dbg(&client->dev,
  		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
  		"mday=%02x, mon=%02x, year=%02x, wday=%02x, "
  		"sr=%02x, int=%02x",
  		__func__,
  		buf[ISL12022_REG_SC],
  		buf[ISL12022_REG_MN],
  		buf[ISL12022_REG_HR],
  		buf[ISL12022_REG_DT],
  		buf[ISL12022_REG_MO],
  		buf[ISL12022_REG_YR],
  		buf[ISL12022_REG_DW],
  		buf[ISL12022_REG_SR],
  		buf[ISL12022_REG_INT]);
  
  	tm->tm_sec = bcd2bin(buf[ISL12022_REG_SC] & 0x7F);
  	tm->tm_min = bcd2bin(buf[ISL12022_REG_MN] & 0x7F);
  	tm->tm_hour = bcd2bin(buf[ISL12022_REG_HR] & 0x3F);
  	tm->tm_mday = bcd2bin(buf[ISL12022_REG_DT] & 0x3F);
  	tm->tm_wday = buf[ISL12022_REG_DW] & 0x07;
  	tm->tm_mon = bcd2bin(buf[ISL12022_REG_MO] & 0x1F) - 1;
  	tm->tm_year = bcd2bin(buf[ISL12022_REG_YR]) + 100;
  
  	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  		"mday=%d, mon=%d, year=%d, wday=%d
  ",
  		__func__,
  		tm->tm_sec, tm->tm_min, tm->tm_hour,
  		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
22652ba72   Alexandre Belloni   rtc: stop validat...
153
  	return 0;
d6c7428f9   Roman Fietze   rtc: add Intersil...
154
  }
143d92bea   Alexandre Belloni   rtc: isl12022: re...
155
  static int isl12022_rtc_set_time(struct device *dev, struct rtc_time *tm)
d6c7428f9   Roman Fietze   rtc: add Intersil...
156
  {
143d92bea   Alexandre Belloni   rtc: isl12022: re...
157
  	struct i2c_client *client = to_i2c_client(dev);
d6c7428f9   Roman Fietze   rtc: add Intersil...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  	struct isl12022 *isl12022 = i2c_get_clientdata(client);
  	size_t i;
  	int ret;
  	uint8_t buf[ISL12022_REG_DW + 1];
  
  	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  		"mday=%d, mon=%d, year=%d, wday=%d
  ",
  		__func__,
  		tm->tm_sec, tm->tm_min, tm->tm_hour,
  		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  
  	if (!isl12022->write_enabled) {
  
  		ret = isl12022_read_regs(client, ISL12022_REG_INT, buf, 1);
  		if (ret)
  			return ret;
  
  		/* Check if WRTC (write rtc enable) is set factory default is
  		 * 0 (not set) */
  		if (!(buf[0] & ISL12022_INT_WRTC)) {
  			dev_info(&client->dev,
  				 "init write enable and 24 hour format
  ");
  
  			/* Set the write enable bit. */
  			ret = isl12022_write_reg(client,
  						 ISL12022_REG_INT,
  						 buf[0] | ISL12022_INT_WRTC);
  			if (ret)
  				return ret;
  
  			/* Write to any RTC register to start RTC, we use the
  			 * HR register, setting the MIL bit to use the 24 hour
  			 * format. */
  			ret = isl12022_read_regs(client, ISL12022_REG_HR,
  						 buf, 1);
  			if (ret)
  				return ret;
  
  			ret = isl12022_write_reg(client,
  						 ISL12022_REG_HR,
  						 buf[0] | ISL12022_HR_MIL);
  			if (ret)
  				return ret;
  		}
c1f5f0549   Gustavo A. R. Silva   rtc: isl12022: us...
204
  		isl12022->write_enabled = true;
d6c7428f9   Roman Fietze   rtc: add Intersil...
205
206
207
208
209
  	}
  
  	/* hours, minutes and seconds */
  	buf[ISL12022_REG_SC] = bin2bcd(tm->tm_sec);
  	buf[ISL12022_REG_MN] = bin2bcd(tm->tm_min);
6d23b2582   Roman Fietze   rtc-isl12022: pro...
210
  	buf[ISL12022_REG_HR] = bin2bcd(tm->tm_hour) | ISL12022_HR_MIL;
d6c7428f9   Roman Fietze   rtc: add Intersil...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  
  	buf[ISL12022_REG_DT] = bin2bcd(tm->tm_mday);
  
  	/* month, 1 - 12 */
  	buf[ISL12022_REG_MO] = bin2bcd(tm->tm_mon + 1);
  
  	/* year and century */
  	buf[ISL12022_REG_YR] = bin2bcd(tm->tm_year % 100);
  
  	buf[ISL12022_REG_DW] = tm->tm_wday & 0x07;
  
  	/* write register's data */
  	for (i = 0; i < ARRAY_SIZE(buf); i++) {
  		ret = isl12022_write_reg(client, ISL12022_REG_SC + i,
  					 buf[ISL12022_REG_SC + i]);
  		if (ret)
  			return -EIO;
0b7e03924   Peter Senna Tschudin   drivers/rtc: remo...
228
  	}
d6c7428f9   Roman Fietze   rtc: add Intersil...
229
230
231
  
  	return 0;
  }
d6c7428f9   Roman Fietze   rtc: add Intersil...
232
233
234
235
236
237
238
239
240
  static const struct rtc_class_ops isl12022_rtc_ops = {
  	.read_time	= isl12022_rtc_read_time,
  	.set_time	= isl12022_rtc_set_time,
  };
  
  static int isl12022_probe(struct i2c_client *client,
  			  const struct i2c_device_id *id)
  {
  	struct isl12022 *isl12022;
d6c7428f9   Roman Fietze   rtc: add Intersil...
241
242
  	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  		return -ENODEV;
dc831f976   Jingoo Han   rtc: rtc-isl12022...
243
244
  	isl12022 = devm_kzalloc(&client->dev, sizeof(struct isl12022),
  				GFP_KERNEL);
d6c7428f9   Roman Fietze   rtc: add Intersil...
245
246
  	if (!isl12022)
  		return -ENOMEM;
d6c7428f9   Roman Fietze   rtc: add Intersil...
247
  	i2c_set_clientdata(client, isl12022);
dc831f976   Jingoo Han   rtc: rtc-isl12022...
248
249
250
  	isl12022->rtc = devm_rtc_device_register(&client->dev,
  					isl12022_driver.driver.name,
  					&isl12022_rtc_ops, THIS_MODULE);
dac30a984   Sachin Kamat   drivers/rtc: Repl...
251
  	return PTR_ERR_OR_ZERO(isl12022->rtc);
d6c7428f9   Roman Fietze   rtc: add Intersil...
252
  }
db04d6284   Stuart Longland   drivers/rtc/rtc-i...
253
  #ifdef CONFIG_OF
a28885bc7   Uwe Kleine-König   rtc: make of_devi...
254
  static const struct of_device_id isl12022_dt_match[] = {
c5cd8273c   Arnaud Ebalard   rtc: isl12022: de...
255
256
  	{ .compatible = "isl,isl12022" }, /* for backward compat., don't use */
  	{ .compatible = "isil,isl12022" },
db04d6284   Stuart Longland   drivers/rtc/rtc-i...
257
258
  	{ },
  };
1c4fc2955   Javier Martinez Canillas   rtc: Export OF mo...
259
  MODULE_DEVICE_TABLE(of, isl12022_dt_match);
db04d6284   Stuart Longland   drivers/rtc/rtc-i...
260
  #endif
d6c7428f9   Roman Fietze   rtc: add Intersil...
261
262
  static const struct i2c_device_id isl12022_id[] = {
  	{ "isl12022", 0 },
d6c7428f9   Roman Fietze   rtc: add Intersil...
263
264
265
266
267
268
269
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, isl12022_id);
  
  static struct i2c_driver isl12022_driver = {
  	.driver		= {
  		.name	= "rtc-isl12022",
db04d6284   Stuart Longland   drivers/rtc/rtc-i...
270
271
272
  #ifdef CONFIG_OF
  		.of_match_table = of_match_ptr(isl12022_dt_match),
  #endif
d6c7428f9   Roman Fietze   rtc: add Intersil...
273
274
  	},
  	.probe		= isl12022_probe,
d6c7428f9   Roman Fietze   rtc: add Intersil...
275
276
  	.id_table	= isl12022_id,
  };
0abc92011   Axel Lin   rtc: convert rtc ...
277
  module_i2c_driver(isl12022_driver);
d6c7428f9   Roman Fietze   rtc: add Intersil...
278
279
280
281
  
  MODULE_AUTHOR("roman.fietze@telemotive.de");
  MODULE_DESCRIPTION("ISL 12022 RTC driver");
  MODULE_LICENSE("GPL");