rtc-ds13307.c 11 KB

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