2 * Dallas Semiconductor DS1682 Elapsed Time Recorder device driver
4 * Written by: Grant Likely <grant.likely@secretlab.ca>
6 * Copyright (C) 2007 Secret Lab Technologies Ltd.
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.
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.
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
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.
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>
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
52 #define DS1682_EEPROM_SIZE 10
55 * Generic counter attributes
57 static ssize_t
ds1682_show(struct device
*dev
, struct device_attribute
*attr
,
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
;
66 dev_dbg(dev
, "ds1682_show() called on %s\n", attr
->attr
.name
);
68 /* Read the register */
69 rc
= i2c_smbus_read_i2c_block_data(client
, sattr
->index
, sattr
->nr
,
74 val
= le32_to_cpu(val_le
);
76 if (sattr
->index
== DS1682_REG_ELAPSED
) {
79 /* Detect and retry when a tick occurs mid-read */
81 rc
= i2c_smbus_read_i2c_block_data(client
, sattr
->index
,
84 if (rc
< 0 || retries
<= 0)
88 val
= le32_to_cpu(val_le
);
90 } while (val
!= check
&& val
!= (check
+ 1));
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
97 return sprintf(buf
, "%llu\n", (sattr
->nr
== 4) ? (val
* 250) : val
);
100 static ssize_t
ds1682_store(struct device
*dev
, struct device_attribute
*attr
,
101 const char *buf
, size_t count
)
103 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
104 struct i2c_client
*client
= to_i2c_client(dev
);
109 dev_dbg(dev
, "ds1682_store() called on %s\n", attr
->attr
.name
);
112 rc
= kstrtoull(buf
, 0, &val
);
114 dev_dbg(dev
, "input string not a number\n");
118 /* Special case: the 32 bit regs are time values with 1/4s
119 * resolution, scale input down to quarter-seconds */
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
,
128 dev_err(dev
, "register write failed; reg=0x%x, size=%i\n",
129 sattr
->index
, sattr
->nr
);
137 * Simple register attributes
139 static SENSOR_DEVICE_ATTR_2(elapsed_time
, S_IRUGO
| S_IWUSR
, ds1682_show
,
140 ds1682_store
, 4, DS1682_REG_ELAPSED
);
141 static SENSOR_DEVICE_ATTR_2(alarm_time
, S_IRUGO
| S_IWUSR
, ds1682_show
,
142 ds1682_store
, 4, DS1682_REG_ALARM
);
143 static SENSOR_DEVICE_ATTR_2(event_count
, S_IRUGO
| S_IWUSR
, ds1682_show
,
144 ds1682_store
, 2, DS1682_REG_EVT_CNTR
);
146 static 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
,
156 * User data attribute
158 static 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
)
162 struct i2c_client
*client
= kobj_to_i2c_client(kobj
);
165 dev_dbg(&client
->dev
, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
168 rc
= i2c_smbus_read_i2c_block_data(client
, DS1682_REG_EEPROM
+ off
,
176 static 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
)
180 struct i2c_client
*client
= kobj_to_i2c_client(kobj
);
182 dev_dbg(&client
->dev
, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
185 /* Write out to the device */
186 if (i2c_smbus_write_i2c_block_data(client
, DS1682_REG_EEPROM
+ off
,
193 static const struct bin_attribute ds1682_eeprom_attr
= {
196 .mode
= S_IRUGO
| S_IWUSR
,
198 .size
= DS1682_EEPROM_SIZE
,
199 .read
= ds1682_eeprom_read
,
200 .write
= ds1682_eeprom_write
,
204 * Called when a ds1682 device is matched with this driver
206 static int ds1682_probe(struct i2c_client
*client
,
207 const struct i2c_device_id
*id
)
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");
218 rc
= sysfs_create_group(&client
->dev
.kobj
, &ds1682_group
);
222 rc
= sysfs_create_bin_file(&client
->dev
.kobj
, &ds1682_eeprom_attr
);
229 sysfs_remove_group(&client
->dev
.kobj
, &ds1682_group
);
234 static int ds1682_remove(struct i2c_client
*client
)
236 sysfs_remove_bin_file(&client
->dev
.kobj
, &ds1682_eeprom_attr
);
237 sysfs_remove_group(&client
->dev
.kobj
, &ds1682_group
);
241 static const struct i2c_device_id ds1682_id
[] = {
245 MODULE_DEVICE_TABLE(i2c
, ds1682_id
);
247 static const struct of_device_id ds1682_of_match
[] = {
248 { .compatible
= "dallas,ds1682", },
251 MODULE_DEVICE_TABLE(of
, ds1682_of_match
);
253 static struct i2c_driver ds1682_driver
= {
256 .of_match_table
= ds1682_of_match
,
258 .probe
= ds1682_probe
,
259 .remove
= ds1682_remove
,
260 .id_table
= ds1682_id
,
263 module_i2c_driver(ds1682_driver
);
265 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
266 MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
267 MODULE_LICENSE("GPL");