rtc-ds1672.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_CONTROL_EOSC 0x80
  21. #define DS1672_REG_TRICKLE 5
  22. /* Prototypes */
  23. static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind);
  24. /*
  25. * In the routines that deal directly with the ds1672 hardware, we use
  26. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
  27. * Epoch is initialized as 2000. Time is set to UTC.
  28. */
  29. static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  30. {
  31. unsigned long time;
  32. unsigned char addr = DS1672_REG_CNT_BASE;
  33. unsigned char buf[4];
  34. struct i2c_msg msgs[] = {
  35. { client->addr, 0, 1, &addr }, /* setup read ptr */
  36. { client->addr, I2C_M_RD, 4, buf }, /* read date */
  37. };
  38. /* read date registers */
  39. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  40. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  41. return -EIO;
  42. }
  43. dev_dbg(&client->dev,
  44. "%s: raw read data - counters=%02x,%02x,%02x,%02x\n"
  45. __FUNCTION__,
  46. buf[0], buf[1], buf[2], buf[3]);
  47. time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  48. rtc_time_to_tm(time, tm);
  49. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  50. "mday=%d, mon=%d, year=%d, wday=%d\n",
  51. __FUNCTION__,
  52. tm->tm_sec, tm->tm_min, tm->tm_hour,
  53. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  54. return 0;
  55. }
  56. static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs)
  57. {
  58. int xfer;
  59. unsigned char buf[6];
  60. buf[0] = DS1672_REG_CNT_BASE;
  61. buf[1] = secs & 0x000000FF;
  62. buf[2] = (secs & 0x0000FF00) >> 8;
  63. buf[3] = (secs & 0x00FF0000) >> 16;
  64. buf[4] = (secs & 0xFF000000) >> 24;
  65. buf[5] = 0; /* set control reg to enable counting */
  66. xfer = i2c_master_send(client, buf, 6);
  67. if (xfer != 6) {
  68. dev_err(&client->dev, "%s: send: %d\n", __FUNCTION__, xfer);
  69. return -EIO;
  70. }
  71. return 0;
  72. }
  73. static int ds1672_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  74. {
  75. unsigned long secs;
  76. dev_dbg(&client->dev,
  77. "%s: secs=%d, mins=%d, hours=%d, ",
  78. "mday=%d, mon=%d, year=%d, wday=%d\n",
  79. __FUNCTION__,
  80. tm->tm_sec, tm->tm_min, tm->tm_hour,
  81. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  82. rtc_tm_to_time(tm, &secs);
  83. return ds1672_set_mmss(client, secs);
  84. }
  85. static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm)
  86. {
  87. return ds1672_get_datetime(to_i2c_client(dev), tm);
  88. }
  89. static int ds1672_rtc_set_time(struct device *dev, struct rtc_time *tm)
  90. {
  91. return ds1672_set_datetime(to_i2c_client(dev), tm);
  92. }
  93. static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs)
  94. {
  95. return ds1672_set_mmss(to_i2c_client(dev), secs);
  96. }
  97. static int ds1672_get_control(struct i2c_client *client, u8 *status)
  98. {
  99. unsigned char addr = DS1672_REG_CONTROL;
  100. struct i2c_msg msgs[] = {
  101. { client->addr, 0, 1, &addr }, /* setup read ptr */
  102. { client->addr, I2C_M_RD, 1, status }, /* read control */
  103. };
  104. /* read control register */
  105. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  106. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  107. return -EIO;
  108. }
  109. return 0;
  110. }
  111. /* following are the sysfs callback functions */
  112. static ssize_t show_control(struct device *dev, struct device_attribute *attr, char *buf)
  113. {
  114. struct i2c_client *client = to_i2c_client(dev);
  115. char *state = "enabled";
  116. u8 control;
  117. int err;
  118. err = ds1672_get_control(client, &control);
  119. if (err)
  120. return err;
  121. if (control & DS1672_REG_CONTROL_EOSC)
  122. state = "disabled";
  123. return sprintf(buf, "%s\n", state);
  124. }
  125. static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
  126. static struct rtc_class_ops ds1672_rtc_ops = {
  127. .read_time = ds1672_rtc_read_time,
  128. .set_time = ds1672_rtc_set_time,
  129. .set_mmss = ds1672_rtc_set_mmss,
  130. };
  131. static int ds1672_attach(struct i2c_adapter *adapter)
  132. {
  133. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  134. return i2c_probe(adapter, &addr_data, ds1672_probe);
  135. }
  136. static int ds1672_detach(struct i2c_client *client)
  137. {
  138. int err;
  139. struct rtc_device *rtc = i2c_get_clientdata(client);
  140. dev_dbg(&client->dev, "%s\n", __FUNCTION__);
  141. if (rtc)
  142. rtc_device_unregister(rtc);
  143. if ((err = i2c_detach_client(client)))
  144. return err;
  145. kfree(client);
  146. return 0;
  147. }
  148. static struct i2c_driver ds1672_driver = {
  149. .driver = {
  150. .name = "ds1672",
  151. },
  152. .id = I2C_DRIVERID_DS1672,
  153. .attach_adapter = &ds1672_attach,
  154. .detach_client = &ds1672_detach,
  155. };
  156. static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
  157. {
  158. int err = 0;
  159. u8 control;
  160. struct i2c_client *client;
  161. struct rtc_device *rtc;
  162. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  163. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  164. err = -ENODEV;
  165. goto exit;
  166. }
  167. if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
  168. err = -ENOMEM;
  169. goto exit;
  170. }
  171. /* I2C client */
  172. client->addr = address;
  173. client->driver = &ds1672_driver;
  174. client->adapter = adapter;
  175. strlcpy(client->name, ds1672_driver.driver.name, I2C_NAME_SIZE);
  176. /* Inform the i2c layer */
  177. if ((err = i2c_attach_client(client)))
  178. goto exit_kfree;
  179. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  180. rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev,
  181. &ds1672_rtc_ops, THIS_MODULE);
  182. if (IS_ERR(rtc)) {
  183. err = PTR_ERR(rtc);
  184. dev_err(&client->dev,
  185. "unable to register the class device\n");
  186. goto exit_detach;
  187. }
  188. i2c_set_clientdata(client, rtc);
  189. /* read control register */
  190. err = ds1672_get_control(client, &control);
  191. if (err) {
  192. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  193. goto exit_detach;
  194. }
  195. if (control & DS1672_REG_CONTROL_EOSC)
  196. dev_warn(&client->dev, "Oscillator not enabled. "
  197. "Set time to enable.\n");
  198. /* Register sysfs hooks */
  199. device_create_file(&client->dev, &dev_attr_control);
  200. return 0;
  201. exit_detach:
  202. i2c_detach_client(client);
  203. exit_kfree:
  204. kfree(client);
  205. exit:
  206. return err;
  207. }
  208. static int __init ds1672_init(void)
  209. {
  210. return i2c_add_driver(&ds1672_driver);
  211. }
  212. static void __exit ds1672_exit(void)
  213. {
  214. i2c_del_driver(&ds1672_driver);
  215. }
  216. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  217. MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
  218. MODULE_LICENSE("GPL");
  219. MODULE_VERSION(DRV_VERSION);
  220. module_init(ds1672_init);
  221. module_exit(ds1672_exit);