rtc-ds1672.c 5.0 KB

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