diff options
author | Shawn Nematbakhsh <shawnn@chromium.org> | 2015-05-22 16:59:06 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2015-05-26 22:39:52 +0000 |
commit | 2259e8ffb7645e0decfadd95fb4d1cdda8208095 (patch) | |
tree | 4f0d32e89c7cc4adbd23fdc2f93762e094fc5fff | |
parent | 51113874f0f903a6b9c5ae2de643093dad68a184 (diff) | |
download | chrome-ec-2259e8ffb7645e0decfadd95fb4d1cdda8208095.tar.gz |
i2c: Move i2c_read_string to common code
Since stm32 and mec1322 now support open-ended i2c_xfer, we can move the
lm4 i2c_read_string implementation to common code and delete all
chip-specific versions.
BUG=chrome-os-partner:39613
TEST=Run "battery" from EC console on Cyan and Oak, verify that battery
info + strings are correctly printed.
BRANCH=None
Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org>
Change-Id: I06369df64bb2eb747d163664b4c96eeacb4b1faa
Reviewed-on: https://chromium-review.googlesource.com/272938
Reviewed-by: Alec Berg <alecaberg@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r-- | chip/lm4/i2c.c | 30 | ||||
-rw-r--r-- | chip/mec1322/i2c.c | 35 | ||||
-rw-r--r-- | chip/npcx/i2c.c | 35 | ||||
-rw-r--r-- | chip/stm32/build.mk | 2 | ||||
-rw-r--r-- | chip/stm32/i2c.c | 39 | ||||
-rw-r--r-- | common/i2c.c | 30 |
6 files changed, 31 insertions, 140 deletions
diff --git a/chip/lm4/i2c.c b/chip/lm4/i2c.c index 56b4666330..5f3b9bfc74 100644 --- a/chip/lm4/i2c.c +++ b/chip/lm4/i2c.c @@ -305,36 +305,6 @@ int i2c_get_line_levels(int port) return LM4_I2C_MBMON(port) & 0x03; } -int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data, - int len) -{ - int rv; - uint8_t reg, block_length; - - i2c_lock(port, 1); - - reg = offset; - /* - * Send device reg space offset, and read back block length. Keep this - * session open without a stop. - */ - rv = i2c_xfer(port, slave_addr, ®, 1, &block_length, 1, - I2C_XFER_START); - if (rv) - goto exit; - - if (len && block_length > (len - 1)) - block_length = len - 1; - - rv = i2c_xfer(port, slave_addr, 0, 0, data, block_length, - I2C_XFER_STOP); - data[block_length] = 0; - -exit: - i2c_lock(port, 0); - return rv; -} - void i2c_set_timeout(int port, uint32_t timeout) { pdata[port].timeout_us = timeout ? timeout : I2C_TIMEOUT_DEFAULT_US; diff --git a/chip/mec1322/i2c.c b/chip/mec1322/i2c.c index dd75daa845..e7254a01f9 100644 --- a/chip/mec1322/i2c.c +++ b/chip/mec1322/i2c.c @@ -379,41 +379,6 @@ int i2c_get_line_levels(int port) return rv; } -int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data, - int len) -{ - int rv; - uint8_t reg, block_length; - - if ((len <= 0) || (len > SMBUS_MAX_BLOCK_SIZE)) - return EC_ERROR_INVAL; - - i2c_lock(port, 1); - - /* - * Read the counted string into the output buffer - */ - reg = offset; - rv = i2c_xfer(port, slave_addr, ®, 1, data, len, I2C_XFER_SINGLE); - if (rv) - goto exit; - - /* - * Block length is the first byte of the returned buffer - */ - block_length = MIN(data[0], len - 1); - - /* - * Move data down, then null-terminate it - */ - memmove(data, data + 1, block_length); - data[block_length] = '\0'; - -exit: - i2c_lock(port, 0); - return rv; -} - int i2c_port_to_controller(int port) { if (port < 0 || port >= MEC1322_I2C_PORT_COUNT) diff --git a/chip/npcx/i2c.c b/chip/npcx/i2c.c index 33f512cc22..a8002d7573 100644 --- a/chip/npcx/i2c.c +++ b/chip/npcx/i2c.c @@ -481,41 +481,6 @@ int i2c_raw_get_sda(int port) return 1; } -int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data, - int len) -{ - int rv; - uint8_t reg, block_length; - - /* Check block protocol size */ - if ((len <= 0) || (len > 32)) - return EC_ERROR_INVAL; - - i2c_lock(port, 1); - reg = offset; - - /* - * Send device reg space offset, and read back block length. Keep this - * session open without a stop. - */ - rv = i2c_xfer(port, slave_addr, ®, 1, &block_length, 1, - I2C_XFER_START); - if (rv) - goto exit; - - if (len && block_length > (len - 1)) - block_length = len - 1; - - rv = i2c_xfer(port, slave_addr, 0, 0, data, block_length, - I2C_XFER_STOP); - data[block_length] = 0; - -exit: - i2c_lock(port, 0); - - return rv; -} - /*****************************************************************************/ /* Hooks */ static void i2c_freq_changed(void) diff --git a/chip/stm32/build.mk b/chip/stm32/build.mk index 326edfb830..19460eacde 100644 --- a/chip/stm32/build.mk +++ b/chip/stm32/build.mk @@ -33,7 +33,7 @@ chip-$(CONFIG_SPI)+=spi.o chip-$(CONFIG_SPI_MASTER_PORT)+=spi_master.o chip-$(CONFIG_COMMON_GPIO)+=gpio.o gpio-$(CHIP_FAMILY).o chip-$(CONFIG_COMMON_TIMER)+=hwtimer$(TIMER_TYPE).o -chip-$(CONFIG_I2C)+=i2c-$(CHIP_FAMILY).o i2c.o +chip-$(CONFIG_I2C)+=i2c-$(CHIP_FAMILY).o chip-$(CONFIG_STREAM_USART)+=usart.o usart-$(CHIP_FAMILY).o chip-$(CONFIG_STREAM_USB)+=usb-stream.o chip-$(CONFIG_WATCHDOG)+=watchdog.o diff --git a/chip/stm32/i2c.c b/chip/stm32/i2c.c deleted file mode 100644 index 9cea83a619..0000000000 --- a/chip/stm32/i2c.c +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -/* STM32 common I2C code for Chrome EC */ - -#include "i2c.h" -#include "util.h" - -/* Maximum transfer of a SMBUS block transfer */ -#define SMBUS_MAX_BLOCK 32 - -int i2c_read_string(int port, int slave_addr, - int offset, uint8_t *data, int len) -{ - int rv; - uint8_t reg, block_length; - - if ((len <= 0) || (len > SMBUS_MAX_BLOCK)) - return EC_ERROR_INVAL; - - i2c_lock(port, 1); - - /* Read the counted string into the output buffer */ - reg = offset; - rv = i2c_xfer(port, slave_addr, ®, 1, data, len, I2C_XFER_SINGLE); - if (rv == EC_SUCCESS) { - /* Block length is the first byte of the returned buffer */ - block_length = MIN(data[0], len - 1); - - /* Move data down, then null-terminate it */ - memmove(data, data + 1, block_length); - data[block_length] = 0; - } - - i2c_lock(port, 0); - return rv; -} diff --git a/common/i2c.c b/common/i2c.c index 800922adea..9659db59c3 100644 --- a/common/i2c.c +++ b/common/i2c.c @@ -130,6 +130,36 @@ int i2c_write8(int port, int slave_addr, int offset, int data) return rv; } +int i2c_read_string(int port, int slave_addr, int offset, uint8_t *data, + int len) +{ + int rv; + uint8_t reg, block_length; + + i2c_lock(port, 1); + + reg = offset; + /* + * Send device reg space offset, and read back block length. Keep this + * session open without a stop. + */ + rv = i2c_xfer(port, slave_addr, ®, 1, &block_length, 1, + I2C_XFER_START); + if (rv) + goto exit; + + if (len && block_length > (len - 1)) + block_length = len - 1; + + rv = i2c_xfer(port, slave_addr, 0, 0, data, block_length, + I2C_XFER_STOP); + data[block_length] = 0; + +exit: + i2c_lock(port, 0); + return rv; +} + int get_sda_from_i2c_port(int port, enum gpio_signal *sda) { int i; |