1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for ina233 4 * 5 * Copyright (c) 2025 Leo Yang 6 */ 7 8 #include <linux/err.h> 9 #include <linux/i2c.h> 10 #include <linux/init.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include "pmbus.h" 14 15 #define MFR_READ_VSHUNT 0xd1 16 #define MFR_CALIBRATION 0xd4 17 18 #define INA233_MAX_CURRENT_DEFAULT 32768000 /* uA */ 19 #define INA233_RSHUNT_DEFAULT 2000 /* uOhm */ 20 21 #define MAX_M_VAL 32767 22 23 static void calculate_coef(int *m, int *R, u32 current_lsb, int power_coef) 24 { 25 u64 scaled_m; 26 int scale_factor = 0; 27 int scale_coef = 1; 28 29 /* 30 * 1000000 from Current_LSB A->uA . 31 * scale_coef is for scaling up to minimize rounding errors, 32 * If there is no decimal information, no need to scale. 33 */ 34 if (1000000 % current_lsb) { 35 /* Scaling to keep integer precision */ 36 scale_factor = -3; 37 scale_coef = 1000; 38 } 39 40 /* 41 * Unit Conversion (Current_LSB A->uA) and use scaling(scale_factor) 42 * to keep integer precision. 43 * Formulae referenced from spec. 44 */ 45 scaled_m = div64_u64(1000000 * scale_coef, (u64)current_lsb * power_coef); 46 47 /* Maximize while keeping it bounded.*/ 48 while (scaled_m > MAX_M_VAL) { 49 scaled_m = div_u64(scaled_m, 10); 50 scale_factor++; 51 } 52 /* Scale up only if fractional part exists. */ 53 while (scaled_m * 10 < MAX_M_VAL && scale_coef != 1) { 54 scaled_m *= 10; 55 scale_factor--; 56 } 57 58 *m = scaled_m; 59 *R = scale_factor; 60 } 61 62 static int ina233_read_word_data(struct i2c_client *client, int page, 63 int phase, int reg) 64 { 65 int ret; 66 67 switch (reg) { 68 case PMBUS_VIRT_READ_VMON: 69 ret = pmbus_read_word_data(client, 0, 0xff, MFR_READ_VSHUNT); 70 if (ret < 0) 71 return ret; 72 73 /* Adjust returned value to match VIN coefficients */ 74 /* VIN: 1.25 mV VSHUNT: 2.5 uV LSB */ 75 ret = clamp_val(DIV_ROUND_CLOSEST((s16)ret * 25, 12500), 76 S16_MIN, S16_MAX) & 0xffff; 77 break; 78 default: 79 ret = -ENODATA; 80 break; 81 } 82 return ret; 83 } 84 85 static int ina233_probe(struct i2c_client *client) 86 { 87 struct device *dev = &client->dev; 88 const char *propname; 89 int ret, m, R; 90 u32 rshunt; 91 u32 max_current; 92 u32 current_lsb; 93 u16 calibration; 94 struct pmbus_driver_info *info; 95 96 info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), 97 GFP_KERNEL); 98 if (!info) 99 return -ENOMEM; 100 101 info->pages = 1; 102 info->format[PSC_VOLTAGE_IN] = direct; 103 info->format[PSC_VOLTAGE_OUT] = direct; 104 info->format[PSC_CURRENT_OUT] = direct; 105 info->format[PSC_POWER] = direct; 106 info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_INPUT 107 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT 108 | PMBUS_HAVE_POUT 109 | PMBUS_HAVE_VMON | PMBUS_HAVE_STATUS_VMON; 110 info->m[PSC_VOLTAGE_IN] = 8; 111 info->R[PSC_VOLTAGE_IN] = 2; 112 info->m[PSC_VOLTAGE_OUT] = 8; 113 info->R[PSC_VOLTAGE_OUT] = 2; 114 info->read_word_data = ina233_read_word_data; 115 116 /* If INA233 skips current/power, shunt-resistor and current-lsb aren't needed. */ 117 /* read rshunt value (uOhm) */ 118 propname = "shunt-resistor"; 119 if (device_property_present(dev, propname)) { 120 ret = device_property_read_u32(dev, propname, &rshunt); 121 if (ret) 122 return dev_err_probe(dev, ret, "%s property read fail.\n", propname); 123 } else { 124 rshunt = INA233_RSHUNT_DEFAULT; 125 } 126 if (!rshunt) 127 return dev_err_probe(dev, -EINVAL, "%s cannot be zero.\n", propname); 128 129 /* read Maximum expected current value (uA) */ 130 propname = "ti,maximum-expected-current-microamp"; 131 if (device_property_present(dev, propname)) { 132 ret = device_property_read_u32(dev, propname, &max_current); 133 if (ret) 134 return dev_err_probe(dev, ret, "%s property read fail.\n", propname); 135 } else { 136 max_current = INA233_MAX_CURRENT_DEFAULT; 137 } 138 if (max_current < 32768) 139 return dev_err_probe(dev, -EINVAL, "%s cannot be less than 32768.\n", propname); 140 141 /* Calculate Current_LSB according to the spec formula */ 142 current_lsb = max_current / 32768; 143 144 /* calculate current coefficient */ 145 calculate_coef(&m, &R, current_lsb, 1); 146 info->m[PSC_CURRENT_OUT] = m; 147 info->R[PSC_CURRENT_OUT] = R; 148 149 /* calculate power coefficient */ 150 calculate_coef(&m, &R, current_lsb, 25); 151 info->m[PSC_POWER] = m; 152 info->R[PSC_POWER] = R; 153 154 /* write MFR_CALIBRATION register, Apply formula from spec with unit scaling. */ 155 calibration = div64_u64(5120000000ULL, (u64)rshunt * current_lsb); 156 if (calibration > 0x7FFF) 157 return dev_err_probe(dev, -EINVAL, 158 "Product of Current_LSB %u and shunt resistor %u too small, MFR_CALIBRATION reg exceeds 0x7FFF.\n", 159 current_lsb, rshunt); 160 ret = i2c_smbus_write_word_data(client, MFR_CALIBRATION, calibration); 161 if (ret < 0) 162 return dev_err_probe(dev, ret, "Unable to write calibration.\n"); 163 164 dev_dbg(dev, "power monitor %s (Rshunt = %u uOhm, Current_LSB = %u uA/bit)\n", 165 client->name, rshunt, current_lsb); 166 167 return pmbus_do_probe(client, info); 168 } 169 170 static const struct i2c_device_id ina233_id[] = { 171 {"ina233", 0}, 172 {} 173 }; 174 MODULE_DEVICE_TABLE(i2c, ina233_id); 175 176 static const struct of_device_id __maybe_unused ina233_of_match[] = { 177 { .compatible = "ti,ina233" }, 178 {} 179 }; 180 MODULE_DEVICE_TABLE(of, ina233_of_match); 181 182 static struct i2c_driver ina233_driver = { 183 .driver = { 184 .name = "ina233", 185 .of_match_table = of_match_ptr(ina233_of_match), 186 }, 187 .probe = ina233_probe, 188 .id_table = ina233_id, 189 }; 190 191 module_i2c_driver(ina233_driver); 192 193 MODULE_AUTHOR("Leo Yang <leo.yang.sy0@gmail.com>"); 194 MODULE_DESCRIPTION("PMBus driver for INA233 and compatible chips"); 195 MODULE_LICENSE("GPL"); 196 MODULE_IMPORT_NS("PMBUS"); 197