rtc-ds13307.c 6.9 KB

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