rtc: rv8803: Remove the check for valid time
[cascardo/linux.git] / drivers / rtc / rtc-rv8803.c
1 /*
2  * RTC driver for the Micro Crystal RV8803
3  *
4  * Copyright (C) 2015 Micro Crystal SA
5  *
6  * Alexandre Belloni <alexandre.belloni@free-electrons.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/bcd.h>
15 #include <linux/bitops.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/rtc.h>
21
22 #define RV8803_SEC                      0x00
23 #define RV8803_MIN                      0x01
24 #define RV8803_HOUR                     0x02
25 #define RV8803_WEEK                     0x03
26 #define RV8803_DAY                      0x04
27 #define RV8803_MONTH                    0x05
28 #define RV8803_YEAR                     0x06
29 #define RV8803_RAM                      0x07
30 #define RV8803_ALARM_MIN                0x08
31 #define RV8803_ALARM_HOUR               0x09
32 #define RV8803_ALARM_WEEK_OR_DAY        0x0A
33 #define RV8803_EXT                      0x0D
34 #define RV8803_FLAG                     0x0E
35 #define RV8803_CTRL                     0x0F
36
37 #define RV8803_EXT_WADA                 BIT(6)
38
39 #define RV8803_FLAG_V1F                 BIT(0)
40 #define RV8803_FLAG_V2F                 BIT(1)
41 #define RV8803_FLAG_AF                  BIT(3)
42 #define RV8803_FLAG_TF                  BIT(4)
43 #define RV8803_FLAG_UF                  BIT(5)
44
45 #define RV8803_CTRL_RESET               BIT(0)
46
47 #define RV8803_CTRL_EIE                 BIT(2)
48 #define RV8803_CTRL_AIE                 BIT(3)
49 #define RV8803_CTRL_TIE                 BIT(4)
50 #define RV8803_CTRL_UIE                 BIT(5)
51
52 struct rv8803_data {
53         struct i2c_client *client;
54         struct rtc_device *rtc;
55         struct mutex flags_lock;
56         u8 ctrl;
57 };
58
59 static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
60 {
61         struct i2c_client *client = dev_id;
62         struct rv8803_data *rv8803 = i2c_get_clientdata(client);
63         unsigned long events = 0;
64         int flags, try = 0;
65
66         mutex_lock(&rv8803->flags_lock);
67
68         do {
69                 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
70                 try++;
71         } while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4));
72         if (flags <= 0) {
73                 mutex_unlock(&rv8803->flags_lock);
74                 return IRQ_NONE;
75         }
76
77         if (flags & RV8803_FLAG_V1F)
78                 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
79
80         if (flags & RV8803_FLAG_V2F)
81                 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
82
83         if (flags & RV8803_FLAG_TF) {
84                 flags &= ~RV8803_FLAG_TF;
85                 rv8803->ctrl &= ~RV8803_CTRL_TIE;
86                 events |= RTC_PF;
87         }
88
89         if (flags & RV8803_FLAG_AF) {
90                 flags &= ~RV8803_FLAG_AF;
91                 rv8803->ctrl &= ~RV8803_CTRL_AIE;
92                 events |= RTC_AF;
93         }
94
95         if (flags & RV8803_FLAG_UF) {
96                 flags &= ~RV8803_FLAG_UF;
97                 rv8803->ctrl &= ~RV8803_CTRL_UIE;
98                 events |= RTC_UF;
99         }
100
101         if (events) {
102                 rtc_update_irq(rv8803->rtc, 1, events);
103                 i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
104                 i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
105                                           rv8803->ctrl);
106         }
107
108         mutex_unlock(&rv8803->flags_lock);
109
110         return IRQ_HANDLED;
111 }
112
113 static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
114 {
115         struct rv8803_data *rv8803 = dev_get_drvdata(dev);
116         u8 date1[7];
117         u8 date2[7];
118         u8 *date = date1;
119         int ret, flags;
120
121         flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
122         if (flags < 0)
123                 return flags;
124
125         if (flags & RV8803_FLAG_V2F) {
126                 dev_warn(dev, "Voltage low, data is invalid.\n");
127                 return -EINVAL;
128         }
129
130         ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
131                                             7, date);
132         if (ret != 7)
133                 return ret < 0 ? ret : -EIO;
134
135         if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
136                 ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
137                                                     7, date2);
138                 if (ret != 7)
139                         return ret < 0 ? ret : -EIO;
140
141                 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
142                         date = date2;
143         }
144
145         tm->tm_sec  = bcd2bin(date[RV8803_SEC] & 0x7f);
146         tm->tm_min  = bcd2bin(date[RV8803_MIN] & 0x7f);
147         tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
148         tm->tm_wday = ffs(date[RV8803_WEEK] & 0x7f);
149         tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
150         tm->tm_mon  = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
151         tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
152
153         return 0;
154 }
155
156 static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
157 {
158         struct rv8803_data *rv8803 = dev_get_drvdata(dev);
159         u8 date[7];
160         int flags, ret;
161
162         if ((tm->tm_year < 100) || (tm->tm_year > 199))
163                 return -EINVAL;
164
165         date[RV8803_SEC]   = bin2bcd(tm->tm_sec);
166         date[RV8803_MIN]   = bin2bcd(tm->tm_min);
167         date[RV8803_HOUR]  = bin2bcd(tm->tm_hour);
168         date[RV8803_WEEK]  = 1 << (tm->tm_wday);
169         date[RV8803_DAY]   = bin2bcd(tm->tm_mday);
170         date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
171         date[RV8803_YEAR]  = bin2bcd(tm->tm_year - 100);
172
173         ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC,
174                                              7, date);
175         if (ret < 0)
176                 return ret;
177
178         mutex_lock(&rv8803->flags_lock);
179
180         flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
181         if (flags < 0) {
182                 mutex_unlock(&rv8803->flags_lock);
183                 return flags;
184         }
185
186         ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG,
187                                         flags & ~RV8803_FLAG_V2F);
188
189         mutex_unlock(&rv8803->flags_lock);
190
191         return ret;
192 }
193
194 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195 {
196         struct rv8803_data *rv8803 = dev_get_drvdata(dev);
197         struct i2c_client *client = rv8803->client;
198         u8 alarmvals[3];
199         int flags, ret;
200
201         ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN,
202                                             3, alarmvals);
203         if (ret != 3)
204                 return ret < 0 ? ret : -EIO;
205
206         flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
207         if (flags < 0)
208                 return flags;
209
210         alrm->time.tm_sec  = 0;
211         alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
212         alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
213         alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
214
215         alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
216         alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
217
218         return 0;
219 }
220
221 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
222 {
223         struct i2c_client *client = to_i2c_client(dev);
224         struct rv8803_data *rv8803 = dev_get_drvdata(dev);
225         u8 alarmvals[3];
226         u8 ctrl[2];
227         int ret, err;
228
229         /* The alarm has no seconds, round up to nearest minute */
230         if (alrm->time.tm_sec) {
231                 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
232
233                 alarm_time += 60 - alrm->time.tm_sec;
234                 rtc_time64_to_tm(alarm_time, &alrm->time);
235         }
236
237         mutex_lock(&rv8803->flags_lock);
238
239         ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl);
240         if (ret != 2) {
241                 mutex_unlock(&rv8803->flags_lock);
242                 return ret < 0 ? ret : -EIO;
243         }
244
245         alarmvals[0] = bin2bcd(alrm->time.tm_min);
246         alarmvals[1] = bin2bcd(alrm->time.tm_hour);
247         alarmvals[2] = bin2bcd(alrm->time.tm_mday);
248
249         if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
250                 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
251                 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
252                                                 rv8803->ctrl);
253                 if (err) {
254                         mutex_unlock(&rv8803->flags_lock);
255                         return err;
256                 }
257         }
258
259         ctrl[1] &= ~RV8803_FLAG_AF;
260         err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]);
261         mutex_unlock(&rv8803->flags_lock);
262         if (err)
263                 return err;
264
265         err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN,
266                                              3, alarmvals);
267         if (err)
268                 return err;
269
270         if (alrm->enabled) {
271                 if (rv8803->rtc->uie_rtctimer.enabled)
272                         rv8803->ctrl |= RV8803_CTRL_UIE;
273                 if (rv8803->rtc->aie_timer.enabled)
274                         rv8803->ctrl |= RV8803_CTRL_AIE;
275
276                 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
277                                                 rv8803->ctrl);
278                 if (err)
279                         return err;
280         }
281
282         return 0;
283 }
284
285 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
286 {
287         struct i2c_client *client = to_i2c_client(dev);
288         struct rv8803_data *rv8803 = dev_get_drvdata(dev);
289         int ctrl, flags, err;
290
291         ctrl = rv8803->ctrl;
292
293         if (enabled) {
294                 if (rv8803->rtc->uie_rtctimer.enabled)
295                         ctrl |= RV8803_CTRL_UIE;
296                 if (rv8803->rtc->aie_timer.enabled)
297                         ctrl |= RV8803_CTRL_AIE;
298         } else {
299                 if (!rv8803->rtc->uie_rtctimer.enabled)
300                         ctrl &= ~RV8803_CTRL_UIE;
301                 if (!rv8803->rtc->aie_timer.enabled)
302                         ctrl &= ~RV8803_CTRL_AIE;
303         }
304
305         mutex_lock(&rv8803->flags_lock);
306         flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
307         if (flags < 0) {
308                 mutex_unlock(&rv8803->flags_lock);
309                 return flags;
310         }
311         flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
312         err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
313         mutex_unlock(&rv8803->flags_lock);
314         if (err)
315                 return err;
316
317         if (ctrl != rv8803->ctrl) {
318                 rv8803->ctrl = ctrl;
319                 err = i2c_smbus_write_byte_data(client, RV8803_CTRL,
320                                                 rv8803->ctrl);
321                 if (err)
322                         return err;
323         }
324
325         return 0;
326 }
327
328 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
329 {
330         struct i2c_client *client = to_i2c_client(dev);
331         struct rv8803_data *rv8803 = dev_get_drvdata(dev);
332         int flags, ret = 0;
333
334         switch (cmd) {
335         case RTC_VL_READ:
336                 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
337                 if (flags < 0)
338                         return flags;
339
340                 if (flags & RV8803_FLAG_V1F)
341                         dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
342
343                 if (flags & RV8803_FLAG_V2F)
344                         dev_warn(&client->dev, "Voltage low, data loss detected.\n");
345
346                 flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
347
348                 if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
349                         return -EFAULT;
350
351                 return 0;
352
353         case RTC_VL_CLR:
354                 mutex_lock(&rv8803->flags_lock);
355                 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
356                 if (flags < 0) {
357                         mutex_unlock(&rv8803->flags_lock);
358                         return flags;
359                 }
360
361                 flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
362                 ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
363                 mutex_unlock(&rv8803->flags_lock);
364                 if (ret < 0)
365                         return ret;
366
367                 return 0;
368
369         default:
370                 return -ENOIOCTLCMD;
371         }
372 }
373
374 static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj,
375                                   struct bin_attribute *attr,
376                                   char *buf, loff_t off, size_t count)
377 {
378         struct device *dev = kobj_to_dev(kobj);
379         struct i2c_client *client = to_i2c_client(dev);
380         int ret;
381
382         ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]);
383         if (ret < 0)
384                 return ret;
385
386         return 1;
387 }
388
389 static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj,
390                                  struct bin_attribute *attr,
391                                  char *buf, loff_t off, size_t count)
392 {
393         struct device *dev = kobj_to_dev(kobj);
394         struct i2c_client *client = to_i2c_client(dev);
395         int ret;
396
397         ret = i2c_smbus_read_byte_data(client, RV8803_RAM);
398         if (ret < 0)
399                 return ret;
400
401         buf[0] = ret;
402
403         return 1;
404 }
405
406 static struct bin_attribute rv8803_nvram_attr = {
407         .attr = {
408                 .name = "nvram",
409                 .mode = S_IRUGO | S_IWUSR,
410         },
411         .size = 1,
412         .read = rv8803_nvram_read,
413         .write = rv8803_nvram_write,
414 };
415
416 static struct rtc_class_ops rv8803_rtc_ops = {
417         .read_time = rv8803_get_time,
418         .set_time = rv8803_set_time,
419         .ioctl = rv8803_ioctl,
420 };
421
422 static int rv8803_probe(struct i2c_client *client,
423                         const struct i2c_device_id *id)
424 {
425         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
426         struct rv8803_data *rv8803;
427         int err, flags, try = 0;
428
429         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
430                                      I2C_FUNC_SMBUS_I2C_BLOCK)) {
431                 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
432                 return -EIO;
433         }
434
435         rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
436                               GFP_KERNEL);
437         if (!rv8803)
438                 return -ENOMEM;
439
440         mutex_init(&rv8803->flags_lock);
441         rv8803->client = client;
442         i2c_set_clientdata(client, rv8803);
443
444         /*
445          * There is a 60µs window where the RTC may not reply on the i2c bus in
446          * that case, the transfer is not ACKed. In that case, ensure there are
447          * multiple attempts.
448          */
449         do {
450                 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
451                 try++;
452         } while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4));
453
454         if (flags < 0)
455                 return flags;
456
457         if (flags & RV8803_FLAG_V1F)
458                 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
459
460         if (flags & RV8803_FLAG_V2F)
461                 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
462
463         if (flags & RV8803_FLAG_AF)
464                 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
465
466         if (client->irq > 0) {
467                 err = devm_request_threaded_irq(&client->dev, client->irq,
468                                                 NULL, rv8803_handle_irq,
469                                                 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
470                                                 "rv8803", client);
471                 if (err) {
472                         dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
473                         client->irq = 0;
474                 } else {
475                         rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
476                         rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
477                         rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
478                 }
479         }
480
481         rv8803->rtc = devm_rtc_device_register(&client->dev, client->name,
482                                                &rv8803_rtc_ops, THIS_MODULE);
483         if (IS_ERR(rv8803->rtc)) {
484                 dev_err(&client->dev, "unable to register the class device\n");
485                 return PTR_ERR(rv8803->rtc);
486         }
487
488         try = 0;
489         do {
490                 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT,
491                                                 RV8803_EXT_WADA);
492                 try++;
493         } while (((err == -ENXIO) || (flags == -EIO)) && (try < 4));
494         if (err)
495                 return err;
496
497         err = device_create_bin_file(&client->dev, &rv8803_nvram_attr);
498         if (err)
499                 return err;
500
501         rv8803->rtc->max_user_freq = 1;
502
503         return 0;
504 }
505
506 static int rv8803_remove(struct i2c_client *client)
507 {
508         device_remove_bin_file(&client->dev, &rv8803_nvram_attr);
509
510         return 0;
511 }
512
513 static const struct i2c_device_id rv8803_id[] = {
514         { "rv8803", 0 },
515         { "rx8900", 0 },
516         { }
517 };
518 MODULE_DEVICE_TABLE(i2c, rv8803_id);
519
520 static struct i2c_driver rv8803_driver = {
521         .driver = {
522                 .name = "rtc-rv8803",
523         },
524         .probe          = rv8803_probe,
525         .remove         = rv8803_remove,
526         .id_table       = rv8803_id,
527 };
528 module_i2c_driver(rv8803_driver);
529
530 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
531 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
532 MODULE_LICENSE("GPL v2");