1
0

rtc-ds13307.c 11 KB

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