2 * cros_ec_lpc_reg - LPC access to the Chrome OS Embedded Controller
4 * Copyright (C) 2016 Google, Inc
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * This driver uses the Chrome OS EC byte-level message-based protocol for
16 * communicating the keyboard state (which keys are pressed) from a keyboard EC
17 * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
18 * but everything else (including deghosting) is done here. The main
19 * motivation for this is to keep the EC firmware as simple as possible, since
20 * it cannot be easily upgraded and EC flash/IRAM space is relatively
25 #include <linux/mfd/cros_ec.h>
26 #include <linux/mfd/cros_ec_commands.h>
28 #include "cros_ec_lpc_mec.h"
30 static u8 lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
35 for (i = 0; i < length; ++i) {
36 dest[i] = inb(offset + i);
40 /* Return checksum of all bytes read */
44 static u8 lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
49 for (i = 0; i < length; ++i) {
50 outb(msg[i], offset + i);
54 /* Return checksum of all bytes written */
58 #ifdef CONFIG_CROS_EC_LPC_MEC
60 u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
65 /* Access desired range through EMI interface */
66 if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
67 /* Ensure we don't straddle EMI region */
68 if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
71 return cros_ec_lpc_io_bytes_mec(MEC_IO_READ, offset, length,
75 if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
76 offset < MEC_EMI_RANGE_START))
79 return lpc_read_bytes(offset, length, dest);
82 u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
87 /* Access desired range through EMI interface */
88 if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
89 /* Ensure we don't straddle EMI region */
90 if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
93 return cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, offset, length,
97 if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
98 offset < MEC_EMI_RANGE_START))
101 return lpc_write_bytes(offset, length, msg);
104 void cros_ec_lpc_reg_init(void)
106 cros_ec_lpc_mec_init();
109 void cros_ec_lpc_reg_destroy(void)
111 cros_ec_lpc_mec_destroy();
114 #else /* CONFIG_CROS_EC_LPC_MEC */
116 u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
118 return lpc_read_bytes(offset, length, dest);
121 u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
123 return lpc_write_bytes(offset, length, msg);
126 void cros_ec_lpc_reg_init(void)
130 void cros_ec_lpc_reg_destroy(void)
134 #endif /* CONFIG_CROS_EC_LPC_MEC */