2 * ADT7310 digital temperature sensor driver supporting ADT7310
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
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>
16 #include <linux/module.h>
22 * ADT7310 registers definition
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
79 * struct adt7310_chip_info - chip specifc information
82 struct adt7310_chip_info {
83 struct spi_device *spi_dev;
88 * adt7310 register access by SPI
91 static int adt7310_spi_read_word(struct adt7310_chip_info *chip, u8 reg, u16 *data)
93 struct spi_device *spi_dev = chip->spi_dev;
94 u8 command = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
97 command |= ADT7310_CMD_READ;
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");
110 *data = be16_to_cpu(*data);
115 static int adt7310_spi_write_word(struct adt7310_chip_info *chip, u8 reg, u16 data)
117 struct spi_device *spi_dev = chip->spi_dev;
121 buf[0] = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
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");
134 static int adt7310_spi_read_byte(struct adt7310_chip_info *chip, u8 reg, u8 *data)
136 struct spi_device *spi_dev = chip->spi_dev;
137 u8 command = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
140 command |= ADT7310_CMD_READ;
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");
156 static int adt7310_spi_write_byte(struct adt7310_chip_info *chip, u8 reg, u8 data)
158 struct spi_device *spi_dev = chip->spi_dev;
162 buf[0] = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
165 ret = spi_write(spi_dev, buf, 2);
167 dev_err(&spi_dev->dev, "SPI write byte error\n");
174 static ssize_t adt7310_show_mode(struct device *dev,
175 struct device_attribute *attr,
178 struct iio_dev *dev_info = dev_get_drvdata(dev);
179 struct adt7310_chip_info *chip = iio_priv(dev_info);
182 config = chip->config & ADT7310_MODE_MASK;
186 return sprintf(buf, "power-down\n");
187 case ADT7310_ONESHOT:
188 return sprintf(buf, "one-shot\n");
190 return sprintf(buf, "sps\n");
192 return sprintf(buf, "full\n");
196 static ssize_t adt7310_store_mode(struct device *dev,
197 struct device_attribute *attr,
201 struct iio_dev *dev_info = dev_get_drvdata(dev);
202 struct adt7310_chip_info *chip = iio_priv(dev_info);
206 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
210 config = chip->config & (~ADT7310_MODE_MASK);
211 if (strcmp(buf, "power-down"))
212 config |= ADT7310_PD;
213 else if (strcmp(buf, "one-shot"))
214 config |= ADT7310_ONESHOT;
215 else if (strcmp(buf, "sps"))
216 config |= ADT7310_SPS;
218 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
222 chip->config = config;
227 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
232 static ssize_t adt7310_show_available_modes(struct device *dev,
233 struct device_attribute *attr,
236 return sprintf(buf, "full\none-shot\nsps\npower-down\n");
239 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, adt7310_show_available_modes, NULL, 0);
241 static ssize_t adt7310_show_resolution(struct device *dev,
242 struct device_attribute *attr,
245 struct iio_dev *dev_info = dev_get_drvdata(dev);
246 struct adt7310_chip_info *chip = iio_priv(dev_info);
250 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
254 if (chip->config & ADT7310_RESOLUTION)
259 return sprintf(buf, "%d bits\n", bits);
262 static ssize_t adt7310_store_resolution(struct device *dev,
263 struct device_attribute *attr,
267 struct iio_dev *dev_info = dev_get_drvdata(dev);
268 struct adt7310_chip_info *chip = iio_priv(dev_info);
273 ret = strict_strtoul(buf, 10, &data);
277 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
281 config = chip->config & (~ADT7310_RESOLUTION);
283 config |= ADT7310_RESOLUTION;
285 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
289 chip->config = config;
294 static IIO_DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR,
295 adt7310_show_resolution,
296 adt7310_store_resolution,
299 static ssize_t adt7310_show_id(struct device *dev,
300 struct device_attribute *attr,
303 struct iio_dev *dev_info = dev_get_drvdata(dev);
304 struct adt7310_chip_info *chip = iio_priv(dev_info);
308 ret = adt7310_spi_read_byte(chip, ADT7310_ID, &id);
312 return sprintf(buf, "device id: 0x%x\nmanufactory id: 0x%x\n",
313 id & ADT7310_DEVICE_ID_MASK,
314 (id & ADT7310_MANUFACTORY_ID_MASK) >> ADT7310_MANUFACTORY_ID_OFFSET);
317 static IIO_DEVICE_ATTR(id, S_IRUGO | S_IWUSR,
322 static ssize_t adt7310_convert_temperature(struct adt7310_chip_info *chip,
327 if (chip->config & ADT7310_RESOLUTION) {
328 if (data & ADT7310_T16_VALUE_SIGN) {
329 /* convert supplement to positive value */
330 data = (u16)((ADT7310_T16_VALUE_SIGN << 1) - (u32)data);
333 return sprintf(buf, "%c%d.%.7d\n", sign,
334 (data >> ADT7310_T16_VALUE_FLOAT_OFFSET),
335 (data & ADT7310_T16_VALUE_FLOAT_MASK) * 78125);
337 if (data & ADT7310_T13_VALUE_SIGN) {
338 /* convert supplement to positive value */
339 data >>= ADT7310_T13_VALUE_OFFSET;
340 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
343 return sprintf(buf, "%c%d.%.4d\n", sign,
344 (data >> ADT7310_T13_VALUE_FLOAT_OFFSET),
345 (data & ADT7310_T13_VALUE_FLOAT_MASK) * 625);
349 static ssize_t adt7310_show_value(struct device *dev,
350 struct device_attribute *attr,
353 struct iio_dev *dev_info = dev_get_drvdata(dev);
354 struct adt7310_chip_info *chip = iio_priv(dev_info);
360 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
366 } while (status & ADT7310_STAT_NOT_RDY);
368 ret = adt7310_spi_read_word(chip, ADT7310_TEMPERATURE, &data);
372 return adt7310_convert_temperature(chip, data, buf);
375 static IIO_DEVICE_ATTR(value, S_IRUGO, adt7310_show_value, NULL, 0);
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,
386 static const struct attribute_group adt7310_attribute_group = {
387 .attrs = adt7310_attributes,
390 static irqreturn_t adt7310_event_handler(int irq, void *private)
392 struct iio_dev *indio_dev = private;
393 struct adt7310_chip_info *chip = iio_priv(indio_dev);
394 s64 timestamp = iio_get_time_ns();
398 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
402 if (status & ADT7310_STAT_T_HIGH)
403 iio_push_event(indio_dev,
404 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
408 if (status & ADT7310_STAT_T_LOW)
409 iio_push_event(indio_dev,
410 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
414 if (status & ADT7310_STAT_T_CRIT)
415 iio_push_event(indio_dev,
416 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
423 static ssize_t adt7310_show_event_mode(struct device *dev,
424 struct device_attribute *attr,
427 struct iio_dev *dev_info = dev_get_drvdata(dev);
428 struct adt7310_chip_info *chip = iio_priv(dev_info);
431 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
435 if (chip->config & ADT7310_EVENT_MODE)
436 return sprintf(buf, "interrupt\n");
438 return sprintf(buf, "comparator\n");
441 static ssize_t adt7310_set_event_mode(struct device *dev,
442 struct device_attribute *attr,
446 struct iio_dev *dev_info = dev_get_drvdata(dev);
447 struct adt7310_chip_info *chip = iio_priv(dev_info);
451 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
455 config = chip->config &= ~ADT7310_EVENT_MODE;
456 if (strcmp(buf, "comparator") != 0)
457 config |= ADT7310_EVENT_MODE;
459 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
463 chip->config = config;
468 static ssize_t adt7310_show_available_event_modes(struct device *dev,
469 struct device_attribute *attr,
472 return sprintf(buf, "comparator\ninterrupt\n");
475 static ssize_t adt7310_show_fault_queue(struct device *dev,
476 struct device_attribute *attr,
479 struct iio_dev *dev_info = dev_get_drvdata(dev);
480 struct adt7310_chip_info *chip = iio_priv(dev_info);
483 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
487 return sprintf(buf, "%d\n", chip->config & ADT7310_FAULT_QUEUE_MASK);
490 static ssize_t adt7310_set_fault_queue(struct device *dev,
491 struct device_attribute *attr,
495 struct iio_dev *dev_info = dev_get_drvdata(dev);
496 struct adt7310_chip_info *chip = iio_priv(dev_info);
501 ret = strict_strtoul(buf, 10, &data);
505 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
509 config = chip->config & ~ADT7310_FAULT_QUEUE_MASK;
511 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
515 chip->config = config;
520 static inline ssize_t adt7310_show_t_bound(struct device *dev,
521 struct device_attribute *attr,
525 struct iio_dev *dev_info = dev_get_drvdata(dev);
526 struct adt7310_chip_info *chip = iio_priv(dev_info);
530 ret = adt7310_spi_read_word(chip, bound_reg, &data);
534 return adt7310_convert_temperature(chip, data, buf);
537 static inline ssize_t adt7310_set_t_bound(struct device *dev,
538 struct device_attribute *attr,
543 struct iio_dev *dev_info = dev_get_drvdata(dev);
544 struct adt7310_chip_info *chip = iio_priv(dev_info);
550 pos = strchr(buf, '.');
552 ret = strict_strtol(buf, 10, &tmp1);
554 if (ret || tmp1 > 127 || tmp1 < -128)
560 if (chip->config & ADT7310_RESOLUTION) {
561 if (len > ADT7310_T16_VALUE_FLOAT_OFFSET)
562 len = ADT7310_T16_VALUE_FLOAT_OFFSET;
564 ret = strict_strtol(pos, 10, &tmp2);
567 tmp2 = (tmp2 / 78125) * 78125;
569 if (len > ADT7310_T13_VALUE_FLOAT_OFFSET)
570 len = ADT7310_T13_VALUE_FLOAT_OFFSET;
572 ret = strict_strtol(pos, 10, &tmp2);
575 tmp2 = (tmp2 / 625) * 625;
584 if (chip->config & ADT7310_RESOLUTION) {
585 data = (data << ADT7310_T16_VALUE_FLOAT_OFFSET) |
586 (tmp2 & ADT7310_T16_VALUE_FLOAT_MASK);
589 /* convert positive value to supplyment */
590 data = (u16)((ADT7310_T16_VALUE_SIGN << 1) - (u32)data);
592 data = (data << ADT7310_T13_VALUE_FLOAT_OFFSET) |
593 (tmp2 & ADT7310_T13_VALUE_FLOAT_MASK);
596 /* convert positive value to supplyment */
597 data = (ADT7310_T13_VALUE_SIGN << 1) - data;
598 data <<= ADT7310_T13_VALUE_OFFSET;
601 ret = adt7310_spi_write_word(chip, bound_reg, data);
608 static ssize_t adt7310_show_t_alarm_high(struct device *dev,
609 struct device_attribute *attr,
612 return adt7310_show_t_bound(dev, attr,
613 ADT7310_T_ALARM_HIGH, buf);
616 static inline ssize_t adt7310_set_t_alarm_high(struct device *dev,
617 struct device_attribute *attr,
621 return adt7310_set_t_bound(dev, attr,
622 ADT7310_T_ALARM_HIGH, buf, len);
625 static ssize_t adt7310_show_t_alarm_low(struct device *dev,
626 struct device_attribute *attr,
629 return adt7310_show_t_bound(dev, attr,
630 ADT7310_T_ALARM_LOW, buf);
633 static inline ssize_t adt7310_set_t_alarm_low(struct device *dev,
634 struct device_attribute *attr,
638 return adt7310_set_t_bound(dev, attr,
639 ADT7310_T_ALARM_LOW, buf, len);
642 static ssize_t adt7310_show_t_crit(struct device *dev,
643 struct device_attribute *attr,
646 return adt7310_show_t_bound(dev, attr,
647 ADT7310_T_CRIT, buf);
650 static inline ssize_t adt7310_set_t_crit(struct device *dev,
651 struct device_attribute *attr,
655 return adt7310_set_t_bound(dev, attr,
656 ADT7310_T_CRIT, buf, len);
659 static ssize_t adt7310_show_t_hyst(struct device *dev,
660 struct device_attribute *attr,
663 struct iio_dev *dev_info = dev_get_drvdata(dev);
664 struct adt7310_chip_info *chip = iio_priv(dev_info);
668 ret = adt7310_spi_read_byte(chip, ADT7310_T_HYST, &t_hyst);
672 return sprintf(buf, "%d\n", t_hyst & ADT7310_T_HYST_MASK);
675 static inline ssize_t adt7310_set_t_hyst(struct device *dev,
676 struct device_attribute *attr,
680 struct iio_dev *dev_info = dev_get_drvdata(dev);
681 struct adt7310_chip_info *chip = iio_priv(dev_info);
686 ret = strict_strtol(buf, 10, &data);
688 if (ret || data > ADT7310_T_HYST_MASK)
693 ret = adt7310_spi_write_byte(chip, ADT7310_T_HYST, t_hyst);
700 static IIO_DEVICE_ATTR(event_mode,
702 adt7310_show_event_mode, adt7310_set_event_mode, 0);
703 static IIO_DEVICE_ATTR(available_event_modes,
705 adt7310_show_available_event_modes, NULL, 0);
706 static IIO_DEVICE_ATTR(fault_queue,
708 adt7310_show_fault_queue, adt7310_set_fault_queue, 0);
709 static IIO_DEVICE_ATTR(t_alarm_high,
711 adt7310_show_t_alarm_high, adt7310_set_t_alarm_high, 0);
712 static IIO_DEVICE_ATTR(t_alarm_low,
714 adt7310_show_t_alarm_low, adt7310_set_t_alarm_low, 0);
715 static IIO_DEVICE_ATTR(t_crit,
717 adt7310_show_t_crit, adt7310_set_t_crit, 0);
718 static IIO_DEVICE_ATTR(t_hyst,
720 adt7310_show_t_hyst, adt7310_set_t_hyst, 0);
722 static struct attribute *adt7310_event_int_attributes[] = {
723 &iio_dev_attr_event_mode.dev_attr.attr,
724 &iio_dev_attr_available_event_modes.dev_attr.attr,
725 &iio_dev_attr_fault_queue.dev_attr.attr,
726 &iio_dev_attr_t_alarm_high.dev_attr.attr,
727 &iio_dev_attr_t_alarm_low.dev_attr.attr,
728 &iio_dev_attr_t_hyst.dev_attr.attr,
732 static struct attribute *adt7310_event_ct_attributes[] = {
733 &iio_dev_attr_event_mode.dev_attr.attr,
734 &iio_dev_attr_available_event_modes.dev_attr.attr,
735 &iio_dev_attr_fault_queue.dev_attr.attr,
736 &iio_dev_attr_t_crit.dev_attr.attr,
737 &iio_dev_attr_t_hyst.dev_attr.attr,
741 static struct attribute_group adt7310_event_attribute_group[ADT7310_IRQS] = {
743 .attrs = adt7310_event_int_attributes,
746 .attrs = adt7310_event_ct_attributes,
751 static const struct iio_info adt7310_info = {
752 .attrs = &adt7310_attribute_group,
753 .event_attrs = adt7310_event_attribute_group,
754 .driver_module = THIS_MODULE,
758 * device probe and remove
761 static int __devinit adt7310_probe(struct spi_device *spi_dev)
763 struct adt7310_chip_info *chip;
764 struct iio_dev *indio_dev;
766 unsigned long *adt7310_platform_data = spi_dev->dev.platform_data;
767 unsigned long irq_flags;
769 indio_dev = iio_allocate_device(sizeof(*chip));
770 if (indio_dev == NULL) {
774 chip = iio_priv(indio_dev);
775 /* this is only used for device removal purposes */
776 dev_set_drvdata(&spi_dev->dev, indio_dev);
778 chip->spi_dev = spi_dev;
780 indio_dev->dev.parent = &spi_dev->dev;
781 indio_dev->name = spi_get_device_id(spi_dev)->name;
782 indio_dev->info = &adt7310_info;
783 indio_dev->modes = INDIO_DIRECT_MODE;
785 ret = iio_device_register(indio_dev);
789 /* CT critcal temperature event. line 0 */
791 if (adt7310_platform_data[2])
792 irq_flags = adt7310_platform_data[2];
794 irq_flags = IRQF_TRIGGER_LOW;
795 ret = request_threaded_irq(spi_dev->irq,
797 &adt7310_event_handler,
802 goto error_unreg_dev;
805 /* INT bound temperature alarm event. line 1 */
806 if (adt7310_platform_data[0]) {
807 ret = request_threaded_irq(adt7310_platform_data[0],
809 &adt7310_event_handler,
810 adt7310_platform_data[1],
814 goto error_unreg_ct_irq;
817 if (spi_dev->irq && adt7310_platform_data[0]) {
818 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
821 goto error_unreg_int_irq;
824 /* set irq polarity low level */
825 chip->config &= ~ADT7310_CT_POLARITY;
827 if (adt7310_platform_data[1] & IRQF_TRIGGER_HIGH)
828 chip->config |= ADT7310_INT_POLARITY;
830 chip->config &= ~ADT7310_INT_POLARITY;
832 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, chip->config);
835 goto error_unreg_int_irq;
839 dev_info(&spi_dev->dev, "%s temperature sensor registered.\n",
845 free_irq(adt7310_platform_data[0], indio_dev);
847 free_irq(spi_dev->irq, indio_dev);
849 iio_device_unregister(indio_dev);
851 iio_free_device(indio_dev);
856 static int __devexit adt7310_remove(struct spi_device *spi_dev)
858 struct iio_dev *indio_dev = dev_get_drvdata(&spi_dev->dev);
859 unsigned long *adt7310_platform_data = spi_dev->dev.platform_data;
861 dev_set_drvdata(&spi_dev->dev, NULL);
862 if (adt7310_platform_data[0])
863 free_irq(adt7310_platform_data[0], indio_dev);
865 free_irq(spi_dev->irq, indio_dev);
866 iio_device_unregister(indio_dev);
871 static const struct spi_device_id adt7310_id[] = {
876 MODULE_DEVICE_TABLE(spi, adt7310_id);
878 static struct spi_driver adt7310_driver = {
881 .bus = &spi_bus_type,
882 .owner = THIS_MODULE,
884 .probe = adt7310_probe,
885 .remove = __devexit_p(adt7310_remove),
886 .id_table = adt7310_id,
889 static __init int adt7310_init(void)
891 return spi_register_driver(&adt7310_driver);
894 static __exit void adt7310_exit(void)
896 spi_unregister_driver(&adt7310_driver);
899 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
900 MODULE_DESCRIPTION("Analog Devices ADT7310 digital"
901 " temperature sensor driver");
902 MODULE_LICENSE("GPL v2");
904 module_init(adt7310_init);
905 module_exit(adt7310_exit);