rtc-ds13307.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * rtc-ds13307.c - RTC driver for the DS1307 and DS1337 I2C chips.
  3. *
  4. * Copyright (C) 2018 Helmut Pozimski
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/rtc.h>
  12. #include <linux/i2c.h>
  13. #include <linux/bcd.h>
  14. #define M_NAME "rtc-ds13307" /* Module name */
  15. #define DEVICE_DS1307 1
  16. #define DEVICE_DS1337 2
  17. #define COMMON_SEC 0x00
  18. #define COMMON_MIN 0x01
  19. #define COMMON_HOUR 0x02
  20. #define COMMON_DAY 0x03
  21. #define COMMON_DATE 0x04
  22. #define COMMON_MONTH 0x05
  23. #define COMMON_YEAR 0x06
  24. #define COMMON_HIGH_BIT 0x80
  25. #define DS1307_MAX_ADDR 0x3F
  26. #define DS1337_CTL 0x0E
  27. #define DS1337_STAT 0x0F
  28. static int model_detected;
  29. static struct i2c_driver ds13307_driver;
  30. /* Reads a specified number of bytes via i2c, returns 0 on success */
  31. static int ds13307_read_bytes(struct i2c_client *client,
  32. u8 *addr, u8 *bytes, int length) {
  33. int r;
  34. struct i2c_msg msgs[] = {
  35. {
  36. .addr = client->addr,
  37. .len = 1,
  38. .buf = addr
  39. },
  40. {
  41. .addr = client->addr,
  42. .flags = I2C_M_RD,
  43. .len = length,
  44. .buf = bytes
  45. }
  46. };
  47. r = i2c_transfer(client->adapter, msgs, 2);
  48. if (r == 2) {
  49. return 0;
  50. } else {
  51. return r;
  52. }
  53. }
  54. /* The oscillator is stopped for both chips when power is first applied,
  55. * therefore this function checks its status and clears the stop bit.
  56. */
  57. static int ds13307_start_oscillator(struct i2c_client *client) {
  58. u8 data, addr, buf[2];
  59. int r, v;
  60. if (model_detected == DEVICE_DS1307) {
  61. addr = COMMON_SEC;
  62. } else {
  63. addr = DS1337_STAT;
  64. }
  65. r = ds13307_read_bytes(client, &addr, &data, 1);
  66. if (data & COMMON_HIGH_BIT) {
  67. buf[0] = addr;
  68. buf[1] = data ^ COMMON_HIGH_BIT;
  69. v = i2c_master_send(client, buf, 2);
  70. if (v == 2) {
  71. printk(KERN_DEBUG "%s: oscillator stop bit successfully cleared\n", M_NAME);
  72. }
  73. } else {
  74. v = 2;
  75. }
  76. if ((r<0) || (v!=2)) {
  77. return -EIO;
  78. }
  79. return 0;
  80. }
  81. static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
  82. struct i2c_client *client;
  83. int r, century = 1, h12 = 0;
  84. u8 buf[7], stopbit;
  85. u8 addr = COMMON_SEC;
  86. client = to_i2c_client(dev);
  87. r = ds13307_read_bytes(client, &addr, buf, 7);
  88. if (model_detected == DEVICE_DS1337) {
  89. addr = DS1337_STAT;
  90. r = ds13307_read_bytes(client, &addr, &stopbit, 1);
  91. stopbit = stopbit & COMMON_HIGH_BIT;
  92. } else {
  93. stopbit = buf[COMMON_SEC] & COMMON_HIGH_BIT;
  94. }
  95. if (r) {
  96. return -EIO;
  97. }
  98. if (stopbit) {
  99. printk(KERN_ERR "%s: Oscillator stop bit is set, values read from rtc device cannot be trusted\n", M_NAME);
  100. return -EINVAL;
  101. }
  102. if (buf[COMMON_HOUR] & 0x40) {
  103. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x40;
  104. if (buf[COMMON_HOUR] & 0x20) {
  105. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x20;
  106. h12 = 1;
  107. }
  108. }
  109. if (model_detected == DEVICE_DS1337) {
  110. century = buf[COMMON_MONTH] & COMMON_HIGH_BIT;
  111. buf[COMMON_MONTH] = buf[COMMON_MONTH] & 0x7F;
  112. }
  113. time->tm_sec = bcd2bin(buf[COMMON_SEC]);
  114. time->tm_min = bcd2bin(buf[COMMON_MIN]);
  115. time->tm_hour = bcd2bin(buf[COMMON_HOUR]);
  116. if(h12) {
  117. time->tm_hour += 12;
  118. }
  119. time->tm_mday = bcd2bin(buf[COMMON_DATE]);
  120. time->tm_wday = bcd2bin(buf[COMMON_DAY]);
  121. time->tm_mon = bcd2bin(buf[COMMON_MONTH]) - 1 ;
  122. if (century) {
  123. time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
  124. } else {
  125. time->tm_year = bcd2bin(buf[COMMON_YEAR]);
  126. }
  127. return 0;
  128. }
  129. static int ds13307_set_time(struct device *dev, struct rtc_time *time) {
  130. struct i2c_client *client;
  131. u8 buf[8];
  132. client = to_i2c_client(dev);
  133. if (ds13307_start_oscillator(client)) {
  134. printk(KERN_ERR "%s: failed to initialize the oscillator\n", M_NAME);
  135. return -EIO;
  136. }
  137. buf[0] = COMMON_SEC;
  138. buf[1] = bin2bcd(time->tm_sec);
  139. buf[2] = bin2bcd(time->tm_min);
  140. buf[3] = bin2bcd(time->tm_hour);
  141. buf[4] = bin2bcd(time->tm_wday);
  142. buf[5] = bin2bcd(time->tm_mday);
  143. if ((model_detected == DEVICE_DS1337) && (time->tm_year >= 100)) {
  144. buf[6] = bin2bcd(time->tm_mon + 1) | COMMON_HIGH_BIT;
  145. } else if ((model_detected == DEVICE_DS1307) && (time->tm_year < 100)) {
  146. printk(KERN_ERR "%s: device does not support century information, dates before 2000 are not possible\n", M_NAME);
  147. return -EINVAL;
  148. } else {
  149. buf[6] = bin2bcd(time->tm_mon + 1);
  150. }
  151. buf[7] = bin2bcd(time->tm_year % 100);
  152. if (i2c_master_send(client, buf, 8) != 8) {
  153. return -EIO;
  154. }
  155. return 0;
  156. }
  157. static const struct rtc_class_ops ds13307_rtc_ops = {
  158. .read_time = ds13307_read_time,
  159. .set_time = ds13307_set_time
  160. };
  161. /* Performs the device detection to distinguish between the
  162. * DS1307 and DS1337 chips. Returns the defined device ID or
  163. * -1 on failure
  164. */
  165. static int ds13307_detect_device(struct i2c_client *client) {
  166. u8 data, addr = DS1307_MAX_ADDR;
  167. int result;
  168. result = ds13307_read_bytes(client, &addr, &data, 1);
  169. if (!result) {
  170. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  171. return DEVICE_DS1307;
  172. }
  173. addr = DS1337_CTL;
  174. result = ds13307_read_bytes(client, &addr, &data, 1);
  175. if (!result) {
  176. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  177. return DEVICE_DS1337;
  178. }
  179. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  180. M_NAME, client->addr);
  181. return -1;
  182. }
  183. static int ds13307_probe(struct i2c_client *client,
  184. const struct i2c_device_id *id) {
  185. struct rtc_device *rtc;
  186. model_detected = ds13307_detect_device(client);
  187. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  188. return -EIO;
  189. }
  190. rtc = devm_rtc_device_register(&client->dev, ds13307_driver.driver.name,
  191. &ds13307_rtc_ops, THIS_MODULE);
  192. if (IS_ERR(rtc)) {
  193. return PTR_ERR(rtc);
  194. }
  195. i2c_set_clientdata(client,rtc);
  196. return 0;
  197. }
  198. static struct i2c_device_id ds13307_idtable[] = {
  199. { "ds1307", 0 },
  200. { "ds1337", 0 }, {}
  201. };
  202. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  203. static struct of_device_id ds13307_of_match[] = {
  204. { .compatible = "dallas,ds1307" },
  205. { .compatible = "dallas,ds1337" },
  206. {}
  207. };
  208. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  209. static struct i2c_driver ds13307_driver = {
  210. .driver = {
  211. .name = M_NAME,
  212. .of_match_table = of_match_ptr(ds13307_of_match),
  213. .owner = THIS_MODULE
  214. },
  215. .id_table = ds13307_idtable,
  216. .probe = ds13307_probe,
  217. };
  218. module_i2c_driver(ds13307_driver);
  219. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  220. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  221. MODULE_LICENSE("GPL");