Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / staging / iio / adc / adt7410.c
blob4157596ea3b0c4fc5ef99e8fa32493c2d5088493
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>
16 #include <linux/module.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/events.h>
23 * ADT7410 registers definition
26 #define ADT7410_TEMPERATURE 0
27 #define ADT7410_STATUS 2
28 #define ADT7410_CONFIG 3
29 #define ADT7410_T_ALARM_HIGH 4
30 #define ADT7410_T_ALARM_LOW 6
31 #define ADT7410_T_CRIT 8
32 #define ADT7410_T_HYST 0xA
33 #define ADT7410_ID 0xB
34 #define ADT7410_RESET 0x2F
37 * ADT7410 status
39 #define ADT7410_STAT_T_LOW 0x10
40 #define ADT7410_STAT_T_HIGH 0x20
41 #define ADT7410_STAT_T_CRIT 0x40
42 #define ADT7410_STAT_NOT_RDY 0x80
45 * ADT7410 config
47 #define ADT7410_FAULT_QUEUE_MASK 0x3
48 #define ADT7410_CT_POLARITY 0x4
49 #define ADT7410_INT_POLARITY 0x8
50 #define ADT7410_EVENT_MODE 0x10
51 #define ADT7410_MODE_MASK 0x60
52 #define ADT7410_ONESHOT 0x20
53 #define ADT7410_SPS 0x40
54 #define ADT7410_PD 0x60
55 #define ADT7410_RESOLUTION 0x80
58 * ADT7410 masks
60 #define ADT7410_T16_VALUE_SIGN 0x8000
61 #define ADT7410_T16_VALUE_FLOAT_OFFSET 7
62 #define ADT7410_T16_VALUE_FLOAT_MASK 0x7F
63 #define ADT7410_T13_VALUE_SIGN 0x1000
64 #define ADT7410_T13_VALUE_OFFSET 3
65 #define ADT7410_T13_VALUE_FLOAT_OFFSET 4
66 #define ADT7410_T13_VALUE_FLOAT_MASK 0xF
67 #define ADT7410_T_HYST_MASK 0xF
68 #define ADT7410_DEVICE_ID_MASK 0xF
69 #define ADT7410_MANUFACTORY_ID_MASK 0xF0
70 #define ADT7410_MANUFACTORY_ID_OFFSET 4
72 #define ADT7410_IRQS 2
75 * struct adt7410_chip_info - chip specifc information
78 struct adt7410_chip_info {
79 struct i2c_client *client;
80 u8 config;
84 * adt7410 register access by I2C
87 static int adt7410_i2c_read_word(struct adt7410_chip_info *chip, u8 reg, u16 *data)
89 struct i2c_client *client = chip->client;
90 int ret = 0;
92 ret = i2c_smbus_read_word_data(client, reg);
93 if (ret < 0) {
94 dev_err(&client->dev, "I2C read error\n");
95 return ret;
98 *data = swab16((u16)ret);
100 return 0;
103 static int adt7410_i2c_write_word(struct adt7410_chip_info *chip, u8 reg, u16 data)
105 struct i2c_client *client = chip->client;
106 int ret = 0;
108 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
109 if (ret < 0)
110 dev_err(&client->dev, "I2C write error\n");
112 return ret;
115 static int adt7410_i2c_read_byte(struct adt7410_chip_info *chip, u8 reg, u8 *data)
117 struct i2c_client *client = chip->client;
118 int ret = 0;
120 ret = i2c_smbus_read_byte_data(client, reg);
121 if (ret < 0) {
122 dev_err(&client->dev, "I2C read error\n");
123 return ret;
126 *data = (u8)ret;
128 return 0;
131 static int adt7410_i2c_write_byte(struct adt7410_chip_info *chip, u8 reg, u8 data)
133 struct i2c_client *client = chip->client;
134 int ret = 0;
136 ret = i2c_smbus_write_byte_data(client, reg, data);
137 if (ret < 0)
138 dev_err(&client->dev, "I2C write error\n");
140 return ret;
143 static ssize_t adt7410_show_mode(struct device *dev,
144 struct device_attribute *attr,
145 char *buf)
147 struct iio_dev *dev_info = dev_to_iio_dev(dev);
148 struct adt7410_chip_info *chip = iio_priv(dev_info);
149 u8 config;
151 config = chip->config & ADT7410_MODE_MASK;
153 switch (config) {
154 case ADT7410_PD:
155 return sprintf(buf, "power-down\n");
156 case ADT7410_ONESHOT:
157 return sprintf(buf, "one-shot\n");
158 case ADT7410_SPS:
159 return sprintf(buf, "sps\n");
160 default:
161 return sprintf(buf, "full\n");
165 static ssize_t adt7410_store_mode(struct device *dev,
166 struct device_attribute *attr,
167 const char *buf,
168 size_t len)
170 struct iio_dev *dev_info = dev_to_iio_dev(dev);
171 struct adt7410_chip_info *chip = iio_priv(dev_info);
172 u16 config;
173 int ret;
175 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
176 if (ret)
177 return -EIO;
179 config = chip->config & (~ADT7410_MODE_MASK);
180 if (strcmp(buf, "power-down"))
181 config |= ADT7410_PD;
182 else if (strcmp(buf, "one-shot"))
183 config |= ADT7410_ONESHOT;
184 else if (strcmp(buf, "sps"))
185 config |= ADT7410_SPS;
187 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
188 if (ret)
189 return -EIO;
191 chip->config = config;
193 return ret;
196 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
197 adt7410_show_mode,
198 adt7410_store_mode,
201 static ssize_t adt7410_show_available_modes(struct device *dev,
202 struct device_attribute *attr,
203 char *buf)
205 return sprintf(buf, "full\none-shot\nsps\npower-down\n");
208 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt7410_show_available_modes, NULL, 0);
210 static ssize_t adt7410_show_resolution(struct device *dev,
211 struct device_attribute *attr,
212 char *buf)
214 struct iio_dev *dev_info = dev_to_iio_dev(dev);
215 struct adt7410_chip_info *chip = iio_priv(dev_info);
216 int ret;
217 int bits;
219 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
220 if (ret)
221 return -EIO;
223 if (chip->config & ADT7410_RESOLUTION)
224 bits = 16;
225 else
226 bits = 13;
228 return sprintf(buf, "%d bits\n", bits);
231 static ssize_t adt7410_store_resolution(struct device *dev,
232 struct device_attribute *attr,
233 const char *buf,
234 size_t len)
236 struct iio_dev *dev_info = dev_to_iio_dev(dev);
237 struct adt7410_chip_info *chip = iio_priv(dev_info);
238 unsigned long data;
239 u16 config;
240 int ret;
242 ret = strict_strtoul(buf, 10, &data);
243 if (ret)
244 return -EINVAL;
246 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
247 if (ret)
248 return -EIO;
250 config = chip->config & (~ADT7410_RESOLUTION);
251 if (data)
252 config |= ADT7410_RESOLUTION;
254 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
255 if (ret)
256 return -EIO;
258 chip->config = config;
260 return len;
263 static IIO_DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR,
264 adt7410_show_resolution,
265 adt7410_store_resolution,
268 static ssize_t adt7410_show_id(struct device *dev,
269 struct device_attribute *attr,
270 char *buf)
272 struct iio_dev *dev_info = dev_to_iio_dev(dev);
273 struct adt7410_chip_info *chip = iio_priv(dev_info);
274 u8 id;
275 int ret;
277 ret = adt7410_i2c_read_byte(chip, ADT7410_ID, &id);
278 if (ret)
279 return -EIO;
281 return sprintf(buf, "device id: 0x%x\nmanufactory id: 0x%x\n",
282 id & ADT7410_DEVICE_ID_MASK,
283 (id & ADT7410_MANUFACTORY_ID_MASK) >> ADT7410_MANUFACTORY_ID_OFFSET);
286 static IIO_DEVICE_ATTR(id, S_IRUGO | S_IWUSR,
287 adt7410_show_id,
288 NULL,
291 static ssize_t adt7410_convert_temperature(struct adt7410_chip_info *chip,
292 u16 data, char *buf)
294 char sign = ' ';
296 if (!(chip->config & ADT7410_RESOLUTION))
297 data &= ~0x7;
299 if (data & ADT7410_T16_VALUE_SIGN) {
300 /* convert supplement to positive value */
301 data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
302 sign = '-';
304 return sprintf(buf, "%c%d.%.7d\n", sign,
305 (data >> ADT7410_T16_VALUE_FLOAT_OFFSET),
306 (data & ADT7410_T16_VALUE_FLOAT_MASK) * 78125);
309 static ssize_t adt7410_show_value(struct device *dev,
310 struct device_attribute *attr,
311 char *buf)
313 struct iio_dev *dev_info = dev_to_iio_dev(dev);
314 struct adt7410_chip_info *chip = iio_priv(dev_info);
315 u8 status;
316 u16 data;
317 int ret, i = 0;
319 do {
320 ret = adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status);
321 if (ret)
322 return -EIO;
323 i++;
324 if (i == 10000)
325 return -EIO;
326 } while (status & ADT7410_STAT_NOT_RDY);
328 ret = adt7410_i2c_read_word(chip, ADT7410_TEMPERATURE, &data);
329 if (ret)
330 return -EIO;
332 return adt7410_convert_temperature(chip, data, buf);
335 static IIO_DEVICE_ATTR(value, S_IRUGO, adt7410_show_value, NULL, 0);
337 static struct attribute *adt7410_attributes[] = {
338 &iio_dev_attr_available_modes.dev_attr.attr,
339 &iio_dev_attr_mode.dev_attr.attr,
340 &iio_dev_attr_resolution.dev_attr.attr,
341 &iio_dev_attr_id.dev_attr.attr,
342 &iio_dev_attr_value.dev_attr.attr,
343 NULL,
346 static const struct attribute_group adt7410_attribute_group = {
347 .attrs = adt7410_attributes,
350 static irqreturn_t adt7410_event_handler(int irq, void *private)
352 struct iio_dev *indio_dev = private;
353 struct adt7410_chip_info *chip = iio_priv(indio_dev);
354 s64 timestamp = iio_get_time_ns();
355 u8 status;
357 if (adt7410_i2c_read_byte(chip, ADT7410_STATUS, &status))
358 return IRQ_HANDLED;
360 if (status & ADT7410_STAT_T_HIGH)
361 iio_push_event(indio_dev,
362 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
363 IIO_EV_TYPE_THRESH,
364 IIO_EV_DIR_RISING),
365 timestamp);
366 if (status & ADT7410_STAT_T_LOW)
367 iio_push_event(indio_dev,
368 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
369 IIO_EV_TYPE_THRESH,
370 IIO_EV_DIR_FALLING),
371 timestamp);
372 if (status & ADT7410_STAT_T_CRIT)
373 iio_push_event(indio_dev,
374 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
375 IIO_EV_TYPE_THRESH,
376 IIO_EV_DIR_RISING),
377 timestamp);
379 return IRQ_HANDLED;
382 static ssize_t adt7410_show_event_mode(struct device *dev,
383 struct device_attribute *attr,
384 char *buf)
386 struct iio_dev *dev_info = dev_to_iio_dev(dev);
387 struct adt7410_chip_info *chip = iio_priv(dev_info);
388 int ret;
390 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
391 if (ret)
392 return -EIO;
394 if (chip->config & ADT7410_EVENT_MODE)
395 return sprintf(buf, "interrupt\n");
396 else
397 return sprintf(buf, "comparator\n");
400 static ssize_t adt7410_set_event_mode(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf,
403 size_t len)
405 struct iio_dev *dev_info = dev_to_iio_dev(dev);
406 struct adt7410_chip_info *chip = iio_priv(dev_info);
407 u16 config;
408 int ret;
410 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
411 if (ret)
412 return -EIO;
414 config = chip->config &= ~ADT7410_EVENT_MODE;
415 if (strcmp(buf, "comparator") != 0)
416 config |= ADT7410_EVENT_MODE;
418 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
419 if (ret)
420 return -EIO;
422 chip->config = config;
424 return ret;
427 static ssize_t adt7410_show_available_event_modes(struct device *dev,
428 struct device_attribute *attr,
429 char *buf)
431 return sprintf(buf, "comparator\ninterrupt\n");
434 static ssize_t adt7410_show_fault_queue(struct device *dev,
435 struct device_attribute *attr,
436 char *buf)
438 struct iio_dev *dev_info = dev_to_iio_dev(dev);
439 struct adt7410_chip_info *chip = iio_priv(dev_info);
440 int ret;
442 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
443 if (ret)
444 return -EIO;
446 return sprintf(buf, "%d\n", chip->config & ADT7410_FAULT_QUEUE_MASK);
449 static ssize_t adt7410_set_fault_queue(struct device *dev,
450 struct device_attribute *attr,
451 const char *buf,
452 size_t len)
454 struct iio_dev *dev_info = dev_to_iio_dev(dev);
455 struct adt7410_chip_info *chip = iio_priv(dev_info);
456 unsigned long data;
457 int ret;
458 u8 config;
460 ret = strict_strtoul(buf, 10, &data);
461 if (ret || data > 3)
462 return -EINVAL;
464 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
465 if (ret)
466 return -EIO;
468 config = chip->config & ~ADT7410_FAULT_QUEUE_MASK;
469 config |= data;
470 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, config);
471 if (ret)
472 return -EIO;
474 chip->config = config;
476 return ret;
479 static inline ssize_t adt7410_show_t_bound(struct device *dev,
480 struct device_attribute *attr,
481 u8 bound_reg,
482 char *buf)
484 struct iio_dev *dev_info = dev_to_iio_dev(dev);
485 struct adt7410_chip_info *chip = iio_priv(dev_info);
486 u16 data;
487 int ret;
489 ret = adt7410_i2c_read_word(chip, bound_reg, &data);
490 if (ret)
491 return -EIO;
493 return adt7410_convert_temperature(chip, data, buf);
496 static inline ssize_t adt7410_set_t_bound(struct device *dev,
497 struct device_attribute *attr,
498 u8 bound_reg,
499 const char *buf,
500 size_t len)
502 struct iio_dev *dev_info = dev_to_iio_dev(dev);
503 struct adt7410_chip_info *chip = iio_priv(dev_info);
504 long tmp1, tmp2;
505 u16 data;
506 char *pos;
507 int ret;
509 pos = strchr(buf, '.');
511 ret = strict_strtol(buf, 10, &tmp1);
513 if (ret || tmp1 > 127 || tmp1 < -128)
514 return -EINVAL;
516 if (pos) {
517 len = strlen(pos);
519 if (chip->config & ADT7410_RESOLUTION) {
520 if (len > ADT7410_T16_VALUE_FLOAT_OFFSET)
521 len = ADT7410_T16_VALUE_FLOAT_OFFSET;
522 pos[len] = 0;
523 ret = strict_strtol(pos, 10, &tmp2);
525 if (!ret)
526 tmp2 = (tmp2 / 78125) * 78125;
527 } else {
528 if (len > ADT7410_T13_VALUE_FLOAT_OFFSET)
529 len = ADT7410_T13_VALUE_FLOAT_OFFSET;
530 pos[len] = 0;
531 ret = strict_strtol(pos, 10, &tmp2);
533 if (!ret)
534 tmp2 = (tmp2 / 625) * 625;
538 if (tmp1 < 0)
539 data = (u16)(-tmp1);
540 else
541 data = (u16)tmp1;
543 if (chip->config & ADT7410_RESOLUTION) {
544 data = (data << ADT7410_T16_VALUE_FLOAT_OFFSET) |
545 (tmp2 & ADT7410_T16_VALUE_FLOAT_MASK);
547 if (tmp1 < 0)
548 /* convert positive value to supplyment */
549 data = (u16)((ADT7410_T16_VALUE_SIGN << 1) - (u32)data);
550 } else {
551 data = (data << ADT7410_T13_VALUE_FLOAT_OFFSET) |
552 (tmp2 & ADT7410_T13_VALUE_FLOAT_MASK);
554 if (tmp1 < 0)
555 /* convert positive value to supplyment */
556 data = (ADT7410_T13_VALUE_SIGN << 1) - data;
557 data <<= ADT7410_T13_VALUE_OFFSET;
560 ret = adt7410_i2c_write_word(chip, bound_reg, data);
561 if (ret)
562 return -EIO;
564 return ret;
567 static ssize_t adt7410_show_t_alarm_high(struct device *dev,
568 struct device_attribute *attr,
569 char *buf)
571 return adt7410_show_t_bound(dev, attr,
572 ADT7410_T_ALARM_HIGH, buf);
575 static inline ssize_t adt7410_set_t_alarm_high(struct device *dev,
576 struct device_attribute *attr,
577 const char *buf,
578 size_t len)
580 return adt7410_set_t_bound(dev, attr,
581 ADT7410_T_ALARM_HIGH, buf, len);
584 static ssize_t adt7410_show_t_alarm_low(struct device *dev,
585 struct device_attribute *attr,
586 char *buf)
588 return adt7410_show_t_bound(dev, attr,
589 ADT7410_T_ALARM_LOW, buf);
592 static inline ssize_t adt7410_set_t_alarm_low(struct device *dev,
593 struct device_attribute *attr,
594 const char *buf,
595 size_t len)
597 return adt7410_set_t_bound(dev, attr,
598 ADT7410_T_ALARM_LOW, buf, len);
601 static ssize_t adt7410_show_t_crit(struct device *dev,
602 struct device_attribute *attr,
603 char *buf)
605 return adt7410_show_t_bound(dev, attr,
606 ADT7410_T_CRIT, buf);
609 static inline ssize_t adt7410_set_t_crit(struct device *dev,
610 struct device_attribute *attr,
611 const char *buf,
612 size_t len)
614 return adt7410_set_t_bound(dev, attr,
615 ADT7410_T_CRIT, buf, len);
618 static ssize_t adt7410_show_t_hyst(struct device *dev,
619 struct device_attribute *attr,
620 char *buf)
622 struct iio_dev *dev_info = dev_to_iio_dev(dev);
623 struct adt7410_chip_info *chip = iio_priv(dev_info);
624 int ret;
625 u8 t_hyst;
627 ret = adt7410_i2c_read_byte(chip, ADT7410_T_HYST, &t_hyst);
628 if (ret)
629 return -EIO;
631 return sprintf(buf, "%d\n", t_hyst & ADT7410_T_HYST_MASK);
634 static inline ssize_t adt7410_set_t_hyst(struct device *dev,
635 struct device_attribute *attr,
636 const char *buf,
637 size_t len)
639 struct iio_dev *dev_info = dev_to_iio_dev(dev);
640 struct adt7410_chip_info *chip = iio_priv(dev_info);
641 int ret;
642 unsigned long data;
643 u8 t_hyst;
645 ret = strict_strtol(buf, 10, &data);
647 if (ret || data > ADT7410_T_HYST_MASK)
648 return -EINVAL;
650 t_hyst = (u8)data;
652 ret = adt7410_i2c_write_byte(chip, ADT7410_T_HYST, t_hyst);
653 if (ret)
654 return -EIO;
656 return ret;
659 static IIO_DEVICE_ATTR(event_mode,
660 S_IRUGO | S_IWUSR,
661 adt7410_show_event_mode, adt7410_set_event_mode, 0);
662 static IIO_DEVICE_ATTR(available_event_modes,
663 S_IRUGO,
664 adt7410_show_available_event_modes, NULL, 0);
665 static IIO_DEVICE_ATTR(fault_queue,
666 S_IRUGO | S_IWUSR,
667 adt7410_show_fault_queue, adt7410_set_fault_queue, 0);
668 static IIO_DEVICE_ATTR(t_alarm_high,
669 S_IRUGO | S_IWUSR,
670 adt7410_show_t_alarm_high, adt7410_set_t_alarm_high, 0);
671 static IIO_DEVICE_ATTR(t_alarm_low,
672 S_IRUGO | S_IWUSR,
673 adt7410_show_t_alarm_low, adt7410_set_t_alarm_low, 0);
674 static IIO_DEVICE_ATTR(t_crit,
675 S_IRUGO | S_IWUSR,
676 adt7410_show_t_crit, adt7410_set_t_crit, 0);
677 static IIO_DEVICE_ATTR(t_hyst,
678 S_IRUGO | S_IWUSR,
679 adt7410_show_t_hyst, adt7410_set_t_hyst, 0);
681 static struct attribute *adt7410_event_int_attributes[] = {
682 &iio_dev_attr_event_mode.dev_attr.attr,
683 &iio_dev_attr_available_event_modes.dev_attr.attr,
684 &iio_dev_attr_fault_queue.dev_attr.attr,
685 &iio_dev_attr_t_alarm_high.dev_attr.attr,
686 &iio_dev_attr_t_alarm_low.dev_attr.attr,
687 &iio_dev_attr_t_crit.dev_attr.attr,
688 &iio_dev_attr_t_hyst.dev_attr.attr,
689 NULL,
692 static struct attribute_group adt7410_event_attribute_group = {
693 .attrs = adt7410_event_int_attributes,
694 .name = "events",
697 static const struct iio_info adt7410_info = {
698 .attrs = &adt7410_attribute_group,
699 .event_attrs = &adt7410_event_attribute_group,
700 .driver_module = THIS_MODULE,
704 * device probe and remove
707 static int __devinit adt7410_probe(struct i2c_client *client,
708 const struct i2c_device_id *id)
710 struct adt7410_chip_info *chip;
711 struct iio_dev *indio_dev;
712 int ret = 0;
713 unsigned long *adt7410_platform_data = client->dev.platform_data;
714 unsigned long local_pdata[] = {0, 0};
716 indio_dev = iio_device_alloc(sizeof(*chip));
717 if (indio_dev == NULL) {
718 ret = -ENOMEM;
719 goto error_ret;
721 chip = iio_priv(indio_dev);
722 /* this is only used for device removal purposes */
723 i2c_set_clientdata(client, indio_dev);
725 chip->client = client;
727 indio_dev->name = id->name;
728 indio_dev->dev.parent = &client->dev;
729 indio_dev->info = &adt7410_info;
730 indio_dev->modes = INDIO_DIRECT_MODE;
732 if (!adt7410_platform_data)
733 adt7410_platform_data = local_pdata;
735 /* CT critcal temperature event. line 0 */
736 if (client->irq) {
737 ret = request_threaded_irq(client->irq,
738 NULL,
739 &adt7410_event_handler,
740 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
741 id->name,
742 indio_dev);
743 if (ret)
744 goto error_free_dev;
747 /* INT bound temperature alarm event. line 1 */
748 if (adt7410_platform_data[0]) {
749 ret = request_threaded_irq(adt7410_platform_data[0],
750 NULL,
751 &adt7410_event_handler,
752 adt7410_platform_data[1] |
753 IRQF_ONESHOT,
754 id->name,
755 indio_dev);
756 if (ret)
757 goto error_unreg_ct_irq;
760 ret = adt7410_i2c_read_byte(chip, ADT7410_CONFIG, &chip->config);
761 if (ret) {
762 ret = -EIO;
763 goto error_unreg_int_irq;
766 chip->config |= ADT7410_RESOLUTION;
768 if (client->irq && adt7410_platform_data[0]) {
770 /* set irq polarity low level */
771 chip->config &= ~ADT7410_CT_POLARITY;
773 if (adt7410_platform_data[1] & IRQF_TRIGGER_HIGH)
774 chip->config |= ADT7410_INT_POLARITY;
775 else
776 chip->config &= ~ADT7410_INT_POLARITY;
779 ret = adt7410_i2c_write_byte(chip, ADT7410_CONFIG, chip->config);
780 if (ret) {
781 ret = -EIO;
782 goto error_unreg_int_irq;
784 ret = iio_device_register(indio_dev);
785 if (ret)
786 goto error_unreg_int_irq;
788 dev_info(&client->dev, "%s temperature sensor registered.\n",
789 id->name);
791 return 0;
793 error_unreg_int_irq:
794 free_irq(adt7410_platform_data[0], indio_dev);
795 error_unreg_ct_irq:
796 free_irq(client->irq, indio_dev);
797 error_free_dev:
798 iio_device_free(indio_dev);
799 error_ret:
800 return ret;
803 static int __devexit adt7410_remove(struct i2c_client *client)
805 struct iio_dev *indio_dev = i2c_get_clientdata(client);
806 unsigned long *adt7410_platform_data = client->dev.platform_data;
808 iio_device_unregister(indio_dev);
809 if (adt7410_platform_data[0])
810 free_irq(adt7410_platform_data[0], indio_dev);
811 if (client->irq)
812 free_irq(client->irq, indio_dev);
813 iio_device_free(indio_dev);
815 return 0;
818 static const struct i2c_device_id adt7410_id[] = {
819 { "adt7410", 0 },
823 MODULE_DEVICE_TABLE(i2c, adt7410_id);
825 static struct i2c_driver adt7410_driver = {
826 .driver = {
827 .name = "adt7410",
829 .probe = adt7410_probe,
830 .remove = __devexit_p(adt7410_remove),
831 .id_table = adt7410_id,
833 module_i2c_driver(adt7410_driver);
835 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
836 MODULE_DESCRIPTION("Analog Devices ADT7410 digital"
837 " temperature sensor driver");
838 MODULE_LICENSE("GPL v2");