Commit | Line | Data |
---|---|---|
22c78d1c WS |
1 | /* |
2 | * Linux I2C core SMBus and SMBus emulation code | |
3 | * | |
4 | * This file contains the SMBus functions which are always included in the I2C | |
5 | * core because they can be emulated via I2C. SMBus specific extensions | |
6 | * (e.g. smbalert) are handled in a seperate i2c-smbus module. | |
7 | * | |
8 | * All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> | |
9 | * SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and | |
10 | * Jean Delvare <jdelvare@suse.de> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by the Free | |
14 | * Software Foundation; either version 2 of the License, or (at your option) | |
15 | * any later version. | |
16 | */ | |
17 | #include <linux/device.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/i2c.h> | |
3c0a60be | 20 | #include <linux/i2c-smbus.h> |
22c78d1c WS |
21 | |
22 | #define CREATE_TRACE_POINTS | |
23 | #include <trace/events/smbus.h> | |
24 | ||
25 | ||
26 | /* The SMBus parts */ | |
27 | ||
28 | #define POLY (0x1070U << 3) | |
29 | static u8 crc8(u16 data) | |
30 | { | |
31 | int i; | |
32 | ||
33 | for (i = 0; i < 8; i++) { | |
34 | if (data & 0x8000) | |
35 | data = data ^ POLY; | |
36 | data = data << 1; | |
37 | } | |
38 | return (u8)(data >> 8); | |
39 | } | |
40 | ||
41 | /* Incremental CRC8 over count bytes in the array pointed to by p */ | |
42 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) | |
43 | { | |
44 | int i; | |
45 | ||
46 | for (i = 0; i < count; i++) | |
47 | crc = crc8((crc ^ p[i]) << 8); | |
48 | return crc; | |
49 | } | |
50 | ||
51 | /* Assume a 7-bit address, which is reasonable for SMBus */ | |
52 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) | |
53 | { | |
54 | /* The address will be sent first */ | |
55 | u8 addr = i2c_8bit_addr_from_msg(msg); | |
56 | pec = i2c_smbus_pec(pec, &addr, 1); | |
57 | ||
58 | /* The data buffer follows */ | |
59 | return i2c_smbus_pec(pec, msg->buf, msg->len); | |
60 | } | |
61 | ||
62 | /* Used for write only transactions */ | |
63 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) | |
64 | { | |
65 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); | |
66 | msg->len++; | |
67 | } | |
68 | ||
69 | /* Return <0 on CRC error | |
70 | If there was a write before this read (most cases) we need to take the | |
71 | partial CRC from the write part into account. | |
72 | Note that this function does modify the message (we need to decrease the | |
73 | message length to hide the CRC byte from the caller). */ | |
74 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) | |
75 | { | |
76 | u8 rpec = msg->buf[--msg->len]; | |
77 | cpec = i2c_smbus_msg_pec(cpec, msg); | |
78 | ||
79 | if (rpec != cpec) { | |
80 | pr_debug("Bad PEC 0x%02x vs. 0x%02x\n", | |
81 | rpec, cpec); | |
82 | return -EBADMSG; | |
83 | } | |
84 | return 0; | |
85 | } | |
86 | ||
87 | /** | |
88 | * i2c_smbus_read_byte - SMBus "receive byte" protocol | |
89 | * @client: Handle to slave device | |
90 | * | |
91 | * This executes the SMBus "receive byte" protocol, returning negative errno | |
92 | * else the byte received from the device. | |
93 | */ | |
94 | s32 i2c_smbus_read_byte(const struct i2c_client *client) | |
95 | { | |
96 | union i2c_smbus_data data; | |
97 | int status; | |
98 | ||
99 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
100 | I2C_SMBUS_READ, 0, | |
101 | I2C_SMBUS_BYTE, &data); | |
102 | return (status < 0) ? status : data.byte; | |
103 | } | |
104 | EXPORT_SYMBOL(i2c_smbus_read_byte); | |
105 | ||
106 | /** | |
107 | * i2c_smbus_write_byte - SMBus "send byte" protocol | |
108 | * @client: Handle to slave device | |
109 | * @value: Byte to be sent | |
110 | * | |
111 | * This executes the SMBus "send byte" protocol, returning negative errno | |
112 | * else zero on success. | |
113 | */ | |
114 | s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value) | |
115 | { | |
116 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
117 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); | |
118 | } | |
119 | EXPORT_SYMBOL(i2c_smbus_write_byte); | |
120 | ||
121 | /** | |
122 | * i2c_smbus_read_byte_data - SMBus "read byte" protocol | |
123 | * @client: Handle to slave device | |
124 | * @command: Byte interpreted by slave | |
125 | * | |
126 | * This executes the SMBus "read byte" protocol, returning negative errno | |
127 | * else a data byte received from the device. | |
128 | */ | |
129 | s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command) | |
130 | { | |
131 | union i2c_smbus_data data; | |
132 | int status; | |
133 | ||
134 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
135 | I2C_SMBUS_READ, command, | |
136 | I2C_SMBUS_BYTE_DATA, &data); | |
137 | return (status < 0) ? status : data.byte; | |
138 | } | |
139 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); | |
140 | ||
141 | /** | |
142 | * i2c_smbus_write_byte_data - SMBus "write byte" protocol | |
143 | * @client: Handle to slave device | |
144 | * @command: Byte interpreted by slave | |
145 | * @value: Byte being written | |
146 | * | |
147 | * This executes the SMBus "write byte" protocol, returning negative errno | |
148 | * else zero on success. | |
149 | */ | |
150 | s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, | |
151 | u8 value) | |
152 | { | |
153 | union i2c_smbus_data data; | |
154 | data.byte = value; | |
155 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
156 | I2C_SMBUS_WRITE, command, | |
157 | I2C_SMBUS_BYTE_DATA, &data); | |
158 | } | |
159 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); | |
160 | ||
161 | /** | |
162 | * i2c_smbus_read_word_data - SMBus "read word" protocol | |
163 | * @client: Handle to slave device | |
164 | * @command: Byte interpreted by slave | |
165 | * | |
166 | * This executes the SMBus "read word" protocol, returning negative errno | |
167 | * else a 16-bit unsigned "word" received from the device. | |
168 | */ | |
169 | s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command) | |
170 | { | |
171 | union i2c_smbus_data data; | |
172 | int status; | |
173 | ||
174 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
175 | I2C_SMBUS_READ, command, | |
176 | I2C_SMBUS_WORD_DATA, &data); | |
177 | return (status < 0) ? status : data.word; | |
178 | } | |
179 | EXPORT_SYMBOL(i2c_smbus_read_word_data); | |
180 | ||
181 | /** | |
182 | * i2c_smbus_write_word_data - SMBus "write word" protocol | |
183 | * @client: Handle to slave device | |
184 | * @command: Byte interpreted by slave | |
185 | * @value: 16-bit "word" being written | |
186 | * | |
187 | * This executes the SMBus "write word" protocol, returning negative errno | |
188 | * else zero on success. | |
189 | */ | |
190 | s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, | |
191 | u16 value) | |
192 | { | |
193 | union i2c_smbus_data data; | |
194 | data.word = value; | |
195 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
196 | I2C_SMBUS_WRITE, command, | |
197 | I2C_SMBUS_WORD_DATA, &data); | |
198 | } | |
199 | EXPORT_SYMBOL(i2c_smbus_write_word_data); | |
200 | ||
201 | /** | |
202 | * i2c_smbus_read_block_data - SMBus "block read" protocol | |
203 | * @client: Handle to slave device | |
204 | * @command: Byte interpreted by slave | |
205 | * @values: Byte array into which data will be read; big enough to hold | |
206 | * the data returned by the slave. SMBus allows at most 32 bytes. | |
207 | * | |
208 | * This executes the SMBus "block read" protocol, returning negative errno | |
209 | * else the number of data bytes in the slave's response. | |
210 | * | |
211 | * Note that using this function requires that the client's adapter support | |
212 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers | |
213 | * support this; its emulation through I2C messaging relies on a specific | |
214 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. | |
215 | */ | |
216 | s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command, | |
217 | u8 *values) | |
218 | { | |
219 | union i2c_smbus_data data; | |
220 | int status; | |
221 | ||
222 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
223 | I2C_SMBUS_READ, command, | |
224 | I2C_SMBUS_BLOCK_DATA, &data); | |
225 | if (status) | |
226 | return status; | |
227 | ||
228 | memcpy(values, &data.block[1], data.block[0]); | |
229 | return data.block[0]; | |
230 | } | |
231 | EXPORT_SYMBOL(i2c_smbus_read_block_data); | |
232 | ||
233 | /** | |
234 | * i2c_smbus_write_block_data - SMBus "block write" protocol | |
235 | * @client: Handle to slave device | |
236 | * @command: Byte interpreted by slave | |
237 | * @length: Size of data block; SMBus allows at most 32 bytes | |
238 | * @values: Byte array which will be written. | |
239 | * | |
240 | * This executes the SMBus "block write" protocol, returning negative errno | |
241 | * else zero on success. | |
242 | */ | |
243 | s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, | |
244 | u8 length, const u8 *values) | |
245 | { | |
246 | union i2c_smbus_data data; | |
247 | ||
248 | if (length > I2C_SMBUS_BLOCK_MAX) | |
249 | length = I2C_SMBUS_BLOCK_MAX; | |
250 | data.block[0] = length; | |
251 | memcpy(&data.block[1], values, length); | |
252 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
253 | I2C_SMBUS_WRITE, command, | |
254 | I2C_SMBUS_BLOCK_DATA, &data); | |
255 | } | |
256 | EXPORT_SYMBOL(i2c_smbus_write_block_data); | |
257 | ||
258 | /* Returns the number of read bytes */ | |
259 | s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, | |
260 | u8 length, u8 *values) | |
261 | { | |
262 | union i2c_smbus_data data; | |
263 | int status; | |
264 | ||
265 | if (length > I2C_SMBUS_BLOCK_MAX) | |
266 | length = I2C_SMBUS_BLOCK_MAX; | |
267 | data.block[0] = length; | |
268 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
269 | I2C_SMBUS_READ, command, | |
270 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
271 | if (status < 0) | |
272 | return status; | |
273 | ||
274 | memcpy(values, &data.block[1], data.block[0]); | |
275 | return data.block[0]; | |
276 | } | |
277 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); | |
278 | ||
279 | s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, | |
280 | u8 length, const u8 *values) | |
281 | { | |
282 | union i2c_smbus_data data; | |
283 | ||
284 | if (length > I2C_SMBUS_BLOCK_MAX) | |
285 | length = I2C_SMBUS_BLOCK_MAX; | |
286 | data.block[0] = length; | |
287 | memcpy(data.block + 1, values, length); | |
288 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
289 | I2C_SMBUS_WRITE, command, | |
290 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
291 | } | |
292 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); | |
293 | ||
294 | /* Simulate a SMBus command using the i2c protocol | |
295 | No checking of parameters is done! */ | |
296 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, | |
297 | unsigned short flags, | |
298 | char read_write, u8 command, int size, | |
299 | union i2c_smbus_data *data) | |
300 | { | |
301 | /* So we need to generate a series of msgs. In the case of writing, we | |
302 | need to use only one message; when reading, we need two. We initialize | |
303 | most things with sane defaults, to keep the code below somewhat | |
304 | simpler. */ | |
305 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; | |
306 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; | |
307 | int num = read_write == I2C_SMBUS_READ ? 2 : 1; | |
308 | int i; | |
309 | u8 partial_pec = 0; | |
310 | int status; | |
311 | struct i2c_msg msg[2] = { | |
312 | { | |
313 | .addr = addr, | |
314 | .flags = flags, | |
315 | .len = 1, | |
316 | .buf = msgbuf0, | |
317 | }, { | |
318 | .addr = addr, | |
319 | .flags = flags | I2C_M_RD, | |
320 | .len = 0, | |
321 | .buf = msgbuf1, | |
322 | }, | |
323 | }; | |
324 | ||
325 | msgbuf0[0] = command; | |
326 | switch (size) { | |
327 | case I2C_SMBUS_QUICK: | |
328 | msg[0].len = 0; | |
329 | /* Special case: The read/write field is used as data */ | |
330 | msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? | |
331 | I2C_M_RD : 0); | |
332 | num = 1; | |
333 | break; | |
334 | case I2C_SMBUS_BYTE: | |
335 | if (read_write == I2C_SMBUS_READ) { | |
336 | /* Special case: only a read! */ | |
337 | msg[0].flags = I2C_M_RD | flags; | |
338 | num = 1; | |
339 | } | |
340 | break; | |
341 | case I2C_SMBUS_BYTE_DATA: | |
342 | if (read_write == I2C_SMBUS_READ) | |
343 | msg[1].len = 1; | |
344 | else { | |
345 | msg[0].len = 2; | |
346 | msgbuf0[1] = data->byte; | |
347 | } | |
348 | break; | |
349 | case I2C_SMBUS_WORD_DATA: | |
350 | if (read_write == I2C_SMBUS_READ) | |
351 | msg[1].len = 2; | |
352 | else { | |
353 | msg[0].len = 3; | |
354 | msgbuf0[1] = data->word & 0xff; | |
355 | msgbuf0[2] = data->word >> 8; | |
356 | } | |
357 | break; | |
358 | case I2C_SMBUS_PROC_CALL: | |
359 | num = 2; /* Special case */ | |
360 | read_write = I2C_SMBUS_READ; | |
361 | msg[0].len = 3; | |
362 | msg[1].len = 2; | |
363 | msgbuf0[1] = data->word & 0xff; | |
364 | msgbuf0[2] = data->word >> 8; | |
365 | break; | |
366 | case I2C_SMBUS_BLOCK_DATA: | |
367 | if (read_write == I2C_SMBUS_READ) { | |
368 | msg[1].flags |= I2C_M_RECV_LEN; | |
369 | msg[1].len = 1; /* block length will be added by | |
370 | the underlying bus driver */ | |
371 | } else { | |
372 | msg[0].len = data->block[0] + 2; | |
373 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { | |
374 | dev_err(&adapter->dev, | |
375 | "Invalid block write size %d\n", | |
376 | data->block[0]); | |
377 | return -EINVAL; | |
378 | } | |
379 | for (i = 1; i < msg[0].len; i++) | |
380 | msgbuf0[i] = data->block[i-1]; | |
381 | } | |
382 | break; | |
383 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
384 | num = 2; /* Another special case */ | |
385 | read_write = I2C_SMBUS_READ; | |
386 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { | |
387 | dev_err(&adapter->dev, | |
388 | "Invalid block write size %d\n", | |
389 | data->block[0]); | |
390 | return -EINVAL; | |
391 | } | |
392 | msg[0].len = data->block[0] + 2; | |
393 | for (i = 1; i < msg[0].len; i++) | |
394 | msgbuf0[i] = data->block[i-1]; | |
395 | msg[1].flags |= I2C_M_RECV_LEN; | |
396 | msg[1].len = 1; /* block length will be added by | |
397 | the underlying bus driver */ | |
398 | break; | |
399 | case I2C_SMBUS_I2C_BLOCK_DATA: | |
400 | if (read_write == I2C_SMBUS_READ) { | |
401 | msg[1].len = data->block[0]; | |
402 | } else { | |
403 | msg[0].len = data->block[0] + 1; | |
404 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { | |
405 | dev_err(&adapter->dev, | |
406 | "Invalid block write size %d\n", | |
407 | data->block[0]); | |
408 | return -EINVAL; | |
409 | } | |
410 | for (i = 1; i <= data->block[0]; i++) | |
411 | msgbuf0[i] = data->block[i]; | |
412 | } | |
413 | break; | |
414 | default: | |
415 | dev_err(&adapter->dev, "Unsupported transaction %d\n", size); | |
416 | return -EOPNOTSUPP; | |
417 | } | |
418 | ||
419 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK | |
420 | && size != I2C_SMBUS_I2C_BLOCK_DATA); | |
421 | if (i) { | |
422 | /* Compute PEC if first message is a write */ | |
423 | if (!(msg[0].flags & I2C_M_RD)) { | |
424 | if (num == 1) /* Write only */ | |
425 | i2c_smbus_add_pec(&msg[0]); | |
426 | else /* Write followed by read */ | |
427 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); | |
428 | } | |
429 | /* Ask for PEC if last message is a read */ | |
430 | if (msg[num-1].flags & I2C_M_RD) | |
431 | msg[num-1].len++; | |
432 | } | |
433 | ||
434 | status = i2c_transfer(adapter, msg, num); | |
435 | if (status < 0) | |
436 | return status; | |
437 | ||
438 | /* Check PEC if last message is a read */ | |
439 | if (i && (msg[num-1].flags & I2C_M_RD)) { | |
440 | status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); | |
441 | if (status < 0) | |
442 | return status; | |
443 | } | |
444 | ||
445 | if (read_write == I2C_SMBUS_READ) | |
446 | switch (size) { | |
447 | case I2C_SMBUS_BYTE: | |
448 | data->byte = msgbuf0[0]; | |
449 | break; | |
450 | case I2C_SMBUS_BYTE_DATA: | |
451 | data->byte = msgbuf1[0]; | |
452 | break; | |
453 | case I2C_SMBUS_WORD_DATA: | |
454 | case I2C_SMBUS_PROC_CALL: | |
455 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); | |
456 | break; | |
457 | case I2C_SMBUS_I2C_BLOCK_DATA: | |
458 | for (i = 0; i < data->block[0]; i++) | |
459 | data->block[i+1] = msgbuf1[i]; | |
460 | break; | |
461 | case I2C_SMBUS_BLOCK_DATA: | |
462 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
463 | for (i = 0; i < msgbuf1[0] + 1; i++) | |
464 | data->block[i] = msgbuf1[i]; | |
465 | break; | |
466 | } | |
467 | return 0; | |
468 | } | |
469 | ||
470 | /** | |
471 | * i2c_smbus_xfer - execute SMBus protocol operations | |
472 | * @adapter: Handle to I2C bus | |
473 | * @addr: Address of SMBus slave on that bus | |
474 | * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) | |
475 | * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE | |
476 | * @command: Byte interpreted by slave, for protocols which use such bytes | |
477 | * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL | |
478 | * @data: Data to be read or written | |
479 | * | |
480 | * This executes an SMBus protocol operation, and returns a negative | |
481 | * errno code else zero on success. | |
482 | */ | |
483 | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, | |
484 | char read_write, u8 command, int protocol, | |
485 | union i2c_smbus_data *data) | |
486 | { | |
487 | unsigned long orig_jiffies; | |
488 | int try; | |
489 | s32 res; | |
490 | ||
491 | /* If enabled, the following two tracepoints are conditional on | |
492 | * read_write and protocol. | |
493 | */ | |
494 | trace_smbus_write(adapter, addr, flags, read_write, | |
495 | command, protocol, data); | |
496 | trace_smbus_read(adapter, addr, flags, read_write, | |
497 | command, protocol); | |
498 | ||
499 | flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; | |
500 | ||
501 | if (adapter->algo->smbus_xfer) { | |
502 | i2c_lock_bus(adapter, I2C_LOCK_SEGMENT); | |
503 | ||
504 | /* Retry automatically on arbitration loss */ | |
505 | orig_jiffies = jiffies; | |
506 | for (res = 0, try = 0; try <= adapter->retries; try++) { | |
507 | res = adapter->algo->smbus_xfer(adapter, addr, flags, | |
508 | read_write, command, | |
509 | protocol, data); | |
510 | if (res != -EAGAIN) | |
511 | break; | |
512 | if (time_after(jiffies, | |
513 | orig_jiffies + adapter->timeout)) | |
514 | break; | |
515 | } | |
516 | i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT); | |
517 | ||
518 | if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) | |
519 | goto trace; | |
520 | /* | |
521 | * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't | |
522 | * implement native support for the SMBus operation. | |
523 | */ | |
524 | } | |
525 | ||
526 | res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, | |
527 | command, protocol, data); | |
528 | ||
529 | trace: | |
530 | /* If enabled, the reply tracepoint is conditional on read_write. */ | |
531 | trace_smbus_reply(adapter, addr, flags, read_write, | |
532 | command, protocol, data); | |
533 | trace_smbus_result(adapter, addr, flags, read_write, | |
534 | command, protocol, res); | |
535 | ||
536 | return res; | |
537 | } | |
538 | EXPORT_SYMBOL(i2c_smbus_xfer); | |
539 | ||
540 | /** | |
541 | * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate | |
542 | * @client: Handle to slave device | |
543 | * @command: Byte interpreted by slave | |
544 | * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes | |
545 | * @values: Byte array into which data will be read; big enough to hold | |
546 | * the data returned by the slave. SMBus allows at most | |
547 | * I2C_SMBUS_BLOCK_MAX bytes. | |
548 | * | |
549 | * This executes the SMBus "block read" protocol if supported by the adapter. | |
550 | * If block read is not supported, it emulates it using either word or byte | |
551 | * read protocols depending on availability. | |
552 | * | |
553 | * The addresses of the I2C slave device that are accessed with this function | |
554 | * must be mapped to a linear region, so that a block read will have the same | |
555 | * effect as a byte read. Before using this function you must double-check | |
556 | * if the I2C slave does support exchanging a block transfer with a byte | |
557 | * transfer. | |
558 | */ | |
559 | s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client, | |
560 | u8 command, u8 length, u8 *values) | |
561 | { | |
562 | u8 i = 0; | |
563 | int status; | |
564 | ||
565 | if (length > I2C_SMBUS_BLOCK_MAX) | |
566 | length = I2C_SMBUS_BLOCK_MAX; | |
567 | ||
568 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) | |
569 | return i2c_smbus_read_i2c_block_data(client, command, length, values); | |
570 | ||
571 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) | |
572 | return -EOPNOTSUPP; | |
573 | ||
574 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) { | |
575 | while ((i + 2) <= length) { | |
576 | status = i2c_smbus_read_word_data(client, command + i); | |
577 | if (status < 0) | |
578 | return status; | |
579 | values[i] = status & 0xff; | |
580 | values[i + 1] = status >> 8; | |
581 | i += 2; | |
582 | } | |
583 | } | |
584 | ||
585 | while (i < length) { | |
586 | status = i2c_smbus_read_byte_data(client, command + i); | |
587 | if (status < 0) | |
588 | return status; | |
589 | values[i] = status; | |
590 | i++; | |
591 | } | |
592 | ||
593 | return i; | |
594 | } | |
595 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated); | |
3c0a60be PR |
596 | |
597 | /** | |
598 | * i2c_setup_smbus_alert - Setup SMBus alert support | |
599 | * @adapter: the target adapter | |
600 | * @setup: setup data for the SMBus alert handler | |
601 | * Context: can sleep | |
602 | * | |
603 | * Setup handling of the SMBus alert protocol on a given I2C bus segment. | |
604 | * | |
605 | * Handling can be done either through our IRQ handler, or by the | |
606 | * adapter (from its handler, periodic polling, or whatever). | |
607 | * | |
608 | * NOTE that if we manage the IRQ, we *MUST* know if it's level or | |
609 | * edge triggered in order to hand it to the workqueue correctly. | |
610 | * If triggering the alert seems to wedge the system, you probably | |
611 | * should have said it's level triggered. | |
612 | * | |
613 | * This returns the ara client, which should be saved for later use with | |
614 | * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL | |
615 | * to indicate an error. | |
616 | */ | |
617 | struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter, | |
618 | struct i2c_smbus_alert_setup *setup) | |
619 | { | |
620 | struct i2c_board_info ara_board_info = { | |
621 | I2C_BOARD_INFO("smbus_alert", 0x0c), | |
622 | .platform_data = setup, | |
623 | }; | |
624 | ||
625 | return i2c_new_device(adapter, &ara_board_info); | |
626 | } | |
627 | EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert); |