rtc-ds13307.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #include <linux/regmap.h>
  16. #include <linux/version.h>
  17. #define M_NAME "rtc-ds13307" /* Module name */
  18. #define DEVICE_DS1307 1
  19. #define DEVICE_DS1337 2
  20. #define COMMON_SEC 0x00
  21. #define COMMON_MIN 0x01
  22. #define COMMON_HOUR 0x02
  23. #define COMMON_DAY 0x03
  24. #define COMMON_DATE 0x04
  25. #define COMMON_MONTH 0x05
  26. #define COMMON_YEAR 0x06
  27. #define COMMON_HIGH_BIT 0x80
  28. #define DS1307_NVRAM_BASE 0x08
  29. #define DS1307_MAX_ADDR 0x3F
  30. #define DS1337_CTL 0x0E
  31. #define DS1337_STAT 0x0F
  32. #define DS1337_ALRM1 0x07
  33. #define IOCTL_DS13307_ALRM _IOR('p', 0x15, u8) /* ioctl to read alarm bit */
  34. static int model_detected;
  35. static struct i2c_driver ds13307_driver;
  36. static struct nvmem_config ds1307_nvmem;
  37. static struct regmap_config regmap_config = {
  38. .reg_bits = 8,
  39. .val_bits = 8,
  40. };
  41. /* Reads a specified number of bytes via i2c, returns 0 on success */
  42. static int ds13307_read_bytes(struct regmap *regmap,
  43. u8 *addr, u8 *bytes, int length) {
  44. int r;
  45. r = regmap_bulk_read(regmap, (*addr), bytes, length);
  46. if (r == length) {
  47. return 0;
  48. } else {
  49. return r;
  50. }
  51. }
  52. /* Writes a specified number of bytes via 2ic, returns 0 on success */
  53. static int ds13307_write_bytes(struct regmap *regmap,
  54. u8 *addr, u8 *bytes, int length) {
  55. int r;
  56. r = regmap_bulk_write(regmap, (*addr), bytes, length);
  57. if (r == length) {
  58. return 0;
  59. } else {
  60. return r;
  61. }
  62. }
  63. /* The oscillator is stopped for both chips when power is first applied,
  64. * therefore this function checks its status and clears the stop bit.
  65. */
  66. static int ds13307_start_oscillator(struct regmap *regmap) {
  67. u8 data, addr, buf;
  68. int r, v = 0;
  69. if (model_detected == DEVICE_DS1307) {
  70. addr = COMMON_SEC;
  71. } else {
  72. addr = DS1337_STAT;
  73. }
  74. r = ds13307_read_bytes(regmap, &addr, &data, 1);
  75. if (data & COMMON_HIGH_BIT) {
  76. buf = data ^ COMMON_HIGH_BIT;
  77. v = ds13307_write_bytes(regmap, &addr, &buf, 1);
  78. if (!v) {
  79. printk(KERN_DEBUG "%s: oscillator stop bit successfully cleared\n", M_NAME);
  80. }
  81. }
  82. if ((r<0) || (v)) {
  83. return -EIO;
  84. }
  85. return 0;
  86. }
  87. /* Checks if the hour value is set to 12h format and checks if it
  88. * was AM or PM, returns 0 if the value was not in 12h format
  89. * or the value was AM so no conversion is necessary
  90. */
  91. static int ds13307_check_12h_am_pm(u8 *byte) {
  92. if (*byte & 0x40) {
  93. (*byte) = *byte ^ 0x40;
  94. if (*byte & 0x20) {
  95. (*byte) = *byte ^ 0x20;
  96. return 1;
  97. }
  98. }
  99. return 0;
  100. }
  101. static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
  102. struct regmap *regmap;
  103. int r, century = 1, h12 = 0;
  104. u8 buf[7], stopbit;
  105. u8 addr = COMMON_SEC;
  106. regmap = dev_get_drvdata(dev);
  107. r = ds13307_read_bytes(regmap, &addr, buf, 7);
  108. if (model_detected == DEVICE_DS1337) {
  109. addr = DS1337_STAT;
  110. r = ds13307_read_bytes(regmap, &addr, &stopbit, 1);
  111. stopbit = stopbit & COMMON_HIGH_BIT;
  112. } else {
  113. stopbit = buf[COMMON_SEC] & COMMON_HIGH_BIT;
  114. }
  115. if (r) {
  116. return -EIO;
  117. }
  118. if (stopbit) {
  119. printk(KERN_ERR "%s: Oscillator stop bit is set, values read from rtc device cannot be trusted\n", M_NAME);
  120. return -EINVAL;
  121. }
  122. h12 = ds13307_check_12h_am_pm(&buf[COMMON_HOUR]);
  123. if (model_detected == DEVICE_DS1337) {
  124. century = buf[COMMON_MONTH] & COMMON_HIGH_BIT;
  125. buf[COMMON_MONTH] = buf[COMMON_MONTH] & 0x7F;
  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. if (century) {
  137. time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
  138. } else {
  139. time->tm_year = bcd2bin(buf[COMMON_YEAR]);
  140. }
  141. return 0;
  142. }
  143. static int ds13307_set_time(struct device *dev, struct rtc_time *time) {
  144. struct regmap *regmap;
  145. u8 buf[7], addr;
  146. regmap = dev_get_drvdata(dev);
  147. if (ds13307_start_oscillator(regmap)) {
  148. printk(KERN_ERR "%s: failed to initialize the oscillator\n", M_NAME);
  149. return -EIO;
  150. }
  151. addr = COMMON_SEC;
  152. buf[0] = bin2bcd(time->tm_sec);
  153. buf[1] = bin2bcd(time->tm_min);
  154. buf[2] = bin2bcd(time->tm_hour);
  155. buf[3] = bin2bcd(time->tm_wday);
  156. buf[4] = bin2bcd(time->tm_mday);
  157. if ((model_detected == DEVICE_DS1337) && (time->tm_year >= 100)) {
  158. buf[5] = bin2bcd(time->tm_mon + 1) | COMMON_HIGH_BIT;
  159. } else if ((model_detected == DEVICE_DS1307) && (time->tm_year < 100)) {
  160. printk(KERN_ERR "%s: device does not support century information, dates before 2000 are not possible\n", M_NAME);
  161. return -EINVAL;
  162. } else {
  163. buf[5] = bin2bcd(time->tm_mon + 1);
  164. }
  165. buf[6] = bin2bcd(time->tm_year % 100);
  166. if(ds13307_write_bytes(regmap, &addr, buf, 7)) {
  167. return -EIO;
  168. }
  169. return 0;
  170. }
  171. static int ds13307_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) {
  172. struct regmap *regmap;
  173. u8 buf[9], addr = DS1337_ALRM1;
  174. int h12;
  175. regmap = dev_get_drvdata(dev);
  176. if (!ds13307_read_bytes(regmap, &addr, buf, 9)) {
  177. // select for A1IE bit
  178. alarm->enabled = buf[7] & 0x01;
  179. // select for A1F bit
  180. alarm->pending = buf[8] & 0x01;
  181. alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  182. alarm->time.tm_min = bcd2bin(buf[1] & 0x7F);
  183. h12 = ds13307_check_12h_am_pm(&buf[2]);
  184. alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F);
  185. if(h12) {
  186. alarm->time.tm_hour += 12;
  187. }
  188. /* if bit is set to 1, it matches the day of the week,
  189. * otherwise the day of the month.
  190. */
  191. if (buf[3] & 0x40) {
  192. alarm->time.tm_wday = bcd2bin(buf[3] & 0x3F);
  193. } else {
  194. alarm->time.tm_mday = bcd2bin(buf[3] & 0x3F);
  195. }
  196. return 0;
  197. }
  198. return -EIO;
  199. }
  200. /* Sets the alarm to the values passed to the function */
  201. static int ds13307_write_alarm(struct device *dev, struct rtc_wkalrm *alarm) {
  202. struct regmap *regmap;
  203. u8 buf[9], addr = DS1337_ALRM1;
  204. regmap = dev_get_drvdata(dev);
  205. if(!ds13307_read_bytes(regmap, &addr, buf, 9)) {
  206. // enable or disable the alarm as requested
  207. if ((alarm->enabled && (!(buf[7] & 0x01))) || ((!alarm->enabled && (buf[7] & 0x01)))) {
  208. buf[7] ^= 0x01;
  209. }
  210. // disable alarm 2
  211. if (buf[7] & 0x02) {
  212. buf[7] ^= 0x02;
  213. }
  214. // clear the status bit
  215. if (buf[8] & 0x01) {
  216. buf[8] ^= 0x01;
  217. }
  218. buf[0] = bin2bcd(alarm->time.tm_sec);
  219. buf[1] = bin2bcd(alarm->time.tm_min);
  220. buf[2] = bin2bcd(alarm->time.tm_hour);
  221. buf[3] = bin2bcd(alarm->time.tm_mday);
  222. if(!ds13307_write_bytes(regmap, &addr, buf, 9)) {
  223. return 0;
  224. }
  225. }
  226. return -EIO;
  227. }
  228. static int ds13307_alarm_irq_enable(struct device *dev, unsigned int enabled) {
  229. struct regmap *regmap;
  230. u8 buf, addr = DS1337_CTL;
  231. regmap = dev_get_drvdata(dev);
  232. if (ds13307_read_bytes(regmap, &addr, &buf, 1)) {
  233. return -EIO;
  234. }
  235. if ((!enabled && (buf & 0x01)) || (enabled & (!(buf & 0x01)))) {
  236. buf ^= 0x01;
  237. }
  238. if(!ds13307_write_bytes(regmap, &addr, &buf, 1)) {
  239. return 0;
  240. }
  241. return -EIO;
  242. }
  243. /* Reads the alarm bit and copies it to userspace */
  244. static int ds13307_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) {
  245. struct regmap *regmap;
  246. u8 addr, buf;
  247. switch(cmd) {
  248. case IOCTL_DS13307_ALRM:
  249. addr = DS1337_STAT;
  250. regmap = dev_get_drvdata(dev);
  251. if (ds13307_read_bytes(regmap, &addr, &buf, 1)) {
  252. return -EIO;
  253. }
  254. buf &= 0x01;
  255. if (copy_to_user((void __user *)arg, &buf, sizeof(u8))) {
  256. return -EFAULT;
  257. }
  258. return 0;
  259. default:
  260. return -ENOIOCTLCMD;
  261. }
  262. }
  263. static struct rtc_class_ops ds13307_rtc_ops = {
  264. .read_time = ds13307_read_time,
  265. .set_time = ds13307_set_time
  266. };
  267. /* Performs the device detection to distinguish between the
  268. * DS1307 and DS1337 chips. Returns the defined device ID or
  269. * -1 on failure
  270. */
  271. static int ds13307_detect_device(struct regmap *regmap) {
  272. u8 data, addr = DS1307_MAX_ADDR;
  273. int result;
  274. result = ds13307_read_bytes(regmap, &addr, &data, 1);
  275. if (!result) {
  276. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  277. return DEVICE_DS1307;
  278. }
  279. addr = DS1337_CTL;
  280. result = ds13307_read_bytes(regmap, &addr, &data, 1);
  281. if (!result) {
  282. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  283. return DEVICE_DS1337;
  284. }
  285. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  286. M_NAME, 0x68);
  287. return -1;
  288. }
  289. static int ds13307_nvram_read(void *priv, unsigned int offset, void *buf, size_t count) {
  290. struct regmap *regmap = priv;
  291. return regmap_bulk_read(regmap, DS1307_NVRAM_BASE + offset, buf, count);
  292. }
  293. static int ds13307_nvram_write(void *priv, unsigned int offset, void *buf, size_t count) {
  294. struct regmap * regmap = priv;
  295. return regmap_bulk_write(regmap, DS1307_NVRAM_BASE + offset, buf, count);
  296. }
  297. static int ds13307_probe(struct i2c_client *client,
  298. const struct i2c_device_id *id) {
  299. struct rtc_device *rtc;
  300. int error;
  301. struct regmap * regmap;
  302. regmap = devm_regmap_init_i2c(client, &regmap_config);
  303. if (IS_ERR(regmap)) {
  304. return PTR_ERR(regmap);
  305. }
  306. model_detected = ds13307_detect_device(regmap);
  307. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  308. return -EIO;
  309. }
  310. rtc = devm_rtc_allocate_device(&client->dev);
  311. if (IS_ERR(rtc)) {
  312. return PTR_ERR(rtc);
  313. }
  314. rtc->uie_unsupported = 1;
  315. device_set_wakeup_capable(&client->dev, 0);
  316. client->irq = 0;
  317. if (model_detected == DEVICE_DS1307) {
  318. ds1307_nvmem.name = "ds1307_nvram";
  319. ds1307_nvmem.word_size = 1;
  320. ds1307_nvmem.size = 56;
  321. ds1307_nvmem.reg_read = ds13307_nvram_read;
  322. ds1307_nvmem.reg_write = ds13307_nvram_write;
  323. ds1307_nvmem.priv = regmap;
  324. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,17,0)
  325. rtc->nvmem_config = &ds1307_nvmem;
  326. #else
  327. rtc_nvmem_register(rtc, &ds1307_nvmem);
  328. #endif
  329. } else {
  330. ds13307_rtc_ops.read_alarm = ds13307_read_alarm;
  331. ds13307_rtc_ops.set_alarm = ds13307_write_alarm;
  332. ds13307_rtc_ops.alarm_irq_enable = ds13307_alarm_irq_enable;
  333. ds13307_rtc_ops.ioctl = ds13307_ioctl;
  334. }
  335. rtc->ops = &ds13307_rtc_ops;
  336. i2c_set_clientdata(client,rtc);
  337. dev_set_drvdata(&client->dev, regmap);
  338. error = rtc_register_device(rtc);
  339. return error ? error: 0;
  340. }
  341. static struct i2c_device_id ds13307_idtable[] = {
  342. { "ds1307", 0 },
  343. { "ds1337", 0 }, {}
  344. };
  345. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  346. static struct of_device_id ds13307_of_match[] = {
  347. { .compatible = "dallas,ds1307" },
  348. { .compatible = "dallas,ds1337" },
  349. {}
  350. };
  351. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  352. static struct i2c_driver ds13307_driver = {
  353. .driver = {
  354. .name = M_NAME,
  355. .of_match_table = of_match_ptr(ds13307_of_match),
  356. .owner = THIS_MODULE
  357. },
  358. .id_table = ds13307_idtable,
  359. .probe = ds13307_probe,
  360. };
  361. module_i2c_driver(ds13307_driver);
  362. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  363. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  364. MODULE_LICENSE("GPL");