rtc-ds1672.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/rtc.h>
  14. #define DRV_VERSION "0.3"
  15. /* Addresses to scan: none. This chip cannot be detected. */
  16. static unsigned short normal_i2c[] = { I2C_CLIENT_END };
  17. /* Insmod parameters */
  18. I2C_CLIENT_INSMOD;
  19. /* Registers */
  20. #define DS1672_REG_CNT_BASE 0
  21. #define DS1672_REG_CONTROL 4
  22. #define DS1672_REG_TRICKLE 5
  23. #define DS1672_REG_CONTROL_EOSC 0x80
  24. /* Prototypes */
  25. static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind);
  26. /*
  27. * In the routines that deal directly with the ds1672 hardware, we use
  28. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
  29. * Epoch is initialized as 2000. Time is set to UTC.
  30. */
  31. static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  32. {
  33. unsigned long time;
  34. unsigned char addr = DS1672_REG_CNT_BASE;
  35. unsigned char buf[4];
  36. struct i2c_msg msgs[] = {
  37. { client->addr, 0, 1, &addr }, /* setup read ptr */
  38. { client->addr, I2C_M_RD, 4, buf }, /* read date */
  39. };
  40. /* read date registers */
  41. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  42. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  43. return -EIO;
  44. }
  45. dev_dbg(&client->dev,
  46. "%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
  47. __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
  48. time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  49. rtc_time_to_tm(time, tm);
  50. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  51. "mday=%d, mon=%d, year=%d, wday=%d\n",
  52. __FUNCTION__, 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. u8 control;
  116. int err;
  117. err = ds1672_get_control(client, &control);
  118. if (err)
  119. return err;
  120. return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC)
  121. ? "disabled" : "enabled");
  122. }
  123. static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
  124. static const struct rtc_class_ops ds1672_rtc_ops = {
  125. .read_time = ds1672_rtc_read_time,
  126. .set_time = ds1672_rtc_set_time,
  127. .set_mmss = ds1672_rtc_set_mmss,
  128. };
  129. static int ds1672_attach(struct i2c_adapter *adapter)
  130. {
  131. return i2c_probe(adapter, &addr_data, ds1672_probe);
  132. }
  133. static int ds1672_detach(struct i2c_client *client)
  134. {
  135. int err;
  136. struct rtc_device *rtc = i2c_get_clientdata(client);
  137. if (rtc)
  138. rtc_device_unregister(rtc);
  139. if ((err = i2c_detach_client(client)))
  140. return err;
  141. kfree(client);
  142. return 0;
  143. }
  144. static struct i2c_driver ds1672_driver = {
  145. .driver = {
  146. .name = "ds1672",
  147. },
  148. .id = I2C_DRIVERID_DS1672,
  149. .attach_adapter = &ds1672_attach,
  150. .detach_client = &ds1672_detach,
  151. };
  152. static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
  153. {
  154. int err = 0;
  155. u8 control;
  156. struct i2c_client *client;
  157. struct rtc_device *rtc;
  158. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  159. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  160. err = -ENODEV;
  161. goto exit;
  162. }
  163. if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
  164. err = -ENOMEM;
  165. goto exit;
  166. }
  167. /* I2C client */
  168. client->addr = address;
  169. client->driver = &ds1672_driver;
  170. client->adapter = adapter;
  171. strlcpy(client->name, ds1672_driver.driver.name, I2C_NAME_SIZE);
  172. /* Inform the i2c layer */
  173. if ((err = i2c_attach_client(client)))
  174. goto exit_kfree;
  175. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  176. rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev,
  177. &ds1672_rtc_ops, THIS_MODULE);
  178. if (IS_ERR(rtc)) {
  179. err = PTR_ERR(rtc);
  180. goto exit_detach;
  181. }
  182. i2c_set_clientdata(client, rtc);
  183. /* read control register */
  184. err = ds1672_get_control(client, &control);
  185. if (err)
  186. goto exit_devreg;
  187. if (control & DS1672_REG_CONTROL_EOSC)
  188. dev_warn(&client->dev, "Oscillator not enabled. "
  189. "Set time to enable.\n");
  190. /* Register sysfs hooks */
  191. err = device_create_file(&client->dev, &dev_attr_control);
  192. if (err)
  193. goto exit_devreg;
  194. return 0;
  195. exit_devreg:
  196. rtc_device_unregister(rtc);
  197. exit_detach:
  198. i2c_detach_client(client);
  199. exit_kfree:
  200. kfree(client);
  201. exit:
  202. return err;
  203. }
  204. static int __init ds1672_init(void)
  205. {
  206. return i2c_add_driver(&ds1672_driver);
  207. }
  208. static void __exit ds1672_exit(void)
  209. {
  210. i2c_del_driver(&ds1672_driver);
  211. }
  212. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  213. MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
  214. MODULE_LICENSE("GPL");
  215. MODULE_VERSION(DRV_VERSION);
  216. module_init(ds1672_init);
  217. module_exit(ds1672_exit);