rtc-ds13307.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * rtc-ds13307.c - RTC driver for the DS1307 and DS13307 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. #define M_NAME "rtc-ds13307" /* Module name */
  15. #define DEVICE_DS1307 1
  16. #define DEVICE_DS1337 2
  17. #define COMMON_SEC 0x00
  18. #define COMMON_MIN 0x01
  19. #define COMMON_HOUR 0x02
  20. #define COMMON_DAY 0x03
  21. #define COMMON_DATE 0x04
  22. #define COMMON_MONTH 0x05
  23. #define COMMON_YEAR 0x06
  24. #define COMMON_HIGH_BIT 0x80
  25. #define DS1307_MAX_ADDR 0x3F
  26. #define DS1337_HOUR 0x02
  27. #define DS1337_DATE 0x03
  28. #define DS1337_CTL 0x0E
  29. #define DS1337_STAT 0x0F
  30. static int model_detected;
  31. static struct i2c_driver ds13307_driver;
  32. /* Reads a specified number of bytes via i2c, returns 0 on success */
  33. static int ds13307_read_bytes(struct i2c_client *client,
  34. unsigned char *addr, u8 *byte, int length) {
  35. int r;
  36. struct i2c_msg msgs[] = {
  37. {
  38. .addr = client->addr,
  39. .len = 1,
  40. .buf = addr
  41. },
  42. {
  43. .addr = client->addr,
  44. .flags = I2C_M_RD,
  45. .len = length,
  46. .buf = byte
  47. }
  48. };
  49. r = i2c_transfer(client->adapter, msgs, 2);
  50. if (r == 2) {
  51. return 0;
  52. } else {
  53. return r;
  54. }
  55. }
  56. static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
  57. struct i2c_client *client;
  58. int r;
  59. int h12 = 0;
  60. u8 buf[7], stopbit;
  61. unsigned char addr = COMMON_SEC;
  62. client = to_i2c_client(dev);
  63. r = ds13307_read_bytes(client, &addr, buf, 7);
  64. if (model_detected == DEVICE_DS1337) {
  65. addr = DS1337_STAT;
  66. r = ds13307_read_bytes(client, &addr, &stopbit, 1);
  67. stopbit = stopbit & COMMON_HIGH_BIT;
  68. } else {
  69. stopbit = buf[COMMON_SEC] & 0x40;
  70. }
  71. if (r) {
  72. return -EIO;
  73. }
  74. if (stopbit) {
  75. printk(KERN_ERR "%s: Oscillator stop bit is set, values read from rtc device cannot be trusted\n", M_NAME);
  76. return -EINVAL;
  77. }
  78. if (buf[COMMON_HOUR] & 0x40) {
  79. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x40;
  80. if (buf[COMMON_HOUR] & 0x20) {
  81. buf[COMMON_HOUR] = buf[COMMON_HOUR] ^ 0x20;
  82. h12 = 1;
  83. }
  84. }
  85. if (model_detected == DEVICE_DS1337) {
  86. buf[COMMON_MONTH] = buf[COMMON_MONTH] ^ COMMON_HIGH_BIT;
  87. }
  88. time->tm_sec = bcd2bin(buf[COMMON_SEC]);
  89. time->tm_min = bcd2bin(buf[COMMON_MIN]);
  90. time->tm_hour = bcd2bin(buf[COMMON_HOUR]);
  91. if(h12) {
  92. time->tm_hour += 12;
  93. }
  94. time->tm_mday = bcd2bin(buf[COMMON_DATE]);
  95. time->tm_wday = bcd2bin(buf[COMMON_DAY]);
  96. time->tm_mon = bcd2bin(buf[COMMON_MONTH]) - 1 ;
  97. time->tm_year = bcd2bin(buf[COMMON_YEAR]) + 100;
  98. return 0;
  99. }
  100. static const struct rtc_class_ops ds13307_rtc_ops = {
  101. .read_time = ds13307_read_time,
  102. };
  103. /* Performs the device detection to distinguish between the
  104. * DS1307 and DS1337 chips. Returns the defined device ID or
  105. * -1 on failure
  106. */
  107. static int ds13307_detect_device(struct i2c_client *client) {
  108. u8 data;
  109. int result;
  110. unsigned char addr = DS1307_MAX_ADDR;
  111. result = ds13307_read_bytes(client, &addr, &data, 1);
  112. if (!result) {
  113. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  114. return DEVICE_DS1307;
  115. }
  116. addr = DS1337_CTL;
  117. result = ds13307_read_bytes(client, &addr, &data, 1);
  118. if (!result) {
  119. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  120. return DEVICE_DS1337;
  121. }
  122. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  123. M_NAME, client->addr);
  124. return -1;
  125. }
  126. /* The oscillator is stopped for both chips when power is first applied,
  127. * therefore this function checks its status and clears the stop bit.
  128. */
  129. static int ds13307_start_oscillator(struct i2c_client *client) {
  130. u8 data;
  131. int r, v;
  132. unsigned char addr;
  133. char buf[2];
  134. if (model_detected == DEVICE_DS1307) {
  135. addr = COMMON_SEC;
  136. } else {
  137. addr = DS1337_STAT;
  138. }
  139. r = ds13307_read_bytes(client, &addr, &data, 1);
  140. if (data & COMMON_HIGH_BIT) {
  141. buf[0] = addr;
  142. buf[1] = data ^ COMMON_HIGH_BIT;
  143. v = i2c_master_send(client, buf, 2);
  144. if (v == 2) {
  145. printk(KERN_DEBUG "%s: oscillator stop bit successfully cleared\n", M_NAME);
  146. }
  147. } else {
  148. v = 2;
  149. }
  150. if ((r<0) || (v!=2)) {
  151. return -EIO;
  152. }
  153. return 0;
  154. }
  155. static int ds13307_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id) {
  157. struct rtc_device *rtc;
  158. model_detected = ds13307_detect_device(client);
  159. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  160. return -EIO;
  161. }
  162. if (ds13307_start_oscillator(client)) {
  163. printk(KERN_ERR "%s: failed to initialize the oscillator\n", M_NAME);
  164. return -EIO;
  165. }
  166. rtc = devm_rtc_device_register(&client->dev, ds13307_driver.driver.name,
  167. &ds13307_rtc_ops, THIS_MODULE);
  168. if (IS_ERR(rtc)) {
  169. return PTR_ERR(rtc);
  170. }
  171. i2c_set_clientdata(client,rtc);
  172. return 0;
  173. }
  174. static struct i2c_device_id ds13307_idtable[] = {
  175. { "ds1307", 0 }, {}
  176. };
  177. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  178. static struct of_device_id ds13307_of_match[] = {
  179. { .compatible = "dallas,ds1307" },
  180. {}
  181. };
  182. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  183. static struct i2c_driver ds13307_driver = {
  184. .driver = {
  185. .name = "rtc-ds13307",
  186. .of_match_table = of_match_ptr(ds13307_of_match),
  187. .owner = THIS_MODULE
  188. },
  189. .id_table = ds13307_idtable,
  190. .probe = ds13307_probe,
  191. };
  192. module_i2c_driver(ds13307_driver);
  193. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  194. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  195. MODULE_LICENSE("GPL");