]> Git Repo - linux.git/blame - drivers/rtc/rtc-s35390a.c
drm/nouveau: fix bogus GPL-2 license header
[linux.git] / drivers / rtc / rtc-s35390a.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
c46288b0
BB
2/*
3 * Seiko Instruments S-35390A RTC Driver
4 *
5 * Copyright (c) 2007 Byron Bradley
c46288b0
BB
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>
8e6583f1 14#include <linux/delay.h>
c46288b0
BB
15
16#define S35390A_CMD_STATUS1 0
17#define S35390A_CMD_STATUS2 1
18#define S35390A_CMD_TIME1 2
542dd33a
ML
19#define S35390A_CMD_TIME2 3
20#define S35390A_CMD_INT2_REG1 5
c46288b0
BB
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
542dd33a
ML
30#define S35390A_ALRM_BYTE_WDAY 0
31#define S35390A_ALRM_BYTE_HOURS 1
32#define S35390A_ALRM_BYTE_MINS 2
33
3bd32722 34/* flags for STATUS1 */
c46288b0
BB
35#define S35390A_FLAG_POC 0x01
36#define S35390A_FLAG_BLD 0x02
3bd32722 37#define S35390A_FLAG_INT2 0x04
c46288b0
BB
38#define S35390A_FLAG_24H 0x40
39#define S35390A_FLAG_RESET 0x80
3bd32722
UKK
40
41/* flag for STATUS2 */
c46288b0
BB
42#define S35390A_FLAG_TEST 0x01
43
542dd33a
ML
44#define S35390A_INT2_MODE_MASK 0xF0
45
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
50
3760f736
JD
51static const struct i2c_device_id s35390a_id[] = {
52 { "s35390a", 0 },
53 { }
54};
55MODULE_DEVICE_TABLE(i2c, s35390a_id);
56
21f27432
JMC
57static const struct of_device_id s35390a_of_match[] = {
58 { .compatible = "s35390a" },
59 { .compatible = "sii,s35390a" },
60 { }
61};
62MODULE_DEVICE_TABLE(of, s35390a_of_match);
63
c46288b0
BB
64struct s35390a {
65 struct i2c_client *client[8];
66 struct rtc_device *rtc;
67 int twentyfourhour;
68};
69
70static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
71{
72 struct i2c_client *client = s35390a->client[reg];
73 struct i2c_msg msg[] = {
65659f63
S
74 {
75 .addr = client->addr,
76 .len = len,
77 .buf = buf
78 },
c46288b0
BB
79 };
80
81 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
82 return -EIO;
83
84 return 0;
85}
86
87static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
88{
89 struct i2c_client *client = s35390a->client[reg];
90 struct i2c_msg msg[] = {
65659f63
S
91 {
92 .addr = client->addr,
93 .flags = I2C_M_RD,
94 .len = len,
95 .buf = buf
96 },
c46288b0
BB
97 };
98
99 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
100 return -EIO;
101
102 return 0;
103}
104
16486d0c 105static int s35390a_init(struct s35390a *s35390a)
c46288b0 106{
ef0f02fd 107 u8 buf;
8e6583f1
UKK
108 int ret;
109 unsigned initcount = 0;
c46288b0 110
8e6583f1
UKK
111 /*
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.
116 *
117 * The 24H bit is kept over reset, so set it already here.
118 */
119initialize:
8e6583f1
UKK
120 buf = S35390A_FLAG_RESET | S35390A_FLAG_24H;
121 ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
122
123 if (ret < 0)
124 return ret;
125
126 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
127 if (ret < 0)
128 return ret;
129
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;
137 }
138
139 return 1;
c46288b0
BB
140}
141
16486d0c
FL
142/*
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.
146 */
147static int s35390a_read_status(struct s35390a *s35390a, char *status1)
148{
149 int ret;
150
151 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1);
152 if (ret < 0)
153 return ret;
154
155 if (*status1 & S35390A_FLAG_POC) {
156 /*
157 * Do not communicate for 0.5 seconds since the power-on
158 * detection circuit is in operation.
159 */
160 msleep(500);
161 return 1;
162 } else if (*status1 & S35390A_FLAG_BLD)
163 return 1;
164 /*
165 * If both POC and BLD are unset everything is fine.
166 */
167 return 0;
168}
169
c46288b0
BB
170static int s35390a_disable_test_mode(struct s35390a *s35390a)
171{
172 char buf[1];
173
174 if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
175 return -EIO;
176
177 if (!(buf[0] & S35390A_FLAG_TEST))
178 return 0;
179
180 buf[0] &= ~S35390A_FLAG_TEST;
181 return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
182}
183
184static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
185{
186 if (s35390a->twentyfourhour)
fe20ba70 187 return bin2bcd(hour);
c46288b0
BB
188
189 if (hour < 12)
fe20ba70 190 return bin2bcd(hour);
c46288b0 191
fe20ba70 192 return 0x40 | bin2bcd(hour - 12);
c46288b0
BB
193}
194
195static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
196{
197 unsigned hour;
198
199 if (s35390a->twentyfourhour)
fe20ba70 200 return bcd2bin(reg & 0x3f);
c46288b0 201
fe20ba70 202 hour = bcd2bin(reg & 0x3f);
c46288b0
BB
203 if (reg & 0x40)
204 hour += 12;
205
206 return hour;
207}
208
779e1aab 209static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
c46288b0 210{
779e1aab 211 struct i2c_client *client = to_i2c_client(dev);
c46288b0
BB
212 struct s35390a *s35390a = i2c_get_clientdata(client);
213 int i, err;
16486d0c 214 char buf[7], status;
c46288b0
BB
215
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);
220
16486d0c
FL
221 if (s35390a_read_status(s35390a, &status) == 1)
222 s35390a_init(s35390a);
223
fe20ba70
AB
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);
c46288b0 228 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
fe20ba70
AB
229 buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
230 buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
c46288b0
BB
231
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]);
235
236 err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
237
238 return err;
239}
240
779e1aab 241static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
c46288b0 242{
779e1aab 243 struct i2c_client *client = to_i2c_client(dev);
c46288b0 244 struct s35390a *s35390a = i2c_get_clientdata(client);
16486d0c 245 char buf[7], status;
c46288b0
BB
246 int i, err;
247
16486d0c
FL
248 if (s35390a_read_status(s35390a, &status) == 1)
249 return -EINVAL;
250
c46288b0
BB
251 err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
252 if (err < 0)
253 return err;
254
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]);
258
fe20ba70
AB
259 tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
260 tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
c46288b0 261 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
fe20ba70
AB
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;
c46288b0
BB
266
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);
271
bb530199 272 return 0;
c46288b0
BB
273}
274
779e1aab 275static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
542dd33a 276{
779e1aab 277 struct i2c_client *client = to_i2c_client(dev);
542dd33a
ML
278 struct s35390a *s35390a = i2c_get_clientdata(client);
279 char buf[3], sts = 0;
280 int err, i;
281
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);
286
5227e8a2 287 /* disable interrupt (which deasserts the irq line) */
542dd33a
ML
288 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
289 if (err < 0)
290 return err;
291
5227e8a2 292 /* clear pending interrupt (in STATUS1 only), if any */
542dd33a
ML
293 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
294 if (err < 0)
295 return err;
296
297 if (alm->enabled)
298 sts = S35390A_INT2_MODE_ALARM;
299 else
300 sts = S35390A_INT2_MODE_NOINTR;
301
302 /* This chip expects the bits of each byte to be in reverse order */
303 sts = bitrev8(sts);
304
305 /* set interupt mode*/
306 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
307 if (err < 0)
308 return err;
309
310 if (alm->time.tm_wday != -1)
311 buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
f87e904d
UKK
312 else
313 buf[S35390A_ALRM_BYTE_WDAY] = 0;
542dd33a
ML
314
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;
318
319 if (alm->time.tm_hour >= 12)
320 buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
321
322 for (i = 0; i < 3; ++i)
323 buf[i] = bitrev8(buf[i]);
324
325 err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
326 sizeof(buf));
327
328 return err;
329}
330
779e1aab 331static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
542dd33a 332{
779e1aab 333 struct i2c_client *client = to_i2c_client(dev);
542dd33a
ML
334 struct s35390a *s35390a = i2c_get_clientdata(client);
335 char buf[3], sts;
336 int i, err;
337
338 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
339 if (err < 0)
340 return err;
341
f87e904d
UKK
342 if ((bitrev8(sts) & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
343 /*
344 * When the alarm isn't enabled, the register to configure
345 * the alarm time isn't accessible.
346 */
347 alm->enabled = 0;
348 return 0;
349 } else {
350 alm->enabled = 1;
351 }
542dd33a
ML
352
353 err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
354 if (err < 0)
355 return err;
356
357 /* This chip returns the bits of each byte in reverse order */
f87e904d 358 for (i = 0; i < 3; ++i)
542dd33a 359 buf[i] = bitrev8(buf[i]);
542dd33a 360
f87e904d
UKK
361 /*
362 * B0 of the three matching registers is an enable flag. Iff it is set
363 * the configured value is used for matching.
364 */
365 if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
366 alm->time.tm_wday =
367 bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
368
369 if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
370 alm->time.tm_hour =
371 s35390a_reg2hr(s35390a,
372 buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
373
374 if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
375 alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
376
377 /* alarm triggers always at s=0 */
378 alm->time.tm_sec = 0;
542dd33a
ML
379
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);
383
384 return 0;
385}
386
7a1fe407
FL
387static int s35390a_rtc_ioctl(struct device *dev, unsigned int cmd,
388 unsigned long arg)
389{
390 struct i2c_client *client = to_i2c_client(dev);
391 struct s35390a *s35390a = i2c_get_clientdata(client);
392 char sts;
393 int err;
394
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;
412 }
413
414 return 0;
415}
416
c46288b0
BB
417static const struct rtc_class_ops s35390a_rtc_ops = {
418 .read_time = s35390a_rtc_read_time,
419 .set_time = s35390a_rtc_set_time,
542dd33a
ML
420 .set_alarm = s35390a_rtc_set_alarm,
421 .read_alarm = s35390a_rtc_read_alarm,
7a1fe407 422 .ioctl = s35390a_rtc_ioctl,
c46288b0
BB
423};
424
425static struct i2c_driver s35390a_driver;
426
d2653e92
JD
427static int s35390a_probe(struct i2c_client *client,
428 const struct i2c_device_id *id)
c46288b0 429{
16486d0c 430 int err, err_read;
c46288b0
BB
431 unsigned int i;
432 struct s35390a *s35390a;
3bd32722 433 char buf, status1;
c46288b0
BB
434
435 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
436 err = -ENODEV;
437 goto exit;
438 }
439
b4cd3d6a
JH
440 s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
441 GFP_KERNEL);
c46288b0
BB
442 if (!s35390a) {
443 err = -ENOMEM;
444 goto exit;
445 }
446
447 s35390a->client[0] = client;
448 i2c_set_clientdata(client, s35390a);
449
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,
60b129d7 453 client->addr + i);
c46288b0
BB
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;
459 }
460 }
461
16486d0c
FL
462 err_read = s35390a_read_status(s35390a, &status1);
463 if (err_read < 0) {
464 err = err_read;
c46288b0
BB
465 dev_err(&client->dev, "error resetting chip\n");
466 goto exit_dummy;
467 }
468
3bd32722 469 if (status1 & S35390A_FLAG_24H)
c46288b0
BB
470 s35390a->twentyfourhour = 1;
471 else
472 s35390a->twentyfourhour = 0;
473
3bd32722
UKK
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;
481 }
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;
487 }
488 }
489
542dd33a
ML
490 device_set_wakeup_capable(&client->dev, 1);
491
b4cd3d6a
JH
492 s35390a->rtc = devm_rtc_device_register(&client->dev,
493 s35390a_driver.driver.name,
494 &s35390a_rtc_ops, THIS_MODULE);
c46288b0
BB
495
496 if (IS_ERR(s35390a->rtc)) {
497 err = PTR_ERR(s35390a->rtc);
498 goto exit_dummy;
499 }
3bd32722
UKK
500
501 if (status1 & S35390A_FLAG_INT2)
502 rtc_update_irq(s35390a->rtc, 1, RTC_AF);
503
c46288b0
BB
504 return 0;
505
506exit_dummy:
507 for (i = 1; i < 8; ++i)
508 if (s35390a->client[i])
509 i2c_unregister_device(s35390a->client[i]);
c46288b0
BB
510
511exit:
512 return err;
513}
514
515static int s35390a_remove(struct i2c_client *client)
516{
517 unsigned int i;
c46288b0 518 struct s35390a *s35390a = i2c_get_clientdata(client);
b4cd3d6a 519
c46288b0
BB
520 for (i = 1; i < 8; ++i)
521 if (s35390a->client[i])
522 i2c_unregister_device(s35390a->client[i]);
523
c46288b0
BB
524 return 0;
525}
526
527static struct i2c_driver s35390a_driver = {
528 .driver = {
529 .name = "rtc-s35390a",
21f27432 530 .of_match_table = of_match_ptr(s35390a_of_match),
c46288b0
BB
531 },
532 .probe = s35390a_probe,
533 .remove = s35390a_remove,
3760f736 534 .id_table = s35390a_id,
c46288b0
BB
535};
536
0abc9201 537module_i2c_driver(s35390a_driver);
c46288b0
BB
538
539MODULE_AUTHOR("Byron Bradley <[email protected]>");
540MODULE_DESCRIPTION("S35390A RTC driver");
541MODULE_LICENSE("GPL");
This page took 0.996205 seconds and 4 git commands to generate.