1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
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
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
74
75 if (sattr->nr == 4)
76 return sprintf(buf, "%llu\n",
77 ((unsigned long long)le32_to_cpu(val)) * 250);
78
79
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
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
102
103 if (sattr->nr == 4)
104 do_div(val, 250);
105
106
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
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
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 if (off >= DS1682_EEPROM_SIZE)
152 return 0;
153
154 if (off + count > DS1682_EEPROM_SIZE)
155 count = DS1682_EEPROM_SIZE - off;
156
157 rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
158 count, buf);
159 if (rc < 0)
160 return -EIO;
161
162 return count;
163}
164
165static ssize_t ds1682_eeprom_write(struct file *filp, struct kobject *kobj,
166 struct bin_attribute *attr,
167 char *buf, loff_t off, 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
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 },
193 .size = DS1682_EEPROM_SIZE,
194 .read = ds1682_eeprom_read,
195 .write = ds1682_eeprom_write,
196};
197
198
199
200
201static int ds1682_probe(struct i2c_client *client,
202 const struct i2c_device_id *id)
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 const struct i2c_device_id ds1682_id[] = {
237 { "ds1682", 0 },
238 { }
239};
240MODULE_DEVICE_TABLE(i2c, ds1682_id);
241
242static struct i2c_driver ds1682_driver = {
243 .driver = {
244 .name = "ds1682",
245 },
246 .probe = ds1682_probe,
247 .remove = ds1682_remove,
248 .id_table = ds1682_id,
249};
250
251module_i2c_driver(ds1682_driver);
252
253MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
254MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
255MODULE_LICENSE("GPL");
256