Merge branch 'vhca-tunnel' into rdma.git for-next
[sfrench/cifs-2.6.git] / drivers / rtc / rtc-s35390a.c
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        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
40
41 /* flag for STATUS2 */
42 #define S35390A_FLAG_TEST       0x01
43
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
51 static const struct i2c_device_id s35390a_id[] = {
52         { "s35390a", 0 },
53         { }
54 };
55 MODULE_DEVICE_TABLE(i2c, s35390a_id);
56
57 static const struct of_device_id s35390a_of_match[] = {
58         { .compatible = "s35390a" },
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         struct rtc_device *rtc;
67         int twentyfourhour;
68 };
69
70 static 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[] = {
74                 {
75                         .addr = client->addr,
76                         .len = len,
77                         .buf = buf
78                 },
79         };
80
81         if ((i2c_transfer(client->adapter, msg, 1)) != 1)
82                 return -EIO;
83
84         return 0;
85 }
86
87 static 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[] = {
91                 {
92                         .addr = client->addr,
93                         .flags = I2C_M_RD,
94                         .len = len,
95                         .buf = buf
96                 },
97         };
98
99         if ((i2c_transfer(client->adapter, msg, 1)) != 1)
100                 return -EIO;
101
102         return 0;
103 }
104
105 static int s35390a_init(struct s35390a *s35390a)
106 {
107         u8 buf;
108         int ret;
109         unsigned initcount = 0;
110
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          */
119 initialize:
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;
140 }
141
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  */
147 static 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
170 static 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
184 static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
185 {
186         if (s35390a->twentyfourhour)
187                 return bin2bcd(hour);
188
189         if (hour < 12)
190                 return bin2bcd(hour);
191
192         return 0x40 | bin2bcd(hour - 12);
193 }
194
195 static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
196 {
197         unsigned hour;
198
199         if (s35390a->twentyfourhour)
200                 return bcd2bin(reg & 0x3f);
201
202         hour = bcd2bin(reg & 0x3f);
203         if (reg & 0x40)
204                 hour += 12;
205
206         return hour;
207 }
208
209 static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
210 {
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;
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
221         if (s35390a_read_status(s35390a, &status) == 1)
222                 s35390a_init(s35390a);
223
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);
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
241 static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
242 {
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;
247
248         if (s35390a_read_status(s35390a, &status) == 1)
249                 return -EINVAL;
250
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
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;
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
272         return 0;
273 }
274
275 static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
276 {
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;
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
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;
291
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;
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;
312         else
313                 buf[S35390A_ALRM_BYTE_WDAY] = 0;
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
331 static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
332 {
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;
337
338         err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
339         if (err < 0)
340                 return err;
341
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         }
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 */
358         for (i = 0; i < 3; ++i)
359                 buf[i] = bitrev8(buf[i]);
360
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;
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
387 static 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
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,
423 };
424
425 static struct i2c_driver s35390a_driver;
426
427 static int s35390a_probe(struct i2c_client *client,
428                          const struct i2c_device_id *id)
429 {
430         int err, err_read;
431         unsigned int i;
432         struct s35390a *s35390a;
433         char buf, status1;
434
435         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
436                 err = -ENODEV;
437                 goto exit;
438         }
439
440         s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
441                                 GFP_KERNEL);
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,
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;
459                 }
460         }
461
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;
467         }
468
469         if (status1 & S35390A_FLAG_24H)
470                 s35390a->twentyfourhour = 1;
471         else
472                 s35390a->twentyfourhour = 0;
473
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
490         device_set_wakeup_capable(&client->dev, 1);
491
492         s35390a->rtc = devm_rtc_device_register(&client->dev,
493                                         s35390a_driver.driver.name,
494                                         &s35390a_rtc_ops, THIS_MODULE);
495
496         if (IS_ERR(s35390a->rtc)) {
497                 err = PTR_ERR(s35390a->rtc);
498                 goto exit_dummy;
499         }
500
501         if (status1 & S35390A_FLAG_INT2)
502                 rtc_update_irq(s35390a->rtc, 1, RTC_AF);
503
504         return 0;
505
506 exit_dummy:
507         for (i = 1; i < 8; ++i)
508                 if (s35390a->client[i])
509                         i2c_unregister_device(s35390a->client[i]);
510
511 exit:
512         return err;
513 }
514
515 static int s35390a_remove(struct i2c_client *client)
516 {
517         unsigned int i;
518         struct s35390a *s35390a = i2c_get_clientdata(client);
519
520         for (i = 1; i < 8; ++i)
521                 if (s35390a->client[i])
522                         i2c_unregister_device(s35390a->client[i]);
523
524         return 0;
525 }
526
527 static struct i2c_driver s35390a_driver = {
528         .driver         = {
529                 .name   = "rtc-s35390a",
530                 .of_match_table = of_match_ptr(s35390a_of_match),
531         },
532         .probe          = s35390a_probe,
533         .remove         = s35390a_remove,
534         .id_table       = s35390a_id,
535 };
536
537 module_i2c_driver(s35390a_driver);
538
539 MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
540 MODULE_DESCRIPTION("S35390A RTC driver");
541 MODULE_LICENSE("GPL");