rtc-ds13307.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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[9], addr = DS1337_ALRM1;
  193. int h12;
  194. client = to_i2c_client(dev);
  195. if (!ds13307_read_bytes(client, &addr, buf, 9)) {
  196. // select for A1IE bit
  197. alarm->enabled = buf[7] & 0x01;
  198. // select for A1F bit
  199. alarm->pending = buf[8] & 0x01;
  200. alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  201. alarm->time.tm_min = bcd2bin(buf[1] & 0x7F);
  202. h12 = ds13307_check_12h_am_pm(&buf[2]);
  203. alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F);
  204. if(h12) {
  205. alarm->time.tm_hour += 12;
  206. }
  207. /* if bit is set to 1, it matches the day of the week,
  208. * otherwise the day of the month.
  209. */
  210. if (buf[3] & 0x40) {
  211. alarm->time.tm_wday = bcd2bin(buf[3] & 0x3F);
  212. } else {
  213. alarm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
  214. }
  215. return 0;
  216. }
  217. return -EIO;
  218. }
  219. /* Sets the alarm to the values passed to the function */
  220. static int ds13307_write_alarm(struct device *dev, struct rtc_wkalrm *alarm) {
  221. struct i2c_client *client;
  222. u8 buf[9], addr = DS1337_ALRM1;
  223. client = to_i2c_client(dev);
  224. if(!ds13307_read_bytes(client, &addr, buf, 9)) {
  225. // enable or disable the alarm as requested
  226. if ((alarm->enabled && (!(buf[7] & 0x01))) || ((!alarm->enabled && (buf[7] & 0x01)))) {
  227. buf[7] ^= 0x01;
  228. }
  229. // disable alarm 2
  230. if (buf[7] & 0x02) {
  231. buf[7] ^= 0x02;
  232. }
  233. // clear the status bit
  234. if (buf[8] & 0x01) {
  235. buf[8] ^= 0x01;
  236. }
  237. buf[0] = bin2bcd(alarm->time.tm_sec);
  238. buf[1] = bin2bcd(alarm->time.tm_min);
  239. buf[2] = bin2bcd(alarm->time.tm_hour);
  240. buf[3] = bin2bcd(alarm->time.tm_mday);
  241. if(!ds13307_write_bytes(client, &addr, buf, 9)) {
  242. return 0;
  243. }
  244. }
  245. return -EIO;
  246. }
  247. static int ds13307_alarm_irq_enable(struct device *dev, unsigned int enabled) {
  248. struct i2c_client *client;
  249. u8 buf, addr = DS1337_CTL;
  250. client = to_i2c_client(dev);
  251. if (ds13307_read_bytes(client, &addr, &buf, 1)) {
  252. return -EIO;
  253. }
  254. if ((!enabled && (buf & 0x01)) || (enabled & (!(buf & 0x01)))) {
  255. buf ^= 0x01;
  256. }
  257. if(!ds13307_write_bytes(client, &addr, &buf, 1)) {
  258. return 0;
  259. }
  260. return -EIO;
  261. }
  262. static struct rtc_class_ops ds13307_rtc_ops = {
  263. .read_time = ds13307_read_time,
  264. .set_time = ds13307_set_time
  265. };
  266. /* Performs the device detection to distinguish between the
  267. * DS1307 and DS1337 chips. Returns the defined device ID or
  268. * -1 on failure
  269. */
  270. static int ds13307_detect_device(struct i2c_client *client) {
  271. u8 data, addr = DS1307_MAX_ADDR;
  272. int result;
  273. result = ds13307_read_bytes(client, &addr, &data, 1);
  274. if (!result) {
  275. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  276. return DEVICE_DS1307;
  277. }
  278. addr = DS1337_CTL;
  279. result = ds13307_read_bytes(client, &addr, &data, 1);
  280. if (!result) {
  281. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  282. return DEVICE_DS1337;
  283. }
  284. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  285. M_NAME, client->addr);
  286. return -1;
  287. }
  288. static int ds13307_nvram_read(void *priv, unsigned int offset, void *buf, size_t count) {
  289. struct i2c_client *client = priv;
  290. u8 addr = DS1307_NVRAM_BASE + offset;
  291. if (!ds13307_read_bytes(client, &addr, buf, count)) {
  292. return count;
  293. }
  294. return -EIO;
  295. }
  296. static int ds13307_nvram_write(void *priv, unsigned int offset, void *buf, size_t count) {
  297. struct i2c_client *client = priv;
  298. u8 addr = DS1307_NVRAM_BASE + offset;
  299. if (!ds13307_write_bytes(client, &addr, buf, count)) {
  300. return count;
  301. }
  302. return -EIO;
  303. }
  304. static int ds13307_probe(struct i2c_client *client,
  305. const struct i2c_device_id *id) {
  306. struct rtc_device *rtc;
  307. int error;
  308. model_detected = ds13307_detect_device(client);
  309. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  310. return -EIO;
  311. }
  312. rtc = devm_rtc_allocate_device(&client->dev);
  313. if (IS_ERR(rtc)) {
  314. return PTR_ERR(rtc);
  315. }
  316. rtc->uie_unsupported = 1;
  317. device_set_wakeup_capable(&client->dev, 0);
  318. client->irq = 0;
  319. if (model_detected == DEVICE_DS1307) {
  320. ds1307_nvmem.name = "ds1307_nvram";
  321. ds1307_nvmem.word_size = 1;
  322. ds1307_nvmem.size = 56;
  323. ds1307_nvmem.reg_read = ds13307_nvram_read;
  324. ds1307_nvmem.reg_write = ds13307_nvram_write;
  325. ds1307_nvmem.priv = client;
  326. rtc->nvmem_config = &ds1307_nvmem;
  327. } else {
  328. ds13307_rtc_ops.read_alarm = ds13307_read_alarm;
  329. ds13307_rtc_ops.set_alarm = ds13307_write_alarm;
  330. ds13307_rtc_ops.alarm_irq_enable = ds13307_alarm_irq_enable;
  331. }
  332. rtc->ops = &ds13307_rtc_ops;
  333. i2c_set_clientdata(client,rtc);
  334. error = rtc_register_device(rtc);
  335. return error ? error: 0;
  336. }
  337. static struct i2c_device_id ds13307_idtable[] = {
  338. { "ds1307", 0 },
  339. { "ds1337", 0 }, {}
  340. };
  341. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  342. static struct of_device_id ds13307_of_match[] = {
  343. { .compatible = "dallas,ds1307" },
  344. { .compatible = "dallas,ds1337" },
  345. {}
  346. };
  347. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  348. static struct i2c_driver ds13307_driver = {
  349. .driver = {
  350. .name = M_NAME,
  351. .of_match_table = of_match_ptr(ds13307_of_match),
  352. .owner = THIS_MODULE
  353. },
  354. .id_table = ds13307_idtable,
  355. .probe = ds13307_probe,
  356. };
  357. module_i2c_driver(ds13307_driver);
  358. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  359. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  360. MODULE_LICENSE("GPL");