Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / rtc / rtc-s35390a.c
blob8c37acb4a0078f3b26ce3f80e6c984c0fcd2e1da
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Seiko Instruments S-35390A RTC Driver
5 * Copyright (c) 2007 Byron Bradley
6 */
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>
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
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
30 #define S35390A_ALRM_BYTE_WDAY 0
31 #define S35390A_ALRM_BYTE_HOURS 1
32 #define S35390A_ALRM_BYTE_MINS 2
34 /* flags for STATUS1 */
35 #define S35390A_FLAG_POC 0x01
36 #define S35390A_FLAG_BLD 0x02
37 #define S35390A_FLAG_INT2 0x04
38 #define S35390A_FLAG_24H 0x40
39 #define S35390A_FLAG_RESET 0x80
41 /* flag for STATUS2 */
42 #define S35390A_FLAG_TEST 0x01
44 #define S35390A_INT2_MODE_MASK 0xF0
46 #define S35390A_INT2_MODE_NOINTR 0x00
47 #define S35390A_INT2_MODE_FREQ 0x10
48 #define S35390A_INT2_MODE_ALARM 0x40
49 #define S35390A_INT2_MODE_PMIN_EDG 0x20
51 static const struct i2c_device_id s35390a_id[] = {
52 { "s35390a", 0 },
53 { }
55 MODULE_DEVICE_TABLE(i2c, s35390a_id);
57 static const struct of_device_id s35390a_of_match[] = {
58 { .compatible = "s35390a" },
59 { .compatible = "sii,s35390a" },
60 { }
62 MODULE_DEVICE_TABLE(of, s35390a_of_match);
64 struct s35390a {
65 struct i2c_client *client[8];
66 struct rtc_device *rtc;
67 int twentyfourhour;
70 static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
72 struct i2c_client *client = s35390a->client[reg];
73 struct i2c_msg msg[] = {
75 .addr = client->addr,
76 .len = len,
77 .buf = buf
81 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
82 return -EIO;
84 return 0;
87 static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
89 struct i2c_client *client = s35390a->client[reg];
90 struct i2c_msg msg[] = {
92 .addr = client->addr,
93 .flags = I2C_M_RD,
94 .len = len,
95 .buf = buf
99 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
100 return -EIO;
102 return 0;
105 static int s35390a_init(struct s35390a *s35390a)
107 u8 buf;
108 int ret;
109 unsigned initcount = 0;
112 * At least one of POC and BLD are set, so reinitialise chip. Keeping
113 * this information in the hardware to know later that the time isn't
114 * valid is unfortunately not possible because POC and BLD are cleared
115 * on read. So the reset is best done now.
117 * The 24H bit is kept over reset, so set it already here.
119 initialize:
120 buf = S35390A_FLAG_RESET | S35390A_FLAG_24H;
121 ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
123 if (ret < 0)
124 return ret;
126 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
127 if (ret < 0)
128 return ret;
130 if (buf & (S35390A_FLAG_POC | S35390A_FLAG_BLD)) {
131 /* Try up to five times to reset the chip */
132 if (initcount < 5) {
133 ++initcount;
134 goto initialize;
135 } else
136 return -EIO;
139 return 1;
143 * Returns <0 on error, 0 if rtc is setup fine and 1 if the chip was reset.
144 * To keep the information if an irq is pending, pass the value read from
145 * STATUS1 to the caller.
147 static int s35390a_read_status(struct s35390a *s35390a, char *status1)
149 int ret;
151 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1);
152 if (ret < 0)
153 return ret;
155 if (*status1 & S35390A_FLAG_POC) {
157 * Do not communicate for 0.5 seconds since the power-on
158 * detection circuit is in operation.
160 msleep(500);
161 return 1;
162 } else if (*status1 & S35390A_FLAG_BLD)
163 return 1;
165 * If both POC and BLD are unset everything is fine.
167 return 0;
170 static int s35390a_disable_test_mode(struct s35390a *s35390a)
172 char buf[1];
174 if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
175 return -EIO;
177 if (!(buf[0] & S35390A_FLAG_TEST))
178 return 0;
180 buf[0] &= ~S35390A_FLAG_TEST;
181 return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
184 static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
186 if (s35390a->twentyfourhour)
187 return bin2bcd(hour);
189 if (hour < 12)
190 return bin2bcd(hour);
192 return 0x40 | bin2bcd(hour - 12);
195 static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
197 unsigned hour;
199 if (s35390a->twentyfourhour)
200 return bcd2bin(reg & 0x3f);
202 hour = bcd2bin(reg & 0x3f);
203 if (reg & 0x40)
204 hour += 12;
206 return hour;
209 static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
211 struct i2c_client *client = to_i2c_client(dev);
212 struct s35390a *s35390a = i2c_get_clientdata(client);
213 int i, err;
214 char buf[7], status;
216 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
217 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
218 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
219 tm->tm_wday);
221 if (s35390a_read_status(s35390a, &status) == 1)
222 s35390a_init(s35390a);
224 buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
225 buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
226 buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
227 buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
228 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
229 buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
230 buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
232 /* This chip expects the bits of each byte to be in reverse order */
233 for (i = 0; i < 7; ++i)
234 buf[i] = bitrev8(buf[i]);
236 err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
238 return err;
241 static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
243 struct i2c_client *client = to_i2c_client(dev);
244 struct s35390a *s35390a = i2c_get_clientdata(client);
245 char buf[7], status;
246 int i, err;
248 if (s35390a_read_status(s35390a, &status) == 1)
249 return -EINVAL;
251 err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
252 if (err < 0)
253 return err;
255 /* This chip returns the bits of each byte in reverse order */
256 for (i = 0; i < 7; ++i)
257 buf[i] = bitrev8(buf[i]);
259 tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
260 tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
261 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
262 tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
263 tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
264 tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
265 tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
267 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
268 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
269 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
270 tm->tm_wday);
272 return 0;
275 static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
277 struct i2c_client *client = to_i2c_client(dev);
278 struct s35390a *s35390a = i2c_get_clientdata(client);
279 char buf[3], sts = 0;
280 int err, i;
282 dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
283 "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
284 alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
285 alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
287 /* disable interrupt (which deasserts the irq line) */
288 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
289 if (err < 0)
290 return err;
292 /* clear pending interrupt (in STATUS1 only), if any */
293 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
294 if (err < 0)
295 return err;
297 if (alm->enabled)
298 sts = S35390A_INT2_MODE_ALARM;
299 else
300 sts = S35390A_INT2_MODE_NOINTR;
302 /* This chip expects the bits of each byte to be in reverse order */
303 sts = bitrev8(sts);
305 /* set interupt mode*/
306 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
307 if (err < 0)
308 return err;
310 if (alm->time.tm_wday != -1)
311 buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
312 else
313 buf[S35390A_ALRM_BYTE_WDAY] = 0;
315 buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
316 alm->time.tm_hour) | 0x80;
317 buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
319 if (alm->time.tm_hour >= 12)
320 buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
322 for (i = 0; i < 3; ++i)
323 buf[i] = bitrev8(buf[i]);
325 err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
326 sizeof(buf));
328 return err;
331 static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
333 struct i2c_client *client = to_i2c_client(dev);
334 struct s35390a *s35390a = i2c_get_clientdata(client);
335 char buf[3], sts;
336 int i, err;
338 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
339 if (err < 0)
340 return err;
342 if ((bitrev8(sts) & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
344 * When the alarm isn't enabled, the register to configure
345 * the alarm time isn't accessible.
347 alm->enabled = 0;
348 return 0;
349 } else {
350 alm->enabled = 1;
353 err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
354 if (err < 0)
355 return err;
357 /* This chip returns the bits of each byte in reverse order */
358 for (i = 0; i < 3; ++i)
359 buf[i] = bitrev8(buf[i]);
362 * B0 of the three matching registers is an enable flag. Iff it is set
363 * the configured value is used for matching.
365 if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
366 alm->time.tm_wday =
367 bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
369 if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
370 alm->time.tm_hour =
371 s35390a_reg2hr(s35390a,
372 buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
374 if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
375 alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
377 /* alarm triggers always at s=0 */
378 alm->time.tm_sec = 0;
380 dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
381 __func__, alm->time.tm_min, alm->time.tm_hour,
382 alm->time.tm_wday);
384 return 0;
387 static int s35390a_rtc_ioctl(struct device *dev, unsigned int cmd,
388 unsigned long arg)
390 struct i2c_client *client = to_i2c_client(dev);
391 struct s35390a *s35390a = i2c_get_clientdata(client);
392 char sts;
393 int err;
395 switch (cmd) {
396 case RTC_VL_READ:
397 /* s35390a_reset set lowvoltage flag and init RTC if needed */
398 err = s35390a_read_status(s35390a, &sts);
399 if (err < 0)
400 return err;
401 if (copy_to_user((void __user *)arg, &err, sizeof(int)))
402 return -EFAULT;
403 break;
404 case RTC_VL_CLR:
405 /* update flag and clear register */
406 err = s35390a_init(s35390a);
407 if (err < 0)
408 return err;
409 break;
410 default:
411 return -ENOIOCTLCMD;
414 return 0;
417 static const struct rtc_class_ops s35390a_rtc_ops = {
418 .read_time = s35390a_rtc_read_time,
419 .set_time = s35390a_rtc_set_time,
420 .set_alarm = s35390a_rtc_set_alarm,
421 .read_alarm = s35390a_rtc_read_alarm,
422 .ioctl = s35390a_rtc_ioctl,
425 static struct i2c_driver s35390a_driver;
427 static int s35390a_probe(struct i2c_client *client,
428 const struct i2c_device_id *id)
430 int err, err_read;
431 unsigned int i;
432 struct s35390a *s35390a;
433 char buf, status1;
435 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
436 err = -ENODEV;
437 goto exit;
440 s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
441 GFP_KERNEL);
442 if (!s35390a) {
443 err = -ENOMEM;
444 goto exit;
447 s35390a->client[0] = client;
448 i2c_set_clientdata(client, s35390a);
450 /* This chip uses multiple addresses, use dummy devices for them */
451 for (i = 1; i < 8; ++i) {
452 s35390a->client[i] = i2c_new_dummy(client->adapter,
453 client->addr + i);
454 if (!s35390a->client[i]) {
455 dev_err(&client->dev, "Address %02x unavailable\n",
456 client->addr + i);
457 err = -EBUSY;
458 goto exit_dummy;
462 err_read = s35390a_read_status(s35390a, &status1);
463 if (err_read < 0) {
464 err = err_read;
465 dev_err(&client->dev, "error resetting chip\n");
466 goto exit_dummy;
469 if (status1 & S35390A_FLAG_24H)
470 s35390a->twentyfourhour = 1;
471 else
472 s35390a->twentyfourhour = 0;
474 if (status1 & S35390A_FLAG_INT2) {
475 /* disable alarm (and maybe test mode) */
476 buf = 0;
477 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
478 if (err < 0) {
479 dev_err(&client->dev, "error disabling alarm");
480 goto exit_dummy;
482 } else {
483 err = s35390a_disable_test_mode(s35390a);
484 if (err < 0) {
485 dev_err(&client->dev, "error disabling test mode\n");
486 goto exit_dummy;
490 device_set_wakeup_capable(&client->dev, 1);
492 s35390a->rtc = devm_rtc_device_register(&client->dev,
493 s35390a_driver.driver.name,
494 &s35390a_rtc_ops, THIS_MODULE);
496 if (IS_ERR(s35390a->rtc)) {
497 err = PTR_ERR(s35390a->rtc);
498 goto exit_dummy;
501 if (status1 & S35390A_FLAG_INT2)
502 rtc_update_irq(s35390a->rtc, 1, RTC_AF);
504 return 0;
506 exit_dummy:
507 for (i = 1; i < 8; ++i)
508 if (s35390a->client[i])
509 i2c_unregister_device(s35390a->client[i]);
511 exit:
512 return err;
515 static int s35390a_remove(struct i2c_client *client)
517 unsigned int i;
518 struct s35390a *s35390a = i2c_get_clientdata(client);
520 for (i = 1; i < 8; ++i)
521 if (s35390a->client[i])
522 i2c_unregister_device(s35390a->client[i]);
524 return 0;
527 static struct i2c_driver s35390a_driver = {
528 .driver = {
529 .name = "rtc-s35390a",
530 .of_match_table = of_match_ptr(s35390a_of_match),
532 .probe = s35390a_probe,
533 .remove = s35390a_remove,
534 .id_table = s35390a_id,
537 module_i2c_driver(s35390a_driver);
539 MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
540 MODULE_DESCRIPTION("S35390A RTC driver");
541 MODULE_LICENSE("GPL");