1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/interrupt.h>
26#include <linux/i2c.h>
27#include <linux/rtc.h>
28#include <linux/bcd.h>
29#include <linux/workqueue.h>
30#include <linux/slab.h>
31#include <linux/pm.h>
32#ifdef CONFIG_RTC_DRV_DS1374_WDT
33#include <linux/fs.h>
34#include <linux/ioctl.h>
35#include <linux/miscdevice.h>
36#include <linux/reboot.h>
37#include <linux/watchdog.h>
38#endif
39
40#define DS1374_REG_TOD0 0x00
41#define DS1374_REG_TOD1 0x01
42#define DS1374_REG_TOD2 0x02
43#define DS1374_REG_TOD3 0x03
44#define DS1374_REG_WDALM0 0x04
45#define DS1374_REG_WDALM1 0x05
46#define DS1374_REG_WDALM2 0x06
47#define DS1374_REG_CR 0x07
48#define DS1374_REG_CR_AIE 0x01
49#define DS1374_REG_CR_WDALM 0x20
50#define DS1374_REG_CR_WACE 0x40
51#define DS1374_REG_SR 0x08
52#define DS1374_REG_SR_OSF 0x80
53#define DS1374_REG_SR_AF 0x01
54#define DS1374_REG_TCR 0x09
55
56static const struct i2c_device_id ds1374_id[] = {
57 { "ds1374", 0 },
58 { }
59};
60MODULE_DEVICE_TABLE(i2c, ds1374_id);
61
62#ifdef CONFIG_OF
63static const struct of_device_id ds1374_of_match[] = {
64 { .compatible = "dallas,ds1374" },
65 { }
66};
67MODULE_DEVICE_TABLE(of, ds1374_of_match);
68#endif
69
70struct ds1374 {
71 struct i2c_client *client;
72 struct rtc_device *rtc;
73 struct work_struct work;
74
75
76
77
78
79 struct mutex mutex;
80 int exiting;
81};
82
83static struct i2c_driver ds1374_driver;
84
85static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
86 int reg, int nbytes)
87{
88 u8 buf[4];
89 int ret;
90 int i;
91
92 if (WARN_ON(nbytes > 4))
93 return -EINVAL;
94
95 ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
96
97 if (ret < 0)
98 return ret;
99 if (ret < nbytes)
100 return -EIO;
101
102 for (i = nbytes - 1, *time = 0; i >= 0; i--)
103 *time = (*time << 8) | buf[i];
104
105 return 0;
106}
107
108static int ds1374_write_rtc(struct i2c_client *client, u32 time,
109 int reg, int nbytes)
110{
111 u8 buf[4];
112 int i;
113
114 if (nbytes > 4) {
115 WARN_ON(1);
116 return -EINVAL;
117 }
118
119 for (i = 0; i < nbytes; i++) {
120 buf[i] = time & 0xff;
121 time >>= 8;
122 }
123
124 return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
125}
126
127static int ds1374_check_rtc_status(struct i2c_client *client)
128{
129 int ret = 0;
130 int control, stat;
131
132 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
133 if (stat < 0)
134 return stat;
135
136 if (stat & DS1374_REG_SR_OSF)
137 dev_warn(&client->dev,
138 "oscillator discontinuity flagged, time unreliable\n");
139
140 stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
141
142 ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
143 if (ret < 0)
144 return ret;
145
146
147
148
149
150
151 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
152 if (control < 0)
153 return control;
154
155 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
156 return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
157}
158
159static int ds1374_read_time(struct device *dev, struct rtc_time *time)
160{
161 struct i2c_client *client = to_i2c_client(dev);
162 u32 itime;
163 int ret;
164
165 ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
166 if (!ret)
167 rtc_time64_to_tm(itime, time);
168
169 return ret;
170}
171
172static int ds1374_set_time(struct device *dev, struct rtc_time *time)
173{
174 struct i2c_client *client = to_i2c_client(dev);
175 unsigned long itime = rtc_tm_to_time64(time);
176
177 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
178}
179
180#ifndef CONFIG_RTC_DRV_DS1374_WDT
181
182
183
184
185static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
186{
187 struct i2c_client *client = to_i2c_client(dev);
188 struct ds1374 *ds1374 = i2c_get_clientdata(client);
189 u32 now, cur_alarm;
190 int cr, sr;
191 int ret = 0;
192
193 if (client->irq <= 0)
194 return -EINVAL;
195
196 mutex_lock(&ds1374->mutex);
197
198 cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
199 if (ret < 0)
200 goto out;
201
202 sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
203 if (ret < 0)
204 goto out;
205
206 ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
207 if (ret)
208 goto out;
209
210 ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
211 if (ret)
212 goto out;
213
214 rtc_time64_to_tm(now + cur_alarm, &alarm->time);
215 alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
216 alarm->pending = !!(sr & DS1374_REG_SR_AF);
217
218out:
219 mutex_unlock(&ds1374->mutex);
220 return ret;
221}
222
223static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
224{
225 struct i2c_client *client = to_i2c_client(dev);
226 struct ds1374 *ds1374 = i2c_get_clientdata(client);
227 struct rtc_time now;
228 unsigned long new_alarm, itime;
229 int cr;
230 int ret = 0;
231
232 if (client->irq <= 0)
233 return -EINVAL;
234
235 ret = ds1374_read_time(dev, &now);
236 if (ret < 0)
237 return ret;
238
239 new_alarm = rtc_tm_to_time64(&alarm->time);
240 itime = rtc_tm_to_time64(&now);
241
242
243
244
245
246
247
248 if (time_before_eq(new_alarm, itime))
249 new_alarm = 1;
250 else
251 new_alarm -= itime;
252
253 mutex_lock(&ds1374->mutex);
254
255 ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
256 if (ret < 0)
257 goto out;
258
259
260
261 cr &= ~DS1374_REG_CR_WACE;
262
263 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
264 if (ret < 0)
265 goto out;
266
267 ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
268 if (ret)
269 goto out;
270
271 if (alarm->enabled) {
272 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
273 cr &= ~DS1374_REG_CR_WDALM;
274
275 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
276 }
277
278out:
279 mutex_unlock(&ds1374->mutex);
280 return ret;
281}
282#endif
283
284static irqreturn_t ds1374_irq(int irq, void *dev_id)
285{
286 struct i2c_client *client = dev_id;
287 struct ds1374 *ds1374 = i2c_get_clientdata(client);
288
289 disable_irq_nosync(irq);
290 schedule_work(&ds1374->work);
291 return IRQ_HANDLED;
292}
293
294static void ds1374_work(struct work_struct *work)
295{
296 struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
297 struct i2c_client *client = ds1374->client;
298 int stat, control;
299
300 mutex_lock(&ds1374->mutex);
301
302 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
303 if (stat < 0)
304 goto unlock;
305
306 if (stat & DS1374_REG_SR_AF) {
307 stat &= ~DS1374_REG_SR_AF;
308 i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
309
310 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
311 if (control < 0)
312 goto out;
313
314 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
315 i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
316
317 rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
318 }
319
320out:
321 if (!ds1374->exiting)
322 enable_irq(client->irq);
323unlock:
324 mutex_unlock(&ds1374->mutex);
325}
326
327#ifndef CONFIG_RTC_DRV_DS1374_WDT
328static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
329{
330 struct i2c_client *client = to_i2c_client(dev);
331 struct ds1374 *ds1374 = i2c_get_clientdata(client);
332 int ret;
333
334 mutex_lock(&ds1374->mutex);
335
336 ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
337 if (ret < 0)
338 goto out;
339
340 if (enabled) {
341 ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
342 ret &= ~DS1374_REG_CR_WDALM;
343 } else {
344 ret &= ~DS1374_REG_CR_WACE;
345 }
346 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
347
348out:
349 mutex_unlock(&ds1374->mutex);
350 return ret;
351}
352#endif
353
354static const struct rtc_class_ops ds1374_rtc_ops = {
355 .read_time = ds1374_read_time,
356 .set_time = ds1374_set_time,
357#ifndef CONFIG_RTC_DRV_DS1374_WDT
358 .read_alarm = ds1374_read_alarm,
359 .set_alarm = ds1374_set_alarm,
360 .alarm_irq_enable = ds1374_alarm_irq_enable,
361#endif
362};
363
364#ifdef CONFIG_RTC_DRV_DS1374_WDT
365
366
367
368
369
370
371
372static struct i2c_client *save_client;
373
374#define WD_TIMO 131762
375
376#define DRV_NAME "DS1374 Watchdog"
377
378static int wdt_margin = WD_TIMO;
379static unsigned long wdt_is_open;
380module_param(wdt_margin, int, 0);
381MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
382
383static const struct watchdog_info ds1374_wdt_info = {
384 .identity = "DS1374 WTD",
385 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
386 WDIOF_MAGICCLOSE,
387};
388
389static int ds1374_wdt_settimeout(unsigned int timeout)
390{
391 int ret = -ENOIOCTLCMD;
392 int cr;
393
394 ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
395 if (ret < 0)
396 goto out;
397
398
399 cr &= ~DS1374_REG_CR_WACE;
400
401 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
402 if (ret < 0)
403 goto out;
404
405
406 ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3);
407 if (ret) {
408 pr_info("couldn't set new watchdog time\n");
409 goto out;
410 }
411
412
413 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
414 cr &= ~DS1374_REG_CR_AIE;
415
416 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
417 if (ret < 0)
418 goto out;
419
420 return 0;
421out:
422 return ret;
423}
424
425
426
427
428
429static void ds1374_wdt_ping(void)
430{
431 u32 val;
432 int ret = 0;
433
434 ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3);
435 if (ret)
436 pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret);
437}
438
439static void ds1374_wdt_disable(void)
440{
441 int cr;
442
443 cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
444
445 cr &= ~DS1374_REG_CR_WACE;
446
447 i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
448}
449
450
451
452
453static int ds1374_wdt_open(struct inode *inode, struct file *file)
454{
455 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
456
457 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
458 mutex_lock(&ds1374->mutex);
459 if (test_and_set_bit(0, &wdt_is_open)) {
460 mutex_unlock(&ds1374->mutex);
461 return -EBUSY;
462 }
463
464
465
466 wdt_is_open = 1;
467 mutex_unlock(&ds1374->mutex);
468 return stream_open(inode, file);
469 }
470 return -ENODEV;
471}
472
473
474
475
476static int ds1374_wdt_release(struct inode *inode, struct file *file)
477{
478 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
479 clear_bit(0, &wdt_is_open);
480
481 return 0;
482}
483
484
485
486
487static ssize_t ds1374_wdt_write(struct file *file, const char __user *data,
488 size_t len, loff_t *ppos)
489{
490 if (len) {
491 ds1374_wdt_ping();
492 return 1;
493 }
494 return 0;
495}
496
497static ssize_t ds1374_wdt_read(struct file *file, char __user *data,
498 size_t len, loff_t *ppos)
499{
500 return 0;
501}
502
503
504
505
506static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd,
507 unsigned long arg)
508{
509 int new_margin, options;
510
511 switch (cmd) {
512 case WDIOC_GETSUPPORT:
513 return copy_to_user((struct watchdog_info __user *)arg,
514 &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0;
515
516 case WDIOC_GETSTATUS:
517 case WDIOC_GETBOOTSTATUS:
518 return put_user(0, (int __user *)arg);
519 case WDIOC_KEEPALIVE:
520 ds1374_wdt_ping();
521 return 0;
522 case WDIOC_SETTIMEOUT:
523 if (get_user(new_margin, (int __user *)arg))
524 return -EFAULT;
525
526
527
528
529 new_margin <<= 12;
530 if (new_margin < 1 || new_margin > 16777216)
531 return -EINVAL;
532
533 wdt_margin = new_margin;
534 ds1374_wdt_settimeout(new_margin);
535 ds1374_wdt_ping();
536
537 case WDIOC_GETTIMEOUT:
538
539 return put_user((wdt_margin >> 12), (int __user *)arg);
540 case WDIOC_SETOPTIONS:
541 if (copy_from_user(&options, (int __user *)arg, sizeof(int)))
542 return -EFAULT;
543
544 if (options & WDIOS_DISABLECARD) {
545 pr_info("disable watchdog\n");
546 ds1374_wdt_disable();
547 return 0;
548 }
549
550 if (options & WDIOS_ENABLECARD) {
551 pr_info("enable watchdog\n");
552 ds1374_wdt_settimeout(wdt_margin);
553 ds1374_wdt_ping();
554 return 0;
555 }
556 return -EINVAL;
557 }
558 return -ENOTTY;
559}
560
561static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
562 unsigned long arg)
563{
564 int ret;
565 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
566
567 mutex_lock(&ds1374->mutex);
568 ret = ds1374_wdt_ioctl(file, cmd, arg);
569 mutex_unlock(&ds1374->mutex);
570
571 return ret;
572}
573
574static int ds1374_wdt_notify_sys(struct notifier_block *this,
575 unsigned long code, void *unused)
576{
577 if (code == SYS_DOWN || code == SYS_HALT)
578
579 ds1374_wdt_disable();
580 return NOTIFY_DONE;
581}
582
583static const struct file_operations ds1374_wdt_fops = {
584 .owner = THIS_MODULE,
585 .read = ds1374_wdt_read,
586 .unlocked_ioctl = ds1374_wdt_unlocked_ioctl,
587 .compat_ioctl = compat_ptr_ioctl,
588 .write = ds1374_wdt_write,
589 .open = ds1374_wdt_open,
590 .release = ds1374_wdt_release,
591 .llseek = no_llseek,
592};
593
594static struct miscdevice ds1374_miscdev = {
595 .minor = WATCHDOG_MINOR,
596 .name = "watchdog",
597 .fops = &ds1374_wdt_fops,
598};
599
600static struct notifier_block ds1374_wdt_notifier = {
601 .notifier_call = ds1374_wdt_notify_sys,
602};
603
604#endif
605
606
607
608
609
610
611
612static int ds1374_probe(struct i2c_client *client,
613 const struct i2c_device_id *id)
614{
615 struct ds1374 *ds1374;
616 int ret;
617
618 ds1374 = devm_kzalloc(&client->dev, sizeof(struct ds1374), GFP_KERNEL);
619 if (!ds1374)
620 return -ENOMEM;
621
622 ds1374->rtc = devm_rtc_allocate_device(&client->dev);
623 if (IS_ERR(ds1374->rtc))
624 return PTR_ERR(ds1374->rtc);
625
626 ds1374->client = client;
627 i2c_set_clientdata(client, ds1374);
628
629 INIT_WORK(&ds1374->work, ds1374_work);
630 mutex_init(&ds1374->mutex);
631
632 ret = ds1374_check_rtc_status(client);
633 if (ret)
634 return ret;
635
636 if (client->irq > 0) {
637 ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0,
638 "ds1374", client);
639 if (ret) {
640 dev_err(&client->dev, "unable to request IRQ\n");
641 return ret;
642 }
643
644 device_set_wakeup_capable(&client->dev, 1);
645 }
646
647 ds1374->rtc->ops = &ds1374_rtc_ops;
648 ds1374->rtc->range_max = U32_MAX;
649
650 ret = rtc_register_device(ds1374->rtc);
651 if (ret)
652 return ret;
653
654#ifdef CONFIG_RTC_DRV_DS1374_WDT
655 save_client = client;
656 ret = misc_register(&ds1374_miscdev);
657 if (ret)
658 return ret;
659 ret = register_reboot_notifier(&ds1374_wdt_notifier);
660 if (ret) {
661 misc_deregister(&ds1374_miscdev);
662 return ret;
663 }
664 ds1374_wdt_settimeout(131072);
665#endif
666
667 return 0;
668}
669
670static int ds1374_remove(struct i2c_client *client)
671{
672 struct ds1374 *ds1374 = i2c_get_clientdata(client);
673#ifdef CONFIG_RTC_DRV_DS1374_WDT
674 misc_deregister(&ds1374_miscdev);
675 ds1374_miscdev.parent = NULL;
676 unregister_reboot_notifier(&ds1374_wdt_notifier);
677#endif
678
679 if (client->irq > 0) {
680 mutex_lock(&ds1374->mutex);
681 ds1374->exiting = 1;
682 mutex_unlock(&ds1374->mutex);
683
684 devm_free_irq(&client->dev, client->irq, client);
685 cancel_work_sync(&ds1374->work);
686 }
687
688 return 0;
689}
690
691#ifdef CONFIG_PM_SLEEP
692static int ds1374_suspend(struct device *dev)
693{
694 struct i2c_client *client = to_i2c_client(dev);
695
696 if (client->irq > 0 && device_may_wakeup(&client->dev))
697 enable_irq_wake(client->irq);
698 return 0;
699}
700
701static int ds1374_resume(struct device *dev)
702{
703 struct i2c_client *client = to_i2c_client(dev);
704
705 if (client->irq > 0 && device_may_wakeup(&client->dev))
706 disable_irq_wake(client->irq);
707 return 0;
708}
709#endif
710
711static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
712
713static struct i2c_driver ds1374_driver = {
714 .driver = {
715 .name = "rtc-ds1374",
716 .of_match_table = of_match_ptr(ds1374_of_match),
717 .pm = &ds1374_pm,
718 },
719 .probe = ds1374_probe,
720 .remove = ds1374_remove,
721 .id_table = ds1374_id,
722};
723
724module_i2c_driver(ds1374_driver);
725
726MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
727MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
728MODULE_LICENSE("GPL");
729