1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Seiko Instruments S-35390A RTC Driver
4  *
5  * Copyright (c) 2007 Byron Bradley
6  */
7 
8 #include <linux/module.h>
9 #include <linux/rtc.h>
10 #include <linux/i2c.h>
11 #include <linux/bitrev.h>
12 #include <linux/bcd.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 
16 #define S35390A_CMD_STATUS1	0
17 #define S35390A_CMD_STATUS2	1
18 #define S35390A_CMD_TIME1	2
19 #define S35390A_CMD_TIME2	3
20 #define S35390A_CMD_INT2_REG1	5
21 
22 #define S35390A_BYTE_YEAR	0
23 #define S35390A_BYTE_MONTH	1
24 #define S35390A_BYTE_DAY	2
25 #define S35390A_BYTE_WDAY	3
26 #define S35390A_BYTE_HOURS	4
27 #define S35390A_BYTE_MINS	5
28 #define S35390A_BYTE_SECS	6
29 
30 #define S35390A_ALRM_BYTE_WDAY	0
31 #define S35390A_ALRM_BYTE_HOURS	1
32 #define S35390A_ALRM_BYTE_MINS	2
33 
34 /* flags for STATUS1 */
35 #define S35390A_FLAG_POC	BIT(0)
36 #define S35390A_FLAG_BLD	BIT(1)
37 #define S35390A_FLAG_INT2	BIT(2)
38 #define S35390A_FLAG_24H	BIT(6)
39 #define S35390A_FLAG_RESET	BIT(7)
40 
41 /* flag for STATUS2 */
42 #define S35390A_FLAG_TEST	BIT(0)
43 
44 /* INT2 pin output mode */
45 #define S35390A_INT2_MODE_MASK		0x0E
46 #define S35390A_INT2_MODE_NOINTR	0x00
47 #define S35390A_INT2_MODE_ALARM		BIT(1) /* INT2AE */
48 #define S35390A_INT2_MODE_PMIN_EDG	BIT(2) /* INT2ME */
49 #define S35390A_INT2_MODE_FREQ		BIT(3) /* INT2FE */
50 #define S35390A_INT2_MODE_PMIN		(BIT(3) | BIT(2)) /* INT2FE | INT2ME */
51 
52 static const struct i2c_device_id s35390a_id[] = {
53 	{ "s35390a" },
54 	{ }
55 };
56 MODULE_DEVICE_TABLE(i2c, s35390a_id);
57 
58 static const __maybe_unused struct of_device_id s35390a_of_match[] = {
59 	{ .compatible = "sii,s35390a" },
60 	{ }
61 };
62 MODULE_DEVICE_TABLE(of, s35390a_of_match);
63 
64 struct s35390a {
65 	struct i2c_client *client[8];
66 	int twentyfourhour;
67 };
68 
69 static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
70 {
71 	struct i2c_client *client = s35390a->client[reg];
72 	struct i2c_msg msg[] = {
73 		{
74 			.addr = client->addr,
75 			.len = len,
76 			.buf = buf
77 		},
78 	};
79 
80 	if ((i2c_transfer(client->adapter, msg, 1)) != 1)
81 		return -EIO;
82 
83 	return 0;
84 }
85 
86 static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
87 {
88 	struct i2c_client *client = s35390a->client[reg];
89 	struct i2c_msg msg[] = {
90 		{
91 			.addr = client->addr,
92 			.flags = I2C_M_RD,
93 			.len = len,
94 			.buf = buf
95 		},
96 	};
97 
98 	if ((i2c_transfer(client->adapter, msg, 1)) != 1)
99 		return -EIO;
100 
101 	return 0;
102 }
103 
104 static int s35390a_init(struct s35390a *s35390a)
105 {
106 	u8 buf;
107 	int ret;
108 	unsigned initcount = 0;
109 
110 	/*
111 	 * At least one of POC and BLD are set, so reinitialise chip. Keeping
112 	 * this information in the hardware to know later that the time isn't
113 	 * valid is unfortunately not possible because POC and BLD are cleared
114 	 * on read. So the reset is best done now.
115 	 *
116 	 * The 24H bit is kept over reset, so set it already here.
117 	 */
118 initialize:
119 	buf = S35390A_FLAG_RESET | S35390A_FLAG_24H;
120 	ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
121 
122 	if (ret < 0)
123 		return ret;
124 
125 	ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
126 	if (ret < 0)
127 		return ret;
128 
129 	if (buf & (S35390A_FLAG_POC | S35390A_FLAG_BLD)) {
130 		/* Try up to five times to reset the chip */
131 		if (initcount < 5) {
132 			++initcount;
133 			goto initialize;
134 		} else
135 			return -EIO;
136 	}
137 
138 	return 1;
139 }
140 
141 /*
142  * Returns <0 on error, 0 if rtc is setup fine and 1 if the chip was reset.
143  * To keep the information if an irq is pending, pass the value read from
144  * STATUS1 to the caller.
145  */
146 static int s35390a_read_status(struct s35390a *s35390a, char *status1)
147 {
148 	int ret;
149 
150 	ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1);
151 	if (ret < 0)
152 		return ret;
153 
154 	if (*status1 & S35390A_FLAG_POC) {
155 		/*
156 		 * Do not communicate for 0.5 seconds since the power-on
157 		 * detection circuit is in operation.
158 		 */
159 		msleep(500);
160 		return 1;
161 	} else if (*status1 & S35390A_FLAG_BLD)
162 		return 1;
163 	/*
164 	 * If both POC and BLD are unset everything is fine.
165 	 */
166 	return 0;
167 }
168 
169 static int s35390a_disable_test_mode(struct s35390a *s35390a)
170 {
171 	char buf[1];
172 
173 	if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
174 		return -EIO;
175 
176 	if (!(buf[0] & S35390A_FLAG_TEST))
177 		return 0;
178 
179 	buf[0] &= ~S35390A_FLAG_TEST;
180 	return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
181 }
182 
183 static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
184 {
185 	if (s35390a->twentyfourhour)
186 		return bin2bcd(hour);
187 
188 	if (hour < 12)
189 		return bin2bcd(hour);
190 
191 	return 0x40 | bin2bcd(hour - 12);
192 }
193 
194 static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
195 {
196 	unsigned hour;
197 
198 	if (s35390a->twentyfourhour)
199 		return bcd2bin(reg & 0x3f);
200 
201 	hour = bcd2bin(reg & 0x3f);
202 	if (reg & 0x40)
203 		hour += 12;
204 
205 	return hour;
206 }
207 
208 static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
209 {
210 	struct i2c_client *client = to_i2c_client(dev);
211 	struct s35390a	*s35390a = i2c_get_clientdata(client);
212 	int i;
213 	char buf[7], status;
214 
215 	dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
216 		"mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
217 		tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
218 		tm->tm_wday);
219 
220 	if (s35390a_read_status(s35390a, &status) == 1)
221 		s35390a_init(s35390a);
222 
223 	buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
224 	buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
225 	buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
226 	buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
227 	buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
228 	buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
229 	buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
230 
231 	/* This chip expects the bits of each byte to be in reverse order */
232 	for (i = 0; i < 7; ++i)
233 		buf[i] = bitrev8(buf[i]);
234 
235 	return s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
236 }
237 
238 static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
239 {
240 	struct i2c_client *client = to_i2c_client(dev);
241 	struct s35390a *s35390a = i2c_get_clientdata(client);
242 	char buf[7], status;
243 	int i, err;
244 
245 	if (s35390a_read_status(s35390a, &status) == 1)
246 		return -EINVAL;
247 
248 	err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
249 	if (err < 0)
250 		return err;
251 
252 	/* This chip returns the bits of each byte in reverse order */
253 	for (i = 0; i < 7; ++i)
254 		buf[i] = bitrev8(buf[i]);
255 
256 	tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
257 	tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
258 	tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
259 	tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
260 	tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
261 	tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
262 	tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
263 
264 	dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
265 		"mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
266 		tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
267 		tm->tm_wday);
268 
269 	return 0;
270 }
271 
272 static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
273 {
274 	struct i2c_client *client = to_i2c_client(dev);
275 	struct s35390a *s35390a = i2c_get_clientdata(client);
276 	char buf[3], sts = 0;
277 	int err, i;
278 
279 	dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
280 		"mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
281 		alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
282 		alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
283 
284 	/* disable interrupt (which deasserts the irq line) */
285 	err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
286 	if (err < 0)
287 		return err;
288 
289 	/* clear pending interrupt (in STATUS1 only), if any */
290 	err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
291 	if (err < 0)
292 		return err;
293 
294 	if (alm->enabled)
295 		sts = S35390A_INT2_MODE_ALARM;
296 	else
297 		sts = S35390A_INT2_MODE_NOINTR;
298 
299 	/* set interupt mode*/
300 	err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
301 	if (err < 0)
302 		return err;
303 
304 	if (alm->time.tm_wday != -1)
305 		buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
306 	else
307 		buf[S35390A_ALRM_BYTE_WDAY] = 0;
308 
309 	buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
310 			alm->time.tm_hour) | 0x80;
311 	buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
312 
313 	if (alm->time.tm_hour >= 12)
314 		buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
315 
316 	for (i = 0; i < 3; ++i)
317 		buf[i] = bitrev8(buf[i]);
318 
319 	err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
320 								sizeof(buf));
321 
322 	return err;
323 }
324 
325 static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
326 {
327 	struct i2c_client *client = to_i2c_client(dev);
328 	struct s35390a *s35390a = i2c_get_clientdata(client);
329 	char buf[3], sts;
330 	int i, err;
331 
332 	err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
333 	if (err < 0)
334 		return err;
335 
336 	if ((sts & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
337 		/*
338 		 * When the alarm isn't enabled, the register to configure
339 		 * the alarm time isn't accessible.
340 		 */
341 		alm->enabled = 0;
342 		return 0;
343 	} else {
344 		alm->enabled = 1;
345 	}
346 
347 	err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
348 	if (err < 0)
349 		return err;
350 
351 	/* This chip returns the bits of each byte in reverse order */
352 	for (i = 0; i < 3; ++i)
353 		buf[i] = bitrev8(buf[i]);
354 
355 	/*
356 	 * B0 of the three matching registers is an enable flag. Iff it is set
357 	 * the configured value is used for matching.
358 	 */
359 	if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
360 		alm->time.tm_wday =
361 			bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
362 
363 	if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
364 		alm->time.tm_hour =
365 			s35390a_reg2hr(s35390a,
366 				       buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
367 
368 	if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
369 		alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
370 
371 	/* alarm triggers always at s=0 */
372 	alm->time.tm_sec = 0;
373 
374 	dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
375 			__func__, alm->time.tm_min, alm->time.tm_hour,
376 			alm->time.tm_wday);
377 
378 	return 0;
379 }
380 
381 static int s35390a_rtc_ioctl(struct device *dev, unsigned int cmd,
382 			     unsigned long arg)
383 {
384 	struct i2c_client *client = to_i2c_client(dev);
385 	struct s35390a *s35390a = i2c_get_clientdata(client);
386 	char sts;
387 	int err;
388 
389 	switch (cmd) {
390 	case RTC_VL_READ:
391 		/* s35390a_reset set lowvoltage flag and init RTC if needed */
392 		err = s35390a_read_status(s35390a, &sts);
393 		if (err < 0)
394 			return err;
395 		if (copy_to_user((void __user *)arg, &err, sizeof(int)))
396 			return -EFAULT;
397 		break;
398 	case RTC_VL_CLR:
399 		/* update flag and clear register */
400 		err = s35390a_init(s35390a);
401 		if (err < 0)
402 			return err;
403 		break;
404 	default:
405 		return -ENOIOCTLCMD;
406 	}
407 
408 	return 0;
409 }
410 
411 static const struct rtc_class_ops s35390a_rtc_ops = {
412 	.read_time	= s35390a_rtc_read_time,
413 	.set_time	= s35390a_rtc_set_time,
414 	.set_alarm	= s35390a_rtc_set_alarm,
415 	.read_alarm	= s35390a_rtc_read_alarm,
416 	.ioctl          = s35390a_rtc_ioctl,
417 };
418 
419 static int s35390a_probe(struct i2c_client *client)
420 {
421 	int err, err_read;
422 	unsigned int i;
423 	struct s35390a *s35390a;
424 	struct rtc_device *rtc;
425 	char buf, status1;
426 	struct device *dev = &client->dev;
427 
428 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
429 		return -ENODEV;
430 
431 	s35390a = devm_kzalloc(dev, sizeof(struct s35390a), GFP_KERNEL);
432 	if (!s35390a)
433 		return -ENOMEM;
434 
435 	s35390a->client[0] = client;
436 	i2c_set_clientdata(client, s35390a);
437 
438 	/* This chip uses multiple addresses, use dummy devices for them */
439 	for (i = 1; i < 8; ++i) {
440 		s35390a->client[i] = devm_i2c_new_dummy_device(dev,
441 							       client->adapter,
442 							       client->addr + i);
443 		if (IS_ERR(s35390a->client[i])) {
444 			dev_err(dev, "Address %02x unavailable\n",
445 				client->addr + i);
446 			return PTR_ERR(s35390a->client[i]);
447 		}
448 	}
449 
450 	rtc = devm_rtc_allocate_device(dev);
451 	if (IS_ERR(rtc))
452 		return PTR_ERR(rtc);
453 
454 	err_read = s35390a_read_status(s35390a, &status1);
455 	if (err_read < 0) {
456 		dev_err(dev, "error resetting chip\n");
457 		return err_read;
458 	}
459 
460 	if (status1 & S35390A_FLAG_24H)
461 		s35390a->twentyfourhour = 1;
462 	else
463 		s35390a->twentyfourhour = 0;
464 
465 	if (status1 & S35390A_FLAG_INT2) {
466 		/* disable alarm (and maybe test mode) */
467 		buf = 0;
468 		err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
469 		if (err < 0) {
470 			dev_err(dev, "error disabling alarm");
471 			return err;
472 		}
473 	} else {
474 		err = s35390a_disable_test_mode(s35390a);
475 		if (err < 0) {
476 			dev_err(dev, "error disabling test mode\n");
477 			return err;
478 		}
479 	}
480 
481 	device_set_wakeup_capable(dev, 1);
482 
483 	rtc->ops = &s35390a_rtc_ops;
484 	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
485 	rtc->range_max = RTC_TIMESTAMP_END_2099;
486 
487 	set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features);
488 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
489 
490 	if (status1 & S35390A_FLAG_INT2)
491 		rtc_update_irq(rtc, 1, RTC_AF);
492 
493 	return devm_rtc_register_device(rtc);
494 }
495 
496 static struct i2c_driver s35390a_driver = {
497 	.driver		= {
498 		.name	= "rtc-s35390a",
499 		.of_match_table = of_match_ptr(s35390a_of_match),
500 	},
501 	.probe		= s35390a_probe,
502 	.id_table	= s35390a_id,
503 };
504 
505 module_i2c_driver(s35390a_driver);
506 
507 MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
508 MODULE_DESCRIPTION("S35390A RTC driver");
509 MODULE_LICENSE("GPL");
510