blob: 513cdb1cb09808be69ac6a44be2cb651ad9ac347 [file] [log] [blame]
Hung-ying Tyan78764a42013-05-15 18:27:29 +08001/*
2 * Chromium OS cros_ec driver - I2C interface
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan78764a42013-05-15 18:27:29 +08005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan78764a42013-05-15 18:27:29 +08007 */
8
9/*
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
13 * KBC.
14 */
15
16#include <common.h>
17#include <i2c.h>
18#include <cros_ec.h>
19
20#ifdef DEBUG_TRACE
21#define debug_trace(fmt, b...) debug(fmt, #b)
22#else
23#define debug_trace(fmt, b...)
24#endif
25
26int 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)
29{
30 int old_bus = 0;
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;
35 uint8_t *ptr;
36 /* Receive input data, so that args will be dword aligned */
37 uint8_t *in_ptr;
Randall Spanglere8c12662014-02-27 13:26:08 -070038 int len, csum, ret;
Hung-ying Tyan78764a42013-05-15 18:27:29 +080039
40 old_bus = i2c_get_bus_num();
41
42 /*
43 * Sanity-check I/O sizes given transaction overhead in internal
44 * buffers.
45 */
46 if (out_bytes > sizeof(dev->dout)) {
47 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
48 return -1;
49 }
50 if (in_bytes > sizeof(dev->din)) {
51 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
52 return -1;
53 }
54 assert(dout_len >= 0);
55 assert(dinp);
56
57 /*
58 * Copy command and data into output buffer so we can do a single I2C
59 * burst transaction.
60 */
61 ptr = dev->dout;
62
63 /*
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.
68 */
69 in_ptr = dev->din + sizeof(int64_t);
Randall Spanglere8c12662014-02-27 13:26:08 -070070
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);
75 return -1;
Hung-ying Tyan78764a42013-05-15 18:27:29 +080076 }
Randall Spanglere8c12662014-02-27 13:26:08 -070077
78 *ptr++ = EC_CMD_VERSION0 + cmd_version;
79 *ptr++ = cmd;
80 *ptr++ = dout_len;
81 in_ptr -= 2; /* Expect status, length bytes */
82
Hung-ying Tyan78764a42013-05-15 18:27:29 +080083 memcpy(ptr, dout, dout_len);
84 ptr += dout_len;
85
Randall Spanglere8c12662014-02-27 13:26:08 -070086 *ptr++ = (uint8_t)
87 cros_ec_calc_checksum(dev->dout, dout_len + 3);
Hung-ying Tyan78764a42013-05-15 18:27:29 +080088
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__,
92 dev->bus_num);
93 return -1;
94 }
95
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);
99 if (ret) {
100 debug("%s: Cannot complete I2C write to 0x%x\n",
101 __func__, dev->addr);
102 ret = -1;
103 }
104
105 if (!ret) {
106 ret = i2c_read(dev->addr, 0, 0, in_ptr, in_bytes);
107 if (ret) {
108 debug("%s: Cannot complete I2C read from 0x%x\n",
109 __func__, dev->addr);
110 ret = -1;
111 }
112 }
113
114 /* Return to original bus number */
115 i2c_set_bus_num(old_bus);
116 if (ret)
117 return ret;
118
119 if (*in_ptr != EC_RES_SUCCESS) {
120 debug("%s: Received bad result code %d\n", __func__, *in_ptr);
121 return -(int)*in_ptr;
122 }
123
Randall Spanglere8c12662014-02-27 13:26:08 -0700124 len = in_ptr[1];
125 if (len + 3 > sizeof(dev->din)) {
126 debug("%s: Received length %#02x too large\n",
127 __func__, len);
128 return -1;
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800129 }
Randall Spanglere8c12662014-02-27 13:26:08 -0700130 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);
134 return -1;
135 }
136 din_len = min(din_len, len);
137 cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800138
139 /* Return pointer to dword-aligned input data, if any */
140 *dinp = dev->din + sizeof(int64_t);
141
142 return din_len;
143}
144
145int cros_ec_i2c_decode_fdt(struct cros_ec_dev *dev, const void *blob)
146{
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__);
153 return -1;
154 }
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__);
158 return -1;
159 }
160
161 return 0;
162}
163
164/**
165 * Initialize I2C protocol.
166 *
167 * @param dev CROS_EC device
168 * @param blob Device tree blob
169 * @return 0 if ok, -1 on error
170 */
171int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob)
172{
173 i2c_init(dev->max_frequency, dev->addr);
174
Hung-ying Tyan78764a42013-05-15 18:27:29 +0800175 return 0;
176}