2 * Chromium OS cros_ec driver - SPI interface
4 * Copyright (c) 2012 The Chromium OS Authors.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * The Matrix Keyboard Protocol driver handles talking to the keyboard
26 * controller chip. Mostly this is for keyboard functions, but some other
27 * things have slipped in, so we provide generic services to talk to the
36 * Send a command to a LPC CROS_EC device and return the reply.
38 * The device's internal input/output buffers are used.
40 * @param dev CROS_EC device
41 * @param cmd Command to send (EC_CMD_...)
42 * @param cmd_version Version of command to send (EC_VER_...)
43 * @param dout Output data (may be NULL If dout_len=0)
44 * @param dout_len Size of output data in bytes
45 * @param dinp Returns pointer to response data. This will be
46 * untouched unless we return a value > 0.
47 * @param din_len Maximum size of response in bytes
48 * @return number of bytes in response, or -1 on error
50 int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
51 const uint8_t *dout, int dout_len,
52 uint8_t **dinp, int din_len)
54 int in_bytes = din_len + 4; /* status, length, checksum, trailer */
61 * Sanity-check input size to make sure it plus transaction overhead
62 * fits in the internal device buffer.
64 if (in_bytes > sizeof(dev->din)) {
65 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
69 /* We represent message length as a byte */
70 if (dout_len > 0xff) {
71 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
76 * Clear input buffer so we don't get false hits for MSG_HEADER
78 memset(dev->din, '\0', in_bytes);
80 if (spi_claim_bus(dev->spi)) {
81 debug("%s: Cannot claim SPI bus\n", __func__);
88 out[2] = (uint8_t)dout_len;
89 memcpy(out + 3, dout, dout_len);
90 csum = cros_ec_calc_checksum(out, 3)
91 + cros_ec_calc_checksum(dout, dout_len);
92 out[3 + dout_len] = (uint8_t)csum;
95 * Send output data and receive input data starting such that the
96 * message body will be dword aligned.
98 p = dev->din + sizeof(int64_t) - 2;
100 cros_ec_dump_data("out", cmd, out, len);
101 rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p,
102 SPI_XFER_BEGIN | SPI_XFER_END);
104 spi_release_bus(dev->spi);
107 debug("%s: Cannot complete SPI transfer\n", __func__);
111 len = min(p[1], din_len);
112 cros_ec_dump_data("in", -1, p, len + 3);
114 /* Response code is first byte of message */
115 if (p[0] != EC_RES_SUCCESS) {
116 printf("%s: Returned status %d\n", __func__, p[0]);
121 csum = cros_ec_calc_checksum(p, len + 2);
122 if (csum != p[len + 2]) {
123 debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
128 /* Anything else is the response data */
134 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
136 /* Decode interface-specific FDT params */
137 dev->max_frequency = fdtdec_get_int(blob, dev->node,
138 "spi-max-frequency", 500000);
139 dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0);
145 * Initialize SPI protocol.
147 * @param dev CROS_EC device
148 * @param blob Device tree blob
149 * @return 0 if ok, -1 on error
151 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
153 dev->spi = spi_setup_slave_fdt(blob, dev->parent_node,
154 dev->cs, dev->max_frequency, 0);
156 debug("%s: Could not setup SPI slave\n", __func__);