Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Dallas Semiconductor DS1682 Elapsed Time Recorder device driver
  4 *
  5 * Written by: Grant Likely <grant.likely@secretlab.ca>
  6 *
  7 * Copyright (C) 2007 Secret Lab Technologies Ltd.
  8 */
  9
 10/*
 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.
 14 *
 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
 22 *
 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.
 28 *
 29 */
 30
 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>
 39
 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
 50#define DS1682_EEPROM_SIZE		10
 51
 52/*
 53 * Generic counter attributes
 54 */
 55static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr,
 56			   char *buf)
 57{
 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;
 61	__le32 val_le = 0;
 62	int rc;
 63
 64	dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
 65
 66	/* Read the register */
 67	rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
 68					   (u8 *)&val_le);
 69	if (rc < 0)
 70		return -EIO;
 71
 72	val = le32_to_cpu(val_le);
 73
 74	if (sattr->index == DS1682_REG_ELAPSED) {
 75		int retries = 5;
 76
 77		/* Detect and retry when a tick occurs mid-read */
 78		do {
 79			rc = i2c_smbus_read_i2c_block_data(client, sattr->index,
 80							   sattr->nr,
 81							   (u8 *)&val_le);
 82			if (rc < 0 || retries <= 0)
 83				return -EIO;
 84
 85			check = val;
 86			val = le32_to_cpu(val_le);
 87			retries--;
 88		} while (val != check && val != (check + 1));
 89	}
 90
 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
 94	 */
 95	return sprintf(buf, "%llu\n", (sattr->nr == 4) ? (val * 250) : val);
 96}
 97
 98static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
 99			    const char *buf, size_t count)
