Lines Matching +full:spi +full:- +full:3 +full:wire
1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
18 #include <linux/spi/spi.h>
42 (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
44 (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
45 #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3)
75 #define LTC2983_RTD_4_WIRE_MASK BIT(3)
79 #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2)
80 #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2)
190 struct spi_device *spi; member
272 * instance a resolution of 2^-10 means we have 10 fractional bits.
286 s64 __res = -(s32)val; in __convert_to_raw_sign()
290 return (u32)-__res; in __convert_to_raw_sign()
297 const struct device *dev = &st->spi->dev; in __ltc2983_fault_handler()
301 return -EIO; in __ltc2983_fault_handler()
314 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan); in __ltc2983_chan_assign_common()
317 chan_val |= LTC2983_CHAN_TYPE(sensor->type); in __ltc2983_chan_assign_common()
318 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg, in __ltc2983_chan_assign_common()
321 return regmap_bulk_write(st->regmap, reg, &__chan_val, in __ltc2983_chan_assign_common()
330 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ : in __ltc2983_chan_custom_sensor_assign()
332 const struct device *dev = &st->spi->dev; in __ltc2983_chan_custom_sensor_assign()
334 * custom->size holds the raw size of the table. However, when in __ltc2983_chan_custom_sensor_assign()
339 const u8 len = custom->is_steinhart ? 0 : in __ltc2983_chan_custom_sensor_assign()
340 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1; in __ltc2983_chan_custom_sensor_assign()
345 if (custom->offset < 0) { in __ltc2983_chan_custom_sensor_assign()
352 if (st->custom_table_size + custom->size > in __ltc2983_chan_custom_sensor_assign()
353 (LTC2983_CUST_SENS_TBL_END_REG - in __ltc2983_chan_custom_sensor_assign()
357 st->custom_table_size, in __ltc2983_chan_custom_sensor_assign()
358 custom->size); in __ltc2983_chan_custom_sensor_assign()
359 return -EINVAL; in __ltc2983_chan_custom_sensor_assign()
362 custom->offset = st->custom_table_size / in __ltc2983_chan_custom_sensor_assign()
364 st->custom_table_size += custom->size; in __ltc2983_chan_custom_sensor_assign()
367 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG; in __ltc2983_chan_custom_sensor_assign()
370 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset); in __ltc2983_chan_custom_sensor_assign()
372 reg, custom->offset, in __ltc2983_chan_custom_sensor_assign()
373 custom->size); in __ltc2983_chan_custom_sensor_assign()
375 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size); in __ltc2983_chan_custom_sensor_assign()
388 struct device *dev = &st->spi->dev; in __ltc2983_custom_sensor_new()
393 const u8 n_size = is_steinhart ? 4 : 3; in __ltc2983_custom_sensor_new()
400 return ERR_PTR(-EINVAL); in __ltc2983_custom_sensor_new()
405 return ERR_PTR(-ENOMEM); in __ltc2983_custom_sensor_new()
407 new_custom->size = n_entries * n_size; in __ltc2983_custom_sensor_new()
409 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) { in __ltc2983_custom_sensor_new()
411 new_custom->size); in __ltc2983_custom_sensor_new()
412 return ERR_PTR(-EINVAL); in __ltc2983_custom_sensor_new()
415 if (st->custom_table_size + new_custom->size > in __ltc2983_custom_sensor_new()
416 (LTC2983_CUST_SENS_TBL_END_REG - in __ltc2983_custom_sensor_new()
419 st->custom_table_size, new_custom->size); in __ltc2983_custom_sensor_new()
420 return ERR_PTR(-EINVAL); in __ltc2983_custom_sensor_new()
424 new_custom->table = devm_kzalloc(dev, new_custom->size, GFP_KERNEL); in __ltc2983_custom_sensor_new()
425 if (!new_custom->table) in __ltc2983_custom_sensor_new()
426 return ERR_PTR(-ENOMEM); in __ltc2983_custom_sensor_new()
454 new_custom->table[tbl++] = in __ltc2983_custom_sensor_new()
455 temp >> (8 * (n_size - j - 1)); in __ltc2983_custom_sensor_new()
458 new_custom->is_steinhart = is_steinhart; in __ltc2983_custom_sensor_new()
463 * sure that sensor_addr - 0x250(start address) is a multiple of 4 in __ltc2983_custom_sensor_new()
466 * also a multiple of 6, we guarantee that the first non-steinhart in __ltc2983_custom_sensor_new()
471 new_custom->offset = st->custom_table_size / in __ltc2983_custom_sensor_new()
473 st->custom_table_size += new_custom->size; in __ltc2983_custom_sensor_new()
476 new_custom->offset = -1; in __ltc2983_custom_sensor_new()
504 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan); in ltc2983_thermocouple_assign_chan()
505 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config); in ltc2983_thermocouple_assign_chan()
507 if (thermo->custom) { in ltc2983_thermocouple_assign_chan()
510 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom, in ltc2983_thermocouple_assign_chan()
524 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan); in ltc2983_rtd_assign_chan()
525 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config); in ltc2983_rtd_assign_chan()
526 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current); in ltc2983_rtd_assign_chan()
527 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve); in ltc2983_rtd_assign_chan()
529 if (rtd->custom) { in ltc2983_rtd_assign_chan()
532 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom, in ltc2983_rtd_assign_chan()
546 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan); in ltc2983_thermistor_assign_chan()
547 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config); in ltc2983_thermistor_assign_chan()
549 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current); in ltc2983_thermistor_assign_chan()
551 if (thermistor->custom) { in ltc2983_thermistor_assign_chan()
555 thermistor->custom, in ltc2983_thermistor_assign_chan()
569 chan_val = LTC2983_DIODE_CFG(diode->sensor_config); in ltc2983_diode_assign_chan()
570 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current); in ltc2983_diode_assign_chan()
571 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value); in ltc2983_diode_assign_chan()
582 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val); in ltc2983_r_sense_assign_chan()
593 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended); in ltc2983_adc_assign_chan()
608 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL); in ltc2983_thermocouple_new()
610 return ERR_PTR(-ENOMEM); in ltc2983_thermocouple_new()
612 if (of_property_read_bool(child, "adi,single-ended")) in ltc2983_thermocouple_new()
613 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1); in ltc2983_thermocouple_new()
615 ret = of_property_read_u32(child, "adi,sensor-oc-current-microamp", in ltc2983_thermocouple_new()
620 thermo->sensor_config |= in ltc2983_thermocouple_new()
624 thermo->sensor_config |= in ltc2983_thermocouple_new()
628 thermo->sensor_config |= in ltc2983_thermocouple_new()
632 thermo->sensor_config |= in ltc2983_thermocouple_new()
633 LTC2983_THERMOCOUPLE_OC_CURR(3); in ltc2983_thermocouple_new()
636 dev_err(&st->spi->dev, in ltc2983_thermocouple_new()
638 return ERR_PTR(-EINVAL); in ltc2983_thermocouple_new()
641 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1); in ltc2983_thermocouple_new()
644 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) && in ltc2983_thermocouple_new()
645 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_thermocouple_new()
646 dev_err(&st->spi->dev, in ltc2983_thermocouple_new()
648 sensor->chan); in ltc2983_thermocouple_new()
649 return ERR_PTR(-EINVAL); in ltc2983_thermocouple_new()
652 phandle = of_parse_phandle(child, "adi,cold-junction-handle", 0); in ltc2983_thermocouple_new()
657 &thermo->cold_junction_chan); in ltc2983_thermocouple_new()
663 dev_err(&st->spi->dev, "Property reg must be given\n"); in ltc2983_thermocouple_new()
665 return ERR_PTR(-EINVAL); in ltc2983_thermocouple_new()
670 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { in ltc2983_thermocouple_new()
671 const char *propname = "adi,custom-thermocouple"; in ltc2983_thermocouple_new()
673 thermo->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_thermocouple_new()
676 if (IS_ERR(thermo->custom)) { in ltc2983_thermocouple_new()
678 return ERR_CAST(thermo->custom); in ltc2983_thermocouple_new()
683 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; in ltc2983_thermocouple_new()
684 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; in ltc2983_thermocouple_new()
687 return &thermo->sensor; in ltc2983_thermocouple_new()
696 struct device *dev = &st->spi->dev; in ltc2983_rtd_new()
702 return ERR_PTR(-ENOMEM); in ltc2983_rtd_new()
704 phandle = of_parse_phandle(child, "adi,rsense-handle", 0); in ltc2983_rtd_new()
706 dev_err(dev, "Property adi,rsense-handle missing or invalid"); in ltc2983_rtd_new()
707 return ERR_PTR(-EINVAL); in ltc2983_rtd_new()
710 ret = of_property_read_u32(phandle, "reg", &rtd->r_sense_chan); in ltc2983_rtd_new()
716 ret = of_property_read_u32(child, "adi,number-of-wires", &n_wires); in ltc2983_rtd_new()
720 rtd->sensor_config = LTC2983_RTD_N_WIRES(0); in ltc2983_rtd_new()
722 case 3: in ltc2983_rtd_new()
723 rtd->sensor_config = LTC2983_RTD_N_WIRES(1); in ltc2983_rtd_new()
726 rtd->sensor_config = LTC2983_RTD_N_WIRES(2); in ltc2983_rtd_new()
730 rtd->sensor_config = LTC2983_RTD_N_WIRES(3); in ltc2983_rtd_new()
734 ret = -EINVAL; in ltc2983_rtd_new()
739 if (of_property_read_bool(child, "adi,rsense-share")) { in ltc2983_rtd_new()
741 if (of_property_read_bool(child, "adi,current-rotate")) { in ltc2983_rtd_new()
742 if (n_wires == 2 || n_wires == 3) { in ltc2983_rtd_new()
744 "Rotation not allowed for 2/3 Wire RTDs"); in ltc2983_rtd_new()
745 ret = -EINVAL; in ltc2983_rtd_new()
748 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); in ltc2983_rtd_new()
750 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1); in ltc2983_rtd_new()
755 * For 4wire RTD with rotation, the channel selection cannot be in ltc2983_rtd_new()
757 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be in ltc2983_rtd_new()
758 * <=1 since chanel - 1 and channel - 2 are used. in ltc2983_rtd_new()
760 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) { in ltc2983_rtd_new()
761 /* 4-wire */ in ltc2983_rtd_new()
765 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK) in ltc2983_rtd_new()
766 max = LTC2983_MAX_CHANNELS_NR - 1; in ltc2983_rtd_new()
768 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK) in ltc2983_rtd_new()
770 (rtd->r_sense_chan <= min)) { in ltc2983_rtd_new()
774 rtd->r_sense_chan); in ltc2983_rtd_new()
776 ret = -EINVAL; in ltc2983_rtd_new()
780 if (sensor->chan < min || sensor->chan > max) { in ltc2983_rtd_new()
782 sensor->chan); in ltc2983_rtd_new()
784 ret = -EINVAL; in ltc2983_rtd_new()
789 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_rtd_new()
790 dev_err(&st->spi->dev, in ltc2983_rtd_new()
791 "Invalid chann:%d for RTD", sensor->chan); in ltc2983_rtd_new()
793 ret = -EINVAL; in ltc2983_rtd_new()
799 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) { in ltc2983_rtd_new()
800 rtd->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_rtd_new()
801 "adi,custom-rtd", in ltc2983_rtd_new()
803 if (IS_ERR(rtd->custom)) { in ltc2983_rtd_new()
805 return ERR_CAST(rtd->custom); in ltc2983_rtd_new()
810 rtd->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_rtd_new()
811 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan; in ltc2983_rtd_new()
813 ret = of_property_read_u32(child, "adi,excitation-current-microamp", in ltc2983_rtd_new()
817 rtd->excitation_current = 1; in ltc2983_rtd_new()
821 rtd->excitation_current = 0x01; in ltc2983_rtd_new()
824 rtd->excitation_current = 0x02; in ltc2983_rtd_new()
827 rtd->excitation_current = 0x03; in ltc2983_rtd_new()
830 rtd->excitation_current = 0x04; in ltc2983_rtd_new()
833 rtd->excitation_current = 0x05; in ltc2983_rtd_new()
836 rtd->excitation_current = 0x06; in ltc2983_rtd_new()
839 rtd->excitation_current = 0x07; in ltc2983_rtd_new()
842 rtd->excitation_current = 0x08; in ltc2983_rtd_new()
845 dev_err(&st->spi->dev, in ltc2983_rtd_new()
848 ret = -EINVAL; in ltc2983_rtd_new()
853 of_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); in ltc2983_rtd_new()
856 return &rtd->sensor; in ltc2983_rtd_new()
868 struct device *dev = &st->spi->dev; in ltc2983_thermistor_new()
875 return ERR_PTR(-ENOMEM); in ltc2983_thermistor_new()
877 phandle = of_parse_phandle(child, "adi,rsense-handle", 0); in ltc2983_thermistor_new()
879 dev_err(dev, "Property adi,rsense-handle missing or invalid"); in ltc2983_thermistor_new()
880 return ERR_PTR(-EINVAL); in ltc2983_thermistor_new()
883 ret = of_property_read_u32(phandle, "reg", &thermistor->r_sense_chan); in ltc2983_thermistor_new()
889 if (of_property_read_bool(child, "adi,single-ended")) { in ltc2983_thermistor_new()
890 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1); in ltc2983_thermistor_new()
891 } else if (of_property_read_bool(child, "adi,rsense-share")) { in ltc2983_thermistor_new()
893 if (of_property_read_bool(child, "adi,current-rotate")) in ltc2983_thermistor_new()
894 thermistor->sensor_config = in ltc2983_thermistor_new()
897 thermistor->sensor_config = in ltc2983_thermistor_new()
901 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) && in ltc2983_thermistor_new()
902 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_thermistor_new()
903 dev_err(&st->spi->dev, in ltc2983_thermistor_new()
905 sensor->chan); in ltc2983_thermistor_new()
906 ret = -EINVAL; in ltc2983_thermistor_new()
911 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) { in ltc2983_thermistor_new()
915 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) { in ltc2983_thermistor_new()
917 propname = "adi,custom-steinhart"; in ltc2983_thermistor_new()
919 propname = "adi,custom-thermistor"; in ltc2983_thermistor_new()
922 thermistor->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_thermistor_new()
926 if (IS_ERR(thermistor->custom)) { in ltc2983_thermistor_new()
928 return ERR_CAST(thermistor->custom); in ltc2983_thermistor_new()
932 thermistor->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_thermistor_new()
933 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan; in ltc2983_thermistor_new()
935 ret = of_property_read_u32(child, "adi,excitation-current-nanoamp", in ltc2983_thermistor_new()
939 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) in ltc2983_thermistor_new()
941 thermistor->excitation_current = 0x03; in ltc2983_thermistor_new()
943 /* default to auto-range */ in ltc2983_thermistor_new()
944 thermistor->excitation_current = 0x0c; in ltc2983_thermistor_new()
949 if (sensor->type >= in ltc2983_thermistor_new()
951 dev_err(&st->spi->dev, in ltc2983_thermistor_new()
953 ret = -EINVAL; in ltc2983_thermistor_new()
956 thermistor->excitation_current = 0x0c; in ltc2983_thermistor_new()
959 thermistor->excitation_current = 0x01; in ltc2983_thermistor_new()
962 thermistor->excitation_current = 0x02; in ltc2983_thermistor_new()
965 thermistor->excitation_current = 0x03; in ltc2983_thermistor_new()
968 thermistor->excitation_current = 0x04; in ltc2983_thermistor_new()
971 thermistor->excitation_current = 0x05; in ltc2983_thermistor_new()
974 thermistor->excitation_current = 0x06; in ltc2983_thermistor_new()
977 thermistor->excitation_current = 0x07; in ltc2983_thermistor_new()
980 thermistor->excitation_current = 0x08; in ltc2983_thermistor_new()
983 thermistor->excitation_current = 0x09; in ltc2983_thermistor_new()
986 thermistor->excitation_current = 0x0a; in ltc2983_thermistor_new()
989 thermistor->excitation_current = 0x0b; in ltc2983_thermistor_new()
992 dev_err(&st->spi->dev, in ltc2983_thermistor_new()
995 ret = -EINVAL; in ltc2983_thermistor_new()
1001 return &thermistor->sensor; in ltc2983_thermistor_new()
1016 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL); in ltc2983_diode_new()
1018 return ERR_PTR(-ENOMEM); in ltc2983_diode_new()
1020 if (of_property_read_bool(child, "adi,single-ended")) in ltc2983_diode_new()
1021 diode->sensor_config = LTC2983_DIODE_SGL(1); in ltc2983_diode_new()
1023 if (of_property_read_bool(child, "adi,three-conversion-cycles")) in ltc2983_diode_new()
1024 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1); in ltc2983_diode_new()
1026 if (of_property_read_bool(child, "adi,average-on")) in ltc2983_diode_new()
1027 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1); in ltc2983_diode_new()
1030 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) && in ltc2983_diode_new()
1031 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_diode_new()
1032 dev_err(&st->spi->dev, in ltc2983_diode_new()
1034 sensor->chan); in ltc2983_diode_new()
1035 return ERR_PTR(-EINVAL); in ltc2983_diode_new()
1038 diode->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_diode_new()
1039 diode->sensor.assign_chan = ltc2983_diode_assign_chan; in ltc2983_diode_new()
1041 ret = of_property_read_u32(child, "adi,excitation-current-microamp", in ltc2983_diode_new()
1046 diode->excitation_current = 0x00; in ltc2983_diode_new()
1049 diode->excitation_current = 0x01; in ltc2983_diode_new()
1052 diode->excitation_current = 0x02; in ltc2983_diode_new()
1055 diode->excitation_current = 0x03; in ltc2983_diode_new()
1058 dev_err(&st->spi->dev, in ltc2983_diode_new()
1061 return ERR_PTR(-EINVAL); in ltc2983_diode_new()
1065 of_property_read_u32(child, "adi,ideal-factor-value", &temp); in ltc2983_diode_new()
1068 diode->ideal_factor_value = __convert_to_raw(temp, 1048576); in ltc2983_diode_new()
1070 return &diode->sensor; in ltc2983_diode_new()
1081 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL); in ltc2983_r_sense_new()
1083 return ERR_PTR(-ENOMEM); in ltc2983_r_sense_new()
1086 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_r_sense_new()
1087 dev_err(&st->spi->dev, "Invalid chann:%d for r_sense", in ltc2983_r_sense_new()
1088 sensor->chan); in ltc2983_r_sense_new()
1089 return ERR_PTR(-EINVAL); in ltc2983_r_sense_new()
1092 ret = of_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp); in ltc2983_r_sense_new()
1094 dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n"); in ltc2983_r_sense_new()
1095 return ERR_PTR(-EINVAL); in ltc2983_r_sense_new()
1098 * Times 1000 because we have milli-ohms and __convert_to_raw in ltc2983_r_sense_new()
1103 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024); in ltc2983_r_sense_new()
1106 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan; in ltc2983_r_sense_new()
1108 return &rsense->sensor; in ltc2983_r_sense_new()
1117 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL); in ltc2983_adc_new()
1119 return ERR_PTR(-ENOMEM); in ltc2983_adc_new()
1121 if (of_property_read_bool(child, "adi,single-ended")) in ltc2983_adc_new()
1122 adc->single_ended = true; in ltc2983_adc_new()
1124 if (!adc->single_ended && in ltc2983_adc_new()
1125 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_adc_new()
1126 dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n", in ltc2983_adc_new()
1127 sensor->chan); in ltc2983_adc_new()
1128 return ERR_PTR(-EINVAL); in ltc2983_adc_new()
1131 adc->sensor.assign_chan = ltc2983_adc_assign_chan; in ltc2983_adc_new()
1132 adc->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_adc_new()
1134 return &adc->sensor; in ltc2983_adc_new()
1145 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan); in ltc2983_chan_read()
1146 dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n", in ltc2983_chan_read()
1147 sensor->chan, start_conversion); in ltc2983_chan_read()
1149 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion); in ltc2983_chan_read()
1153 reinit_completion(&st->completion); in ltc2983_chan_read()
1157 * Depending on the sensor configuration, there are 2/3 conversions in ltc2983_chan_read()
1160 time = wait_for_completion_timeout(&st->completion, in ltc2983_chan_read()
1163 dev_warn(&st->spi->dev, "Conversion timed out\n"); in ltc2983_chan_read()
1164 return -ETIMEDOUT; in ltc2983_chan_read()
1168 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan), in ltc2983_chan_read()
1169 &st->temp, sizeof(st->temp)); in ltc2983_chan_read()
1173 *val = __be32_to_cpu(st->temp); in ltc2983_chan_read()
1176 dev_err(&st->spi->dev, "Invalid conversion detected\n"); in ltc2983_chan_read()
1177 return -EIO; in ltc2983_chan_read()
1180 ret = sensor->fault_handler(st, *val); in ltc2983_chan_read()
1196 if (chan->address >= st->num_channels) { in ltc2983_read_raw()
1197 dev_err(&st->spi->dev, "Invalid chan address:%ld", in ltc2983_read_raw()
1198 chan->address); in ltc2983_read_raw()
1199 return -EINVAL; in ltc2983_read_raw()
1204 mutex_lock(&st->lock); in ltc2983_read_raw()
1205 ret = ltc2983_chan_read(st, st->sensors[chan->address], val); in ltc2983_read_raw()
1206 mutex_unlock(&st->lock); in ltc2983_read_raw()
1209 switch (chan->type) { in ltc2983_read_raw()
1223 return -EINVAL; in ltc2983_read_raw()
1227 return -EINVAL; in ltc2983_read_raw()
1238 return regmap_read(st->regmap, reg, readval); in ltc2983_reg_access()
1240 return regmap_write(st->regmap, reg, writeval); in ltc2983_reg_access()
1247 complete(&st->completion); in ltc2983_irq_handler()
1266 struct device *dev = &st->spi->dev; in ltc2983_parse_dt()
1269 of_property_read_u32(dev->of_node, "adi,mux-delay-config-us", in ltc2983_parse_dt()
1270 &st->mux_delay_config); in ltc2983_parse_dt()
1272 of_property_read_u32(dev->of_node, "adi,filter-notch-freq", in ltc2983_parse_dt()
1273 &st->filter_notch_freq); in ltc2983_parse_dt()
1275 st->num_channels = of_get_available_child_count(dev->of_node); in ltc2983_parse_dt()
1276 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors), in ltc2983_parse_dt()
1278 if (!st->sensors) in ltc2983_parse_dt()
1279 return -ENOMEM; in ltc2983_parse_dt()
1281 st->iio_channels = st->num_channels; in ltc2983_parse_dt()
1282 for_each_available_child_of_node(dev->of_node, child) { in ltc2983_parse_dt()
1294 ret = -EINVAL; in ltc2983_parse_dt()
1299 ret = -EINVAL; in ltc2983_parse_dt()
1304 ret = of_property_read_u32(child, "adi,sensor-type", in ltc2983_parse_dt()
1308 "adi,sensor-type property must given for child nodes\n"); in ltc2983_parse_dt()
1318 st->sensors[chan] = ltc2983_thermocouple_new(child, st, in ltc2983_parse_dt()
1322 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor); in ltc2983_parse_dt()
1325 st->sensors[chan] = ltc2983_thermistor_new(child, st, in ltc2983_parse_dt()
1328 st->sensors[chan] = ltc2983_diode_new(child, st, in ltc2983_parse_dt()
1331 st->sensors[chan] = ltc2983_r_sense_new(child, st, in ltc2983_parse_dt()
1334 st->iio_channels--; in ltc2983_parse_dt()
1336 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor); in ltc2983_parse_dt()
1339 ret = -EINVAL; in ltc2983_parse_dt()
1343 if (IS_ERR(st->sensors[chan])) { in ltc2983_parse_dt()
1345 PTR_ERR(st->sensors[chan])); in ltc2983_parse_dt()
1346 ret = PTR_ERR(st->sensors[chan]); in ltc2983_parse_dt()
1350 st->sensors[chan]->chan = sensor.chan; in ltc2983_parse_dt()
1351 st->sensors[chan]->type = sensor.type; in ltc2983_parse_dt()
1370 time = wait_for_completion_timeout(&st->completion, in ltc2983_setup()
1374 dev_err(&st->spi->dev, "Device startup timed out\n"); in ltc2983_setup()
1375 return -ETIMEDOUT; in ltc2983_setup()
1378 st->iio_chan = devm_kzalloc(&st->spi->dev, in ltc2983_setup()
1379 st->iio_channels * sizeof(*st->iio_chan), in ltc2983_setup()
1382 if (!st->iio_chan) in ltc2983_setup()
1383 return -ENOMEM; in ltc2983_setup()
1385 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG, in ltc2983_setup()
1387 LTC2983_NOTCH_FREQ(st->filter_notch_freq)); in ltc2983_setup()
1391 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG, in ltc2983_setup()
1392 st->mux_delay_config); in ltc2983_setup()
1396 for (chan = 0; chan < st->num_channels; chan++) { in ltc2983_setup()
1399 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]); in ltc2983_setup()
1405 * re-configure the device channels. in ltc2983_setup()
1408 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR || in ltc2983_setup()
1413 if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) { in ltc2983_setup()
1425 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++, in ltc2983_setup()
1468 static int ltc2983_probe(struct spi_device *spi) in ltc2983_probe() argument
1472 const char *name = spi_get_device_id(spi)->name; in ltc2983_probe()
1475 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in ltc2983_probe()
1477 return -ENOMEM; in ltc2983_probe()
1481 st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config); in ltc2983_probe()
1482 if (IS_ERR(st->regmap)) { in ltc2983_probe()
1483 dev_err(&spi->dev, "Failed to initialize regmap\n"); in ltc2983_probe()
1484 return PTR_ERR(st->regmap); in ltc2983_probe()
1487 mutex_init(&st->lock); in ltc2983_probe()
1488 init_completion(&st->completion); in ltc2983_probe()
1489 st->spi = spi; in ltc2983_probe()
1490 spi_set_drvdata(spi, st); in ltc2983_probe()
1499 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler, in ltc2983_probe()
1502 dev_err(&spi->dev, "failed to request an irq, %d", ret); in ltc2983_probe()
1510 indio_dev->name = name; in ltc2983_probe()
1511 indio_dev->num_channels = st->iio_channels; in ltc2983_probe()
1512 indio_dev->channels = st->iio_chan; in ltc2983_probe()
1513 indio_dev->modes = INDIO_DIRECT_MODE; in ltc2983_probe()
1514 indio_dev->info = <c2983_iio_info; in ltc2983_probe()
1516 return devm_iio_device_register(&spi->dev, indio_dev); in ltc2983_probe()
1525 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy); in ltc2983_resume()
1526 /* we need to re-assign the channels */ in ltc2983_resume()
1534 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP); in ltc2983_suspend()
1543 MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1564 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");