staging:iio: rework of attribute registration.
[deliverable/linux.git] / drivers / staging / iio / adc / ad7291.c
CommitLineData
ddaecd5b
SZ
1/*
2 * AD7291 digital temperature sensor driver supporting AD7291
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>
ddaecd5b
SZ
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>
ddaecd5b
SZ
18
19#include "../iio.h"
20#include "../sysfs.h"
21
22/*
23 * AD7291 registers definition
24 */
25#define AD7291_COMMAND 0
26#define AD7291_VOLTAGE 1
27#define AD7291_T_SENSE 2
28#define AD7291_T_AVERAGE 3
29#define AD7291_VOLTAGE_LIMIT_BASE 4
30#define AD7291_VOLTAGE_LIMIT_COUNT 8
31#define AD7291_T_SENSE_HIGH 0x1c
32#define AD7291_T_SENSE_LOW 0x1d
33#define AD7291_T_SENSE_HYST 0x1e
34#define AD7291_VOLTAGE_ALERT_STATUS 0x1f
35#define AD7291_T_ALERT_STATUS 0x20
36
37/*
38 * AD7291 command
39 */
40#define AD7291_AUTOCYCLE 0x1
41#define AD7291_RESET 0x2
42#define AD7291_ALART_CLEAR 0x4
43#define AD7291_ALART_POLARITY 0x8
44#define AD7291_EXT_REF 0x10
45#define AD7291_NOISE_DELAY 0x20
46#define AD7291_T_SENSE_MASK 0x40
47#define AD7291_VOLTAGE_MASK 0xff00
48#define AD7291_VOLTAGE_OFFSET 0x8
49
50/*
51 * AD7291 value masks
52 */
53#define AD7291_CHANNEL_MASK 0xf000
54#define AD7291_VALUE_MASK 0xfff
55#define AD7291_T_VALUE_SIGN 0x400
56#define AD7291_T_VALUE_FLOAT_OFFSET 2
57#define AD7291_T_VALUE_FLOAT_MASK 0x2
58
59/*
60 * struct ad7291_chip_info - chip specifc information
61 */
62
63struct ad7291_chip_info {
ddaecd5b 64 struct i2c_client *client;
ddaecd5b
SZ
65 u16 command;
66 u8 channels; /* Active voltage channels */
67};
68
69/*
70 * struct ad7291_chip_info - chip specifc information
71 */
72
73struct ad7291_limit_regs {
74 u16 data_high;
75 u16 data_low;
76 u16 hysteresis;
77};
78
79/*
80 * ad7291 register access by I2C
81 */
82static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
83{
84 struct i2c_client *client = chip->client;
85 int ret = 0;
86
87 ret = i2c_smbus_read_word_data(client, reg);
88 if (ret < 0) {
89 dev_err(&client->dev, "I2C read error\n");
90 return ret;
91 }
92
93 *data = swab16((u16)ret);
94
95 return 0;
96}
97
98static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
99{
100 struct i2c_client *client = chip->client;
101 int ret = 0;
102
103 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
104 if (ret < 0)
105 dev_err(&client->dev, "I2C write error\n");
106
107 return ret;
108}
109
110/* Returns negative errno, or else the number of words read. */
111static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
112{
113 struct i2c_client *client = chip->client;
114 u8 commands[4];
115 int ret = 0;
116 int i, count;
117
118 if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
119 count = 2;
120 else if (reg == AD7291_VOLTAGE) {
121 if (!chip->channels) {
122 dev_err(&client->dev, "No voltage channel is selected.\n");
123 return -EINVAL;
124 }
125 count = 2 + chip->channels * 2;
126 } else {
127 dev_err(&client->dev, "I2C wrong data register\n");
128 return -EINVAL;
129 }
130
131 commands[0] = 0;
132 commands[1] = (chip->command >> 8) & 0xff;
133 commands[2] = chip->command & 0xff;
134 commands[3] = reg;
135
136 ret = i2c_master_send(client, commands, 4);
137 if (ret < 0) {
138 dev_err(&client->dev, "I2C master send error\n");
139 return ret;
140 }
141
142 ret = i2c_master_recv(client, (u8 *)data, count);
143 if (ret < 0) {
144 dev_err(&client->dev, "I2C master receive error\n");
145 return ret;
146 }
147 ret >>= 2;
148
149 for (i = 0; i < ret; i++)
150 data[i] = swab16(data[i]);
151
152 return ret;
153}
154
155static ssize_t ad7291_show_mode(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
158{
159 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 160 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
161
162 if (chip->command & AD7291_AUTOCYCLE)
163 return sprintf(buf, "autocycle\n");
164 else
165 return sprintf(buf, "command\n");
166}
167
168static ssize_t ad7291_store_mode(struct device *dev,
169 struct device_attribute *attr,
170 const char *buf,
171 size_t len)
172{
173 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 174 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
175 u16 command;
176 int ret;
177
178 command = chip->command & (~AD7291_AUTOCYCLE);
179 if (strcmp(buf, "autocycle"))
180 command |= AD7291_AUTOCYCLE;
181
182 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
183 if (ret)
184 return -EIO;
185
186 chip->command = command;
187
188 return ret;
189}
190
191static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
192 ad7291_show_mode,
193 ad7291_store_mode,
194 0);
195
196static ssize_t ad7291_show_available_modes(struct device *dev,
197 struct device_attribute *attr,
198 char *buf)
199{
200 return sprintf(buf, "command\nautocycle\n");
201}
202
203static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
204
205static ssize_t ad7291_store_reset(struct device *dev,
206 struct device_attribute *attr,
207 const char *buf,
208 size_t len)
209{
210 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 211 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
212 u16 command;
213 int ret;
214
215 command = chip->command | AD7291_RESET;
216
217 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
218 if (ret)
219 return -EIO;
220
221 return ret;
222}
223
224static IIO_DEVICE_ATTR(reset, S_IWUSR,
225 NULL,
226 ad7291_store_reset,
227 0);
228
229static ssize_t ad7291_show_ext_ref(struct device *dev,
230 struct device_attribute *attr,
231 char *buf)
232{
233 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 234 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
235
236 return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
237}
238
239static ssize_t ad7291_store_ext_ref(struct device *dev,
240 struct device_attribute *attr,
241 const char *buf,
242 size_t len)
243{
244 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 245 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
246 u16 command;
247 int ret;
248
249 command = chip->command & (~AD7291_EXT_REF);
250 if (strcmp(buf, "1"))
251 command |= AD7291_EXT_REF;
252
253 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
254 if (ret)
255 return -EIO;
256
257 chip->command = command;
258
259 return ret;
260}
261
262static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
263 ad7291_show_ext_ref,
264 ad7291_store_ext_ref,
265 0);
266
267static ssize_t ad7291_show_noise_delay(struct device *dev,
268 struct device_attribute *attr,
269 char *buf)
270{
271 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 272 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
273
274 return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
275}
276
277static ssize_t ad7291_store_noise_delay(struct device *dev,
278 struct device_attribute *attr,
279 const char *buf,
280 size_t len)
281{
282 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 283 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
284 u16 command;
285 int ret;
286
287 command = chip->command & (~AD7291_NOISE_DELAY);
288 if (strcmp(buf, "1"))
289 command |= AD7291_NOISE_DELAY;
290
291 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
292 if (ret)
293 return -EIO;
294
295 chip->command = command;
296
297 return ret;
298}
299
300static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
301 ad7291_show_noise_delay,
302 ad7291_store_noise_delay,
303 0);
304
305static ssize_t ad7291_show_t_sense(struct device *dev,
306 struct device_attribute *attr,
307 char *buf)
308{
309 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 310 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
311 u16 data;
312 char sign = ' ';
313 int ret;
314
315 ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
316 if (ret)
317 return -EIO;
318
319 if (data & AD7291_T_VALUE_SIGN) {
320 /* convert supplement to positive value */
321 data = (AD7291_T_VALUE_SIGN << 1) - data;
322 sign = '-';
323 }
324
325 return sprintf(buf, "%c%d.%.2d\n", sign,
326 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
327 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
328}
329
330static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
331
332static ssize_t ad7291_show_t_average(struct device *dev,
333 struct device_attribute *attr,
334 char *buf)
335{
336 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 337 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
338 u16 data;
339 char sign = ' ';
340 int ret;
341
342 ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
343 if (ret)
344 return -EIO;
345
346 if (data & AD7291_T_VALUE_SIGN) {
347 /* convert supplement to positive value */
348 data = (AD7291_T_VALUE_SIGN << 1) - data;
349 sign = '-';
350 }
351
352 return sprintf(buf, "%c%d.%.2d\n", sign,
353 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
354 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
355}
356
357static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0);
358
359static ssize_t ad7291_show_voltage(struct device *dev,
360 struct device_attribute *attr,
361 char *buf)
362{
363 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 364 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
365 u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
366 int i, size, ret;
367
368 ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
369 if (ret)
370 return -EIO;
371
372 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
373 if (chip->command & (AD7291_T_SENSE_MASK << i)) {
374 ret = sprintf(buf, "channel[%d]=%d\n", i,
375 data[i] & AD7291_VALUE_MASK);
376 if (ret < 0)
377 break;
378 buf += ret;
379 size += ret;
380 }
381 }
382
383 return size;
384}
385
386static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
387
388static ssize_t ad7291_show_channel_mask(struct device *dev,
389 struct device_attribute *attr,
390 char *buf)
391{
392 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 393 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
394
395 return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
396 AD7291_VOLTAGE_OFFSET);
397}
398
399static ssize_t ad7291_store_channel_mask(struct device *dev,
400 struct device_attribute *attr,
401 const char *buf,
402 size_t len)
403{
404 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 405 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
406 u16 command;
407 unsigned long data;
408 int i, ret;
409
410 ret = strict_strtoul(buf, 16, &data);
411 if (ret || data > 0xff)
412 return -EINVAL;
413
414 command = chip->command & (~AD7291_VOLTAGE_MASK);
415 command |= data << AD7291_VOLTAGE_OFFSET;
416
417 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
418 if (ret)
419 return -EIO;
420
421 chip->command = command;
422
423 for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
424 if (chip->command & (AD7291_T_SENSE_MASK << i))
425 chip->channels++;
426 }
427
428 return ret;
429}
430
431static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
432 ad7291_show_channel_mask,
433 ad7291_store_channel_mask,
434 0);
435
ddaecd5b
SZ
436static struct attribute *ad7291_attributes[] = {
437 &iio_dev_attr_available_modes.dev_attr.attr,
438 &iio_dev_attr_mode.dev_attr.attr,
439 &iio_dev_attr_reset.dev_attr.attr,
440 &iio_dev_attr_ext_ref.dev_attr.attr,
441 &iio_dev_attr_noise_delay.dev_attr.attr,
442 &iio_dev_attr_t_sense.dev_attr.attr,
443 &iio_dev_attr_t_average.dev_attr.attr,
444 &iio_dev_attr_voltage.dev_attr.attr,
445 &iio_dev_attr_channel_mask.dev_attr.attr,
ddaecd5b
SZ
446 NULL,
447};
448
449static const struct attribute_group ad7291_attribute_group = {
450 .attrs = ad7291_attributes,
451};
452
453/*
454 * temperature bound events
455 */
456
58c0323c 457static irqreturn_t ad7291_event_handler(int irq, void *private)
ddaecd5b 458{
58c0323c 459 struct iio_dev *indio_dev = private;
d4397972 460 struct ad7291_chip_info *chip = iio_priv(private);
ddaecd5b
SZ
461 u16 t_status, v_status;
462 u16 command;
463 int i;
58c0323c 464 s64 timestamp = iio_get_time_ns();
ddaecd5b
SZ
465
466 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
58c0323c 467 return IRQ_HANDLED;
ddaecd5b
SZ
468
469 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
58c0323c 470 return IRQ_HANDLED;
ddaecd5b
SZ
471
472 if (!(t_status || v_status))
58c0323c 473 return IRQ_HANDLED;
ddaecd5b
SZ
474
475 command = chip->command | AD7291_ALART_CLEAR;
476 ad7291_i2c_write(chip, AD7291_COMMAND, command);
477
478 command = chip->command & ~AD7291_ALART_CLEAR;
479 ad7291_i2c_write(chip, AD7291_COMMAND, command);
480
b206c3bb 481 if (t_status & (1 << 0))
5aa96188 482 iio_push_event(indio_dev,
b206c3bb
JC
483 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
484 0,
485 IIO_EV_TYPE_THRESH,
486 IIO_EV_DIR_FALLING),
487 timestamp);
488 if (t_status & (1 << 1))
5aa96188 489 iio_push_event(indio_dev,
b206c3bb
JC
490 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
491 0,
492 IIO_EV_TYPE_THRESH,
493 IIO_EV_DIR_RISING),
494 timestamp);
495 if (t_status & (1 << 2))
5aa96188 496 iio_push_event(indio_dev,
b206c3bb
JC
497 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
498 0,
499 IIO_EV_TYPE_THRESH,
500 IIO_EV_DIR_FALLING),
501 timestamp);
502 if (t_status & (1 << 3))
5aa96188 503 iio_push_event(indio_dev,
b206c3bb
JC
504 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
505 0,
506 IIO_EV_TYPE_THRESH,
507 IIO_EV_DIR_RISING),
508 timestamp);
509
510 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
ddaecd5b 511 if (v_status & (1 << i))
5aa96188 512 iio_push_event(indio_dev,
b206c3bb
JC
513 IIO_UNMOD_EVENT_CODE(IIO_IN,
514 i/2,
515 IIO_EV_TYPE_THRESH,
516 IIO_EV_DIR_FALLING),
517 timestamp);
518 if (v_status & (1 << (i + 1)))
5aa96188 519 iio_push_event(indio_dev,
b206c3bb
JC
520 IIO_UNMOD_EVENT_CODE(IIO_IN,
521 i/2,
522 IIO_EV_TYPE_THRESH,
523 IIO_EV_DIR_RISING),
524 timestamp);
ddaecd5b 525 }
ddaecd5b 526
58c0323c 527 return IRQ_HANDLED;
ddaecd5b
SZ
528}
529
ddaecd5b
SZ
530static inline ssize_t ad7291_show_t_bound(struct device *dev,
531 struct device_attribute *attr,
ddaecd5b
SZ
532 char *buf)
533{
534 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 535 struct ad7291_chip_info *chip = iio_priv(dev_info);
58c0323c 536 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
ddaecd5b
SZ
537 u16 data;
538 char sign = ' ';
539 int ret;
540
58c0323c 541 ret = ad7291_i2c_read(chip, this_attr->address, &data);
ddaecd5b
SZ
542 if (ret)
543 return -EIO;
544
545 data &= AD7291_VALUE_MASK;
546 if (data & AD7291_T_VALUE_SIGN) {
547 /* convert supplement to positive value */
548 data = (AD7291_T_VALUE_SIGN << 1) - data;
549 sign = '-';
550 }
551
552 return sprintf(buf, "%c%d.%.2d\n", sign,
553 data >> AD7291_T_VALUE_FLOAT_OFFSET,
554 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
555}
556
557static inline ssize_t ad7291_set_t_bound(struct device *dev,
558 struct device_attribute *attr,
ddaecd5b
SZ
559 const char *buf,
560 size_t len)
561{
562 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 563 struct ad7291_chip_info *chip = iio_priv(dev_info);
58c0323c 564 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
ddaecd5b
SZ
565 long tmp1, tmp2;
566 u16 data;
567 char *pos;
568 int ret;
569
570 pos = strchr(buf, '.');
571
572 ret = strict_strtol(buf, 10, &tmp1);
573
574 if (ret || tmp1 > 127 || tmp1 < -128)
575 return -EINVAL;
576
577 if (pos) {
578 len = strlen(pos);
579 if (len > AD7291_T_VALUE_FLOAT_OFFSET)
580 len = AD7291_T_VALUE_FLOAT_OFFSET;
581 pos[len] = 0;
582 ret = strict_strtol(pos, 10, &tmp2);
583
584 if (!ret)
585 tmp2 = (tmp2 / 25) * 25;
586 }
587
588 if (tmp1 < 0)
589 data = (u16)(-tmp1);
590 else
591 data = (u16)tmp1;
592 data = (data << AD7291_T_VALUE_FLOAT_OFFSET) |
593 (tmp2 & AD7291_T_VALUE_FLOAT_MASK);
594 if (tmp1 < 0)
595 /* convert positive value to supplyment */
596 data = (AD7291_T_VALUE_SIGN << 1) - data;
597
58c0323c 598 ret = ad7291_i2c_write(chip, this_attr->address, data);
ddaecd5b
SZ
599 if (ret)
600 return -EIO;
601
602 return ret;
603}
604
ddaecd5b
SZ
605static inline ssize_t ad7291_show_v_bound(struct device *dev,
606 struct device_attribute *attr,
607 u8 bound_reg,
608 char *buf)
609{
610 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 611 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
612 u16 data;
613 int ret;
614
615 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
616 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
617 AD7291_VOLTAGE_LIMIT_COUNT)
618 return -EINVAL;
619
620 ret = ad7291_i2c_read(chip, bound_reg, &data);
621 if (ret)
622 return -EIO;
623
624 data &= AD7291_VALUE_MASK;
625
626 return sprintf(buf, "%d\n", data);
627}
628
629static inline ssize_t ad7291_set_v_bound(struct device *dev,
630 struct device_attribute *attr,
631 u8 bound_reg,
632 const char *buf,
633 size_t len)
634{
635 struct iio_dev *dev_info = dev_get_drvdata(dev);
d4397972 636 struct ad7291_chip_info *chip = iio_priv(dev_info);
ddaecd5b
SZ
637 unsigned long value;
638 u16 data;
639 int ret;
640
641 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
642 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
643 AD7291_VOLTAGE_LIMIT_COUNT)
644 return -EINVAL;
645
646 ret = strict_strtoul(buf, 10, &value);
647
648 if (ret || value >= 4096)
649 return -EINVAL;
650
651 data = (u16)value;
652 ret = ad7291_i2c_write(chip, bound_reg, data);
653 if (ret)
654 return -EIO;
655
656 return ret;
657}
658
58c0323c
JC
659static IIO_DEVICE_ATTR(t_sense_high_value,
660 S_IRUGO | S_IWUSR,
661 ad7291_show_t_bound, ad7291_set_t_bound,
662 AD7291_T_SENSE_HIGH);
663static IIO_DEVICE_ATTR(t_sense_low_value,
664 S_IRUGO | S_IWUSR,
665 ad7291_show_t_bound, ad7291_set_t_bound,
666 AD7291_T_SENSE_LOW);
667static IIO_DEVICE_ATTR(t_sense_hyst_value,
668 S_IRUGO | S_IWUSR,
669 ad7291_show_t_bound, ad7291_set_t_bound,
670 AD7291_T_SENSE_HYST);
671static IIO_DEVICE_ATTR(v0_high,
672 S_IRUGO | S_IWUSR,
673 ad7291_show_t_bound, ad7291_set_t_bound, 0x04);
674static IIO_DEVICE_ATTR(v0_low,
675 S_IRUGO | S_IWUSR,
676 ad7291_show_t_bound, ad7291_set_t_bound, 0x05);
677static IIO_DEVICE_ATTR(v0_hyst,
678 S_IRUGO | S_IWUSR,
679 ad7291_show_t_bound, ad7291_set_t_bound, 0x06);
680static IIO_DEVICE_ATTR(v1_high,
681 S_IRUGO | S_IWUSR,
682 ad7291_show_t_bound, ad7291_set_t_bound, 0x07);
683static IIO_DEVICE_ATTR(v1_low,
684 S_IRUGO | S_IWUSR,
685 ad7291_show_t_bound, ad7291_set_t_bound, 0x08);
686static IIO_DEVICE_ATTR(v1_hyst,
687 S_IRUGO | S_IWUSR,
688 ad7291_show_t_bound, ad7291_set_t_bound, 0x09);
689static IIO_DEVICE_ATTR(v2_high,
690 S_IRUGO | S_IWUSR,
691 ad7291_show_t_bound, ad7291_set_t_bound, 0x0A);
692static IIO_DEVICE_ATTR(v2_low,
693 S_IRUGO | S_IWUSR,
694 ad7291_show_t_bound, ad7291_set_t_bound, 0x0B);
695static IIO_DEVICE_ATTR(v2_hyst,
696 S_IRUGO | S_IWUSR,
697 ad7291_show_t_bound, ad7291_set_t_bound, 0x0C);
698static IIO_DEVICE_ATTR(v3_high,
699 S_IRUGO | S_IWUSR,
700 /* Datasheet suggests this one and this one only
701 has the registers in different order */
702 ad7291_show_t_bound, ad7291_set_t_bound, 0x0E);
703static IIO_DEVICE_ATTR(v3_low,
704 S_IRUGO | S_IWUSR,
705 ad7291_show_t_bound, ad7291_set_t_bound, 0x0D);
706static IIO_DEVICE_ATTR(v3_hyst,
707 S_IRUGO | S_IWUSR,
708 ad7291_show_t_bound, ad7291_set_t_bound, 0x0F);
709static IIO_DEVICE_ATTR(v4_high,
710 S_IRUGO | S_IWUSR,
711 ad7291_show_t_bound, ad7291_set_t_bound, 0x10);
712static IIO_DEVICE_ATTR(v4_low,
713 S_IRUGO | S_IWUSR,
714 ad7291_show_t_bound, ad7291_set_t_bound, 0x11);
715static IIO_DEVICE_ATTR(v4_hyst,
716 S_IRUGO | S_IWUSR,
717 ad7291_show_t_bound, ad7291_set_t_bound, 0x12);
718static IIO_DEVICE_ATTR(v5_high,
719 S_IRUGO | S_IWUSR,
720 ad7291_show_t_bound, ad7291_set_t_bound, 0x13);
721static IIO_DEVICE_ATTR(v5_low,
722 S_IRUGO | S_IWUSR,
723 ad7291_show_t_bound, ad7291_set_t_bound, 0x14);
724static IIO_DEVICE_ATTR(v5_hyst,
725 S_IRUGO | S_IWUSR,
726 ad7291_show_t_bound, ad7291_set_t_bound, 0x15);
727static IIO_DEVICE_ATTR(v6_high,
728 S_IRUGO | S_IWUSR,
729 ad7291_show_t_bound, ad7291_set_t_bound, 0x16);
730static IIO_DEVICE_ATTR(v6_low,
731 S_IRUGO | S_IWUSR,
732 ad7291_show_t_bound, ad7291_set_t_bound, 0x17);
733static IIO_DEVICE_ATTR(v6_hyst,
734 S_IRUGO | S_IWUSR,
735 ad7291_show_t_bound, ad7291_set_t_bound, 0x18);
736static IIO_DEVICE_ATTR(v7_high,
737 S_IRUGO | S_IWUSR,
738 ad7291_show_t_bound, ad7291_set_t_bound, 0x19);
739static IIO_DEVICE_ATTR(v7_low,
740 S_IRUGO | S_IWUSR,
741 ad7291_show_t_bound, ad7291_set_t_bound, 0x1A);
742static IIO_DEVICE_ATTR(v7_hyst,
743 S_IRUGO | S_IWUSR,
744 ad7291_show_t_bound, ad7291_set_t_bound, 0x1B);
ddaecd5b
SZ
745
746static struct attribute *ad7291_event_attributes[] = {
58c0323c
JC
747 &iio_dev_attr_t_sense_high_value.dev_attr.attr,
748 &iio_dev_attr_t_sense_low_value.dev_attr.attr,
749 &iio_dev_attr_t_sense_hyst_value.dev_attr.attr,
750 &iio_dev_attr_v0_high.dev_attr.attr,
751 &iio_dev_attr_v0_low.dev_attr.attr,
752 &iio_dev_attr_v0_hyst.dev_attr.attr,
753 &iio_dev_attr_v1_high.dev_attr.attr,
754 &iio_dev_attr_v1_low.dev_attr.attr,
755 &iio_dev_attr_v1_hyst.dev_attr.attr,
756 &iio_dev_attr_v2_high.dev_attr.attr,
757 &iio_dev_attr_v2_low.dev_attr.attr,
758 &iio_dev_attr_v2_hyst.dev_attr.attr,
759 &iio_dev_attr_v3_high.dev_attr.attr,
760 &iio_dev_attr_v3_low.dev_attr.attr,
761 &iio_dev_attr_v3_hyst.dev_attr.attr,
762 &iio_dev_attr_v4_high.dev_attr.attr,
763 &iio_dev_attr_v4_low.dev_attr.attr,
764 &iio_dev_attr_v4_hyst.dev_attr.attr,
765 &iio_dev_attr_v5_high.dev_attr.attr,
766 &iio_dev_attr_v5_low.dev_attr.attr,
767 &iio_dev_attr_v5_hyst.dev_attr.attr,
768 &iio_dev_attr_v6_high.dev_attr.attr,
769 &iio_dev_attr_v6_low.dev_attr.attr,
770 &iio_dev_attr_v6_hyst.dev_attr.attr,
771 &iio_dev_attr_v7_high.dev_attr.attr,
772 &iio_dev_attr_v7_low.dev_attr.attr,
773 &iio_dev_attr_v7_hyst.dev_attr.attr,
ddaecd5b
SZ
774 NULL,
775};
776
777static struct attribute_group ad7291_event_attribute_group = {
778 .attrs = ad7291_event_attributes,
779};
780
6fe8135f
JC
781static const struct iio_info ad7291_info = {
782 .attrs = &ad7291_attribute_group,
6fe8135f
JC
783 .event_attrs = &ad7291_event_attribute_group,
784};
785
ddaecd5b
SZ
786/*
787 * device probe and remove
788 */
789
790static int __devinit ad7291_probe(struct i2c_client *client,
791 const struct i2c_device_id *id)
792{
793 struct ad7291_chip_info *chip;
d4397972 794 struct iio_dev *indio_dev;
ddaecd5b
SZ
795 int ret = 0;
796
d4397972
JC
797 indio_dev = iio_allocate_device(sizeof(*chip));
798 if (indio_dev == NULL) {
799 ret = -ENOMEM;
800 goto error_ret;
801 }
03bda05d 802 chip = iio_priv(indio_dev);
ddaecd5b 803 /* this is only used for device removal purposes */
d4397972 804 i2c_set_clientdata(client, indio_dev);
ddaecd5b
SZ
805
806 chip->client = client;
ddaecd5b
SZ
807 chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
808
d4397972
JC
809 indio_dev->name = id->name;
810 indio_dev->dev.parent = &client->dev;
811 indio_dev->info = &ad7291_info;
812 indio_dev->modes = INDIO_DIRECT_MODE;
ddaecd5b 813
ddaecd5b 814 if (client->irq > 0) {
58c0323c
JC
815 ret = request_threaded_irq(client->irq,
816 NULL,
817 &ad7291_event_handler,
818 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
845bd12a 819 id->name,
d4397972 820 indio_dev);
ddaecd5b 821 if (ret)
26d25ae3 822 goto error_free_dev;
ddaecd5b 823
ddaecd5b
SZ
824 /* set irq polarity low level */
825 chip->command |= AD7291_ALART_POLARITY;
826 }
827
828 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
829 if (ret) {
830 ret = -EIO;
831 goto error_unreg_irq;
832 }
833
26d25ae3
JC
834 ret = iio_device_register(indio_dev);
835 if (ret)
836 goto error_unreg_irq;
837
ddaecd5b
SZ
838 dev_info(&client->dev, "%s temperature sensor registered.\n",
839 id->name);
840
841 return 0;
842
843error_unreg_irq:
d4397972 844 free_irq(client->irq, indio_dev);
ddaecd5b 845error_free_dev:
d4397972
JC
846 iio_free_device(indio_dev);
847error_ret:
ddaecd5b
SZ
848 return ret;
849}
850
851static int __devexit ad7291_remove(struct i2c_client *client)
852{
d4397972 853 struct iio_dev *indio_dev = i2c_get_clientdata(client);
ddaecd5b
SZ
854
855 if (client->irq)
d4397972 856 free_irq(client->irq, indio_dev);
ddaecd5b 857 iio_device_unregister(indio_dev);
ddaecd5b
SZ
858
859 return 0;
860}
861
862static const struct i2c_device_id ad7291_id[] = {
863 { "ad7291", 0 },
864 {}
865};
866
867MODULE_DEVICE_TABLE(i2c, ad7291_id);
868
869static struct i2c_driver ad7291_driver = {
870 .driver = {
871 .name = "ad7291",
872 },
873 .probe = ad7291_probe,
874 .remove = __devexit_p(ad7291_remove),
875 .id_table = ad7291_id,
876};
877
878static __init int ad7291_init(void)
879{
880 return i2c_add_driver(&ad7291_driver);
881}
882
883static __exit void ad7291_exit(void)
884{
885 i2c_del_driver(&ad7291_driver);
886}
887
888MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
889MODULE_DESCRIPTION("Analog Devices AD7291 digital"
890 " temperature sensor driver");
891MODULE_LICENSE("GPL v2");
892
893module_init(ad7291_init);
894module_exit(ad7291_exit);
This page took 0.17249 seconds and 5 git commands to generate.