10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
14 #include <linux/list.h>
16 #include <linux/module.h>
25 #define ADT7310_STATUS 0
26 #define ADT7310_CONFIG 1
27 #define ADT7310_TEMPERATURE 2
29 #define ADT7310_T_CRIT 4
30 #define ADT7310_T_HYST 5
31 #define ADT7310_T_ALARM_HIGH 6
32 #define ADT7310_T_ALARM_LOW 7
37 #define ADT7310_STAT_T_LOW 0x10
38 #define ADT7310_STAT_T_HIGH 0x20
39 #define ADT7310_STAT_T_CRIT 0x40
40 #define ADT7310_STAT_NOT_RDY 0x80
45 #define ADT7310_FAULT_QUEUE_MASK 0x3
46 #define ADT7310_CT_POLARITY 0x4
47 #define ADT7310_INT_POLARITY 0x8
48 #define ADT7310_EVENT_MODE 0x10
49 #define ADT7310_MODE_MASK 0x60
50 #define ADT7310_ONESHOT 0x20
51 #define ADT7310_SPS 0x40
52 #define ADT7310_PD 0x60
53 #define ADT7310_RESOLUTION 0x80
58 #define ADT7310_T16_VALUE_SIGN 0x8000
59 #define ADT7310_T16_VALUE_FLOAT_OFFSET 7
60 #define ADT7310_T16_VALUE_FLOAT_MASK 0x7F
61 #define ADT7310_T13_VALUE_SIGN 0x1000
62 #define ADT7310_T13_VALUE_OFFSET 3
63 #define ADT7310_T13_VALUE_FLOAT_OFFSET 4
64 #define ADT7310_T13_VALUE_FLOAT_MASK 0xF
65 #define ADT7310_T_HYST_MASK 0xF
66 #define ADT7310_DEVICE_ID_MASK 0x7
67 #define ADT7310_MANUFACTORY_ID_MASK 0xF8
68 #define ADT7310_MANUFACTORY_ID_OFFSET 3
71 #define ADT7310_CMD_REG_MASK 0x28
72 #define ADT7310_CMD_REG_OFFSET 3
73 #define ADT7310_CMD_READ 0x40
74 #define ADT7310_CMD_CON_READ 0x4
76 #define ADT7310_IRQS 2
98 ret = spi_write(spi_dev, &command,
sizeof(command));
100 dev_err(&spi_dev->
dev,
"SPI write command error\n");
104 ret = spi_read(spi_dev, (
u8 *)data,
sizeof(*data));
106 dev_err(&spi_dev->
dev,
"SPI read word error\n");
122 buf[1] = (
u8)(data >> 8);
123 buf[2] = (
u8)(data & 0xFF);
125 ret = spi_write(spi_dev, buf, 3);
127 dev_err(&spi_dev->
dev,
"SPI write word error\n");
141 ret = spi_write(spi_dev, &command,
sizeof(command));
143 dev_err(&spi_dev->
dev,
"SPI write command error\n");
147 ret = spi_read(spi_dev, data,
sizeof(*data));
149 dev_err(&spi_dev->
dev,
"SPI read byte error\n");
165 ret = spi_write(spi_dev, buf, 2);
167 dev_err(&spi_dev->
dev,
"SPI write byte error\n");
186 return sprintf(buf,
"power-down\n");
188 return sprintf(buf,
"one-shot\n");
201 struct iio_dev *dev_info = dev_to_iio_dev(dev);
211 if (
strcmp(buf,
"power-down"))
213 else if (
strcmp(buf,
"one-shot"))
215 else if (
strcmp(buf,
"sps"))
232 static ssize_t adt7310_show_available_modes(
struct device *dev,
236 return sprintf(buf,
"full\none-shot\nsps\npower-down\n");
245 struct iio_dev *dev_info = dev_to_iio_dev(dev);
259 return sprintf(buf,
"%d bits\n", bits);
267 struct iio_dev *dev_info = dev_to_iio_dev(dev);
295 adt7310_show_resolution,
296 adt7310_store_resolution,
303 struct iio_dev *dev_info = dev_to_iio_dev(dev);
308 ret = adt7310_spi_read_byte(chip,
ADT7310_ID, &
id);
312 return sprintf(buf,
"device id: 0x%x\nmanufactory id: 0x%x\n",
330 data = (
u16)((ADT7310_T16_VALUE_SIGN << 1) - (
u32)data);
333 return sprintf(buf,
"%c%d.%.7d\n", sign,
340 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
343 return sprintf(buf,
"%c%d.%.4d\n", sign,
353 struct iio_dev *dev_info = dev_to_iio_dev(dev);
372 return adt7310_convert_temperature(chip, data, buf);
377 static struct attribute *adt7310_attributes[] = {
378 &iio_dev_attr_available_modes.dev_attr.attr,
379 &iio_dev_attr_mode.dev_attr.attr,
380 &iio_dev_attr_resolution.dev_attr.attr,
381 &iio_dev_attr_id.dev_attr.attr,
382 &iio_dev_attr_value.dev_attr.attr,
387 .attrs = adt7310_attributes,
390 static irqreturn_t adt7310_event_handler(
int irq,
void *
private)
392 struct iio_dev *indio_dev =
private;
429 struct iio_dev *dev_info = dev_to_iio_dev(dev);
438 return sprintf(buf,
"interrupt\n");
440 return sprintf(buf,
"comparator\n");
448 struct iio_dev *dev_info = dev_to_iio_dev(dev);
458 if (
strcmp(buf,
"comparator") != 0)
470 static ssize_t adt7310_show_available_event_modes(
struct device *dev,
474 return sprintf(buf,
"comparator\ninterrupt\n");
481 struct iio_dev *dev_info = dev_to_iio_dev(dev);
497 struct iio_dev *dev_info = dev_to_iio_dev(dev);
522 static inline ssize_t adt7310_show_t_bound(
struct device *dev,
527 struct iio_dev *dev_info = dev_to_iio_dev(dev);
532 ret = adt7310_spi_read_word(chip, bound_reg, &data);
536 return adt7310_convert_temperature(chip, data, buf);
539 static inline ssize_t adt7310_set_t_bound(
struct device *dev,
545 struct iio_dev *dev_info = dev_to_iio_dev(dev);
556 if (ret || tmp1 > 127 || tmp1 < -128)
569 tmp2 = (tmp2 / 78125) * 78125;
577 tmp2 = (tmp2 / 625) * 625;
592 data = (
u16)((ADT7310_T16_VALUE_SIGN << 1) - (
u32)data);
599 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
603 ret = adt7310_spi_write_word(chip, bound_reg, data);
614 return adt7310_show_t_bound(dev, attr,
618 static inline ssize_t adt7310_set_t_alarm_high(
struct device *dev,
623 return adt7310_set_t_bound(dev, attr,
631 return adt7310_show_t_bound(dev, attr,
635 static inline ssize_t adt7310_set_t_alarm_low(
struct device *dev,
640 return adt7310_set_t_bound(dev, attr,
648 return adt7310_show_t_bound(dev, attr,
657 return adt7310_set_t_bound(dev, attr,
665 struct iio_dev *dev_info = dev_to_iio_dev(dev);
682 struct iio_dev *dev_info = dev_to_iio_dev(dev);
704 adt7310_show_event_mode, adt7310_set_event_mode, 0);
707 adt7310_show_available_event_modes,
NULL, 0);
710 adt7310_show_fault_queue, adt7310_set_fault_queue, 0);
713 adt7310_show_t_alarm_high, adt7310_set_t_alarm_high, 0);
716 adt7310_show_t_alarm_low, adt7310_set_t_alarm_low, 0);
719 adt7310_show_t_crit, adt7310_set_t_crit, 0);
722 adt7310_show_t_hyst, adt7310_set_t_hyst, 0);
724 static struct attribute *adt7310_event_int_attributes[] = {
725 &iio_dev_attr_event_mode.dev_attr.attr,
726 &iio_dev_attr_available_event_modes.dev_attr.attr,
727 &iio_dev_attr_fault_queue.dev_attr.attr,
728 &iio_dev_attr_t_alarm_high.dev_attr.attr,
729 &iio_dev_attr_t_alarm_low.dev_attr.attr,
730 &iio_dev_attr_t_crit.dev_attr.attr,
731 &iio_dev_attr_t_hyst.dev_attr.attr,
736 .attrs = adt7310_event_int_attributes,
740 static const struct iio_info adt7310_info = {
741 .attrs = &adt7310_attribute_group,
742 .event_attrs = &adt7310_event_attribute_group,
755 unsigned long *adt7310_platform_data = spi_dev->
dev.platform_data;
756 unsigned long irq_flags;
759 if (indio_dev ==
NULL) {
763 chip = iio_priv(indio_dev);
769 indio_dev->
dev.parent = &spi_dev->
dev;
771 indio_dev->
info = &adt7310_info;
776 if (adt7310_platform_data[2])
777 irq_flags = adt7310_platform_data[2];
782 &adt7310_event_handler,
791 if (adt7310_platform_data[0]) {
794 &adt7310_event_handler,
795 adt7310_platform_data[1] |
800 goto error_unreg_ct_irq;
803 if (spi_dev->
irq && adt7310_platform_data[0]) {
807 goto error_unreg_int_irq;
821 goto error_unreg_int_irq;
827 goto error_unreg_int_irq;
829 dev_info(&spi_dev->
dev,
"%s temperature sensor registered.\n",
835 free_irq(adt7310_platform_data[0], indio_dev);
847 unsigned long *adt7310_platform_data = spi_dev->
dev.platform_data;
851 if (adt7310_platform_data[0])
852 free_irq(adt7310_platform_data[0], indio_dev);
872 .probe = adt7310_probe,
874 .id_table = adt7310_id,
880 " temperature sensor driver");