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. /* 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;
  71. int r, v;
  72. unsigned char addr;
  73. char buf[2];
  74. if (model_detected == DEVICE_DS1307) {
  75. addr = COMMON_SEC;
  76. } else {
  77. addr = DS1337_STAT;
  78. }
  79. r = ds13307_read_bytes(client, &addr, &data, 1);
  80. if (data & COMMON_HIGH_BIT) {
  81. buf[0] = addr;
  82. buf[1] = data ^ COMMON_HIGH_BIT;
  83. v = i2c_master_send(client, buf, 2);
  84. if (v == 2) {
  85. printk(KERN_DEBUG "%s: oscillator stop bit successfully cleared\n", M_NAME);
  86. }
  87. } else {
  88. v = 2;
  89. }
  90. if ((r<0) || (v!=2)) {
  91. return -EIO;
  92. }
  93. return 0;
  94. }
  95. static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
  96. struct i2c_client *client;
  97. int r;
  98. int h12 = 0;
  99. u8 buf[7], stopbit;
  100. u8 addr = COMMON_SEC;
  101. client = to_i2c_client(dev);
  102. r = ds13307_read_bytes(client, &addr, buf, 7);
  103. if (model_detected == DEVICE_DS1337) {
  104. addr = DS1337_STAT;
  105. r = ds13307_read_bytes(client, &addr, &stopbit, 1);
  106. stopbit = stopbit & COMMON_HIGH_BIT;
  107. } else {
  108. stopbit = buf[COMMON_SEC] & COMMON_HIGH_BIT;
  109. }
  110. if (r) {
  111. return -EIO;
  112. }
  113. if (stopbit) {
  114. printk(KERN_ERR "%s: Oscillator stop bit is set, values read from rtc device cannot be trusted\n", M_NAME);
  115. return -EINVAL;
  116. }
  117. if (buf[COMMON_HOUR] & 0x40) {
  118. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x40;
  119. if (buf[COMMON_HOUR] & 0x20) {
  120. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x20;
  121. h12 = 1;
  122. }
  123. }
  124. if (model_detected == DEVICE_DS1337) {
  125. buf[COMMON_MONTH] = buf[COMMON_MONTH] ^ COMMON_HIGH_BIT;
  126. }
  127. time->tm_sec = bcd2bin(buf[COMMON_SEC]);
  128. time->tm_min = bcd2bin(buf[COMMON_MIN]);
  129. time->tm_hour = bcd2bin(buf[COMMON_HOUR]);
  130. if(h12) {
  131. time->tm_hour += 12;
  132. }
  133. time->tm_mday = bcd2bin(buf[COMMON_DATE]);
  134. time->tm_wday = bcd2bin(buf[COMMON_DAY]);
  135. time->tm_mon = bcd2bin(buf[COMMON_MONTH]) - 1 ;
  136. time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
  137. return 0;
  138. }
  139. static int ds13307_set_time(struct device *dev, struct rtc_time *time) {
  140. struct i2c_client *client;
  141. int r = 0, i;
  142. u8 buf[7], addrs[7];
  143. client = to_i2c_client(dev);
  144. if (ds13307_start_oscillator(client)) {
  145. printk(KERN_ERR "%s: failed to initialize the oscillator\n", M_NAME);
  146. return -EIO;
  147. }
  148. buf[0] = bin2bcd(time->tm_sec);
  149. buf[1] = bin2bcd(time->tm_min);
  150. buf[2] = bin2bcd(time->tm_hour);
  151. buf[3] = bin2bcd(time->tm_mday);
  152. if (model_detected == DEVICE_DS1337) {
  153. buf[4] = bin2bcd(time->tm_mon + 1) | COMMON_HIGH_BIT;
  154. } else {
  155. buf[4] = bin2bcd(time->tm_mon + 1);
  156. }
  157. buf[5] = bin2bcd(time->tm_year % 100);
  158. buf[6] = bin2bcd(time->tm_wday);
  159. addrs[0] = COMMON_SEC;
  160. addrs[1] = COMMON_MIN;
  161. addrs[2] = COMMON_HOUR;
  162. addrs[3] = COMMON_DATE;
  163. addrs[4] = COMMON_MONTH;
  164. addrs[5] = COMMON_YEAR;
  165. addrs[6] = COMMON_DAY;
  166. for (i=0; i<6; i++) {
  167. r = ds13307_write_single_byte(client, &addrs[i], &buf[i]);
  168. if (r) {
  169. return -EIO;
  170. }
  171. }
  172. return 0;
  173. }
  174. static const struct rtc_class_ops ds13307_rtc_ops = {
  175. .read_time = ds13307_read_time,
  176. .set_time = ds13307_set_time
  177. };
  178. /* Performs the device detection to distinguish between the
  179. * DS1307 and DS1337 chips. Returns the defined device ID or
  180. * -1 on failure
  181. */
  182. static int ds13307_detect_device(struct i2c_client *client) {
  183. u8 data;
  184. int result;
  185. unsigned char addr = DS1307_MAX_ADDR;
  186. result = ds13307_read_bytes(client, &addr, &data, 1);
  187. if (!result) {
  188. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  189. return DEVICE_DS1307;
  190. }
  191. addr = DS1337_CTL;
  192. result = ds13307_read_bytes(client, &addr, &data, 1);
  193. if (!result) {
  194. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  195. return DEVICE_DS1337;
  196. }
  197. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  198. M_NAME, client->addr);
  199. return -1;
  200. }
  201. static int ds13307_probe(struct i2c_client *client,
  202. const struct i2c_device_id *id) {
  203. struct rtc_device *rtc;
  204. model_detected = ds13307_detect_device(client);
  205. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  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");