Linux Audio

Check our new training course

Loading...
v3.15
  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	__le32 val = 0;
 63	int rc;
 64
 65	dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
 66
 67	/* Read the register */
 68	rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
 69					   (u8 *) & val);
 70	if (rc < 0)
 71		return -EIO;
 72
 73	/* Special case: the 32 bit regs are time values with 1/4s
 74	 * resolution, scale them up to milliseconds */
 75	if (sattr->nr == 4)
 76		return sprintf(buf, "%llu\n",
 77			((unsigned long long)le32_to_cpu(val)) * 250);
 78
 79	/* Format the output string and return # of bytes */
 80	return sprintf(buf, "%li\n", (long)le32_to_cpu(val));
 81}
 82
 83static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
 84			    const char *buf, size_t count)
 85{
 86	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
 87	struct i2c_client *client = to_i2c_client(dev);
 88	char *endp;
 89	u64 val;
 90	__le32 val_le;
 91	int rc;
 92
 93	dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
 94
 95	/* Decode input */
 96	val = simple_strtoull(buf, &endp, 0);
 97	if (buf == endp) {
 98		dev_dbg(dev, "input string not a number\n");
 99		return -EINVAL;
100	}
101
102	/* Special case: the 32 bit regs are time values with 1/4s
103	 * resolution, scale input down to quarter-seconds */
104	if (sattr->nr == 4)
105		do_div(val, 250);
106
107	/* write out the value */
108	val_le = cpu_to_le32(val);
109	rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
110					    (u8 *) & val_le);
111	if (rc < 0) {
112		dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
113			sattr->index, sattr->nr);
114		return -EIO;
115	}
116
117	return count;
118}
119
120/*
121 * Simple register attributes
122 */
123static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
124			    ds1682_store, 4, DS1682_REG_ELAPSED);
125static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
126			    ds1682_store, 4, DS1682_REG_ALARM);
127static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
128			    ds1682_store, 2, DS1682_REG_EVT_CNTR);
129
130static const struct attribute_group ds1682_group = {
131	.attrs = (struct attribute *[]) {
132		&sensor_dev_attr_elapsed_time.dev_attr.attr,
133		&sensor_dev_attr_alarm_time.dev_attr.attr,
134		&sensor_dev_attr_event_count.dev_attr.attr,
135		NULL,
136	},
137};
138
139/*
140 * User data attribute
141 */
142static ssize_t ds1682_eeprom_read(struct file *filp, struct kobject *kobj,
143				  struct bin_attribute *attr,
144				  char *buf, loff_t off, size_t count)
145{
146	struct i2c_client *client = kobj_to_i2c_client(kobj);
147	int rc;
148
149	dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
150		buf, off, count);
151
152	if (off >= DS1682_EEPROM_SIZE)
153		return 0;
154
155	if (off + count > DS1682_EEPROM_SIZE)
156		count = DS1682_EEPROM_SIZE - off;
157
158	rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
159					   count, buf);
160	if (rc < 0)
161		return -EIO;
162
163	return count;
164}
165
166static ssize_t ds1682_eeprom_write(struct file *filp, struct kobject *kobj,
167				   struct bin_attribute *attr,
168				   char *buf, loff_t off, size_t count)
169{
170	struct i2c_client *client = kobj_to_i2c_client(kobj);
171
172	dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
173		buf, off, count);
174
175	if (off >= DS1682_EEPROM_SIZE)
176		return -ENOSPC;
177
178	if (off + count > DS1682_EEPROM_SIZE)
179		count = DS1682_EEPROM_SIZE - off;
180
181	/* Write out to the device */
182	if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
183					   count, buf) < 0)
184		return -EIO;
185
186	return count;
187}
188
189static struct bin_attribute ds1682_eeprom_attr = {
190	.attr = {
191		.name = "eeprom",
192		.mode = S_IRUGO | S_IWUSR,
193	},
194	.size = DS1682_EEPROM_SIZE,
195	.read = ds1682_eeprom_read,
196	.write = ds1682_eeprom_write,
197};
198
199/*
200 * Called when a ds1682 device is matched with this driver
201 */
202static int ds1682_probe(struct i2c_client *client,
203			const struct i2c_device_id *id)
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 int 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	return 0;
235}
236
237static const struct i2c_device_id ds1682_id[] = {
238	{ "ds1682", 0 },
239	{ }
240};
241MODULE_DEVICE_TABLE(i2c, ds1682_id);
242
243static struct i2c_driver ds1682_driver = {
244	.driver = {
245		.name = "ds1682",
246	},
247	.probe = ds1682_probe,
248	.remove = ds1682_remove,
249	.id_table = ds1682_id,
250};
251
252module_i2c_driver(ds1682_driver);
253
254MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
255MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
256MODULE_LICENSE("GPL");
v4.6
  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	__le32 val = 0;
 63	int rc;
 64
 65	dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
 66
 67	/* Read the register */
 68	rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
 69					   (u8 *) & val);
 70	if (rc < 0)
 71		return -EIO;
 72
 73	/* Special case: the 32 bit regs are time values with 1/4s
 74	 * resolution, scale them up to milliseconds */
 75	if (sattr->nr == 4)
 76		return sprintf(buf, "%llu\n",
 77			((unsigned long long)le32_to_cpu(val)) * 250);
 78
 79	/* Format the output string and return # of bytes */
 80	return sprintf(buf, "%li\n", (long)le32_to_cpu(val));
 81}
 82
 83static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
 84			    const char *buf, size_t count)
 85{
 86	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
 87	struct i2c_client *client = to_i2c_client(dev);
 
 88	u64 val;
 89	__le32 val_le;
 90	int rc;
 91
 92	dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
 93
 94	/* Decode input */
 95	rc = kstrtoull(buf, 0, &val);
 96	if (rc < 0) {
 97		dev_dbg(dev, "input string not a number\n");
 98		return -EINVAL;
 99	}
100
101	/* Special case: the 32 bit regs are time values with 1/4s
102	 * resolution, scale input down to quarter-seconds */
103	if (sattr->nr == 4)
104		do_div(val, 250);
105
106	/* write out the value */
107	val_le = cpu_to_le32(val);
108	rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
109					    (u8 *) & val_le);
110	if (rc < 0) {
111		dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
112			sattr->index, sattr->nr);
113		return -EIO;
114	}
115
116	return count;
117}
118
119/*
120 * Simple register attributes
121 */
122static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
123			    ds1682_store, 4, DS1682_REG_ELAPSED);
124static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
125			    ds1682_store, 4, DS1682_REG_ALARM);
126static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
127			    ds1682_store, 2, DS1682_REG_EVT_CNTR);
128
129static const struct attribute_group ds1682_group = {
130	.attrs = (struct attribute *[]) {
131		&sensor_dev_attr_elapsed_time.dev_attr.attr,
132		&sensor_dev_attr_alarm_time.dev_attr.attr,
133		&sensor_dev_attr_event_count.dev_attr.attr,
134		NULL,
135	},
136};
137
138/*
139 * User data attribute
140 */
141static ssize_t ds1682_eeprom_read(struct file *filp, struct kobject *kobj,
142				  struct bin_attribute *attr,
143				  char *buf, loff_t off, size_t count)
144{
145	struct i2c_client *client = kobj_to_i2c_client(kobj);
146	int rc;
147
148	dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
149		buf, off, count);
150
 
 
 
 
 
 
151	rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
152					   count, buf);
153	if (rc < 0)
154		return -EIO;
155
156	return count;
157}
158
159static ssize_t ds1682_eeprom_write(struct file *filp, struct kobject *kobj,
160				   struct bin_attribute *attr,
161				   char *buf, loff_t off, size_t count)
162{
163	struct i2c_client *client = kobj_to_i2c_client(kobj);
164
165	dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
166		buf, off, count);
 
 
 
 
 
 
167
168	/* Write out to the device */
169	if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
170					   count, buf) < 0)
171		return -EIO;
172
173	return count;
174}
175
176static struct bin_attribute ds1682_eeprom_attr = {
177	.attr = {
178		.name = "eeprom",
179		.mode = S_IRUGO | S_IWUSR,
180	},
181	.size = DS1682_EEPROM_SIZE,
182	.read = ds1682_eeprom_read,
183	.write = ds1682_eeprom_write,
184};
185
186/*
187 * Called when a ds1682 device is matched with this driver
188 */
189static int ds1682_probe(struct i2c_client *client,
190			const struct i2c_device_id *id)
191{
192	int rc;
193
194	if (!i2c_check_functionality(client->adapter,
195				     I2C_FUNC_SMBUS_I2C_BLOCK)) {
196		dev_err(&client->dev, "i2c bus does not support the ds1682\n");
197		rc = -ENODEV;
198		goto exit;
199	}
200
201	rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
202	if (rc)
203		goto exit;
204
205	rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
206	if (rc)
207		goto exit_bin_attr;
208
209	return 0;
210
211 exit_bin_attr:
212	sysfs_remove_group(&client->dev.kobj, &ds1682_group);
213 exit:
214	return rc;
215}
216
217static int ds1682_remove(struct i2c_client *client)
218{
219	sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
220	sysfs_remove_group(&client->dev.kobj, &ds1682_group);
221	return 0;
222}
223
224static const struct i2c_device_id ds1682_id[] = {
225	{ "ds1682", 0 },
226	{ }
227};
228MODULE_DEVICE_TABLE(i2c, ds1682_id);
229
230static struct i2c_driver ds1682_driver = {
231	.driver = {
232		.name = "ds1682",
233	},
234	.probe = ds1682_probe,
235	.remove = ds1682_remove,
236	.id_table = ds1682_id,
237};
238
239module_i2c_driver(ds1682_driver);
240
241MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
242MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
243MODULE_LICENSE("GPL");