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