rtc-pcf85063.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 DRV_VERSION "0.0.1"
  19. #define PCF85063_REG_CTRL1 0x00 /* status */
  20. #define PCF85063_REG_CTRL2 0x01
  21. #define PCF85063_REG_SC 0x04 /* datetime */
  22. #define PCF85063_REG_MN 0x05
  23. #define PCF85063_REG_HR 0x06
  24. #define PCF85063_REG_DM 0x07
  25. #define PCF85063_REG_DW 0x08
  26. #define PCF85063_REG_MO 0x09
  27. #define PCF85063_REG_YR 0x0A
  28. #define PCF85063_MO_C 0x80 /* century */
  29. static struct i2c_driver pcf85063_driver;
  30. struct pcf85063 {
  31. struct rtc_device *rtc;
  32. int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
  33. int voltage_low; /* indicates if a low_voltage was detected */
  34. };
  35. /*
  36. * In the routines that deal directly with the pcf85063 hardware, we use
  37. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  38. */
  39. static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  40. {
  41. struct pcf85063 *pcf85063 = i2c_get_clientdata(client);
  42. unsigned char buf[13] = { PCF85063_REG_CTRL1 };
  43. struct i2c_msg msgs[] = {
  44. {/* setup read ptr */
  45. .addr = client->addr,
  46. .len = 1,
  47. .buf = buf
  48. },
  49. {/* read status + date */
  50. .addr = client->addr,
  51. .flags = I2C_M_RD,
  52. .len = 13,
  53. .buf = buf
  54. },
  55. };
  56. /* read registers */
  57. if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
  58. dev_err(&client->dev, "%s: read error\n", __func__);
  59. return -EIO;
  60. }
  61. tm->tm_sec = bcd2bin(buf[PCF85063_REG_SC] & 0x7F);
  62. tm->tm_min = bcd2bin(buf[PCF85063_REG_MN] & 0x7F);
  63. tm->tm_hour = bcd2bin(buf[PCF85063_REG_HR] & 0x3F); /* rtc hr 0-23 */
  64. tm->tm_mday = bcd2bin(buf[PCF85063_REG_DM] & 0x3F);
  65. tm->tm_wday = buf[PCF85063_REG_DW] & 0x07;
  66. tm->tm_mon = bcd2bin(buf[PCF85063_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  67. tm->tm_year = bcd2bin(buf[PCF85063_REG_YR]);
  68. if (tm->tm_year < 70)
  69. tm->tm_year += 100; /* assume we are in 1970...2069 */
  70. /* detect the polarity heuristically. see note above. */
  71. pcf85063->c_polarity = (buf[PCF85063_REG_MO] & PCF85063_MO_C) ?
  72. (tm->tm_year >= 100) : (tm->tm_year < 100);
  73. /* the clock can give out invalid datetime, but we cannot return
  74. * -EINVAL otherwise hwclock will refuse to set the time on bootup.
  75. */
  76. if (rtc_valid_tm(tm) < 0)
  77. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  78. return 0;
  79. }
  80. static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  81. {
  82. int i = 0, err = 0;
  83. unsigned char buf[11];
  84. /* Control & status */
  85. buf[PCF85063_REG_CTRL1] = 0;
  86. buf[PCF85063_REG_CTRL2] = 5;
  87. /* hours, minutes and seconds */
  88. buf[PCF85063_REG_SC] = bin2bcd(tm->tm_sec) & 0x7F;
  89. buf[PCF85063_REG_MN] = bin2bcd(tm->tm_min);
  90. buf[PCF85063_REG_HR] = bin2bcd(tm->tm_hour);
  91. /* Day of month, 1 - 31 */
  92. buf[PCF85063_REG_DM] = bin2bcd(tm->tm_mday);
  93. /* Day, 0 - 6 */
  94. buf[PCF85063_REG_DW] = tm->tm_wday & 0x07;
  95. /* month, 1 - 12 */
  96. buf[PCF85063_REG_MO] = bin2bcd(tm->tm_mon + 1);
  97. /* year and century */
  98. buf[PCF85063_REG_YR] = bin2bcd(tm->tm_year % 100);
  99. /* write register's data */
  100. for (i = 0; i < sizeof(buf); i++) {
  101. unsigned char data[2] = { i, buf[i] };
  102. err = i2c_master_send(client, data, sizeof(data));
  103. if (err != sizeof(data)) {
  104. dev_err(&client->dev, "%s: err=%d addr=%02x, data=%02x\n",
  105. __func__, err, data[0], data[1]);
  106. return -EIO;
  107. }
  108. }
  109. return 0;
  110. }
  111. static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
  112. {
  113. return pcf85063_get_datetime(to_i2c_client(dev), tm);
  114. }
  115. static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
  116. {
  117. return pcf85063_set_datetime(to_i2c_client(dev), tm);
  118. }
  119. static const struct rtc_class_ops pcf85063_rtc_ops = {
  120. .read_time = pcf85063_rtc_read_time,
  121. .set_time = pcf85063_rtc_set_time
  122. };
  123. static int pcf85063_probe(struct i2c_client *client,
  124. const struct i2c_device_id *id)
  125. {
  126. struct pcf85063 *pcf85063;
  127. dev_dbg(&client->dev, "%s\n", __func__);
  128. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  129. return -ENODEV;
  130. pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
  131. GFP_KERNEL);
  132. if (!pcf85063)
  133. return -ENOMEM;
  134. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  135. i2c_set_clientdata(client, pcf85063);
  136. pcf85063->rtc = devm_rtc_device_register(&client->dev,
  137. pcf85063_driver.driver.name,
  138. &pcf85063_rtc_ops, THIS_MODULE);
  139. return PTR_ERR_OR_ZERO(pcf85063->rtc);
  140. }
  141. static const struct i2c_device_id pcf85063_id[] = {
  142. { "pcf85063", 0 },
  143. { }
  144. };
  145. MODULE_DEVICE_TABLE(i2c, pcf85063_id);
  146. #ifdef CONFIG_OF
  147. static const struct of_device_id pcf85063_of_match[] = {
  148. { .compatible = "nxp,pcf85063" },
  149. {}
  150. };
  151. MODULE_DEVICE_TABLE(of, pcf85063_of_match);
  152. #endif
  153. static struct i2c_driver pcf85063_driver = {
  154. .driver = {
  155. .name = "rtc-pcf85063",
  156. .owner = THIS_MODULE,
  157. .of_match_table = of_match_ptr(pcf85063_of_match),
  158. },
  159. .probe = pcf85063_probe,
  160. .id_table = pcf85063_id,
  161. };
  162. module_i2c_driver(pcf85063_driver);
  163. MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
  164. MODULE_DESCRIPTION("PCF85063 RTC driver");
  165. MODULE_LICENSE("GPL");
  166. MODULE_VERSION(DRV_VERSION);