summaryrefslogtreecommitdiffstats
path: root/drivers/base/regmap/regmap-i2c.c
blob: 5e3705162c923ff9169ec789f9e90e2f40219039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: GPL-2.0-only
/*
 * 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, &regmap_regmap_i2c_bus, client, config);
}