hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/rtc/rtc-ds1374.c
....@@ -14,7 +14,7 @@
1414 */
1515 /*
1616 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
17
- * recommened in .../Documentation/i2c/writing-clients section
17
+ * recommended in .../Documentation/i2c/writing-clients.rst section
1818 * "Sending and receiving", using SMBus level communication is preferred.
1919 */
2020
....@@ -46,6 +46,7 @@
4646 #define DS1374_REG_WDALM2 0x06
4747 #define DS1374_REG_CR 0x07 /* Control */
4848 #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */
49
+#define DS1374_REG_CR_WDSTR 0x08 /* 1=INT, 0=RST */
4950 #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
5051 #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */
5152 #define DS1374_REG_SR 0x08 /* Status */
....@@ -71,7 +72,9 @@
7172 struct i2c_client *client;
7273 struct rtc_device *rtc;
7374 struct work_struct work;
74
-
75
+#ifdef CONFIG_RTC_DRV_DS1374_WDT
76
+ struct watchdog_device wdt;
77
+#endif
7578 /* The mutex protects alarm operations, and prevents a race
7679 * between the enable_irq() in the workqueue and the free_irq()
7780 * in the remove function.
....@@ -164,7 +167,7 @@
164167
165168 ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
166169 if (!ret)
167
- rtc_time_to_tm(itime, time);
170
+ rtc_time64_to_tm(itime, time);
168171
169172 return ret;
170173 }
....@@ -172,9 +175,8 @@
172175 static int ds1374_set_time(struct device *dev, struct rtc_time *time)
173176 {
174177 struct i2c_client *client = to_i2c_client(dev);
175
- unsigned long itime;
178
+ unsigned long itime = rtc_tm_to_time64(time);
176179
177
- rtc_tm_to_time(time, &itime);
178180 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
179181 }
180182
....@@ -212,7 +214,7 @@
212214 if (ret)
213215 goto out;
214216
215
- rtc_time_to_tm(now + cur_alarm, &alarm->time);
217
+ rtc_time64_to_tm(now + cur_alarm, &alarm->time);
216218 alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
217219 alarm->pending = !!(sr & DS1374_REG_SR_AF);
218220
....@@ -237,8 +239,8 @@
237239 if (ret < 0)
238240 return ret;
239241
240
- rtc_tm_to_time(&alarm->time, &new_alarm);
241
- rtc_tm_to_time(&now, &itime);
242
+ new_alarm = rtc_tm_to_time64(&alarm->time);
243
+ itime = rtc_tm_to_time64(&now);
242244
243245 /* This can happen due to races, in addition to dates that are
244246 * truly in the past. To avoid requiring the caller to check for
....@@ -370,238 +372,96 @@
370372 *
371373 *****************************************************************************
372374 */
373
-static struct i2c_client *save_client;
374375 /* Default margin */
375
-#define WD_TIMO 131762
376
+#define TIMER_MARGIN_DEFAULT 32
377
+#define TIMER_MARGIN_MIN 1
378
+#define TIMER_MARGIN_MAX 4095 /* 24-bit value */
376379
377
-#define DRV_NAME "DS1374 Watchdog"
378
-
379
-static int wdt_margin = WD_TIMO;
380
-static unsigned long wdt_is_open;
380
+static int wdt_margin;
381381 module_param(wdt_margin, int, 0);
382382 MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
383383
384
+static bool nowayout = WATCHDOG_NOWAYOUT;
385
+module_param(nowayout, bool, 0);
386
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default ="
387
+ __MODULE_STRING(WATCHDOG_NOWAYOUT)")");
388
+
384389 static const struct watchdog_info ds1374_wdt_info = {
385
- .identity = "DS1374 WTD",
390
+ .identity = "DS1374 Watchdog",
386391 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
387392 WDIOF_MAGICCLOSE,
388393 };
389394
390
-static int ds1374_wdt_settimeout(unsigned int timeout)
395
+static int ds1374_wdt_settimeout(struct watchdog_device *wdt, unsigned int timeout)
391396 {
392
- int ret = -ENOIOCTLCMD;
393
- int cr;
397
+ struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
398
+ struct i2c_client *client = ds1374->client;
399
+ int ret, cr;
394400
395
- ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
396
- if (ret < 0)
397
- goto out;
401
+ wdt->timeout = timeout;
402
+
403
+ cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
404
+ if (cr < 0)
405
+ return cr;
398406
399407 /* Disable any existing watchdog/alarm before setting the new one */
400408 cr &= ~DS1374_REG_CR_WACE;
401409
402
- ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
410
+ ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
403411 if (ret < 0)
404
- goto out;
412
+ return ret;
405413
406414 /* Set new watchdog time */
407
- ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3);
408
- if (ret) {
409
- pr_info("couldn't set new watchdog time\n");
410
- goto out;
411
- }
415
+ timeout = timeout * 4096;
416
+ ret = ds1374_write_rtc(client, timeout, DS1374_REG_WDALM0, 3);
417
+ if (ret)
418
+ return ret;
412419
413420 /* Enable watchdog timer */
414421 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
422
+ cr &= ~DS1374_REG_CR_WDSTR;/* for RST PIN */
415423 cr &= ~DS1374_REG_CR_AIE;
416424
417
- ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
425
+ ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
418426 if (ret < 0)
419
- goto out;
427
+ return ret;
420428
421429 return 0;
422
-out:
423
- return ret;
424430 }
425
-
426431
427432 /*
428433 * Reload the watchdog timer. (ie, pat the watchdog)
429434 */
430
-static void ds1374_wdt_ping(void)
435
+static int ds1374_wdt_start(struct watchdog_device *wdt)
431436 {
437
+ struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
432438 u32 val;
433
- int ret = 0;
434439
435
- ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3);
436
- if (ret)
437
- pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret);
440
+ return ds1374_read_rtc(ds1374->client, &val, DS1374_REG_WDALM0, 3);
438441 }
439442
440
-static void ds1374_wdt_disable(void)
443
+static int ds1374_wdt_stop(struct watchdog_device *wdt)
441444 {
442
- int ret = -ENOIOCTLCMD;
445
+ struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
446
+ struct i2c_client *client = ds1374->client;
443447 int cr;
444448
445
- cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
449
+ cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
450
+ if (cr < 0)
451
+ return cr;
452
+
446453 /* Disable watchdog timer */
447454 cr &= ~DS1374_REG_CR_WACE;
448455
449
- ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
456
+ return i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
450457 }
451458
452
-/*
453
- * Watchdog device is opened, and watchdog starts running.
454
- */
455
-static int ds1374_wdt_open(struct inode *inode, struct file *file)
456
-{
457
- struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
458
-
459
- if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
460
- mutex_lock(&ds1374->mutex);
461
- if (test_and_set_bit(0, &wdt_is_open)) {
462
- mutex_unlock(&ds1374->mutex);
463
- return -EBUSY;
464
- }
465
- /*
466
- * Activate
467
- */
468
- wdt_is_open = 1;
469
- mutex_unlock(&ds1374->mutex);
470
- return nonseekable_open(inode, file);
471
- }
472
- return -ENODEV;
473
-}
474
-
475
-/*
476
- * Close the watchdog device.
477
- */
478
-static int ds1374_wdt_release(struct inode *inode, struct file *file)
479
-{
480
- if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
481
- clear_bit(0, &wdt_is_open);
482
-
483
- return 0;
484
-}
485
-
486
-/*
487
- * Pat the watchdog whenever device is written to.
488
- */
489
-static ssize_t ds1374_wdt_write(struct file *file, const char __user *data,
490
- size_t len, loff_t *ppos)
491
-{
492
- if (len) {
493
- ds1374_wdt_ping();
494
- return 1;
495
- }
496
- return 0;
497
-}
498
-
499
-static ssize_t ds1374_wdt_read(struct file *file, char __user *data,
500
- size_t len, loff_t *ppos)
501
-{
502
- return 0;
503
-}
504
-
505
-/*
506
- * Handle commands from user-space.
507
- */
508
-static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd,
509
- unsigned long arg)
510
-{
511
- int new_margin, options;
512
-
513
- switch (cmd) {
514
- case WDIOC_GETSUPPORT:
515
- return copy_to_user((struct watchdog_info __user *)arg,
516
- &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0;
517
-
518
- case WDIOC_GETSTATUS:
519
- case WDIOC_GETBOOTSTATUS:
520
- return put_user(0, (int __user *)arg);
521
- case WDIOC_KEEPALIVE:
522
- ds1374_wdt_ping();
523
- return 0;
524
- case WDIOC_SETTIMEOUT:
525
- if (get_user(new_margin, (int __user *)arg))
526
- return -EFAULT;
527
-
528
- /* the hardware's tick rate is 4096 Hz, so
529
- * the counter value needs to be scaled accordingly
530
- */
531
- new_margin <<= 12;
532
- if (new_margin < 1 || new_margin > 16777216)
533
- return -EINVAL;
534
-
535
- wdt_margin = new_margin;
536
- ds1374_wdt_settimeout(new_margin);
537
- ds1374_wdt_ping();
538
- /* fallthrough */
539
- case WDIOC_GETTIMEOUT:
540
- /* when returning ... inverse is true */
541
- return put_user((wdt_margin >> 12), (int __user *)arg);
542
- case WDIOC_SETOPTIONS:
543
- if (copy_from_user(&options, (int __user *)arg, sizeof(int)))
544
- return -EFAULT;
545
-
546
- if (options & WDIOS_DISABLECARD) {
547
- pr_info("disable watchdog\n");
548
- ds1374_wdt_disable();
549
- return 0;
550
- }
551
-
552
- if (options & WDIOS_ENABLECARD) {
553
- pr_info("enable watchdog\n");
554
- ds1374_wdt_settimeout(wdt_margin);
555
- ds1374_wdt_ping();
556
- return 0;
557
- }
558
- return -EINVAL;
559
- }
560
- return -ENOTTY;
561
-}
562
-
563
-static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
564
- unsigned long arg)
565
-{
566
- int ret;
567
- struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
568
-
569
- mutex_lock(&ds1374->mutex);
570
- ret = ds1374_wdt_ioctl(file, cmd, arg);
571
- mutex_unlock(&ds1374->mutex);
572
-
573
- return ret;
574
-}
575
-
576
-static int ds1374_wdt_notify_sys(struct notifier_block *this,
577
- unsigned long code, void *unused)
578
-{
579
- if (code == SYS_DOWN || code == SYS_HALT)
580
- /* Disable Watchdog */
581
- ds1374_wdt_disable();
582
- return NOTIFY_DONE;
583
-}
584
-
585
-static const struct file_operations ds1374_wdt_fops = {
586
- .owner = THIS_MODULE,
587
- .read = ds1374_wdt_read,
588
- .unlocked_ioctl = ds1374_wdt_unlocked_ioctl,
589
- .write = ds1374_wdt_write,
590
- .open = ds1374_wdt_open,
591
- .release = ds1374_wdt_release,
592
- .llseek = no_llseek,
459
+static const struct watchdog_ops ds1374_wdt_ops = {
460
+ .owner = THIS_MODULE,
461
+ .start = ds1374_wdt_start,
462
+ .stop = ds1374_wdt_stop,
463
+ .set_timeout = ds1374_wdt_settimeout,
593464 };
594
-
595
-static struct miscdevice ds1374_miscdev = {
596
- .minor = WATCHDOG_MINOR,
597
- .name = "watchdog",
598
- .fops = &ds1374_wdt_fops,
599
-};
600
-
601
-static struct notifier_block ds1374_wdt_notifier = {
602
- .notifier_call = ds1374_wdt_notify_sys,
603
-};
604
-
605465 #endif /*CONFIG_RTC_DRV_DS1374_WDT*/
606466 /*
607467 *****************************************************************************
....@@ -646,22 +506,29 @@
646506 }
647507
648508 ds1374->rtc->ops = &ds1374_rtc_ops;
509
+ ds1374->rtc->range_max = U32_MAX;
649510
650511 ret = rtc_register_device(ds1374->rtc);
651512 if (ret)
652513 return ret;
653514
654515 #ifdef CONFIG_RTC_DRV_DS1374_WDT
655
- save_client = client;
656
- ret = misc_register(&ds1374_miscdev);
516
+ ds1374->wdt.info = &ds1374_wdt_info;
517
+ ds1374->wdt.ops = &ds1374_wdt_ops;
518
+ ds1374->wdt.timeout = TIMER_MARGIN_DEFAULT;
519
+ ds1374->wdt.min_timeout = TIMER_MARGIN_MIN;
520
+ ds1374->wdt.max_timeout = TIMER_MARGIN_MAX;
521
+
522
+ watchdog_init_timeout(&ds1374->wdt, wdt_margin, &client->dev);
523
+ watchdog_set_nowayout(&ds1374->wdt, nowayout);
524
+ watchdog_stop_on_reboot(&ds1374->wdt);
525
+ watchdog_stop_on_unregister(&ds1374->wdt);
526
+ watchdog_set_drvdata(&ds1374->wdt, ds1374);
527
+ ds1374_wdt_settimeout(&ds1374->wdt, ds1374->wdt.timeout);
528
+
529
+ ret = devm_watchdog_register_device(&client->dev, &ds1374->wdt);
657530 if (ret)
658531 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);
665532 #endif
666533
667534 return 0;
....@@ -670,11 +537,6 @@
670537 static int ds1374_remove(struct i2c_client *client)
671538 {
672539 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
678540
679541 if (client->irq > 0) {
680542 mutex_lock(&ds1374->mutex);