summaryrefslogtreecommitdiff
path: root/include/smbus.h
blob: 15d42084b0c264a3dd977c5ca14925c00f7b302e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * @file smbus.h
 * @brief smbus interface APIs
 * @see http://smbus.org/specs/smbus20.pdf
 */
#ifndef __CROS_EC_SMBUS_H
#define __CROS_EC_SMBUS_H

/** Maximum transfer of a SMBUS block transfer */
#define SMBUS_MAX_BLOCK_SIZE 32

/**
 * smbus write word
 *   write 2 byte data + 1 byte pec
 */
struct smbus_wr_word {
	uint8_t slave_addr;/**< i2c_addr << 1 */
	uint8_t smbus_cmd; /**< smbus cmd */
	uint8_t data[3];   /**< smbus data */
} __packed;

/**
 * smbus write block data
 * smbus write 1 byte size + 32 byte data + 1 byte pec
 */
struct smbus_wr_block {
	uint8_t slave_addr;/**< i2c_addr << 1 */
	uint8_t smbus_cmd; /**< smbus cmd */
	uint8_t size;      /**< write size */
	uint8_t data[SMBUS_MAX_BLOCK_SIZE+1];
} __packed;

/**
 * smbus read word
 * smbus read 2 byte + 1 pec
 */
struct smbus_rd_word {
	uint8_t slave_addr;   /**< i2c_addr << 1 */
	uint8_t smbus_cmd;    /**< smbus cmd */
	uint8_t slave_addr_rd;/**< (i2c_addr << 1) | 0x1 */
	uint8_t data[3];      /**< smbus data */
} __packed;

/**
 * smbus read block data
 * smbus read 1 byte size + 32 byte data + 1 byte pec
 */
struct smbus_rd_block {
	uint8_t slave_addr;   /**< i2c_addr << 1 */
	uint8_t smbus_cmd;    /**< smbus cmd */
	uint8_t slave_addr_rd;/**< (i2c_addr << 1) | 0x1 */
	uint8_t size;         /**< read block size */
	uint8_t data[SMBUS_MAX_BLOCK_SIZE+1]; /**< smbus data */
} __packed;

/**
 * smbus_write_word
 * smbus write 2 bytes
 * @param i2c_port uint8_t, i2c port address
 * @param slave_addr uint8_t, i2c slave address:= (i2c_addr << 1)
 * @param smbus_cmd uint8_t, smbus command
 * @param d16 uint16_t, 2-bytes data
 * @return error_code
 *       EC_SUCCESS if success; none-zero if fail
 *       EC_ERROR_BUSY if interface is bussy
 *       none zero error code if fail
 */
int smbus_write_word(uint8_t i2c_port, uint8_t slave_addr,
			uint8_t smbus_cmd, uint16_t d16);

/**
 * smbus_write_block
 * smbus write upto 32 bytes
 *   case 1:  n-1 byte data, 1 byte PEC
 *     [S][i2c Address][Wr=0][A][cmd][A] ...[Di][Ai]... [PEC][A][P]
 *
 *   case 2:  1 byte data-size, n -2 byte data, 1 byte PEC
 *     [S][i2c Address][Wr=0][A][cmd][A][size][A] ...[Di][Ai]... [PEC][A][P]
 *
 * @param i2c_port uint8_t, i2c port address
 * @param slave_addr uint8_t, i2c slave address:= (i2c_addr << 1)
 * @param smbus_cmd uint8_t, smbus command
 * @param data uint8_t *, n-bytes data
 * @param len uint8_t,  data length
 * @return error_code
 *       EC_SUCCESS if success; none-zero if fail
 *       EC_ERROR_BUSY if interface is bussy
 *       none zero error code if fail
 */
int smbus_write_block(uint8_t i2c_port, uint8_t slave_addr,
			uint8_t smbus_cmd, uint8_t *data, uint8_t len);

/**
 * smbus_read_word
 * smbus read 2 bytes
 * @param i2c_port uint8_t, i2c port address
 * @param slave_addr uint8_t, i2c slave address:= (i2c_addr << 1)
 * @param smbus_cmd uint8_t, smbus command
 * @param p16 uint16_t *, a pointer to 2-bytes data
 * @return error_code
 *       EC_SUCCESS if success; none-zero if fail
 *       EC_ERROR_BUSY if interface is bussy
 *       none zero error code if fail
 */
int smbus_read_word(uint8_t i2c_port, uint8_t slave_addr,
			uint8_t smbus_cmd, uint16_t *p16);

/**
 * smbus_read_block
 * smbus read upto 32 bytes
 * case 1:  n-1 byte data, 1 byte PEC
 *    [S][i2c addr][Wr=0][A][cmd][A]
 *    [S][i2c addr][Rd=1][A]...[Di][Ai]...[PEC][A][P]
 *
 * case 2:  1 byte data-size, n - 2 byte data, 1 byte PEC
 *    [S][i2c addr][Wr=0][A][cmd][A]
 *    [S][i2c addr][Rd=1][A][size][A]...[Di][Ai]...[PEC][A][P]
 *
 * @param i2c_port uint8_t, i2c port address
 * @param slave_addr uint8_t, i2c slave address:= (i2c_addr << 1)
 * @param smbus_cmd uint8_t, smbus command
 * @param data uint8_t *, n-bytes data
 * @param plen uint8_t *, a pointer data length
 * @return error_code
 *       EC_SUCCESS if success; none-zero if fail
 *       EC_ERROR_BUSY if interface is busy
 *       none zero error code if fail
 */
int smbus_read_block(uint8_t i2c_port, uint8_t slave_addr,
			uint8_t smbus_cmd, uint8_t *data, uint8_t *plen);

/**
 * smbus_read_string
 * smbus read ascii string (upto 32-byte data + 1-byte NULL)
 * Read bytestream from <slaveaddr>:<smbus_cmd> with format:
 *     [length_N] [byte_0] [byte_1] ... [byte_N-1][byte_N='\0']
 *
 * <len>  : the max length of receiving buffer. to read N bytes
 *          ascii, len should be at least N+1 to include the
 *          terminating 0 (NULL).
 *
 * @param i2c_port uint8_t, i2c port address
 * @param slave_addr uint8_t, i2c slave address:= (i2c_addr << 1)
 * @param smbus_cmd uint8_t, smbus command
 * @param data uint8_t *, n-bytes data
 * @param len uint8_t,  data length
 * @return error_code
 *       EC_SUCCESS if success; none-zero if fail
 *       EC_ERROR_BUSY if interface is bussy
 *       none zero error code if fail
 */
int smbus_read_string(int i2c_port, uint8_t slave_addr, uint8_t smbus_cmd,
			uint8_t *data, uint8_t len);

#endif /* __CROS_EC_SMBUS_H */