summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorNicolas Boichat <drinkcat@google.com>2016-05-17 17:29:17 +0800
committerchrome-bot <chrome-bot@chromium.org>2016-05-24 19:23:27 -0700
commitd2bbc229f378b741bfc2ff0d2092d501531626a9 (patch)
tree1cf377b9da3396614a96b9118986c1dbc557f304 /common
parent90b934d4e95c3ac8f100fc2447ff3c51960ff268 (diff)
downloadchrome-ec-d2bbc229f378b741bfc2ff0d2092d501531626a9.tar.gz
common/i2c: Add get_i2c_port function
Also, remove port_is_valid which essentially does the same thing, and is now redundant. BRANCH=none BUG=chrome-os-partner:52431 TEST=Book elm-rev1 Change-Id: I8e13e18b58fbb185de1e354fdd45acf7c6be39bf Signed-off-by: Nicolas Boichat <drinkcat@google.com> Reviewed-on: https://chromium-review.googlesource.com/345760 Commit-Ready: Nicolas Boichat <drinkcat@chromium.org> Tested-by: Nicolas Boichat <drinkcat@chromium.org> Reviewed-by: Randall Spangler <rspangler@google.com>
Diffstat (limited to 'common')
-rw-r--r--common/i2c.c64
1 files changed, 26 insertions, 38 deletions
diff --git a/common/i2c.c b/common/i2c.c
index c06df9d784..f22c86540e 100644
--- a/common/i2c.c
+++ b/common/i2c.c
@@ -35,6 +35,19 @@
static struct mutex port_mutex[I2C_CONTROLLER_COUNT];
static uint32_t i2c_port_active_count;
+const struct i2c_port_t *get_i2c_port(int port)
+{
+ int i;
+
+ /* Find the matching port in i2c_ports[] table. */
+ for (i = 0; i < i2c_ports_used; i++) {
+ if (i2c_ports[i].port == port)
+ return &i2c_ports[i];
+ }
+
+ return NULL;
+}
+
int i2c_xfer(int port, int slave_addr, const uint8_t *out, int out_size,
uint8_t *in, int in_size, int flags)
{
@@ -254,43 +267,31 @@ exit:
int get_sda_from_i2c_port(int port, enum gpio_signal *sda)
{
- int i;
-
- /* Find the matching port in i2c_ports[] table. */
- for (i = 0; i < i2c_ports_used; i++) {
- if (i2c_ports[i].port == port)
- break;
- }
+ const struct i2c_port_t *i2c_port = get_i2c_port(port);
/* Crash if the port given is not in the i2c_ports[] table. */
- ASSERT(i != i2c_ports_used);
+ ASSERT(i2c_port);
/* Check if the SCL and SDA pins have been defined for this port. */
- if (i2c_ports[i].scl == 0 && i2c_ports[i].sda == 0)
+ if (i2c_port->scl == 0 && i2c_port->sda == 0)
return EC_ERROR_INVAL;
- *sda = i2c_ports[i].sda;
+ *sda = i2c_port->sda;
return EC_SUCCESS;
}
int get_scl_from_i2c_port(int port, enum gpio_signal *scl)
{
- int i;
-
- /* Find the matching port in i2c_ports[] table. */
- for (i = 0; i < i2c_ports_used; i++) {
- if (i2c_ports[i].port == port)
- break;
- }
+ const struct i2c_port_t *i2c_port = get_i2c_port(port);
/* Crash if the port given is not in the i2c_ports[] table. */
- ASSERT(i != i2c_ports_used);
+ ASSERT(i2c_port);
/* Check if the SCL and SDA pins have been defined for this port. */
- if (i2c_ports[i].scl == 0 && i2c_ports[i].sda == 0)
+ if (i2c_port->scl == 0 && i2c_port->sda == 0)
return EC_ERROR_INVAL;
- *scl = i2c_ports[i].scl;
+ *scl = i2c_port->scl;
return EC_SUCCESS;
}
@@ -464,16 +465,6 @@ unwedge_done:
* as ectool supports EC_CMD_I2C_PASSTHRU.
*/
-static int port_is_valid(int port)
-{
- int i;
-
- for (i = 0; i < i2c_ports_used; i++)
- if (i2c_ports[i].port == port)
- return 1;
- return 0;
-}
-
static int i2c_command_read(struct host_cmd_handler_args *args)
{
const struct ec_params_i2c_read *p = args->params;
@@ -485,7 +476,7 @@ static int i2c_command_read(struct host_cmd_handler_args *args)
return EC_RES_ACCESS_DENIED;
#endif
- if (!port_is_valid(p->port))
+ if (!get_i2c_port(p->port))
return EC_RES_INVALID_PARAM;
if (p->read_size == 16)
@@ -512,7 +503,7 @@ static int i2c_command_write(struct host_cmd_handler_args *args)
return EC_RES_ACCESS_DENIED;
#endif
- if (!port_is_valid(p->port))
+ if (!get_i2c_port(p->port))
return EC_RES_INVALID_PARAM;
if (p->write_size == 16)
@@ -561,12 +552,6 @@ static int check_i2c_params(const struct host_cmd_handler_args *args)
return EC_RES_INVALID_PARAM;
}
- if (!port_is_valid(params->port)) {
- PTHRUPRINTF("i2c passthru invalid port %d",
- params->port);
- return EC_RES_INVALID_PARAM;
- }
-
/* Loop and process messages */;
for (msgnum = 0, msg = params->msg; msgnum < params->num_msgs;
msgnum++, msg++) {
@@ -626,6 +611,9 @@ static int i2c_command_passthru(struct host_cmd_handler_args *args)
return EC_RES_ACCESS_DENIED;
#endif
+ if (!get_i2c_port(params->port))
+ return EC_RES_INVALID_PARAM;
+
ret = check_i2c_params(args);
if (ret)
return ret;