rtc-ds1672.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * An rtc/i2c driver for the Dallas DS1672
  3. * Copyright 2005 Alessandro Zummo
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/i2c.h>
  11. #include <linux/rtc.h>
  12. #define DRV_VERSION "0.2"
  13. /* Addresses to scan: none. This chip cannot be detected. */
  14. static unsigned short normal_i2c[] = { I2C_CLIENT_END };
  15. /* Insmod parameters */
  16. I2C_CLIENT_INSMOD;
  17. /* Registers */
  18. #define DS1672_REG_CNT_BASE 0
  19. #define DS1672_REG_CONTROL 4
  20. #define DS1672_REG_TRICKLE 5
  21. /* Prototypes */
  22. static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind);
  23. /*
  24. * In the routines that deal directly with the ds1672 hardware, we use
  25. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
  26. * Epoch is initialized as 2000. Time is set to UTC.
  27. */
  28. static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  29. {
  30. unsigned long time;
  31. unsigned char addr = DS1672_REG_CNT_BASE;
  32. unsigned char buf[4];
  33. struct i2c_msg msgs[] = {
  34. { client->addr, 0, 1, &addr }, /* setup read ptr */
  35. { client->addr, I2C_M_RD, 4, buf }, /* read date */
  36. };
  37. /* read date registers */
  38. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  39. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  40. return -EIO;
  41. }
  42. dev_dbg(&client->dev,
  43. "%s: raw read data - counters=%02x,%02x,%02x,%02x\n"
  44. __FUNCTION__,
  45. buf[0], buf[1], buf[2], buf[3]);
  46. time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  47. rtc_time_to_tm(time, tm);
  48. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  49. "mday=%d, mon=%d, year=%d, wday=%d\n",
  50. __FUNCTION__,
  51. tm->tm_sec, tm->tm_min, tm->tm_hour,
  52. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  53. return 0;
  54. }
  55. static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs)
  56. {
  57. int xfer;
  58. unsigned char buf[5];
  59. buf[0] = DS1672_REG_CNT_BASE;
  60. buf[1] = secs & 0x000000FF;
  61. buf[2] = (secs & 0x0000FF00) >> 8;
  62. buf[3] = (secs & 0x00FF0000) >> 16;
  63. buf[4] = (secs & 0xFF000000) >> 24;
  64. xfer = i2c_master_send(client, buf, 5);
  65. if (xfer != 5) {
  66. dev_err(&client->dev, "%s: send: %d\n", __FUNCTION__, xfer);
  67. return -EIO;
  68. }
  69. return 0;
  70. }
  71. static int ds1672_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  72. {
  73. unsigned long secs;
  74. dev_dbg(&client->dev,
  75. "%s: secs=%d, mins=%d, hours=%d, ",
  76. "mday=%d, mon=%d, year=%d, wday=%d\n",
  77. __FUNCTION__,
  78. tm->tm_sec, tm->tm_min, tm->tm_hour,
  79. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  80. rtc_tm_to_time(tm, &secs);
  81. return ds1672_set_mmss(client, secs);
  82. }
  83. static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm)
  84. {
  85. return ds1672_get_datetime(to_i2c_client(dev), tm);
  86. }
  87. static int ds1672_rtc_set_time(struct device *dev, struct rtc_time *tm)
  88. {
  89. return ds1672_set_datetime(to_i2c_client(dev), tm);
  90. }
  91. static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs)
  92. {
  93. return ds1672_set_mmss(to_i2c_client(dev), secs);
  94. }
  95. static struct rtc_class_ops ds1672_rtc_ops = {
  96. .read_time = ds1672_rtc_read_time,
  97. .set_time = ds1672_rtc_set_time,
  98. .set_mmss = ds1672_rtc_set_mmss,
  99. };
  100. static int ds1672_attach(struct i2c_adapter *adapter)
  101. {
  102. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  103. return i2c_probe(adapter, &addr_data, ds1672_probe);
  104. }
  105. static int ds1672_detach(struct i2c_client *client)
  106. {
  107. int err;
  108. struct rtc_device *rtc = i2c_get_clientdata(client);
  109. dev_dbg(&client->dev, "%s\n", __FUNCTION__);
  110. if (rtc)
  111. rtc_device_unregister(rtc);
  112. if ((err = i2c_detach_client(client)))
  113. return err;
  114. kfree(client);
  115. return 0;
  116. }
  117. static struct i2c_driver ds1672_driver = {
  118. .driver = {
  119. .name = "ds1672",
  120. },
  121. .id = I2C_DRIVERID_DS1672,
  122. .attach_adapter = &ds1672_attach,
  123. .detach_client = &ds1672_detach,
  124. };
  125. static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
  126. {
  127. int err = 0;
  128. struct i2c_client *client;
  129. struct rtc_device *rtc;
  130. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  131. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  132. err = -ENODEV;
  133. goto exit;
  134. }
  135. if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
  136. err = -ENOMEM;
  137. goto exit;
  138. }
  139. /* I2C client */
  140. client->addr = address;
  141. client->driver = &ds1672_driver;
  142. client->adapter = adapter;
  143. strlcpy(client->name, ds1672_driver.driver.name, I2C_NAME_SIZE);
  144. /* Inform the i2c layer */
  145. if ((err = i2c_attach_client(client)))
  146. goto exit_kfree;
  147. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  148. rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev,
  149. &ds1672_rtc_ops, THIS_MODULE);
  150. if (IS_ERR(rtc)) {
  151. err = PTR_ERR(rtc);
  152. dev_err(&client->dev,
  153. "unable to register the class device\n");
  154. goto exit_detach;
  155. }
  156. i2c_set_clientdata(client, rtc);
  157. return 0;
  158. exit_detach:
  159. i2c_detach_client(client);
  160. exit_kfree:
  161. kfree(client);
  162. exit:
  163. return err;
  164. }
  165. static int __init ds1672_init(void)
  166. {
  167. return i2c_add_driver(&ds1672_driver);
  168. }
  169. static void __exit ds1672_exit(void)
  170. {
  171. i2c_del_driver(&ds1672_driver);
  172. }
  173. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  174. MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
  175. MODULE_LICENSE("GPL");
  176. MODULE_VERSION(DRV_VERSION);
  177. module_init(ds1672_init);
  178. module_exit(ds1672_exit);