On 5/19/20 10:19 AM, Dan Murphy wrote:ACK. I am going to rewrite that altogether.
+static int bq2515x_set_ilim_lvl(struct bq2515x_device *bq2515x, int val)Clamp to these limits, not reject. Or better, modify the below loop so
+{
+ int i;
+
+ if (val > BQ2515X_ILIM_MAX || val < BQ2515X_ILIM_MIN)
+ return -EINVAL;
it clamps to the highest or lowest value in bq2515x_ilim_lvl_values[],
then drop these #defines.
+Index out of bounds for the i = 0 case.
+ for (i = 0; i < ARRAY_SIZE(bq2515x_ilim_lvl_values); i++) {
+ if (val == bq2515x_ilim_lvl_values[i])
+ break;
+
+ if (val > bq2515x_ilim_lvl_values[i - 1] &&
+ val < bq2515x_ilim_lvl_values[i]) {You are still missing the case where the value is closer to the [i]
+ if (val - bq2515x_ilim_lvl_values[i - 1] <
+ bq2515x_ilim_lvl_values[i] - val) {
element, you check that it is between [i-1] and [i], but only chose
[i-1] when it is closer to that than [i] but equal and greater case is
missing.
Given this sets input current limits, would instead always rounding down
be the safer option?
Andrew
+ i = i - 1;
+ break;
+ }
+ }
+ }
+
+ return regmap_write(bq2515x->regmap, BQ2515X_ILIMCTRL, i);
+}