Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / adc / adt7410.c
blob76aa0639a55ab01a6aad84effd3f9ccc946bd896
1 /*
2 * ADT7410 digital temperature sensor driver supporting ADT7410
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/list.h>
15 #include <linux/i2c.h>
17 #include "../iio.h"
18 #include "../sysfs.h"
21 * ADT7410 registers definition
24 #define ADT7410_TEMPERATURE 0
25 #define ADT7410_STATUS 2
26 #define ADT7410_CONFIG 3
27 #define ADT7410_T_ALARM_HIGH 4
28 #define ADT7410_T_ALARM_LOW 6
29 #define ADT7410_T_CRIT 8
30 #define ADT7410_T_HYST 0xA
31 #define ADT7410_ID 0xB
32 #define ADT7410_RESET 0x2F
35 * ADT7410 status
37 #define ADT7410_STAT_T_LOW 0x10
38 #define ADT7410_STAT_T_HIGH 0x20
39 #define ADT7410_STAT_T_CRIT 0x40
40 #define ADT7410_STAT_NOT_RDY 0x80
43 * ADT7410 config
45 #define ADT7410_FAULT_QUEUE_MASK 0x3
46 #define ADT7410_CT_POLARITY 0x4
47 #define ADT7410_INT_POLARITY 0x8
48 #define ADT7410_EVENT_MODE 0x10
49 #define ADT7410_MODE_MASK 0x60
50 #define ADT7410_ONESHOT 0x20
51 #define ADT7410_SPS 0x40
52 #define ADT7410_PD 0x60
53 #define ADT7410_RESOLUTION 0x80
56 * ADT7410 masks
58 #define ADT7410_T16_VALUE_SIGN 0x8000
59 #define ADT7410_T16_VALUE_FLOAT_OFFSET 7
60 #define ADT7410_T16_VALUE_FLOAT_MASK 0x7F
61 #define ADT7410_T13_VALUE_SIGN 0x1000
62 #define ADT7410_T13_VALUE_OFFSET 3
63 #define ADT7410_T13_VALUE_FLOAT_OFFSET 4
64 #define ADT7410_T13_VALUE_FLOAT_MASK 0xF
65 #define ADT7410_T_HYST_MASK 0xF
66 #define ADT7410_DEVICE_ID_MASK 0xF
67 #define ADT7410_MANUFACTORY_ID_MASK 0xF0
68 #define ADT7410_MANUFACTORY_ID_OFFSET 4
70 #define ADT7410_IRQS 2
73 * struct adt7410_chip_info - chip specifc information
76 struct adt7410_chip_info {
77 struct i2c_client *client;
78 u8 config;
82 * adt7410 register access by I2C
85 static int adt7410_i2c_read_word(struct adt7410_chip_info *chip, u8 reg, u16 *data)
87 struct i2c_client *client = chip->client;
88 int ret = 0;
90 ret = i2c_smbus_read_word_data(client, reg);
91 if (ret < 0) {
92 dev_err(&client->dev, "I2C read error\n");
93 return ret;
96 *data = swab16((u16)ret);
98 return 0;
101 static int adt7410_i2c_write_word(struct adt7410_chip_info *chip, u8 reg, u16 data)
103 struct i2c_client *client = chip->client;
104 int ret = 0;
106 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
107 if (ret < 0)
108 dev_err(&client->dev, "I2C write error\n");
110 return ret;
113 static int adt7410_i2c_read_byte(struct adt7410_chip_info *chip, u8 reg, u8 *data)
115 struct i2c_client *client = chip->client;
116 int ret = 0;
118 ret = i2c_smbus_read_byte_data(client, reg);
119 if (ret < 0) {
120 dev_err(&client->dev, "I2C read error\n");
121 return ret;
124 *data = (u8)ret;
126 return 0;
129 static int adt7410_i2c_write_byte(struct adt7410_chip_info *chip, u8 reg, u8 data)
131 struct i2c_client *client = chip->client;
132 int ret = 0;
134 ret = i2c_smbus_write_byte_data(client, reg, data);
135 if (ret < 0)
136 dev_err(&client->dev, "I2C write error\n");
138 return ret;
141 static ssize_t adt7410_show_mode(struct device *dev,
142 struct device_attribute *attr,
143 char *buf)
145 struct iio_dev *dev_info = dev_get_drvdata(dev);
146 struct adt7410_chip_info *chip = iio_priv(dev_info);
147 u8 config;
149 config = chip->config & ADT7410_MODE_MASK;
151 switch (config) {
152 case ADT7410_PD:
153 return sprintf(buf, "power-down\n");
154 case ADT7410_ONESHOT:
155 return sprintf(buf, "one-shot\n");
156 case ADT7410_SPS:
157 return sprintf(buf, "sps\n");
158 default:
159 return sprintf(buf, "full\n");
163 static ssize_t adt7410_store_mode(struct device *dev,
164 struct device_attribute *attr,
165 const char *buf,
166 size_t len)
168 struct iio_dev *dev_info = dev_get_drvdata(dev);
169 struct adt7410_chip_info *chip = iio_priv(dev_info);
170 u16 config;
171 int ret;
173 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
174 if (ret)
175 return -EIO;
177 config = chip->config & (~ADT7410_MODE_MASK);
178 if (strcmp(buf, "power-down"))
179 config |= ADT7410_PD;
180 else if (strcmp(buf, "one-shot"))
181 config |= ADT7410_ONESHOT;
182 else if (strcmp(buf, "sps"))
183 config |= ADT7410_SPS;
185 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
186 if (ret)
187 return -EIO;
189 chip->config = config;
191 return ret;
194 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
195 adt7410_show_mode,
196 adt7410_store_mode,
199 static ssize_t adt7410_show_available_modes(struct device *dev,
200 struct device_attribute *attr,
201 char *buf)
203 return sprintf(buf, "full\none-shot\nsps\npower-down\n");
206 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt7410_show_available_modes, NULL, 0);
208 static ssize_t adt7410_show_resolution(struct device *dev,
209 struct device_attribute *attr,
210 char *buf)
212 struct iio_dev *dev_info = dev_get_drvdata(dev);
213 struct adt7410_chip_info *chip = iio_priv(dev_info);
214 int ret;
215 int bits;
217 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
218 if (ret)
219 return -EIO;
221 if (chip->config & ADT7410_RESOLUTION)
222 bits = 16;
223 else
224 bits = 13;
226 return sprintf(buf, "%d bits\n", bits);
229 static ssize_t adt7410_store_resolution(struct device *dev,
230 struct device_attribute *attr,
231 const char *buf,
232 size_t len)
234 struct iio_dev *dev_info = dev_get_drvdata(dev);
235 struct adt7410_chip_info *chip = iio_priv(dev_info);
236 unsigned long data;
237 u16 config;
238 int ret;
240 ret = strict_strtoul(buf, 10, &data);
241 if (ret)
242 return -EINVAL;
244 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
245 if (ret)
246 return -EIO;
248 config = chip->config & (~ADT7410_RESOLUTION);
249 if (data)
250 config |= ADT7410_RESOLUTION;
252 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
253 if (ret)
254 return -EIO;
256 chip->config = config;
258 return ret;
261 static IIO_DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR,
262 adt7410_show_resolution,
263 adt7410_store_resolution,
266 static ssize_t adt7410_show_id(struct device *dev,
267 struct device_attribute *attr,
268 char *buf)
270 struct iio_dev *dev_info = dev_get_drvdata(dev);
271 struct adt7410_chip_info *chip = iio_priv(dev_info);
272 u8 id;
273 int ret;
275 ret = adt7410_i2c_read_byte(chip, ADT7410_ID, &id);
276 if (ret)
277 return -EIO;
279 return sprintf(buf, "device id: 0x%x\nmanufactory id: 0x%x\n",
280 id & ADT7410_DEVICE_ID_MASK,
281 (id & ADT7410_MANUFACTORY_ID_MASK) >> ADT7410_MANUFACTORY_ID_OFFSET);
284 static IIO_DEVICE_ATTR(id, S_IRUGO | S_IWUSR,
285 adt7410_show_id,
286 NULL,
289 static ssize_t adt7410_convert_temperature(struct adt7410_chip_info *chip,
290 u16 data, char *buf)
292 char sign = ' ';
294 if (chip->config & ADT7410_RESOLUTION) {
295 if (data & ADT7410_T16_VALUE_SIGN) {
296 /* convert supplement to positive value */
297 data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
298 sign = '-';
300 return sprintf(buf, "%c%d.%.7d\n", sign,
301 (data >> ADT7410_T16_VALUE_FLOAT_OFFSET),
302 (data & ADT7410_T16_VALUE_FLOAT_MASK) * 78125);
303 } else {
304 if (data & ADT7410_T13_VALUE_SIGN) {
305 /* convert supplement to positive value */
306 data >>= ADT7410_T13_VALUE_OFFSET;
307 data = (ADT7410_T13_VALUE_SIGN << 1) - data;
308 sign = '-';
310 return sprintf(buf, "%c%d.%.4d\n", sign,
311 (data >> ADT7410_T13_VALUE_FLOAT_OFFSET),
312 (data & ADT7410_T13_VALUE_FLOAT_MASK) * 625);
316 static ssize_t adt7410_show_value(struct device *dev,
317 struct device_attribute *attr,
318 char *buf)
320 struct iio_dev *dev_info = dev_get_drvdata(dev);
321 struct adt7410_chip_info *chip = iio_priv(dev_info);
322 u8 status;
323 u16 data;
324 int ret, i = 0;
326 do {
327 ret = adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status);
328 if (ret)
329 return -EIO;
330 i++;
331 if (i == 10000)
332 return -EIO;
333 } while (status & ADT7410_STAT_NOT_RDY);
335 ret = adt7410_i2c_read_word(chip, ADT7410_TEMPERATURE, &data);
336 if (ret)
337 return -EIO;
339 return adt7410_convert_temperature(chip, data, buf);
342 static IIO_DEVICE_ATTR(value, S_IRUGO, adt7410_show_value, NULL, 0);
344 static struct attribute *adt7410_attributes[] = {
345 &iio_dev_attr_available_modes.dev_attr.attr,
346 &iio_dev_attr_mode.dev_attr.attr,
347 &iio_dev_attr_resolution.dev_attr.attr,
348 &iio_dev_attr_id.dev_attr.attr,
349 &iio_dev_attr_value.dev_attr.attr,
350 NULL,
353 static const struct attribute_group adt7410_attribute_group = {
354 .attrs = adt7410_attributes,
357 static irqreturn_t adt7410_event_handler(int irq, void *private)
359 struct iio_dev *indio_dev = private;
360 struct adt7410_chip_info *chip = iio_priv(indio_dev);
361 s64 timestamp = iio_get_time_ns();
362 u8 status;
364 if (adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status))
365 return IRQ_HANDLED;
367 if (status & ADT7410_STAT_T_HIGH)
368 iio_push_event(indio_dev, 0,
369 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
370 IIO_EV_TYPE_THRESH,
371 IIO_EV_DIR_RISING),
372 timestamp);
373 if (status & ADT7410_STAT_T_LOW)
374 iio_push_event(indio_dev, 0,
375 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
376 IIO_EV_TYPE_THRESH,
377 IIO_EV_DIR_FALLING),
378 timestamp);
379 if (status & ADT7410_STAT_T_CRIT)
380 iio_push_event(indio_dev, 0,
381 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
382 IIO_EV_TYPE_THRESH,
383 IIO_EV_DIR_RISING),
384 timestamp);
386 return IRQ_HANDLED;
389 static ssize_t adt7410_show_event_mode(struct device *dev,
390 struct device_attribute *attr,
391 char *buf)
393 struct iio_dev *dev_info = dev_get_drvdata(dev);
394 struct adt7410_chip_info *chip = iio_priv(dev_info);
395 int ret;
397 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
398 if (ret)
399 return -EIO;
401 if (chip->config & ADT7410_EVENT_MODE)
402 return sprintf(buf, "interrupt\n");
403 else
404 return sprintf(buf, "comparator\n");
407 static ssize_t adt7410_set_event_mode(struct device *dev,
408 struct device_attribute *attr,
409 const char *buf,
410 size_t len)
412 struct iio_dev *dev_info = dev_get_drvdata(dev);
413 struct adt7410_chip_info *chip = iio_priv(dev_info);
414 u16 config;
415 int ret;
417 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
418 if (ret)
419 return -EIO;
421 config = chip->config &= ~ADT7410_EVENT_MODE;
422 if (strcmp(buf, "comparator") != 0)
423 config |= ADT7410_EVENT_MODE;
425 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
426 if (ret)
427 return -EIO;
429 chip->config = config;
431 return ret;
434 static ssize_t adt7410_show_available_event_modes(struct device *dev,
435 struct device_attribute *attr,
436 char *buf)
438 return sprintf(buf, "comparator\ninterrupt\n");
441 static ssize_t adt7410_show_fault_queue(struct device *dev,
442 struct device_attribute *attr,
443 char *buf)
445 struct iio_dev *dev_info = dev_get_drvdata(dev);
446 struct adt7410_chip_info *chip = iio_priv(dev_info);
447 int ret;
449 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
450 if (ret)
451 return -EIO;
453 return sprintf(buf, "%d\n", chip->config & ADT7410_FAULT_QUEUE_MASK);
456 static ssize_t adt7410_set_fault_queue(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf,
459 size_t len)
461 struct iio_dev *dev_info = dev_get_drvdata(dev);
462 struct adt7410_chip_info *chip = iio_priv(dev_info);
463 unsigned long data;
464 int ret;
465 u8 config;
467 ret = strict_strtoul(buf, 10, &data);
468 if (ret || data > 3)
469 return -EINVAL;
471 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
472 if (ret)
473 return -EIO;
475 config = chip->config & ~ADT7410_FAULT_QUEUE_MASK;
476 config |= data;
477 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
478 if (ret)
479 return -EIO;
481 chip->config = config;
483 return ret;
486 static inline ssize_t adt7410_show_t_bound(struct device *dev,
487 struct device_attribute *attr,
488 u8 bound_reg,
489 char *buf)
491 struct iio_dev *dev_info = dev_get_drvdata(dev);
492 struct adt7410_chip_info *chip = iio_priv(dev_info);
493 u16 data;
494 int ret;
496 ret = adt7410_i2c_read_word(chip, bound_reg, &data);
497 if (ret)
498 return -EIO;
500 return adt7410_convert_temperature(chip, data, buf);
503 static inline ssize_t adt7410_set_t_bound(struct device *dev,
504 struct device_attribute *attr,
505 u8 bound_reg,
506 const char *buf,
507 size_t len)
509 struct iio_dev *dev_info = dev_get_drvdata(dev);
510 struct adt7410_chip_info *chip = iio_priv(dev_info);
511 long tmp1, tmp2;
512 u16 data;
513 char *pos;
514 int ret;
516 pos = strchr(buf, '.');
518 ret = strict_strtol(buf, 10, &tmp1);
520 if (ret || tmp1 > 127 || tmp1 < -128)
521 return -EINVAL;
523 if (pos) {
524 len = strlen(pos);
526 if (chip->config & ADT7410_RESOLUTION) {
527 if (len > ADT7410_T16_VALUE_FLOAT_OFFSET)
528 len = ADT7410_T16_VALUE_FLOAT_OFFSET;
529 pos[len] = 0;
530 ret = strict_strtol(pos, 10, &tmp2);
532 if (!ret)
533 tmp2 = (tmp2 / 78125) * 78125;
534 } else {
535 if (len > ADT7410_T13_VALUE_FLOAT_OFFSET)
536 len = ADT7410_T13_VALUE_FLOAT_OFFSET;
537 pos[len] = 0;
538 ret = strict_strtol(pos, 10, &tmp2);
540 if (!ret)
541 tmp2 = (tmp2 / 625) * 625;
545 if (tmp1 < 0)
546 data = (u16)(-tmp1);
547 else
548 data = (u16)tmp1;
550 if (chip->config & ADT7410_RESOLUTION) {
551 data = (data << ADT7410_T16_VALUE_FLOAT_OFFSET) |
552 (tmp2 & ADT7410_T16_VALUE_FLOAT_MASK);
554 if (tmp1 < 0)
555 /* convert positive value to supplyment */
556 data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
557 } else {
558 data = (data << ADT7410_T13_VALUE_FLOAT_OFFSET) |
559 (tmp2 & ADT7410_T13_VALUE_FLOAT_MASK);
561 if (tmp1 < 0)
562 /* convert positive value to supplyment */
563 data = (ADT7410_T13_VALUE_SIGN << 1) - data;
564 data <<= ADT7410_T13_VALUE_OFFSET;
567 ret = adt7410_i2c_write_word(chip, bound_reg, data);
568 if (ret)
569 return -EIO;
571 return ret;
574 static ssize_t adt7410_show_t_alarm_high(struct device *dev,
575 struct device_attribute *attr,
576 char *buf)
578 return adt7410_show_t_bound(dev, attr,
579 ADT7410_T_ALARM_HIGH, buf);
582 static inline ssize_t adt7410_set_t_alarm_high(struct device *dev,
583 struct device_attribute *attr,
584 const char *buf,
585 size_t len)
587 return adt7410_set_t_bound(dev, attr,
588 ADT7410_T_ALARM_HIGH, buf, len);
591 static ssize_t adt7410_show_t_alarm_low(struct device *dev,
592 struct device_attribute *attr,
593 char *buf)
595 return adt7410_show_t_bound(dev, attr,
596 ADT7410_T_ALARM_LOW, buf);
599 static inline ssize_t adt7410_set_t_alarm_low(struct device *dev,
600 struct device_attribute *attr,
601 const char *buf,
602 size_t len)
604 return adt7410_set_t_bound(dev, attr,
605 ADT7410_T_ALARM_LOW, buf, len);
608 static ssize_t adt7410_show_t_crit(struct device *dev,
609 struct device_attribute *attr,
610 char *buf)
612 return adt7410_show_t_bound(dev, attr,
613 ADT7410_T_CRIT, buf);
616 static inline ssize_t adt7410_set_t_crit(struct device *dev,
617 struct device_attribute *attr,
618 const char *buf,
619 size_t len)
621 return adt7410_set_t_bound(dev, attr,
622 ADT7410_T_CRIT, buf, len);
625 static ssize_t adt7410_show_t_hyst(struct device *dev,
626 struct device_attribute *attr,
627 char *buf)
629 struct iio_dev *dev_info = dev_get_drvdata(dev);
630 struct adt7410_chip_info *chip = iio_priv(dev_info);
631 int ret;
632 u8 t_hyst;
634 ret = adt7410_i2c_read_byte(chip, ADT7410_T_HYST, &t_hyst);
635 if (ret)
636 return -EIO;
638 return sprintf(buf, "%d\n", t_hyst & ADT7410_T_HYST_MASK);
641 static inline ssize_t adt7410_set_t_hyst(struct device *dev,
642 struct device_attribute *attr,
643 const char *buf,
644 size_t len)
646 struct iio_dev *dev_info = dev_get_drvdata(dev);
647 struct adt7410_chip_info *chip = iio_priv(dev_info);
648 int ret;
649 unsigned long data;
650 u8 t_hyst;
652 ret = strict_strtol(buf, 10, &data);
654 if (ret || data > ADT7410_T_HYST_MASK)
655 return -EINVAL;
657 t_hyst = (u8)data;
659 ret = adt7410_i2c_write_byte(chip, ADT7410_T_HYST, t_hyst);
660 if (ret)
661 return -EIO;
663 return ret;
666 static IIO_DEVICE_ATTR(event_mode,
667 S_IRUGO | S_IWUSR,
668 adt7410_show_event_mode, adt7410_set_event_mode, 0);
669 static IIO_DEVICE_ATTR(available_event_modes,
670 S_IRUGO,
671 adt7410_show_available_event_modes, NULL, 0);
672 static IIO_DEVICE_ATTR(fault_queue,
673 S_IRUGO | S_IWUSR,
674 adt7410_show_fault_queue, adt7410_set_fault_queue, 0);
675 static IIO_DEVICE_ATTR(t_alarm_high,
676 S_IRUGO | S_IWUSR,
677 adt7410_show_t_alarm_high, adt7410_set_t_alarm_high, 0);
678 static IIO_DEVICE_ATTR(t_alarm_low,
679 S_IRUGO | S_IWUSR,
680 adt7410_show_t_alarm_low, adt7410_set_t_alarm_low, 0);
681 static IIO_DEVICE_ATTR(t_crit,
682 S_IRUGO | S_IWUSR,
683 adt7410_show_t_crit, adt7410_set_t_crit, 0);
684 static IIO_DEVICE_ATTR(t_hyst,
685 S_IRUGO | S_IWUSR,
686 adt7410_show_t_hyst, adt7410_set_t_hyst, 0);
688 static struct attribute *adt7410_event_int_attributes[] = {
689 &iio_dev_attr_event_mode.dev_attr.attr,
690 &iio_dev_attr_available_event_modes.dev_attr.attr,
691 &iio_dev_attr_fault_queue.dev_attr.attr,
692 &iio_dev_attr_t_alarm_high.dev_attr.attr,
693 &iio_dev_attr_t_alarm_low.dev_attr.attr,
694 &iio_dev_attr_t_hyst.dev_attr.attr,
695 NULL,
698 static struct attribute *adt7410_event_ct_attributes[] = {
699 &iio_dev_attr_event_mode.dev_attr.attr,
700 &iio_dev_attr_available_event_modes.dev_attr.attr,
701 &iio_dev_attr_fault_queue.dev_attr.attr,
702 &iio_dev_attr_t_crit.dev_attr.attr,
703 &iio_dev_attr_t_hyst.dev_attr.attr,
704 NULL,
707 static struct attribute_group adt7410_event_attribute_group[ADT7410_IRQS] = {
709 .attrs = adt7410_event_int_attributes,
710 }, {
711 .attrs = adt7410_event_ct_attributes,
715 static const struct iio_info adt7410_info = {
716 .attrs = &adt7410_attribute_group,
717 .num_interrupt_lines = ADT7410_IRQS,
718 .event_attrs = adt7410_event_attribute_group,
719 .driver_module = THIS_MODULE,
723 * device probe and remove
726 static int __devinit adt7410_probe(struct i2c_client *client,
727 const struct i2c_device_id *id)
729 struct adt7410_chip_info *chip;
730 struct iio_dev *indio_dev;
731 int ret = 0;
732 unsigned long *adt7410_platform_data = client->dev.platform_data;
734 indio_dev = iio_allocate_device(sizeof(*chip));
735 if (indio_dev == NULL) {
736 ret = -ENOMEM;
737 goto error_ret;
739 chip = iio_priv(indio_dev);
740 /* this is only used for device removal purposes */
741 i2c_set_clientdata(client, indio_dev);
743 chip->client = client;
745 indio_dev->name = id->name;
746 indio_dev->dev.parent = &client->dev;
747 indio_dev->info = &adt7410_info;
748 indio_dev->modes = INDIO_DIRECT_MODE;
750 ret = iio_device_register(indio_dev);
751 if (ret)
752 goto error_free_dev;
754 /* CT critcal temperature event. line 0 */
755 if (client->irq) {
756 ret = request_threaded_irq(client->irq,
757 NULL,
758 &adt7410_event_handler,
759 IRQF_TRIGGER_LOW,
760 id->name,
761 indio_dev);
762 if (ret)
763 goto error_unreg_dev;
766 /* INT bound temperature alarm event. line 1 */
767 if (adt7410_platform_data[0]) {
768 ret = request_threaded_irq(adt7410_platform_data[0],
769 NULL,
770 &adt7410_event_handler,
771 adt7410_platform_data[1],
772 id->name,
773 indio_dev);
774 if (ret)
775 goto error_unreg_ct_irq;
778 if (client->irq && adt7410_platform_data[0]) {
780 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
781 if (ret) {
782 ret = -EIO;
783 goto error_unreg_int_irq;
786 /* set irq polarity low level */
787 chip->config &= ~ADT7410_CT_POLARITY;
789 if (adt7410_platform_data[1] & IRQF_TRIGGER_HIGH)
790 chip->config |= ADT7410_INT_POLARITY;
791 else
792 chip->config &= ~ADT7410_INT_POLARITY;
794 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, chip->config);
795 if (ret) {
796 ret = -EIO;
797 goto error_unreg_int_irq;
801 dev_info(&client->dev, "%s temperature sensor registered.\n",
802 id->name);
804 return 0;
806 error_unreg_int_irq:
807 free_irq(adt7410_platform_data[0], indio_dev);
808 error_unreg_ct_irq:
809 free_irq(client->irq, indio_dev);
810 error_unreg_dev:
811 iio_device_unregister(indio_dev);
812 error_free_dev:
813 iio_free_device(indio_dev);
814 error_ret:
815 return ret;
818 static int __devexit adt7410_remove(struct i2c_client *client)
820 struct iio_dev *indio_dev = i2c_get_clientdata(client);
821 unsigned long *adt7410_platform_data = client->dev.platform_data;
823 if (adt7410_platform_data[0])
824 free_irq(adt7410_platform_data[0], indio_dev);
825 if (client->irq)
826 free_irq(client->irq, indio_dev);
827 iio_device_unregister(indio_dev);
829 return 0;
832 static const struct i2c_device_id adt7410_id[] = {
833 { "adt7410", 0 },
837 MODULE_DEVICE_TABLE(i2c, adt7410_id);
839 static struct i2c_driver adt7410_driver = {
840 .driver = {
841 .name = "adt7410",
843 .probe = adt7410_probe,
844 .remove = __devexit_p(adt7410_remove),
845 .id_table = adt7410_id,
848 static __init int adt7410_init(void)
850 return i2c_add_driver(&adt7410_driver);
853 static __exit void adt7410_exit(void)
855 i2c_del_driver(&adt7410_driver);
858 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
859 MODULE_DESCRIPTION("Analog Devices ADT7410 digital"
860 " temperature sensor driver");
861 MODULE_LICENSE("GPL v2");
863 module_init(adt7410_init);
864 module_exit(adt7410_exit);