]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/misc/cros_ec_i2c.c
0fbab991b5b0af8d121a6e71efc720665fe2b60d
[karo-tx-uboot.git] / drivers / misc / cros_ec_i2c.c
1 /*
2  * Chromium OS cros_ec driver - I2C interface
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
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
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)
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;
38         int ret;
39
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);
70         if (!dev->cmd_version_is_supported) {
71                 /* Send an old-style command */
72                 *ptr++ = cmd;
73                 out_bytes = dout_len + 1;
74                 in_bytes = din_len + 2;
75                 in_ptr--;       /* Expect just a status byte */
76         } else {
77                 *ptr++ = EC_CMD_VERSION0 + cmd_version;
78                 *ptr++ = cmd;
79                 *ptr++ = dout_len;
80                 in_ptr -= 2;    /* Expect status, length bytes */
81         }
82         memcpy(ptr, dout, dout_len);
83         ptr += dout_len;
84
85         if (dev->cmd_version_is_supported)
86                 *ptr++ = (uint8_t)
87                          cros_ec_calc_checksum(dev->dout, dout_len + 3);
88
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
124         if (dev->cmd_version_is_supported) {
125                 int len, csum;
126
127                 len = in_ptr[1];
128                 if (len + 3 > sizeof(dev->din)) {
129                         debug("%s: Received length %#02x too large\n",
130                               __func__, len);
131                         return -1;
132                 }
133                 csum = cros_ec_calc_checksum(in_ptr, 2 + len);
134                 if (csum != in_ptr[2 + len]) {
135                         debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
136                               __func__, in_ptr[2 + din_len], csum);
137                         return -1;
138                 }
139                 din_len = min(din_len, len);
140                 cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
141         } else {
142                 cros_ec_dump_data("in (old)", -1, in_ptr, in_bytes);
143         }
144
145         /* Return pointer to dword-aligned input data, if any */
146         *dinp = dev->din + sizeof(int64_t);
147
148         return din_len;
149 }
150
151 int cros_ec_i2c_decode_fdt(struct cros_ec_dev *dev, const void *blob)
152 {
153         /* Decode interface-specific FDT params */
154         dev->max_frequency = fdtdec_get_int(blob, dev->node,
155                                             "i2c-max-frequency", 100000);
156         dev->bus_num = i2c_get_bus_num_fdt(dev->parent_node);
157         if (dev->bus_num == -1) {
158                 debug("%s: Failed to read bus number\n", __func__);
159                 return -1;
160         }
161         dev->addr = fdtdec_get_int(blob, dev->node, "reg", -1);
162         if (dev->addr == -1) {
163                 debug("%s: Failed to read device address\n", __func__);
164                 return -1;
165         }
166
167         return 0;
168 }
169
170 /**
171  * Initialize I2C protocol.
172  *
173  * @param dev           CROS_EC device
174  * @param blob          Device tree blob
175  * @return 0 if ok, -1 on error
176  */
177 int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob)
178 {
179         i2c_init(dev->max_frequency, dev->addr);
180
181         dev->cmd_version_is_supported = 0;
182
183         return 0;
184 }