ds1682.c 6.8 KB

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