rtc-ds13307.c 8.9 KB

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