rtc-pcf85063.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /*
  19. * Information for this driver was pulled from the following datasheets.
  20. *
  21. * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
  22. * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
  23. *
  24. * PCF85063A -- Rev. 6 — 18 November 2015
  25. * PCF85063TP -- Rev. 4 — 6 May 2015
  26. */
  27. #define PCF85063_REG_CTRL1 0x00 /* status */
  28. #define PCF85063_REG_CTRL1_STOP BIT(5)
  29. #define PCF85063_REG_CTRL2 0x01
  30. #define PCF85063_REG_SC 0x04 /* datetime */
  31. #define PCF85063_REG_SC_OS 0x80
  32. #define PCF85063_REG_MN 0x05
  33. #define PCF85063_REG_HR 0x06
  34. #define PCF85063_REG_DM 0x07
  35. #define PCF85063_REG_DW 0x08
  36. #define PCF85063_REG_MO 0x09
  37. #define PCF85063_REG_YR 0x0A
  38. static struct i2c_driver pcf85063_driver;
  39. static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
  40. {
  41. s32 ret;
  42. ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
  43. if (ret < 0) {
  44. dev_err(&client->dev, "Failing to stop the clock\n");
  45. return -EIO;
  46. }
  47. /* stop the clock */
  48. ret |= PCF85063_REG_CTRL1_STOP;
  49. ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
  50. if (ret < 0) {
  51. dev_err(&client->dev, "Failing to stop the clock\n");
  52. return -EIO;
  53. }
  54. *ctrl1 = ret;
  55. return 0;
  56. }
  57. static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
  58. {
  59. s32 ret;
  60. /* start the clock */
  61. ctrl1 &= PCF85063_REG_CTRL1_STOP;
  62. ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1);
  63. if (ret < 0) {
  64. dev_err(&client->dev, "Failing to start the clock\n");
  65. return -EIO;
  66. }
  67. return 0;
  68. }
  69. static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  70. {
  71. int rc;
  72. u8 regs[7];
  73. /*
  74. * while reading, the time/date registers are blocked and not updated
  75. * anymore until the access is finished. To not lose a second
  76. * event, the access must be finished within one second. So, read all
  77. * time/date registers in one turn.
  78. */
  79. rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
  80. sizeof(regs), regs);
  81. if (rc != sizeof(regs)) {
  82. dev_err(&client->dev, "date/time register read error\n");
  83. return -EIO;
  84. }
  85. /* if the clock has lost its power it makes no sense to use its time */
  86. if (regs[0] & PCF85063_REG_SC_OS) {
  87. dev_warn(&client->dev, "Power loss detected, invalid time\n");
  88. return -EINVAL;
  89. }
  90. tm->tm_sec = bcd2bin(regs[0] & 0x7F);
  91. tm->tm_min = bcd2bin(regs[1] & 0x7F);
  92. tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
  93. tm->tm_mday = bcd2bin(regs[3] & 0x3F);
  94. tm->tm_wday = regs[4] & 0x07;
  95. tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
  96. tm->tm_year = bcd2bin(regs[6]);
  97. tm->tm_year += 100;
  98. return rtc_valid_tm(tm);
  99. }
  100. static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  101. {
  102. int rc;
  103. u8 regs[7];
  104. u8 ctrl1;
  105. if ((tm->tm_year < 100) || (tm->tm_year > 199))
  106. return -EINVAL;
  107. /*
  108. * to accurately set the time, reset the divider chain and keep it in
  109. * reset state until all time/date registers are written
  110. */
  111. rc = pcf85063_stop_clock(client, &ctrl1);
  112. if (rc != 0)
  113. return rc;
  114. /* hours, minutes and seconds */
  115. regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
  116. regs[1] = bin2bcd(tm->tm_min);
  117. regs[2] = bin2bcd(tm->tm_hour);
  118. /* Day of month, 1 - 31 */
  119. regs[3] = bin2bcd(tm->tm_mday);
  120. /* Day, 0 - 6 */
  121. regs[4] = tm->tm_wday & 0x07;
  122. /* month, 1 - 12 */
  123. regs[5] = bin2bcd(tm->tm_mon + 1);
  124. /* year and century */
  125. regs[6] = bin2bcd(tm->tm_year - 100);
  126. /* write all registers at once */
  127. rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
  128. sizeof(regs), regs);
  129. if (rc < 0) {
  130. dev_err(&client->dev, "date/time register write error\n");
  131. return rc;
  132. }
  133. /*
  134. * Write the control register as a separate action since the size of
  135. * the register space is different between the PCF85063TP and
  136. * PCF85063A devices. The rollover point can not be used.
  137. */
  138. rc = pcf85063_start_clock(client, ctrl1);
  139. if (rc != 0)
  140. return rc;
  141. return 0;
  142. }
  143. static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
  144. {
  145. return pcf85063_get_datetime(to_i2c_client(dev), tm);
  146. }
  147. static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
  148. {
  149. return pcf85063_set_datetime(to_i2c_client(dev), tm);
  150. }
  151. static const struct rtc_class_ops pcf85063_rtc_ops = {
  152. .read_time = pcf85063_rtc_read_time,
  153. .set_time = pcf85063_rtc_set_time
  154. };
  155. static int pcf85063_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id)
  157. {
  158. struct rtc_device *rtc;
  159. dev_dbg(&client->dev, "%s\n", __func__);
  160. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  161. return -ENODEV;
  162. rtc = devm_rtc_device_register(&client->dev,
  163. pcf85063_driver.driver.name,
  164. &pcf85063_rtc_ops, THIS_MODULE);
  165. return PTR_ERR_OR_ZERO(rtc);
  166. }
  167. static const struct i2c_device_id pcf85063_id[] = {
  168. { "pcf85063", 0 },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(i2c, pcf85063_id);
  172. #ifdef CONFIG_OF
  173. static const struct of_device_id pcf85063_of_match[] = {
  174. { .compatible = "nxp,pcf85063" },
  175. {}
  176. };
  177. MODULE_DEVICE_TABLE(of, pcf85063_of_match);
  178. #endif
  179. static struct i2c_driver pcf85063_driver = {
  180. .driver = {
  181. .name = "rtc-pcf85063",
  182. .of_match_table = of_match_ptr(pcf85063_of_match),
  183. },
  184. .probe = pcf85063_probe,
  185. .id_table = pcf85063_id,
  186. };
  187. module_i2c_driver(pcf85063_driver);
  188. MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
  189. MODULE_DESCRIPTION("PCF85063 RTC driver");
  190. MODULE_LICENSE("GPL");