rtc-ds1672.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_rtc_read_time(struct device *dev, struct rtc_time *tm)
  68. {
  69. return ds1672_get_datetime(to_i2c_client(dev), tm);
  70. }
  71. static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs)
  72. {
  73. return ds1672_set_mmss(to_i2c_client(dev), secs);
  74. }
  75. static int ds1672_get_control(struct i2c_client *client, u8 *status)
  76. {
  77. unsigned char addr = DS1672_REG_CONTROL;
  78. struct i2c_msg msgs[] = {
  79. {client->addr, 0, 1, &addr}, /* setup read ptr */
  80. {client->addr, I2C_M_RD, 1, status}, /* read control */
  81. };
  82. /* read control register */
  83. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  84. dev_err(&client->dev, "%s: read error\n", __func__);
  85. return -EIO;
  86. }
  87. return 0;
  88. }
  89. /* following are the sysfs callback functions */
  90. static ssize_t show_control(struct device *dev, struct device_attribute *attr,
  91. char *buf)
  92. {
  93. struct i2c_client *client = to_i2c_client(dev);
  94. u8 control;
  95. int err;
  96. err = ds1672_get_control(client, &control);
  97. if (err)
  98. return err;
  99. return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC)
  100. ? "disabled" : "enabled");
  101. }
  102. static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
  103. static const struct rtc_class_ops ds1672_rtc_ops = {
  104. .read_time = ds1672_rtc_read_time,
  105. .set_mmss = ds1672_rtc_set_mmss,
  106. };
  107. static int ds1672_remove(struct i2c_client *client)
  108. {
  109. struct rtc_device *rtc = i2c_get_clientdata(client);
  110. if (rtc)
  111. rtc_device_unregister(rtc);
  112. return 0;
  113. }
  114. static int ds1672_probe(struct i2c_client *client,
  115. const struct i2c_device_id *id)
  116. {
  117. int err = 0;
  118. u8 control;
  119. struct rtc_device *rtc;
  120. dev_dbg(&client->dev, "%s\n", __func__);
  121. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  122. return -ENODEV;
  123. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  124. rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev,
  125. &ds1672_rtc_ops, THIS_MODULE);
  126. if (IS_ERR(rtc))
  127. return PTR_ERR(rtc);
  128. i2c_set_clientdata(client, rtc);
  129. /* read control register */
  130. err = ds1672_get_control(client, &control);
  131. if (err)
  132. goto exit_devreg;
  133. if (control & DS1672_REG_CONTROL_EOSC)
  134. dev_warn(&client->dev, "Oscillator not enabled. "
  135. "Set time to enable.\n");
  136. /* Register sysfs hooks */
  137. err = device_create_file(&client->dev, &dev_attr_control);
  138. if (err)
  139. goto exit_devreg;
  140. return 0;
  141. exit_devreg:
  142. rtc_device_unregister(rtc);
  143. return err;
  144. }
  145. static struct i2c_device_id ds1672_id[] = {
  146. { "ds1672", 0 },
  147. { }
  148. };
  149. static struct i2c_driver ds1672_driver = {
  150. .driver = {
  151. .name = "rtc-ds1672",
  152. },
  153. .probe = &ds1672_probe,
  154. .remove = &ds1672_remove,
  155. .id_table = ds1672_id,
  156. };
  157. static int __init ds1672_init(void)
  158. {
  159. return i2c_add_driver(&ds1672_driver);
  160. }
  161. static void __exit ds1672_exit(void)
  162. {
  163. i2c_del_driver(&ds1672_driver);
  164. }
  165. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  166. MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
  167. MODULE_LICENSE("GPL");
  168. MODULE_VERSION(DRV_VERSION);
  169. module_init(ds1672_init);
  170. module_exit(ds1672_exit);