summaryrefslogtreecommitdiff
path: root/src/device/i2c_bus.c
diff options
context:
space:
mode:
authorNico Huber <nico.huber@secunet.com>2017-08-01 17:09:35 +0200
committerMartin Roth <martinroth@google.com>2017-08-18 15:33:45 +0000
commit581738642fbeacdf97fc737a41b3128d72cf1a1c (patch)
tree8f40edb039a66fb6ccdc29b88f7e3b007b048e82 /src/device/i2c_bus.c
parent0f2dd1eff9930e30dddd9aabceb5d85ee3b4e980 (diff)
downloadcoreboot-581738642fbeacdf97fc737a41b3128d72cf1a1c.tar.xz
Reinvent I2C ops
Do not use the global platform_i2c_transfer() function that can only be implemented by a single driver. Instead, make a `struct device` aware transfer() function the only interface function for I2C controller dri- vers to implement. To not force the slave device drivers to be implemented either above generic I2C or specialized SMBus operations, we support SMBus control- lers in the slave device interface too. We start with four simple slave functions: i2c_readb(), i2c_writeb(), i2c_readb_at() and i2c_writeb_at(). They are all compatible to respec- tive SMBus functions. But we keep aliases because it would be weird to force e.g. an I2C EEPROM driver to call smbus_read_byte(). Change-Id: I98386f91bf4799ba3df84ec8bc0f64edd4142818 Signed-off-by: Nico Huber <nico.huber@secunet.com> Reviewed-on: https://review.coreboot.org/20846 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Aaron Durbin <adurbin@chromium.org> Reviewed-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
Diffstat (limited to 'src/device/i2c_bus.c')
-rw-r--r--src/device/i2c_bus.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/device/i2c_bus.c b/src/device/i2c_bus.c
new file mode 100644
index 0000000000..1c543efaba
--- /dev/null
+++ b/src/device/i2c_bus.c
@@ -0,0 +1,161 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <console/console.h>
+#include <device/smbus.h>
+#include <device/i2c_bus.h>
+
+struct bus *i2c_link(struct device *const dev)
+{
+ if (!dev || !dev->bus)
+ return NULL;
+
+ struct bus *link = dev->bus;
+ while (link) {
+ struct device *const parent = link->dev;
+
+ if (parent && parent->ops &&
+ (parent->ops->ops_i2c_bus || parent->ops->ops_smbus_bus))
+ break;
+
+ if (parent && parent->bus)
+ link = parent->bus;
+ else
+ link = NULL;
+ }
+
+ if (!link) {
+ printk(BIOS_ALERT, "%s Cannot find I2C or SMBus bus operations",
+ dev_path(dev));
+ }
+
+ return link;
+}
+
+int i2c_readb(struct device *const dev)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ uint8_t val;
+ const struct i2c_msg msg = {
+ .flags = I2C_M_RD,
+ .slave = dev->path.i2c.device,
+ .buf = &val,
+ .len = sizeof(val),
+ };
+
+ const int ret = busdev->ops->ops_i2c_bus->
+ transfer(busdev, &msg, 1);
+ if (ret)
+ return ret;
+ else
+ return val;
+ } else if (busdev->ops->ops_smbus_bus->recv_byte) {
+ return busdev->ops->ops_smbus_bus->recv_byte(dev);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->recv_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}
+
+int i2c_writeb(struct device *const dev, uint8_t val)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ const struct i2c_msg msg = {
+ .flags = 0,
+ .slave = dev->path.i2c.device,
+ .buf = &val,
+ .len = sizeof(val),
+ };
+ return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1);
+ } else if (busdev->ops->ops_smbus_bus->send_byte) {
+ return busdev->ops->ops_smbus_bus->send_byte(dev, val);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->send_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}
+
+int i2c_readb_at(struct device *const dev, uint8_t off)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ uint8_t val;
+ const struct i2c_msg msg[] = {
+ {
+ .flags = 0,
+ .slave = dev->path.i2c.device,
+ .buf = &off,
+ .len = sizeof(off),
+ },
+ {
+ .flags = I2C_M_RD,
+ .slave = dev->path.i2c.device,
+ .buf = &val,
+ .len = sizeof(val),
+ },
+ };
+
+ const int ret = busdev->ops->ops_i2c_bus->
+ transfer(busdev, msg, ARRAY_SIZE(msg));
+ if (ret)
+ return ret;
+ else
+ return val;
+ } else if (busdev->ops->ops_smbus_bus->read_byte) {
+ return busdev->ops->ops_smbus_bus->read_byte(dev, off);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->read_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}
+
+int i2c_writeb_at(struct device *const dev,
+ const uint8_t off, const uint8_t val)
+{
+ struct device *const busdev = i2c_busdev(dev);
+ if (!busdev)
+ return -1;
+
+ if (busdev->ops->ops_i2c_bus) {
+ uint8_t buf[] = { off, val };
+ const struct i2c_msg msg = {
+ .flags = 0,
+ .slave = dev->path.i2c.device,
+ .buf = buf,
+ .len = sizeof(buf),
+ };
+ return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1);
+ } else if (busdev->ops->ops_smbus_bus->write_byte) {
+ return busdev->ops->ops_smbus_bus->write_byte(dev, off, val);
+ } else {
+ printk(BIOS_ERR, "%s Missing ops_smbus_bus->write_byte",
+ dev_path(busdev));
+ return -1;
+ }
+}