1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * ddbridge-i2c.h: Digital Devices bridge i2c driver 4 * 5 * Copyright (C) 2010-2017 Digital Devices GmbH 6 * Ralph Metzler <rjkm@metzlerbros.de> 7 * Marcus Metzler <mocm@metzlerbros.de> 8 */ 9 10 #ifndef __DDBRIDGE_I2C_H__ 11 #define __DDBRIDGE_I2C_H__ 12 13 #include <linux/i2c.h> 14 15 #include "ddbridge.h" 16 17 /******************************************************************************/ 18 19 void ddb_i2c_release(struct ddb *dev); 20 int ddb_i2c_init(struct ddb *dev); 21 22 /******************************************************************************/ 23 24 static int __maybe_unused i2c_io(struct i2c_adapter *adapter, u8 adr, 25 u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen) 26 { 27 struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, 28 .buf = wbuf, .len = wlen }, 29 { .addr = adr, .flags = I2C_M_RD, 30 .buf = rbuf, .len = rlen } }; 31 32 return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; 33 } 34 35 static int __maybe_unused i2c_write(struct i2c_adapter *adap, u8 adr, 36 u8 *data, int len) 37 { 38 struct i2c_msg msg = { .addr = adr, .flags = 0, 39 .buf = data, .len = len }; 40 41 return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1; 42 } 43 44 static int __maybe_unused i2c_read(struct i2c_adapter *adapter, u8 adr, u8 *val) 45 { 46 struct i2c_msg msgs[1] = { { .addr = adr, .flags = I2C_M_RD, 47 .buf = val, .len = 1 } }; 48 49 return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1; 50 } 51 52 static int __maybe_unused i2c_read_regs(struct i2c_adapter *adapter, 53 u8 adr, u8 reg, u8 *val, u8 len) 54 { 55 struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, 56 .buf = ®, .len = 1 }, 57 { .addr = adr, .flags = I2C_M_RD, 58 .buf = val, .len = len } }; 59 60 return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; 61 } 62 63 static int __maybe_unused i2c_read_regs16(struct i2c_adapter *adapter, 64 u8 adr, u16 reg, u8 *val, u8 len) 65 { 66 u8 msg[2] = { reg >> 8, reg & 0xff }; 67 struct i2c_msg msgs[2] = { { .addr = adr, .flags = 0, 68 .buf = msg, .len = 2 }, 69 { .addr = adr, .flags = I2C_M_RD, 70 .buf = val, .len = len } }; 71 72 return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1; 73 } 74 75 static int __maybe_unused i2c_write_reg16(struct i2c_adapter *adap, 76 u8 adr, u16 reg, u8 val) 77 { 78 u8 msg[3] = { reg >> 8, reg & 0xff, val }; 79 80 return i2c_write(adap, adr, msg, 3); 81 } 82 83 static int __maybe_unused i2c_write_reg(struct i2c_adapter *adap, 84 u8 adr, u8 reg, u8 val) 85 { 86 u8 msg[2] = { reg, val }; 87 88 return i2c_write(adap, adr, msg, 2); 89 } 90 91 static int __maybe_unused i2c_read_reg16(struct i2c_adapter *adapter, 92 u8 adr, u16 reg, u8 *val) 93 { 94 return i2c_read_regs16(adapter, adr, reg, val, 1); 95 } 96 97 static int __maybe_unused i2c_read_reg(struct i2c_adapter *adapter, 98 u8 adr, u8 reg, u8 *val) 99 { 100 return i2c_read_regs(adapter, adr, reg, val, 1); 101 } 102 103 #endif /* __DDBRIDGE_I2C_H__ */ 104