rtc-ds13307.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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;
  96. int h12 = 0;
  97. u8 buf[7], stopbit;
  98. u8 addr = COMMON_SEC;
  99. client = to_i2c_client(dev);
  100. r = ds13307_read_bytes(client, &addr, buf, 7);
  101. if (model_detected == DEVICE_DS1337) {
  102. addr = DS1337_STAT;
  103. r = ds13307_read_bytes(client, &addr, &stopbit, 1);
  104. stopbit = stopbit & COMMON_HIGH_BIT;
  105. } else {
  106. stopbit = buf[COMMON_SEC] & COMMON_HIGH_BIT;
  107. }
  108. if (r) {
  109. return -EIO;
  110. }
  111. if (stopbit) {
  112. printk(KERN_ERR "%s: Oscillator stop bit is set, values read from rtc device cannot be trusted\n", M_NAME);
  113. return -EINVAL;
  114. }
  115. if (buf[COMMON_HOUR] & 0x40) {
  116. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x40;
  117. if (buf[COMMON_HOUR] & 0x20) {
  118. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x20;
  119. h12 = 1;
  120. }
  121. }
  122. if (model_detected == DEVICE_DS1337) {
  123. buf[COMMON_MONTH] = buf[COMMON_MONTH] ^ COMMON_HIGH_BIT;
  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. time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
  135. return 0;
  136. }
  137. static int ds13307_set_time(struct device *dev, struct rtc_time *time) {
  138. struct i2c_client *client;
  139. int r = 0, i;
  140. u8 buf[7], addrs[7];
  141. client = to_i2c_client(dev);
  142. if (ds13307_start_oscillator(client)) {
  143. printk(KERN_ERR "%s: failed to initialize the oscillator\n", M_NAME);
  144. return -EIO;
  145. }
  146. buf[0] = bin2bcd(time->tm_sec);
  147. buf[1] = bin2bcd(time->tm_min);
  148. buf[2] = bin2bcd(time->tm_hour);
  149. buf[3] = bin2bcd(time->tm_mday);
  150. if (model_detected == DEVICE_DS1337) {
  151. buf[4] = bin2bcd(time->tm_mon + 1) | COMMON_HIGH_BIT;
  152. } else {
  153. buf[4] = bin2bcd(time->tm_mon + 1);
  154. }
  155. buf[5] = bin2bcd(time->tm_year % 100);
  156. buf[6] = bin2bcd(time->tm_wday);
  157. addrs[0] = COMMON_SEC;
  158. addrs[1] = COMMON_MIN;
  159. addrs[2] = COMMON_HOUR;
  160. addrs[3] = COMMON_DATE;
  161. addrs[4] = COMMON_MONTH;
  162. addrs[5] = COMMON_YEAR;
  163. addrs[6] = COMMON_DAY;
  164. for (i=0; i<6; i++) {
  165. r = ds13307_write_single_byte(client, &addrs[i], &buf[i]);
  166. if (r) {
  167. return -EIO;
  168. }
  169. }
  170. return 0;
  171. }
  172. static const struct rtc_class_ops ds13307_rtc_ops = {
  173. .read_time = ds13307_read_time,
  174. .set_time = ds13307_set_time
  175. };
  176. /* Performs the device detection to distinguish between the
  177. * DS1307 and DS1337 chips. Returns the defined device ID or
  178. * -1 on failure
  179. */
  180. static int ds13307_detect_device(struct i2c_client *client) {
  181. u8 data, addr = DS1307_MAX_ADDR;
  182. int result;
  183. result = ds13307_read_bytes(client, &addr, &data, 1);
  184. if (!result) {
  185. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  186. return DEVICE_DS1307;
  187. }
  188. addr = DS1337_CTL;
  189. result = ds13307_read_bytes(client, &addr, &data, 1);
  190. if (!result) {
  191. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  192. return DEVICE_DS1337;
  193. }
  194. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  195. M_NAME, client->addr);
  196. return -1;
  197. }
  198. static int ds13307_probe(struct i2c_client *client,
  199. const struct i2c_device_id *id) {
  200. struct rtc_device *rtc;
  201. model_detected = ds13307_detect_device(client);
  202. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  203. return -EIO;
  204. }
  205. rtc = devm_rtc_device_register(&client->dev, ds13307_driver.driver.name,
  206. &ds13307_rtc_ops, THIS_MODULE);
  207. if (IS_ERR(rtc)) {
  208. return PTR_ERR(rtc);
  209. }
  210. i2c_set_clientdata(client,rtc);
  211. return 0;
  212. }
  213. static struct i2c_device_id ds13307_idtable[] = {
  214. { "ds1307", 0 },
  215. { "ds1337", 0 }, {}
  216. };
  217. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  218. static struct of_device_id ds13307_of_match[] = {
  219. { .compatible = "dallas,ds1307" },
  220. { .compatible = "dallas,ds1337" },
  221. {}
  222. };
  223. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  224. static struct i2c_driver ds13307_driver = {
  225. .driver = {
  226. .name = M_NAME,
  227. .of_match_table = of_match_ptr(ds13307_of_match),
  228. .owner = THIS_MODULE
  229. },
  230. .id_table = ds13307_idtable,
  231. .probe = ds13307_probe,
  232. };
  233. module_i2c_driver(ds13307_driver);
  234. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  235. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  236. MODULE_LICENSE("GPL");