2
0
mirror of https://github.com/edk2-porting/linux-next.git synced 2024-12-17 09:43:59 +08:00

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>
This commit is contained in:
Mark Brown 2011-10-04 22:05:47 +01:00
parent 0eef6b0415
commit f094fea68f

View File

@ -12,6 +12,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <trace/events/regmap.h> #include <trace/events/regmap.h>
#include <linux/bsearch.h>
#include <linux/sort.h> #include <linux/sort.h>
#include "internal.h" #include "internal.h"
@ -355,25 +356,7 @@ unsigned int regcache_get_val(const void *base, unsigned int idx,
return -1; return -1;
} }
int regcache_lookup_reg(struct regmap *map, unsigned int reg) static int regcache_default_cmp(const void *a, const void *b)
{
unsigned int min, max, index;
min = 0;
max = map->num_reg_defaults - 1;
do {
index = (min + max) / 2;
if (map->reg_defaults[index].reg == reg)
return index;
if (map->reg_defaults[index].reg < reg)
min = index + 1;
else
max = index;
} while (min <= max);
return -1;
}
static int regcache_insert_cmp(const void *a, const void *b)
{ {
const struct reg_default *_a = a; const struct reg_default *_a = a;
const struct reg_default *_b = b; const struct reg_default *_b = b;
@ -381,6 +364,23 @@ static int regcache_insert_cmp(const void *a, const void *b)
return _a->reg - _b->reg; return _a->reg - _b->reg;
} }
int regcache_lookup_reg(struct regmap *map, unsigned int reg)
{
struct reg_default key;
struct reg_default *r;
key.reg = reg;
key.def = 0;
r = bsearch(&key, map->reg_defaults, map->num_reg_defaults,
sizeof(struct reg_default), regcache_default_cmp);
if (r)
return r - map->reg_defaults;
else
return -1;
}
int regcache_insert_reg(struct regmap *map, unsigned int reg, int regcache_insert_reg(struct regmap *map, unsigned int reg,
unsigned int val) unsigned int val)
{ {
@ -396,6 +396,6 @@ int regcache_insert_reg(struct regmap *map, unsigned int reg,
map->reg_defaults[map->num_reg_defaults - 1].reg = reg; map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
map->reg_defaults[map->num_reg_defaults - 1].def = val; map->reg_defaults[map->num_reg_defaults - 1].def = val;
sort(map->reg_defaults, map->num_reg_defaults, sort(map->reg_defaults, map->num_reg_defaults,
sizeof(struct reg_default), regcache_insert_cmp, NULL); sizeof(struct reg_default), regcache_default_cmp, NULL);
return 0; return 0;
} }