rtc-em3027.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An rtc/i2c driver for the EM Microelectronic EM3027
  4. * Copyright 2011 CompuLab, Ltd.
  5. *
  6. * Author: Mike Rapoport <mike@compulab.co.il>
  7. *
  8. * Based on rtc-ds1672.c by Alessandro Zummo <a.zummo@towertech.it>
  9. */
  10. #include <linux/i2c.h>
  11. #include <linux/rtc.h>
  12. #include <linux/bcd.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. /* Registers */
  16. #define EM3027_REG_ON_OFF_CTRL 0x00
  17. #define EM3027_REG_IRQ_CTRL 0x01
  18. #define EM3027_REG_IRQ_FLAGS 0x02
  19. #define EM3027_REG_STATUS 0x03
  20. #define EM3027_REG_RST_CTRL 0x04
  21. #define EM3027_REG_WATCH_SEC 0x08
  22. #define EM3027_REG_WATCH_MIN 0x09
  23. #define EM3027_REG_WATCH_HOUR 0x0a
  24. #define EM3027_REG_WATCH_DATE 0x0b
  25. #define EM3027_REG_WATCH_DAY 0x0c
  26. #define EM3027_REG_WATCH_MON 0x0d
  27. #define EM3027_REG_WATCH_YEAR 0x0e
  28. #define EM3027_REG_ALARM_SEC 0x10
  29. #define EM3027_REG_ALARM_MIN 0x11
  30. #define EM3027_REG_ALARM_HOUR 0x12
  31. #define EM3027_REG_ALARM_DATE 0x13
  32. #define EM3027_REG_ALARM_DAY 0x14
  33. #define EM3027_REG_ALARM_MON 0x15
  34. #define EM3027_REG_ALARM_YEAR 0x16
  35. static struct i2c_driver em3027_driver;
  36. static int em3027_get_time(struct device *dev, struct rtc_time *tm)
  37. {
  38. struct i2c_client *client = to_i2c_client(dev);
  39. unsigned char addr = EM3027_REG_WATCH_SEC;
  40. unsigned char buf[7];
  41. struct i2c_msg msgs[] = {
  42. {/* setup read addr */
  43. .addr = client->addr,
  44. .len = 1,
  45. .buf = &addr
  46. },
  47. {/* read time/date */
  48. .addr = client->addr,
  49. .flags = I2C_M_RD,
  50. .len = 7,
  51. .buf = buf
  52. },
  53. };
  54. /* read time/date registers */
  55. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  56. dev_err(&client->dev, "%s: read error\n", __func__);
  57. return -EIO;
  58. }
  59. tm->tm_sec = bcd2bin(buf[0]);
  60. tm->tm_min = bcd2bin(buf[1]);
  61. tm->tm_hour = bcd2bin(buf[2]);
  62. tm->tm_mday = bcd2bin(buf[3]);
  63. tm->tm_wday = bcd2bin(buf[4]);
  64. tm->tm_mon = bcd2bin(buf[5]) - 1;
  65. tm->tm_year = bcd2bin(buf[6]) + 100;
  66. return 0;
  67. }
  68. static int em3027_set_time(struct device *dev, struct rtc_time *tm)
  69. {
  70. struct i2c_client *client = to_i2c_client(dev);
  71. unsigned char buf[8];
  72. struct i2c_msg msg = {
  73. .addr = client->addr,
  74. .len = 8,
  75. .buf = buf, /* write time/date */
  76. };
  77. buf[0] = EM3027_REG_WATCH_SEC;
  78. buf[1] = bin2bcd(tm->tm_sec);
  79. buf[2] = bin2bcd(tm->tm_min);
  80. buf[3] = bin2bcd(tm->tm_hour);
  81. buf[4] = bin2bcd(tm->tm_mday);
  82. buf[5] = bin2bcd(tm->tm_wday);
  83. buf[6] = bin2bcd(tm->tm_mon + 1);
  84. buf[7] = bin2bcd(tm->tm_year % 100);
  85. /* write time/date registers */
  86. if ((i2c_transfer(client->adapter, &msg, 1)) != 1) {
  87. dev_err(&client->dev, "%s: write error\n", __func__);
  88. return -EIO;
  89. }
  90. return 0;
  91. }
  92. static const struct rtc_class_ops em3027_rtc_ops = {
  93. .read_time = em3027_get_time,
  94. .set_time = em3027_set_time,
  95. };
  96. static int em3027_probe(struct i2c_client *client,
  97. const struct i2c_device_id *id)
  98. {
  99. struct rtc_device *rtc;
  100. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  101. return -ENODEV;
  102. rtc = devm_rtc_device_register(&client->dev, em3027_driver.driver.name,
  103. &em3027_rtc_ops, THIS_MODULE);
  104. if (IS_ERR(rtc))
  105. return PTR_ERR(rtc);
  106. i2c_set_clientdata(client, rtc);
  107. return 0;
  108. }
  109. static const struct i2c_device_id em3027_id[] = {
  110. { "em3027", 0 },
  111. { }
  112. };
  113. MODULE_DEVICE_TABLE(i2c, em3027_id);
  114. #ifdef CONFIG_OF
  115. static const struct of_device_id em3027_of_match[] = {
  116. { .compatible = "emmicro,em3027", },
  117. {}
  118. };
  119. MODULE_DEVICE_TABLE(of, em3027_of_match);
  120. #endif
  121. static struct i2c_driver em3027_driver = {
  122. .driver = {
  123. .name = "rtc-em3027",
  124. .of_match_table = of_match_ptr(em3027_of_match),
  125. },
  126. .probe = &em3027_probe,
  127. .id_table = em3027_id,
  128. };
  129. module_i2c_driver(em3027_driver);
  130. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  131. MODULE_DESCRIPTION("EM Microelectronic EM3027 RTC driver");
  132. MODULE_LICENSE("GPL");