/* Return the value from the given register in uW, mV, or mA */ staticlonglong ltc2945_reg_to_val(struct device *dev, u8 reg)
{ struct ltc2945_data *data = dev_get_drvdata(dev); struct regmap *regmap = data->regmap;
u32 shunt_resistor = data->shunt_resistor; unsignedint control;
u8 buf[3]; longlong val; int ret;
ret = regmap_bulk_read(regmap, reg, buf,
is_power_reg(reg) ? 3 : 2); if (ret < 0) return ret;
if (is_power_reg(reg)) { /* 24-bit power */
val = (buf[0] << 16) + (buf[1] << 8) + buf[2];
} else { /* 12-bit current, voltage */
val = (buf[0] << 4) + (buf[1] >> 4);
}
switch (reg) { case LTC2945_POWER_H: case LTC2945_MAX_POWER_H: case LTC2945_MIN_POWER_H: case LTC2945_MAX_POWER_THRES_H: case LTC2945_MIN_POWER_THRES_H: /* * Convert to uW * Control register bit 0 selects if voltage at SENSE+/VDD * or voltage at ADIN is used to measure power.
*/
ret = regmap_read(regmap, LTC2945_CONTROL, &control); if (ret < 0) return ret; if (control & CONTROL_MULT_SELECT) { /* 25 mV * 25 uV = 0.625 uV resolution. */
val *= 625LL;
} else { /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
val = (val * 25LL) >> 1;
}
val *= 1000; /* Overflow check: Assuming max 24-bit power, val is at most 53 bits right now. */
val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor); /* * Overflow check: After division, depending on shunt resistor, * val can still be > 32 bits so returning long long makes sense
*/
break; case LTC2945_VIN_H: case LTC2945_MAX_VIN_H: case LTC2945_MIN_VIN_H: case LTC2945_MAX_VIN_THRES_H: case LTC2945_MIN_VIN_THRES_H: /* 25 mV resolution. Convert to mV. */
val *= 25; break; case LTC2945_ADIN_H: case LTC2945_MAX_ADIN_H: case LTC2945_MIN_ADIN_THRES_H: case LTC2945_MAX_ADIN_THRES_H: case LTC2945_MIN_ADIN_H: /* 0.5mV resolution. Convert to mV. */
val = val >> 1; break; case LTC2945_SENSE_H: case LTC2945_MAX_SENSE_H: case LTC2945_MIN_SENSE_H: case LTC2945_MAX_SENSE_THRES_H: case LTC2945_MIN_SENSE_THRES_H: /* 25 uV resolution. Convert to mA. */
val *= 25 * 1000; /* Overflow check: Assuming max 12-bit sense, val is at most 27 bits right now */
val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor); /* Overflow check: After division, <= 27 bits */ break; default: return -EINVAL;
} return val;
}
/* Ensure we don't overflow */
val = clamp_val(val, 0, U32_MAX);
switch (reg) { case LTC2945_POWER_H: case LTC2945_MAX_POWER_H: case LTC2945_MIN_POWER_H: case LTC2945_MAX_POWER_THRES_H: case LTC2945_MIN_POWER_THRES_H: /* * Control register bit 0 selects if voltage at SENSE+/VDD * or voltage at ADIN is used to measure power, which in turn * determines register calculations.
*/
ret = regmap_read(regmap, LTC2945_CONTROL, &control); if (ret < 0) return ret; if (control & CONTROL_MULT_SELECT) { /* 25 mV * 25 uV = 0.625 uV resolution. */
val *= shunt_resistor; /* Overflow check: Assuming 32-bit val and shunt resistor, val <= 64bits */
val = DIV_ROUND_CLOSEST_ULL(val, 625 * 1000); /* Overflow check: val is now <= 44 bits */
} else { /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
val *= shunt_resistor; /* Overflow check: Assuming 32-bit val and shunt resistor, val <= 64bits */
val = DIV_ROUND_CLOSEST_ULL(val, 25 * 1000) * 2; /* Overflow check: val is now <= 51 bits */
} break; case LTC2945_VIN_H: case LTC2945_MAX_VIN_H: case LTC2945_MIN_VIN_H: case LTC2945_MAX_VIN_THRES_H: case LTC2945_MIN_VIN_THRES_H: /* 25 mV resolution. */
val = DIV_ROUND_CLOSEST_ULL(val, 25); break; case LTC2945_ADIN_H: case LTC2945_MAX_ADIN_H: case LTC2945_MIN_ADIN_THRES_H: case LTC2945_MAX_ADIN_THRES_H: case LTC2945_MIN_ADIN_H: /* 0.5mV resolution. */
val *= 2; break; case LTC2945_SENSE_H: case LTC2945_MAX_SENSE_H: case LTC2945_MIN_SENSE_H: case LTC2945_MAX_SENSE_THRES_H: case LTC2945_MIN_SENSE_THRES_H: /* 25 uV resolution. Convert to mA. */
val *= shunt_resistor; /* Overflow check: Assuming 32-bit val and 32-bit shunt resistor, val is 64bits */
val = DIV_ROUND_CLOSEST_ULL(val, 25 * 1000); /* Overflow check: val is now <= 50 bits */ break; default: return -EINVAL;
} return val;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.