/src/barebox/drivers/i2c/i2c-smbus.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include <common.h> |
4 | | #include <errno.h> |
5 | | #include <i2c/i2c.h> |
6 | | |
7 | | /* The SMBus parts */ |
8 | | |
9 | 0 | #define POLY (0x1070U << 3) |
10 | | static u8 crc8(u16 data) |
11 | 0 | { |
12 | 0 | int i; |
13 | |
|
14 | 0 | for (i = 0; i < 8; i++) { |
15 | 0 | if (data & 0x8000) |
16 | 0 | data = data ^ POLY; |
17 | 0 | data = data << 1; |
18 | 0 | } |
19 | 0 | return (u8)(data >> 8); |
20 | 0 | } |
21 | | |
22 | | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
23 | | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) |
24 | 0 | { |
25 | 0 | int i; |
26 | |
|
27 | 0 | for (i = 0; i < count; i++) |
28 | 0 | crc = crc8((crc ^ p[i]) << 8); |
29 | 0 | return crc; |
30 | 0 | } |
31 | | |
32 | | /* Assume a 7-bit address, which is reasonable for SMBus */ |
33 | | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) |
34 | 0 | { |
35 | | /* The address will be sent first */ |
36 | 0 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); |
37 | 0 | pec = i2c_smbus_pec(pec, &addr, 1); |
38 | | |
39 | | /* The data buffer follows */ |
40 | 0 | return i2c_smbus_pec(pec, msg->buf, msg->len); |
41 | 0 | } |
42 | | |
43 | | /* Used for write only transactions */ |
44 | | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) |
45 | 0 | { |
46 | 0 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
47 | 0 | msg->len++; |
48 | 0 | } |
49 | | |
50 | | /* Return <0 on CRC error |
51 | | If there was a write before this read (most cases) we need to take the |
52 | | partial CRC from the write part into account. |
53 | | Note that this function does modify the message (we need to decrease the |
54 | | message length to hide the CRC byte from the caller). */ |
55 | | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) |
56 | 0 | { |
57 | 0 | u8 rpec = msg->buf[--msg->len]; |
58 | 0 | cpec = i2c_smbus_msg_pec(cpec, msg); |
59 | |
|
60 | 0 | if (rpec != cpec) { |
61 | 0 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", |
62 | 0 | rpec, cpec); |
63 | 0 | return -EBADMSG; |
64 | 0 | } |
65 | 0 | return 0; |
66 | 0 | } |
67 | | |
68 | | /** |
69 | | * i2c_smbus_read_byte - SMBus "receive byte" protocol |
70 | | * @client: Handle to slave device |
71 | | * |
72 | | * This executes the SMBus "receive byte" protocol, returning negative errno |
73 | | * else the byte received from the device. |
74 | | */ |
75 | | s32 i2c_smbus_read_byte(const struct i2c_client *client) |
76 | 0 | { |
77 | 0 | union i2c_smbus_data data; |
78 | 0 | int status; |
79 | |
|
80 | 0 | status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
81 | 0 | I2C_SMBUS_READ, 0, |
82 | 0 | I2C_SMBUS_BYTE, &data); |
83 | 0 | return (status < 0) ? status : data.byte; |
84 | 0 | } |
85 | | EXPORT_SYMBOL(i2c_smbus_read_byte); |
86 | | |
87 | | /** |
88 | | * i2c_smbus_write_byte - SMBus "send byte" protocol |
89 | | * @client: Handle to slave device |
90 | | * @value: Byte to be sent |
91 | | * |
92 | | * This executes the SMBus "send byte" protocol, returning negative errno |
93 | | * else zero on success. |
94 | | */ |
95 | | s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value) |
96 | 0 | { |
97 | 0 | return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
98 | 0 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
99 | 0 | } |
100 | | EXPORT_SYMBOL(i2c_smbus_write_byte); |
101 | | |
102 | | /** |
103 | | * i2c_smbus_read_byte_data - SMBus "read byte" protocol |
104 | | * @client: Handle to slave device |
105 | | * @command: Byte interpreted by slave |
106 | | * |
107 | | * This executes the SMBus "read byte" protocol, returning negative errno |
108 | | * else a data byte received from the device. |
109 | | */ |
110 | | s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command) |
111 | 0 | { |
112 | 0 | union i2c_smbus_data data; |
113 | 0 | int status; |
114 | |
|
115 | 0 | status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
116 | 0 | I2C_SMBUS_READ, command, |
117 | 0 | I2C_SMBUS_BYTE_DATA, &data); |
118 | 0 | return (status < 0) ? status : data.byte; |
119 | 0 | } |
120 | | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
121 | | |
122 | | /** |
123 | | * i2c_smbus_write_byte_data - SMBus "write byte" protocol |
124 | | * @client: Handle to slave device |
125 | | * @command: Byte interpreted by slave |
126 | | * @value: Byte being written |
127 | | * |
128 | | * This executes the SMBus "write byte" protocol, returning negative errno |
129 | | * else zero on success. |
130 | | */ |
131 | | s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, |
132 | | u8 value) |
133 | 0 | { |
134 | 0 | union i2c_smbus_data data; |
135 | 0 | data.byte = value; |
136 | 0 | return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
137 | 0 | I2C_SMBUS_WRITE, command, |
138 | 0 | I2C_SMBUS_BYTE_DATA, &data); |
139 | 0 | } |
140 | | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
141 | | |
142 | | /** |
143 | | * i2c_smbus_read_word_data - SMBus "read word" protocol |
144 | | * @client: Handle to slave device |
145 | | * @command: Byte interpreted by slave |
146 | | * |
147 | | * This executes the SMBus "read word" protocol, returning negative errno |
148 | | * else a 16-bit unsigned "word" received from the device. |
149 | | */ |
150 | | s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command) |
151 | 0 | { |
152 | 0 | union i2c_smbus_data data; |
153 | 0 | int status; |
154 | |
|
155 | 0 | status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
156 | 0 | I2C_SMBUS_READ, command, |
157 | 0 | I2C_SMBUS_WORD_DATA, &data); |
158 | 0 | return (status < 0) ? status : data.word; |
159 | 0 | } |
160 | | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
161 | | |
162 | | /** |
163 | | * i2c_smbus_write_word_data - SMBus "write word" protocol |
164 | | * @client: Handle to slave device |
165 | | * @command: Byte interpreted by slave |
166 | | * @value: 16-bit "word" being written |
167 | | * |
168 | | * This executes the SMBus "write word" protocol, returning negative errno |
169 | | * else zero on success. |
170 | | */ |
171 | | s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, |
172 | | u16 value) |
173 | 0 | { |
174 | 0 | union i2c_smbus_data data; |
175 | 0 | data.word = value; |
176 | 0 | return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
177 | 0 | I2C_SMBUS_WRITE, command, |
178 | 0 | I2C_SMBUS_WORD_DATA, &data); |
179 | 0 | } |
180 | | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
181 | | |
182 | | /* Returns the number of read bytes */ |
183 | | s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, |
184 | | u8 length, u8 *values) |
185 | 0 | { |
186 | 0 | union i2c_smbus_data data; |
187 | 0 | int status; |
188 | |
|
189 | 0 | if (length > I2C_SMBUS_BLOCK_MAX) |
190 | 0 | length = I2C_SMBUS_BLOCK_MAX; |
191 | 0 | data.block[0] = length; |
192 | 0 | status = i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
193 | 0 | I2C_SMBUS_READ, command, |
194 | 0 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
195 | 0 | if (status < 0) |
196 | 0 | return status; |
197 | | |
198 | 0 | memcpy(values, &data.block[1], data.block[0]); |
199 | 0 | return data.block[0]; |
200 | 0 | } |
201 | | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
202 | | |
203 | | s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, |
204 | | u8 length, const u8 *values) |
205 | 0 | { |
206 | 0 | union i2c_smbus_data data; |
207 | |
|
208 | 0 | if (length > I2C_SMBUS_BLOCK_MAX) |
209 | 0 | length = I2C_SMBUS_BLOCK_MAX; |
210 | 0 | data.block[0] = length; |
211 | 0 | memcpy(data.block + 1, values, length); |
212 | 0 | return i2c_smbus_xfer(client->adapter, client->addr, 0 /* client->flags */, |
213 | 0 | I2C_SMBUS_WRITE, command, |
214 | 0 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
215 | 0 | } |
216 | | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
217 | | |
218 | | /* Simulate a SMBus command using the i2c protocol |
219 | | No checking of parameters is done! */ |
220 | | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, |
221 | | unsigned short flags, |
222 | | char read_write, u8 command, int size, |
223 | | union i2c_smbus_data *data) |
224 | 0 | { |
225 | | /* So we need to generate a series of msgs. In the case of writing, we |
226 | | need to use only one message; when reading, we need two. We initialize |
227 | | most things with sane defaults, to keep the code below somewhat |
228 | | simpler. */ |
229 | 0 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
230 | 0 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; |
231 | 0 | int num = read_write == I2C_SMBUS_READ ? 2 : 1; |
232 | 0 | int i; |
233 | 0 | u8 partial_pec = 0; |
234 | 0 | int status; |
235 | 0 | struct i2c_msg msg[2] = { |
236 | 0 | { |
237 | 0 | .addr = addr, |
238 | 0 | .flags = flags, |
239 | 0 | .len = 1, |
240 | 0 | .buf = msgbuf0, |
241 | 0 | }, { |
242 | 0 | .addr = addr, |
243 | 0 | .flags = flags | I2C_M_RD, |
244 | 0 | .len = 0, |
245 | 0 | .buf = msgbuf1, |
246 | 0 | }, |
247 | 0 | }; |
248 | |
|
249 | 0 | msgbuf0[0] = command; |
250 | 0 | switch (size) { |
251 | 0 | case I2C_SMBUS_QUICK: |
252 | 0 | msg[0].len = 0; |
253 | | /* Special case: The read/write field is used as data */ |
254 | 0 | msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? |
255 | 0 | I2C_M_RD : 0); |
256 | 0 | num = 1; |
257 | 0 | break; |
258 | 0 | case I2C_SMBUS_BYTE: |
259 | 0 | if (read_write == I2C_SMBUS_READ) { |
260 | | /* Special case: only a read! */ |
261 | 0 | msg[0].flags = I2C_M_RD | flags; |
262 | 0 | num = 1; |
263 | 0 | } |
264 | 0 | break; |
265 | 0 | case I2C_SMBUS_BYTE_DATA: |
266 | 0 | if (read_write == I2C_SMBUS_READ) |
267 | 0 | msg[1].len = 1; |
268 | 0 | else { |
269 | 0 | msg[0].len = 2; |
270 | 0 | msgbuf0[1] = data->byte; |
271 | 0 | } |
272 | 0 | break; |
273 | 0 | case I2C_SMBUS_WORD_DATA: |
274 | 0 | if (read_write == I2C_SMBUS_READ) |
275 | 0 | msg[1].len = 2; |
276 | 0 | else { |
277 | 0 | msg[0].len = 3; |
278 | 0 | msgbuf0[1] = data->word & 0xff; |
279 | 0 | msgbuf0[2] = data->word >> 8; |
280 | 0 | } |
281 | 0 | break; |
282 | 0 | case I2C_SMBUS_I2C_BLOCK_DATA: |
283 | 0 | if (read_write == I2C_SMBUS_READ) { |
284 | 0 | msg[1].len = data->block[0]; |
285 | 0 | } else { |
286 | 0 | msg[0].len = data->block[0] + 1; |
287 | 0 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
288 | 0 | dev_err(&adapter->dev, |
289 | 0 | "Invalid block write size %d\n", |
290 | 0 | data->block[0]); |
291 | 0 | return -EINVAL; |
292 | 0 | } |
293 | 0 | for (i = 1; i <= data->block[0]; i++) |
294 | 0 | msgbuf0[i] = data->block[i]; |
295 | 0 | } |
296 | 0 | break; |
297 | 0 | default: |
298 | 0 | dev_err(&adapter->dev, "Unsupported transaction %d\n", size); |
299 | 0 | return -EOPNOTSUPP; |
300 | 0 | } |
301 | | |
302 | 0 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
303 | 0 | && size != I2C_SMBUS_I2C_BLOCK_DATA); |
304 | 0 | if (i) { |
305 | | /* Compute PEC if first message is a write */ |
306 | 0 | if (!(msg[0].flags & I2C_M_RD)) { |
307 | 0 | if (num == 1) /* Write only */ |
308 | 0 | i2c_smbus_add_pec(&msg[0]); |
309 | 0 | else /* Write followed by read */ |
310 | 0 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); |
311 | 0 | } |
312 | | /* Ask for PEC if last message is a read */ |
313 | 0 | if (msg[num-1].flags & I2C_M_RD) |
314 | 0 | msg[num-1].len++; |
315 | 0 | } |
316 | |
|
317 | 0 | status = i2c_transfer(adapter, msg, num); |
318 | 0 | if (status < 0) |
319 | 0 | return status; |
320 | | |
321 | | /* Check PEC if last message is a read */ |
322 | 0 | if (i && (msg[num-1].flags & I2C_M_RD)) { |
323 | 0 | status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); |
324 | 0 | if (status < 0) |
325 | 0 | return status; |
326 | 0 | } |
327 | | |
328 | 0 | if (read_write == I2C_SMBUS_READ) |
329 | 0 | switch (size) { |
330 | 0 | case I2C_SMBUS_BYTE: |
331 | 0 | data->byte = msgbuf0[0]; |
332 | 0 | break; |
333 | 0 | case I2C_SMBUS_BYTE_DATA: |
334 | 0 | data->byte = msgbuf1[0]; |
335 | 0 | break; |
336 | 0 | case I2C_SMBUS_WORD_DATA: |
337 | 0 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
338 | 0 | break; |
339 | 0 | case I2C_SMBUS_I2C_BLOCK_DATA: |
340 | 0 | for (i = 0; i < data->block[0]; i++) |
341 | 0 | data->block[i+1] = msgbuf1[i]; |
342 | 0 | break; |
343 | 0 | } |
344 | 0 | return 0; |
345 | 0 | } |
346 | | |
347 | | /** |
348 | | * i2c_smbus_xfer - execute SMBus protocol operations |
349 | | * @adapter: Handle to I2C bus |
350 | | * @addr: Address of SMBus slave on that bus |
351 | | * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) |
352 | | * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE |
353 | | * @command: Byte interpreted by slave, for protocols which use such bytes |
354 | | * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL |
355 | | * @data: Data to be read or written |
356 | | * |
357 | | * This executes an SMBus protocol operation, and returns a negative |
358 | | * errno code else zero on success. |
359 | | */ |
360 | | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, |
361 | | char read_write, u8 command, int protocol, |
362 | | union i2c_smbus_data *data) |
363 | 0 | { |
364 | 0 | s32 res; |
365 | |
|
366 | 0 | flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; |
367 | |
|
368 | 0 | res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, |
369 | 0 | command, protocol, data); |
370 | |
|
371 | 0 | return res; |
372 | 0 | } |
373 | | EXPORT_SYMBOL(i2c_smbus_xfer); |