ds1682.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Dallas Semiconductor DS1682 Elapsed Time Recorder device driver
  3. *
  4. * Written by: Grant Likely <grant.likely@secretlab.ca>
  5. *
  6. * Copyright (C) 2007 Secret Lab Technologies Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * The DS1682 elapsed timer recorder is a simple device that implements
  14. * one elapsed time counter, one event counter, an alarm signal and 10
  15. * bytes of general purpose EEPROM.
  16. *
  17. * This driver provides access to the DS1682 counters and user data via
  18. * the sysfs. The following attributes are added to the device node:
  19. * elapsed_time (u32): Total elapsed event time in ms resolution
  20. * alarm_time (u32): When elapsed time exceeds the value in alarm_time,
  21. * then the alarm pin is asserted.
  22. * event_count (u16): number of times the event pin has gone low.
  23. * eeprom (u8[10]): general purpose EEPROM
  24. *
  25. * Counter registers and user data are both read/write unless the device
  26. * has been write protected. This driver does not support turning off write
  27. * protection. Once write protection is turned on, it is impossible to
  28. * turn it off again, so I have left the feature out of this driver to avoid
  29. * accidental enabling, but it is trivial to add write protect support.
  30. *
  31. */
  32. #include <linux/module.h>
  33. #include <linux/i2c.h>
  34. #include <linux/string.h>
  35. #include <linux/list.h>
  36. #include <linux/sysfs.h>
  37. #include <linux/ctype.h>
  38. #include <linux/hwmon-sysfs.h>
  39. /* Device registers */
  40. #define DS1682_REG_CONFIG 0x00
  41. #define DS1682_REG_ALARM 0x01
  42. #define DS1682_REG_ELAPSED 0x05
  43. #define DS1682_REG_EVT_CNTR 0x09
  44. #define DS1682_REG_EEPROM 0x0b
  45. #define DS1682_REG_RESET 0x1d
  46. #define DS1682_REG_WRITE_DISABLE 0x1e
  47. #define DS1682_REG_WRITE_MEM_DISABLE 0x1f
  48. #define DS1682_EEPROM_SIZE 10
  49. /*
  50. * Generic counter attributes
  51. */
  52. static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr,
  53. char *buf)
  54. {
  55. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  56. struct i2c_client *client = to_i2c_client(dev);
  57. unsigned long long val, check;
  58. __le32 val_le = 0;
  59. int rc;
  60. dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
  61. /* Read the register */
  62. rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
  63. (u8 *)&val_le);
  64. if (rc < 0)
  65. return -EIO;
  66. val = le32_to_cpu(val_le);
  67. if (sattr->index == DS1682_REG_ELAPSED) {
  68. int retries = 5;
  69. /* Detect and retry when a tick occurs mid-read */
  70. do {
  71. rc = i2c_smbus_read_i2c_block_data(client, sattr->index,
  72. sattr->nr,
  73. (u8 *)&val_le);
  74. if (rc < 0 || retries <= 0)
  75. return -EIO;
  76. check = val;
  77. val = le32_to_cpu(val_le);
  78. retries--;
  79. } while (val != check && val != (check + 1));
  80. }
  81. /* Format the output string and return # of bytes
  82. * Special case: the 32 bit regs are time values with 1/4s
  83. * resolution, scale them up to milliseconds
  84. */
  85. return sprintf(buf, "%llu\n", (sattr->nr == 4) ? (val * 250) : val);
  86. }
  87. static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
  88. const char *buf, size_t count)
  89. {
  90. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  91. struct i2c_client *client = to_i2c_client(dev);
  92. u64 val;
  93. __le32 val_le;
  94. int rc;
  95. dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
  96. /* Decode input */
  97. rc = kstrtoull(buf, 0, &val);
  98. if (rc < 0) {
  99. dev_dbg(dev, "input string not a number\n");
  100. return -EINVAL;
  101. }
  102. /* Special case: the 32 bit regs are time values with 1/4s
  103. * resolution, scale input down to quarter-seconds */
  104. if (sattr->nr == 4)
  105. do_div(val, 250);
  106. /* write out the value */
  107. val_le = cpu_to_le32(val);
  108. rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
  109. (u8 *) & val_le);
  110. if (rc < 0) {
  111. dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
  112. sattr->index, sattr->nr);
  113. return -EIO;
  114. }
  115. return count;
  116. }
  117. /*
  118. * Simple register attributes
  119. */
  120. static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
  121. ds1682_store, 4, DS1682_REG_ELAPSED);
  122. static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
  123. ds1682_store, 4, DS1682_REG_ALARM);
  124. static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
  125. ds1682_store, 2, DS1682_REG_EVT_CNTR);
  126. static const struct attribute_group ds1682_group = {
  127. .attrs = (struct attribute *[]) {
  128. &sensor_dev_attr_elapsed_time.dev_attr.attr,
  129. &sensor_dev_attr_alarm_time.dev_attr.attr,
  130. &sensor_dev_attr_event_count.dev_attr.attr,
  131. NULL,
  132. },
  133. };
  134. /*
  135. * User data attribute
  136. */
  137. static ssize_t ds1682_eeprom_read(struct file *filp, struct kobject *kobj,
  138. struct bin_attribute *attr,
  139. char *buf, loff_t off, size_t count)
  140. {
  141. struct i2c_client *client = kobj_to_i2c_client(kobj);
  142. int rc;
  143. dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
  144. buf, off, count);
  145. rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
  146. count, buf);
  147. if (rc < 0)
  148. return -EIO;
  149. return count;
  150. }
  151. static ssize_t ds1682_eeprom_write(struct file *filp, struct kobject *kobj,
  152. struct bin_attribute *attr,
  153. char *buf, loff_t off, size_t count)
  154. {
  155. struct i2c_client *client = kobj_to_i2c_client(kobj);
  156. dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
  157. buf, off, count);
  158. /* Write out to the device */
  159. if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
  160. count, buf) < 0)
  161. return -EIO;
  162. return count;
  163. }
  164. static const struct bin_attribute ds1682_eeprom_attr = {
  165. .attr = {
  166. .name = "eeprom",
  167. .mode = S_IRUGO | S_IWUSR,
  168. },
  169. .size = DS1682_EEPROM_SIZE,
  170. .read = ds1682_eeprom_read,
  171. .write = ds1682_eeprom_write,
  172. };
  173. /*
  174. * Called when a ds1682 device is matched with this driver
  175. */
  176. static int ds1682_probe(struct i2c_client *client,
  177. const struct i2c_device_id *id)
  178. {
  179. int rc;
  180. if (!i2c_check_functionality(client->adapter,
  181. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  182. dev_err(&client->dev, "i2c bus does not support the ds1682\n");
  183. rc = -ENODEV;
  184. goto exit;
  185. }
  186. rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
  187. if (rc)
  188. goto exit;
  189. rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
  190. if (rc)
  191. goto exit_bin_attr;
  192. return 0;
  193. exit_bin_attr:
  194. sysfs_remove_group(&client->dev.kobj, &ds1682_group);
  195. exit:
  196. return rc;
  197. }
  198. static int ds1682_remove(struct i2c_client *client)
  199. {
  200. sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
  201. sysfs_remove_group(&client->dev.kobj, &ds1682_group);
  202. return 0;
  203. }
  204. static const struct i2c_device_id ds1682_id[] = {
  205. { "ds1682", 0 },
  206. { }
  207. };
  208. MODULE_DEVICE_TABLE(i2c, ds1682_id);
  209. static const struct of_device_id ds1682_of_match[] = {
  210. { .compatible = "dallas,ds1682", },
  211. {}
  212. };
  213. MODULE_DEVICE_TABLE(of, ds1682_of_match);
  214. static struct i2c_driver ds1682_driver = {
  215. .driver = {
  216. .name = "ds1682",
  217. .of_match_table = ds1682_of_match,
  218. },
  219. .probe = ds1682_probe,
  220. .remove = ds1682_remove,
  221. .id_table = ds1682_id,
  222. };
  223. module_i2c_driver(ds1682_driver);
  224. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  225. MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
  226. MODULE_LICENSE("GPL");