rtc-ds13307.c 11 KB

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