Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

hwmon: (ina238) Only configure calibration and shunt registers if needed

Prepare for supporting chips with internal shunt resistor by only setting
calibration and shunt resistor registers if no current LSB is configured.

Do not display a log message during probe if a chip does not have shunt
and gain registers since those would otherwise display 0, and a message
just indicating that the driver was loaded would be just noise.

Reviewed-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
Tested-by: Chris Packham <chris.packham@alliedtelesis.co.nz> # INA780
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

+42 -40
+42 -40
drivers/hwmon/ina238.c
··· 745 745 return PTR_ERR(data->regmap); 746 746 } 747 747 748 - /* load shunt value */ 749 - if (device_property_read_u32(dev, "shunt-resistor", &data->rshunt) < 0) 750 - data->rshunt = INA238_RSHUNT_DEFAULT; 751 - if (data->rshunt == 0) { 752 - dev_err(dev, "invalid shunt resister value %u\n", data->rshunt); 753 - return -EINVAL; 754 - } 755 - 756 - /* load shunt gain value */ 757 - if (device_property_read_u32(dev, "ti,shunt-gain", &data->gain) < 0) 758 - data->gain = 4; /* Default of ADCRANGE = 0 */ 759 - if (data->gain != 1 && data->gain != 2 && data->gain != 4) { 760 - dev_err(dev, "invalid shunt gain value %u\n", data->gain); 761 - return -EINVAL; 762 - } 763 - 764 748 /* Setup CONFIG register */ 765 749 config = data->config->config_default; 766 - if (chip == sq52206) { 767 - if (data->gain == 1) 768 - config |= SQ52206_CONFIG_ADCRANGE_HIGH; /* ADCRANGE = 10/11 is /1 */ 769 - else if (data->gain == 2) 770 - config |= SQ52206_CONFIG_ADCRANGE_LOW; /* ADCRANGE = 01 is /2 */ 771 - } else if (data->gain == 1) { 772 - config |= INA238_CONFIG_ADCRANGE; /* ADCRANGE = 1 is /1 */ 750 + if (data->config->current_lsb) { 751 + data->voltage_lsb[0] = INA238_SHUNT_VOLTAGE_LSB; 752 + data->current_lsb = data->config->current_lsb; 753 + } else { 754 + /* load shunt value */ 755 + if (device_property_read_u32(dev, "shunt-resistor", &data->rshunt) < 0) 756 + data->rshunt = INA238_RSHUNT_DEFAULT; 757 + if (data->rshunt == 0) { 758 + dev_err(dev, "invalid shunt resister value %u\n", data->rshunt); 759 + return -EINVAL; 760 + } 761 + 762 + /* load shunt gain value */ 763 + if (device_property_read_u32(dev, "ti,shunt-gain", &data->gain) < 0) 764 + data->gain = 4; /* Default of ADCRANGE = 0 */ 765 + if (data->gain != 1 && data->gain != 2 && data->gain != 4) { 766 + dev_err(dev, "invalid shunt gain value %u\n", data->gain); 767 + return -EINVAL; 768 + } 769 + 770 + /* Setup SHUNT_CALIBRATION register with fixed value */ 771 + ret = regmap_write(data->regmap, INA238_SHUNT_CALIBRATION, 772 + INA238_CALIBRATION_VALUE); 773 + if (ret < 0) { 774 + dev_err(dev, "error configuring the device: %d\n", ret); 775 + return -ENODEV; 776 + } 777 + if (chip == sq52206) { 778 + if (data->gain == 1) /* ADCRANGE = 10/11 is /1 */ 779 + config |= SQ52206_CONFIG_ADCRANGE_HIGH; 780 + else if (data->gain == 2) /* ADCRANGE = 01 is /2 */ 781 + config |= SQ52206_CONFIG_ADCRANGE_LOW; 782 + } else if (data->gain == 1) { /* ADCRANGE = 1 is /1 */ 783 + config |= INA238_CONFIG_ADCRANGE; 784 + } 785 + data->voltage_lsb[0] = INA238_SHUNT_VOLTAGE_LSB * data->gain / 4; 786 + data->current_lsb = DIV_U64_ROUND_CLOSEST(250ULL * INA238_FIXED_SHUNT * data->gain, 787 + data->rshunt); 773 788 } 789 + 774 790 ret = regmap_write(data->regmap, INA238_CONFIG, config); 775 791 if (ret < 0) { 776 792 dev_err(dev, "error configuring the device: %d\n", ret); ··· 796 780 /* Setup ADC_CONFIG register */ 797 781 ret = regmap_write(data->regmap, INA238_ADC_CONFIG, 798 782 INA238_ADC_CONFIG_DEFAULT); 799 - if (ret < 0) { 800 - dev_err(dev, "error configuring the device: %d\n", ret); 801 - return -ENODEV; 802 - } 803 - 804 - /* Setup SHUNT_CALIBRATION register with fixed value */ 805 - ret = regmap_write(data->regmap, INA238_SHUNT_CALIBRATION, 806 - INA238_CALIBRATION_VALUE); 807 783 if (ret < 0) { 808 784 dev_err(dev, "error configuring the device: %d\n", ret); 809 785 return -ENODEV; ··· 812 804 return -ENODEV; 813 805 } 814 806 815 - data->voltage_lsb[0] = INA238_SHUNT_VOLTAGE_LSB * data->gain / 4; 816 807 data->voltage_lsb[1] = data->config->bus_voltage_lsb; 817 - 818 - if (data->config->current_lsb) 819 - data->current_lsb = data->config->current_lsb; 820 - else 821 - data->current_lsb = DIV_U64_ROUND_CLOSEST(250ULL * INA238_FIXED_SHUNT * data->gain, 822 - data->rshunt); 823 808 824 809 data->power_lsb = DIV_ROUND_CLOSEST(data->current_lsb * 825 810 data->config->power_calculate_factor, ··· 825 824 if (IS_ERR(hwmon_dev)) 826 825 return PTR_ERR(hwmon_dev); 827 826 828 - dev_info(dev, "power monitor %s (Rshunt = %u uOhm, gain = %u)\n", 829 - client->name, data->rshunt, data->gain); 827 + if (data->rshunt) 828 + dev_info(dev, "power monitor %s (Rshunt = %u uOhm, gain = %u)\n", 829 + client->name, data->rshunt, data->gain); 830 830 831 831 return 0; 832 832 }