From cbc29a2160d93f077098a022de3f8b8971ed85a2 Mon Sep 17 00:00:00 2001
From: Karthikeyan Ramasubramanian <kramasub@google.com>
Date: Wed, 30 Sep 2020 17:04:15 -0600
Subject: drivers/i2c: Add chip driver for GPIO based I2C multiplexer

Add identifiers in ACPI tables for GPIO based I2C multiplexer. The
multiplexer device defines the GPIO resource used to select the
adapter/bus lines. The multiplexer adapter device defines the address
to select the adapter/client lines. These ACPI identifiers are consumed
by the i2c-mux-gpio kernel driver:
https://www.kernel.org/doc/html/latest/i2c/muxes/i2c-mux-gpio.html

BUG=b:169444894
TEST=Build and boot waddledee to OS. Ensure that the ACPI identifiers
are added for I2C devices multiplexed using I2C MUX under the
appropriate scope. Here is the output SSDT:
Scope (\_SB.PCI0.I2C3)
{
    Device (MUX0)
    {
        Name (_HID, "PRP0001")  // _HID: Hardware ID
        Method (_STA, 0, NotSerialized)  // _STA: Status
        {
            Return (0x0F)
        }

        Name (_CRS, ResourceTemplate ()  // _CRS: Current Resource Settings
        {
            GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly,
                "\\_SB.PCI0.GPIO", 0x00, ResourceConsumer, ,
                )
                {   // Pin list
                    0x0125
                }
        })
        Name (_DSD, Package (0x02)  // _DSD: Device-Specific Data
        {
            ToUUID ("daffd814-6eba-4d8c-8a91-bc9bbf4aa301") /* Device Properties for _DSD */,
            Package (0x02)
            {
                Package (0x02)
                {
                    "compatible",
                    "i2c-mux-gpio"
                },

                Package (0x02)
                {
                    "mux-gpios",
                    Package (0x04)
                    {
                        \_SB.PCI0.I2C3.MUX0,
                        Zero,
                        Zero,
                        Zero
                    }
                }
            }
        })
    }
}

Change-Id: Ib371108cc6043c133681066bf7bf4b2e00771e8b
Signed-off-by: Karthikeyan Ramasubramanian <kramasub@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/45911
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Furquan Shaikh <furquan@google.com>
---
 src/drivers/i2c/gpiomux/Kconfig          |  6 +++
 src/drivers/i2c/gpiomux/Makefile.inc     |  1 +
 src/drivers/i2c/gpiomux/mux/Makefile.inc |  1 +
 src/drivers/i2c/gpiomux/mux/chip.h       | 17 +++++++
 src/drivers/i2c/gpiomux/mux/mux.c        | 82 ++++++++++++++++++++++++++++++++
 5 files changed, 107 insertions(+)
 create mode 100644 src/drivers/i2c/gpiomux/Kconfig
 create mode 100644 src/drivers/i2c/gpiomux/Makefile.inc
 create mode 100644 src/drivers/i2c/gpiomux/mux/Makefile.inc
 create mode 100644 src/drivers/i2c/gpiomux/mux/chip.h
 create mode 100644 src/drivers/i2c/gpiomux/mux/mux.c

