cros_ec_lpc_reg.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * cros_ec_lpc_reg - LPC access to the Chrome OS Embedded Controller
  3. *
  4. * Copyright (C) 2016 Google, Inc
  5. *
  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.
  9. *
  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.
  14. *
  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
  21. * expensive.
  22. */
  23. #include <linux/io.h>
  24. #include <linux/mfd/cros_ec.h>
  25. #include <linux/mfd/cros_ec_commands.h>
  26. #include <linux/mfd/cros_ec_lpc_mec.h>
  27. static u8 lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
  28. {
  29. int i;
  30. int sum = 0;
  31. for (i = 0; i < length; ++i) {
  32. dest[i] = inb(offset + i);
  33. sum += dest[i];
  34. }
  35. /* Return checksum of all bytes read */
  36. return sum;
  37. }
  38. static u8 lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
  39. {
  40. int i;
  41. int sum = 0;
  42. for (i = 0; i < length; ++i) {
  43. outb(msg[i], offset + i);
  44. sum += msg[i];
  45. }
  46. /* Return checksum of all bytes written */
  47. return sum;
  48. }
  49. #ifdef CONFIG_CROS_EC_LPC_MEC
  50. u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
  51. {
  52. if (length == 0)
  53. return 0;
  54. /* Access desired range through EMI interface */
  55. if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
  56. /* Ensure we don't straddle EMI region */
  57. if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
  58. return 0;
  59. return cros_ec_lpc_io_bytes_mec(MEC_IO_READ, offset, length,
  60. dest);
  61. }
  62. if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
  63. offset < MEC_EMI_RANGE_START))
  64. return 0;
  65. return lpc_read_bytes(offset, length, dest);
  66. }
  67. u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
  68. {
  69. if (length == 0)
  70. return 0;
  71. /* Access desired range through EMI interface */
  72. if (offset >= MEC_EMI_RANGE_START && offset <= MEC_EMI_RANGE_END) {
  73. /* Ensure we don't straddle EMI region */
  74. if (WARN_ON(offset + length - 1 > MEC_EMI_RANGE_END))
  75. return 0;
  76. return cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, offset, length,
  77. msg);
  78. }
  79. if (WARN_ON(offset + length > MEC_EMI_RANGE_START &&
  80. offset < MEC_EMI_RANGE_START))
  81. return 0;
  82. return lpc_write_bytes(offset, length, msg);
  83. }
  84. void cros_ec_lpc_reg_init(void)
  85. {
  86. cros_ec_lpc_mec_init();
  87. }
  88. void cros_ec_lpc_reg_destroy(void)
  89. {
  90. cros_ec_lpc_mec_destroy();
  91. }
  92. #else /* CONFIG_CROS_EC_LPC_MEC */
  93. u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length, u8 *dest)
  94. {
  95. return lpc_read_bytes(offset, length, dest);
  96. }
  97. u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length, u8 *msg)
  98. {
  99. return lpc_write_bytes(offset, length, msg);
  100. }
  101. void cros_ec_lpc_reg_init(void)
  102. {
  103. }
  104. void cros_ec_lpc_reg_destroy(void)
  105. {
  106. }
  107. #endif /* CONFIG_CROS_EC_LPC_MEC */