rtc-ds1672.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * An rtc/i2c driver for the Dallas DS1672
  4. * Copyright 2005-06 Tower Technologies
  5. *
  6. * Author: Alessandro Zummo <[email protected]>
  7. */
  8. #include <linux/i2c.h>
  9. #include <linux/rtc.h>
  10. #include <linux/module.h>
  11. /* Registers */
  12. #define DS1672_REG_CNT_BASE 0
  13. #define DS1672_REG_CONTROL 4
  14. #define DS1672_REG_TRICKLE 5
  15. #define DS1672_REG_CONTROL_EOSC 0x80
  16. /*
  17. * In the routines that deal directly with the ds1672 hardware, we use
  18. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
  19. * Time is set to UTC.
  20. */
  21. static int ds1672_read_time(struct device *dev, struct rtc_time *tm)
  22. {
  23. struct i2c_client *client = to_i2c_client(dev);
  24. unsigned long time;
  25. unsigned char addr = DS1672_REG_CONTROL;
  26. unsigned char buf[4];
  27. struct i2c_msg msgs[] = {
  28. {/* setup read ptr */
  29. .addr = client->addr,
  30. .len = 1,
  31. .buf = &addr
  32. },
  33. {/* read date */
  34. .addr = client->addr,
  35. .flags = I2C_M_RD,
  36. .len = 1,
  37. .buf = buf
  38. },
  39. };
  40. /* read control register */
  41. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  42. dev_warn(&client->dev, "Unable to read the control register\n");
  43. return -EIO;
  44. }
  45. if (buf[0] & DS1672_REG_CONTROL_EOSC) {
  46. dev_warn(&client->dev, "Oscillator not enabled. Set time to enable.\n");
  47. return -EINVAL;
  48. }
  49. addr = DS1672_REG_CNT_BASE;
  50. msgs[1].len = 4;
  51. /* read date registers */
  52. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  53. dev_err(&client->dev, "%s: read error\n", __func__);
  54. return -EIO;
  55. }
  56. dev_dbg(&client->dev,
  57. "%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
  58. __func__, buf[0], buf[1], buf[2], buf[3]);
  59. time = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  60. (buf[1] << 8) | buf[0];
  61. rtc_time64_to_tm(time, tm);
  62. dev_dbg(&client->dev, "%s: tm is %ptR\n", __func__, tm);
  63. return 0;
  64. }
  65. static int ds1672_set_time(struct device *dev, struct rtc_time *tm)
  66. {
  67. struct i2c_client *client = to_i2c_client(dev);
  68. int xfer;
  69. unsigned char buf[6];
  70. unsigned long secs = rtc_tm_to_time64(tm);
  71. buf[0] = DS1672_REG_CNT_BASE;
  72. buf[1] = secs & 0x000000FF;
  73. buf[2] = (secs & 0x0000FF00) >> 8;
  74. buf[3] = (secs & 0x00FF0000) >> 16;
  75. buf[4] = (secs & 0xFF000000) >> 24;
  76. buf[5] = 0; /* set control reg to enable counting */
  77. xfer = i2c_master_send(client, buf, 6);
  78. if (xfer != 6) {
  79. dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
  80. return -EIO;
  81. }
  82. return 0;
  83. }
  84. static const struct rtc_class_ops ds1672_rtc_ops = {
  85. .read_time = ds1672_read_time,
  86. .set_time = ds1672_set_time,
  87. };
  88. static int ds1672_probe(struct i2c_client *client)
  89. {
  90. int err = 0;
  91. struct rtc_device *rtc;
  92. dev_dbg(&client->dev, "%s\n", __func__);
  93. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  94. return -ENODEV;
  95. rtc = devm_rtc_allocate_device(&client->dev);
  96. if (IS_ERR(rtc))
  97. return PTR_ERR(rtc);
  98. rtc->ops = &ds1672_rtc_ops;
  99. rtc->range_max = U32_MAX;
  100. err = devm_rtc_register_device(rtc);
  101. if (err)
  102. return err;
  103. i2c_set_clientdata(client, rtc);
  104. return 0;
  105. }
  106. static const struct i2c_device_id ds1672_id[] = {
  107. { "ds1672", 0 },
  108. { }
  109. };
  110. MODULE_DEVICE_TABLE(i2c, ds1672_id);
  111. static const __maybe_unused struct of_device_id ds1672_of_match[] = {
  112. { .compatible = "dallas,ds1672" },
  113. { }
  114. };
  115. MODULE_DEVICE_TABLE(of, ds1672_of_match);
  116. static struct i2c_driver ds1672_driver = {
  117. .driver = {
  118. .name = "rtc-ds1672",
  119. .of_match_table = of_match_ptr(ds1672_of_match),
  120. },
  121. .probe_new = ds1672_probe,
  122. .id_table = ds1672_id,
  123. };
  124. module_i2c_driver(ds1672_driver);
  125. MODULE_AUTHOR("Alessandro Zummo <[email protected]>");
  126. MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
  127. MODULE_LICENSE("GPL");