Lines Matching +full:rsense +full:- +full:share

1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
46 (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
48 (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
116 /* cold junction for thermocouples and rsense for rtd's and thermistor's */
278 * instance a resolution of 2^-10 means we have 10 fractional bits.
292 s64 __res = -(s32)val; in __convert_to_raw_sign()
296 return (u32)-__res; in __convert_to_raw_sign()
303 const struct device *dev = &st->spi->dev; in __ltc2983_fault_handler()
307 return -EIO; in __ltc2983_fault_handler()
320 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan); in __ltc2983_chan_assign_common()
323 chan_val |= LTC2983_CHAN_TYPE(sensor->type); in __ltc2983_chan_assign_common()
324 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg, in __ltc2983_chan_assign_common()
327 return regmap_bulk_write(st->regmap, reg, &__chan_val, in __ltc2983_chan_assign_common()
336 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ : in __ltc2983_chan_custom_sensor_assign()
338 const struct device *dev = &st->spi->dev; in __ltc2983_chan_custom_sensor_assign()
340 * custom->size holds the raw size of the table. However, when in __ltc2983_chan_custom_sensor_assign()
345 const u8 len = custom->is_steinhart ? 0 : in __ltc2983_chan_custom_sensor_assign()
346 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1; in __ltc2983_chan_custom_sensor_assign()
351 if (custom->offset < 0) { in __ltc2983_chan_custom_sensor_assign()
358 if (st->custom_table_size + custom->size > in __ltc2983_chan_custom_sensor_assign()
359 (LTC2983_CUST_SENS_TBL_END_REG - in __ltc2983_chan_custom_sensor_assign()
363 st->custom_table_size, in __ltc2983_chan_custom_sensor_assign()
364 custom->size); in __ltc2983_chan_custom_sensor_assign()
365 return -EINVAL; in __ltc2983_chan_custom_sensor_assign()
368 custom->offset = st->custom_table_size / in __ltc2983_chan_custom_sensor_assign()
370 st->custom_table_size += custom->size; in __ltc2983_chan_custom_sensor_assign()
373 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG; in __ltc2983_chan_custom_sensor_assign()
376 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset); in __ltc2983_chan_custom_sensor_assign()
378 reg, custom->offset, in __ltc2983_chan_custom_sensor_assign()
379 custom->size); in __ltc2983_chan_custom_sensor_assign()
381 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size); in __ltc2983_chan_custom_sensor_assign()
390 struct device *dev = &st->spi->dev; in __ltc2983_custom_sensor_new()
406 return ERR_PTR(-EINVAL); in __ltc2983_custom_sensor_new()
411 return ERR_PTR(-ENOMEM); in __ltc2983_custom_sensor_new()
413 new_custom->size = n_entries * n_size; in __ltc2983_custom_sensor_new()
415 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) { in __ltc2983_custom_sensor_new()
416 dev_err(dev, "Steinhart sensors size(%zu) must be %u\n", new_custom->size, in __ltc2983_custom_sensor_new()
418 return ERR_PTR(-EINVAL); in __ltc2983_custom_sensor_new()
421 if (st->custom_table_size + new_custom->size > in __ltc2983_custom_sensor_new()
422 (LTC2983_CUST_SENS_TBL_END_REG - in __ltc2983_custom_sensor_new()
425 st->custom_table_size, new_custom->size); in __ltc2983_custom_sensor_new()
426 return ERR_PTR(-EINVAL); in __ltc2983_custom_sensor_new()
431 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL); in __ltc2983_custom_sensor_new()
433 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL); in __ltc2983_custom_sensor_new()
434 if (!new_custom->table) in __ltc2983_custom_sensor_new()
435 return ERR_PTR(-ENOMEM); in __ltc2983_custom_sensor_new()
445 ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries); in __ltc2983_custom_sensor_new()
449 cpu_to_be32_array(new_custom->table, new_custom->table, n_entries); in __ltc2983_custom_sensor_new()
451 ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries); in __ltc2983_custom_sensor_new()
456 u64 temp = ((u64 *)new_custom->table)[index]; in __ltc2983_custom_sensor_new()
465 put_unaligned_be24(temp, new_custom->table + index * 3); in __ltc2983_custom_sensor_new()
469 new_custom->is_steinhart = is_steinhart; in __ltc2983_custom_sensor_new()
474 * sure that sensor_addr - 0x250(start address) is a multiple of 4 in __ltc2983_custom_sensor_new()
477 * also a multiple of 6, we guarantee that the first non-steinhart in __ltc2983_custom_sensor_new()
482 new_custom->offset = st->custom_table_size / in __ltc2983_custom_sensor_new()
484 st->custom_table_size += new_custom->size; in __ltc2983_custom_sensor_new()
487 new_custom->offset = -1; in __ltc2983_custom_sensor_new()
515 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan); in ltc2983_thermocouple_assign_chan()
516 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config); in ltc2983_thermocouple_assign_chan()
518 if (thermo->custom) { in ltc2983_thermocouple_assign_chan()
521 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom, in ltc2983_thermocouple_assign_chan()
535 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan); in ltc2983_rtd_assign_chan()
536 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config); in ltc2983_rtd_assign_chan()
537 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current); in ltc2983_rtd_assign_chan()
538 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve); in ltc2983_rtd_assign_chan()
540 if (rtd->custom) { in ltc2983_rtd_assign_chan()
543 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom, in ltc2983_rtd_assign_chan()
557 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan); in ltc2983_thermistor_assign_chan()
558 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config); in ltc2983_thermistor_assign_chan()
560 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current); in ltc2983_thermistor_assign_chan()
562 if (thermistor->custom) { in ltc2983_thermistor_assign_chan()
566 thermistor->custom, in ltc2983_thermistor_assign_chan()
580 chan_val = LTC2983_DIODE_CFG(diode->sensor_config); in ltc2983_diode_assign_chan()
581 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current); in ltc2983_diode_assign_chan()
582 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value); in ltc2983_diode_assign_chan()
590 struct ltc2983_rsense *rsense = to_rsense(sensor); in ltc2983_r_sense_assign_chan() local
593 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val); in ltc2983_r_sense_assign_chan()
604 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended); in ltc2983_adc_assign_chan()
618 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL); in ltc2983_thermocouple_new()
620 return ERR_PTR(-ENOMEM); in ltc2983_thermocouple_new()
622 if (fwnode_property_read_bool(child, "adi,single-ended")) in ltc2983_thermocouple_new()
623 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1); in ltc2983_thermocouple_new()
625 ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current); in ltc2983_thermocouple_new()
629 thermo->sensor_config |= in ltc2983_thermocouple_new()
633 thermo->sensor_config |= in ltc2983_thermocouple_new()
637 thermo->sensor_config |= in ltc2983_thermocouple_new()
641 thermo->sensor_config |= in ltc2983_thermocouple_new()
645 dev_err(&st->spi->dev, in ltc2983_thermocouple_new()
647 return ERR_PTR(-EINVAL); in ltc2983_thermocouple_new()
650 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1); in ltc2983_thermocouple_new()
653 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) && in ltc2983_thermocouple_new()
654 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_thermocouple_new()
655 dev_err(&st->spi->dev, in ltc2983_thermocouple_new()
657 sensor->chan); in ltc2983_thermocouple_new()
658 return ERR_PTR(-EINVAL); in ltc2983_thermocouple_new()
661 ref = fwnode_find_reference(child, "adi,cold-junction-handle", 0); in ltc2983_thermocouple_new()
665 ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan); in ltc2983_thermocouple_new()
671 dev_err(&st->spi->dev, "Property reg must be given\n"); in ltc2983_thermocouple_new()
677 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { in ltc2983_thermocouple_new()
678 const char *propname = "adi,custom-thermocouple"; in ltc2983_thermocouple_new()
680 thermo->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_thermocouple_new()
683 if (IS_ERR(thermo->custom)) { in ltc2983_thermocouple_new()
684 ret = PTR_ERR(thermo->custom); in ltc2983_thermocouple_new()
690 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; in ltc2983_thermocouple_new()
691 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; in ltc2983_thermocouple_new()
694 return &thermo->sensor; in ltc2983_thermocouple_new()
707 struct device *dev = &st->spi->dev; in ltc2983_rtd_new()
713 return ERR_PTR(-ENOMEM); in ltc2983_rtd_new()
715 ref = fwnode_find_reference(child, "adi,rsense-handle", 0); in ltc2983_rtd_new()
717 dev_err(dev, "Property adi,rsense-handle missing or invalid"); in ltc2983_rtd_new()
721 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan); in ltc2983_rtd_new()
727 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires); in ltc2983_rtd_new()
731 rtd->sensor_config = LTC2983_RTD_N_WIRES(0); in ltc2983_rtd_new()
734 rtd->sensor_config = LTC2983_RTD_N_WIRES(1); in ltc2983_rtd_new()
737 rtd->sensor_config = LTC2983_RTD_N_WIRES(2); in ltc2983_rtd_new()
740 /* 4 wires, Kelvin Rsense */ in ltc2983_rtd_new()
741 rtd->sensor_config = LTC2983_RTD_N_WIRES(3); in ltc2983_rtd_new()
745 ret = -EINVAL; in ltc2983_rtd_new()
750 if (fwnode_property_read_bool(child, "adi,rsense-share")) { in ltc2983_rtd_new()
751 /* Current rotation is only available with rsense sharing */ in ltc2983_rtd_new()
752 if (fwnode_property_read_bool(child, "adi,current-rotate")) { in ltc2983_rtd_new()
756 ret = -EINVAL; in ltc2983_rtd_new()
759 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); in ltc2983_rtd_new()
761 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1); in ltc2983_rtd_new()
768 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be in ltc2983_rtd_new()
769 * <=1 since chanel - 1 and channel - 2 are used. in ltc2983_rtd_new()
771 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) { in ltc2983_rtd_new()
772 /* 4-wire */ in ltc2983_rtd_new()
776 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK) in ltc2983_rtd_new()
777 max = LTC2983_MAX_CHANNELS_NR - 1; in ltc2983_rtd_new()
779 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK) in ltc2983_rtd_new()
781 (rtd->r_sense_chan <= min)) { in ltc2983_rtd_new()
782 /* kelvin rsense*/ in ltc2983_rtd_new()
784 "Invalid rsense chann:%d to use in kelvin rsense", in ltc2983_rtd_new()
785 rtd->r_sense_chan); in ltc2983_rtd_new()
787 ret = -EINVAL; in ltc2983_rtd_new()
791 if (sensor->chan < min || sensor->chan > max) { in ltc2983_rtd_new()
793 sensor->chan); in ltc2983_rtd_new()
795 ret = -EINVAL; in ltc2983_rtd_new()
800 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_rtd_new()
801 dev_err(&st->spi->dev, in ltc2983_rtd_new()
802 "Invalid chann:%d for RTD", sensor->chan); in ltc2983_rtd_new()
804 ret = -EINVAL; in ltc2983_rtd_new()
810 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) { in ltc2983_rtd_new()
811 rtd->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_rtd_new()
812 "adi,custom-rtd", in ltc2983_rtd_new()
814 if (IS_ERR(rtd->custom)) { in ltc2983_rtd_new()
815 ret = PTR_ERR(rtd->custom); in ltc2983_rtd_new()
821 rtd->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_rtd_new()
822 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan; in ltc2983_rtd_new()
824 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp", in ltc2983_rtd_new()
828 rtd->excitation_current = 1; in ltc2983_rtd_new()
832 rtd->excitation_current = 0x01; in ltc2983_rtd_new()
835 rtd->excitation_current = 0x02; in ltc2983_rtd_new()
838 rtd->excitation_current = 0x03; in ltc2983_rtd_new()
841 rtd->excitation_current = 0x04; in ltc2983_rtd_new()
844 rtd->excitation_current = 0x05; in ltc2983_rtd_new()
847 rtd->excitation_current = 0x06; in ltc2983_rtd_new()
850 rtd->excitation_current = 0x07; in ltc2983_rtd_new()
853 rtd->excitation_current = 0x08; in ltc2983_rtd_new()
856 dev_err(&st->spi->dev, in ltc2983_rtd_new()
859 ret = -EINVAL; in ltc2983_rtd_new()
864 fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); in ltc2983_rtd_new()
867 return &rtd->sensor; in ltc2983_rtd_new()
878 struct device *dev = &st->spi->dev; in ltc2983_thermistor_new()
885 return ERR_PTR(-ENOMEM); in ltc2983_thermistor_new()
887 ref = fwnode_find_reference(child, "adi,rsense-handle", 0); in ltc2983_thermistor_new()
889 dev_err(dev, "Property adi,rsense-handle missing or invalid"); in ltc2983_thermistor_new()
893 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan); in ltc2983_thermistor_new()
895 dev_err(dev, "rsense channel must be configured...\n"); in ltc2983_thermistor_new()
899 if (fwnode_property_read_bool(child, "adi,single-ended")) { in ltc2983_thermistor_new()
900 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1); in ltc2983_thermistor_new()
901 } else if (fwnode_property_read_bool(child, "adi,rsense-share")) { in ltc2983_thermistor_new()
902 /* rotation is only possible if sharing rsense */ in ltc2983_thermistor_new()
903 if (fwnode_property_read_bool(child, "adi,current-rotate")) in ltc2983_thermistor_new()
904 thermistor->sensor_config = in ltc2983_thermistor_new()
907 thermistor->sensor_config = in ltc2983_thermistor_new()
911 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) && in ltc2983_thermistor_new()
912 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_thermistor_new()
913 dev_err(&st->spi->dev, in ltc2983_thermistor_new()
915 sensor->chan); in ltc2983_thermistor_new()
916 ret = -EINVAL; in ltc2983_thermistor_new()
921 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) { in ltc2983_thermistor_new()
925 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) { in ltc2983_thermistor_new()
927 propname = "adi,custom-steinhart"; in ltc2983_thermistor_new()
929 propname = "adi,custom-thermistor"; in ltc2983_thermistor_new()
932 thermistor->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_thermistor_new()
936 if (IS_ERR(thermistor->custom)) { in ltc2983_thermistor_new()
937 ret = PTR_ERR(thermistor->custom); in ltc2983_thermistor_new()
942 thermistor->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_thermistor_new()
943 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan; in ltc2983_thermistor_new()
945 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp", in ltc2983_thermistor_new()
949 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) in ltc2983_thermistor_new()
951 thermistor->excitation_current = 0x03; in ltc2983_thermistor_new()
953 /* default to auto-range */ in ltc2983_thermistor_new()
954 thermistor->excitation_current = 0x0c; in ltc2983_thermistor_new()
959 if (sensor->type >= in ltc2983_thermistor_new()
961 dev_err(&st->spi->dev, in ltc2983_thermistor_new()
963 ret = -EINVAL; in ltc2983_thermistor_new()
966 thermistor->excitation_current = 0x0c; in ltc2983_thermistor_new()
969 thermistor->excitation_current = 0x01; in ltc2983_thermistor_new()
972 thermistor->excitation_current = 0x02; in ltc2983_thermistor_new()
975 thermistor->excitation_current = 0x03; in ltc2983_thermistor_new()
978 thermistor->excitation_current = 0x04; in ltc2983_thermistor_new()
981 thermistor->excitation_current = 0x05; in ltc2983_thermistor_new()
984 thermistor->excitation_current = 0x06; in ltc2983_thermistor_new()
987 thermistor->excitation_current = 0x07; in ltc2983_thermistor_new()
990 thermistor->excitation_current = 0x08; in ltc2983_thermistor_new()
993 thermistor->excitation_current = 0x09; in ltc2983_thermistor_new()
996 thermistor->excitation_current = 0x0a; in ltc2983_thermistor_new()
999 thermistor->excitation_current = 0x0b; in ltc2983_thermistor_new()
1002 dev_err(&st->spi->dev, in ltc2983_thermistor_new()
1005 ret = -EINVAL; in ltc2983_thermistor_new()
1011 return &thermistor->sensor; in ltc2983_thermistor_new()
1025 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL); in ltc2983_diode_new()
1027 return ERR_PTR(-ENOMEM); in ltc2983_diode_new()
1029 if (fwnode_property_read_bool(child, "adi,single-ended")) in ltc2983_diode_new()
1030 diode->sensor_config = LTC2983_DIODE_SGL(1); in ltc2983_diode_new()
1032 if (fwnode_property_read_bool(child, "adi,three-conversion-cycles")) in ltc2983_diode_new()
1033 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1); in ltc2983_diode_new()
1035 if (fwnode_property_read_bool(child, "adi,average-on")) in ltc2983_diode_new()
1036 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1); in ltc2983_diode_new()
1039 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) && in ltc2983_diode_new()
1040 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_diode_new()
1041 dev_err(&st->spi->dev, in ltc2983_diode_new()
1043 sensor->chan); in ltc2983_diode_new()
1044 return ERR_PTR(-EINVAL); in ltc2983_diode_new()
1047 diode->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_diode_new()
1048 diode->sensor.assign_chan = ltc2983_diode_assign_chan; in ltc2983_diode_new()
1050 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp", in ltc2983_diode_new()
1055 diode->excitation_current = 0x00; in ltc2983_diode_new()
1058 diode->excitation_current = 0x01; in ltc2983_diode_new()
1061 diode->excitation_current = 0x02; in ltc2983_diode_new()
1064 diode->excitation_current = 0x03; in ltc2983_diode_new()
1067 dev_err(&st->spi->dev, in ltc2983_diode_new()
1070 return ERR_PTR(-EINVAL); in ltc2983_diode_new()
1074 fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp); in ltc2983_diode_new()
1077 diode->ideal_factor_value = __convert_to_raw(temp, 1048576); in ltc2983_diode_new()
1079 return &diode->sensor; in ltc2983_diode_new()
1086 struct ltc2983_rsense *rsense; in ltc2983_r_sense_new() local
1090 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL); in ltc2983_r_sense_new()
1091 if (!rsense) in ltc2983_r_sense_new()
1092 return ERR_PTR(-ENOMEM); in ltc2983_r_sense_new()
1095 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_r_sense_new()
1096 dev_err(&st->spi->dev, "Invalid chann:%d for r_sense", in ltc2983_r_sense_new()
1097 sensor->chan); in ltc2983_r_sense_new()
1098 return ERR_PTR(-EINVAL); in ltc2983_r_sense_new()
1101 ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp); in ltc2983_r_sense_new()
1103 dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n"); in ltc2983_r_sense_new()
1104 return ERR_PTR(-EINVAL); in ltc2983_r_sense_new()
1107 * Times 1000 because we have milli-ohms and __convert_to_raw in ltc2983_r_sense_new()
1112 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024); in ltc2983_r_sense_new()
1115 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan; in ltc2983_r_sense_new()
1117 return &rsense->sensor; in ltc2983_r_sense_new()
1126 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL); in ltc2983_adc_new()
1128 return ERR_PTR(-ENOMEM); in ltc2983_adc_new()
1130 if (fwnode_property_read_bool(child, "adi,single-ended")) in ltc2983_adc_new()
1131 adc->single_ended = true; in ltc2983_adc_new()
1133 if (!adc->single_ended && in ltc2983_adc_new()
1134 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { in ltc2983_adc_new()
1135 dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n", in ltc2983_adc_new()
1136 sensor->chan); in ltc2983_adc_new()
1137 return ERR_PTR(-EINVAL); in ltc2983_adc_new()
1140 adc->sensor.assign_chan = ltc2983_adc_assign_chan; in ltc2983_adc_new()
1141 adc->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_adc_new()
1143 return &adc->sensor; in ltc2983_adc_new()
1154 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan); in ltc2983_chan_read()
1155 dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n", in ltc2983_chan_read()
1156 sensor->chan, start_conversion); in ltc2983_chan_read()
1158 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion); in ltc2983_chan_read()
1162 reinit_completion(&st->completion); in ltc2983_chan_read()
1169 time = wait_for_completion_timeout(&st->completion, in ltc2983_chan_read()
1172 dev_warn(&st->spi->dev, "Conversion timed out\n"); in ltc2983_chan_read()
1173 return -ETIMEDOUT; in ltc2983_chan_read()
1177 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan), in ltc2983_chan_read()
1178 &st->temp, sizeof(st->temp)); in ltc2983_chan_read()
1182 *val = __be32_to_cpu(st->temp); in ltc2983_chan_read()
1185 dev_err(&st->spi->dev, "Invalid conversion detected\n"); in ltc2983_chan_read()
1186 return -EIO; in ltc2983_chan_read()
1189 ret = sensor->fault_handler(st, *val); in ltc2983_chan_read()
1205 if (chan->address >= st->num_channels) { in ltc2983_read_raw()
1206 dev_err(&st->spi->dev, "Invalid chan address:%ld", in ltc2983_read_raw()
1207 chan->address); in ltc2983_read_raw()
1208 return -EINVAL; in ltc2983_read_raw()
1213 mutex_lock(&st->lock); in ltc2983_read_raw()
1214 ret = ltc2983_chan_read(st, st->sensors[chan->address], val); in ltc2983_read_raw()
1215 mutex_unlock(&st->lock); in ltc2983_read_raw()
1218 switch (chan->type) { in ltc2983_read_raw()
1232 return -EINVAL; in ltc2983_read_raw()
1236 return -EINVAL; in ltc2983_read_raw()
1247 return regmap_read(st->regmap, reg, readval); in ltc2983_reg_access()
1249 return regmap_write(st->regmap, reg, writeval); in ltc2983_reg_access()
1256 complete(&st->completion); in ltc2983_irq_handler()
1274 struct device *dev = &st->spi->dev; in ltc2983_parse_dt()
1278 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config); in ltc2983_parse_dt()
1280 device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq); in ltc2983_parse_dt()
1282 st->num_channels = device_get_child_node_count(dev); in ltc2983_parse_dt()
1283 if (!st->num_channels) { in ltc2983_parse_dt()
1284 dev_err(&st->spi->dev, "At least one channel must be given!"); in ltc2983_parse_dt()
1285 return -EINVAL; in ltc2983_parse_dt()
1288 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors), in ltc2983_parse_dt()
1290 if (!st->sensors) in ltc2983_parse_dt()
1291 return -ENOMEM; in ltc2983_parse_dt()
1293 st->iio_channels = st->num_channels; in ltc2983_parse_dt()
1306 ret = -EINVAL; in ltc2983_parse_dt()
1311 ret = -EINVAL; in ltc2983_parse_dt()
1316 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type); in ltc2983_parse_dt()
1319 "adi,sensor-type property must given for child nodes\n"); in ltc2983_parse_dt()
1329 st->sensors[chan] = ltc2983_thermocouple_new(child, st, in ltc2983_parse_dt()
1333 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor); in ltc2983_parse_dt()
1336 st->sensors[chan] = ltc2983_thermistor_new(child, st, in ltc2983_parse_dt()
1339 st->sensors[chan] = ltc2983_diode_new(child, st, in ltc2983_parse_dt()
1342 st->sensors[chan] = ltc2983_r_sense_new(child, st, in ltc2983_parse_dt()
1344 /* don't add rsense to iio */ in ltc2983_parse_dt()
1345 st->iio_channels--; in ltc2983_parse_dt()
1347 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor); in ltc2983_parse_dt()
1350 ret = -EINVAL; in ltc2983_parse_dt()
1354 if (IS_ERR(st->sensors[chan])) { in ltc2983_parse_dt()
1356 PTR_ERR(st->sensors[chan])); in ltc2983_parse_dt()
1357 ret = PTR_ERR(st->sensors[chan]); in ltc2983_parse_dt()
1361 st->sensors[chan]->chan = sensor.chan; in ltc2983_parse_dt()
1362 st->sensors[chan]->type = sensor.type; in ltc2983_parse_dt()
1380 ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status, in ltc2983_setup()
1384 dev_err(&st->spi->dev, "Device startup timed out\n"); in ltc2983_setup()
1388 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG, in ltc2983_setup()
1390 LTC2983_NOTCH_FREQ(st->filter_notch_freq)); in ltc2983_setup()
1394 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG, in ltc2983_setup()
1395 st->mux_delay_config); in ltc2983_setup()
1399 for (chan = 0; chan < st->num_channels; chan++) { in ltc2983_setup()
1402 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]); in ltc2983_setup()
1408 * re-configure the device channels. in ltc2983_setup()
1409 * We also don't assign iio channels for rsense. in ltc2983_setup()
1411 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR || in ltc2983_setup()
1416 if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) { in ltc2983_setup()
1428 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++, in ltc2983_setup()
1476 const char *name = spi_get_device_id(spi)->name; in ltc2983_probe()
1479 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in ltc2983_probe()
1481 return -ENOMEM; in ltc2983_probe()
1485 st->regmap = devm_regmap_init_spi(spi, &ltc2983_regmap_config); in ltc2983_probe()
1486 if (IS_ERR(st->regmap)) { in ltc2983_probe()
1487 dev_err(&spi->dev, "Failed to initialize regmap\n"); in ltc2983_probe()
1488 return PTR_ERR(st->regmap); in ltc2983_probe()
1491 mutex_init(&st->lock); in ltc2983_probe()
1492 init_completion(&st->completion); in ltc2983_probe()
1493 st->spi = spi; in ltc2983_probe()
1500 gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH); in ltc2983_probe()
1510 st->iio_chan = devm_kzalloc(&spi->dev, in ltc2983_probe()
1511 st->iio_channels * sizeof(*st->iio_chan), in ltc2983_probe()
1513 if (!st->iio_chan) in ltc2983_probe()
1514 return -ENOMEM; in ltc2983_probe()
1520 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler, in ltc2983_probe()
1523 dev_err(&spi->dev, "failed to request an irq, %d", ret); in ltc2983_probe()
1527 indio_dev->name = name; in ltc2983_probe()
1528 indio_dev->num_channels = st->iio_channels; in ltc2983_probe()
1529 indio_dev->channels = st->iio_chan; in ltc2983_probe()
1530 indio_dev->modes = INDIO_DIRECT_MODE; in ltc2983_probe()
1531 indio_dev->info = &ltc2983_iio_info; in ltc2983_probe()
1533 return devm_iio_device_register(&spi->dev, indio_dev); in ltc2983_probe()
1542 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy); in ltc2983_resume()
1543 /* we need to re-assign the channels */ in ltc2983_resume()
1551 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP); in ltc2983_suspend()