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