aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/chips/ds1682.c
diff options
context:
space:
mode:
authorGrant Likely <grant.likely@secretlab.ca>2007-07-12 08:12:29 -0400
committerJean Delvare <khali@hyperion.delvare>2007-07-12 08:12:29 -0400
commit5162b75b24963eebe62c4d4161d0fe0b337a313b (patch)
tree789f176279aa5890f7805ebd3b2ae9d7a8b8aa4c /drivers/i2c/chips/ds1682.c
parent4b2643d7d9bdcd776749e17f73c168ddf02e93cb (diff)
i2c: New DS1682 chip driver
A driver for the Dallas DS1682 elapsed time recorder chip. Tested on a MPC5200 based board using the integrated i2c adapter. Signed-off-by: Grant Likely <grant.likely@secretlab.ca> Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'drivers/i2c/chips/ds1682.c')
-rw-r--r--drivers/i2c/chips/ds1682.c259
1 files changed, 259 insertions, 0 deletions
diff --git a/drivers/i2c/chips/ds1682.c b/drivers/i2c/chips/ds1682.c
new file mode 100644
index 000000000000..25fd4676fb17
--- /dev/null
+++ b/drivers/i2c/chips/ds1682.c
@@ -0,0 +1,259 @@
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/slab.h>
37#include <linux/i2c.h>
38#include <linux/string.h>
39#include <linux/list.h>
40#include <linux/sysfs.h>
41#include <linux/ctype.h>
42#include <linux/hwmon-sysfs.h>
43
44/* Device registers */
45#define DS1682_REG_CONFIG 0x00
46#define DS1682_REG_ALARM 0x01
47#define DS1682_REG_ELAPSED 0x05
48#define DS1682_REG_EVT_CNTR 0x09
49#define DS1682_REG_EEPROM 0x0b
50#define DS1682_REG_RESET 0x1d
51#define DS1682_REG_WRITE_DISABLE 0x1e
52#define DS1682_REG_WRITE_MEM_DISABLE 0x1f
53
54#define DS1682_EEPROM_SIZE 10
55
56/*
57 * Generic counter attributes
58 */
59static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr,
60 char *buf)
61{
62 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
63 struct i2c_client *client = to_i2c_client(dev);
64 __le32 val = 0;
65 int rc;
66
67 dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
68
69 /* Read the register */
70 rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
71 (u8 *) & val);
72 if (rc < 0)
73 return -EIO;
74
75 /* Special case: the 32 bit regs are time values with 1/4s
76 * resolution, scale them up to milliseconds */
77 if (sattr->nr == 4)
78 return sprintf(buf, "%llu\n", ((u64) 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 kobject *kobj, char *buf, loff_t off,
144 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 kobject *kobj, char *buf, loff_t off,
167 size_t count)
168{
169 struct i2c_client *client = kobj_to_i2c_client(kobj);
170
171 dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
172 buf, off, count);
173
174 if (off >= DS1682_EEPROM_SIZE)
175 return -ENOSPC;
176
177 if (off + count > DS1682_EEPROM_SIZE)
178 count = DS1682_EEPROM_SIZE - off;
179
180 /* Write out to the device */
181 if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
182 count, buf) < 0)
183 return -EIO;
184
185 return count;
186}
187
188static struct bin_attribute ds1682_eeprom_attr = {
189 .attr = {
190 .name = "eeprom",
191 .mode = S_IRUGO | S_IWUSR,
192 .owner = THIS_MODULE,
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{
204 int rc;
205
206 if (!i2c_check_functionality(client->adapter,
207 I2C_FUNC_SMBUS_I2C_BLOCK)) {
208 dev_err(&client->dev, "i2c bus does not support the ds1682\n");
209 rc = -ENODEV;
210 goto exit;
211 }
212
213 rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
214 if (rc)
215 goto exit;
216
217 rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
218 if (rc)
219 goto exit_bin_attr;
220
221 return 0;
222
223 exit_bin_attr:
224 sysfs_remove_group(&client->dev.kobj, &ds1682_group);
225 exit:
226 return rc;
227}
228
229static int ds1682_remove(struct i2c_client *client)
230{
231 sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
232 sysfs_remove_group(&client->dev.kobj, &ds1682_group);
233 return 0;
234}
235
236static struct i2c_driver ds1682_driver = {
237 .driver = {
238 .name = "ds1682",
239 },
240 .probe = ds1682_probe,
241 .remove = ds1682_remove,
242};
243
244static int __init ds1682_init(void)
245{
246 return i2c_add_driver(&ds1682_driver);
247}
248
249static void __exit ds1682_exit(void)
250{
251 i2c_del_driver(&ds1682_driver);
252}
253
254MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
255MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
256MODULE_LICENSE("GPL");
257
258module_init(ds1682_init);
259module_exit(ds1682_exit);