.. | .. |
---|
14 | 14 | */ |
---|
15 | 15 | /* |
---|
16 | 16 | * 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 |
---|
18 | 18 | * "Sending and receiving", using SMBus level communication is preferred. |
---|
19 | 19 | */ |
---|
20 | 20 | |
---|
.. | .. |
---|
46 | 46 | #define DS1374_REG_WDALM2 0x06 |
---|
47 | 47 | #define DS1374_REG_CR 0x07 /* Control */ |
---|
48 | 48 | #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */ |
---|
| 49 | +#define DS1374_REG_CR_WDSTR 0x08 /* 1=INT, 0=RST */ |
---|
49 | 50 | #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */ |
---|
50 | 51 | #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */ |
---|
51 | 52 | #define DS1374_REG_SR 0x08 /* Status */ |
---|
.. | .. |
---|
71 | 72 | struct i2c_client *client; |
---|
72 | 73 | struct rtc_device *rtc; |
---|
73 | 74 | struct work_struct work; |
---|
74 | | - |
---|
| 75 | +#ifdef CONFIG_RTC_DRV_DS1374_WDT |
---|
| 76 | + struct watchdog_device wdt; |
---|
| 77 | +#endif |
---|
75 | 78 | /* The mutex protects alarm operations, and prevents a race |
---|
76 | 79 | * between the enable_irq() in the workqueue and the free_irq() |
---|
77 | 80 | * in the remove function. |
---|
.. | .. |
---|
164 | 167 | |
---|
165 | 168 | ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4); |
---|
166 | 169 | if (!ret) |
---|
167 | | - rtc_time_to_tm(itime, time); |
---|
| 170 | + rtc_time64_to_tm(itime, time); |
---|
168 | 171 | |
---|
169 | 172 | return ret; |
---|
170 | 173 | } |
---|
.. | .. |
---|
172 | 175 | static int ds1374_set_time(struct device *dev, struct rtc_time *time) |
---|
173 | 176 | { |
---|
174 | 177 | struct i2c_client *client = to_i2c_client(dev); |
---|
175 | | - unsigned long itime; |
---|
| 178 | + unsigned long itime = rtc_tm_to_time64(time); |
---|
176 | 179 | |
---|
177 | | - rtc_tm_to_time(time, &itime); |
---|
178 | 180 | return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4); |
---|
179 | 181 | } |
---|
180 | 182 | |
---|
.. | .. |
---|
212 | 214 | if (ret) |
---|
213 | 215 | goto out; |
---|
214 | 216 | |
---|
215 | | - rtc_time_to_tm(now + cur_alarm, &alarm->time); |
---|
| 217 | + rtc_time64_to_tm(now + cur_alarm, &alarm->time); |
---|
216 | 218 | alarm->enabled = !!(cr & DS1374_REG_CR_WACE); |
---|
217 | 219 | alarm->pending = !!(sr & DS1374_REG_SR_AF); |
---|
218 | 220 | |
---|
.. | .. |
---|
237 | 239 | if (ret < 0) |
---|
238 | 240 | return ret; |
---|
239 | 241 | |
---|
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); |
---|
242 | 244 | |
---|
243 | 245 | /* This can happen due to races, in addition to dates that are |
---|
244 | 246 | * truly in the past. To avoid requiring the caller to check for |
---|
.. | .. |
---|
370 | 372 | * |
---|
371 | 373 | ***************************************************************************** |
---|
372 | 374 | */ |
---|
373 | | -static struct i2c_client *save_client; |
---|
374 | 375 | /* 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 */ |
---|
376 | 379 | |
---|
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; |
---|
381 | 381 | module_param(wdt_margin, int, 0); |
---|
382 | 382 | MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)"); |
---|
383 | 383 | |
---|
| 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 | + |
---|
384 | 389 | static const struct watchdog_info ds1374_wdt_info = { |
---|
385 | | - .identity = "DS1374 WTD", |
---|
| 390 | + .identity = "DS1374 Watchdog", |
---|
386 | 391 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
---|
387 | 392 | WDIOF_MAGICCLOSE, |
---|
388 | 393 | }; |
---|
389 | 394 | |
---|
390 | | -static int ds1374_wdt_settimeout(unsigned int timeout) |
---|
| 395 | +static int ds1374_wdt_settimeout(struct watchdog_device *wdt, unsigned int timeout) |
---|
391 | 396 | { |
---|
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; |
---|
394 | 400 | |
---|
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; |
---|
398 | 406 | |
---|
399 | 407 | /* Disable any existing watchdog/alarm before setting the new one */ |
---|
400 | 408 | cr &= ~DS1374_REG_CR_WACE; |
---|
401 | 409 | |
---|
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); |
---|
403 | 411 | if (ret < 0) |
---|
404 | | - goto out; |
---|
| 412 | + return ret; |
---|
405 | 413 | |
---|
406 | 414 | /* 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; |
---|
412 | 419 | |
---|
413 | 420 | /* Enable watchdog timer */ |
---|
414 | 421 | cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM; |
---|
| 422 | + cr &= ~DS1374_REG_CR_WDSTR;/* for RST PIN */ |
---|
415 | 423 | cr &= ~DS1374_REG_CR_AIE; |
---|
416 | 424 | |
---|
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); |
---|
418 | 426 | if (ret < 0) |
---|
419 | | - goto out; |
---|
| 427 | + return ret; |
---|
420 | 428 | |
---|
421 | 429 | return 0; |
---|
422 | | -out: |
---|
423 | | - return ret; |
---|
424 | 430 | } |
---|
425 | | - |
---|
426 | 431 | |
---|
427 | 432 | /* |
---|
428 | 433 | * Reload the watchdog timer. (ie, pat the watchdog) |
---|
429 | 434 | */ |
---|
430 | | -static void ds1374_wdt_ping(void) |
---|
| 435 | +static int ds1374_wdt_start(struct watchdog_device *wdt) |
---|
431 | 436 | { |
---|
| 437 | + struct ds1374 *ds1374 = watchdog_get_drvdata(wdt); |
---|
432 | 438 | u32 val; |
---|
433 | | - int ret = 0; |
---|
434 | 439 | |
---|
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); |
---|
438 | 441 | } |
---|
439 | 442 | |
---|
440 | | -static void ds1374_wdt_disable(void) |
---|
| 443 | +static int ds1374_wdt_stop(struct watchdog_device *wdt) |
---|
441 | 444 | { |
---|
442 | | - int ret = -ENOIOCTLCMD; |
---|
| 445 | + struct ds1374 *ds1374 = watchdog_get_drvdata(wdt); |
---|
| 446 | + struct i2c_client *client = ds1374->client; |
---|
443 | 447 | int cr; |
---|
444 | 448 | |
---|
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 | + |
---|
446 | 453 | /* Disable watchdog timer */ |
---|
447 | 454 | cr &= ~DS1374_REG_CR_WACE; |
---|
448 | 455 | |
---|
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); |
---|
450 | 457 | } |
---|
451 | 458 | |
---|
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, |
---|
593 | 464 | }; |
---|
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 | | - |
---|
605 | 465 | #endif /*CONFIG_RTC_DRV_DS1374_WDT*/ |
---|
606 | 466 | /* |
---|
607 | 467 | ***************************************************************************** |
---|
.. | .. |
---|
646 | 506 | } |
---|
647 | 507 | |
---|
648 | 508 | ds1374->rtc->ops = &ds1374_rtc_ops; |
---|
| 509 | + ds1374->rtc->range_max = U32_MAX; |
---|
649 | 510 | |
---|
650 | 511 | ret = rtc_register_device(ds1374->rtc); |
---|
651 | 512 | if (ret) |
---|
652 | 513 | return ret; |
---|
653 | 514 | |
---|
654 | 515 | #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); |
---|
657 | 530 | if (ret) |
---|
658 | 531 | 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 | 532 | #endif |
---|
666 | 533 | |
---|
667 | 534 | return 0; |
---|
.. | .. |
---|
670 | 537 | static int ds1374_remove(struct i2c_client *client) |
---|
671 | 538 | { |
---|
672 | 539 | 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 | 540 | |
---|
679 | 541 | if (client->irq > 0) { |
---|
680 | 542 | mutex_lock(&ds1374->mutex); |
---|