staging:iio:adc:ad7887 move to irqchip based trigger handling.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / adc / ad7291.c
blob23ad18ec320621df6a3eb21220814ee7db7ab3d9
1 /*
2 * AD7291 digital temperature sensor driver supporting AD7291
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/gpio.h>
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>
18 #include "../iio.h"
19 #include "../sysfs.h"
22 * AD7291 registers definition
24 #define AD7291_COMMAND 0
25 #define AD7291_VOLTAGE 1
26 #define AD7291_T_SENSE 2
27 #define AD7291_T_AVERAGE 3
28 #define AD7291_VOLTAGE_LIMIT_BASE 4
29 #define AD7291_VOLTAGE_LIMIT_COUNT 8
30 #define AD7291_T_SENSE_HIGH 0x1c
31 #define AD7291_T_SENSE_LOW 0x1d
32 #define AD7291_T_SENSE_HYST 0x1e
33 #define AD7291_VOLTAGE_ALERT_STATUS 0x1f
34 #define AD7291_T_ALERT_STATUS 0x20
37 * AD7291 command
39 #define AD7291_AUTOCYCLE 0x1
40 #define AD7291_RESET 0x2
41 #define AD7291_ALART_CLEAR 0x4
42 #define AD7291_ALART_POLARITY 0x8
43 #define AD7291_EXT_REF 0x10
44 #define AD7291_NOISE_DELAY 0x20
45 #define AD7291_T_SENSE_MASK 0x40
46 #define AD7291_VOLTAGE_MASK 0xff00
47 #define AD7291_VOLTAGE_OFFSET 0x8
50 * AD7291 value masks
52 #define AD7291_CHANNEL_MASK 0xf000
53 #define AD7291_VALUE_MASK 0xfff
54 #define AD7291_T_VALUE_SIGN 0x400
55 #define AD7291_T_VALUE_FLOAT_OFFSET 2
56 #define AD7291_T_VALUE_FLOAT_MASK 0x2
59 * struct ad7291_chip_info - chip specifc information
62 struct ad7291_chip_info {
63 const char *name;
64 struct i2c_client *client;
65 struct iio_dev *indio_dev;
66 u16 command;
67 u8 channels; /* Active voltage channels */
71 * struct ad7291_chip_info - chip specifc information
74 struct ad7291_limit_regs {
75 u16 data_high;
76 u16 data_low;
77 u16 hysteresis;
81 * ad7291 register access by I2C
83 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
85 struct i2c_client *client = chip->client;
86 int ret = 0;
88 ret = i2c_smbus_read_word_data(client, reg);
89 if (ret < 0) {
90 dev_err(&client->dev, "I2C read error\n");
91 return ret;
94 *data = swab16((u16)ret);
96 return 0;
99 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
101 struct i2c_client *client = chip->client;
102 int ret = 0;
104 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
105 if (ret < 0)
106 dev_err(&client->dev, "I2C write error\n");
108 return ret;
111 /* Returns negative errno, or else the number of words read. */
112 static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
114 struct i2c_client *client = chip->client;
115 u8 commands[4];
116 int ret = 0;
117 int i, count;
119 if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
120 count = 2;
121 else if (reg == AD7291_VOLTAGE) {
122 if (!chip->channels) {
123 dev_err(&client->dev, "No voltage channel is selected.\n");
124 return -EINVAL;
126 count = 2 + chip->channels * 2;
127 } else {
128 dev_err(&client->dev, "I2C wrong data register\n");
129 return -EINVAL;
132 commands[0] = 0;
133 commands[1] = (chip->command >> 8) & 0xff;
134 commands[2] = chip->command & 0xff;
135 commands[3] = reg;
137 ret = i2c_master_send(client, commands, 4);
138 if (ret < 0) {
139 dev_err(&client->dev, "I2C master send error\n");
140 return ret;
143 ret = i2c_master_recv(client, (u8 *)data, count);
144 if (ret < 0) {
145 dev_err(&client->dev, "I2C master receive error\n");
146 return ret;
148 ret >>= 2;
150 for (i = 0; i < ret; i++)
151 data[i] = swab16(data[i]);
153 return ret;
156 static ssize_t ad7291_show_mode(struct device *dev,
157 struct device_attribute *attr,
158 char *buf)
160 struct iio_dev *dev_info = dev_get_drvdata(dev);
161 struct ad7291_chip_info *chip = dev_info->dev_data;
163 if (chip->command & AD7291_AUTOCYCLE)
164 return sprintf(buf, "autocycle\n");
165 else
166 return sprintf(buf, "command\n");
169 static ssize_t ad7291_store_mode(struct device *dev,
170 struct device_attribute *attr,
171 const char *buf,
172 size_t len)
174 struct iio_dev *dev_info = dev_get_drvdata(dev);
175 struct ad7291_chip_info *chip = dev_info->dev_data;
176 u16 command;
177 int ret;
179 command = chip->command & (~AD7291_AUTOCYCLE);
180 if (strcmp(buf, "autocycle"))
181 command |= AD7291_AUTOCYCLE;
183 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
184 if (ret)
185 return -EIO;
187 chip->command = command;
189 return ret;
192 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
193 ad7291_show_mode,
194 ad7291_store_mode,
197 static ssize_t ad7291_show_available_modes(struct device *dev,
198 struct device_attribute *attr,
199 char *buf)
201 return sprintf(buf, "command\nautocycle\n");
204 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
206 static ssize_t ad7291_store_reset(struct device *dev,
207 struct device_attribute *attr,
208 const char *buf,
209 size_t len)
211 struct iio_dev *dev_info = dev_get_drvdata(dev);
212 struct ad7291_chip_info *chip = dev_info->dev_data;
213 u16 command;
214 int ret;
216 command = chip->command | AD7291_RESET;
218 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
219 if (ret)
220 return -EIO;
222 return ret;
225 static IIO_DEVICE_ATTR(reset, S_IWUSR,
226 NULL,
227 ad7291_store_reset,
230 static ssize_t ad7291_show_ext_ref(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
234 struct iio_dev *dev_info = dev_get_drvdata(dev);
235 struct ad7291_chip_info *chip = dev_info->dev_data;
237 return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
240 static ssize_t ad7291_store_ext_ref(struct device *dev,
241 struct device_attribute *attr,
242 const char *buf,
243 size_t len)
245 struct iio_dev *dev_info = dev_get_drvdata(dev);
246 struct ad7291_chip_info *chip = dev_info->dev_data;
247 u16 command;
248 int ret;
250 command = chip->command & (~AD7291_EXT_REF);
251 if (strcmp(buf, "1"))
252 command |= AD7291_EXT_REF;
254 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
255 if (ret)
256 return -EIO;
258 chip->command = command;
260 return ret;
263 static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
264 ad7291_show_ext_ref,
265 ad7291_store_ext_ref,
268 static ssize_t ad7291_show_noise_delay(struct device *dev,
269 struct device_attribute *attr,
270 char *buf)
272 struct iio_dev *dev_info = dev_get_drvdata(dev);
273 struct ad7291_chip_info *chip = dev_info->dev_data;
275 return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
278 static ssize_t ad7291_store_noise_delay(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf,
281 size_t len)
283 struct iio_dev *dev_info = dev_get_drvdata(dev);
284 struct ad7291_chip_info *chip = dev_info->dev_data;
285 u16 command;
286 int ret;
288 command = chip->command & (~AD7291_NOISE_DELAY);
289 if (strcmp(buf, "1"))
290 command |= AD7291_NOISE_DELAY;
292 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
293 if (ret)
294 return -EIO;
296 chip->command = command;
298 return ret;
301 static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
302 ad7291_show_noise_delay,
303 ad7291_store_noise_delay,
306 static ssize_t ad7291_show_t_sense(struct device *dev,
307 struct device_attribute *attr,
308 char *buf)
310 struct iio_dev *dev_info = dev_get_drvdata(dev);
311 struct ad7291_chip_info *chip = dev_info->dev_data;
312 u16 data;
313 char sign = ' ';
314 int ret;
316 ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
317 if (ret)
318 return -EIO;
320 if (data & AD7291_T_VALUE_SIGN) {
321 /* convert supplement to positive value */
322 data = (AD7291_T_VALUE_SIGN << 1) - data;
323 sign = '-';
326 return sprintf(buf, "%c%d.%.2d\n", sign,
327 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
328 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
331 static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
333 static ssize_t ad7291_show_t_average(struct device *dev,
334 struct device_attribute *attr,
335 char *buf)
337 struct iio_dev *dev_info = dev_get_drvdata(dev);
338 struct ad7291_chip_info *chip = dev_info->dev_data;
339 u16 data;
340 char sign = ' ';
341 int ret;
343 ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
344 if (ret)
345 return -EIO;
347 if (data & AD7291_T_VALUE_SIGN) {
348 /* convert supplement to positive value */
349 data = (AD7291_T_VALUE_SIGN << 1) - data;
350 sign = '-';
353 return sprintf(buf, "%c%d.%.2d\n", sign,
354 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
355 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
358 static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0);
360 static ssize_t ad7291_show_voltage(struct device *dev,
361 struct device_attribute *attr,
362 char *buf)
364 struct iio_dev *dev_info = dev_get_drvdata(dev);
365 struct ad7291_chip_info *chip = dev_info->dev_data;
366 u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
367 int i, size, ret;
369 ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
370 if (ret)
371 return -EIO;
373 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
374 if (chip->command & (AD7291_T_SENSE_MASK << i)) {
375 ret = sprintf(buf, "channel[%d]=%d\n", i,
376 data[i] & AD7291_VALUE_MASK);
377 if (ret < 0)
378 break;
379 buf += ret;
380 size += ret;
384 return size;
387 static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
389 static ssize_t ad7291_show_channel_mask(struct device *dev,
390 struct device_attribute *attr,
391 char *buf)
393 struct iio_dev *dev_info = dev_get_drvdata(dev);
394 struct ad7291_chip_info *chip = dev_info->dev_data;
396 return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
397 AD7291_VOLTAGE_OFFSET);
400 static ssize_t ad7291_store_channel_mask(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf,
403 size_t len)
405 struct iio_dev *dev_info = dev_get_drvdata(dev);
406 struct ad7291_chip_info *chip = dev_info->dev_data;
407 u16 command;
408 unsigned long data;
409 int i, ret;
411 ret = strict_strtoul(buf, 16, &data);
412 if (ret || data > 0xff)
413 return -EINVAL;
415 command = chip->command & (~AD7291_VOLTAGE_MASK);
416 command |= data << AD7291_VOLTAGE_OFFSET;
418 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
419 if (ret)
420 return -EIO;
422 chip->command = command;
424 for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
425 if (chip->command & (AD7291_T_SENSE_MASK << i))
426 chip->channels++;
429 return ret;
432 static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
433 ad7291_show_channel_mask,
434 ad7291_store_channel_mask,
437 static ssize_t ad7291_show_name(struct device *dev,
438 struct device_attribute *attr,
439 char *buf)
441 struct iio_dev *dev_info = dev_get_drvdata(dev);
442 struct ad7291_chip_info *chip = dev_info->dev_data;
443 return sprintf(buf, "%s\n", chip->name);
446 static IIO_DEVICE_ATTR(name, S_IRUGO, ad7291_show_name, NULL, 0);
448 static struct attribute *ad7291_attributes[] = {
449 &iio_dev_attr_available_modes.dev_attr.attr,
450 &iio_dev_attr_mode.dev_attr.attr,
451 &iio_dev_attr_reset.dev_attr.attr,
452 &iio_dev_attr_ext_ref.dev_attr.attr,
453 &iio_dev_attr_noise_delay.dev_attr.attr,
454 &iio_dev_attr_t_sense.dev_attr.attr,
455 &iio_dev_attr_t_average.dev_attr.attr,
456 &iio_dev_attr_voltage.dev_attr.attr,
457 &iio_dev_attr_channel_mask.dev_attr.attr,
458 &iio_dev_attr_name.dev_attr.attr,
459 NULL,
462 static const struct attribute_group ad7291_attribute_group = {
463 .attrs = ad7291_attributes,
467 * temperature bound events
470 #define IIO_EVENT_CODE_AD7291_T_SENSE_HIGH IIO_BUFFER_EVENT_CODE(0)
471 #define IIO_EVENT_CODE_AD7291_T_SENSE_LOW IIO_BUFFER_EVENT_CODE(1)
472 #define IIO_EVENT_CODE_AD7291_T_AVG_HIGH IIO_BUFFER_EVENT_CODE(2)
473 #define IIO_EVENT_CODE_AD7291_T_AVG_LOW IIO_BUFFER_EVENT_CODE(3)
474 #define IIO_EVENT_CODE_AD7291_VOLTAGE_BASE IIO_BUFFER_EVENT_CODE(4)
476 static irqreturn_t ad7291_event_handler(int irq, void *private)
478 struct iio_dev *indio_dev = private;
479 struct ad7291_chip_info *chip = iio_dev_get_devdata(private);
480 u16 t_status, v_status;
481 u16 command;
482 int i;
483 s64 timestamp = iio_get_time_ns();
485 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
486 return IRQ_HANDLED;
488 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
489 return IRQ_HANDLED;
491 if (!(t_status || v_status))
492 return IRQ_HANDLED;
494 command = chip->command | AD7291_ALART_CLEAR;
495 ad7291_i2c_write(chip, AD7291_COMMAND, command);
497 command = chip->command & ~AD7291_ALART_CLEAR;
498 ad7291_i2c_write(chip, AD7291_COMMAND, command);
500 for (i = 0; i < 4; i++) {
501 if (t_status & (1 << i))
502 iio_push_event(indio_dev, 0,
503 IIO_EVENT_CODE_AD7291_T_SENSE_HIGH + i,
504 timestamp);
507 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i++) {
508 if (v_status & (1 << i))
509 iio_push_event(indio_dev, 0,
510 IIO_EVENT_CODE_AD7291_VOLTAGE_BASE + i,
511 timestamp);
514 return IRQ_HANDLED;
517 static inline ssize_t ad7291_show_t_bound(struct device *dev,
518 struct device_attribute *attr,
519 char *buf)
521 struct iio_dev *dev_info = dev_get_drvdata(dev);
522 struct ad7291_chip_info *chip = dev_info->dev_data;
523 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
524 u16 data;
525 char sign = ' ';
526 int ret;
528 ret = ad7291_i2c_read(chip, this_attr->address, &data);
529 if (ret)
530 return -EIO;
532 data &= AD7291_VALUE_MASK;
533 if (data & AD7291_T_VALUE_SIGN) {
534 /* convert supplement to positive value */
535 data = (AD7291_T_VALUE_SIGN << 1) - data;
536 sign = '-';
539 return sprintf(buf, "%c%d.%.2d\n", sign,
540 data >> AD7291_T_VALUE_FLOAT_OFFSET,
541 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
544 static inline ssize_t ad7291_set_t_bound(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf,
547 size_t len)
549 struct iio_dev *dev_info = dev_get_drvdata(dev);
550 struct ad7291_chip_info *chip = dev_info->dev_data;
551 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
552 long tmp1, tmp2;
553 u16 data;
554 char *pos;
555 int ret;
557 pos = strchr(buf, '.');
559 ret = strict_strtol(buf, 10, &tmp1);
561 if (ret || tmp1 > 127 || tmp1 < -128)
562 return -EINVAL;
564 if (pos) {
565 len = strlen(pos);
566 if (len > AD7291_T_VALUE_FLOAT_OFFSET)
567 len = AD7291_T_VALUE_FLOAT_OFFSET;
568 pos[len] = 0;
569 ret = strict_strtol(pos, 10, &tmp2);
571 if (!ret)
572 tmp2 = (tmp2 / 25) * 25;
575 if (tmp1 < 0)
576 data = (u16)(-tmp1);
577 else
578 data = (u16)tmp1;
579 data = (data << AD7291_T_VALUE_FLOAT_OFFSET) |
580 (tmp2 & AD7291_T_VALUE_FLOAT_MASK);
581 if (tmp1 < 0)
582 /* convert positive value to supplyment */
583 data = (AD7291_T_VALUE_SIGN << 1) - data;
585 ret = ad7291_i2c_write(chip, this_attr->address, data);
586 if (ret)
587 return -EIO;
589 return ret;
592 static inline ssize_t ad7291_show_v_bound(struct device *dev,
593 struct device_attribute *attr,
594 u8 bound_reg,
595 char *buf)
597 struct iio_dev *dev_info = dev_get_drvdata(dev);
598 struct ad7291_chip_info *chip = dev_info->dev_data;
599 u16 data;
600 int ret;
602 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
603 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
604 AD7291_VOLTAGE_LIMIT_COUNT)
605 return -EINVAL;
607 ret = ad7291_i2c_read(chip, bound_reg, &data);
608 if (ret)
609 return -EIO;
611 data &= AD7291_VALUE_MASK;
613 return sprintf(buf, "%d\n", data);
616 static inline ssize_t ad7291_set_v_bound(struct device *dev,
617 struct device_attribute *attr,
618 u8 bound_reg,
619 const char *buf,
620 size_t len)
622 struct iio_dev *dev_info = dev_get_drvdata(dev);
623 struct ad7291_chip_info *chip = dev_info->dev_data;
624 unsigned long value;
625 u16 data;
626 int ret;
628 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
629 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
630 AD7291_VOLTAGE_LIMIT_COUNT)
631 return -EINVAL;
633 ret = strict_strtoul(buf, 10, &value);
635 if (ret || value >= 4096)
636 return -EINVAL;
638 data = (u16)value;
639 ret = ad7291_i2c_write(chip, bound_reg, data);
640 if (ret)
641 return -EIO;
643 return ret;
646 static IIO_DEVICE_ATTR(t_sense_high_value,
647 S_IRUGO | S_IWUSR,
648 ad7291_show_t_bound, ad7291_set_t_bound,
649 AD7291_T_SENSE_HIGH);
650 static IIO_DEVICE_ATTR(t_sense_low_value,
651 S_IRUGO | S_IWUSR,
652 ad7291_show_t_bound, ad7291_set_t_bound,
653 AD7291_T_SENSE_LOW);
654 static IIO_DEVICE_ATTR(t_sense_hyst_value,
655 S_IRUGO | S_IWUSR,
656 ad7291_show_t_bound, ad7291_set_t_bound,
657 AD7291_T_SENSE_HYST);
658 static IIO_DEVICE_ATTR(v0_high,
659 S_IRUGO | S_IWUSR,
660 ad7291_show_t_bound, ad7291_set_t_bound, 0x04);
661 static IIO_DEVICE_ATTR(v0_low,
662 S_IRUGO | S_IWUSR,
663 ad7291_show_t_bound, ad7291_set_t_bound, 0x05);
664 static IIO_DEVICE_ATTR(v0_hyst,
665 S_IRUGO | S_IWUSR,
666 ad7291_show_t_bound, ad7291_set_t_bound, 0x06);
667 static IIO_DEVICE_ATTR(v1_high,
668 S_IRUGO | S_IWUSR,
669 ad7291_show_t_bound, ad7291_set_t_bound, 0x07);
670 static IIO_DEVICE_ATTR(v1_low,
671 S_IRUGO | S_IWUSR,
672 ad7291_show_t_bound, ad7291_set_t_bound, 0x08);
673 static IIO_DEVICE_ATTR(v1_hyst,
674 S_IRUGO | S_IWUSR,
675 ad7291_show_t_bound, ad7291_set_t_bound, 0x09);
676 static IIO_DEVICE_ATTR(v2_high,
677 S_IRUGO | S_IWUSR,
678 ad7291_show_t_bound, ad7291_set_t_bound, 0x0A);
679 static IIO_DEVICE_ATTR(v2_low,
680 S_IRUGO | S_IWUSR,
681 ad7291_show_t_bound, ad7291_set_t_bound, 0x0B);
682 static IIO_DEVICE_ATTR(v2_hyst,
683 S_IRUGO | S_IWUSR,
684 ad7291_show_t_bound, ad7291_set_t_bound, 0x0C);
685 static IIO_DEVICE_ATTR(v3_high,
686 S_IRUGO | S_IWUSR,
687 /* Datasheet suggests this one and this one only
688 has the registers in different order */
689 ad7291_show_t_bound, ad7291_set_t_bound, 0x0E);
690 static IIO_DEVICE_ATTR(v3_low,
691 S_IRUGO | S_IWUSR,
692 ad7291_show_t_bound, ad7291_set_t_bound, 0x0D);
693 static IIO_DEVICE_ATTR(v3_hyst,
694 S_IRUGO | S_IWUSR,
695 ad7291_show_t_bound, ad7291_set_t_bound, 0x0F);
696 static IIO_DEVICE_ATTR(v4_high,
697 S_IRUGO | S_IWUSR,
698 ad7291_show_t_bound, ad7291_set_t_bound, 0x10);
699 static IIO_DEVICE_ATTR(v4_low,
700 S_IRUGO | S_IWUSR,
701 ad7291_show_t_bound, ad7291_set_t_bound, 0x11);
702 static IIO_DEVICE_ATTR(v4_hyst,
703 S_IRUGO | S_IWUSR,
704 ad7291_show_t_bound, ad7291_set_t_bound, 0x12);
705 static IIO_DEVICE_ATTR(v5_high,
706 S_IRUGO | S_IWUSR,
707 ad7291_show_t_bound, ad7291_set_t_bound, 0x13);
708 static IIO_DEVICE_ATTR(v5_low,
709 S_IRUGO | S_IWUSR,
710 ad7291_show_t_bound, ad7291_set_t_bound, 0x14);
711 static IIO_DEVICE_ATTR(v5_hyst,
712 S_IRUGO | S_IWUSR,
713 ad7291_show_t_bound, ad7291_set_t_bound, 0x15);
714 static IIO_DEVICE_ATTR(v6_high,
715 S_IRUGO | S_IWUSR,
716 ad7291_show_t_bound, ad7291_set_t_bound, 0x16);
717 static IIO_DEVICE_ATTR(v6_low,
718 S_IRUGO | S_IWUSR,
719 ad7291_show_t_bound, ad7291_set_t_bound, 0x17);
720 static IIO_DEVICE_ATTR(v6_hyst,
721 S_IRUGO | S_IWUSR,
722 ad7291_show_t_bound, ad7291_set_t_bound, 0x18);
723 static IIO_DEVICE_ATTR(v7_high,
724 S_IRUGO | S_IWUSR,
725 ad7291_show_t_bound, ad7291_set_t_bound, 0x19);
726 static IIO_DEVICE_ATTR(v7_low,
727 S_IRUGO | S_IWUSR,
728 ad7291_show_t_bound, ad7291_set_t_bound, 0x1A);
729 static IIO_DEVICE_ATTR(v7_hyst,
730 S_IRUGO | S_IWUSR,
731 ad7291_show_t_bound, ad7291_set_t_bound, 0x1B);
733 static struct attribute *ad7291_event_attributes[] = {
734 &iio_dev_attr_t_sense_high_value.dev_attr.attr,
735 &iio_dev_attr_t_sense_low_value.dev_attr.attr,
736 &iio_dev_attr_t_sense_hyst_value.dev_attr.attr,
737 &iio_dev_attr_v0_high.dev_attr.attr,
738 &iio_dev_attr_v0_low.dev_attr.attr,
739 &iio_dev_attr_v0_hyst.dev_attr.attr,
740 &iio_dev_attr_v1_high.dev_attr.attr,
741 &iio_dev_attr_v1_low.dev_attr.attr,
742 &iio_dev_attr_v1_hyst.dev_attr.attr,
743 &iio_dev_attr_v2_high.dev_attr.attr,
744 &iio_dev_attr_v2_low.dev_attr.attr,
745 &iio_dev_attr_v2_hyst.dev_attr.attr,
746 &iio_dev_attr_v3_high.dev_attr.attr,
747 &iio_dev_attr_v3_low.dev_attr.attr,
748 &iio_dev_attr_v3_hyst.dev_attr.attr,
749 &iio_dev_attr_v4_high.dev_attr.attr,
750 &iio_dev_attr_v4_low.dev_attr.attr,
751 &iio_dev_attr_v4_hyst.dev_attr.attr,
752 &iio_dev_attr_v5_high.dev_attr.attr,
753 &iio_dev_attr_v5_low.dev_attr.attr,
754 &iio_dev_attr_v5_hyst.dev_attr.attr,
755 &iio_dev_attr_v6_high.dev_attr.attr,
756 &iio_dev_attr_v6_low.dev_attr.attr,
757 &iio_dev_attr_v6_hyst.dev_attr.attr,
758 &iio_dev_attr_v7_high.dev_attr.attr,
759 &iio_dev_attr_v7_low.dev_attr.attr,
760 &iio_dev_attr_v7_hyst.dev_attr.attr,
761 NULL,
764 static struct attribute_group ad7291_event_attribute_group = {
765 .attrs = ad7291_event_attributes,
769 * device probe and remove
772 static int __devinit ad7291_probe(struct i2c_client *client,
773 const struct i2c_device_id *id)
775 struct ad7291_chip_info *chip;
776 int ret = 0;
778 chip = kzalloc(sizeof(struct ad7291_chip_info), GFP_KERNEL);
780 if (chip == NULL)
781 return -ENOMEM;
783 /* this is only used for device removal purposes */
784 i2c_set_clientdata(client, chip);
786 chip->client = client;
787 chip->name = id->name;
788 chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
790 chip->indio_dev = iio_allocate_device(0);
791 if (chip->indio_dev == NULL) {
792 ret = -ENOMEM;
793 goto error_free_chip;
796 chip->indio_dev->dev.parent = &client->dev;
797 chip->indio_dev->attrs = &ad7291_attribute_group;
798 chip->indio_dev->event_attrs = &ad7291_event_attribute_group;
799 chip->indio_dev->dev_data = (void *)chip;
800 chip->indio_dev->driver_module = THIS_MODULE;
801 chip->indio_dev->num_interrupt_lines = 1;
802 chip->indio_dev->modes = INDIO_DIRECT_MODE;
804 ret = iio_device_register(chip->indio_dev);
805 if (ret)
806 goto error_free_dev;
808 if (client->irq > 0) {
809 ret = request_threaded_irq(client->irq,
810 NULL,
811 &ad7291_event_handler,
812 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
813 chip->name,
814 chip->indio_dev);
815 if (ret)
816 goto error_unreg_dev;
818 /* set irq polarity low level */
819 chip->command |= AD7291_ALART_POLARITY;
822 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
823 if (ret) {
824 ret = -EIO;
825 goto error_unreg_irq;
828 dev_info(&client->dev, "%s temperature sensor registered.\n",
829 id->name);
831 return 0;
833 error_unreg_irq:
834 free_irq(client->irq, chip->indio_dev);
835 error_unreg_dev:
836 iio_device_unregister(chip->indio_dev);
837 error_free_dev:
838 iio_free_device(chip->indio_dev);
839 error_free_chip:
840 kfree(chip);
842 return ret;
845 static int __devexit ad7291_remove(struct i2c_client *client)
847 struct ad7291_chip_info *chip = i2c_get_clientdata(client);
848 struct iio_dev *indio_dev = chip->indio_dev;
850 if (client->irq)
851 free_irq(client->irq, chip->indio_dev);
852 iio_device_unregister(indio_dev);
853 iio_free_device(chip->indio_dev);
854 kfree(chip);
856 return 0;
859 static const struct i2c_device_id ad7291_id[] = {
860 { "ad7291", 0 },
864 MODULE_DEVICE_TABLE(i2c, ad7291_id);
866 static struct i2c_driver ad7291_driver = {
867 .driver = {
868 .name = "ad7291",
870 .probe = ad7291_probe,
871 .remove = __devexit_p(ad7291_remove),
872 .id_table = ad7291_id,
875 static __init int ad7291_init(void)
877 return i2c_add_driver(&ad7291_driver);
880 static __exit void ad7291_exit(void)
882 i2c_del_driver(&ad7291_driver);
885 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
886 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
887 " temperature sensor driver");
888 MODULE_LICENSE("GPL v2");
890 module_init(ad7291_init);
891 module_exit(ad7291_exit);