rtc-ds13307.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_MAX_ADDR 0x3F
  17. #define DS1337_CTL 0x0E
  18. static int model_detected;
  19. static const struct rtc_class_ops ds13307_rtc_ops = {
  20. };
  21. /* Reads a single byte via i2c, returns 0 on success */
  22. static int ds13307_read_single_byte(struct i2c_client *client,
  23. unsigned char *addr, u8 *byte) {
  24. int r;
  25. struct i2c_msg msgs[] = {
  26. {
  27. .addr = client->addr,
  28. .len = 1,
  29. .buf = addr
  30. },
  31. {
  32. .addr = client->addr,
  33. .flags = I2C_M_RD,
  34. .len = 1,
  35. .buf = byte
  36. }
  37. };
  38. r = i2c_transfer(client->adapter, msgs, 2);
  39. if (r == 2) {
  40. return 0;
  41. } else {
  42. return r;
  43. }
  44. }
  45. /* Performs the device detection to distinguish between the
  46. * DS1307 and DS1337 chips. Returns the defined device ID or
  47. * -1 on failure
  48. */
  49. static int ds13307_detect_device(struct i2c_client *client) {
  50. u8 data;
  51. int result;
  52. unsigned char addr = DS1307_MAX_ADDR;
  53. result = ds13307_read_single_byte(client, &addr, &data);
  54. if (!result) {
  55. printk(KERN_INFO "%s: Detected device DS1307\n", M_NAME);
  56. return DEVICE_DS1307;
  57. }
  58. addr = DS1337_CTL;
  59. result = ds13307_read_single_byte(client, &addr, &data);
  60. if (!result) {
  61. printk(KERN_INFO "%s: Detected device DS1337\n", M_NAME);
  62. return DEVICE_DS1337;
  63. }
  64. printk(KERN_ERR "%s: Could not talk to I2C device at addr %x, is it connected?\n",
  65. M_NAME, client->addr);
  66. return -1;
  67. }
  68. static int ds13307_probe(struct i2c_client *client,
  69. const struct i2c_device_id *id) {
  70. model_detected = ds13307_detect_device(client);
  71. if ((model_detected != DEVICE_DS1307) && (model_detected != DEVICE_DS1337)) {
  72. return -EIO;
  73. }
  74. return 0;
  75. }
  76. static struct i2c_device_id ds13307_idtable[] = {
  77. { "ds1307", 0 }, {}
  78. };
  79. MODULE_DEVICE_TABLE(i2c, ds13307_idtable);
  80. static struct of_device_id ds13307_of_match[] = {
  81. { .compatible = "dallas,ds1307" },
  82. {}
  83. };
  84. MODULE_DEVICE_TABLE(of, ds13307_of_match);
  85. static struct i2c_driver ds13307_driver = {
  86. .driver = {
  87. .name = "rtc-ds13307",
  88. .of_match_table = of_match_ptr(ds13307_of_match),
  89. },
  90. .id_table = ds13307_idtable,
  91. .probe = ds13307_probe,
  92. };
  93. module_i2c_driver(ds13307_driver);
  94. MODULE_AUTHOR("Helmut Pozimski <helmut@pozimski.eu>");
  95. MODULE_DESCRIPTION("DS1307 and DS1337 RTC driver");
  96. MODULE_LICENSE("GPL");