rtc-ds13307.c 7.5 KB

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