rtc-em3027.c 3.8 KB

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