rtc-em3027.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. {client->addr, 0, 1, &addr}, /* setup read addr */
  45. {client->addr, I2C_M_RD, 7, buf}, /* read time/date */
  46. };
  47. /* read time/date registers */
  48. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  49. dev_err(&client->dev, "%s: read error\n", __func__);
  50. return -EIO;
  51. }
  52. tm->tm_sec = bcd2bin(buf[0]);
  53. tm->tm_min = bcd2bin(buf[1]);
  54. tm->tm_hour = bcd2bin(buf[2]);
  55. tm->tm_mday = bcd2bin(buf[3]);
  56. tm->tm_wday = bcd2bin(buf[4]);
  57. tm->tm_mon = bcd2bin(buf[5]);
  58. tm->tm_year = bcd2bin(buf[6]) + 100;
  59. return 0;
  60. }
  61. static int em3027_set_time(struct device *dev, struct rtc_time *tm)
  62. {
  63. struct i2c_client *client = to_i2c_client(dev);
  64. unsigned char buf[8];
  65. struct i2c_msg msg = {
  66. client->addr, 0, 8, buf, /* write time/date */
  67. };
  68. buf[0] = EM3027_REG_WATCH_SEC;
  69. buf[1] = bin2bcd(tm->tm_sec);
  70. buf[2] = bin2bcd(tm->tm_min);
  71. buf[3] = bin2bcd(tm->tm_hour);
  72. buf[4] = bin2bcd(tm->tm_mday);
  73. buf[5] = bin2bcd(tm->tm_wday);
  74. buf[6] = bin2bcd(tm->tm_mon);
  75. buf[7] = bin2bcd(tm->tm_year % 100);
  76. /* write time/date registers */
  77. if ((i2c_transfer(client->adapter, &msg, 1)) != 1) {
  78. dev_err(&client->dev, "%s: write error\n", __func__);
  79. return -EIO;
  80. }
  81. return 0;
  82. }
  83. static const struct rtc_class_ops em3027_rtc_ops = {
  84. .read_time = em3027_get_time,
  85. .set_time = em3027_set_time,
  86. };
  87. static int em3027_probe(struct i2c_client *client,
  88. const struct i2c_device_id *id)
  89. {
  90. struct rtc_device *rtc;
  91. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  92. return -ENODEV;
  93. rtc = rtc_device_register(em3027_driver.driver.name, &client->dev,
  94. &em3027_rtc_ops, THIS_MODULE);
  95. if (IS_ERR(rtc))
  96. return PTR_ERR(rtc);
  97. i2c_set_clientdata(client, rtc);
  98. return 0;
  99. }
  100. static int em3027_remove(struct i2c_client *client)
  101. {
  102. struct rtc_device *rtc = i2c_get_clientdata(client);
  103. if (rtc)
  104. rtc_device_unregister(rtc);
  105. return 0;
  106. }
  107. static struct i2c_device_id em3027_id[] = {
  108. { "em3027", 0 },
  109. { }
  110. };
  111. static struct i2c_driver em3027_driver = {
  112. .driver = {
  113. .name = "rtc-em3027",
  114. },
  115. .probe = &em3027_probe,
  116. .remove = &em3027_remove,
  117. .id_table = em3027_id,
  118. };
  119. module_i2c_driver(em3027_driver);
  120. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  121. MODULE_DESCRIPTION("EM Microelectronic EM3027 RTC driver");
  122. MODULE_LICENSE("GPL");