rtc-pcf85063.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * An I2C driver for the PCF85063 RTC
  3. * Copyright 2014 Rose Technology
  4. *
  5. * Author: Søren Andersen <san@rosetechnology.dk>
  6. * Maintainers: http://www.nslu2-linux.org/
  7. *
  8. * based on the other drivers in this same directory.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/i2c.h>
  15. #include <linux/bcd.h>
  16. #include <linux/rtc.h>
  17. #include <linux/module.h>
  18. #define PCF85063_REG_CTRL1 0x00 /* status */
  19. #define PCF85063_REG_CTRL1_STOP BIT(5)
  20. #define PCF85063_REG_CTRL2 0x01
  21. #define PCF85063_REG_SC 0x04 /* datetime */
  22. #define PCF85063_REG_SC_OS 0x80
  23. #define PCF85063_REG_MN 0x05
  24. #define PCF85063_REG_HR 0x06
  25. #define PCF85063_REG_DM 0x07
  26. #define PCF85063_REG_DW 0x08
  27. #define PCF85063_REG_MO 0x09
  28. #define PCF85063_REG_YR 0x0A
  29. static struct i2c_driver pcf85063_driver;
  30. static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
  31. {
  32. s32 ret;
  33. ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
  34. if (ret < 0) {
  35. dev_err(&client->dev, "Failing to stop the clock\n");
  36. return -EIO;
  37. }
  38. /* stop the clock */
  39. ret |= PCF85063_REG_CTRL1_STOP;
  40. ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
  41. if (ret < 0) {
  42. dev_err(&client->dev, "Failing to stop the clock\n");
  43. return -EIO;
  44. }
  45. *ctrl1 = ret;
  46. return 0;
  47. }
  48. /*
  49. * In the routines that deal directly with the pcf85063 hardware, we use
  50. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  51. */
  52. static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  53. {
  54. int rc;
  55. u8 regs[7];
  56. /*
  57. * while reading, the time/date registers are blocked and not updated
  58. * anymore until the access is finished. To not lose a second
  59. * event, the access must be finished within one second. So, read all
  60. * time/date registers in one turn.
  61. */
  62. rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
  63. sizeof(regs), regs);
  64. if (rc != sizeof(regs)) {
  65. dev_err(&client->dev, "date/time register read error\n");
  66. return -EIO;
  67. }
  68. /* if the clock has lost its power it makes no sense to use its time */
  69. if (regs[0] & PCF85063_REG_SC_OS) {
  70. dev_warn(&client->dev, "Power loss detected, invalid time\n");
  71. return -EINVAL;
  72. }
  73. tm->tm_sec = bcd2bin(regs[0] & 0x7F);
  74. tm->tm_min = bcd2bin(regs[1] & 0x7F);
  75. tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
  76. tm->tm_mday = bcd2bin(regs[3] & 0x3F);
  77. tm->tm_wday = regs[4] & 0x07;
  78. tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
  79. tm->tm_year = bcd2bin(regs[6]);
  80. if (tm->tm_year < 70)
  81. tm->tm_year += 100; /* assume we are in 1970...2069 */
  82. return rtc_valid_tm(tm);
  83. }
  84. static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  85. {
  86. int rc;
  87. u8 regs[8];
  88. /*
  89. * to accurately set the time, reset the divider chain and keep it in
  90. * reset state until all time/date registers are written
  91. */
  92. rc = pcf85063_stop_clock(client, &regs[7]);
  93. if (rc != 0)
  94. return rc;
  95. /* hours, minutes and seconds */
  96. regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
  97. regs[1] = bin2bcd(tm->tm_min);
  98. regs[2] = bin2bcd(tm->tm_hour);
  99. /* Day of month, 1 - 31 */
  100. regs[3] = bin2bcd(tm->tm_mday);
  101. /* Day, 0 - 6 */
  102. regs[4] = tm->tm_wday & 0x07;
  103. /* month, 1 - 12 */
  104. regs[5] = bin2bcd(tm->tm_mon + 1);
  105. /* year and century */
  106. regs[6] = bin2bcd(tm->tm_year % 100);
  107. /*
  108. * after all time/date registers are written, let the 'address auto
  109. * increment' feature wrap around and write register CTRL1 to re-enable
  110. * the clock divider chain again
  111. */
  112. regs[7] &= ~PCF85063_REG_CTRL1_STOP;
  113. /* write all registers at once */
  114. rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
  115. sizeof(regs), regs);
  116. if (rc < 0) {
  117. dev_err(&client->dev, "date/time register write error\n");
  118. return rc;
  119. }
  120. return 0;
  121. }
  122. static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
  123. {
  124. return pcf85063_get_datetime(to_i2c_client(dev), tm);
  125. }
  126. static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
  127. {
  128. return pcf85063_set_datetime(to_i2c_client(dev), tm);
  129. }
  130. static const struct rtc_class_ops pcf85063_rtc_ops = {
  131. .read_time = pcf85063_rtc_read_time,
  132. .set_time = pcf85063_rtc_set_time
  133. };
  134. static int pcf85063_probe(struct i2c_client *client,
  135. const struct i2c_device_id *id)
  136. {
  137. struct rtc_device *rtc;
  138. dev_dbg(&client->dev, "%s\n", __func__);
  139. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  140. return -ENODEV;
  141. rtc = devm_rtc_device_register(&client->dev,
  142. pcf85063_driver.driver.name,
  143. &pcf85063_rtc_ops, THIS_MODULE);
  144. return PTR_ERR_OR_ZERO(rtc);
  145. }
  146. static const struct i2c_device_id pcf85063_id[] = {
  147. { "pcf85063", 0 },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(i2c, pcf85063_id);
  151. #ifdef CONFIG_OF
  152. static const struct of_device_id pcf85063_of_match[] = {
  153. { .compatible = "nxp,pcf85063" },
  154. {}
  155. };
  156. MODULE_DEVICE_TABLE(of, pcf85063_of_match);
  157. #endif
  158. static struct i2c_driver pcf85063_driver = {
  159. .driver = {
  160. .name = "rtc-pcf85063",
  161. .of_match_table = of_match_ptr(pcf85063_of_match),
  162. },
  163. .probe = pcf85063_probe,
  164. .id_table = pcf85063_id,
  165. };
  166. module_i2c_driver(pcf85063_driver);
  167. MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
  168. MODULE_DESCRIPTION("PCF85063 RTC driver");
  169. MODULE_LICENSE("GPL");