diff --git a/src/drivers/i2c/gpiomux/Kconfig b/src/drivers/i2c/gpiomux/Kconfig
new file mode 100644
index 0000000000..f09a641eb7
--- /dev/null
+++ b/src/drivers/i2c/gpiomux/Kconfig
@@ -0,0 +1,6 @@
+config DRIVERS_I2C_GPIO_MUX
+	bool
+	default n
+	depends on HAVE_ACPI_TABLES
+	help
+	  When enabled, add identifiers in ACPI tables for GPIO based I2C multiplexer.
diff --git a/src/drivers/i2c/gpiomux/Makefile.inc b/src/drivers/i2c/gpiomux/Makefile.inc
new file mode 100644
index 0000000000..fec8d5b00f
--- /dev/null
+++ b/src/drivers/i2c/gpiomux/Makefile.inc
@@ -0,0 +1 @@
+subdirs-$(CONFIG_DRIVERS_I2C_GPIO_MUX) += mux
diff --git a/src/drivers/i2c/gpiomux/mux/Makefile.inc b/src/drivers/i2c/gpiomux/mux/Makefile.inc
new file mode 100644
index 0000000000..e51f706eda
--- /dev/null
+++ b/src/drivers/i2c/gpiomux/mux/Makefile.inc
@@ -0,0 +1 @@
+ramstage-$(CONFIG_DRIVERS_I2C_GPIO_MUX) += mux.c
diff --git a/src/drivers/i2c/gpiomux/mux/chip.h b/src/drivers/i2c/gpiomux/mux/chip.h
new file mode 100644
index 0000000000..9906b258db
--- /dev/null
+++ b/src/drivers/i2c/gpiomux/mux/chip.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __I2C_GPIOMUX_MUX_CHIP_H__
+#define __I2C_GPIOMUX_MUX_CHIP_H__
+
+#include <acpi/acpi_device.h>
+#include <types.h>
+
+#define MAX_NUM_MUX_GPIOS 4
+
+struct drivers_i2c_gpiomux_mux_config {
+	/* GPIOs used to select the mux lines */
+	uint32_t mux_gpio_count;
+	struct acpi_gpio mux_gpio[MAX_NUM_MUX_GPIOS];
+};
+
+#endif /* __I2C_GPIOMUX_MUX_CHIP_H__ */
diff --git a/src/drivers/i2c/gpiomux/mux/mux.c b/src/drivers/i2c/gpiomux/mux/mux.c
new file mode 100644
index 0000000000..66c8cc5cc3
--- /dev/null
+++ b/src/drivers/i2c/gpiomux/mux/mux.c
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <acpi/acpi_device.h>
+#include <acpi/acpigen.h>
+#include <console/console.h>
+#include <device/device.h>
+#include <device/path.h>
+#include <stdlib.h>
+#include <string.h>
+#include "chip.h"
+
+static const char *i2c_gpiomux_mux_acpi_name(const struct device *dev)
+{
+	static char name[ACPI_NAME_BUFFER_SIZE];
+
+	snprintf(name, ACPI_NAME_BUFFER_SIZE, "MUX%01.1X", dev->path.generic.id);
+	return name;
+}
+
+static void i2c_gpiomux_mux_fill_ssdt(const struct device *dev)
+{
+	const char *scope = acpi_device_scope(dev);
+	const char *path = acpi_device_path(dev);
+	struct drivers_i2c_gpiomux_mux_config *config = config_of(dev);
+	struct acpi_dp *dsd = NULL;
+	const char *compat_string = "i2c-mux-gpio";
+	struct acpi_gpio_res_params param[MAX_NUM_MUX_GPIOS];
+	int i;
+
+	if (!dev->enabled || !scope || !path)
+		return;
+
+	/* Device */
+	acpigen_write_scope(scope);
+	acpigen_write_device(acpi_device_name(dev));
+	acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
+	acpigen_write_STA(acpi_device_status(dev));
+
+	/* Resources */
+	acpigen_write_name("_CRS");
+	acpigen_write_resourcetemplate_header();
+	for (i = 0; i < config->mux_gpio_count; i++) {
+		acpi_device_write_gpio(&config->mux_gpio[i]);
+		param[i].ref = path;
+		param[i].index = i;
+		param[i].pin = 0;
+		param[i].active_low = config->mux_gpio[i].active_low;
+	}
+	acpigen_write_resourcetemplate_footer();
+
+	/* DSD */
+	dsd = acpi_dp_new_table("_DSD");
+	acpi_dp_add_string(dsd, "compatible", compat_string);
+	acpi_dp_add_gpio_array(dsd, "mux-gpios", param, config->mux_gpio_count);
+	acpi_dp_write(dsd);
+
+	acpigen_pop_len(); /* Device */
+	acpigen_pop_len(); /* Scope */
+
+	printk(BIOS_INFO, "%s: %s at %s\n", path, dev->chip_ops->name, dev_path(dev));
+}
+
+static struct device_operations i2c_gpiomux_mux_ops = {
+	.read_resources		= noop_read_resources,
+	.set_resources		= noop_set_resources,
+	.scan_bus		= scan_static_bus,
+	.acpi_name		= i2c_gpiomux_mux_acpi_name,
+	.acpi_fill_ssdt		= i2c_gpiomux_mux_fill_ssdt,
+};
+
+static void i2c_gpiomux_mux_enable(struct device *dev)
+{
+	if (!dev)
+		return;
+
+	dev->ops = &i2c_gpiomux_mux_ops;
+}
+
+struct chip_operations drivers_i2c_gpiomux_mux_ops = {
+	CHIP_NAME("I2C GPIO MUX Device")
+	.enable_dev = i2c_gpiomux_mux_enable
+};
-- 
cgit v1.2.3