static int ltc4215_get_voltage(struct device *dev, u8 reg) { struct ltc4215_data *data = ltc4215_update_device(dev); const u8 regval = data->regs[reg]; u32 voltage = 0; switch (reg) { case LTC4215_SENSE: voltage = regval * 151 / 1000; break; case LTC4215_SOURCE: voltage = regval * 605 / 10; break; case LTC4215_ADIN: voltage = regval * 482 * 125 / 1000; break; default: WARN_ON_ONCE(1); break; } return voltage; }
/* Return the voltage from the given register in millivolts */ static int ltc4215_get_voltage(struct device *dev, u8 reg) { struct ltc4215_data *data = ltc4215_update_device(dev); const u8 regval = data->regs[reg]; u32 voltage = 0; switch (reg) { case LTC4215_SENSE: /* 151 uV per increment */ voltage = regval * 151 / 1000; break; case LTC4215_SOURCE: /* 60.5 mV per increment */ voltage = regval * 605 / 10; break; case LTC4215_ADIN: /* The ADIN input is divided by 12.5, and has 4.82 mV * per increment, so we have the additional multiply */ voltage = regval * 482 * 125 / 1000; break; default: /* If we get here, the developer messed up */ WARN_ON_ONCE(1); break; } return voltage; }
/* Return the current from the sense resistor in mA */ static unsigned int ltc4215_get_current(struct device *dev) { struct ltc4215_data *data = ltc4215_update_device(dev); /* The strange looking conversions that follow are fixed-point * math, since we cannot do floating point in the kernel. * * Step 1: convert sense register to microVolts * Step 2: convert voltage to milliAmperes * * If you play around with the V=IR equation, you come up with * the following: X uV / Y mOhm == Z mA * * With the resistors that are fractions of a milliOhm, we multiply * the voltage and resistance by 10, to shift the decimal point. * Now we can use the normal division operator again. */ /* Calculate voltage in microVolts (151 uV per increment) */ const unsigned int voltage = data->regs[LTC4215_SENSE] * 151; /* Calculate current in milliAmperes (4 milliOhm sense resistor) */ const unsigned int curr = voltage / 4; return curr; }
static ssize_t ltc4215_show_alarm(struct device *dev, struct device_attribute *da, char *buf) { struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da); struct ltc4215_data *data = ltc4215_update_device(dev); const u8 reg = data->regs[attr->index]; const u32 mask = attr->nr; return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0); }
static unsigned int ltc4215_get_current(struct device *dev) { struct ltc4215_data *data = ltc4215_update_device(dev); const unsigned int voltage = data->regs[LTC4215_SENSE] * 151; const unsigned int curr = voltage / 4; return curr; }