2020-04-02 23:48:16 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2016-03-15 07:38:44 +01:00
|
|
|
|
2017-08-01 14:02:40 +02:00
|
|
|
#include <device/i2c_simple.h>
|
2016-06-08 00:38:14 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-10-24 05:41:00 +02:00
|
|
|
int i2c_read_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t *data,
|
2016-03-15 07:38:44 +01:00
|
|
|
uint8_t mask, uint8_t shift)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint8_t buf = 0;
|
|
|
|
|
|
|
|
ret = i2c_readb(bus, chip, reg, &buf);
|
|
|
|
|
|
|
|
buf &= (mask << shift);
|
|
|
|
*data = (buf >> shift);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-24 05:41:00 +02:00
|
|
|
int i2c_write_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t data,
|
2016-03-15 07:38:44 +01:00
|
|
|
uint8_t mask, uint8_t shift)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint8_t buf = 0;
|
|
|
|
|
|
|
|
ret = i2c_readb(bus, chip, reg, &buf);
|
|
|
|
|
|
|
|
buf &= ~(mask << shift);
|
|
|
|
buf |= (data << shift);
|
|
|
|
|
|
|
|
ret |= i2c_writeb(bus, chip, reg, buf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|