Re: [PATCH 1/4] regmap: Add generic non-memory mapped register access API

From: Takashi Iwai
Date: Mon Jul 18 2011 - 06:38:25 EST


At Mon, 18 Jul 2011 19:07:40 +0900,
Mark Brown wrote:
>
> +static int _regmap_raw_write(struct regmap *map, unsigned int reg,
> + const void *val, size_t val_len)
> +{
> + void *buf;
> + int ret = -ENOTSUPP;
> + size_t len;
> +
> + map->format.format_reg(map->work_buf, reg);
> +
> + /* Try to do a gather write if we can */
> + if (map->bus->gather_write)
> + ret = map->bus->gather_write(map->dev, map->work_buf,
> + map->format.reg_bytes,
> + val, val_len);
> +
> + /* Otherwise fall back on linearising by hand. */
> + if (ret == -ENOTSUPP) {
> + len = map->format.reg_bytes + val_len;
> + buf = kmalloc(len, GFP_KERNEL);
> + if (!buf)
> + return -ENOMEM;
> +
> + memcpy(buf, map->work_buf, map->format.reg_bytes);
> + memcpy(buf + map->format.reg_bytes, val, val_len);
> + ret = map->bus->write(map->dev, buf, len);
> +
> + kfree(buf);

In most cases, val = map->work_buf + map->format.reg_bytes.
How about a bit optimization?

if (ret == -ENOTSUPP) {
len = map->format.reg_bytes + val_len;
if (val == map->work_buf + map->format.reg_bytes)
ret = map->bus->write(map->dev, map->work_buf, len);
else {
buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;

memcpy(buf, map->work_buf, map->format.reg_bytes);
memcpy(buf + map->format.reg_bytes, val, val_len);
ret = map->bus->write(map->dev, buf, len);

kfree(buf);
}
}


thanks,

Takashi
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/