1 // SPDX-License-Identifier: GPL-2.0-only
3 * Dallas Semiconductor DS1682 Elapsed Time Recorder device driver
5 * Written by: Grant Likely <grant.likely@secretlab.ca>
7 * Copyright (C) 2007 Secret Lab Technologies Ltd.
11 * The DS1682 elapsed timer recorder is a simple device that implements
12 * one elapsed time counter, one event counter, an alarm signal and 10
13 * bytes of general purpose EEPROM.
15 * This driver provides access to the DS1682 counters and user data via
16 * the sysfs. The following attributes are added to the device node:
17 * elapsed_time (u32): Total elapsed event time in ms resolution
18 * alarm_time (u32): When elapsed time exceeds the value in alarm_time,
19 * then the alarm pin is asserted.
20 * event_count (u16): number of times the event pin has gone low.
21 * eeprom (u8[10]): general purpose EEPROM
23 * Counter registers and user data are both read/write unless the device
24 * has been write protected. This driver does not support turning off write
25 * protection. Once write protection is turned on, it is impossible to
26 * turn it off again, so I have left the feature out of this driver to avoid
27 * accidental enabling, but it is trivial to add write protect support.
31 #include <linux/module.h>
32 #include <linux/i2c.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
35 #include <linux/nvmem-provider.h>
36 #include <linux/sysfs.h>
37 #include <linux/ctype.h>
38 #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
50 #define DS1682_EEPROM_SIZE 10
53 * Generic counter attributes
55 static ssize_t
ds1682_show(struct device
*dev
, struct device_attribute
*attr
,
58 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
59 struct i2c_client
*client
= to_i2c_client(dev
);
60 unsigned long long val
, check
;
64 dev_dbg(dev
, "ds1682_show() called on %s\n", attr
->attr
.name
);
66 /* Read the register */
67 rc
= i2c_smbus_read_i2c_block_data(client
, sattr
->index
, sattr
->nr
,
72 val
= le32_to_cpu(val_le
);
74 if (sattr
->index
== DS1682_REG_ELAPSED
) {
77 /* Detect and retry when a tick occurs mid-read */
79 rc
= i2c_smbus_read_i2c_block_data(client
, sattr
->index
,
82 if (rc
< 0 || retries
<= 0)
86 val
= le32_to_cpu(val_le
);
88 } while (val
!= check
&& val
!= (check
+ 1));
91 /* Format the output string and return # of bytes
92 * Special case: the 32 bit regs are time values with 1/4s
93 * resolution, scale them up to milliseconds
95 return sprintf(buf
, "%llu\n", (sattr
->nr
== 4) ? (val
* 250) : val
);
98 static ssize_t
ds1682_store(struct device
*dev
, struct device_attribute
*attr
,
99 const char *buf
, size_t count
)
101 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
102 struct i2c_client
*client
= to_i2c_client(dev
);
107 dev_dbg(dev
, "ds1682_store() called on %s\n", attr
->attr
.name
);
110 rc
= kstrtoull(buf
, 0, &val
);
112 dev_dbg(dev
, "input string not a number\n");
116 /* Special case: the 32 bit regs are time values with 1/4s
117 * resolution, scale input down to quarter-seconds */
121 /* write out the value */
122 val_le
= cpu_to_le32(val
);
123 rc
= i2c_smbus_write_i2c_block_data(client
, sattr
->index
, sattr
->nr
,
126 dev_err(dev
, "register write failed; reg=0x%x, size=%i\n",
127 sattr
->index
, sattr
->nr
);
135 * Simple register attributes
137 static SENSOR_DEVICE_ATTR_2(elapsed_time
, S_IRUGO
| S_IWUSR
, ds1682_show
,
138 ds1682_store
, 4, DS1682_REG_ELAPSED
);
139 static SENSOR_DEVICE_ATTR_2(alarm_time
, S_IRUGO
| S_IWUSR
, ds1682_show
,
140 ds1682_store
, 4, DS1682_REG_ALARM
);
141 static SENSOR_DEVICE_ATTR_2(event_count
, S_IRUGO
| S_IWUSR
, ds1682_show
,
142 ds1682_store
, 2, DS1682_REG_EVT_CNTR
);
144 static const struct attribute_group ds1682_group
= {
145 .attrs
= (struct attribute
*[]) {
146 &sensor_dev_attr_elapsed_time
.dev_attr
.attr
,
147 &sensor_dev_attr_alarm_time
.dev_attr
.attr
,
148 &sensor_dev_attr_event_count
.dev_attr
.attr
,
154 * User data attribute
156 static ssize_t
ds1682_eeprom_read(struct file
*filp
, struct kobject
*kobj
,
157 struct bin_attribute
*attr
,
158 char *buf
, loff_t off
, size_t count
)
160 struct i2c_client
*client
= kobj_to_i2c_client(kobj
);
163 dev_dbg(&client
->dev
, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
166 rc
= i2c_smbus_read_i2c_block_data(client
, DS1682_REG_EEPROM
+ off
,
174 static ssize_t
ds1682_eeprom_write(struct file
*filp
, struct kobject
*kobj
,
175 struct bin_attribute
*attr
,
176 char *buf
, loff_t off
, size_t count
)
178 struct i2c_client
*client
= kobj_to_i2c_client(kobj
);
180 dev_dbg(&client
->dev
, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
183 /* Write out to the device */
184 if (i2c_smbus_write_i2c_block_data(client
, DS1682_REG_EEPROM
+ off
,
191 static const struct bin_attribute ds1682_eeprom_attr
= {
194 .mode
= S_IRUGO
| S_IWUSR
,
196 .size
= DS1682_EEPROM_SIZE
,
197 .read
= ds1682_eeprom_read
,
198 .write
= ds1682_eeprom_write
,
201 static int ds1682_nvmem_read(void *priv
, unsigned int offset
, void *val
,
204 struct i2c_client
*client
= priv
;
207 ret
= i2c_smbus_read_i2c_block_data(client
, DS1682_REG_EEPROM
+ offset
,
209 return ret
< 0 ? ret
: 0;
212 static int ds1682_nvmem_write(void *priv
, unsigned int offset
, void *val
,
215 struct i2c_client
*client
= priv
;
218 ret
= i2c_smbus_write_i2c_block_data(client
, DS1682_REG_EEPROM
+ offset
,
220 return ret
< 0 ? ret
: 0;
224 * Called when a ds1682 device is matched with this driver
226 static int ds1682_probe(struct i2c_client
*client
)
228 struct nvmem_config config
= {
230 .owner
= THIS_MODULE
,
231 .type
= NVMEM_TYPE_EEPROM
,
232 .reg_read
= ds1682_nvmem_read
,
233 .reg_write
= ds1682_nvmem_write
,
234 .size
= DS1682_EEPROM_SIZE
,
237 struct nvmem_device
*nvmem
;
240 if (!i2c_check_functionality(client
->adapter
,
241 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
242 dev_err(&client
->dev
, "i2c bus does not support the ds1682\n");
247 nvmem
= devm_nvmem_register(&client
->dev
, &config
);
248 if (IS_ENABLED(CONFIG_NVMEM
) && IS_ERR(nvmem
))
249 return PTR_ERR(nvmem
);
251 rc
= sysfs_create_group(&client
->dev
.kobj
, &ds1682_group
);
255 rc
= sysfs_create_bin_file(&client
->dev
.kobj
, &ds1682_eeprom_attr
);
262 sysfs_remove_group(&client
->dev
.kobj
, &ds1682_group
);
267 static void ds1682_remove(struct i2c_client
*client
)
269 sysfs_remove_bin_file(&client
->dev
.kobj
, &ds1682_eeprom_attr
);
270 sysfs_remove_group(&client
->dev
.kobj
, &ds1682_group
);
273 static const struct i2c_device_id ds1682_id
[] = {
277 MODULE_DEVICE_TABLE(i2c
, ds1682_id
);
279 static const struct of_device_id ds1682_of_match
[] = {
280 { .compatible
= "dallas,ds1682", },
283 MODULE_DEVICE_TABLE(of
, ds1682_of_match
);
285 static struct i2c_driver ds1682_driver
= {
288 .of_match_table
= ds1682_of_match
,
290 .probe
= ds1682_probe
,
291 .remove
= ds1682_remove
,
292 .id_table
= ds1682_id
,
295 module_i2c_driver(ds1682_driver
);
297 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
298 MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
299 MODULE_LICENSE("GPL");