Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Yidi Lin | d33ebd1 | 2016-03-15 14:38:44 +0800 | [diff] [blame] | 2 | |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 3 | #include <device/i2c_simple.h> |
Duncan Laurie | ec00968 | 2016-06-07 15:38:14 -0700 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 6 | int i2c_read_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t *data, |
Yidi Lin | d33ebd1 | 2016-03-15 14:38:44 +0800 | [diff] [blame] | 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 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 20 | int i2c_write_field(unsigned int bus, uint8_t chip, uint8_t reg, uint8_t data, |
Yidi Lin | d33ebd1 | 2016-03-15 14:38:44 +0800 | [diff] [blame] | 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 | } |