ds1682.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. __le32 val = 0;
  58. int rc;
  59. dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
  60. /* Read the register */
  61. rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
  62. (u8 *) & val);
  63. if (rc < 0)
  64. return -EIO;
  65. /* Special case: the 32 bit regs are time values with 1/4s
  66. * resolution, scale them up to milliseconds */
  67. if (sattr->nr == 4)
  68. return sprintf(buf, "%llu\n",
  69. ((unsigned long long)le32_to_cpu(val)) * 250);
  70. /* Format the output string and return # of bytes */
  71. return sprintf(buf, "%li\n", (long)le32_to_cpu(val));
  72. }
  73. static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
  74. const char *buf, size_t count)
  75. {
  76. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  77. struct i2c_client *client = to_i2c_client(dev);
  78. u64 val;
  79. __le32 val_le;
  80. int rc;
  81. dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
  82. /* Decode input */
  83. rc = kstrtoull(buf, 0, &val);
  84. if (rc < 0) {
  85. dev_dbg(dev, "input string not a number\n");
  86. return -EINVAL;
  87. }
  88. /* Special case: the 32 bit regs are time values with 1/4s
  89. * resolution, scale input down to quarter-seconds */
  90. if (sattr->nr == 4)
  91. do_div(val, 250);
  92. /* write out the value */
  93. val_le = cpu_to_le32(val);
  94. rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
  95. (u8 *) & val_le);
  96. if (rc < 0) {
  97. dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
  98. sattr->index, sattr->nr);
  99. return -EIO;
  100. }
  101. return count;
  102. }
  103. /*
  104. * Simple register attributes
  105. */
  106. static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
  107. ds1682_store, 4, DS1682_REG_ELAPSED);
  108. static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
  109. ds1682_store, 4, DS1682_REG_ALARM);
  110. static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
  111. ds1682_store, 2, DS1682_REG_EVT_CNTR);
  112. static const struct attribute_group ds1682_group = {
  113. .attrs = (struct attribute *[]) {
  114. &sensor_dev_attr_elapsed_time.dev_attr.attr,
  115. &sensor_dev_attr_alarm_time.dev_attr.attr,
  116. &sensor_dev_attr_event_count.dev_attr.attr,
  117. NULL,
  118. },
  119. };
  120. /*
  121. * User data attribute
  122. */
  123. static ssize_t ds1682_eeprom_read(struct file *filp, struct kobject *kobj,
  124. struct bin_attribute *attr,
  125. char *buf, loff_t off, size_t count)
  126. {
  127. struct i2c_client *client = kobj_to_i2c_client(kobj);
  128. int rc;
  129. dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
  130. buf, off, count);
  131. rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
  132. count, buf);
  133. if (rc < 0)
  134. return -EIO;
  135. return count;
  136. }
  137. static ssize_t ds1682_eeprom_write(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. dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
  143. buf, off, count);
  144. /* Write out to the device */
  145. if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
  146. count, buf) < 0)
  147. return -EIO;
  148. return count;
  149. }
  150. static struct bin_attribute ds1682_eeprom_attr = {
  151. .attr = {
  152. .name = "eeprom",
  153. .mode = S_IRUGO | S_IWUSR,
  154. },
  155. .size = DS1682_EEPROM_SIZE,
  156. .read = ds1682_eeprom_read,
  157. .write = ds1682_eeprom_write,
  158. };
  159. /*
  160. * Called when a ds1682 device is matched with this driver
  161. */
  162. static int ds1682_probe(struct i2c_client *client,
  163. const struct i2c_device_id *id)
  164. {
  165. int rc;
  166. if (!i2c_check_functionality(client->adapter,
  167. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  168. dev_err(&client->dev, "i2c bus does not support the ds1682\n");
  169. rc = -ENODEV;
  170. goto exit;
  171. }
  172. rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
  173. if (rc)
  174. goto exit;
  175. rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
  176. if (rc)
  177. goto exit_bin_attr;
  178. return 0;
  179. exit_bin_attr:
  180. sysfs_remove_group(&client->dev.kobj, &ds1682_group);
  181. exit:
  182. return rc;
  183. }
  184. static int ds1682_remove(struct i2c_client *client)
  185. {
  186. sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
  187. sysfs_remove_group(&client->dev.kobj, &ds1682_group);
  188. return 0;
  189. }
  190. static const struct i2c_device_id ds1682_id[] = {
  191. { "ds1682", 0 },
  192. { }
  193. };
  194. MODULE_DEVICE_TABLE(i2c, ds1682_id);
  195. static struct i2c_driver ds1682_driver = {
  196. .driver = {
  197. .name = "ds1682",
  198. },
  199. .probe = ds1682_probe,
  200. .remove = ds1682_remove,
  201. .id_table = ds1682_id,
  202. };
  203. module_i2c_driver(ds1682_driver);
  204. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  205. MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
  206. MODULE_LICENSE("GPL");