Skip to content

Commit f094fea

Browse files
committed
regmap: Use bsearch() to search the register defaults
Rather than open coding a binary search use the standard bsearch() using the comparison function we're already using for sort() on insert. This fixes a lockup I was observing due to iterating on min <= max rather than min < max when we fail to look up. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Acked-by: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
1 parent 0eef6b0 commit f094fea

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

‎drivers/base/regmap/regcache.c‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <linux/slab.h>
1414
#include <trace/events/regmap.h>
15+
#include <linux/bsearch.h>
1516
#include <linux/sort.h>
1617

1718
#include "internal.h"
@@ -355,32 +356,31 @@ unsigned int regcache_get_val(const void *base, unsigned int idx,
355356
return -1;
356357
}
357358

358-
int regcache_lookup_reg(struct regmap *map, unsigned int reg)
359-
{
360-
unsigned int min, max, index;
361-
362-
min = 0;
363-
max = map->num_reg_defaults - 1;
364-
do {
365-
index = (min + max) / 2;
366-
if (map->reg_defaults[index].reg == reg)
367-
return index;
368-
if (map->reg_defaults[index].reg < reg)
369-
min = index + 1;
370-
else
371-
max = index;
372-
} while (min <= max);
373-
return -1;
374-
}
375-
376-
static int regcache_insert_cmp(const void *a, const void *b)
359+
static int regcache_default_cmp(const void *a, const void *b)
377360
{
378361
const struct reg_default *_a = a;
379362
const struct reg_default *_b = b;
380363

381364
return _a->reg - _b->reg;
382365
}
383366

367+
int regcache_lookup_reg(struct regmap *map, unsigned int reg)
368+
{
369+
struct reg_default key;
370+
struct reg_default *r;
371+
372+
key.reg = reg;
373+
key.def = 0;
374+
375+
r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
376+
sizeof(struct reg_default), regcache_default_cmp);
377+
378+
if (r)
379+
return r - map->reg_defaults;
380+
else
381+
return -1;
382+
}
383+
384384
int regcache_insert_reg(struct regmap *map, unsigned int reg,
385385
unsigned int val)
386386
{
@@ -396,6 +396,6 @@ int regcache_insert_reg(struct regmap *map, unsigned int reg,
396396
map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
397397
map->reg_defaults[map->num_reg_defaults - 1].def = val;
398398
sort(map->reg_defaults, map->num_reg_defaults,
399-
sizeof(struct reg_default), regcache_insert_cmp, NULL);
399+
sizeof(struct reg_default), regcache_default_cmp, NULL);
400400
return 0;
401401
}

0 commit comments

Comments
 (0)