Lines Matching +full:shunt +full:- +full:resistor +full:- +full:micro +full:- +full:ohms

1 // SPDX-License-Identifier: GPL-2.0
7 * Local Temperature Sensor and Current Shunt Monitor
12 * and Local Temperature Sensor and Current Shunt Monitor
15 * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com>
110 // Max possible value is -256 to +256 but datasheet indicated -40 to 125.
112 #define MIN_TEMP_LIMIT -40000
179 return 5 - ffs(data->pga_gain); in tmp51x_get_pga_shift()
198 *val = sign_extend32(regval, 17 - tmp51x_get_pga_shift(data)); in tmp51x_get_value()
199 *val = DIV_ROUND_CLOSEST(*val * 10000, data->shunt_uohms); in tmp51x_get_value()
210 *val = regval * data->pwr_lsb_uw; in tmp51x_get_value()
214 *val = sign_extend32(regval, 16) * data->curr_lsb_ua; in tmp51x_get_value()
237 return -EOPNOTSUPP; in tmp51x_get_value()
255 val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10000); in tmp51x_set_value()
257 regval = clamp_val(val, -max_val, max_val); in tmp51x_set_value()
262 max_val = (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) ? in tmp51x_set_value()
269 regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0, in tmp51x_set_value()
289 return -EOPNOTSUPP; in tmp51x_set_value()
293 return regmap_write(data->regmap, reg, regval); in tmp51x_set_value()
295 return regmap_update_bits(data->regmap, reg, mask, regval); in tmp51x_set_value()
405 return -EOPNOTSUPP; in tmp51x_read()
410 ret = regmap_read(data->regmap, reg, &regval); in tmp51x_read()
424 return -EOPNOTSUPP; in tmp51x_write()
437 if (data->id == tmp512 && channel == 4) in tmp51x_is_visible()
463 if (!data->shunt_uohms) in tmp51x_is_visible()
477 if (!data->shunt_uohms) in tmp51x_is_visible()
532 int vshunt_max = data->pga_gain * 40; in tmp51x_calibrate()
542 if (data->shunt_uohms == 0) in tmp51x_calibrate()
543 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0); in tmp51x_calibrate()
546 data->shunt_uohms); in tmp51x_calibrate()
552 data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * 1000, 32767); in tmp51x_calibrate()
553 data->pwr_lsb_uw = 20 * data->curr_lsb_ua; in tmp51x_calibrate()
555 div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, in tmp51x_calibrate()
558 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, in tmp51x_calibrate()
568 int ret = regmap_write(data->regmap, TMP51X_SHUNT_CONFIG, in tmp51x_init()
569 data->shunt_config); in tmp51x_init()
573 ret = regmap_write(data->regmap, TMP51X_TEMP_CONFIG, data->temp_config); in tmp51x_init()
578 ret = regmap_update_bits(data->regmap, TMP51X_N_FACTOR_AND_HYST_1, in tmp51x_init()
579 TMP51X_NFACTOR_MASK, data->nfactor[0] << 8); in tmp51x_init()
583 ret = regmap_write(data->regmap, TMP51X_N_FACTOR_2, in tmp51x_init()
584 data->nfactor[1] << 8); in tmp51x_init()
588 if (data->id == tmp513) { in tmp51x_init()
589 ret = regmap_write(data->regmap, TMP513_N_FACTOR_3, in tmp51x_init()
590 data->nfactor[2] << 8); in tmp51x_init()
600 return regmap_read(data->regmap, TMP51X_STATUS, &regval); in tmp51x_init()
626 if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) { in tmp51x_vbus_range_to_reg()
627 data->shunt_config |= TMP51X_BUS_VOLTAGE_MASK; in tmp51x_vbus_range_to_reg()
628 } else if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_16V) { in tmp51x_vbus_range_to_reg()
629 data->shunt_config &= ~TMP51X_BUS_VOLTAGE_MASK; in tmp51x_vbus_range_to_reg()
631 dev_err(dev, "ti,bus-range-microvolt is invalid: %u\n", in tmp51x_vbus_range_to_reg()
632 data->vbus_range_uvolt); in tmp51x_vbus_range_to_reg()
633 return -EINVAL; in tmp51x_vbus_range_to_reg()
640 if (data->pga_gain == 8) { in tmp51x_pga_gain_to_reg()
641 data->shunt_config |= CURRENT_SENSE_VOLTAGE_320_MASK; in tmp51x_pga_gain_to_reg()
642 } else if (data->pga_gain == 4) { in tmp51x_pga_gain_to_reg()
643 data->shunt_config |= CURRENT_SENSE_VOLTAGE_160_MASK; in tmp51x_pga_gain_to_reg()
644 } else if (data->pga_gain == 2) { in tmp51x_pga_gain_to_reg()
645 data->shunt_config |= CURRENT_SENSE_VOLTAGE_80_MASK; in tmp51x_pga_gain_to_reg()
646 } else if (data->pga_gain == 1) { in tmp51x_pga_gain_to_reg()
647 data->shunt_config |= CURRENT_SENSE_VOLTAGE_40_MASK; in tmp51x_pga_gain_to_reg()
649 dev_err(dev, "ti,pga-gain is invalid: %u\n", data->pga_gain); in tmp51x_pga_gain_to_reg()
650 return -EINVAL; in tmp51x_pga_gain_to_reg()
661 ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", &val); in tmp51x_read_properties()
662 data->shunt_uohms = (ret >= 0) ? val : TMP51X_SHUNT_VALUE_DEFAULT; in tmp51x_read_properties()
664 ret = device_property_read_u32(dev, "ti,bus-range-microvolt", &val); in tmp51x_read_properties()
665 data->vbus_range_uvolt = (ret >= 0) ? val : TMP51X_VBUS_RANGE_DEFAULT; in tmp51x_read_properties()
670 ret = device_property_read_u32(dev, "ti,pga-gain", &val); in tmp51x_read_properties()
671 data->pga_gain = (ret >= 0) ? val : TMP51X_PGA_DEFAULT; in tmp51x_read_properties()
677 (data->id == tmp513) ? 3 : 2); in tmp51x_read_properties()
679 memcpy(data->nfactor, nfactor, (data->id == tmp513) ? 3 : 2); in tmp51x_read_properties()
681 // Check if shunt value is compatible with pga-gain in tmp51x_read_properties()
682 if (data->shunt_uohms > data->pga_gain * 40 * 1000 * 1000) { in tmp51x_read_properties()
683 dev_err(dev, "shunt-resistor: %u too big for pga_gain: %u\n", in tmp51x_read_properties()
684 data->shunt_uohms, data->pga_gain); in tmp51x_read_properties()
685 return -EINVAL; in tmp51x_read_properties()
693 data->vbus_range_uvolt = TMP51X_VBUS_RANGE_DEFAULT; in tmp51x_use_default()
694 data->pga_gain = TMP51X_PGA_DEFAULT; in tmp51x_use_default()
695 data->shunt_uohms = TMP51X_SHUNT_VALUE_DEFAULT; in tmp51x_use_default()
700 data->shunt_config = TMP51X_SHUNT_CONFIG_DEFAULT; in tmp51x_configure()
701 data->temp_config = (data->id == tmp513) ? in tmp51x_configure()
704 if (dev->of_node) in tmp51x_configure()
714 struct device *dev = &client->dev; in tmp51x_probe()
721 return -ENOMEM; in tmp51x_probe()
723 if (client->dev.of_node) in tmp51x_probe()
724 data->id = (enum tmp51x_ids)device_get_match_data(&client->dev); in tmp51x_probe()
726 data->id = i2c_match_id(tmp51x_id, client)->driver_data; in tmp51x_probe()
734 data->regmap = devm_regmap_init_i2c(client, &tmp51x_regmap_config); in tmp51x_probe()
735 if (IS_ERR(data->regmap)) { in tmp51x_probe()
737 return PTR_ERR(data->regmap); in tmp51x_probe()
743 return -ENODEV; in tmp51x_probe()
746 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, in tmp51x_probe()
753 dev_dbg(dev, "power monitor %s\n", client->name); in tmp51x_probe()