ds1682.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/nvmem-provider.h>
  34. #include <linux/sysfs.h>
  35. #include <linux/ctype.h>
  36. #include <linux/hwmon-sysfs.h>
  37. /* Device registers */
  38. #define DS1682_REG_CONFIG 0x00
  39. #define DS1682_REG_ALARM 0x01
  40. #define DS1682_REG_ELAPSED 0x05
  41. #define DS1682_REG_EVT_CNTR 0x09
  42. #define DS1682_REG_EEPROM 0x0b
  43. #define DS1682_REG_RESET 0x1d
  44. #define DS1682_REG_WRITE_DISABLE 0x1e
  45. #define DS1682_REG_WRITE_MEM_DISABLE 0x1f
  46. #define DS1682_EEPROM_SIZE 10
  47. /*
  48. * Generic counter attributes
  49. */
  50. static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr,
  51. char *buf)
  52. {
  53. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  54. struct i2c_client *client = to_i2c_client(dev);
  55. unsigned long long val, check;
  56. __le32 val_le = 0;
  57. int rc;
  58. dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
  59. /* Read the register */
  60. rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
  61. (u8 *)&val_le);
  62. if (rc < 0)
  63. return -EIO;
  64. val = le32_to_cpu(val_le);
  65. if (sattr->index == DS1682_REG_ELAPSED) {
  66. int retries = 5;
  67. /* Detect and retry when a tick occurs mid-read */
  68. do {
  69. rc = i2c_smbus_read_i2c_block_data(client, sattr->index,
  70. sattr->nr,
  71. (u8 *)&val_le);
  72. if (rc < 0 || retries <= 0)
  73. return -EIO;
  74. check = val;
  75. val = le32_to_cpu(val_le);
  76. retries--;
  77. } while (val != check && val != (check + 1));
  78. }
  79. /* Format the output string and return # of bytes
  80. * Special case: the 32 bit regs are time values with 1/4s
  81. * resolution, scale them up to milliseconds
  82. */
  83. return sprintf(buf, "%llu\n", (sattr->nr == 4) ? (val * 250) : val);
  84. }
  85. static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
  86. const char *buf, size_t count)
  87. {
  88. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  89. struct i2c_client *client = to_i2c_client(dev);
  90. u64 val;
  91. __le32 val_le;
  92. int rc;
  93. dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
  94. /* Decode input */
  95. rc = kstrtoull(buf, 0, &val);
  96. if (rc < 0) {
  97. dev_dbg(dev, "input string not a number\n");
  98. return -EINVAL;
  99. }
  100. /* Special case: the 32 bit regs are time values with 1/4s
  101. * resolution, scale input down to quarter-seconds */
  102. if (sattr->nr == 4)
  103. do_div(val, 250);
  104. /* write out the value */
  105. val_le = cpu_to_le32(val);
  106. rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
  107. (u8 *) & val_le);
  108. if (rc < 0) {
  109. dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
  110. sattr->index, sattr->nr);
  111. return -EIO;
  112. }
  113. return count;
  114. }
  115. /*
  116. * Simple register attributes
  117. */
  118. static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
  119. ds1682_store, 4, DS1682_REG_ELAPSED);
  120. static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
  121. ds1682_store, 4, DS1682_REG_ALARM);
  122. static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
  123. ds1682_store, 2, DS1682_REG_EVT_CNTR);
  124. static const struct attribute_group ds1682_group = {
  125. .attrs = (struct attribute *[]) {
  126. &sensor_dev_attr_elapsed_time.dev_attr.attr,
  127. &sensor_dev_attr_alarm_time.dev_attr.attr,
  128. &sensor_dev_attr_event_count.dev_attr.attr,
  129. NULL,
  130. },
  131. };
  132. /*
  133. * User data attribute
  134. */
  135. static ssize_t ds1682_eeprom_read(struct file *filp, struct kobject *kobj,
  136. struct bin_attribute *attr,
  137. char *buf, loff_t off, size_t count)
  138. {
  139. struct i2c_client *client = kobj_to_i2c_client(kobj);
  140. int rc;
  141. dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
  142. buf, off, count);
  143. rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
  144. count, buf);
  145. if (rc < 0)
  146. return -EIO;
  147. return count;
  148. }
  149. static ssize_t ds1682_eeprom_write(struct file *filp, struct kobject *kobj,
  150. struct bin_attribute *attr,
  151. char *buf, loff_t off, size_t count)
  152. {
  153. struct i2c_client *client = kobj_to_i2c_client(kobj);
  154. dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
  155. buf, off, count);
  156. /* Write out to the device */
  157. if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
  158. count, buf) < 0)
  159. return -EIO;
  160. return count;
  161. }
  162. static const struct bin_attribute ds1682_eeprom_attr = {
  163. .attr = {
  164. .name = "eeprom",
  165. .mode = S_IRUGO | S_IWUSR,
  166. },
  167. .size = DS1682_EEPROM_SIZE,
  168. .read = ds1682_eeprom_read,
  169. .write = ds1682_eeprom_write,
  170. };
  171. static int ds1682_nvmem_read(void *priv, unsigned int offset, void *val,
  172. size_t bytes)
  173. {
  174. struct i2c_client *client = priv;
  175. int ret;
  176. ret = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + offset,
  177. bytes, val);
  178. return ret < 0 ? ret : 0;
  179. }
  180. static int ds1682_nvmem_write(void *priv, unsigned int offset, void *val,
  181. size_t bytes)
  182. {
  183. struct i2c_client *client = priv;
  184. int ret;
  185. ret = i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + offset,
  186. bytes, val);
  187. return ret < 0 ? ret : 0;
  188. }
  189. /*
  190. * Called when a ds1682 device is matched with this driver
  191. */
  192. static int ds1682_probe(struct i2c_client *client)
  193. {
  194. struct nvmem_config config = {
  195. .dev = &client->dev,
  196. .owner = THIS_MODULE,
  197. .type = NVMEM_TYPE_EEPROM,
  198. .reg_read = ds1682_nvmem_read,
  199. .reg_write = ds1682_nvmem_write,
  200. .size = DS1682_EEPROM_SIZE,
  201. .priv = client,
  202. };
  203. struct nvmem_device *nvmem;
  204. int rc;
  205. if (!i2c_check_functionality(client->adapter,
  206. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  207. dev_err(&client->dev, "i2c bus does not support the ds1682\n");
  208. rc = -ENODEV;
  209. goto exit;
  210. }
  211. nvmem = devm_nvmem_register(&client->dev, &config);
  212. if (IS_ENABLED(CONFIG_NVMEM) && IS_ERR(nvmem))
  213. return PTR_ERR(nvmem);
  214. rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
  215. if (rc)
  216. goto exit;
  217. rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
  218. if (rc)
  219. goto exit_bin_attr;
  220. return 0;
  221. exit_bin_attr:
  222. sysfs_remove_group(&client->dev.kobj, &ds1682_group);
  223. exit:
  224. return rc;
  225. }
  226. static void ds1682_remove(struct i2c_client *client)
  227. {
  228. sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
  229. sysfs_remove_group(&client->dev.kobj, &ds1682_group);
  230. }
  231. static const struct i2c_device_id ds1682_id[] = {
  232. { "ds1682" },
  233. { }
  234. };
  235. MODULE_DEVICE_TABLE(i2c, ds1682_id);
  236. static const struct of_device_id ds1682_of_match[] = {
  237. { .compatible = "dallas,ds1682", },
  238. {}
  239. };
  240. MODULE_DEVICE_TABLE(of, ds1682_of_match);
  241. static struct i2c_driver ds1682_driver = {
  242. .driver = {
  243. .name = "ds1682",
  244. .of_match_table = ds1682_of_match,
  245. },
  246. .probe = ds1682_probe,
  247. .remove = ds1682_remove,
  248. .id_table = ds1682_id,
  249. };
  250. module_i2c_driver(ds1682_driver);
  251. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  252. MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
  253. MODULE_LICENSE("GPL");