1
0

rtc-ds13307.c 6.4 KB

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