Lines Matching +full:temp +full:- +full:sensor
1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
60 (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
62 (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
192 container_of(_sensor, struct ltc2983_thermocouple, sensor)
195 container_of(_sensor, struct ltc2983_rtd, sensor)
198 container_of(_sensor, struct ltc2983_thermistor, sensor)
201 container_of(_sensor, struct ltc2983_diode, sensor)
204 container_of(_sensor, struct ltc2983_rsense, sensor)
207 container_of(_sensor, struct ltc2983_adc, sensor)
210 container_of(_sensor, struct ltc2983_temp, sensor)
237 __be32 temp __aligned(IIO_DMA_MINALIGN);
245 const struct ltc2983_sensor *sensor);
246 /* specifies the sensor channel */
248 /* sensor type */
253 /* raw table sensor data */
262 struct ltc2983_sensor sensor; member
269 struct ltc2983_sensor sensor; member
278 struct ltc2983_sensor sensor; member
286 struct ltc2983_sensor sensor; member
293 struct ltc2983_sensor sensor; member
298 struct ltc2983_sensor sensor; member
303 struct ltc2983_sensor sensor; member
312 * instance a resolution of 2^-10 means we have 10 fractional bits.
326 s64 __res = -(s32)val; in __convert_to_raw_sign()
330 return (u32)-__res; in __convert_to_raw_sign()
337 const struct device *dev = &st->spi->dev; in __ltc2983_fault_handler()
340 dev_err(dev, "Invalid conversion: Sensor HARD fault\n"); in __ltc2983_fault_handler()
341 return -EIO; in __ltc2983_fault_handler()
344 dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n"); in __ltc2983_fault_handler()
351 const struct ltc2983_sensor *sensor, in __ltc2983_chan_assign_common() argument
354 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan); in __ltc2983_chan_assign_common()
356 chan_val |= LTC2983_CHAN_TYPE(sensor->type); in __ltc2983_chan_assign_common()
357 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg, in __ltc2983_chan_assign_common()
359 st->chan_val = cpu_to_be32(chan_val); in __ltc2983_chan_assign_common()
360 return regmap_bulk_write(st->regmap, reg, &st->chan_val, in __ltc2983_chan_assign_common()
361 sizeof(st->chan_val)); in __ltc2983_chan_assign_common()
369 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ : in __ltc2983_chan_custom_sensor_assign()
371 const struct device *dev = &st->spi->dev; in __ltc2983_chan_custom_sensor_assign()
373 * custom->size holds the raw size of the table. However, when in __ltc2983_chan_custom_sensor_assign()
374 * configuring the sensor channel, we must write the number of in __ltc2983_chan_custom_sensor_assign()
378 const u8 len = custom->is_steinhart ? 0 : in __ltc2983_chan_custom_sensor_assign()
379 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1; in __ltc2983_chan_custom_sensor_assign()
384 if (custom->offset < 0) { in __ltc2983_chan_custom_sensor_assign()
388 * sensor, a steinhart sensor might have been added changing in __ltc2983_chan_custom_sensor_assign()
391 if (st->custom_table_size + custom->size > in __ltc2983_chan_custom_sensor_assign()
392 (LTC2983_CUST_SENS_TBL_END_REG - in __ltc2983_chan_custom_sensor_assign()
395 "Not space left(%d) for new custom sensor(%zu)", in __ltc2983_chan_custom_sensor_assign()
396 st->custom_table_size, in __ltc2983_chan_custom_sensor_assign()
397 custom->size); in __ltc2983_chan_custom_sensor_assign()
398 return -EINVAL; in __ltc2983_chan_custom_sensor_assign()
401 custom->offset = st->custom_table_size / in __ltc2983_chan_custom_sensor_assign()
403 st->custom_table_size += custom->size; in __ltc2983_chan_custom_sensor_assign()
406 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG; in __ltc2983_chan_custom_sensor_assign()
409 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset); in __ltc2983_chan_custom_sensor_assign()
410 dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu", in __ltc2983_chan_custom_sensor_assign()
411 reg, custom->offset, in __ltc2983_chan_custom_sensor_assign()
412 custom->size); in __ltc2983_chan_custom_sensor_assign()
413 /* write custom sensor table */ in __ltc2983_chan_custom_sensor_assign()
414 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size); in __ltc2983_chan_custom_sensor_assign()
423 struct device *dev = &st->spi->dev; in __ltc2983_custom_sensor_new()
438 return dev_err_ptr_probe(dev, -EINVAL, in __ltc2983_custom_sensor_new()
443 return ERR_PTR(-ENOMEM); in __ltc2983_custom_sensor_new()
445 new_custom->size = n_entries * n_size; in __ltc2983_custom_sensor_new()
447 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) in __ltc2983_custom_sensor_new()
448 return dev_err_ptr_probe(dev, -EINVAL, in __ltc2983_custom_sensor_new()
450 new_custom->size, LTC2983_CUSTOM_STEINHART_SIZE); in __ltc2983_custom_sensor_new()
453 if (st->custom_table_size + new_custom->size > in __ltc2983_custom_sensor_new()
454 (LTC2983_CUST_SENS_TBL_END_REG - LTC2983_CUST_SENS_TBL_START_REG) + 1) in __ltc2983_custom_sensor_new()
455 return dev_err_ptr_probe(dev, -EINVAL, in __ltc2983_custom_sensor_new()
456 "No space left(%d) for new custom sensor(%zu)\n", in __ltc2983_custom_sensor_new()
457 st->custom_table_size, new_custom->size); in __ltc2983_custom_sensor_new()
461 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL); in __ltc2983_custom_sensor_new()
463 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL); in __ltc2983_custom_sensor_new()
464 if (!new_custom->table) in __ltc2983_custom_sensor_new()
465 return ERR_PTR(-ENOMEM); in __ltc2983_custom_sensor_new()
475 ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries); in __ltc2983_custom_sensor_new()
479 cpu_to_be32_array(new_custom->table, new_custom->table, n_entries); in __ltc2983_custom_sensor_new()
481 ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries); in __ltc2983_custom_sensor_new()
486 u64 temp = ((u64 *)new_custom->table)[index]; in __ltc2983_custom_sensor_new() local
489 temp = __convert_to_raw(temp, 1024); in __ltc2983_custom_sensor_new()
490 else if (has_signed && (s64)temp < 0) in __ltc2983_custom_sensor_new()
491 temp = __convert_to_raw_sign(temp, resolution); in __ltc2983_custom_sensor_new()
493 temp = __convert_to_raw(temp, resolution); in __ltc2983_custom_sensor_new()
495 put_unaligned_be24(temp, new_custom->table + index * 3); in __ltc2983_custom_sensor_new()
499 new_custom->is_steinhart = is_steinhart; in __ltc2983_custom_sensor_new()
504 * sure that sensor_addr - 0x250(start address) is a multiple of 4 in __ltc2983_custom_sensor_new()
507 * also a multiple of 6, we guarantee that the first non-steinhart in __ltc2983_custom_sensor_new()
508 * sensor will sit in a correct address without the need of filling in __ltc2983_custom_sensor_new()
512 new_custom->offset = st->custom_table_size / in __ltc2983_custom_sensor_new()
514 st->custom_table_size += new_custom->size; in __ltc2983_custom_sensor_new()
517 new_custom->offset = -1; in __ltc2983_custom_sensor_new()
540 const struct ltc2983_sensor *sensor) in ltc2983_thermocouple_assign_chan() argument
542 struct ltc2983_thermocouple *thermo = to_thermocouple(sensor); in ltc2983_thermocouple_assign_chan()
545 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan); in ltc2983_thermocouple_assign_chan()
546 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config); in ltc2983_thermocouple_assign_chan()
548 if (thermo->custom) { in ltc2983_thermocouple_assign_chan()
551 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom, in ltc2983_thermocouple_assign_chan()
556 return __ltc2983_chan_assign_common(st, sensor, chan_val); in ltc2983_thermocouple_assign_chan()
560 const struct ltc2983_sensor *sensor) in ltc2983_rtd_assign_chan() argument
562 struct ltc2983_rtd *rtd = to_rtd(sensor); in ltc2983_rtd_assign_chan()
565 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan); in ltc2983_rtd_assign_chan()
566 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config); in ltc2983_rtd_assign_chan()
567 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current); in ltc2983_rtd_assign_chan()
568 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve); in ltc2983_rtd_assign_chan()
570 if (rtd->custom) { in ltc2983_rtd_assign_chan()
573 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom, in ltc2983_rtd_assign_chan()
578 return __ltc2983_chan_assign_common(st, sensor, chan_val); in ltc2983_rtd_assign_chan()
582 const struct ltc2983_sensor *sensor) in ltc2983_thermistor_assign_chan() argument
584 struct ltc2983_thermistor *thermistor = to_thermistor(sensor); in ltc2983_thermistor_assign_chan()
587 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan); in ltc2983_thermistor_assign_chan()
588 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config); in ltc2983_thermistor_assign_chan()
590 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current); in ltc2983_thermistor_assign_chan()
592 if (thermistor->custom) { in ltc2983_thermistor_assign_chan()
596 thermistor->custom, in ltc2983_thermistor_assign_chan()
601 return __ltc2983_chan_assign_common(st, sensor, chan_val); in ltc2983_thermistor_assign_chan()
605 const struct ltc2983_sensor *sensor) in ltc2983_diode_assign_chan() argument
607 struct ltc2983_diode *diode = to_diode(sensor); in ltc2983_diode_assign_chan()
610 chan_val = LTC2983_DIODE_CFG(diode->sensor_config); in ltc2983_diode_assign_chan()
611 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current); in ltc2983_diode_assign_chan()
612 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value); in ltc2983_diode_assign_chan()
614 return __ltc2983_chan_assign_common(st, sensor, chan_val); in ltc2983_diode_assign_chan()
618 const struct ltc2983_sensor *sensor) in ltc2983_r_sense_assign_chan() argument
620 struct ltc2983_rsense *rsense = to_rsense(sensor); in ltc2983_r_sense_assign_chan()
623 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val); in ltc2983_r_sense_assign_chan()
625 return __ltc2983_chan_assign_common(st, sensor, chan_val); in ltc2983_r_sense_assign_chan()
629 const struct ltc2983_sensor *sensor) in ltc2983_adc_assign_chan() argument
631 struct ltc2983_adc *adc = to_adc(sensor); in ltc2983_adc_assign_chan()
634 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended); in ltc2983_adc_assign_chan()
636 return __ltc2983_chan_assign_common(st, sensor, chan_val); in ltc2983_adc_assign_chan()
640 const struct ltc2983_sensor *sensor) in ltc2983_temp_assign_chan() argument
642 struct ltc2983_temp *temp = to_temp(sensor); in ltc2983_temp_assign_chan() local
646 chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended); in ltc2983_temp_assign_chan()
648 ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val); in ltc2983_temp_assign_chan()
652 return __ltc2983_chan_assign_common(st, sensor, chan_val); in ltc2983_temp_assign_chan()
657 const struct ltc2983_sensor *sensor) in ltc2983_thermocouple_new() argument
663 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL); in ltc2983_thermocouple_new()
665 return ERR_PTR(-ENOMEM); in ltc2983_thermocouple_new()
667 if (fwnode_property_read_bool(child, "adi,single-ended")) in ltc2983_thermocouple_new()
668 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1); in ltc2983_thermocouple_new()
670 ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current); in ltc2983_thermocouple_new()
674 thermo->sensor_config |= in ltc2983_thermocouple_new()
678 thermo->sensor_config |= in ltc2983_thermocouple_new()
682 thermo->sensor_config |= in ltc2983_thermocouple_new()
686 thermo->sensor_config |= in ltc2983_thermocouple_new()
690 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_thermocouple_new()
695 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1); in ltc2983_thermocouple_new()
698 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) && in ltc2983_thermocouple_new()
699 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) in ltc2983_thermocouple_new()
700 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_thermocouple_new()
702 sensor->chan); in ltc2983_thermocouple_new()
705 fwnode_find_reference(child, "adi,cold-junction-handle", 0); in ltc2983_thermocouple_new()
709 ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan); in ltc2983_thermocouple_new()
715 return dev_err_ptr_probe(&st->spi->dev, ret, in ltc2983_thermocouple_new()
719 /* check custom sensor */ in ltc2983_thermocouple_new()
720 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { in ltc2983_thermocouple_new()
721 const char *propname = "adi,custom-thermocouple"; in ltc2983_thermocouple_new()
723 thermo->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_thermocouple_new()
726 if (IS_ERR(thermo->custom)) in ltc2983_thermocouple_new()
727 return ERR_CAST(thermo->custom); in ltc2983_thermocouple_new()
731 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; in ltc2983_thermocouple_new()
732 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; in ltc2983_thermocouple_new()
734 return &thermo->sensor; in ltc2983_thermocouple_new()
739 const struct ltc2983_sensor *sensor) in ltc2983_rtd_new() argument
743 struct device *dev = &st->spi->dev; in ltc2983_rtd_new()
748 return ERR_PTR(-ENOMEM); in ltc2983_rtd_new()
751 fwnode_find_reference(child, "adi,rsense-handle", 0); in ltc2983_rtd_new()
754 "Property adi,rsense-handle missing or invalid\n"); in ltc2983_rtd_new()
756 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan); in ltc2983_rtd_new()
761 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires); in ltc2983_rtd_new()
765 rtd->sensor_config = LTC2983_RTD_N_WIRES(0); in ltc2983_rtd_new()
768 rtd->sensor_config = LTC2983_RTD_N_WIRES(1); in ltc2983_rtd_new()
771 rtd->sensor_config = LTC2983_RTD_N_WIRES(2); in ltc2983_rtd_new()
775 rtd->sensor_config = LTC2983_RTD_N_WIRES(3); in ltc2983_rtd_new()
778 return dev_err_ptr_probe(dev, -EINVAL, in ltc2983_rtd_new()
784 if (fwnode_property_read_bool(child, "adi,rsense-share")) { in ltc2983_rtd_new()
786 if (fwnode_property_read_bool(child, "adi,current-rotate")) { in ltc2983_rtd_new()
788 return dev_err_ptr_probe(dev, -EINVAL, in ltc2983_rtd_new()
791 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); in ltc2983_rtd_new()
793 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1); in ltc2983_rtd_new()
801 * <=1 since chanel - 1 and channel - 2 are used. in ltc2983_rtd_new()
803 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) { in ltc2983_rtd_new()
804 /* 4-wire */ in ltc2983_rtd_new()
806 max = st->info->max_channels_nr; in ltc2983_rtd_new()
808 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK) in ltc2983_rtd_new()
809 max = st->info->max_channels_nr - 1; in ltc2983_rtd_new()
811 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK) in ltc2983_rtd_new()
813 (rtd->r_sense_chan <= min)) in ltc2983_rtd_new()
815 return dev_err_ptr_probe(dev, -EINVAL, in ltc2983_rtd_new()
817 rtd->r_sense_chan); in ltc2983_rtd_new()
819 if (sensor->chan < min || sensor->chan > max) in ltc2983_rtd_new()
820 return dev_err_ptr_probe(dev, -EINVAL, in ltc2983_rtd_new()
822 sensor->chan); in ltc2983_rtd_new()
825 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) in ltc2983_rtd_new()
826 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_rtd_new()
828 sensor->chan); in ltc2983_rtd_new()
831 /* check custom sensor */ in ltc2983_rtd_new()
832 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) { in ltc2983_rtd_new()
833 rtd->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_rtd_new()
834 "adi,custom-rtd", in ltc2983_rtd_new()
836 if (IS_ERR(rtd->custom)) in ltc2983_rtd_new()
837 return ERR_CAST(rtd->custom); in ltc2983_rtd_new()
841 rtd->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_rtd_new()
842 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan; in ltc2983_rtd_new()
844 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp", in ltc2983_rtd_new()
848 rtd->excitation_current = 1; in ltc2983_rtd_new()
852 rtd->excitation_current = 0x01; in ltc2983_rtd_new()
855 rtd->excitation_current = 0x02; in ltc2983_rtd_new()
858 rtd->excitation_current = 0x03; in ltc2983_rtd_new()
861 rtd->excitation_current = 0x04; in ltc2983_rtd_new()
864 rtd->excitation_current = 0x05; in ltc2983_rtd_new()
867 rtd->excitation_current = 0x06; in ltc2983_rtd_new()
870 rtd->excitation_current = 0x07; in ltc2983_rtd_new()
873 rtd->excitation_current = 0x08; in ltc2983_rtd_new()
876 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_rtd_new()
882 fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); in ltc2983_rtd_new()
884 return &rtd->sensor; in ltc2983_rtd_new()
889 const struct ltc2983_sensor *sensor) in ltc2983_thermistor_new() argument
892 struct device *dev = &st->spi->dev; in ltc2983_thermistor_new()
898 return ERR_PTR(-ENOMEM); in ltc2983_thermistor_new()
901 fwnode_find_reference(child, "adi,rsense-handle", 0); in ltc2983_thermistor_new()
904 "Property adi,rsense-handle missing or invalid\n"); in ltc2983_thermistor_new()
906 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan); in ltc2983_thermistor_new()
911 if (fwnode_property_read_bool(child, "adi,single-ended")) { in ltc2983_thermistor_new()
912 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1); in ltc2983_thermistor_new()
913 } else if (fwnode_property_read_bool(child, "adi,rsense-share")) { in ltc2983_thermistor_new()
915 if (fwnode_property_read_bool(child, "adi,current-rotate")) in ltc2983_thermistor_new()
916 thermistor->sensor_config = in ltc2983_thermistor_new()
919 thermistor->sensor_config = in ltc2983_thermistor_new()
923 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) && in ltc2983_thermistor_new()
924 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) in ltc2983_thermistor_new()
925 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_thermistor_new()
927 sensor->chan); in ltc2983_thermistor_new()
929 /* check custom sensor */ in ltc2983_thermistor_new()
930 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) { in ltc2983_thermistor_new()
934 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) { in ltc2983_thermistor_new()
936 propname = "adi,custom-steinhart"; in ltc2983_thermistor_new()
938 propname = "adi,custom-thermistor"; in ltc2983_thermistor_new()
941 thermistor->custom = __ltc2983_custom_sensor_new(st, child, in ltc2983_thermistor_new()
945 if (IS_ERR(thermistor->custom)) in ltc2983_thermistor_new()
946 return ERR_CAST(thermistor->custom); in ltc2983_thermistor_new()
949 thermistor->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_thermistor_new()
950 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan; in ltc2983_thermistor_new()
952 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp", in ltc2983_thermistor_new()
956 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) in ltc2983_thermistor_new()
958 thermistor->excitation_current = 0x03; in ltc2983_thermistor_new()
960 /* default to auto-range */ in ltc2983_thermistor_new()
961 thermistor->excitation_current = 0x0c; in ltc2983_thermistor_new()
966 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) in ltc2983_thermistor_new()
967 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_thermistor_new()
970 thermistor->excitation_current = 0x0c; in ltc2983_thermistor_new()
973 thermistor->excitation_current = 0x01; in ltc2983_thermistor_new()
976 thermistor->excitation_current = 0x02; in ltc2983_thermistor_new()
979 thermistor->excitation_current = 0x03; in ltc2983_thermistor_new()
982 thermistor->excitation_current = 0x04; in ltc2983_thermistor_new()
985 thermistor->excitation_current = 0x05; in ltc2983_thermistor_new()
988 thermistor->excitation_current = 0x06; in ltc2983_thermistor_new()
991 thermistor->excitation_current = 0x07; in ltc2983_thermistor_new()
994 thermistor->excitation_current = 0x08; in ltc2983_thermistor_new()
997 thermistor->excitation_current = 0x09; in ltc2983_thermistor_new()
1000 thermistor->excitation_current = 0x0a; in ltc2983_thermistor_new()
1003 thermistor->excitation_current = 0x0b; in ltc2983_thermistor_new()
1006 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_thermistor_new()
1012 return &thermistor->sensor; in ltc2983_thermistor_new()
1017 const struct ltc2983_sensor *sensor) in ltc2983_diode_new() argument
1020 u32 temp = 0, excitation_current = 0; in ltc2983_diode_new() local
1023 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL); in ltc2983_diode_new()
1025 return ERR_PTR(-ENOMEM); in ltc2983_diode_new()
1027 if (fwnode_property_read_bool(child, "adi,single-ended")) in ltc2983_diode_new()
1028 diode->sensor_config = LTC2983_DIODE_SGL(1); in ltc2983_diode_new()
1030 if (fwnode_property_read_bool(child, "adi,three-conversion-cycles")) in ltc2983_diode_new()
1031 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1); in ltc2983_diode_new()
1033 if (fwnode_property_read_bool(child, "adi,average-on")) in ltc2983_diode_new()
1034 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1); in ltc2983_diode_new()
1037 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) && in ltc2983_diode_new()
1038 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) in ltc2983_diode_new()
1039 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_diode_new()
1041 sensor->chan); in ltc2983_diode_new()
1044 diode->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_diode_new()
1045 diode->sensor.assign_chan = ltc2983_diode_assign_chan; in ltc2983_diode_new()
1047 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp", in ltc2983_diode_new()
1052 diode->excitation_current = 0x00; in ltc2983_diode_new()
1055 diode->excitation_current = 0x01; in ltc2983_diode_new()
1058 diode->excitation_current = 0x02; in ltc2983_diode_new()
1061 diode->excitation_current = 0x03; in ltc2983_diode_new()
1064 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_diode_new()
1070 fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp); in ltc2983_diode_new()
1073 diode->ideal_factor_value = __convert_to_raw(temp, 1048576); in ltc2983_diode_new()
1075 return &diode->sensor; in ltc2983_diode_new()
1080 const struct ltc2983_sensor *sensor) in ltc2983_r_sense_new() argument
1084 u32 temp; in ltc2983_r_sense_new() local
1086 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL); in ltc2983_r_sense_new()
1088 return ERR_PTR(-ENOMEM); in ltc2983_r_sense_new()
1091 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) in ltc2983_r_sense_new()
1092 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_r_sense_new()
1094 sensor->chan); in ltc2983_r_sense_new()
1096 ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp); in ltc2983_r_sense_new()
1098 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_r_sense_new()
1099 "Property adi,rsense-val-milli-ohms missing\n"); in ltc2983_r_sense_new()
1101 * Times 1000 because we have milli-ohms and __convert_to_raw in ltc2983_r_sense_new()
1106 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024); in ltc2983_r_sense_new()
1109 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan; in ltc2983_r_sense_new()
1111 return &rsense->sensor; in ltc2983_r_sense_new()
1116 const struct ltc2983_sensor *sensor) in ltc2983_adc_new() argument
1120 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL); in ltc2983_adc_new()
1122 return ERR_PTR(-ENOMEM); in ltc2983_adc_new()
1124 if (fwnode_property_read_bool(child, "adi,single-ended")) in ltc2983_adc_new()
1125 adc->single_ended = true; in ltc2983_adc_new()
1127 if (!adc->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) in ltc2983_adc_new()
1128 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_adc_new()
1130 sensor->chan); in ltc2983_adc_new()
1133 adc->sensor.assign_chan = ltc2983_adc_assign_chan; in ltc2983_adc_new()
1134 adc->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_adc_new()
1136 return &adc->sensor; in ltc2983_adc_new()
1141 const struct ltc2983_sensor *sensor) in ltc2983_temp_new() argument
1143 struct ltc2983_temp *temp; in ltc2983_temp_new() local
1145 temp = devm_kzalloc(&st->spi->dev, sizeof(*temp), GFP_KERNEL); in ltc2983_temp_new()
1146 if (!temp) in ltc2983_temp_new()
1147 return ERR_PTR(-ENOMEM); in ltc2983_temp_new()
1149 if (fwnode_property_read_bool(child, "adi,single-ended")) in ltc2983_temp_new()
1150 temp->single_ended = true; in ltc2983_temp_new()
1152 if (!temp->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) in ltc2983_temp_new()
1153 return dev_err_ptr_probe(&st->spi->dev, -EINVAL, in ltc2983_temp_new()
1154 "Invalid chan:%d for differential temp\n", in ltc2983_temp_new()
1155 sensor->chan); in ltc2983_temp_new()
1157 temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp", in ltc2983_temp_new()
1159 if (IS_ERR(temp->custom)) in ltc2983_temp_new()
1160 return ERR_CAST(temp->custom); in ltc2983_temp_new()
1163 temp->sensor.assign_chan = ltc2983_temp_assign_chan; in ltc2983_temp_new()
1164 temp->sensor.fault_handler = ltc2983_common_fault_handler; in ltc2983_temp_new()
1166 return &temp->sensor; in ltc2983_temp_new()
1170 const struct ltc2983_sensor *sensor, int *val) in ltc2983_chan_read() argument
1177 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan); in ltc2983_chan_read()
1178 dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n", in ltc2983_chan_read()
1179 sensor->chan, start_conversion); in ltc2983_chan_read()
1181 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion); in ltc2983_chan_read()
1185 reinit_completion(&st->completion); in ltc2983_chan_read()
1189 * Depending on the sensor configuration, there are 2/3 conversions in ltc2983_chan_read()
1192 time = wait_for_completion_timeout(&st->completion, in ltc2983_chan_read()
1195 dev_warn(&st->spi->dev, "Conversion timed out\n"); in ltc2983_chan_read()
1196 return -ETIMEDOUT; in ltc2983_chan_read()
1200 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan), in ltc2983_chan_read()
1201 &st->temp, sizeof(st->temp)); in ltc2983_chan_read()
1205 *val = __be32_to_cpu(st->temp); in ltc2983_chan_read()
1208 dev_err(&st->spi->dev, "Invalid conversion detected\n"); in ltc2983_chan_read()
1209 return -EIO; in ltc2983_chan_read()
1212 ret = sensor->fault_handler(st, *val); in ltc2983_chan_read()
1228 if (chan->address >= st->num_channels) { in ltc2983_read_raw()
1229 dev_err(&st->spi->dev, "Invalid chan address:%ld", in ltc2983_read_raw()
1230 chan->address); in ltc2983_read_raw()
1231 return -EINVAL; in ltc2983_read_raw()
1236 mutex_lock(&st->lock); in ltc2983_read_raw()
1237 ret = ltc2983_chan_read(st, st->sensors[chan->address], val); in ltc2983_read_raw()
1238 mutex_unlock(&st->lock); in ltc2983_read_raw()
1241 switch (chan->type) { in ltc2983_read_raw()
1255 return -EINVAL; in ltc2983_read_raw()
1259 return -EINVAL; in ltc2983_read_raw()
1270 return regmap_read(st->regmap, reg, readval); in ltc2983_reg_access()
1272 return regmap_write(st->regmap, reg, writeval); in ltc2983_reg_access()
1279 complete(&st->completion); in ltc2983_irq_handler()
1297 struct device *dev = &st->spi->dev; in ltc2983_parse_fw()
1300 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config); in ltc2983_parse_fw()
1302 device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq); in ltc2983_parse_fw()
1304 st->num_channels = device_get_child_node_count(dev); in ltc2983_parse_fw()
1305 if (!st->num_channels) in ltc2983_parse_fw()
1306 return dev_err_probe(&st->spi->dev, -EINVAL, in ltc2983_parse_fw()
1309 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors), in ltc2983_parse_fw()
1311 if (!st->sensors) in ltc2983_parse_fw()
1312 return -ENOMEM; in ltc2983_parse_fw()
1314 st->iio_channels = st->num_channels; in ltc2983_parse_fw()
1316 struct ltc2983_sensor sensor; in ltc2983_parse_fw() local
1318 ret = fwnode_property_read_u32(child, "reg", &sensor.chan); in ltc2983_parse_fw()
1324 if (sensor.chan < LTC2983_MIN_CHANNELS_NR || in ltc2983_parse_fw()
1325 sensor.chan > st->info->max_channels_nr) in ltc2983_parse_fw()
1326 return dev_err_probe(dev, -EINVAL, in ltc2983_parse_fw()
1328 sensor.chan, in ltc2983_parse_fw()
1330 st->info->max_channels_nr); in ltc2983_parse_fw()
1332 if (channel_avail_mask & BIT(sensor.chan)) in ltc2983_parse_fw()
1333 return dev_err_probe(dev, -EINVAL, in ltc2983_parse_fw()
1335 sensor.chan); in ltc2983_parse_fw()
1337 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type); in ltc2983_parse_fw()
1340 "adi,sensor-type property must given for child nodes\n"); in ltc2983_parse_fw()
1342 dev_dbg(dev, "Create new sensor, type %u, chann %u", in ltc2983_parse_fw()
1343 sensor.type, sensor.chan); in ltc2983_parse_fw()
1345 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE && in ltc2983_parse_fw()
1346 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { in ltc2983_parse_fw()
1347 st->sensors[chan] = ltc2983_thermocouple_new(child, st, in ltc2983_parse_fw()
1348 &sensor); in ltc2983_parse_fw()
1349 } else if (sensor.type >= LTC2983_SENSOR_RTD && in ltc2983_parse_fw()
1350 sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) { in ltc2983_parse_fw()
1351 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor); in ltc2983_parse_fw()
1352 } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR && in ltc2983_parse_fw()
1353 sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) { in ltc2983_parse_fw()
1354 st->sensors[chan] = ltc2983_thermistor_new(child, st, in ltc2983_parse_fw()
1355 &sensor); in ltc2983_parse_fw()
1356 } else if (sensor.type == LTC2983_SENSOR_DIODE) { in ltc2983_parse_fw()
1357 st->sensors[chan] = ltc2983_diode_new(child, st, in ltc2983_parse_fw()
1358 &sensor); in ltc2983_parse_fw()
1359 } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) { in ltc2983_parse_fw()
1360 st->sensors[chan] = ltc2983_r_sense_new(child, st, in ltc2983_parse_fw()
1361 &sensor); in ltc2983_parse_fw()
1363 st->iio_channels--; in ltc2983_parse_fw()
1364 } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) { in ltc2983_parse_fw()
1365 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor); in ltc2983_parse_fw()
1366 } else if (st->info->has_temp && in ltc2983_parse_fw()
1367 sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) { in ltc2983_parse_fw()
1368 st->sensors[chan] = ltc2983_temp_new(child, st, &sensor); in ltc2983_parse_fw()
1370 return dev_err_probe(dev, -EINVAL, in ltc2983_parse_fw()
1371 "Unknown sensor type %d\n", in ltc2983_parse_fw()
1372 sensor.type); in ltc2983_parse_fw()
1375 if (IS_ERR(st->sensors[chan])) in ltc2983_parse_fw()
1376 return dev_err_probe(dev, PTR_ERR(st->sensors[chan]), in ltc2983_parse_fw()
1377 "Failed to create sensor\n"); in ltc2983_parse_fw()
1379 /* set generic sensor parameters */ in ltc2983_parse_fw()
1380 st->sensors[chan]->chan = sensor.chan; in ltc2983_parse_fw()
1381 st->sensors[chan]->type = sensor.type; in ltc2983_parse_fw()
1383 channel_avail_mask |= BIT(sensor.chan); in ltc2983_parse_fw()
1398 ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG, in ltc2983_eeprom_cmd()
1399 &st->eeprom_key, sizeof(st->eeprom_key)); in ltc2983_eeprom_cmd()
1403 reinit_completion(&st->completion); in ltc2983_eeprom_cmd()
1405 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, in ltc2983_eeprom_cmd()
1410 time = wait_for_completion_timeout(&st->completion, in ltc2983_eeprom_cmd()
1413 return dev_err_probe(&st->spi->dev, -ETIMEDOUT, in ltc2983_eeprom_cmd()
1416 ret = regmap_read(st->regmap, status_reg, &val); in ltc2983_eeprom_cmd()
1421 return dev_err_probe(&st->spi->dev, -EINVAL, in ltc2983_eeprom_cmd()
1433 ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status, in ltc2983_setup()
1437 return dev_err_probe(&st->spi->dev, ret, in ltc2983_setup()
1440 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG, in ltc2983_setup()
1442 LTC2983_NOTCH_FREQ(st->filter_notch_freq)); in ltc2983_setup()
1446 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG, in ltc2983_setup()
1447 st->mux_delay_config); in ltc2983_setup()
1451 if (st->info->has_eeprom && !assign_iio) { in ltc2983_setup()
1460 for (chan = 0; chan < st->num_channels; chan++) { in ltc2983_setup()
1463 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]); in ltc2983_setup()
1469 * re-configure the device channels. in ltc2983_setup()
1472 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR || in ltc2983_setup()
1477 if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) { in ltc2983_setup()
1487 * reference the sensor given the iio_chan_spec in ltc2983_setup()
1489 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++, in ltc2983_setup()
1543 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in ltc2983_probe()
1545 return -ENOMEM; in ltc2983_probe()
1549 st->info = spi_get_device_match_data(spi); in ltc2983_probe()
1550 if (!st->info) in ltc2983_probe()
1551 return -ENODEV; in ltc2983_probe()
1553 st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config); in ltc2983_probe()
1554 if (IS_ERR(st->regmap)) in ltc2983_probe()
1555 return dev_err_probe(&spi->dev, PTR_ERR(st->regmap), in ltc2983_probe()
1558 mutex_init(&st->lock); in ltc2983_probe()
1559 init_completion(&st->completion); in ltc2983_probe()
1560 st->spi = spi; in ltc2983_probe()
1561 st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY); in ltc2983_probe()
1568 ret = devm_regulator_get_enable(&spi->dev, "vdd"); in ltc2983_probe()
1572 gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH); in ltc2983_probe()
1582 st->iio_chan = devm_kzalloc(&spi->dev, in ltc2983_probe()
1583 st->iio_channels * sizeof(*st->iio_chan), in ltc2983_probe()
1585 if (!st->iio_chan) in ltc2983_probe()
1586 return -ENOMEM; in ltc2983_probe()
1592 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler, in ltc2983_probe()
1593 IRQF_TRIGGER_RISING, st->info->name, st); in ltc2983_probe()
1595 return dev_err_probe(&spi->dev, ret, in ltc2983_probe()
1598 if (st->info->has_eeprom) { in ltc2983_probe()
1607 indio_dev->name = st->info->name; in ltc2983_probe()
1608 indio_dev->num_channels = st->iio_channels; in ltc2983_probe()
1609 indio_dev->channels = st->iio_chan; in ltc2983_probe()
1610 indio_dev->modes = INDIO_DIRECT_MODE; in ltc2983_probe()
1611 indio_dev->info = <c2983_iio_info; in ltc2983_probe()
1613 return devm_iio_device_register(&spi->dev, indio_dev); in ltc2983_probe()
1622 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy); in ltc2983_resume()
1623 /* we need to re-assign the channels */ in ltc2983_resume()
1631 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP); in ltc2983_suspend()