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