1 /* SPDX-License-Identifier: GPL-2.0 */
3 * ddbridge-i2c.h: Digital Devices bridge i2c driver
5 * Copyright (C) 2010-2017 Digital Devices GmbH
6 * Ralph Metzler <rjkm@metzlerbros.de>
7 * Marcus Metzler <mocm@metzlerbros.de>
10 #ifndef __DDBRIDGE_I2C_H__
11 #define __DDBRIDGE_I2C_H__
13 #include <linux/i2c.h>
17 /******************************************************************************/
19 void ddb_i2c_release(struct ddb
*dev
);
20 int ddb_i2c_init(struct ddb
*dev
);
22 /******************************************************************************/
24 static int __maybe_unused
i2c_io(struct i2c_adapter
*adapter
, u8 adr
,
25 u8
*wbuf
, u32 wlen
, u8
*rbuf
, u32 rlen
)
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
} };
32 return (i2c_transfer(adapter
, msgs
, 2) == 2) ? 0 : -1;
35 static int __maybe_unused
i2c_write(struct i2c_adapter
*adap
, u8 adr
,
38 struct i2c_msg msg
= { .addr
= adr
, .flags
= 0,
39 .buf
= data
, .len
= len
};
41 return (i2c_transfer(adap
, &msg
, 1) == 1) ? 0 : -1;
44 static int __maybe_unused
i2c_read(struct i2c_adapter
*adapter
, u8 adr
, u8
*val
)
46 struct i2c_msg msgs
[1] = { { .addr
= adr
, .flags
= I2C_M_RD
,
47 .buf
= val
, .len
= 1 } };
49 return (i2c_transfer(adapter
, msgs
, 1) == 1) ? 0 : -1;
52 static int __maybe_unused
i2c_read_regs(struct i2c_adapter
*adapter
,
53 u8 adr
, u8 reg
, u8
*val
, u8 len
)
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
} };
60 return (i2c_transfer(adapter
, msgs
, 2) == 2) ? 0 : -1;
63 static int __maybe_unused
i2c_read_regs16(struct i2c_adapter
*adapter
,
64 u8 adr
, u16 reg
, u8
*val
, u8 len
)
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
} };
72 return (i2c_transfer(adapter
, msgs
, 2) == 2) ? 0 : -1;
75 static int __maybe_unused
i2c_write_reg16(struct i2c_adapter
*adap
,
76 u8 adr
, u16 reg
, u8 val
)
78 u8 msg
[3] = { reg
>> 8, reg
& 0xff, val
};
80 return i2c_write(adap
, adr
, msg
, 3);
83 static int __maybe_unused
i2c_write_reg(struct i2c_adapter
*adap
,
84 u8 adr
, u8 reg
, u8 val
)
86 u8 msg
[2] = { reg
, val
};
88 return i2c_write(adap
, adr
, msg
, 2);
91 static int __maybe_unused
i2c_read_reg16(struct i2c_adapter
*adapter
,
92 u8 adr
, u16 reg
, u8
*val
)
94 return i2c_read_regs16(adapter
, adr
, reg
, val
, 1);
97 static int __maybe_unused
i2c_read_reg(struct i2c_adapter
*adapter
,
98 u8 adr
, u8 reg
, u8
*val
)
100 return i2c_read_regs(adapter
, adr
, reg
, val
, 1);
103 #endif /* __DDBRIDGE_I2C_H__ */