2 * Chromium OS cros_ec driver - I2C interface
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
10 * The Matrix Keyboard Protocol driver handles talking to the keyboard
11 * controller chip. Mostly this is for keyboard functions, but some other
12 * things have slipped in, so we provide generic services to talk to the
21 #define debug_trace(fmt, b...) debug(fmt, #b)
23 #define debug_trace(fmt, b...)
26 int cros_ec_i2c_command(struct cros_ec_dev
*dev
, uint8_t cmd
, int cmd_version
,
27 const uint8_t *dout
, int dout_len
,
28 uint8_t **dinp
, int din_len
)
31 /* version8, cmd8, arglen8, out8[dout_len], csum8 */
32 int out_bytes
= dout_len
+ 4;
33 /* response8, arglen8, in8[din_len], checksum8 */
34 int in_bytes
= din_len
+ 3;
36 /* Receive input data, so that args will be dword aligned */
40 old_bus
= i2c_get_bus_num();
43 * Sanity-check I/O sizes given transaction overhead in internal
46 if (out_bytes
> sizeof(dev
->dout
)) {
47 debug("%s: Cannot send %d bytes\n", __func__
, dout_len
);
50 if (in_bytes
> sizeof(dev
->din
)) {
51 debug("%s: Cannot receive %d bytes\n", __func__
, din_len
);
54 assert(dout_len
>= 0);
58 * Copy command and data into output buffer so we can do a single I2C
64 * in_ptr starts of pointing to a dword-aligned input data buffer.
65 * We decrement it back by the number of header bytes we expect to
66 * receive, so that the first parameter of the resulting input data
67 * will be dword aligned.
69 in_ptr
= dev
->din
+ sizeof(int64_t);
71 if (dev
->protocol_version
!= 2) {
72 /* Something we don't support */
73 debug("%s: Protocol version %d unsupported\n",
74 __func__
, dev
->protocol_version
);
78 *ptr
++ = EC_CMD_VERSION0
+ cmd_version
;
81 in_ptr
-= 2; /* Expect status, length bytes */
83 memcpy(ptr
, dout
, dout_len
);
87 cros_ec_calc_checksum(dev
->dout
, dout_len
+ 3);
89 /* Set to the proper i2c bus */
90 if (i2c_set_bus_num(dev
->bus_num
)) {
91 debug("%s: Cannot change to I2C bus %d\n", __func__
,
96 /* Send output data */
97 cros_ec_dump_data("out", -1, dev
->dout
, out_bytes
);
98 ret
= i2c_write(dev
->addr
, 0, 0, dev
->dout
, out_bytes
);
100 debug("%s: Cannot complete I2C write to 0x%x\n",
101 __func__
, dev
->addr
);
106 ret
= i2c_read(dev
->addr
, 0, 0, in_ptr
, in_bytes
);
108 debug("%s: Cannot complete I2C read from 0x%x\n",
109 __func__
, dev
->addr
);
114 /* Return to original bus number */
115 i2c_set_bus_num(old_bus
);
119 if (*in_ptr
!= EC_RES_SUCCESS
) {
120 debug("%s: Received bad result code %d\n", __func__
, *in_ptr
);
121 return -(int)*in_ptr
;
125 if (len
+ 3 > sizeof(dev
->din
)) {
126 debug("%s: Received length %#02x too large\n",
130 csum
= cros_ec_calc_checksum(in_ptr
, 2 + len
);
131 if (csum
!= in_ptr
[2 + len
]) {
132 debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
133 __func__
, in_ptr
[2 + din_len
], csum
);
136 din_len
= min(din_len
, len
);
137 cros_ec_dump_data("in", -1, in_ptr
, din_len
+ 3);
139 /* Return pointer to dword-aligned input data, if any */
140 *dinp
= dev
->din
+ sizeof(int64_t);
145 int cros_ec_i2c_decode_fdt(struct cros_ec_dev
*dev
, const void *blob
)
147 /* Decode interface-specific FDT params */
148 dev
->max_frequency
= fdtdec_get_int(blob
, dev
->node
,
149 "i2c-max-frequency", 100000);
150 dev
->bus_num
= i2c_get_bus_num_fdt(dev
->parent_node
);
151 if (dev
->bus_num
== -1) {
152 debug("%s: Failed to read bus number\n", __func__
);
155 dev
->addr
= fdtdec_get_int(blob
, dev
->node
, "reg", -1);
156 if (dev
->addr
== -1) {
157 debug("%s: Failed to read device address\n", __func__
);
165 * Initialize I2C protocol.
167 * @param dev CROS_EC device
168 * @param blob Device tree blob
169 * @return 0 if ok, -1 on error
171 int cros_ec_i2c_init(struct cros_ec_dev
*dev
, const void *blob
)
173 i2c_init(dev
->max_frequency
, dev
->addr
);