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>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 only, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #ifndef __DDBRIDGE_I2C_H__
20 #define __DDBRIDGE_I2C_H__
22 #include <linux/i2c.h>
26 /******************************************************************************/
28 void ddb_i2c_release(struct ddb
*dev
);
29 int ddb_i2c_init(struct ddb
*dev
);
31 /******************************************************************************/
33 static int __maybe_unused
i2c_io(struct i2c_adapter
*adapter
, u8 adr
,
34 u8
*wbuf
, u32 wlen
, u8
*rbuf
, u32 rlen
)
36 struct i2c_msg msgs
[2] = { { .addr
= adr
, .flags
= 0,
37 .buf
= wbuf
, .len
= wlen
},
38 { .addr
= adr
, .flags
= I2C_M_RD
,
39 .buf
= rbuf
, .len
= rlen
} };
41 return (i2c_transfer(adapter
, msgs
, 2) == 2) ? 0 : -1;
44 static int __maybe_unused
i2c_write(struct i2c_adapter
*adap
, u8 adr
,
47 struct i2c_msg msg
= { .addr
= adr
, .flags
= 0,
48 .buf
= data
, .len
= len
};
50 return (i2c_transfer(adap
, &msg
, 1) == 1) ? 0 : -1;
53 static int __maybe_unused
i2c_read(struct i2c_adapter
*adapter
, u8 adr
, u8
*val
)
55 struct i2c_msg msgs
[1] = { { .addr
= adr
, .flags
= I2C_M_RD
,
56 .buf
= val
, .len
= 1 } };
58 return (i2c_transfer(adapter
, msgs
, 1) == 1) ? 0 : -1;
61 static int __maybe_unused
i2c_read_regs(struct i2c_adapter
*adapter
,
62 u8 adr
, u8 reg
, u8
*val
, u8 len
)
64 struct i2c_msg msgs
[2] = { { .addr
= adr
, .flags
= 0,
65 .buf
= ®
, .len
= 1 },
66 { .addr
= adr
, .flags
= I2C_M_RD
,
67 .buf
= val
, .len
= len
} };
69 return (i2c_transfer(adapter
, msgs
, 2) == 2) ? 0 : -1;
72 static int __maybe_unused
i2c_read_regs16(struct i2c_adapter
*adapter
,
73 u8 adr
, u16 reg
, u8
*val
, u8 len
)
75 u8 msg
[2] = { reg
>> 8, reg
& 0xff };
76 struct i2c_msg msgs
[2] = { { .addr
= adr
, .flags
= 0,
77 .buf
= msg
, .len
= 2 },
78 { .addr
= adr
, .flags
= I2C_M_RD
,
79 .buf
= val
, .len
= len
} };
81 return (i2c_transfer(adapter
, msgs
, 2) == 2) ? 0 : -1;
84 static int __maybe_unused
i2c_write_reg16(struct i2c_adapter
*adap
,
85 u8 adr
, u16 reg
, u8 val
)
87 u8 msg
[3] = { reg
>> 8, reg
& 0xff, val
};
89 return i2c_write(adap
, adr
, msg
, 3);
92 static int __maybe_unused
i2c_write_reg(struct i2c_adapter
*adap
,
93 u8 adr
, u8 reg
, u8 val
)
95 u8 msg
[2] = { reg
, val
};
97 return i2c_write(adap
, adr
, msg
, 2);
100 static int __maybe_unused
i2c_read_reg16(struct i2c_adapter
*adapter
,
101 u8 adr
, u16 reg
, u8
*val
)
103 return i2c_read_regs16(adapter
, adr
, reg
, val
, 1);
106 static int __maybe_unused
i2c_read_reg(struct i2c_adapter
*adapter
,
107 u8 adr
, u8 reg
, u8
*val
)
109 return i2c_read_regs(adapter
, adr
, reg
, val
, 1);
112 #endif /* __DDBRIDGE_I2C_H__ */