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>
21 * ADT7310 registers definition
24 #define ADT7310_STATUS 0
25 #define ADT7310_CONFIG 1
26 #define ADT7310_TEMPERATURE 2
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
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
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
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;
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;
96 command |= ADT7310_CMD_READ;
97 ret = spi_write(spi_dev, &command, sizeof(command));
99 dev_err(&spi_dev->dev, "SPI write command error\n");
103 ret = spi_read(spi_dev, (u8 *)data, sizeof(*data));
105 dev_err(&spi_dev->dev, "SPI read word error\n");
109 *data = be16_to_cpu(*data);
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;
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);
126 dev_err(&spi_dev->dev, "SPI write word error\n");
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;
139 command |= ADT7310_CMD_READ;
140 ret = spi_write(spi_dev, &command, sizeof(command));
142 dev_err(&spi_dev->dev, "SPI write command error\n");
146 ret = spi_read(spi_dev, data, sizeof(*data));
148 dev_err(&spi_dev->dev, "SPI read byte error\n");
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;
161 buf[0] = (reg << ADT7310_CMD_REG_OFFSET) & ADT7310_CMD_REG_MASK;
164 ret = spi_write(spi_dev, buf, 2);
166 dev_err(&spi_dev->dev, "SPI write byte error\n");
173 static ssize_t adt7310_show_mode(struct device *dev,
174 struct device_attribute *attr,
177 struct iio_dev *dev_info = dev_get_drvdata(dev);
178 struct adt7310_chip_info *chip = iio_priv(dev_info);
181 config = chip->config & ADT7310_MODE_MASK;
185 return sprintf(buf, "power-down\n");
186 case ADT7310_ONESHOT:
187 return sprintf(buf, "one-shot\n");
189 return sprintf(buf, "sps\n");
191 return sprintf(buf, "full\n");
195 static ssize_t adt7310_store_mode(struct device *dev,
196 struct device_attribute *attr,
200 struct iio_dev *dev_info = dev_get_drvdata(dev);
201 struct adt7310_chip_info *chip = iio_priv(dev_info);
205 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
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);
221 chip->config = config;
226 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
231 static ssize_t adt7310_show_available_modes(struct device *dev,
232 struct device_attribute *attr,
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,
244 struct iio_dev *dev_info = dev_get_drvdata(dev);
245 struct adt7310_chip_info *chip = iio_priv(dev_info);
249 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
253 if (chip->config & ADT7310_RESOLUTION)
258 return sprintf(buf, "%d bits\n", bits);
261 static ssize_t adt7310_store_resolution(struct device *dev,
262 struct device_attribute *attr,
266 struct iio_dev *dev_info = dev_get_drvdata(dev);
267 struct adt7310_chip_info *chip = iio_priv(dev_info);
272 ret = strict_strtoul(buf, 10, &data);
276 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
280 config = chip->config & (~ADT7310_RESOLUTION);
282 config |= ADT7310_RESOLUTION;
284 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
288 chip->config = config;
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,
302 struct iio_dev *dev_info = dev_get_drvdata(dev);
303 struct adt7310_chip_info *chip = iio_priv(dev_info);
307 ret = adt7310_spi_read_byte(chip, ADT7310_ID, &id);
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,
321 static ssize_t adt7310_convert_temperature(struct adt7310_chip_info *chip,
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);
332 return sprintf(buf, "%c%d.%.7d\n", sign,
333 (data >> ADT7310_T16_VALUE_FLOAT_OFFSET),
334 (data & ADT7310_T16_VALUE_FLOAT_MASK) * 78125);
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;
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,
352 struct iio_dev *dev_info = dev_get_drvdata(dev);
353 struct adt7310_chip_info *chip = iio_priv(dev_info);
359 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
365 } while (status & ADT7310_STAT_NOT_RDY);
367 ret = adt7310_spi_read_word(chip, ADT7310_TEMPERATURE, &data);
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,
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();
397 ret = adt7310_spi_read_byte(chip, ADT7310_STATUS, &status);
401 if (status & ADT7310_STAT_T_HIGH)
402 iio_push_event(indio_dev, 0,
403 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
407 if (status & ADT7310_STAT_T_LOW)
408 iio_push_event(indio_dev, 0,
409 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
413 if (status & ADT7310_STAT_T_CRIT)
414 iio_push_event(indio_dev, 0,
415 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
422 static ssize_t adt7310_show_event_mode(struct device *dev,
423 struct device_attribute *attr,
426 struct iio_dev *dev_info = dev_get_drvdata(dev);
427 struct adt7310_chip_info *chip = iio_priv(dev_info);
430 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
434 if (chip->config & ADT7310_EVENT_MODE)
435 return sprintf(buf, "interrupt\n");
437 return sprintf(buf, "comparator\n");
440 static ssize_t adt7310_set_event_mode(struct device *dev,
441 struct device_attribute *attr,
445 struct iio_dev *dev_info = dev_get_drvdata(dev);
446 struct adt7310_chip_info *chip = iio_priv(dev_info);
450 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
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);
462 chip->config = config;
467 static ssize_t adt7310_show_available_event_modes(struct device *dev,
468 struct device_attribute *attr,
471 return sprintf(buf, "comparator\ninterrupt\n");
474 static ssize_t adt7310_show_fault_queue(struct device *dev,
475 struct device_attribute *attr,
478 struct iio_dev *dev_info = dev_get_drvdata(dev);
479 struct adt7310_chip_info *chip = iio_priv(dev_info);
482 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
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,
494 struct iio_dev *dev_info = dev_get_drvdata(dev);
495 struct adt7310_chip_info *chip = iio_priv(dev_info);
500 ret = strict_strtoul(buf, 10, &data);
504 ret = adt7310_spi_read_byte(chip, ADT7310_CONFIG, &chip->config);
508 config = chip->config & ~ADT7310_FAULT_QUEUE_MASK;
510 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, config);
514 chip->config = config;
519 static inline ssize_t adt7310_show_t_bound(struct device *dev,
520 struct device_attribute *attr,
524 struct iio_dev *dev_info = dev_get_drvdata(dev);
525 struct adt7310_chip_info *chip = iio_priv(dev_info);
529 ret = adt7310_spi_read_word(chip, bound_reg, &data);
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,
542 struct iio_dev *dev_info = dev_get_drvdata(dev);
543 struct adt7310_chip_info *chip = iio_priv(dev_info);
549 pos = strchr(buf, '.');
551 ret = strict_strtol(buf, 10, &tmp1);
553 if (ret || tmp1 > 127 || tmp1 < -128)
559 if (chip->config & ADT7310_RESOLUTION) {
560 if (len > ADT7310_T16_VALUE_FLOAT_OFFSET)
561 len = ADT7310_T16_VALUE_FLOAT_OFFSET;
563 ret = strict_strtol(pos, 10, &tmp2);
566 tmp2 = (tmp2 / 78125) * 78125;
568 if (len > ADT7310_T13_VALUE_FLOAT_OFFSET)
569 len = ADT7310_T13_VALUE_FLOAT_OFFSET;
571 ret = strict_strtol(pos, 10, &tmp2);
574 tmp2 = (tmp2 / 625) * 625;
583 if (chip->config & ADT7310_RESOLUTION) {
584 data = (data << ADT7310_T16_VALUE_FLOAT_OFFSET) |
585 (tmp2 & ADT7310_T16_VALUE_FLOAT_MASK);
588 /* convert positive value to supplyment */
589 data = (u16)((ADT7310_T16_VALUE_SIGN << 1) - (u32)data);
591 data = (data << ADT7310_T13_VALUE_FLOAT_OFFSET) |
592 (tmp2 & ADT7310_T13_VALUE_FLOAT_MASK);
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);
607 static ssize_t adt7310_show_t_alarm_high(struct device *dev,
608 struct device_attribute *attr,
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,
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,
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,
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,
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,
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,
662 struct iio_dev *dev_info = dev_get_drvdata(dev);
663 struct adt7310_chip_info *chip = iio_priv(dev_info);
667 ret = adt7310_spi_read_byte(chip, ADT7310_T_HYST, &t_hyst);
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,
679 struct iio_dev *dev_info = dev_get_drvdata(dev);
680 struct adt7310_chip_info *chip = iio_priv(dev_info);
685 ret = strict_strtol(buf, 10, &data);
687 if (ret || data > ADT7310_T_HYST_MASK)
692 ret = adt7310_spi_write_byte(chip, ADT7310_T_HYST, t_hyst);
699 static IIO_DEVICE_ATTR(event_mode,
701 adt7310_show_event_mode, adt7310_set_event_mode, 0);
702 static IIO_DEVICE_ATTR(available_event_modes,
704 adt7310_show_available_event_modes, NULL, 0);
705 static IIO_DEVICE_ATTR(fault_queue,
707 adt7310_show_fault_queue, adt7310_set_fault_queue, 0);
708 static IIO_DEVICE_ATTR(t_alarm_high,
710 adt7310_show_t_alarm_high, adt7310_set_t_alarm_high, 0);
711 static IIO_DEVICE_ATTR(t_alarm_low,
713 adt7310_show_t_alarm_low, adt7310_set_t_alarm_low, 0);
714 static IIO_DEVICE_ATTR(t_crit,
716 adt7310_show_t_crit, adt7310_set_t_crit, 0);
717 static IIO_DEVICE_ATTR(t_hyst,
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,
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,
740 static struct attribute_group adt7310_event_attribute_group[ADT7310_IRQS] = {
742 .attrs = adt7310_event_int_attributes,
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;
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) {
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);
787 /* CT critcal temperature event. line 0 */
789 if (adt7310_platform_data[2])
790 irq_flags = adt7310_platform_data[2];
792 irq_flags = IRQF_TRIGGER_LOW;
793 ret = request_threaded_irq(spi_dev->irq,
795 &adt7310_event_handler,
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],
807 &adt7310_event_handler,
808 adt7310_platform_data[1],
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);
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;
828 chip->config &= ~ADT7310_INT_POLARITY;
830 ret = adt7310_spi_write_byte(chip, ADT7310_CONFIG, chip->config);
833 goto error_unreg_int_irq;
837 dev_info(&spi_dev->dev, "%s temperature sensor registered.\n",
843 free_irq(adt7310_platform_data[0], indio_dev);
845 free_irq(spi_dev->irq, indio_dev);
847 iio_device_unregister(indio_dev);
849 iio_free_device(indio_dev);
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);
863 free_irq(spi_dev->irq, indio_dev);
864 iio_device_unregister(indio_dev);
865 iio_free_device(indio_dev);
870 static const struct spi_device_id adt7310_id[] = {
875 MODULE_DEVICE_TABLE(spi, adt7310_id);
877 static struct spi_driver adt7310_driver = {
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);