rtc-ds1672.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * An rtc/i2c driver for the Dallas DS1672
  3. * Copyright 2005-06 Tower Technologies
  4. *
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/i2c.h>
  12. #include <linux/rtc.h>
  13. #include <linux/module.h>
  14. /* Registers */
  15. #define DS1672_REG_CNT_BASE 0
  16. #define DS1672_REG_CONTROL 4
  17. #define DS1672_REG_TRICKLE 5
  18. #define DS1672_REG_CONTROL_EOSC 0x80
  19. static struct i2c_driver ds1672_driver;
  20. /*
  21. * In the routines that deal directly with the ds1672 hardware, we use
  22. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
  23. * Epoch is initialized as 2000. Time is set to UTC.
  24. */
  25. static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  26. {
  27. unsigned long time;
  28. unsigned char addr = DS1672_REG_CNT_BASE;
  29. unsigned char buf[4];
  30. struct i2c_msg msgs[] = {
  31. {/* setup read ptr */
  32. .addr = client->addr,
  33. .len = 1,
  34. .buf = &addr
  35. },
  36. {/* read date */
  37. .addr = client->addr,
  38. .flags = I2C_M_RD,
  39. .len = 4,
  40. .buf = buf
  41. },
  42. };
  43. /* read date registers */
  44. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  45. dev_err(&client->dev, "%s: read error\n", __func__);
  46. return -EIO;
  47. }
  48. dev_dbg(&client->dev,
  49. "%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
  50. __func__, buf[0], buf[1], buf[2], buf[3]);
  51. time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  52. rtc_time_to_tm(time, tm);
  53. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  54. "mday=%d, mon=%d, year=%d, wday=%d\n",
  55. __func__, tm->tm_sec, tm->tm_min, tm->tm_hour,
  56. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  57. return 0;
  58. }
  59. static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs)
  60. {
  61. int xfer;
  62. unsigned char buf[6];
  63. buf[0] = DS1672_REG_CNT_BASE;
  64. buf[1] = secs & 0x000000FF;
  65. buf[2] = (secs & 0x0000FF00) >> 8;
  66. buf[3] = (secs & 0x00FF0000) >> 16;
  67. buf[4] = (secs & 0xFF000000) >> 24;
  68. buf[5] = 0; /* set control reg to enable counting */
  69. xfer = i2c_master_send(client, buf, 6);
  70. if (xfer != 6) {
  71. dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
  72. return -EIO;
  73. }
  74. return 0;
  75. }
  76. static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm)
  77. {
  78. return ds1672_get_datetime(to_i2c_client(dev), tm);
  79. }
  80. static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs)
  81. {
  82. return ds1672_set_mmss(to_i2c_client(dev), secs);
  83. }
  84. static int ds1672_get_control(struct i2c_client *client, u8 *status)
  85. {
  86. unsigned char addr = DS1672_REG_CONTROL;
  87. struct i2c_msg msgs[] = {
  88. {/* setup read ptr */
  89. .addr = client->addr,
  90. .len = 1,
  91. .buf = &addr
  92. },
  93. {/* read control */
  94. .addr = client->addr,
  95. .flags = I2C_M_RD,
  96. .len = 1,
  97. .buf = status
  98. },
  99. };
  100. /* read control register */
  101. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  102. dev_err(&client->dev, "%s: read error\n", __func__);
  103. return -EIO;
  104. }
  105. return 0;
  106. }
  107. /* following are the sysfs callback functions */
  108. static ssize_t show_control(struct device *dev, struct device_attribute *attr,
  109. char *buf)
  110. {
  111. struct i2c_client *client = to_i2c_client(dev);
  112. u8 control;
  113. int err;
  114. err = ds1672_get_control(client, &control);
  115. if (err)
  116. return err;
  117. return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC)
  118. ? "disabled" : "enabled");
  119. }
  120. static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
  121. static const struct rtc_class_ops ds1672_rtc_ops = {
  122. .read_time = ds1672_rtc_read_time,
  123. .set_mmss = ds1672_rtc_set_mmss,
  124. };
  125. static int ds1672_probe(struct i2c_client *client,
  126. const struct i2c_device_id *id)
  127. {
  128. int err = 0;
  129. u8 control;
  130. struct rtc_device *rtc;
  131. dev_dbg(&client->dev, "%s\n", __func__);
  132. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  133. return -ENODEV;
  134. rtc = devm_rtc_device_register(&client->dev, ds1672_driver.driver.name,
  135. &ds1672_rtc_ops, THIS_MODULE);
  136. if (IS_ERR(rtc))
  137. return PTR_ERR(rtc);
  138. i2c_set_clientdata(client, rtc);
  139. /* read control register */
  140. err = ds1672_get_control(client, &control);
  141. if (err) {
  142. dev_warn(&client->dev, "Unable to read the control register\n");
  143. }
  144. if (control & DS1672_REG_CONTROL_EOSC)
  145. dev_warn(&client->dev, "Oscillator not enabled. "
  146. "Set time to enable.\n");
  147. /* Register sysfs hooks */
  148. err = device_create_file(&client->dev, &dev_attr_control);
  149. if (err)
  150. dev_err(&client->dev, "Unable to create sysfs entry: %s\n",
  151. dev_attr_control.attr.name);
  152. return 0;
  153. }
  154. static struct i2c_device_id ds1672_id[] = {
  155. { "ds1672", 0 },
  156. { }
  157. };
  158. MODULE_DEVICE_TABLE(i2c, ds1672_id);
  159. static struct i2c_driver ds1672_driver = {
  160. .driver = {
  161. .name = "rtc-ds1672",
  162. },
  163. .probe = &ds1672_probe,
  164. .id_table = ds1672_id,
  165. };
  166. module_i2c_driver(ds1672_driver);
  167. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  168. MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
  169. MODULE_LICENSE("GPL");