rtc-ds13307.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #define M_NAME "rtc-ds13307" /* Module name */
  14. #define DEVICE_DS1307 1
  15. #define DEVICE_DS1337 2
  16. #define DS1307_SEC 0x00
  17. #define DS1307_MAX_ADDR 0x3F
  18. #define DS1337_CTL 0x0E
  19. #define DS1337_STAT 0x0F
  20. static int model_detected;
  21. static const struct rtc_class_ops ds13307_rtc_ops = {
  22. };
  23. /* Reads a single byte via i2c, returns 0 on success */
  24. static int ds13307_read_single_byte(struct i2c_client *client,
  25. unsigned char *addr, u8 *byte) {
  26. int r;
  27. struct i2c_msg msgs[] = {
  28. {
  29. .addr = client->addr,
  30. .len = 1,
  31. .buf = addr
  32. },
  33. {
  34. .addr = client->addr,
  35. .flags = I2C_M_RD,
  36. .len = 1,
  37. .buf = byte
  38. }
  39. };
  40. r = i2c_transfer(client->adapter, msgs, 2);
  41. if (r == 2) {
  42. return 0;
  43. } else {
  44. return r;
  45. }
  46. }
  47. /* Performs the device detection to distinguish between the
  48. * DS1307 and DS1337 chips. Returns the defined device ID or
  49. * -1 on failure
  50. */
  51. static int ds13307_detect_device(struct i2c_client *client) {
  52. u8 data;
  53. int result;
  54. unsigned char addr = DS1307_MAX_ADDR;
  55. result = ds13307_read_single_byte(client, &addr, &data);
  56. if (!result) {
  57. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  58. return DEVICE_DS1307;
  59. }
  60. addr = DS1337_CTL;
  61. result = ds13307_read_single_byte(client, &addr, &data);
  62. if (!result) {
  63. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  64. return DEVICE_DS1337;
  65. }
  66. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  67. M_NAME, client->addr);
  68. return -1;
  69. }
  70. /* The oscillator is stopped for both chips when power is first applied,
  71. * therefore this function checks its status and clears the stop bit.
  72. */
  73. static int ds13307_start_oscillator(struct i2c_client *client) {
  74. u8 data;
  75. int r, v;
  76. unsigned char addr;
  77. char buf[2];
  78. if (model_detected == DEVICE_DS1307) {
  79. addr = DS1307_SEC;
  80. } else {
  81. addr = DS1337_STAT;
  82. }
  83. r = ds13307_read_single_byte(client, &addr, &data);
  84. if (data & 0x80) {
  85. buf[0] = addr;
  86. buf[1] = data ^ 0x80;
  87. v = i2c_master_send(client, buf, 2);
  88. } else {
  89. v = 2;
  90. }
  91. if ((r<0) || (v!=2)) {
  92. return -EIO;
  93. }
  94. return 0;
  95. }
  96. static int ds13307_probe(struct i2c_client *client,
  97. const struct i2c_device_id *id) {
  98. model_detected = ds13307_detect_device(client);
  99. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  100. return -EIO;
  101. }
  102. if (ds13307_start_oscillator(client)) {
  103. printk(KERN_ERR "%s: failed to initialize the oscillator\n", M_NAME);
  104. return -EIO;
  105. } else {
  106. printk(KERN_DEBUG "%s: oscillator stop bit successfully cleared\n", M_NAME);
  107. }
  108. return 0;
  109. }
  110. static struct i2c_device_id ds13307_idtable[] = {
  111. { "ds1307", 0 }, {}
  112. };
  113. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  114. static struct of_device_id ds13307_of_match[] = {
  115. { .compatible = "dallas,ds1307" },
  116. {}
  117. };
  118. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  119. static struct i2c_driver ds13307_driver = {
  120. .driver = {
  121. .name = "rtc-ds13307",
  122. .of_match_table = of_match_ptr(ds13307_of_match),
  123. },
  124. .id_table = ds13307_idtable,
  125. .probe = ds13307_probe,
  126. };
  127. module_i2c_driver(ds13307_driver);
  128. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  129. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  130. MODULE_LICENSE("GPL");