rtc-ds1672.c 4.9 KB

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