blob: b18ce5610a9467707506fdab35dfb7b472eaee66 [file] [log] [blame]
Byron Bradleyc46288b2008-03-04 14:28:25 -08001/*
2 * Seiko Instruments S-35390A RTC Driver
3 *
4 * Copyright (c) 2007 Byron Bradley
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/rtc.h>
14#include <linux/i2c.h>
15#include <linux/bitrev.h>
16#include <linux/bcd.h>
17#include <linux/slab.h>
Uwe Kleine-König8e6583f2016-07-02 17:28:09 +020018#include <linux/delay.h>
Byron Bradleyc46288b2008-03-04 14:28:25 -080019
20#define S35390A_CMD_STATUS1 0
21#define S35390A_CMD_STATUS2 1
22#define S35390A_CMD_TIME1 2
Michael Langer542dd332012-10-04 17:14:37 -070023#define S35390A_CMD_TIME2 3
24#define S35390A_CMD_INT2_REG1 5
Byron Bradleyc46288b2008-03-04 14:28:25 -080025
26#define S35390A_BYTE_YEAR 0
27#define S35390A_BYTE_MONTH 1
28#define S35390A_BYTE_DAY 2
29#define S35390A_BYTE_WDAY 3
30#define S35390A_BYTE_HOURS 4
31#define S35390A_BYTE_MINS 5
32#define S35390A_BYTE_SECS 6
33
Michael Langer542dd332012-10-04 17:14:37 -070034#define S35390A_ALRM_BYTE_WDAY 0
35#define S35390A_ALRM_BYTE_HOURS 1
36#define S35390A_ALRM_BYTE_MINS 2
37
Uwe Kleine-König3bd32722016-07-02 17:28:10 +020038/* flags for STATUS1 */
Richard Leitner097aa242019-05-23 13:54:51 +020039#define S35390A_FLAG_POC BIT(0)
40#define S35390A_FLAG_BLD BIT(1)
41#define S35390A_FLAG_INT2 BIT(2)
42#define S35390A_FLAG_24H BIT(6)
43#define S35390A_FLAG_RESET BIT(7)
Uwe Kleine-König3bd32722016-07-02 17:28:10 +020044
45/* flag for STATUS2 */
Richard Leitner097aa242019-05-23 13:54:51 +020046#define S35390A_FLAG_TEST BIT(0)
Byron Bradleyc46288b2008-03-04 14:28:25 -080047
Richard Leitnera86bd902019-05-23 13:54:48 +020048/* INT2 pin output mode */
49#define S35390A_INT2_MODE_MASK 0x0E
Michael Langer542dd332012-10-04 17:14:37 -070050#define S35390A_INT2_MODE_NOINTR 0x00
Richard Leitnera86bd902019-05-23 13:54:48 +020051#define S35390A_INT2_MODE_ALARM BIT(1) /* INT2AE */
52#define S35390A_INT2_MODE_PMIN_EDG BIT(2) /* INT2ME */
53#define S35390A_INT2_MODE_FREQ BIT(3) /* INT2FE */
54#define S35390A_INT2_MODE_PMIN (BIT(3) | BIT(2)) /* INT2FE | INT2ME */
Michael Langer542dd332012-10-04 17:14:37 -070055
Jean Delvare3760f732008-04-29 23:11:40 +020056static const struct i2c_device_id s35390a_id[] = {
57 { "s35390a", 0 },
58 { }
59};
60MODULE_DEVICE_TABLE(i2c, s35390a_id);
61
Javier Martinez Canillas21f27432017-03-03 11:29:21 -030062static const struct of_device_id s35390a_of_match[] = {
63 { .compatible = "s35390a" },
64 { .compatible = "sii,s35390a" },
65 { }
66};
67MODULE_DEVICE_TABLE(of, s35390a_of_match);
68
Byron Bradleyc46288b2008-03-04 14:28:25 -080069struct s35390a {
70 struct i2c_client *client[8];
71 struct rtc_device *rtc;
72 int twentyfourhour;
73};
74
75static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
76{
77 struct i2c_client *client = s35390a->client[reg];
78 struct i2c_msg msg[] = {
Shubhrajyoti D65659f62012-10-04 17:14:21 -070079 {
80 .addr = client->addr,
81 .len = len,
82 .buf = buf
83 },
Byron Bradleyc46288b2008-03-04 14:28:25 -080084 };
85
86 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
87 return -EIO;
88
89 return 0;
90}
91
92static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
93{
94 struct i2c_client *client = s35390a->client[reg];
95 struct i2c_msg msg[] = {
Shubhrajyoti D65659f62012-10-04 17:14:21 -070096 {
97 .addr = client->addr,
98 .flags = I2C_M_RD,
99 .len = len,
100 .buf = buf
101 },
Byron Bradleyc46288b2008-03-04 14:28:25 -0800102 };
103
104 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
105 return -EIO;
106
107 return 0;
108}
109
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200110static int s35390a_init(struct s35390a *s35390a)
Byron Bradleyc46288b2008-03-04 14:28:25 -0800111{
Nathan Chancelloref0f02f2018-10-19 13:43:45 -0700112 u8 buf;
Uwe Kleine-König8e6583f2016-07-02 17:28:09 +0200113 int ret;
114 unsigned initcount = 0;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800115
Uwe Kleine-König8e6583f2016-07-02 17:28:09 +0200116 /*
117 * At least one of POC and BLD are set, so reinitialise chip. Keeping
118 * this information in the hardware to know later that the time isn't
119 * valid is unfortunately not possible because POC and BLD are cleared
120 * on read. So the reset is best done now.
121 *
122 * The 24H bit is kept over reset, so set it already here.
123 */
124initialize:
Uwe Kleine-König8e6583f2016-07-02 17:28:09 +0200125 buf = S35390A_FLAG_RESET | S35390A_FLAG_24H;
126 ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
127
128 if (ret < 0)
129 return ret;
130
131 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
132 if (ret < 0)
133 return ret;
134
135 if (buf & (S35390A_FLAG_POC | S35390A_FLAG_BLD)) {
136 /* Try up to five times to reset the chip */
137 if (initcount < 5) {
138 ++initcount;
139 goto initialize;
140 } else
141 return -EIO;
142 }
143
144 return 1;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800145}
146
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200147/*
148 * Returns <0 on error, 0 if rtc is setup fine and 1 if the chip was reset.
149 * To keep the information if an irq is pending, pass the value read from
150 * STATUS1 to the caller.
151 */
152static int s35390a_read_status(struct s35390a *s35390a, char *status1)
153{
154 int ret;
155
156 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1);
157 if (ret < 0)
158 return ret;
159
160 if (*status1 & S35390A_FLAG_POC) {
161 /*
162 * Do not communicate for 0.5 seconds since the power-on
163 * detection circuit is in operation.
164 */
165 msleep(500);
166 return 1;
167 } else if (*status1 & S35390A_FLAG_BLD)
168 return 1;
169 /*
170 * If both POC and BLD are unset everything is fine.
171 */
172 return 0;
173}
174
Byron Bradleyc46288b2008-03-04 14:28:25 -0800175static int s35390a_disable_test_mode(struct s35390a *s35390a)
176{
177 char buf[1];
178
179 if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
180 return -EIO;
181
182 if (!(buf[0] & S35390A_FLAG_TEST))
183 return 0;
184
185 buf[0] &= ~S35390A_FLAG_TEST;
186 return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
187}
188
189static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
190{
191 if (s35390a->twentyfourhour)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700192 return bin2bcd(hour);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800193
194 if (hour < 12)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700195 return bin2bcd(hour);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800196
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700197 return 0x40 | bin2bcd(hour - 12);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800198}
199
200static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
201{
202 unsigned hour;
203
204 if (s35390a->twentyfourhour)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700205 return bcd2bin(reg & 0x3f);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800206
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700207 hour = bcd2bin(reg & 0x3f);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800208 if (reg & 0x40)
209 hour += 12;
210
211 return hour;
212}
213
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100214static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
Byron Bradleyc46288b2008-03-04 14:28:25 -0800215{
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100216 struct i2c_client *client = to_i2c_client(dev);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800217 struct s35390a *s35390a = i2c_get_clientdata(client);
218 int i, err;
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200219 char buf[7], status;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800220
221 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
222 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
223 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
224 tm->tm_wday);
225
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200226 if (s35390a_read_status(s35390a, &status) == 1)
227 s35390a_init(s35390a);
228
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700229 buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
230 buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
231 buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
232 buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800233 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700234 buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
235 buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800236
237 /* This chip expects the bits of each byte to be in reverse order */
238 for (i = 0; i < 7; ++i)
239 buf[i] = bitrev8(buf[i]);
240
241 err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
242
243 return err;
244}
245
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100246static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
Byron Bradleyc46288b2008-03-04 14:28:25 -0800247{
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100248 struct i2c_client *client = to_i2c_client(dev);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800249 struct s35390a *s35390a = i2c_get_clientdata(client);
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200250 char buf[7], status;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800251 int i, err;
252
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200253 if (s35390a_read_status(s35390a, &status) == 1)
254 return -EINVAL;
255
Byron Bradleyc46288b2008-03-04 14:28:25 -0800256 err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
257 if (err < 0)
258 return err;
259
260 /* This chip returns the bits of each byte in reverse order */
261 for (i = 0; i < 7; ++i)
262 buf[i] = bitrev8(buf[i]);
263
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700264 tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
265 tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800266 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700267 tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
268 tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
269 tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
270 tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800271
272 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
273 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
274 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
275 tm->tm_wday);
276
Alexandre Bellonibb530192018-02-21 13:54:46 +0100277 return 0;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800278}
279
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100280static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
Michael Langer542dd332012-10-04 17:14:37 -0700281{
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100282 struct i2c_client *client = to_i2c_client(dev);
Michael Langer542dd332012-10-04 17:14:37 -0700283 struct s35390a *s35390a = i2c_get_clientdata(client);
284 char buf[3], sts = 0;
285 int err, i;
286
287 dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
288 "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
289 alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
290 alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
291
Richard Leitnerc0e12842019-05-23 13:54:49 +0200292 if (alm->time.tm_sec != 0)
293 dev_warn(&client->dev, "Alarms are only supported on a per minute basis!\n");
294
Uwe Kleine-König5227e8a2016-07-02 17:28:11 +0200295 /* disable interrupt (which deasserts the irq line) */
Michael Langer542dd332012-10-04 17:14:37 -0700296 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
297 if (err < 0)
298 return err;
299
Uwe Kleine-König5227e8a2016-07-02 17:28:11 +0200300 /* clear pending interrupt (in STATUS1 only), if any */
Michael Langer542dd332012-10-04 17:14:37 -0700301 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
302 if (err < 0)
303 return err;
304
305 if (alm->enabled)
306 sts = S35390A_INT2_MODE_ALARM;
307 else
308 sts = S35390A_INT2_MODE_NOINTR;
309
Michael Langer542dd332012-10-04 17:14:37 -0700310 /* set interupt mode*/
311 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
312 if (err < 0)
313 return err;
314
315 if (alm->time.tm_wday != -1)
316 buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
Uwe Kleine-Königf87e9042016-07-02 17:28:08 +0200317 else
318 buf[S35390A_ALRM_BYTE_WDAY] = 0;
Michael Langer542dd332012-10-04 17:14:37 -0700319
320 buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
321 alm->time.tm_hour) | 0x80;
322 buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
323
324 if (alm->time.tm_hour >= 12)
325 buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
326
327 for (i = 0; i < 3; ++i)
328 buf[i] = bitrev8(buf[i]);
329
330 err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
331 sizeof(buf));
332
333 return err;
334}
335
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100336static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
Michael Langer542dd332012-10-04 17:14:37 -0700337{
Alexandre Belloni779e1aa2018-02-21 14:00:18 +0100338 struct i2c_client *client = to_i2c_client(dev);
Michael Langer542dd332012-10-04 17:14:37 -0700339 struct s35390a *s35390a = i2c_get_clientdata(client);
340 char buf[3], sts;
341 int i, err;
342
343 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
344 if (err < 0)
345 return err;
346
Richard Leitnera86bd902019-05-23 13:54:48 +0200347 if ((sts & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
Uwe Kleine-Königf87e9042016-07-02 17:28:08 +0200348 /*
349 * When the alarm isn't enabled, the register to configure
350 * the alarm time isn't accessible.
351 */
352 alm->enabled = 0;
353 return 0;
354 } else {
355 alm->enabled = 1;
356 }
Michael Langer542dd332012-10-04 17:14:37 -0700357
358 err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
359 if (err < 0)
360 return err;
361
362 /* This chip returns the bits of each byte in reverse order */
Uwe Kleine-Königf87e9042016-07-02 17:28:08 +0200363 for (i = 0; i < 3; ++i)
Michael Langer542dd332012-10-04 17:14:37 -0700364 buf[i] = bitrev8(buf[i]);
Michael Langer542dd332012-10-04 17:14:37 -0700365
Uwe Kleine-Königf87e9042016-07-02 17:28:08 +0200366 /*
367 * B0 of the three matching registers is an enable flag. Iff it is set
368 * the configured value is used for matching.
369 */
370 if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
371 alm->time.tm_wday =
372 bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
373
374 if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
375 alm->time.tm_hour =
376 s35390a_reg2hr(s35390a,
377 buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
378
379 if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
380 alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
381
382 /* alarm triggers always at s=0 */
383 alm->time.tm_sec = 0;
Michael Langer542dd332012-10-04 17:14:37 -0700384
385 dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
386 __func__, alm->time.tm_min, alm->time.tm_hour,
387 alm->time.tm_wday);
388
389 return 0;
390}
391
Fabien Lahoudere7a1fe402017-07-05 10:02:30 +0200392static int s35390a_rtc_ioctl(struct device *dev, unsigned int cmd,
393 unsigned long arg)
394{
395 struct i2c_client *client = to_i2c_client(dev);
396 struct s35390a *s35390a = i2c_get_clientdata(client);
397 char sts;
398 int err;
399
400 switch (cmd) {
401 case RTC_VL_READ:
402 /* s35390a_reset set lowvoltage flag and init RTC if needed */
403 err = s35390a_read_status(s35390a, &sts);
404 if (err < 0)
405 return err;
406 if (copy_to_user((void __user *)arg, &err, sizeof(int)))
407 return -EFAULT;
408 break;
409 case RTC_VL_CLR:
410 /* update flag and clear register */
411 err = s35390a_init(s35390a);
412 if (err < 0)
413 return err;
414 break;
415 default:
416 return -ENOIOCTLCMD;
417 }
418
419 return 0;
420}
421
Byron Bradleyc46288b2008-03-04 14:28:25 -0800422static const struct rtc_class_ops s35390a_rtc_ops = {
423 .read_time = s35390a_rtc_read_time,
424 .set_time = s35390a_rtc_set_time,
Michael Langer542dd332012-10-04 17:14:37 -0700425 .set_alarm = s35390a_rtc_set_alarm,
426 .read_alarm = s35390a_rtc_read_alarm,
Fabien Lahoudere7a1fe402017-07-05 10:02:30 +0200427 .ioctl = s35390a_rtc_ioctl,
Byron Bradleyc46288b2008-03-04 14:28:25 -0800428};
429
430static struct i2c_driver s35390a_driver;
431
Jean Delvared2653e92008-04-29 23:11:39 +0200432static int s35390a_probe(struct i2c_client *client,
433 const struct i2c_device_id *id)
Byron Bradleyc46288b2008-03-04 14:28:25 -0800434{
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200435 int err, err_read;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800436 unsigned int i;
437 struct s35390a *s35390a;
Uwe Kleine-König3bd32722016-07-02 17:28:10 +0200438 char buf, status1;
Richard Leitner03279632019-05-23 13:54:50 +0200439 struct device *dev = &client->dev;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800440
441 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
442 err = -ENODEV;
443 goto exit;
444 }
445
Richard Leitner03279632019-05-23 13:54:50 +0200446 s35390a = devm_kzalloc(dev, sizeof(struct s35390a), GFP_KERNEL);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800447 if (!s35390a) {
448 err = -ENOMEM;
449 goto exit;
450 }
451
452 s35390a->client[0] = client;
453 i2c_set_clientdata(client, s35390a);
454
455 /* This chip uses multiple addresses, use dummy devices for them */
456 for (i = 1; i < 8; ++i) {
457 s35390a->client[i] = i2c_new_dummy(client->adapter,
Jean Delvare60b129d2008-05-11 20:37:06 +0200458 client->addr + i);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800459 if (!s35390a->client[i]) {
Richard Leitner03279632019-05-23 13:54:50 +0200460 dev_err(dev, "Address %02x unavailable\n",
461 client->addr + i);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800462 err = -EBUSY;
463 goto exit_dummy;
464 }
465 }
466
Fabien Lahoudere16486d02017-07-05 10:02:29 +0200467 err_read = s35390a_read_status(s35390a, &status1);
468 if (err_read < 0) {
469 err = err_read;
Richard Leitner03279632019-05-23 13:54:50 +0200470 dev_err(dev, "error resetting chip\n");
Byron Bradleyc46288b2008-03-04 14:28:25 -0800471 goto exit_dummy;
472 }
473
Uwe Kleine-König3bd32722016-07-02 17:28:10 +0200474 if (status1 & S35390A_FLAG_24H)
Byron Bradleyc46288b2008-03-04 14:28:25 -0800475 s35390a->twentyfourhour = 1;
476 else
477 s35390a->twentyfourhour = 0;
478
Uwe Kleine-König3bd32722016-07-02 17:28:10 +0200479 if (status1 & S35390A_FLAG_INT2) {
480 /* disable alarm (and maybe test mode) */
481 buf = 0;
482 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
483 if (err < 0) {
Richard Leitner03279632019-05-23 13:54:50 +0200484 dev_err(dev, "error disabling alarm");
Uwe Kleine-König3bd32722016-07-02 17:28:10 +0200485 goto exit_dummy;
486 }
487 } else {
488 err = s35390a_disable_test_mode(s35390a);
489 if (err < 0) {
Richard Leitner03279632019-05-23 13:54:50 +0200490 dev_err(dev, "error disabling test mode\n");
Uwe Kleine-König3bd32722016-07-02 17:28:10 +0200491 goto exit_dummy;
492 }
493 }
494
Richard Leitner03279632019-05-23 13:54:50 +0200495 device_set_wakeup_capable(dev, 1);
Michael Langer542dd332012-10-04 17:14:37 -0700496
Richard Leitner03279632019-05-23 13:54:50 +0200497 s35390a->rtc = devm_rtc_device_register(dev, s35390a_driver.driver.name,
498 &s35390a_rtc_ops, THIS_MODULE);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800499
500 if (IS_ERR(s35390a->rtc)) {
501 err = PTR_ERR(s35390a->rtc);
502 goto exit_dummy;
503 }
Uwe Kleine-König3bd32722016-07-02 17:28:10 +0200504
Richard Leitnerc0e12842019-05-23 13:54:49 +0200505 /* supports per-minute alarms only, therefore set uie_unsupported */
506 s35390a->rtc->uie_unsupported = 1;
507
Uwe Kleine-König3bd32722016-07-02 17:28:10 +0200508 if (status1 & S35390A_FLAG_INT2)
509 rtc_update_irq(s35390a->rtc, 1, RTC_AF);
510
Byron Bradleyc46288b2008-03-04 14:28:25 -0800511 return 0;
512
513exit_dummy:
514 for (i = 1; i < 8; ++i)
515 if (s35390a->client[i])
516 i2c_unregister_device(s35390a->client[i]);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800517
518exit:
519 return err;
520}
521
522static int s35390a_remove(struct i2c_client *client)
523{
524 unsigned int i;
Byron Bradleyc46288b2008-03-04 14:28:25 -0800525 struct s35390a *s35390a = i2c_get_clientdata(client);
Jingoo Hanb4cd3d62013-04-29 16:20:53 -0700526
Byron Bradleyc46288b2008-03-04 14:28:25 -0800527 for (i = 1; i < 8; ++i)
528 if (s35390a->client[i])
529 i2c_unregister_device(s35390a->client[i]);
530
Byron Bradleyc46288b2008-03-04 14:28:25 -0800531 return 0;
532}
533
534static struct i2c_driver s35390a_driver = {
535 .driver = {
536 .name = "rtc-s35390a",
Javier Martinez Canillas21f27432017-03-03 11:29:21 -0300537 .of_match_table = of_match_ptr(s35390a_of_match),
Byron Bradleyc46288b2008-03-04 14:28:25 -0800538 },
539 .probe = s35390a_probe,
540 .remove = s35390a_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200541 .id_table = s35390a_id,
Byron Bradleyc46288b2008-03-04 14:28:25 -0800542};
543
Axel Lin0abc9202012-03-23 15:02:31 -0700544module_i2c_driver(s35390a_driver);
Byron Bradleyc46288b2008-03-04 14:28:25 -0800545
546MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
547MODULE_DESCRIPTION("S35390A RTC driver");
548MODULE_LICENSE("GPL");