On 5/19/20 9:46 AM, Andrew F. Davis wrote:
On 5/19/20 10:19 AM, Dan Murphy wrote:
+static int bq2515x_set_ilim_lvl(struct bq2515x_device *bq2515x, int val)
+{
+ int i;
+
+ if (val > BQ2515X_ILIM_MAX || val < BQ2515X_ILIM_MIN)
+ return -EINVAL;
Clamp to these limits, not reject. Or better, modify the below loop so
it clamps to the highest or lowest value in bq2515x_ilim_lvl_values[],
then drop these #defines.
+
+ 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] &&
Index out of bounds for the i = 0 case.
+ val < bq2515x_ilim_lvl_values[i]) {
+ if (val - bq2515x_ilim_lvl_values[i - 1] <
+ bq2515x_ilim_lvl_values[i] - val) {
You are still missing the case where the value is closer to the [i]
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
ACK. I am going to rewrite that altogether.
+ i = i - 1;
+ break;
+ }
+ }
+ }
+
+ return regmap_write(bq2515x->regmap, BQ2515X_ILIMCTRL, i);
+}