rtc-ds13307.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /* Writes a single byte to a specified address on the device */
  55. static int ds13307_write_single_byte(struct i2c_client *client, u8 *addr, u8 *byte) {
  56. int r;
  57. u8 buf[2];
  58. buf[0] = (*addr);
  59. buf[1] = (*byte);
  60. r = i2c_master_send(client, buf, 2);
  61. if (r != 2) {
  62. return -EIO;
  63. }
  64. return 0;
  65. }
  66. /* The oscillator is stopped for both chips when power is first applied,
  67. * therefore this function checks its status and clears the stop bit.
  68. */
  69. static int ds13307_start_oscillator(struct i2c_client *client) {
  70. u8 data, addr, buf[2];
  71. int r, v;
  72. if (model_detected == DEVICE_DS1307) {
  73. addr = COMMON_SEC;
  74. } else {
  75. addr = DS1337_STAT;
  76. }
  77. r = ds13307_read_bytes(client, &addr, &data, 1);
  78. if (data & COMMON_HIGH_BIT) {
  79. buf[0] = addr;
  80. buf[1] = data ^ COMMON_HIGH_BIT;
  81. v = i2c_master_send(client, buf, 2);
  82. if (v == 2) {
  83. printk(KERN_DEBUG "%s: oscillator stop bit successfully cleared\n", M_NAME);
  84. }
  85. } else {
  86. v = 2;
  87. }
  88. if ((r<0) || (v!=2)) {
  89. return -EIO;
  90. }
  91. return 0;
  92. }
  93. static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
  94. struct i2c_client *client;
  95. int r, century = 1, h12 = 0;
  96. u8 buf[7], stopbit;
  97. u8 addr = COMMON_SEC;
  98. client = to_i2c_client(dev);
  99. r = ds13307_read_bytes(client, &addr, buf, 7);
  100. if (model_detected == DEVICE_DS1337) {
  101. addr = DS1337_STAT;
  102. r = ds13307_read_bytes(client, &addr, &stopbit, 1);
  103. stopbit = stopbit & COMMON_HIGH_BIT;
  104. } else {
  105. stopbit = buf[COMMON_SEC] & COMMON_HIGH_BIT;
  106. }
  107. if (r) {
  108. return -EIO;
  109. }
  110. if (stopbit) {
  111. printk(KERN_ERR "%s: Oscillator stop bit is set, values read from rtc device cannot be trusted\n", M_NAME);
  112. return -EINVAL;
  113. }
  114. if (buf[COMMON_HOUR] & 0x40) {
  115. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x40;
  116. if (buf[COMMON_HOUR] & 0x20) {
  117. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x20;
  118. h12 = 1;
  119. }
  120. }
  121. if (model_detected == DEVICE_DS1337) {
  122. century = buf[COMMON_MONTH] & COMMON_HIGH_BIT;
  123. buf[COMMON_MONTH] = buf[COMMON_MONTH] & 0x7F;
  124. }
  125. time->tm_sec = bcd2bin(buf[COMMON_SEC]);
  126. time->tm_min = bcd2bin(buf[COMMON_MIN]);
  127. time->tm_hour = bcd2bin(buf[COMMON_HOUR]);
  128. if(h12) {
  129. time->tm_hour += 12;
  130. }
  131. time->tm_mday = bcd2bin(buf[COMMON_DATE]);
  132. time->tm_wday = bcd2bin(buf[COMMON_DAY]);
  133. time->tm_mon = bcd2bin(buf[COMMON_MONTH]) - 1 ;
  134. if (century) {
  135. time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
  136. } else {
  137. time->tm_year = bcd2bin(buf[COMMON_YEAR]);
  138. }
  139. return 0;
  140. }
  141. static int ds13307_set_time(struct device *dev, struct rtc_time *time) {
  142. struct i2c_client *client;
  143. int r = 0, i;
  144. u8 buf[7], addrs[7];
  145. client = to_i2c_client(dev);
  146. if (ds13307_start_oscillator(client)) {
  147. printk(KERN_ERR "%s: failed to initialize the oscillator\n", M_NAME);
  148. return -EIO;
  149. }
  150. buf[0] = bin2bcd(time->tm_sec);
  151. buf[1] = bin2bcd(time->tm_min);
  152. buf[2] = bin2bcd(time->tm_hour);
  153. buf[3] = bin2bcd(time->tm_mday);
  154. if ((model_detected == DEVICE_DS1337) && (time->tm_year >= 100)) {
  155. buf[4] = bin2bcd(time->tm_mon + 1) | COMMON_HIGH_BIT;
  156. } else {
  157. buf[4] = bin2bcd(time->tm_mon + 1);
  158. }
  159. buf[5] = bin2bcd(time->tm_year % 100);
  160. buf[6] = bin2bcd(time->tm_wday);
  161. addrs[0] = COMMON_SEC;
  162. addrs[1] = COMMON_MIN;
  163. addrs[2] = COMMON_HOUR;
  164. addrs[3] = COMMON_DATE;
  165. addrs[4] = COMMON_MONTH;
  166. addrs[5] = COMMON_YEAR;
  167. addrs[6] = COMMON_DAY;
  168. for (i=0; i<6; i++) {
  169. r = ds13307_write_single_byte(client, &addrs[i], &buf[i]);
  170. if (r) {
  171. return -EIO;
  172. }
  173. }
  174. return 0;
  175. }
  176. static const struct rtc_class_ops ds13307_rtc_ops = {
  177. .read_time = ds13307_read_time,
  178. .set_time = ds13307_set_time
  179. };
  180. /* Performs the device detection to distinguish between the
  181. * DS1307 and DS1337 chips. Returns the defined device ID or
  182. * -1 on failure
  183. */
  184. static int ds13307_detect_device(struct i2c_client *client) {
  185. u8 data, addr = DS1307_MAX_ADDR;
  186. int result;
  187. result = ds13307_read_bytes(client, &addr, &data, 1);
  188. if (!result) {
  189. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  190. return DEVICE_DS1307;
  191. }
  192. addr = DS1337_CTL;
  193. result = ds13307_read_bytes(client, &addr, &data, 1);
  194. if (!result) {
  195. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  196. return DEVICE_DS1337;
  197. }
  198. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  199. M_NAME, client->addr);
  200. return -1;
  201. }
  202. static int ds13307_probe(struct i2c_client *client,
  203. const struct i2c_device_id *id) {
  204. struct rtc_device *rtc;
  205. model_detected = ds13307_detect_device(client);
  206. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  207. return -EIO;
  208. }
  209. rtc = devm_rtc_device_register(&client->dev, ds13307_driver.driver.name,
  210. &ds13307_rtc_ops, THIS_MODULE);
  211. if (IS_ERR(rtc)) {
  212. return PTR_ERR(rtc);
  213. }
  214. i2c_set_clientdata(client,rtc);
  215. return 0;
  216. }
  217. static struct i2c_device_id ds13307_idtable[] = {
  218. { "ds1307", 0 },
  219. { "ds1337", 0 }, {}
  220. };
  221. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  222. static struct of_device_id ds13307_of_match[] = {
  223. { .compatible = "dallas,ds1307" },
  224. { .compatible = "dallas,ds1337" },
  225. {}
  226. };
  227. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  228. static struct i2c_driver ds13307_driver = {
  229. .driver = {
  230. .name = M_NAME,
  231. .of_match_table = of_match_ptr(ds13307_of_match),
  232. .owner = THIS_MODULE
  233. },
  234. .id_table = ds13307_idtable,
  235. .probe = ds13307_probe,
  236. };
  237. module_i2c_driver(ds13307_driver);
  238. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  239. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  240. MODULE_LICENSE("GPL");