Lines Matching +full:milli +full:- +full:ohms
1 // SPDX-License-Identifier: GPL-2.0
15 * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com>
115 // Max possible value is -256 to +256 but datasheet indicated -40 to 125.
117 #define MIN_TEMP_LIMIT -40000
181 // Set the shift based on the gain: 8 -> 1, 4 -> 2, 2 -> 3, 1 -> 4
184 return 5 - ffs(data->pga_gain); in tmp51x_get_pga_shift()
205 16 - tmp51x_get_pga_shift(data) : 15); in tmp51x_get_value()
206 *val = DIV_ROUND_CLOSEST(*val * 10 * (long)MILLI, (long)data->shunt_uohms); in tmp51x_get_value()
218 *val = regval * data->pwr_lsb_uw; in tmp51x_get_value()
222 *val = sign_extend32(regval, 15) * (long)data->curr_lsb_ua; in tmp51x_get_value()
223 *val = DIV_ROUND_CLOSEST(*val, (long)MILLI); in tmp51x_get_value()
245 return -EOPNOTSUPP; in tmp51x_get_value()
263 val = DIV_ROUND_CLOSEST(val * (long)data->shunt_uohms, 10 * (long)MILLI); in tmp51x_set_value()
265 regval = clamp_val(val, -max_val, max_val); in tmp51x_set_value()
270 max_val = (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) ? in tmp51x_set_value()
277 regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0, in tmp51x_set_value()
297 return -EOPNOTSUPP; in tmp51x_set_value()
301 return regmap_write(data->regmap, reg, regval); in tmp51x_set_value()
303 return regmap_update_bits(data->regmap, reg, mask, regval); in tmp51x_set_value()
413 return -EOPNOTSUPP; in tmp51x_read()
418 ret = regmap_read(data->regmap, reg, ®val); in tmp51x_read()
432 return -EOPNOTSUPP; in tmp51x_write()
445 if (data->id == tmp512 && channel == 3) in tmp51x_is_visible()
471 if (!data->shunt_uohms) in tmp51x_is_visible()
485 if (!data->shunt_uohms) in tmp51x_is_visible()
540 int vshunt_max = data->pga_gain * 40; in tmp51x_calibrate()
550 if (data->shunt_uohms == 0) in tmp51x_calibrate()
551 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0); in tmp51x_calibrate()
553 max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * MICRO, data->shunt_uohms); in tmp51x_calibrate()
559 data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767); in tmp51x_calibrate()
560 data->pwr_lsb_uw = 20 * data->curr_lsb_ua; in tmp51x_calibrate()
562 div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO); in tmp51x_calibrate()
564 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, in tmp51x_calibrate()
574 int ret = regmap_write(data->regmap, TMP51X_SHUNT_CONFIG, in tmp51x_init()
575 data->shunt_config); in tmp51x_init()
579 ret = regmap_write(data->regmap, TMP51X_TEMP_CONFIG, data->temp_config); in tmp51x_init()
584 ret = regmap_update_bits(data->regmap, TMP51X_N_FACTOR_AND_HYST_1, in tmp51x_init()
585 TMP51X_NFACTOR_MASK, data->nfactor[0] << 8); in tmp51x_init()
589 ret = regmap_write(data->regmap, TMP51X_N_FACTOR_2, in tmp51x_init()
590 data->nfactor[1] << 8); in tmp51x_init()
594 if (data->id == tmp513) { in tmp51x_init()
595 ret = regmap_write(data->regmap, TMP513_N_FACTOR_3, in tmp51x_init()
596 data->nfactor[2] << 8); in tmp51x_init()
606 return regmap_read(data->regmap, TMP51X_STATUS, ®val); in tmp51x_init()
632 if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) { in tmp51x_vbus_range_to_reg()
633 data->shunt_config |= TMP51X_BUS_VOLTAGE_MASK; in tmp51x_vbus_range_to_reg()
634 } else if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_16V) { in tmp51x_vbus_range_to_reg()
635 data->shunt_config &= ~TMP51X_BUS_VOLTAGE_MASK; in tmp51x_vbus_range_to_reg()
637 return dev_err_probe(dev, -EINVAL, in tmp51x_vbus_range_to_reg()
638 "ti,bus-range-microvolt is invalid: %u\n", in tmp51x_vbus_range_to_reg()
639 data->vbus_range_uvolt); in tmp51x_vbus_range_to_reg()
646 if (data->pga_gain == 8) { in tmp51x_pga_gain_to_reg()
647 data->shunt_config |= CURRENT_SENSE_VOLTAGE_320_MASK; in tmp51x_pga_gain_to_reg()
648 } else if (data->pga_gain == 4) { in tmp51x_pga_gain_to_reg()
649 data->shunt_config |= CURRENT_SENSE_VOLTAGE_160_MASK; in tmp51x_pga_gain_to_reg()
650 } else if (data->pga_gain == 2) { in tmp51x_pga_gain_to_reg()
651 data->shunt_config |= CURRENT_SENSE_VOLTAGE_80_MASK; in tmp51x_pga_gain_to_reg()
652 } else if (data->pga_gain == 1) { in tmp51x_pga_gain_to_reg()
653 data->shunt_config |= CURRENT_SENSE_VOLTAGE_40_MASK; in tmp51x_pga_gain_to_reg()
655 return dev_err_probe(dev, -EINVAL, in tmp51x_pga_gain_to_reg()
656 "ti,pga-gain is invalid: %u\n", data->pga_gain); in tmp51x_pga_gain_to_reg()
667 ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", &val); in tmp51x_read_properties()
668 data->shunt_uohms = (ret >= 0) ? val : TMP51X_SHUNT_VALUE_DEFAULT; in tmp51x_read_properties()
670 ret = device_property_read_u32(dev, "ti,bus-range-microvolt", &val); in tmp51x_read_properties()
671 data->vbus_range_uvolt = (ret >= 0) ? val : TMP51X_VBUS_RANGE_DEFAULT; in tmp51x_read_properties()
676 ret = device_property_read_u32(dev, "ti,pga-gain", &val); in tmp51x_read_properties()
677 data->pga_gain = (ret >= 0) ? val : TMP51X_PGA_DEFAULT; in tmp51x_read_properties()
683 (data->id == tmp513) ? 3 : 2); in tmp51x_read_properties()
685 memcpy(data->nfactor, nfactor, (data->id == tmp513) ? 3 : 2); in tmp51x_read_properties()
687 // Check if shunt value is compatible with pga-gain in tmp51x_read_properties()
688 if (data->shunt_uohms > data->pga_gain * 40 * MICRO) { in tmp51x_read_properties()
689 return dev_err_probe(dev, -EINVAL, in tmp51x_read_properties()
690 "shunt-resistor: %u too big for pga_gain: %u\n", in tmp51x_read_properties()
691 data->shunt_uohms, data->pga_gain); in tmp51x_read_properties()
699 data->vbus_range_uvolt = TMP51X_VBUS_RANGE_DEFAULT; in tmp51x_use_default()
700 data->pga_gain = TMP51X_PGA_DEFAULT; in tmp51x_use_default()
701 data->shunt_uohms = TMP51X_SHUNT_VALUE_DEFAULT; in tmp51x_use_default()
706 data->shunt_config = TMP51X_SHUNT_CONFIG_DEFAULT; in tmp51x_configure()
707 data->temp_config = (data->id == tmp513) ? in tmp51x_configure()
710 if (dev->of_node) in tmp51x_configure()
720 struct device *dev = &client->dev; in tmp51x_probe()
727 return -ENOMEM; in tmp51x_probe()
729 data->id = (uintptr_t)i2c_get_match_data(client); in tmp51x_probe()
735 data->regmap = devm_regmap_init_i2c(client, &tmp51x_regmap_config); in tmp51x_probe()
736 if (IS_ERR(data->regmap)) in tmp51x_probe()
737 return dev_err_probe(dev, PTR_ERR(data->regmap), in tmp51x_probe()
744 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, in tmp51x_probe()
751 dev_dbg(dev, "power monitor %s\n", client->name); in tmp51x_probe()