Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / adc / ad7291.c
blob96cbb17bc2cd9340442d5d837d57af5e1e3b3f0d
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 struct i2c_client *client;
64 u16 command;
65 u8 channels; /* Active voltage channels */
69 * struct ad7291_chip_info - chip specifc information
72 struct ad7291_limit_regs {
73 u16 data_high;
74 u16 data_low;
75 u16 hysteresis;
79 * ad7291 register access by I2C
81 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
83 struct i2c_client *client = chip->client;
84 int ret = 0;
86 ret = i2c_smbus_read_word_data(client, reg);
87 if (ret < 0) {
88 dev_err(&client->dev, "I2C read error\n");
89 return ret;
92 *data = swab16((u16)ret);
94 return 0;
97 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
99 struct i2c_client *client = chip->client;
100 int ret = 0;
102 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
103 if (ret < 0)
104 dev_err(&client->dev, "I2C write error\n");
106 return ret;
109 /* Returns negative errno, or else the number of words read. */
110 static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
112 struct i2c_client *client = chip->client;
113 u8 commands[4];
114 int ret = 0;
115 int i, count;
117 if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
118 count = 2;
119 else if (reg == AD7291_VOLTAGE) {
120 if (!chip->channels) {
121 dev_err(&client->dev, "No voltage channel is selected.\n");
122 return -EINVAL;
124 count = 2 + chip->channels * 2;
125 } else {
126 dev_err(&client->dev, "I2C wrong data register\n");
127 return -EINVAL;
130 commands[0] = 0;
131 commands[1] = (chip->command >> 8) & 0xff;
132 commands[2] = chip->command & 0xff;
133 commands[3] = reg;
135 ret = i2c_master_send(client, commands, 4);
136 if (ret < 0) {
137 dev_err(&client->dev, "I2C master send error\n");
138 return ret;
141 ret = i2c_master_recv(client, (u8 *)data, count);
142 if (ret < 0) {
143 dev_err(&client->dev, "I2C master receive error\n");
144 return ret;
146 ret >>= 2;
148 for (i = 0; i < ret; i++)
149 data[i] = swab16(data[i]);
151 return ret;
154 static ssize_t ad7291_show_mode(struct device *dev,
155 struct device_attribute *attr,
156 char *buf)
158 struct iio_dev *dev_info = dev_get_drvdata(dev);
159 struct ad7291_chip_info *chip = iio_priv(dev_info);
161 if (chip->command & AD7291_AUTOCYCLE)
162 return sprintf(buf, "autocycle\n");
163 else
164 return sprintf(buf, "command\n");
167 static ssize_t ad7291_store_mode(struct device *dev,
168 struct device_attribute *attr,
169 const char *buf,
170 size_t len)
172 struct iio_dev *dev_info = dev_get_drvdata(dev);
173 struct ad7291_chip_info *chip = iio_priv(dev_info);
174 u16 command;
175 int ret;
177 command = chip->command & (~AD7291_AUTOCYCLE);
178 if (strcmp(buf, "autocycle"))
179 command |= AD7291_AUTOCYCLE;
181 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
182 if (ret)
183 return -EIO;
185 chip->command = command;
187 return ret;
190 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
191 ad7291_show_mode,
192 ad7291_store_mode,
195 static ssize_t ad7291_show_available_modes(struct device *dev,
196 struct device_attribute *attr,
197 char *buf)
199 return sprintf(buf, "command\nautocycle\n");
202 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
204 static ssize_t ad7291_store_reset(struct device *dev,
205 struct device_attribute *attr,
206 const char *buf,
207 size_t len)
209 struct iio_dev *dev_info = dev_get_drvdata(dev);
210 struct ad7291_chip_info *chip = iio_priv(dev_info);
211 u16 command;
212 int ret;
214 command = chip->command | AD7291_RESET;
216 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
217 if (ret)
218 return -EIO;
220 return ret;
223 static IIO_DEVICE_ATTR(reset, S_IWUSR,
224 NULL,
225 ad7291_store_reset,
228 static ssize_t ad7291_show_ext_ref(struct device *dev,
229 struct device_attribute *attr,
230 char *buf)
232 struct iio_dev *dev_info = dev_get_drvdata(dev);
233 struct ad7291_chip_info *chip = iio_priv(dev_info);
235 return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
238 static ssize_t ad7291_store_ext_ref(struct device *dev,
239 struct device_attribute *attr,
240 const char *buf,
241 size_t len)
243 struct iio_dev *dev_info = dev_get_drvdata(dev);
244 struct ad7291_chip_info *chip = iio_priv(dev_info);
245 u16 command;
246 int ret;
248 command = chip->command & (~AD7291_EXT_REF);
249 if (strcmp(buf, "1"))
250 command |= AD7291_EXT_REF;
252 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
253 if (ret)
254 return -EIO;
256 chip->command = command;
258 return ret;
261 static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
262 ad7291_show_ext_ref,
263 ad7291_store_ext_ref,
266 static ssize_t ad7291_show_noise_delay(struct device *dev,
267 struct device_attribute *attr,
268 char *buf)
270 struct iio_dev *dev_info = dev_get_drvdata(dev);
271 struct ad7291_chip_info *chip = iio_priv(dev_info);
273 return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
276 static ssize_t ad7291_store_noise_delay(struct device *dev,
277 struct device_attribute *attr,
278 const char *buf,
279 size_t len)
281 struct iio_dev *dev_info = dev_get_drvdata(dev);
282 struct ad7291_chip_info *chip = iio_priv(dev_info);
283 u16 command;
284 int ret;
286 command = chip->command & (~AD7291_NOISE_DELAY);
287 if (strcmp(buf, "1"))
288 command |= AD7291_NOISE_DELAY;
290 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
291 if (ret)
292 return -EIO;
294 chip->command = command;
296 return ret;
299 static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
300 ad7291_show_noise_delay,
301 ad7291_store_noise_delay,
304 static ssize_t ad7291_show_t_sense(struct device *dev,
305 struct device_attribute *attr,
306 char *buf)
308 struct iio_dev *dev_info = dev_get_drvdata(dev);
309 struct ad7291_chip_info *chip = iio_priv(dev_info);
310 u16 data;
311 char sign = ' ';
312 int ret;
314 ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
315 if (ret)
316 return -EIO;
318 if (data & AD7291_T_VALUE_SIGN) {
319 /* convert supplement to positive value */
320 data = (AD7291_T_VALUE_SIGN << 1) - data;
321 sign = '-';
324 return sprintf(buf, "%c%d.%.2d\n", sign,
325 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
326 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
329 static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
331 static ssize_t ad7291_show_t_average(struct device *dev,
332 struct device_attribute *attr,
333 char *buf)
335 struct iio_dev *dev_info = dev_get_drvdata(dev);
336 struct ad7291_chip_info *chip = iio_priv(dev_info);
337 u16 data;
338 char sign = ' ';
339 int ret;
341 ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
342 if (ret)
343 return -EIO;
345 if (data & AD7291_T_VALUE_SIGN) {
346 /* convert supplement to positive value */
347 data = (AD7291_T_VALUE_SIGN << 1) - data;
348 sign = '-';
351 return sprintf(buf, "%c%d.%.2d\n", sign,
352 (data >> AD7291_T_VALUE_FLOAT_OFFSET),
353 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
356 static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0);
358 static ssize_t ad7291_show_voltage(struct device *dev,
359 struct device_attribute *attr,
360 char *buf)
362 struct iio_dev *dev_info = dev_get_drvdata(dev);
363 struct ad7291_chip_info *chip = iio_priv(dev_info);
364 u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
365 int i, size, ret;
367 ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
368 if (ret)
369 return -EIO;
371 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
372 if (chip->command & (AD7291_T_SENSE_MASK << i)) {
373 ret = sprintf(buf, "channel[%d]=%d\n", i,
374 data[i] & AD7291_VALUE_MASK);
375 if (ret < 0)
376 break;
377 buf += ret;
378 size += ret;
382 return size;
385 static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
387 static ssize_t ad7291_show_channel_mask(struct device *dev,
388 struct device_attribute *attr,
389 char *buf)
391 struct iio_dev *dev_info = dev_get_drvdata(dev);
392 struct ad7291_chip_info *chip = iio_priv(dev_info);
394 return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
395 AD7291_VOLTAGE_OFFSET);
398 static ssize_t ad7291_store_channel_mask(struct device *dev,
399 struct device_attribute *attr,
400 const char *buf,
401 size_t len)
403 struct iio_dev *dev_info = dev_get_drvdata(dev);
404 struct ad7291_chip_info *chip = iio_priv(dev_info);
405 u16 command;
406 unsigned long data;
407 int i, ret;
409 ret = strict_strtoul(buf, 16, &data);
410 if (ret || data > 0xff)
411 return -EINVAL;
413 command = chip->command & (~AD7291_VOLTAGE_MASK);
414 command |= data << AD7291_VOLTAGE_OFFSET;
416 ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
417 if (ret)
418 return -EIO;
420 chip->command = command;
422 for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
423 if (chip->command & (AD7291_T_SENSE_MASK << i))
424 chip->channels++;
427 return ret;
430 static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
431 ad7291_show_channel_mask,
432 ad7291_store_channel_mask,
435 static struct attribute *ad7291_attributes[] = {
436 &iio_dev_attr_available_modes.dev_attr.attr,
437 &iio_dev_attr_mode.dev_attr.attr,
438 &iio_dev_attr_reset.dev_attr.attr,
439 &iio_dev_attr_ext_ref.dev_attr.attr,
440 &iio_dev_attr_noise_delay.dev_attr.attr,
441 &iio_dev_attr_t_sense.dev_attr.attr,
442 &iio_dev_attr_t_average.dev_attr.attr,
443 &iio_dev_attr_voltage.dev_attr.attr,
444 &iio_dev_attr_channel_mask.dev_attr.attr,
445 NULL,
448 static const struct attribute_group ad7291_attribute_group = {
449 .attrs = ad7291_attributes,
453 * temperature bound events
456 static irqreturn_t ad7291_event_handler(int irq, void *private)
458 struct iio_dev *indio_dev = private;
459 struct ad7291_chip_info *chip = iio_priv(private);
460 u16 t_status, v_status;
461 u16 command;
462 int i;
463 s64 timestamp = iio_get_time_ns();
465 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
466 return IRQ_HANDLED;
468 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
469 return IRQ_HANDLED;
471 if (!(t_status || v_status))
472 return IRQ_HANDLED;
474 command = chip->command | AD7291_ALART_CLEAR;
475 ad7291_i2c_write(chip, AD7291_COMMAND, command);
477 command = chip->command & ~AD7291_ALART_CLEAR;
478 ad7291_i2c_write(chip, AD7291_COMMAND, command);
480 if (t_status & (1 << 0))
481 iio_push_event(indio_dev, 0,
482 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
484 IIO_EV_TYPE_THRESH,
485 IIO_EV_DIR_FALLING),
486 timestamp);
487 if (t_status & (1 << 1))
488 iio_push_event(indio_dev, 0,
489 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
491 IIO_EV_TYPE_THRESH,
492 IIO_EV_DIR_RISING),
493 timestamp);
494 if (t_status & (1 << 2))
495 iio_push_event(indio_dev, 0,
496 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
498 IIO_EV_TYPE_THRESH,
499 IIO_EV_DIR_FALLING),
500 timestamp);
501 if (t_status & (1 << 3))
502 iio_push_event(indio_dev, 0,
503 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
505 IIO_EV_TYPE_THRESH,
506 IIO_EV_DIR_RISING),
507 timestamp);
509 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
510 if (v_status & (1 << i))
511 iio_push_event(indio_dev, 0,
512 IIO_UNMOD_EVENT_CODE(IIO_IN,
513 i/2,
514 IIO_EV_TYPE_THRESH,
515 IIO_EV_DIR_FALLING),
516 timestamp);
517 if (v_status & (1 << (i + 1)))
518 iio_push_event(indio_dev, 0,
519 IIO_UNMOD_EVENT_CODE(IIO_IN,
520 i/2,
521 IIO_EV_TYPE_THRESH,
522 IIO_EV_DIR_RISING),
523 timestamp);
526 return IRQ_HANDLED;
529 static inline ssize_t ad7291_show_t_bound(struct device *dev,
530 struct device_attribute *attr,
531 char *buf)
533 struct iio_dev *dev_info = dev_get_drvdata(dev);
534 struct ad7291_chip_info *chip = iio_priv(dev_info);
535 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
536 u16 data;
537 char sign = ' ';
538 int ret;
540 ret = ad7291_i2c_read(chip, this_attr->address, &data);
541 if (ret)
542 return -EIO;
544 data &= AD7291_VALUE_MASK;
545 if (data & AD7291_T_VALUE_SIGN) {
546 /* convert supplement to positive value */
547 data = (AD7291_T_VALUE_SIGN << 1) - data;
548 sign = '-';
551 return sprintf(buf, "%c%d.%.2d\n", sign,
552 data >> AD7291_T_VALUE_FLOAT_OFFSET,
553 (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
556 static inline ssize_t ad7291_set_t_bound(struct device *dev,
557 struct device_attribute *attr,
558 const char *buf,
559 size_t len)
561 struct iio_dev *dev_info = dev_get_drvdata(dev);
562 struct ad7291_chip_info *chip = iio_priv(dev_info);
563 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
564 long tmp1, tmp2;
565 u16 data;
566 char *pos;
567 int ret;
569 pos = strchr(buf, '.');
571 ret = strict_strtol(buf, 10, &tmp1);
573 if (ret || tmp1 > 127 || tmp1 < -128)
574 return -EINVAL;
576 if (pos) {
577 len = strlen(pos);
578 if (len > AD7291_T_VALUE_FLOAT_OFFSET)
579 len = AD7291_T_VALUE_FLOAT_OFFSET;
580 pos[len] = 0;
581 ret = strict_strtol(pos, 10, &tmp2);
583 if (!ret)
584 tmp2 = (tmp2 / 25) * 25;
587 if (tmp1 < 0)
588 data = (u16)(-tmp1);
589 else
590 data = (u16)tmp1;
591 data = (data << AD7291_T_VALUE_FLOAT_OFFSET) |
592 (tmp2 & AD7291_T_VALUE_FLOAT_MASK);
593 if (tmp1 < 0)
594 /* convert positive value to supplyment */
595 data = (AD7291_T_VALUE_SIGN << 1) - data;
597 ret = ad7291_i2c_write(chip, this_attr->address, data);
598 if (ret)
599 return -EIO;
601 return ret;
604 static inline ssize_t ad7291_show_v_bound(struct device *dev,
605 struct device_attribute *attr,
606 u8 bound_reg,
607 char *buf)
609 struct iio_dev *dev_info = dev_get_drvdata(dev);
610 struct ad7291_chip_info *chip = iio_priv(dev_info);
611 u16 data;
612 int ret;
614 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
615 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
616 AD7291_VOLTAGE_LIMIT_COUNT)
617 return -EINVAL;
619 ret = ad7291_i2c_read(chip, bound_reg, &data);
620 if (ret)
621 return -EIO;
623 data &= AD7291_VALUE_MASK;
625 return sprintf(buf, "%d\n", data);
628 static inline ssize_t ad7291_set_v_bound(struct device *dev,
629 struct device_attribute *attr,
630 u8 bound_reg,
631 const char *buf,
632 size_t len)
634 struct iio_dev *dev_info = dev_get_drvdata(dev);
635 struct ad7291_chip_info *chip = iio_priv(dev_info);
636 unsigned long value;
637 u16 data;
638 int ret;
640 if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
641 bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
642 AD7291_VOLTAGE_LIMIT_COUNT)
643 return -EINVAL;
645 ret = strict_strtoul(buf, 10, &value);
647 if (ret || value >= 4096)
648 return -EINVAL;
650 data = (u16)value;
651 ret = ad7291_i2c_write(chip, bound_reg, data);
652 if (ret)
653 return -EIO;
655 return ret;
658 static IIO_DEVICE_ATTR(t_sense_high_value,
659 S_IRUGO | S_IWUSR,
660 ad7291_show_t_bound, ad7291_set_t_bound,
661 AD7291_T_SENSE_HIGH);
662 static IIO_DEVICE_ATTR(t_sense_low_value,
663 S_IRUGO | S_IWUSR,
664 ad7291_show_t_bound, ad7291_set_t_bound,
665 AD7291_T_SENSE_LOW);
666 static IIO_DEVICE_ATTR(t_sense_hyst_value,
667 S_IRUGO | S_IWUSR,
668 ad7291_show_t_bound, ad7291_set_t_bound,
669 AD7291_T_SENSE_HYST);
670 static IIO_DEVICE_ATTR(v0_high,
671 S_IRUGO | S_IWUSR,
672 ad7291_show_t_bound, ad7291_set_t_bound, 0x04);
673 static IIO_DEVICE_ATTR(v0_low,
674 S_IRUGO | S_IWUSR,
675 ad7291_show_t_bound, ad7291_set_t_bound, 0x05);
676 static IIO_DEVICE_ATTR(v0_hyst,
677 S_IRUGO | S_IWUSR,
678 ad7291_show_t_bound, ad7291_set_t_bound, 0x06);
679 static IIO_DEVICE_ATTR(v1_high,
680 S_IRUGO | S_IWUSR,
681 ad7291_show_t_bound, ad7291_set_t_bound, 0x07);
682 static IIO_DEVICE_ATTR(v1_low,
683 S_IRUGO | S_IWUSR,
684 ad7291_show_t_bound, ad7291_set_t_bound, 0x08);
685 static IIO_DEVICE_ATTR(v1_hyst,
686 S_IRUGO | S_IWUSR,
687 ad7291_show_t_bound, ad7291_set_t_bound, 0x09);
688 static IIO_DEVICE_ATTR(v2_high,
689 S_IRUGO | S_IWUSR,
690 ad7291_show_t_bound, ad7291_set_t_bound, 0x0A);
691 static IIO_DEVICE_ATTR(v2_low,
692 S_IRUGO | S_IWUSR,
693 ad7291_show_t_bound, ad7291_set_t_bound, 0x0B);
694 static IIO_DEVICE_ATTR(v2_hyst,
695 S_IRUGO | S_IWUSR,
696 ad7291_show_t_bound, ad7291_set_t_bound, 0x0C);
697 static IIO_DEVICE_ATTR(v3_high,
698 S_IRUGO | S_IWUSR,
699 /* Datasheet suggests this one and this one only
700 has the registers in different order */
701 ad7291_show_t_bound, ad7291_set_t_bound, 0x0E);
702 static IIO_DEVICE_ATTR(v3_low,
703 S_IRUGO | S_IWUSR,
704 ad7291_show_t_bound, ad7291_set_t_bound, 0x0D);
705 static IIO_DEVICE_ATTR(v3_hyst,
706 S_IRUGO | S_IWUSR,
707 ad7291_show_t_bound, ad7291_set_t_bound, 0x0F);
708 static IIO_DEVICE_ATTR(v4_high,
709 S_IRUGO | S_IWUSR,
710 ad7291_show_t_bound, ad7291_set_t_bound, 0x10);
711 static IIO_DEVICE_ATTR(v4_low,
712 S_IRUGO | S_IWUSR,
713 ad7291_show_t_bound, ad7291_set_t_bound, 0x11);
714 static IIO_DEVICE_ATTR(v4_hyst,
715 S_IRUGO | S_IWUSR,
716 ad7291_show_t_bound, ad7291_set_t_bound, 0x12);
717 static IIO_DEVICE_ATTR(v5_high,
718 S_IRUGO | S_IWUSR,
719 ad7291_show_t_bound, ad7291_set_t_bound, 0x13);
720 static IIO_DEVICE_ATTR(v5_low,
721 S_IRUGO | S_IWUSR,
722 ad7291_show_t_bound, ad7291_set_t_bound, 0x14);
723 static IIO_DEVICE_ATTR(v5_hyst,
724 S_IRUGO | S_IWUSR,
725 ad7291_show_t_bound, ad7291_set_t_bound, 0x15);
726 static IIO_DEVICE_ATTR(v6_high,
727 S_IRUGO | S_IWUSR,
728 ad7291_show_t_bound, ad7291_set_t_bound, 0x16);
729 static IIO_DEVICE_ATTR(v6_low,
730 S_IRUGO | S_IWUSR,
731 ad7291_show_t_bound, ad7291_set_t_bound, 0x17);
732 static IIO_DEVICE_ATTR(v6_hyst,
733 S_IRUGO | S_IWUSR,
734 ad7291_show_t_bound, ad7291_set_t_bound, 0x18);
735 static IIO_DEVICE_ATTR(v7_high,
736 S_IRUGO | S_IWUSR,
737 ad7291_show_t_bound, ad7291_set_t_bound, 0x19);
738 static IIO_DEVICE_ATTR(v7_low,
739 S_IRUGO | S_IWUSR,
740 ad7291_show_t_bound, ad7291_set_t_bound, 0x1A);
741 static IIO_DEVICE_ATTR(v7_hyst,
742 S_IRUGO | S_IWUSR,
743 ad7291_show_t_bound, ad7291_set_t_bound, 0x1B);
745 static struct attribute *ad7291_event_attributes[] = {
746 &iio_dev_attr_t_sense_high_value.dev_attr.attr,
747 &iio_dev_attr_t_sense_low_value.dev_attr.attr,
748 &iio_dev_attr_t_sense_hyst_value.dev_attr.attr,
749 &iio_dev_attr_v0_high.dev_attr.attr,
750 &iio_dev_attr_v0_low.dev_attr.attr,
751 &iio_dev_attr_v0_hyst.dev_attr.attr,
752 &iio_dev_attr_v1_high.dev_attr.attr,
753 &iio_dev_attr_v1_low.dev_attr.attr,
754 &iio_dev_attr_v1_hyst.dev_attr.attr,
755 &iio_dev_attr_v2_high.dev_attr.attr,
756 &iio_dev_attr_v2_low.dev_attr.attr,
757 &iio_dev_attr_v2_hyst.dev_attr.attr,
758 &iio_dev_attr_v3_high.dev_attr.attr,
759 &iio_dev_attr_v3_low.dev_attr.attr,
760 &iio_dev_attr_v3_hyst.dev_attr.attr,
761 &iio_dev_attr_v4_high.dev_attr.attr,
762 &iio_dev_attr_v4_low.dev_attr.attr,
763 &iio_dev_attr_v4_hyst.dev_attr.attr,
764 &iio_dev_attr_v5_high.dev_attr.attr,
765 &iio_dev_attr_v5_low.dev_attr.attr,
766 &iio_dev_attr_v5_hyst.dev_attr.attr,
767 &iio_dev_attr_v6_high.dev_attr.attr,
768 &iio_dev_attr_v6_low.dev_attr.attr,
769 &iio_dev_attr_v6_hyst.dev_attr.attr,
770 &iio_dev_attr_v7_high.dev_attr.attr,
771 &iio_dev_attr_v7_low.dev_attr.attr,
772 &iio_dev_attr_v7_hyst.dev_attr.attr,
773 NULL,
776 static struct attribute_group ad7291_event_attribute_group = {
777 .attrs = ad7291_event_attributes,
780 static const struct iio_info ad7291_info = {
781 .attrs = &ad7291_attribute_group,
782 .num_interrupt_lines = 1,
783 .event_attrs = &ad7291_event_attribute_group,
787 * device probe and remove
790 static int __devinit ad7291_probe(struct i2c_client *client,
791 const struct i2c_device_id *id)
793 struct ad7291_chip_info *chip;
794 struct iio_dev *indio_dev;
795 int ret = 0;
797 indio_dev = iio_allocate_device(sizeof(*chip));
798 if (indio_dev == NULL) {
799 ret = -ENOMEM;
800 goto error_ret;
802 chip = iio_priv(indio_dev);
803 /* this is only used for device removal purposes */
804 i2c_set_clientdata(client, indio_dev);
806 chip->client = client;
807 chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
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;
814 ret = iio_device_register(indio_dev);
815 if (ret)
816 goto error_free_dev;
818 if (client->irq > 0) {
819 ret = request_threaded_irq(client->irq,
820 NULL,
821 &ad7291_event_handler,
822 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
823 id->name,
824 indio_dev);
825 if (ret)
826 goto error_unreg_dev;
828 /* set irq polarity low level */
829 chip->command |= AD7291_ALART_POLARITY;
832 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
833 if (ret) {
834 ret = -EIO;
835 goto error_unreg_irq;
838 dev_info(&client->dev, "%s temperature sensor registered.\n",
839 id->name);
841 return 0;
843 error_unreg_irq:
844 free_irq(client->irq, indio_dev);
845 error_unreg_dev:
846 iio_device_unregister(indio_dev);
847 error_free_dev:
848 iio_free_device(indio_dev);
849 error_ret:
850 return ret;
853 static int __devexit ad7291_remove(struct i2c_client *client)
855 struct iio_dev *indio_dev = i2c_get_clientdata(client);
857 if (client->irq)
858 free_irq(client->irq, indio_dev);
859 iio_device_unregister(indio_dev);
860 iio_free_device(indio_dev);
862 return 0;
865 static const struct i2c_device_id ad7291_id[] = {
866 { "ad7291", 0 },
870 MODULE_DEVICE_TABLE(i2c, ad7291_id);
872 static struct i2c_driver ad7291_driver = {
873 .driver = {
874 .name = "ad7291",
876 .probe = ad7291_probe,
877 .remove = __devexit_p(ad7291_remove),
878 .id_table = ad7291_id,
881 static __init int ad7291_init(void)
883 return i2c_add_driver(&ad7291_driver);
886 static __exit void ad7291_exit(void)
888 i2c_del_driver(&ad7291_driver);
891 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
892 MODULE_DESCRIPTION("Analog Devices AD7291 digital"
893 " temperature sensor driver");
894 MODULE_LICENSE("GPL v2");
896 module_init(ad7291_init);
897 module_exit(ad7291_exit);