ds1682.c 7.0 KB

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