Linux offers a helper for creating regmaps over i2c. Add a similar helper for barebox to ease driver porting. Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- drivers/base/regmap/Makefile | 1 + drivers/base/regmap/regmap-i2c.c | 44 ++++++++++++++++++++++++++++++++ include/regmap.h | 13 ++++++++++ 3 files changed, 58 insertions(+) create mode 100644 drivers/base/regmap/regmap-i2c.c diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile index ab2387037d37..b12b69531156 100644 --- a/drivers/base/regmap/Makefile +++ b/drivers/base/regmap/Makefile @@ -1,2 +1,3 @@ obj-y += regmap.o obj-y += regmap-mmio.o +obj-$(CONFIG_I2C) += regmap-i2c.o diff --git a/drivers/base/regmap/regmap-i2c.c b/drivers/base/regmap/regmap-i2c.c new file mode 100644 index 000000000000..88b24ae6a825 --- /dev/null +++ b/drivers/base/regmap/regmap-i2c.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2021, Ahmad Fatoum, Pengutronix + */ + +#include <i2c/i2c.h> +#include <regmap.h> + + +static int regmap_i2c_reg_read(void *client, unsigned int reg, unsigned int *val) +{ + u8 buf[1]; + int ret; + + ret = i2c_read_reg(client, reg, buf, 1); + if (ret != 1) + return ret; + + *val = buf[0]; + return 0; +} + +static int regmap_i2c_reg_write(void *client, unsigned int reg, unsigned int val) +{ + u8 buf[] = { val & 0xff }; + int ret; + + ret = i2c_write_reg(client, reg, buf, 1); + if (ret != 1) + return ret; + + return 0; +} + +static const struct regmap_bus regmap_regmap_i2c_bus = { + .reg_write = regmap_i2c_reg_write, + .reg_read = regmap_i2c_reg_read, +}; + +struct regmap *regmap_init_i2c(struct i2c_client *client, + const struct regmap_config *config) +{ + return regmap_init(&client->dev, ®map_regmap_i2c_bus, client, config); +} diff --git a/include/regmap.h b/include/regmap.h index b43cd936fa61..c5cf954ad6d9 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -76,6 +76,19 @@ struct regmap *regmap_init_mmio_clk(struct device_d *dev, const char *clk_id, void __iomem *regs, const struct regmap_config *config); +/** + * regmap_init_i2c() - Initialise i2c register map + * + * @i2c: Device that will be interacted with + * @config: Configuration for register map + * + * The return value will be an ERR_PTR() on error or a valid pointer + * to a struct regmap. + */ +struct i2c_client; +struct regmap *regmap_init_i2c(struct i2c_client *i2c, + const struct regmap_config *config); + /** * regmap_init_mmio() - Initialise register map * -- 2.29.2 _______________________________________________ barebox mailing list barebox@xxxxxxxxxxxxxxxxxxx http://lists.infradead.org/mailman/listinfo/barebox