rtc-ds1672.c 5.6 KB

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