100{
101	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
102	struct i2c_client *client = to_i2c_client(dev);
103	u64 val;
104	__le32 val_le;
105	int rc;
106
107	dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
108
109	/* Decode input */
110	rc = kstrtoull(buf, 0, &val);
111	if (rc < 0) {
112		dev_dbg(dev, "input string not a number\n");
113		return -EINVAL;
114	}
115
116	/* Special case: the 32 bit regs are time values with 1/4s
117	 * resolution, scale input down to quarter-seconds */
118	if (sattr->nr == 4)
119		do_div(val, 250);
120
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,
124					    (u8 *) & val_le);
125	if (rc < 0) {
126		dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
127			sattr->index, sattr->nr);
128		return -EIO;
129	}
130
131	return count;
132}
133
134/*
135 * Simple register attributes
136 */
137static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
138			    ds1682_store, 4, DS1682_REG_ELAPSED);
139static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
140			    ds1682_store, 4, DS1682_REG_ALARM);
141static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
142			    ds1682_store, 2, DS1682_REG_EVT_CNTR);
143
144static 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,
149		NULL,
150	},
151};
152
153/*
154 * User data attribute
155 */
156static 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)
159{
160	struct i2c_client *client = kobj_to_i2c_client(kobj);
161	int rc;
162
163	dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
164		buf, off, count);
165
166	rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
167					   count, buf);
168	if (rc < 0)
169		return -EIO;
170
171	return count;
172}
173
174static 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)
177{
178	struct i2c_client *client = kobj_to_i2c_client(kobj);
179
180	dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
181		buf, off, count);
182
183	/* Write out to the device */
184	if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
185					   count, buf) < 0)
186		return -EIO;
187
188	return count;
189}
190
191static const struct bin_attribute ds1682_eeprom_attr = {
192	.attr = {
193		.name = "eeprom",
194		.mode = S_IRUGO | S_IWUSR,
195	},
196	.size = DS1682_EEPROM_SIZE,
197	.read = ds1682_eeprom_read,
198	.write = ds1682_eeprom_write,
199};
200
201static int ds1682_nvmem_read(void *priv, unsigned int offset, void *val,
202			     size_t bytes)
203{
204	struct i2c_client *client = priv;
205	int ret;
206
207	ret = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + offset,
208					    bytes, val);
209	return ret < 0 ? ret : 0;
210}
211
212static int ds1682_nvmem_write(void *priv, unsigned int offset, void *val,
213			      size_t bytes)
214{
215	struct i2c_client *client = priv;
216	int ret;
217
218	ret = i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + offset,
219					     bytes, val);
220	return ret < 0 ? ret : 0;
221}
222
223/*
224 * Called when a ds1682 device is matched with this driver
225 */
226static int ds1682_probe(struct i2c_client *client)
227{
228	struct nvmem_config config = {
229		.dev = &client->dev,
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,
235		.priv = client,
236	};
237	struct nvmem_device *nvmem;
238	int rc;
239
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");
243		rc = -ENODEV;
244		goto exit;
245	}
246
247	nvmem = devm_nvmem_register(&client->dev, &config);
248	if (IS_ENABLED(CONFIG_NVMEM) && IS_ERR(nvmem))
249		return PTR_ERR(nvmem);
250
251	rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
252	if (rc)
253		goto exit;
254
255	rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
256	if (rc)
257		goto exit_bin_attr;
258
259	return 0;
260
261 exit_bin_attr:
262	sysfs_remove_group(&client->dev.kobj, &ds1682_group);
263 exit:
264	return rc;
265}
266
267static void ds1682_remove(struct i2c_client *client)
268{
269	sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
270	sysfs_remove_group(&client->dev.kobj, &ds1682_group);
271}
272
273static const struct i2c_device_id ds1682_id[] = {
274	{ "ds1682" },
275	{ }
276};
277MODULE_DEVICE_TABLE(i2c, ds1682_id);
278
279static const struct of_device_id ds1682_of_match[] = {
280	{ .compatible = "dallas,ds1682", },
281	{}
282};
283MODULE_DEVICE_TABLE(of, ds1682_of_match);
284
285static struct i2c_driver ds1682_driver = {
286	.driver = {
287		.name = "ds1682",
288		.of_match_table = ds1682_of_match,
289	},
290	.probe = ds1682_probe,
291	.remove = ds1682_remove,
292	.id_table = ds1682_id,
293};
294
295module_i2c_driver(ds1682_driver);
296
297MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
298MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
299MODULE_LICENSE("GPL");
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Dallas Semiconductor DS1682 Elapsed Time Recorder device driver
  4 *
  5 * Written by: Grant Likely <grant.likely@secretlab.ca>
  6 *
  7 * Copyright (C) 2007 Secret Lab Technologies Ltd.
  8 */
  9
 10/*
 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.
 14 *
 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
 22 *
 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.
 28 *
 29 */
 30
 31#include <linux/module.h>
 32#include <linux/i2c.h>
 33#include <linux/string.h>
 34#include <linux/list.h>
 
 35#include <linux/sysfs.h>
 36#include <linux/ctype.h>
 37#include <linux/hwmon-sysfs.h>
 38
 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
 49#define DS1682_EEPROM_SIZE		10
 50
 51/*
 52 * Generic counter attributes
 53 */
 54static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr,
 55			   char *buf)
 56{
 57	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
 58	struct i2c_client *client = to_i2c_client(dev);
 59	unsigned long long val, check;
 60	__le32 val_le = 0;
 61	int rc;
 62
 63	dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
 64
 65	/* Read the register */
 66	rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
 67					   (u8 *)&val_le);
 68	if (rc < 0)
 69		return -EIO;
 70
 71	val = le32_to_cpu(val_le);
 72
 73	if (sattr->index == DS1682_REG_ELAPSED) {
 74		int retries = 5;
 75
 76		/* Detect and retry when a tick occurs mid-read */
 77		do {
 78			rc = i2c_smbus_read_i2c_block_data(client, sattr->index,
 79							   sattr->nr,
 80							   (u8 *)&val_le);
 81			if (rc < 0 || retries <= 0)
 82				return -EIO;
 83
 84			check = val;
 85			val = le32_to_cpu(val_le);
 86			retries--;
 87		} while (val != check && val != (check + 1));
 88	}
 89
 90	/* Format the output string and return # of bytes
 91	 * Special case: the 32 bit regs are time values with 1/4s
 92	 * resolution, scale them up to milliseconds
 93	 */
 94	return sprintf(buf, "%llu\n", (sattr->nr == 4) ? (val * 250) : val);
 95}
 96
 97static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
 98			    const char *buf, size_t count)
 99{
100	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
101	struct i2c_client *client = to_i2c_client(dev);
102	u64 val;
103	__le32 val_le;
104	int rc;
105
106	dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
107
108	/* Decode input */
109	rc = kstrtoull(buf, 0, &val);
110	if (rc < 0) {
111		dev_dbg(dev, "input string not a number\n");
112		return -EINVAL;
113	}
114
115	/* Special case: the 32 bit regs are time values with 1/4s
116	 * resolution, scale input down to quarter-seconds */
117	if (sattr->nr == 4)
118		do_div(val, 250);
119
120	/* write out the value */
121	val_le = cpu_to_le32(val);
122	rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
123					    (u8 *) & val_le);
124	if (rc < 0) {
125		dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
126			sattr->index, sattr->nr);
127		return -EIO;
128	}
129
130	return count;
131}
132
133/*
134 * Simple register attributes
135 */
136static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
137			    ds1682_store, 4, DS1682_REG_ELAPSED);
138static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
139			    ds1682_store, 4, DS1682_REG_ALARM);
140static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
141			    ds1682_store, 2, DS1682_REG_EVT_CNTR);
142
143static const struct attribute_group ds1682_group = {
144	.attrs = (struct attribute *[]) {
145		&sensor_dev_attr_elapsed_time.dev_attr.attr,
146		&sensor_dev_attr_alarm_time.dev_attr.attr,
147		&sensor_dev_attr_event_count.dev_attr.attr,
148		NULL,
149	},
150};
151
152/*
153 * User data attribute
154 */
155static ssize_t ds1682_eeprom_read(struct file *filp, struct kobject *kobj,
156				  struct bin_attribute *attr,
157				  char *buf, loff_t off, size_t count)
158{
159	struct i2c_client *client = kobj_to_i2c_client(kobj);
160	int rc;
161
162	dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
163		buf, off, count);
164
165	rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
166					   count, buf);
167	if (rc < 0)
168		return -EIO;
169
170	return count;
171}
172
173static ssize_t ds1682_eeprom_write(struct file *filp, struct kobject *kobj,
174				   struct bin_attribute *attr,
175				   char *buf, loff_t off, size_t count)
176{
177	struct i2c_client *client = kobj_to_i2c_client(kobj);
178
179	dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
180		buf, off, count);
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 const 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{
 
 
 
 
 
 
 
 
 
 
205	int rc;
206
207	if (!i2c_check_functionality(client->adapter,
208				     I2C_FUNC_SMBUS_I2C_BLOCK)) {
209		dev_err(&client->dev, "i2c bus does not support the ds1682\n");
210		rc = -ENODEV;
211		goto exit;
212	}
213
 
 
 
 
214	rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
215	if (rc)
216		goto exit;
217
218	rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
219	if (rc)
220		goto exit_bin_attr;
221
222	return 0;
223
224 exit_bin_attr:
225	sysfs_remove_group(&client->dev.kobj, &ds1682_group);
226 exit:
227	return rc;
228}
229
230static void ds1682_remove(struct i2c_client *client)
231{
232	sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
233	sysfs_remove_group(&client->dev.kobj, &ds1682_group);
234}
235
236static const struct i2c_device_id ds1682_id[] = {
237	{ "ds1682", 0 },
238	{ }
239};
240MODULE_DEVICE_TABLE(i2c, ds1682_id);
241
242static const struct of_device_id ds1682_of_match[] = {
243	{ .compatible = "dallas,ds1682", },
244	{}
245};
246MODULE_DEVICE_TABLE(of, ds1682_of_match);
247
248static struct i2c_driver ds1682_driver = {
249	.driver = {
250		.name = "ds1682",
251		.of_match_table = ds1682_of_match,
252	},
253	.probe = ds1682_probe,
254	.remove = ds1682_remove,
255	.id_table = ds1682_id,
256};
257
258module_i2c_driver(ds1682_driver);
259
260MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
261MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
262MODULE_LICENSE("GPL");