1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <device/i2c_simple.h>
4 #include <stdint.h>
5
i2c_read_field(unsigned int bus,uint8_t chip,uint8_t reg,uint8_t * data,uint8_t mask,uint8_t shift)6 int i2c_read_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t *data,
7 uint8_t mask, uint8_t shift)
8 {
9 int ret;
10 uint8_t buf = 0;
11
12 ret = i2c_readb(bus, chip, reg, &buf);
13
14 buf &= (mask << shift);
15 *data = (buf >> shift);
16
17 return ret;
18 }
19
i2c_write_field(unsigned int bus,uint8_t chip,uint8_t reg,uint8_t data,uint8_t mask,uint8_t shift)20 int i2c_write_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t data,
21 uint8_t mask, uint8_t shift)
22 {
23 int ret;
24 uint8_t buf = 0;
25
26 ret = i2c_readb(bus, chip, reg, &buf);
27
28 buf &= ~(mask << shift);
29 buf |= (data << shift);
30
31 ret |= i2c_writeb(bus, chip, reg, buf);
32
33 return ret;
34 }
35