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