Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / adc / adt7310.c
blob1a41b8034405ea18955d17e0c68fe00e1bce7605
1 /*
2 * ADT7310 digital temperature sensor driver supporting ADT7310
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/spi/spi.h>
17 #include "../iio.h"
18 #include "../sysfs.h"
21 * ADT7310 registers definition
24 #define ADT7310_STATUS 0
25 #define ADT7310_CONFIG 1
26 #define ADT7310_TEMPERATURE 2
27 #define ADT7310_ID 3
28 #define ADT7310_T_CRIT 4
29 #define ADT7310_T_HYST 5
30 #define ADT7310_T_ALARM_HIGH 6
31 #define ADT7310_T_ALARM_LOW 7
34 * ADT7310 status
36 #define ADT7310_STAT_T_LOW 0x10
37 #define ADT7310_STAT_T_HIGH 0x20
38 #define ADT7310_STAT_T_CRIT 0x40
39 #define ADT7310_STAT_NOT_RDY 0x80
42 * ADT7310 config
44 #define ADT7310_FAULT_QUEUE_MASK 0x3
45 #define ADT7310_CT_POLARITY 0x4
46 #define ADT7310_INT_POLARITY 0x8
47 #define ADT7310_EVENT_MODE 0x10
48 #define ADT7310_MODE_MASK 0x60
49 #define ADT7310_ONESHOT 0x20
50 #define ADT7310_SPS 0x40
51 #define ADT7310_PD 0x60
52 #define ADT7310_RESOLUTION 0x80
55 * ADT7310 masks
57 #define ADT7310_T16_VALUE_SIGN 0x8000
58 #define ADT7310_T16_VALUE_FLOAT_OFFSET 7
59 #define ADT7310_T16_VALUE_FLOAT_MASK 0x7F
60 #define ADT7310_T13_VALUE_SIGN 0x1000
61 #define ADT7310_T13_VALUE_OFFSET 3
62 #define ADT7310_T13_VALUE_FLOAT_OFFSET 4
63 #define ADT7310_T13_VALUE_FLOAT_MASK 0xF
64 #define ADT7310_T_HYST_MASK 0xF
65 #define ADT7310_DEVICE_ID_MASK 0x7
66 #define ADT7310_MANUFACTORY_ID_MASK 0xF8
67 #define ADT7310_MANUFACTORY_ID_OFFSET 3
70 #define ADT7310_CMD_REG_MASK 0x28
71 #define ADT7310_CMD_REG_OFFSET 3
72 #define ADT7310_CMD_READ 0x40
73 #define ADT7310_CMD_CON_READ 0x4
75 #define ADT7310_IRQS 2
78 * struct adt7310_chip_info - chip specifc information
81 struct adt7310_chip_info {
82 struct spi_device *spi_dev;
83 u8 config;
87 * adt7310 register access by SPI
90 static int adt7310_spi_read_word(struct adt7310_chip_info *chip, u8 reg, u16 *data)
92 struct spi_device *spi_dev = chip->spi_dev;
93 u8 command = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
94 int ret = 0;
96 command |= ADT7310_CMD_READ;
97 ret = spi_write(spi_dev, &command, sizeof(command));
98 if (ret < 0) {
99 dev_err(&spi_dev->dev, "SPI write command error\n");
100 return ret;
103 ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
104 if (ret < 0) {
105 dev_err(&spi_dev->dev, "SPI read word error\n");
106 return ret;
109 *data = be16_to_cpu(*data);
111 return 0;
114 static int adt7310_spi_write_word(struct adt7310_chip_info *chip, u8 reg, u16 data)
116 struct spi_device *spi_dev = chip->spi_dev;
117 u8 buf[3];
118 int ret = 0;
120 buf[0] = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
121 buf[1] = (u8)(data >> 8);
122 buf[2] = (u8)(data & 0xFF);
124 ret = spi_write(spi_dev, buf, 3);
125 if (ret < 0) {
126 dev_err(&spi_dev->dev, "SPI write word error\n");
127 return ret;
130 return ret;
133 static int adt7310_spi_read_byte(struct adt7310_chip_info *chip, u8 reg, u8 *data)
135 struct spi_device *spi_dev = chip->spi_dev;
136 u8 command = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
137 int ret = 0;
139 command |= ADT7310_CMD_READ;
140 ret = spi_write(spi_dev, &command, sizeof(command));
141 if (ret < 0) {
142 dev_err(&spi_dev->dev, "SPI write command error\n");
143 return ret;
146 ret = spi_read(spi_dev, data, sizeof(*data));
147 if (ret < 0) {
148 dev_err(&spi_dev->dev, "SPI read byte error\n");
149 return ret;
152 return 0;
155 static int adt7310_spi_write_byte(struct adt7310_chip_info *chip, u8 reg, u8 data)
157 struct spi_device *spi_dev = chip->spi_dev;
158 u8 buf[2];
159 int ret = 0;
161 buf[0] = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
162 buf[1] = data;
164 ret = spi_write(spi_dev, buf, 2);
165 if (ret < 0) {
166 dev_err(&spi_dev->dev, "SPI write byte error\n");
167 return ret;
170 return ret;
173 static ssize_t adt7310_show_mode(struct device *dev,
174 struct device_attribute *attr,
175 char *buf)
177 struct iio_dev *dev_info = dev_get_drvdata(dev);
178 struct adt7310_chip_info *chip = iio_priv(dev_info);
179 u8 config;
181 config = chip->config & ADT7310_MODE_MASK;
183 switch (config) {
184 case ADT7310_PD:
185 return sprintf(buf, "power-down\n");
186 case ADT7310_ONESHOT:
187 return sprintf(buf, "one-shot\n");
188 case ADT7310_SPS:
189 return sprintf(buf, "sps\n");
190 default:
191 return sprintf(buf, "full\n");
195 static ssize_t adt7310_store_mode(struct device *dev,
196 struct device_attribute *attr,
197 const char *buf,
198 size_t len)
200 struct iio_dev *dev_info = dev_get_drvdata(dev);
201 struct adt7310_chip_info *chip = iio_priv(dev_info);
202 u16 config;
203 int ret;
205 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
206 if (ret)
207 return -EIO;
209 config = chip->config & (~ADT7310_MODE_MASK);
210 if (strcmp(buf, "power-down"))
211 config |= ADT7310_PD;
212 else if (strcmp(buf, "one-shot"))
213 config |= ADT7310_ONESHOT;
214 else if (strcmp(buf, "sps"))
215 config |= ADT7310_SPS;
217 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
218 if (ret)
219 return -EIO;
221 chip->config = config;
223 return len;
226 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
227 adt7310_show_mode,
228 adt7310_store_mode,
231 static ssize_t adt7310_show_available_modes(struct device *dev,
232 struct device_attribute *attr,
233 char *buf)
235 return sprintf(buf, "full\none-shot\nsps\npower-down\n");
238 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt7310_show_available_modes, NULL, 0);
240 static ssize_t adt7310_show_resolution(struct device *dev,
241 struct device_attribute *attr,
242 char *buf)
244 struct iio_dev *dev_info = dev_get_drvdata(dev);
245 struct adt7310_chip_info *chip = iio_priv(dev_info);
246 int ret;
247 int bits;
249 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
250 if (ret)
251 return -EIO;
253 if (chip->config & ADT7310_RESOLUTION)
254 bits = 16;
255 else
256 bits = 13;
258 return sprintf(buf, "%d bits\n", bits);
261 static ssize_t adt7310_store_resolution(struct device *dev,
262 struct device_attribute *attr,
263 const char *buf,
264 size_t len)
266 struct iio_dev *dev_info = dev_get_drvdata(dev);
267 struct adt7310_chip_info *chip = iio_priv(dev_info);
268 unsigned long data;
269 u16 config;
270 int ret;
272 ret = strict_strtoul(buf, 10, &data);
273 if (ret)
274 return -EINVAL;
276 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
277 if (ret)
278 return -EIO;
280 config = chip->config & (~ADT7310_RESOLUTION);
281 if (data)
282 config |= ADT7310_RESOLUTION;
284 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
285 if (ret)
286 return -EIO;
288 chip->config = config;
290 return len;
293 static IIO_DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR,
294 adt7310_show_resolution,
295 adt7310_store_resolution,
298 static ssize_t adt7310_show_id(struct device *dev,
299 struct device_attribute *attr,
300 char *buf)
302 struct iio_dev *dev_info = dev_get_drvdata(dev);
303 struct adt7310_chip_info *chip = iio_priv(dev_info);
304 u8 id;
305 int ret;
307 ret = adt7310_spi_read_byte(chip, ADT7310_ID, &id);
308 if (ret)
309 return -EIO;
311 return sprintf(buf, "device id: 0x%x\nmanufactory id: 0x%x\n",
312 id & ADT7310_DEVICE_ID_MASK,
313 (id & ADT7310_MANUFACTORY_ID_MASK) >> ADT7310_MANUFACTORY_ID_OFFSET);
316 static IIO_DEVICE_ATTR(id, S_IRUGO | S_IWUSR,
317 adt7310_show_id,
318 NULL,
321 static ssize_t adt7310_convert_temperature(struct adt7310_chip_info *chip,
322 u16 data, char *buf)
324 char sign = ' ';
326 if (chip->config & ADT7310_RESOLUTION) {
327 if (data & ADT7310_T16_VALUE_SIGN) {
328 /* convert supplement to positive value */
329 data = (u16)((ADT7310_T16_VALUE_SIGN << 1) - (u32)data);
330 sign = '-';
332 return sprintf(buf, "%c%d.%.7d\n", sign,
333 (data >> ADT7310_T16_VALUE_FLOAT_OFFSET),
334 (data & ADT7310_T16_VALUE_FLOAT_MASK) * 78125);
335 } else {
336 if (data & ADT7310_T13_VALUE_SIGN) {
337 /* convert supplement to positive value */
338 data >>= ADT7310_T13_VALUE_OFFSET;
339 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
340 sign = '-';
342 return sprintf(buf, "%c%d.%.4d\n", sign,
343 (data >> ADT7310_T13_VALUE_FLOAT_OFFSET),
344 (data & ADT7310_T13_VALUE_FLOAT_MASK) * 625);
348 static ssize_t adt7310_show_value(struct device *dev,
349 struct device_attribute *attr,
350 char *buf)
352 struct iio_dev *dev_info = dev_get_drvdata(dev);
353 struct adt7310_chip_info *chip = iio_priv(dev_info);
354 u8 status;
355 u16 data;
356 int ret, i = 0;
358 do {
359 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
360 if (ret)
361 return -EIO;
362 i++;
363 if (i == 10000)
364 return -EIO;
365 } while (status & ADT7310_STAT_NOT_RDY);
367 ret = adt7310_spi_read_word(chip, ADT7310_TEMPERATURE, &data);
368 if (ret)
369 return -EIO;
371 return adt7310_convert_temperature(chip, data, buf);
374 static IIO_DEVICE_ATTR(value, S_IRUGO, adt7310_show_value, NULL, 0);
376 static struct attribute *adt7310_attributes[] = {
377 &iio_dev_attr_available_modes.dev_attr.attr,
378 &iio_dev_attr_mode.dev_attr.attr,
379 &iio_dev_attr_resolution.dev_attr.attr,
380 &iio_dev_attr_id.dev_attr.attr,
381 &iio_dev_attr_value.dev_attr.attr,
382 NULL,
385 static const struct attribute_group adt7310_attribute_group = {
386 .attrs = adt7310_attributes,
389 static irqreturn_t adt7310_event_handler(int irq, void *private)
391 struct iio_dev *indio_dev = private;
392 struct adt7310_chip_info *chip = iio_priv(indio_dev);
393 s64 timestamp = iio_get_time_ns();
394 u8 status;
395 int ret;
397 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
398 if (ret)
399 return ret;
401 if (status & ADT7310_STAT_T_HIGH)
402 iio_push_event(indio_dev, 0,
403 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
404 IIO_EV_TYPE_THRESH,
405 IIO_EV_DIR_RISING),
406 timestamp);
407 if (status & ADT7310_STAT_T_LOW)
408 iio_push_event(indio_dev, 0,
409 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
410 IIO_EV_TYPE_THRESH,
411 IIO_EV_DIR_FALLING),
412 timestamp);
413 if (status & ADT7310_STAT_T_CRIT)
414 iio_push_event(indio_dev, 0,
415 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
416 IIO_EV_TYPE_THRESH,
417 IIO_EV_DIR_RISING),
418 timestamp);
419 return IRQ_HANDLED;
422 static ssize_t adt7310_show_event_mode(struct device *dev,
423 struct device_attribute *attr,
424 char *buf)
426 struct iio_dev *dev_info = dev_get_drvdata(dev);
427 struct adt7310_chip_info *chip = iio_priv(dev_info);
428 int ret;
430 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
431 if (ret)
432 return -EIO;
434 if (chip->config & ADT7310_EVENT_MODE)
435 return sprintf(buf, "interrupt\n");
436 else
437 return sprintf(buf, "comparator\n");
440 static ssize_t adt7310_set_event_mode(struct device *dev,
441 struct device_attribute *attr,
442 const char *buf,
443 size_t len)
445 struct iio_dev *dev_info = dev_get_drvdata(dev);
446 struct adt7310_chip_info *chip = iio_priv(dev_info);
447 u16 config;
448 int ret;
450 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
451 if (ret)
452 return -EIO;
454 config = chip->config &= ~ADT7310_EVENT_MODE;
455 if (strcmp(buf, "comparator") != 0)
456 config |= ADT7310_EVENT_MODE;
458 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
459 if (ret)
460 return -EIO;
462 chip->config = config;
464 return len;
467 static ssize_t adt7310_show_available_event_modes(struct device *dev,
468 struct device_attribute *attr,
469 char *buf)
471 return sprintf(buf, "comparator\ninterrupt\n");
474 static ssize_t adt7310_show_fault_queue(struct device *dev,
475 struct device_attribute *attr,
476 char *buf)
478 struct iio_dev *dev_info = dev_get_drvdata(dev);
479 struct adt7310_chip_info *chip = iio_priv(dev_info);
480 int ret;
482 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
483 if (ret)
484 return -EIO;
486 return sprintf(buf, "%d\n", chip->config & ADT7310_FAULT_QUEUE_MASK);
489 static ssize_t adt7310_set_fault_queue(struct device *dev,
490 struct device_attribute *attr,
491 const char *buf,
492 size_t len)
494 struct iio_dev *dev_info = dev_get_drvdata(dev);
495 struct adt7310_chip_info *chip = iio_priv(dev_info);
496 unsigned long data;
497 int ret;
498 u8 config;
500 ret = strict_strtoul(buf, 10, &data);
501 if (ret || data > 3)
502 return -EINVAL;
504 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
505 if (ret)
506 return -EIO;
508 config = chip->config & ~ADT7310_FAULT_QUEUE_MASK;
509 config |= data;
510 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
511 if (ret)
512 return -EIO;
514 chip->config = config;
516 return len;
519 static inline ssize_t adt7310_show_t_bound(struct device *dev,
520 struct device_attribute *attr,
521 u8 bound_reg,
522 char *buf)
524 struct iio_dev *dev_info = dev_get_drvdata(dev);
525 struct adt7310_chip_info *chip = iio_priv(dev_info);
526 u16 data;
527 int ret;
529 ret = adt7310_spi_read_word(chip, bound_reg, &data);
530 if (ret)
531 return -EIO;
533 return adt7310_convert_temperature(chip, data, buf);
536 static inline ssize_t adt7310_set_t_bound(struct device *dev,
537 struct device_attribute *attr,
538 u8 bound_reg,
539 const char *buf,
540 size_t len)
542 struct iio_dev *dev_info = dev_get_drvdata(dev);
543 struct adt7310_chip_info *chip = iio_priv(dev_info);
544 long tmp1, tmp2;
545 u16 data;
546 char *pos;
547 int ret;
549 pos = strchr(buf, '.');
551 ret = strict_strtol(buf, 10, &tmp1);
553 if (ret || tmp1 > 127 || tmp1 < -128)
554 return -EINVAL;
556 if (pos) {
557 len = strlen(pos);
559 if (chip->config & ADT7310_RESOLUTION) {
560 if (len > ADT7310_T16_VALUE_FLOAT_OFFSET)
561 len = ADT7310_T16_VALUE_FLOAT_OFFSET;
562 pos[len] = 0;
563 ret = strict_strtol(pos, 10, &tmp2);
565 if (!ret)
566 tmp2 = (tmp2 / 78125) * 78125;
567 } else {
568 if (len > ADT7310_T13_VALUE_FLOAT_OFFSET)
569 len = ADT7310_T13_VALUE_FLOAT_OFFSET;
570 pos[len] = 0;
571 ret = strict_strtol(pos, 10, &tmp2);
573 if (!ret)
574 tmp2 = (tmp2 / 625) * 625;
578 if (tmp1 < 0)
579 data = (u16)(-tmp1);
580 else
581 data = (u16)tmp1;
583 if (chip->config & ADT7310_RESOLUTION) {
584 data = (data << ADT7310_T16_VALUE_FLOAT_OFFSET) |
585 (tmp2 & ADT7310_T16_VALUE_FLOAT_MASK);
587 if (tmp1 < 0)
588 /* convert positive value to supplyment */
589 data = (u16)((ADT7310_T16_VALUE_SIGN << 1) - (u32)data);
590 } else {
591 data = (data << ADT7310_T13_VALUE_FLOAT_OFFSET) |
592 (tmp2 & ADT7310_T13_VALUE_FLOAT_MASK);
594 if (tmp1 < 0)
595 /* convert positive value to supplyment */
596 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
597 data <<= ADT7310_T13_VALUE_OFFSET;
600 ret = adt7310_spi_write_word(chip, bound_reg, data);
601 if (ret)
602 return -EIO;
604 return len;
607 static ssize_t adt7310_show_t_alarm_high(struct device *dev,
608 struct device_attribute *attr,
609 char *buf)
611 return adt7310_show_t_bound(dev, attr,
612 ADT7310_T_ALARM_HIGH, buf);
615 static inline ssize_t adt7310_set_t_alarm_high(struct device *dev,
616 struct device_attribute *attr,
617 const char *buf,
618 size_t len)
620 return adt7310_set_t_bound(dev, attr,
621 ADT7310_T_ALARM_HIGH, buf, len);
624 static ssize_t adt7310_show_t_alarm_low(struct device *dev,
625 struct device_attribute *attr,
626 char *buf)
628 return adt7310_show_t_bound(dev, attr,
629 ADT7310_T_ALARM_LOW, buf);
632 static inline ssize_t adt7310_set_t_alarm_low(struct device *dev,
633 struct device_attribute *attr,
634 const char *buf,
635 size_t len)
637 return adt7310_set_t_bound(dev, attr,
638 ADT7310_T_ALARM_LOW, buf, len);
641 static ssize_t adt7310_show_t_crit(struct device *dev,
642 struct device_attribute *attr,
643 char *buf)
645 return adt7310_show_t_bound(dev, attr,
646 ADT7310_T_CRIT, buf);
649 static inline ssize_t adt7310_set_t_crit(struct device *dev,
650 struct device_attribute *attr,
651 const char *buf,
652 size_t len)
654 return adt7310_set_t_bound(dev, attr,
655 ADT7310_T_CRIT, buf, len);
658 static ssize_t adt7310_show_t_hyst(struct device *dev,
659 struct device_attribute *attr,
660 char *buf)
662 struct iio_dev *dev_info = dev_get_drvdata(dev);
663 struct adt7310_chip_info *chip = iio_priv(dev_info);
664 int ret;
665 u8 t_hyst;
667 ret = adt7310_spi_read_byte(chip, ADT7310_T_HYST, &t_hyst);
668 if (ret)
669 return -EIO;
671 return sprintf(buf, "%d\n", t_hyst & ADT7310_T_HYST_MASK);
674 static inline ssize_t adt7310_set_t_hyst(struct device *dev,
675 struct device_attribute *attr,
676 const char *buf,
677 size_t len)
679 struct iio_dev *dev_info = dev_get_drvdata(dev);
680 struct adt7310_chip_info *chip = iio_priv(dev_info);
681 int ret;
682 unsigned long data;
683 u8 t_hyst;
685 ret = strict_strtol(buf, 10, &data);
687 if (ret || data > ADT7310_T_HYST_MASK)
688 return -EINVAL;
690 t_hyst = (u8)data;
692 ret = adt7310_spi_write_byte(chip, ADT7310_T_HYST, t_hyst);
693 if (ret)
694 return -EIO;
696 return len;
699 static IIO_DEVICE_ATTR(event_mode,
700 S_IRUGO | S_IWUSR,
701 adt7310_show_event_mode, adt7310_set_event_mode, 0);
702 static IIO_DEVICE_ATTR(available_event_modes,
703 S_IRUGO | S_IWUSR,
704 adt7310_show_available_event_modes, NULL, 0);
705 static IIO_DEVICE_ATTR(fault_queue,
706 S_IRUGO | S_IWUSR,
707 adt7310_show_fault_queue, adt7310_set_fault_queue, 0);
708 static IIO_DEVICE_ATTR(t_alarm_high,
709 S_IRUGO | S_IWUSR,
710 adt7310_show_t_alarm_high, adt7310_set_t_alarm_high, 0);
711 static IIO_DEVICE_ATTR(t_alarm_low,
712 S_IRUGO | S_IWUSR,
713 adt7310_show_t_alarm_low, adt7310_set_t_alarm_low, 0);
714 static IIO_DEVICE_ATTR(t_crit,
715 S_IRUGO | S_IWUSR,
716 adt7310_show_t_crit, adt7310_set_t_crit, 0);
717 static IIO_DEVICE_ATTR(t_hyst,
718 S_IRUGO | S_IWUSR,
719 adt7310_show_t_hyst, adt7310_set_t_hyst, 0);
721 static struct attribute *adt7310_event_int_attributes[] = {
722 &iio_dev_attr_event_mode.dev_attr.attr,
723 &iio_dev_attr_available_event_modes.dev_attr.attr,
724 &iio_dev_attr_fault_queue.dev_attr.attr,
725 &iio_dev_attr_t_alarm_high.dev_attr.attr,
726 &iio_dev_attr_t_alarm_low.dev_attr.attr,
727 &iio_dev_attr_t_hyst.dev_attr.attr,
728 NULL,
731 static struct attribute *adt7310_event_ct_attributes[] = {
732 &iio_dev_attr_event_mode.dev_attr.attr,
733 &iio_dev_attr_available_event_modes.dev_attr.attr,
734 &iio_dev_attr_fault_queue.dev_attr.attr,
735 &iio_dev_attr_t_crit.dev_attr.attr,
736 &iio_dev_attr_t_hyst.dev_attr.attr,
737 NULL,
740 static struct attribute_group adt7310_event_attribute_group[ADT7310_IRQS] = {
742 .attrs = adt7310_event_int_attributes,
743 }, {
744 .attrs = adt7310_event_ct_attributes,
748 static const struct iio_info adt7310_info = {
749 .attrs = &adt7310_attribute_group,
750 .num_interrupt_lines = ADT7310_IRQS,
751 .event_attrs = adt7310_event_attribute_group,
752 .driver_module = THIS_MODULE,
756 * device probe and remove
759 static int __devinit adt7310_probe(struct spi_device *spi_dev)
761 struct adt7310_chip_info *chip;
762 struct iio_dev *indio_dev;
763 int ret = 0;
764 unsigned long *adt7310_platform_data = spi_dev->dev.platform_data;
765 unsigned long irq_flags;
767 indio_dev = iio_allocate_device(sizeof(*chip));
768 if (indio_dev == NULL) {
769 ret = -ENOMEM;
770 goto error_ret;
772 chip = iio_priv(indio_dev);
773 /* this is only used for device removal purposes */
774 dev_set_drvdata(&spi_dev->dev, indio_dev);
776 chip->spi_dev = spi_dev;
778 indio_dev->dev.parent = &spi_dev->dev;
779 indio_dev->name = spi_get_device_id(spi_dev)->name;
780 indio_dev->info = &adt7310_info;
781 indio_dev->modes = INDIO_DIRECT_MODE;
783 ret = iio_device_register(indio_dev);
784 if (ret)
785 goto error_free_dev;
787 /* CT critcal temperature event. line 0 */
788 if (spi_dev->irq) {
789 if (adt7310_platform_data[2])
790 irq_flags = adt7310_platform_data[2];
791 else
792 irq_flags = IRQF_TRIGGER_LOW;
793 ret = request_threaded_irq(spi_dev->irq,
794 NULL,
795 &adt7310_event_handler,
796 irq_flags,
797 indio_dev->name,
798 indio_dev);
799 if (ret)
800 goto error_unreg_dev;
803 /* INT bound temperature alarm event. line 1 */
804 if (adt7310_platform_data[0]) {
805 ret = request_threaded_irq(adt7310_platform_data[0],
806 NULL,
807 &adt7310_event_handler,
808 adt7310_platform_data[1],
809 indio_dev->name,
810 indio_dev);
811 if (ret)
812 goto error_unreg_ct_irq;
815 if (spi_dev->irq && adt7310_platform_data[0]) {
816 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
817 if (ret) {
818 ret = -EIO;
819 goto error_unreg_int_irq;
822 /* set irq polarity low level */
823 chip->config &= ~ADT7310_CT_POLARITY;
825 if (adt7310_platform_data[1] & IRQF_TRIGGER_HIGH)
826 chip->config |= ADT7310_INT_POLARITY;
827 else
828 chip->config &= ~ADT7310_INT_POLARITY;
830 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, chip->config);
831 if (ret) {
832 ret = -EIO;
833 goto error_unreg_int_irq;
837 dev_info(&spi_dev->dev, "%s temperature sensor registered.\n",
838 indio_dev->name);
840 return 0;
842 error_unreg_int_irq:
843 free_irq(adt7310_platform_data[0], indio_dev);
844 error_unreg_ct_irq:
845 free_irq(spi_dev->irq, indio_dev);
846 error_unreg_dev:
847 iio_device_unregister(indio_dev);
848 error_free_dev:
849 iio_free_device(indio_dev);
850 error_ret:
851 return ret;
854 static int __devexit adt7310_remove(struct spi_device *spi_dev)
856 struct iio_dev *indio_dev = dev_get_drvdata(&spi_dev->dev);
857 unsigned long *adt7310_platform_data = spi_dev->dev.platform_data;
859 dev_set_drvdata(&spi_dev->dev, NULL);
860 if (adt7310_platform_data[0])
861 free_irq(adt7310_platform_data[0], indio_dev);
862 if (spi_dev->irq)
863 free_irq(spi_dev->irq, indio_dev);
864 iio_device_unregister(indio_dev);
865 iio_free_device(indio_dev);
867 return 0;
870 static const struct spi_device_id adt7310_id[] = {
871 { "adt7310", 0 },
875 MODULE_DEVICE_TABLE(spi, adt7310_id);
877 static struct spi_driver adt7310_driver = {
878 .driver = {
879 .name = "adt7310",
880 .bus = &spi_bus_type,
881 .owner = THIS_MODULE,
883 .probe = adt7310_probe,
884 .remove = __devexit_p(adt7310_remove),
885 .id_table = adt7310_id,
888 static __init int adt7310_init(void)
890 return spi_register_driver(&adt7310_driver);
893 static __exit void adt7310_exit(void)
895 spi_unregister_driver(&adt7310_driver);
898 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
899 MODULE_DESCRIPTION("Analog Devices ADT7310 digital"
900 " temperature sensor driver");
901 MODULE_LICENSE("GPL v2");
903 module_init(adt7310_init);
904 module_exit(adt7310_exit);