1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#ifndef __DDBRIDGE_I2C_H__
20#define __DDBRIDGE_I2C_H__
21
22#include <linux/i2c.h>
23
24#include "ddbridge.h"
25
26
27
28void ddb_i2c_release(struct ddb *dev);
29int ddb_i2c_init(struct ddb *dev);
30
31
32
33static int __maybe_unused i2c_io(struct i2c_adapter *adapter, u8 adr,
34 u8 *wbuf, u32 wlen, u8 *rbuf, u32 rlen)
35{
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 } };
40
41 return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
42}
43
44static int __maybe_unused i2c_write(struct i2c_adapter *adap, u8 adr,
45 u8 *data, int len)
46{
47 struct i2c_msg msg = { .addr = adr, .flags = 0,
48 .buf = data, .len = len };
49
50 return (i2c_transfer(adap, &msg, 1) == 1) ? 0 : -1;
51}
52
53static int __maybe_unused i2c_read(struct i2c_adapter *adapter, u8 adr, u8 *val)
54{
55 struct i2c_msg msgs[1] = { { .addr = adr, .flags = I2C_M_RD,
56 .buf = val, .len = 1 } };
57
58 return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1;
59}
60
61static int __maybe_unused i2c_read_regs(struct i2c_adapter *adapter,
62 u8 adr, u8 reg, u8 *val, u8 len)
63{
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 } };
68
69 return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
70}
71
72static int __maybe_unused i2c_read_regs16(struct i2c_adapter *adapter,
73 u8 adr, u16 reg, u8 *val, u8 len)
74{
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 } };
80
81 return (i2c_transfer(adapter, msgs, 2) == 2) ? 0 : -1;
82}
83
84static int __maybe_unused i2c_write_reg16(struct i2c_adapter *adap,
85 u8 adr, u16 reg, u8 val)
86{
87 u8 msg[3] = { reg >> 8, reg & 0xff, val };
88
89 return i2c_write(adap, adr, msg, 3);
90}
91
92static int __maybe_unused i2c_write_reg(struct i2c_adapter *adap,
93 u8 adr, u8 reg, u8 val)
94{
95 u8 msg[2] = { reg, val };
96
97 return i2c_write(adap, adr, msg, 2);
98}
99
100static int __maybe_unused i2c_read_reg16(struct i2c_adapter *adapter,
101 u8 adr, u16 reg, u8 *val)
102{
103 return i2c_read_regs16(adapter, adr, reg, val, 1);
104}
105
106static int __maybe_unused i2c_read_reg(struct i2c_adapter *adapter,
107 u8 adr, u8 reg, u8 *val)
108{
109 return i2c_read_regs(adapter, adr, reg, val, 1);
110}
111
112#endif
113