Commit | Line | Data |
---|---|---|
671d85f2 BS |
1 | /* |
2 | * AD774X capacitive sensor driver supporting AD7745/6/7 | |
3 | * | |
4 | * Copyright 2010 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2 or later. | |
7 | */ | |
8 | ||
9 | #include <linux/interrupt.h> | |
10 | #include <linux/gpio.h> | |
671d85f2 BS |
11 | #include <linux/device.h> |
12 | #include <linux/kernel.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/sysfs.h> | |
15 | #include <linux/list.h> | |
16 | #include <linux/i2c.h> | |
99c97852 | 17 | #include <linux/module.h> |
671d85f2 BS |
18 | |
19 | #include "../iio.h" | |
20 | #include "../sysfs.h" | |
21 | ||
22 | /* | |
23 | * AD774X registers definition | |
24 | */ | |
25 | ||
26 | #define AD774X_STATUS 0 | |
27 | #define AD774X_STATUS_RDY (1 << 2) | |
28 | #define AD774X_STATUS_RDYVT (1 << 1) | |
29 | #define AD774X_STATUS_RDYCAP (1 << 0) | |
30 | #define AD774X_CAP_DATA_HIGH 1 | |
31 | #define AD774X_CAP_DATA_MID 2 | |
32 | #define AD774X_CAP_DATA_LOW 3 | |
33 | #define AD774X_VT_DATA_HIGH 4 | |
34 | #define AD774X_VT_DATA_MID 5 | |
35 | #define AD774X_VT_DATA_LOW 6 | |
36 | #define AD774X_CAP_SETUP 7 | |
37 | #define AD774X_VT_SETUP 8 | |
38 | #define AD774X_EXEC_SETUP 9 | |
39 | #define AD774X_CFG 10 | |
40 | #define AD774X_CAPDACA 11 | |
41 | #define AD774X_CAPDACB 12 | |
42 | #define AD774X_CAPDAC_EN (1 << 7) | |
43 | #define AD774X_CAP_OFFH 13 | |
44 | #define AD774X_CAP_OFFL 14 | |
45 | #define AD774X_CAP_GAINH 15 | |
46 | #define AD774X_CAP_GAINL 16 | |
47 | #define AD774X_VOLT_GAINH 17 | |
48 | #define AD774X_VOLT_GAINL 18 | |
49 | ||
50 | #define AD774X_MAX_CONV_MODE 6 | |
51 | ||
52 | /* | |
53 | * struct ad774x_chip_info - chip specifc information | |
54 | */ | |
55 | ||
56 | struct ad774x_chip_info { | |
671d85f2 | 57 | struct i2c_client *client; |
671d85f2 | 58 | bool inter; |
671d85f2 BS |
59 | u16 cap_offs; /* Capacitive offset */ |
60 | u16 cap_gain; /* Capacitive gain calibration */ | |
61 | u16 volt_gain; /* Voltage gain calibration */ | |
62 | u8 cap_setup; | |
63 | u8 vt_setup; | |
64 | u8 exec_setup; | |
65 | ||
66 | char *conversion_mode; | |
67 | }; | |
68 | ||
69 | struct ad774x_conversion_mode { | |
70 | char *name; | |
71 | u8 reg_cfg; | |
72 | }; | |
73 | ||
724cf92d JC |
74 | static struct ad774x_conversion_mode |
75 | ad774x_conv_mode_table[AD774X_MAX_CONV_MODE] = { | |
671d85f2 BS |
76 | { "idle", 0 }, |
77 | { "continuous-conversion", 1 }, | |
78 | { "single-conversion", 2 }, | |
79 | { "power-down", 3 }, | |
80 | { "offset-calibration", 5 }, | |
81 | { "gain-calibration", 6 }, | |
82 | }; | |
83 | ||
84 | /* | |
85 | * ad774x register access by I2C | |
86 | */ | |
87 | ||
88 | static int ad774x_i2c_read(struct ad774x_chip_info *chip, u8 reg, u8 *data, int len) | |
89 | { | |
90 | struct i2c_client *client = chip->client; | |
91 | int ret; | |
92 | ||
93 | ret = i2c_master_send(client, ®, 1); | |
94 | if (ret < 0) { | |
95 | dev_err(&client->dev, "I2C write error\n"); | |
96 | return ret; | |
97 | } | |
98 | ||
99 | ret = i2c_master_recv(client, data, len); | |
100 | if (ret < 0) { | |
101 | dev_err(&client->dev, "I2C read error\n"); | |
102 | return ret; | |
103 | } | |
104 | ||
105 | return ret; | |
106 | } | |
107 | ||
108 | static int ad774x_i2c_write(struct ad774x_chip_info *chip, u8 reg, u8 data) | |
109 | { | |
110 | struct i2c_client *client = chip->client; | |
111 | int ret; | |
112 | ||
113 | u8 tx[2] = { | |
114 | reg, | |
115 | data, | |
116 | }; | |
117 | ||
118 | ret = i2c_master_send(client, tx, 2); | |
119 | if (ret < 0) | |
120 | dev_err(&client->dev, "I2C write error\n"); | |
121 | ||
122 | return ret; | |
123 | } | |
124 | ||
125 | /* | |
126 | * sysfs nodes | |
127 | */ | |
128 | ||
129 | #define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show) \ | |
130 | IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0) | |
131 | #define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store) \ | |
132 | IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0) | |
133 | #define IIO_DEV_ATTR_CAP_SETUP(_mode, _show, _store) \ | |
322c9563 | 134 | IIO_DEVICE_ATTR(in_capacitance_setup, _mode, _show, _store, 0) |
671d85f2 | 135 | #define IIO_DEV_ATTR_VT_SETUP(_mode, _show, _store) \ |
322c9563 | 136 | IIO_DEVICE_ATTR(in_voltage0_setup, _mode, _show, _store, 0) |
671d85f2 BS |
137 | #define IIO_DEV_ATTR_EXEC_SETUP(_mode, _show, _store) \ |
138 | IIO_DEVICE_ATTR(exec_setup, _mode, _show, _store, 0) | |
139 | #define IIO_DEV_ATTR_VOLT_GAIN(_mode, _show, _store) \ | |
322c9563 | 140 | IIO_DEVICE_ATTR(in_voltage0_gain, _mode, _show, _store, 0) |
671d85f2 | 141 | #define IIO_DEV_ATTR_CAP_OFFS(_mode, _show, _store) \ |
322c9563 | 142 | IIO_DEVICE_ATTR(in_capacitance_offs, _mode, _show, _store, 0) |
671d85f2 | 143 | #define IIO_DEV_ATTR_CAP_GAIN(_mode, _show, _store) \ |
322c9563 | 144 | IIO_DEVICE_ATTR(in_capacitance_gain, _mode, _show, _store, 0) |
671d85f2 | 145 | #define IIO_DEV_ATTR_CAP_DATA(_show) \ |
322c9563 | 146 | IIO_DEVICE_ATTR(in_capacitance0_raw, S_IRUGO, _show, NULL, 0) |
671d85f2 | 147 | #define IIO_DEV_ATTR_VT_DATA(_show) \ |
322c9563 | 148 | IIO_DEVICE_ATTR(in_voltage0_raw, S_IRUGO, _show, NULL, 0) |
671d85f2 BS |
149 | |
150 | static ssize_t ad774x_show_conversion_modes(struct device *dev, | |
151 | struct device_attribute *attr, | |
152 | char *buf) | |
153 | { | |
154 | int i; | |
155 | int len = 0; | |
156 | ||
157 | for (i = 0; i < AD774X_MAX_CONV_MODE; i++) | |
158 | len += sprintf(buf + len, "%s ", ad774x_conv_mode_table[i].name); | |
159 | ||
160 | len += sprintf(buf + len, "\n"); | |
161 | ||
162 | return len; | |
163 | } | |
164 | ||
165 | static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad774x_show_conversion_modes); | |
166 | ||
167 | static ssize_t ad774x_show_conversion_mode(struct device *dev, | |
168 | struct device_attribute *attr, | |
169 | char *buf) | |
170 | { | |
171 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 172 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
173 | |
174 | return sprintf(buf, "%s\n", chip->conversion_mode); | |
175 | } | |
176 | ||
177 | static ssize_t ad774x_store_conversion_mode(struct device *dev, | |
178 | struct device_attribute *attr, | |
179 | const char *buf, | |
180 | size_t len) | |
181 | { | |
182 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 183 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
184 | u8 cfg; |
185 | int i; | |
186 | ||
187 | ad774x_i2c_read(chip, AD774X_CFG, &cfg, 1); | |
188 | ||
189 | for (i = 0; i < AD774X_MAX_CONV_MODE; i++) { | |
190 | if (strncmp(buf, ad774x_conv_mode_table[i].name, | |
191 | strlen(ad774x_conv_mode_table[i].name) - 1) == 0) { | |
192 | chip->conversion_mode = ad774x_conv_mode_table[i].name; | |
193 | cfg |= 0x18 | ad774x_conv_mode_table[i].reg_cfg; | |
194 | ad774x_i2c_write(chip, AD774X_CFG, cfg); | |
195 | return len; | |
196 | } | |
197 | } | |
198 | ||
199 | dev_err(dev, "not supported conversion mode\n"); | |
200 | ||
201 | return -EINVAL; | |
202 | } | |
203 | ||
204 | static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR, | |
205 | ad774x_show_conversion_mode, | |
206 | ad774x_store_conversion_mode); | |
207 | ||
208 | static ssize_t ad774x_show_dac_value(struct device *dev, | |
209 | struct device_attribute *attr, | |
210 | char *buf) | |
211 | { | |
212 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 213 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
214 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
215 | u8 data; | |
216 | ||
217 | ad774x_i2c_read(chip, this_attr->address, &data, 1); | |
218 | ||
219 | return sprintf(buf, "%02x\n", data & 0x7F); | |
220 | } | |
221 | ||
222 | static ssize_t ad774x_store_dac_value(struct device *dev, | |
223 | struct device_attribute *attr, | |
224 | const char *buf, | |
225 | size_t len) | |
226 | { | |
227 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 228 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
229 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
230 | unsigned long data; | |
231 | int ret; | |
232 | ||
233 | ret = strict_strtoul(buf, 10, &data); | |
234 | ||
235 | if (!ret) { | |
236 | ad774x_i2c_write(chip, this_attr->address, | |
237 | (data ? AD774X_CAPDAC_EN : 0) | (data & 0x7F)); | |
238 | return len; | |
239 | } | |
240 | ||
241 | return -EINVAL; | |
242 | } | |
243 | ||
322c9563 | 244 | static IIO_DEVICE_ATTR(out_capacitance0_raw, S_IRUGO | S_IWUSR, |
671d85f2 BS |
245 | ad774x_show_dac_value, |
246 | ad774x_store_dac_value, | |
247 | AD774X_CAPDACA); | |
248 | ||
322c9563 | 249 | static IIO_DEVICE_ATTR(out_capacitance1_raw, S_IRUGO | S_IWUSR, |
671d85f2 BS |
250 | ad774x_show_dac_value, |
251 | ad774x_store_dac_value, | |
252 | AD774X_CAPDACB); | |
253 | ||
254 | static ssize_t ad774x_show_cap_setup(struct device *dev, | |
255 | struct device_attribute *attr, | |
256 | char *buf) | |
257 | { | |
258 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 259 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
260 | |
261 | return sprintf(buf, "0x%02x\n", chip->cap_setup); | |
262 | } | |
263 | ||
264 | static ssize_t ad774x_store_cap_setup(struct device *dev, | |
265 | struct device_attribute *attr, | |
266 | const char *buf, | |
267 | size_t len) | |
268 | { | |
269 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 270 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
271 | unsigned long data; |
272 | int ret; | |
273 | ||
274 | ret = strict_strtoul(buf, 10, &data); | |
275 | ||
276 | if ((!ret) && (data < 0x100)) { | |
277 | ad774x_i2c_write(chip, AD774X_CAP_SETUP, data); | |
278 | chip->cap_setup = data; | |
279 | return len; | |
280 | } | |
281 | ||
282 | return -EINVAL; | |
283 | } | |
284 | ||
285 | static IIO_DEV_ATTR_CAP_SETUP(S_IRUGO | S_IWUSR, | |
286 | ad774x_show_cap_setup, | |
287 | ad774x_store_cap_setup); | |
288 | ||
289 | static ssize_t ad774x_show_vt_setup(struct device *dev, | |
290 | struct device_attribute *attr, | |
291 | char *buf) | |
292 | { | |
293 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 294 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
295 | |
296 | return sprintf(buf, "0x%02x\n", chip->vt_setup); | |
297 | } | |
298 | ||
299 | static ssize_t ad774x_store_vt_setup(struct device *dev, | |
300 | struct device_attribute *attr, | |
301 | const char *buf, | |
302 | size_t len) | |
303 | { | |
304 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 305 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
306 | unsigned long data; |
307 | int ret; | |
308 | ||
309 | ret = strict_strtoul(buf, 10, &data); | |
310 | ||
311 | if ((!ret) && (data < 0x100)) { | |
312 | ad774x_i2c_write(chip, AD774X_VT_SETUP, data); | |
313 | chip->vt_setup = data; | |
314 | return len; | |
315 | } | |
316 | ||
317 | return -EINVAL; | |
318 | } | |
319 | ||
320 | static IIO_DEV_ATTR_VT_SETUP(S_IRUGO | S_IWUSR, | |
321 | ad774x_show_vt_setup, | |
322 | ad774x_store_vt_setup); | |
323 | ||
324 | static ssize_t ad774x_show_exec_setup(struct device *dev, | |
325 | struct device_attribute *attr, | |
326 | char *buf) | |
327 | { | |
328 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 329 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
330 | |
331 | return sprintf(buf, "0x%02x\n", chip->exec_setup); | |
332 | } | |
333 | ||
334 | static ssize_t ad774x_store_exec_setup(struct device *dev, | |
335 | struct device_attribute *attr, | |
336 | const char *buf, | |
337 | size_t len) | |
338 | { | |
339 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 340 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
341 | unsigned long data; |
342 | int ret; | |
343 | ||
344 | ret = strict_strtoul(buf, 10, &data); | |
345 | ||
346 | if ((!ret) && (data < 0x100)) { | |
347 | ad774x_i2c_write(chip, AD774X_EXEC_SETUP, data); | |
348 | chip->exec_setup = data; | |
349 | return len; | |
350 | } | |
351 | ||
352 | return -EINVAL; | |
353 | } | |
354 | ||
355 | static IIO_DEV_ATTR_EXEC_SETUP(S_IRUGO | S_IWUSR, | |
356 | ad774x_show_exec_setup, | |
357 | ad774x_store_exec_setup); | |
358 | ||
359 | static ssize_t ad774x_show_volt_gain(struct device *dev, | |
360 | struct device_attribute *attr, | |
361 | char *buf) | |
362 | { | |
363 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 364 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
365 | |
366 | return sprintf(buf, "%d\n", chip->volt_gain); | |
367 | } | |
368 | ||
369 | static ssize_t ad774x_store_volt_gain(struct device *dev, | |
370 | struct device_attribute *attr, | |
371 | const char *buf, | |
372 | size_t len) | |
373 | { | |
374 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 375 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
376 | unsigned long data; |
377 | int ret; | |
378 | ||
379 | ret = strict_strtoul(buf, 10, &data); | |
380 | ||
381 | if ((!ret) && (data < 0x10000)) { | |
382 | ad774x_i2c_write(chip, AD774X_VOLT_GAINH, data >> 8); | |
383 | ad774x_i2c_write(chip, AD774X_VOLT_GAINL, data); | |
384 | chip->volt_gain = data; | |
385 | return len; | |
386 | } | |
387 | ||
388 | return -EINVAL; | |
389 | } | |
390 | ||
391 | static IIO_DEV_ATTR_VOLT_GAIN(S_IRUGO | S_IWUSR, | |
392 | ad774x_show_volt_gain, | |
393 | ad774x_store_volt_gain); | |
394 | ||
395 | static ssize_t ad774x_show_cap_data(struct device *dev, | |
396 | struct device_attribute *attr, | |
397 | char *buf) | |
398 | { | |
399 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 400 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
401 | unsigned long data; |
402 | char tmp[3]; | |
403 | ||
404 | ad774x_i2c_read(chip, AD774X_CAP_DATA_HIGH, tmp, 3); | |
405 | data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2]; | |
406 | ||
407 | return sprintf(buf, "%ld\n", data); | |
408 | } | |
409 | ||
410 | static IIO_DEV_ATTR_CAP_DATA(ad774x_show_cap_data); | |
411 | ||
412 | static ssize_t ad774x_show_vt_data(struct device *dev, | |
413 | struct device_attribute *attr, | |
414 | char *buf) | |
415 | { | |
416 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 417 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
418 | unsigned long data; |
419 | char tmp[3]; | |
420 | ||
421 | ad774x_i2c_read(chip, AD774X_VT_DATA_HIGH, tmp, 3); | |
422 | data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2]; | |
423 | ||
424 | return sprintf(buf, "%ld\n", data); | |
425 | } | |
426 | ||
427 | static IIO_DEV_ATTR_VT_DATA(ad774x_show_vt_data); | |
428 | ||
429 | static ssize_t ad774x_show_cap_offs(struct device *dev, | |
430 | struct device_attribute *attr, | |
431 | char *buf) | |
432 | { | |
433 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 434 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
435 | |
436 | return sprintf(buf, "%d\n", chip->cap_offs); | |
437 | } | |
438 | ||
439 | static ssize_t ad774x_store_cap_offs(struct device *dev, | |
440 | struct device_attribute *attr, | |
441 | const char *buf, | |
442 | size_t len) | |
443 | { | |
444 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 445 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
446 | unsigned long data; |
447 | int ret; | |
448 | ||
449 | ret = strict_strtoul(buf, 10, &data); | |
450 | ||
451 | if ((!ret) && (data < 0x10000)) { | |
452 | ad774x_i2c_write(chip, AD774X_CAP_OFFH, data >> 8); | |
453 | ad774x_i2c_write(chip, AD774X_CAP_OFFL, data); | |
454 | chip->cap_offs = data; | |
455 | return len; | |
456 | } | |
457 | ||
458 | return -EINVAL; | |
459 | } | |
460 | ||
461 | static IIO_DEV_ATTR_CAP_OFFS(S_IRUGO | S_IWUSR, | |
462 | ad774x_show_cap_offs, | |
463 | ad774x_store_cap_offs); | |
464 | ||
465 | static ssize_t ad774x_show_cap_gain(struct device *dev, | |
466 | struct device_attribute *attr, | |
467 | char *buf) | |
468 | { | |
469 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 470 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
471 | |
472 | return sprintf(buf, "%d\n", chip->cap_gain); | |
473 | } | |
474 | ||
475 | static ssize_t ad774x_store_cap_gain(struct device *dev, | |
476 | struct device_attribute *attr, | |
477 | const char *buf, | |
478 | size_t len) | |
479 | { | |
480 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
febafb94 | 481 | struct ad774x_chip_info *chip = iio_priv(dev_info); |
671d85f2 BS |
482 | unsigned long data; |
483 | int ret; | |
484 | ||
485 | ret = strict_strtoul(buf, 10, &data); | |
486 | ||
487 | if ((!ret) && (data < 0x10000)) { | |
488 | ad774x_i2c_write(chip, AD774X_CAP_GAINH, data >> 8); | |
489 | ad774x_i2c_write(chip, AD774X_CAP_GAINL, data); | |
490 | chip->cap_gain = data; | |
491 | return len; | |
492 | } | |
493 | ||
494 | return -EINVAL; | |
495 | } | |
496 | ||
497 | static IIO_DEV_ATTR_CAP_GAIN(S_IRUGO | S_IWUSR, | |
498 | ad774x_show_cap_gain, | |
499 | ad774x_store_cap_gain); | |
500 | ||
671d85f2 BS |
501 | static struct attribute *ad774x_attributes[] = { |
502 | &iio_dev_attr_available_conversion_modes.dev_attr.attr, | |
503 | &iio_dev_attr_conversion_mode.dev_attr.attr, | |
322c9563 JC |
504 | &iio_dev_attr_in_capacitance_setup.dev_attr.attr, |
505 | &iio_dev_attr_in_voltage0_setup.dev_attr.attr, | |
671d85f2 | 506 | &iio_dev_attr_exec_setup.dev_attr.attr, |
322c9563 JC |
507 | &iio_dev_attr_in_capacitance_offs.dev_attr.attr, |
508 | &iio_dev_attr_in_capacitance_gain.dev_attr.attr, | |
509 | &iio_dev_attr_in_voltage0_gain.dev_attr.attr, | |
510 | &iio_dev_attr_in_voltage0_raw.dev_attr.attr, | |
511 | &iio_dev_attr_in_capacitance0_raw.dev_attr.attr, | |
512 | &iio_dev_attr_out_capacitance0_raw.dev_attr.attr, | |
513 | &iio_dev_attr_out_capacitance1_raw.dev_attr.attr, | |
671d85f2 BS |
514 | NULL, |
515 | }; | |
516 | ||
517 | static const struct attribute_group ad774x_attribute_group = { | |
518 | .attrs = ad774x_attributes, | |
519 | }; | |
520 | ||
521 | /* | |
522 | * data ready events | |
523 | */ | |
524 | ||
02db0bb3 JC |
525 | #define IIO_EVENT_CODE_CAP_RDY 0 |
526 | #define IIO_EVENT_CODE_VT_RDY 1 | |
671d85f2 BS |
527 | |
528 | #define IIO_EVENT_ATTR_CAP_RDY_SH(_evlist, _show, _store, _mask) \ | |
529 | IIO_EVENT_ATTR_SH(cap_rdy, _evlist, _show, _store, _mask) | |
530 | ||
531 | #define IIO_EVENT_ATTR_VT_RDY_SH(_evlist, _show, _store, _mask) \ | |
532 | IIO_EVENT_ATTR_SH(vt_rdy, _evlist, _show, _store, _mask) | |
533 | ||
724cf92d | 534 | static irqreturn_t ad774x_event_handler(int irq, void *private) |
671d85f2 | 535 | { |
724cf92d | 536 | struct iio_dev *indio_dev = private; |
febafb94 | 537 | struct ad774x_chip_info *chip = iio_priv(indio_dev); |
671d85f2 BS |
538 | u8 int_status; |
539 | ||
671d85f2 BS |
540 | ad774x_i2c_read(chip, AD774X_STATUS, &int_status, 1); |
541 | ||
542 | if (int_status & AD774X_STATUS_RDYCAP) | |
5aa96188 | 543 | iio_push_event(indio_dev, |
724cf92d JC |
544 | IIO_EVENT_CODE_CAP_RDY, |
545 | iio_get_time_ns()); | |
671d85f2 BS |
546 | |
547 | if (int_status & AD774X_STATUS_RDYVT) | |
5aa96188 | 548 | iio_push_event(indio_dev, |
724cf92d JC |
549 | IIO_EVENT_CODE_VT_RDY, |
550 | iio_get_time_ns()); | |
671d85f2 | 551 | |
724cf92d | 552 | return IRQ_HANDLED; |
671d85f2 BS |
553 | } |
554 | ||
724cf92d JC |
555 | static IIO_CONST_ATTR(cap_rdy_en, "1"); |
556 | static IIO_CONST_ATTR(vt_rdy_en, "1"); | |
671d85f2 BS |
557 | |
558 | static struct attribute *ad774x_event_attributes[] = { | |
724cf92d JC |
559 | &iio_const_attr_cap_rdy_en.dev_attr.attr, |
560 | &iio_const_attr_vt_rdy_en.dev_attr.attr, | |
671d85f2 BS |
561 | NULL, |
562 | }; | |
563 | ||
564 | static struct attribute_group ad774x_event_attribute_group = { | |
565 | .attrs = ad774x_event_attributes, | |
8e7d9672 | 566 | .name = "events", |
671d85f2 BS |
567 | }; |
568 | ||
6fe8135f JC |
569 | static const struct iio_info ad774x_info = { |
570 | .attrs = &ad774x_event_attribute_group, | |
571 | .event_attrs = &ad774x_event_attribute_group, | |
6fe8135f JC |
572 | .driver_module = THIS_MODULE, |
573 | }; | |
671d85f2 BS |
574 | /* |
575 | * device probe and remove | |
576 | */ | |
577 | ||
578 | static int __devinit ad774x_probe(struct i2c_client *client, | |
579 | const struct i2c_device_id *id) | |
580 | { | |
26d25ae3 | 581 | int ret; |
febafb94 JC |
582 | struct ad774x_chip_info *chip; |
583 | struct iio_dev *indio_dev; | |
584 | ||
585 | indio_dev = iio_allocate_device(sizeof(*chip)); | |
586 | if (indio_dev == NULL) { | |
671d85f2 BS |
587 | ret = -ENOMEM; |
588 | goto error_ret; | |
589 | } | |
febafb94 | 590 | chip = iio_priv(indio_dev); |
671d85f2 | 591 | /* this is only used for device removal purposes */ |
febafb94 | 592 | i2c_set_clientdata(client, indio_dev); |
671d85f2 BS |
593 | |
594 | chip->client = client; | |
671d85f2 | 595 | |
671d85f2 | 596 | /* Establish that the iio_dev is a child of the i2c device */ |
febafb94 JC |
597 | indio_dev->name = id->name; |
598 | indio_dev->dev.parent = &client->dev; | |
599 | indio_dev->info = &ad774x_info; | |
600 | indio_dev->modes = INDIO_DIRECT_MODE; | |
671d85f2 | 601 | |
671d85f2 | 602 | if (client->irq) { |
724cf92d JC |
603 | ret = request_threaded_irq(client->irq, |
604 | NULL, | |
605 | &ad774x_event_handler, | |
606 | IRQF_TRIGGER_FALLING, | |
607 | "ad774x", | |
febafb94 | 608 | indio_dev); |
671d85f2 BS |
609 | if (ret) |
610 | goto error_free_dev; | |
671d85f2 BS |
611 | } |
612 | ||
26d25ae3 JC |
613 | ret = iio_device_register(indio_dev); |
614 | if (ret) | |
615 | goto error_free_irq; | |
616 | ||
671d85f2 BS |
617 | dev_err(&client->dev, "%s capacitive sensor registered, irq: %d\n", id->name, client->irq); |
618 | ||
619 | return 0; | |
620 | ||
26d25ae3 JC |
621 | error_free_irq: |
622 | free_irq(client->irq, indio_dev); | |
671d85f2 | 623 | error_free_dev: |
26d25ae3 | 624 | iio_free_device(indio_dev); |
671d85f2 BS |
625 | error_ret: |
626 | return ret; | |
627 | } | |
628 | ||
629 | static int __devexit ad774x_remove(struct i2c_client *client) | |
630 | { | |
febafb94 | 631 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
671d85f2 BS |
632 | |
633 | if (client->irq) | |
724cf92d | 634 | free_irq(client->irq, indio_dev); |
671d85f2 | 635 | iio_device_unregister(indio_dev); |
671d85f2 BS |
636 | |
637 | return 0; | |
638 | } | |
639 | ||
640 | static const struct i2c_device_id ad774x_id[] = { | |
641 | { "ad7745", 0 }, | |
642 | { "ad7746", 0 }, | |
643 | { "ad7747", 0 }, | |
644 | {} | |
645 | }; | |
646 | ||
647 | MODULE_DEVICE_TABLE(i2c, ad774x_id); | |
648 | ||
649 | static struct i2c_driver ad774x_driver = { | |
650 | .driver = { | |
651 | .name = "ad774x", | |
652 | }, | |
653 | .probe = ad774x_probe, | |
654 | .remove = __devexit_p(ad774x_remove), | |
655 | .id_table = ad774x_id, | |
656 | }; | |
657 | ||
658 | static __init int ad774x_init(void) | |
659 | { | |
660 | return i2c_add_driver(&ad774x_driver); | |
661 | } | |
662 | ||
663 | static __exit void ad774x_exit(void) | |
664 | { | |
665 | i2c_del_driver(&ad774x_driver); | |
666 | } | |
667 | ||
668 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
669 | MODULE_DESCRIPTION("Analog Devices ad7745/6/7 capacitive sensor driver"); | |
670 | MODULE_LICENSE("GPL v2"); | |
671 | ||
672 | module_init(ad774x_init); | |
673 | module_exit(ad774x_exit); |