From patchwork Thu Aug 9 02:20:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 10560801 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 76AC414C0 for ; Thu, 9 Aug 2018 02:21:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B90E2AB2B for ; Thu, 9 Aug 2018 02:21:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4EBCD2AD6C; Thu, 9 Aug 2018 02:21:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EC6B92AB2B for ; Thu, 9 Aug 2018 02:21:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=M0nbOHRpC4pFnD6MybJR+Om6SbwawpzqZIoFxzb7JZU=; b=Es++0PhoFdpoF5tJEBWnzyd4cK XFlOd8RIYVyzSvH+kntQo+883KPiMvDKIHB1Onrskw3+OQV98oZ3i8QJFECAaOLgeNJ4eluVJ2sQ8 CMBxJ+qT7cxteXiP7ae7WATkNQGFaUceKdwew/kZJJ5kuqg41Fgp5iv5albL356qn9uwEhgnhUvz4 41LzOhoPkgGub0eRVdeij4xLlmVSvMLEZoLac7DOgLS+/2SwhfWT0DZH8TVk7uPwV0fqZLPtNf0Wh bgrRe1in38yeZW9BeeLkH4++yVoWL3D6sxD5z3FcFpt0tFDi2EFlZIHfwz9PspMJu9MtZbkzLC0s0 GMEy+KRA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fnaaI-00012v-QG; Thu, 09 Aug 2018 02:21:54 +0000 Received: from mail.nic.cz ([217.31.204.67]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fnaZv-0000a2-30 for linux-arm-kernel@lists.infradead.org; Thu, 09 Aug 2018 02:21:35 +0000 Received: from dellmb.labs.office.nic.cz (unknown [IPv6:2001:1488:fffe:6:cac7:3539:7f1f:463]) by mail.nic.cz (Postfix) with ESMTP id 03FF0617B9; Thu, 9 Aug 2018 04:21:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nic.cz; s=default; t=1533781279; bh=GMFvBFjbo8qbbA7E2JhYsEB1eKAGf5a9XbBawKxC22k=; h=From:To:Date; b=VQWJjP7CvnHbtP/sxXlJXHeSse3UaU+M4TMAWETTo91iVjnPmHzEDN6jkzy94WRZZ QB8Dey6i9XCHbjkYWDzisItprpGpTnqmsxApnT0MjEdCL6/uKnGNaTlJInpqx5MWM1 gkqn+JjFp0ulySkwuyEoYMBveR34gZQztnr/qffE= From: =?utf-8?q?Marek_Beh=C3=BAn?= To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 2/7] drivers: mfd: Add support for Moxtet bus Date: Thu, 9 Aug 2018 04:20:32 +0200 Message-Id: <20180809022037.12001-3-marek.behun@nic.cz> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180809022037.12001-1-marek.behun@nic.cz> References: <20180809022037.12001-1-marek.behun@nic.cz> X-Virus-Scanned: clamav-milter 0.99.2 at mail X-Virus-Status: Clean X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180808_192131_497294_5C96A1CC X-CRM114-Status: GOOD ( 24.11 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Marek_Beh=C3=BAn?= MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On the Turris Mox router there can be connected different modules to the main CPU board, currently a module with a SFP cage, a module with MiniPCIe connector, a 4-port switch module and a 8-port switch module, for example: [CPU]-[PCIe]-[8-port switch]-[8-port switch]-[SFP] Each of this modules has an input and output shift register, and these are connected via SPI to CPU board. Via this SPI connection we are able to discover which modules are connected and we can also read/write some configuration to the modules. Fromi/to each module 8 bits can be read (of which lower 4 bits identify the module) and written. For example from the module with a SFP cage we can read the LOS, TX-FAULT and MOD-DEF0 signals, while we can write TX-DISABLE and RATE-SELECT signals. Other modules may support something else. This driver creates a new bus type, called "moxtet". For each Mox module it finds via SPI, it creates a new device on the moxtet bus so that drivers can be written for them, for example a gpio driver for the module with a SFP cage. The topology of how Mox modules are connected can then be read by listing /sys/bus/moxtet/devices. Signed-off-by: Marek Behun --- Documentation/devicetree/bindings/mfd/moxtet.txt | 36 ++ MAINTAINERS | 7 + drivers/mfd/Kconfig | 10 + drivers/mfd/Makefile | 1 + drivers/mfd/moxtet.c | 501 +++++++++++++++++++++++ include/linux/mfd/moxtet.h | 103 +++++ 6 files changed, 658 insertions(+) create mode 100644 Documentation/devicetree/bindings/mfd/moxtet.txt create mode 100644 drivers/mfd/moxtet.c create mode 100644 include/linux/mfd/moxtet.h diff --git a/Documentation/devicetree/bindings/mfd/moxtet.txt b/Documentation/devicetree/bindings/mfd/moxtet.txt new file mode 100644 index 000000000000..02b96fbd5ddd --- /dev/null +++ b/Documentation/devicetree/bindings/mfd/moxtet.txt @@ -0,0 +1,36 @@ +Turris Mox module configuration bus (over SPI) + +Required properties: + - compatible : Should be "cznic,moxtet". + - #address-cells : Has to be 1 + - #size-cells : Has to be 0 +For other required and optional properties of SPI slave +nodes please refer to ../spi/spi-bus.txt. + +Required properties of subnodes: + - reg : Should be position on the Moxtet bus + - moxtet,id : Should be ID of the Moxtet device connected + +The driver finds the devices connected to the bus by itself, but it may be +needed to reference some of them from other parts of the device tree. In that +case the devices can be defined as subnodes of the moxtet node. + +Example: + + moxtet@1 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "cznic,moxtet"; + reg = <1>; + spi-max-frequency = <1000000>; + spi-cpol; + spi-cpha; + + moxtet_sfp: moxtet-sfp@0 { + compatible = "cznic,moxtet-sfp"; + gpio-controller; + #gpio-cells; + reg = <0>; + moxtet,id = <1>; + } + }; diff --git a/MAINTAINERS b/MAINTAINERS index 7cebd5bba8a8..27ca12e8309a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1378,6 +1378,13 @@ F: drivers/clocksource/timer-prima2.c F: drivers/clocksource/timer-atlas7.c N: [^a-z]sirf +ARM/CZ.NIC TURRIS MOX SUPPORT +M: Marek Behun +W: http://mox.turris.cz +S: Maintained +F: include/mfd/moxtet.h +F: drivers/mfd/moxtet.c + ARM/EBSA110 MACHINE SUPPORT M: Russell King L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers) diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig index b860eb5aa194..395d8b15f8a8 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig @@ -784,6 +784,16 @@ config MFD_MAX8998 additional drivers must be enabled in order to use the functionality of the device. +config MFD_MOXTET + tristate "CZ.NIC Turris Mox module configuration bus" + depends on SPI_MASTER && OF + help + Say yes here to add support for the module configuration bus found + on CZ.NIC's Turris Mox. This is needed for the ability to read + in what order the modules are connected and to get/set some of + their settings. For example the GPIOs on Mox SFP module are + configured through this bus. + config MFD_MT6397 tristate "MediaTek MT6397 PMIC Support" select MFD_CORE diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile index e9fd20dba18d..1401863e27b1 100644 --- a/drivers/mfd/Makefile +++ b/drivers/mfd/Makefile @@ -147,6 +147,7 @@ max8925-objs := max8925-core.o max8925-i2c.o obj-$(CONFIG_MFD_MAX8925) += max8925.o obj-$(CONFIG_MFD_MAX8997) += max8997.o max8997-irq.o obj-$(CONFIG_MFD_MAX8998) += max8998.o max8998-irq.o +obj-$(CONFIG_MFD_MOXTET) += moxtet.o pcf50633-objs := pcf50633-core.o pcf50633-irq.o obj-$(CONFIG_MFD_PCF50633) += pcf50633.o diff --git a/drivers/mfd/moxtet.c b/drivers/mfd/moxtet.c new file mode 100644 index 000000000000..1cd1a62002e7 --- /dev/null +++ b/drivers/mfd/moxtet.c @@ -0,0 +1,501 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Turris Mox module configuration bus driver + * + * Copyright (C) 2018 Marek Behun + */ + +#include +#include +#include +#include +#include + +static ssize_t +module_id_show(struct device *dev, struct device_attribute *a, char *buf) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + + return sprintf(buf, "0x%x\n", mdev->id); +} +static DEVICE_ATTR_RO(module_id); + +static ssize_t +module_name_show(struct device *dev, struct device_attribute *a, char *buf) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + + return sprintf(buf, "%s\n", turris_mox_module_name(mdev->id)); +} +static DEVICE_ATTR_RO(module_name); + +static ssize_t +input_value_show(struct device *dev, struct device_attribute *a, char *buf) +{ + int ret; + + ret = moxtet_device_read(dev); + if (ret < 0) + return ret; + + return sprintf(buf, "0x%x\n", ret); +} +static DEVICE_ATTR_RO(input_value); + +static ssize_t +output_value_show(struct device *dev, struct device_attribute *a, char *buf) +{ + int ret; + + ret = moxtet_device_written(dev); + if (ret < 0) + return ret; + + return sprintf(buf, "0x%x\n", ret); +} + +static ssize_t +output_value_store(struct device *dev, struct device_attribute *a, + const char *buf, size_t count) +{ + unsigned long val; + int ret; + + ret = kstrtoul(buf, 0, &val); + if (ret < 0) + return ret; + + if (val > 0xff) + return -ERANGE; + + ret = moxtet_device_write(dev, val); + if (ret < 0) + return ret; + + return count; +} +static DEVICE_ATTR_RW(output_value); + +static struct attribute *moxtet_dev_attrs[] = { + &dev_attr_module_id.attr, + &dev_attr_module_name.attr, + &dev_attr_input_value.attr, + &dev_attr_output_value.attr, + NULL, +}; + +static const struct attribute_group moxtet_dev_group = { + .attrs = moxtet_dev_attrs, +}; + +static const struct attribute_group *moxtet_dev_groups[] = { + &moxtet_dev_group, + NULL, +}; + +static int moxtet_match(struct device *dev, struct device_driver *drv) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + struct moxtet_driver *tdrv = to_moxtet_driver(drv); + const enum turris_mox_module_id *t; + + if (of_driver_match_device(dev, drv)) + return 1; + + if (!tdrv->id_table) + return 0; + + for (t = tdrv->id_table; *t; ++t) + if (*t == mdev->id) + return 1; + + return 0; +} + +struct bus_type moxtet_bus_type = { + .name = "moxtet", + .dev_groups = moxtet_dev_groups, + .match = moxtet_match, +}; +EXPORT_SYMBOL_GPL(moxtet_bus_type); + +int __moxtet_register_driver(struct module *owner, + struct moxtet_driver *mdrv) +{ + mdrv->driver.owner = owner; + mdrv->driver.bus = &moxtet_bus_type; + return driver_register(&mdrv->driver); +} +EXPORT_SYMBOL_GPL(__moxtet_register_driver); + +static int moxtet_dev_check(struct device *dev, void *data) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + struct moxtet_device *new_dev = data; + + if (mdev->moxtet == new_dev->moxtet && mdev->id == new_dev->id && + mdev->idx == new_dev->idx) + return -EBUSY; + return 0; +} + +static void moxtet_dev_release(struct device *dev) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + + put_device(mdev->moxtet->dev); + kfree(mdev); +} + +static struct moxtet_device * +moxtet_alloc_device(struct moxtet *moxtet) +{ + struct moxtet_device *dev; + + if (!get_device(moxtet->dev)) + return NULL; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) { + put_device(moxtet->dev); + return NULL; + } + + dev->moxtet = moxtet; + dev->dev.parent = moxtet->dev; + dev->dev.bus = &moxtet_bus_type; + dev->dev.release = moxtet_dev_release; + + device_initialize(&dev->dev); + + return dev; +} + +static int moxtet_add_device(struct moxtet_device *dev) +{ + static DEFINE_MUTEX(add_mutex); + int ret; + + if (dev->idx >= TURRIS_MOX_MAX_MODULES || dev->id > 0xf) + return -EINVAL; + + dev_set_name(&dev->dev, "moxtet-%s.%u", + turris_mox_module_name(dev->id), dev->idx); + + mutex_lock(&add_mutex); + + ret = bus_for_each_dev(&moxtet_bus_type, NULL, dev, + moxtet_dev_check); + if (ret) + goto done; + + ret = device_add(&dev->dev); + if (ret < 0) + dev_err(dev->moxtet->dev, "can't add %s, status %d\n", + dev_name(dev->moxtet->dev), ret); + +done: + mutex_unlock(&add_mutex); + return ret; +} + +static int __unregister(struct device *dev, void *null) +{ + if (dev->of_node) { + of_node_clear_flag(dev->of_node, OF_POPULATED); + of_node_put(dev->of_node); + } + + device_unregister(dev); + + return 0; +} + +static struct moxtet_device * +of_register_moxtet_device(struct moxtet *moxtet, struct device_node *nc) +{ + struct moxtet_device *dev; + u32 val; + int ret; + + dev = moxtet_alloc_device(moxtet); + if (!dev) { + dev_err(moxtet->dev, + "Moxtet device alloc error for %pOF\n", nc); + return ERR_PTR(-ENOMEM); + } + + ret = of_property_read_u32(nc, "reg", &val); + if (ret || val >= TURRIS_MOX_MAX_MODULES) { + dev_err(moxtet->dev, "%pOF has no valid 'reg' property (%d)\n", + nc, ret); + goto err_put; + } + dev->idx = val; + + ret = of_property_read_u32(nc, "moxtet,id", &val); + if (ret || val > 0xf) { + dev_err(moxtet->dev, + "%pOF has no valid 'moxtet,id' property (%d)\n", nc, + ret); + goto err_put; + } + dev->id = val; + + if (moxtet->modules[dev->idx] != dev->id) { + dev_err(moxtet->dev, + "%pOF requested Moxtet device ID 0x%x, 0x%x found\n", + nc, dev->id, moxtet->modules[dev->idx]); + goto err_put; + } + + of_node_get(nc); + dev->dev.of_node = nc; + + ret = moxtet_add_device(dev); + if (ret) { + dev_err(moxtet->dev, + "Moxtet device register error for %pOF\n", nc); + of_node_put(nc); + goto err_put; + } + + return dev; + +err_put: + put_device(&dev->dev); + return ERR_PTR(ret); +} + +static void of_register_moxtet_devices(struct moxtet *moxtet) +{ + struct moxtet_device *dev; + struct device_node *nc; + + if (!moxtet->dev->of_node) + return; + + for_each_available_child_of_node(moxtet->dev->of_node, nc) { + if (of_node_test_and_set_flag(nc, OF_POPULATED)) + continue; + dev = of_register_moxtet_device(moxtet, nc); + if (IS_ERR(dev)) { + dev_warn(moxtet->dev, + "Failed to create Moxtet device for %pOF\n", + nc); + of_node_clear_flag(nc, OF_POPULATED); + } + } +} + +static void +moxtet_register_devices_from_topology(struct moxtet *moxtet) +{ + struct moxtet_device *dev; + int i, ret; + + for (i = 0; i < moxtet->count; ++i) { + dev = moxtet_alloc_device(moxtet); + if (!dev) { + dev_err(moxtet->dev, "Moxtet device %u alloc error\n", + i); + continue; + } + + dev->idx = i; + dev->id = moxtet->modules[i]; + + ret = moxtet_add_device(dev); + if (ret && ret != -EBUSY) { + put_device(&dev->dev); + dev_err(moxtet->dev, + "Moxtet device %u register error: %i\n", i, + ret); + } + } +} + +static int moxtet_find_topology(struct moxtet *moxtet) +{ + u8 buf[TURRIS_MOX_MAX_MODULES]; + int i, ret; + + ret = spi_read(to_spi_device(moxtet->dev), buf, TURRIS_MOX_MAX_MODULES); + if (ret < 0) + return ret; + + if (buf[0] == TURRIS_MOX_CPU_ID_EMMC) { + dev_info(moxtet->dev, "Found eMMC Turris Mox CPU module\n"); + } else if (buf[0] == TURRIS_MOX_CPU_ID_SD) { + dev_info(moxtet->dev, "Found SD Turris Mox CPU module\n"); + } else { + dev_err(moxtet->dev, "Invalid Turris Mox CPU module 0x%02x\n", + buf[0]); + return -ENODEV; + } + + moxtet->count = 0; + + for (i = 1; i < TURRIS_MOX_MAX_MODULES; ++i) { + int module_id; + + if (buf[i] == 0xff) + break; + + module_id = buf[i] & 0xf; + + moxtet->modules[i-1] = module_id; + ++moxtet->count; + + switch (module_id) { + case TURRIS_MOX_MODULE_SFP: + dev_info(moxtet->dev, "SFP module found\n"); + break; + case TURRIS_MOX_MODULE_PCI: + dev_info(moxtet->dev, "PCIe module found\n"); + break; + case TURRIS_MOX_MODULE_TOPAZ: + dev_info(moxtet->dev, "Topaz Switch module found\n"); + break; + case TURRIS_MOX_MODULE_PERIDOT: + dev_info(moxtet->dev, "Peridot Switch module found\n"); + break; + default: + dev_info(moxtet->dev, + "Unknown Moxtet module found (ID 0x%02x)\n", + module_id); + } + } + + return 0; +} + +int moxtet_device_read(struct device *dev) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + struct moxtet *moxtet = mdev->moxtet; + u8 buf[TURRIS_MOX_MAX_MODULES]; + struct spi_transfer xfer = { + .rx_buf = buf, + .tx_buf = moxtet->tx, + .len = moxtet->count + 1 + }; + int ret; + + if (mdev->idx >= moxtet->count) + return -EINVAL; + + mutex_lock(&moxtet->lock); + + ret = spi_sync_transfer(to_spi_device(moxtet->dev), &xfer, 1); + + mutex_unlock(&moxtet->lock); + + if (ret < 0) + return ret; + + return buf[mdev->idx + 1] >> 4; +} +EXPORT_SYMBOL_GPL(moxtet_device_read); + +int moxtet_device_write(struct device *dev, u8 val) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + struct moxtet *moxtet = mdev->moxtet; + int ret; + + if (mdev->idx >= moxtet->count) + return -EINVAL; + + mutex_lock(&moxtet->lock); + + moxtet->tx[moxtet->count - mdev->idx] = val; + + ret = spi_write(to_spi_device(moxtet->dev), moxtet->tx, + moxtet->count + 1); + + mutex_unlock(&moxtet->lock); + + return ret; +} +EXPORT_SYMBOL_GPL(moxtet_device_write); + +int moxtet_device_written(struct device *dev) +{ + struct moxtet_device *mdev = to_moxtet_device(dev); + struct moxtet *moxtet = mdev->moxtet; + + if (mdev->idx >= moxtet->count) + return -EINVAL; + + return moxtet->tx[moxtet->count - mdev->idx]; +} +EXPORT_SYMBOL_GPL(moxtet_device_written); + +static int moxtet_probe(struct spi_device *spi) +{ + struct moxtet *moxtet; + int ret; + + ret = spi_setup(spi); + if (ret < 0) + return ret; + + moxtet = devm_kzalloc(&spi->dev, sizeof(struct moxtet), + GFP_KERNEL); + if (!moxtet) + return -ENOMEM; + + moxtet->dev = &spi->dev; + spi_set_drvdata(spi, moxtet); + + mutex_init(&moxtet->lock); + + ret = moxtet_find_topology(moxtet); + if (ret < 0) + return ret; + + of_register_moxtet_devices(moxtet); + moxtet_register_devices_from_topology(moxtet); + + return 0; +} + +static int moxtet_remove(struct spi_device *spi) +{ + struct moxtet *moxtet = spi_get_drvdata(spi); + int dummy; + + dummy = device_for_each_child(moxtet->dev, NULL, __unregister); + + mutex_destroy(&moxtet->lock); + + return 0; +} + +static const struct of_device_id moxtet_dt_ids[] = { + { .compatible = "cznic,moxtet" }, + {}, +}; +MODULE_DEVICE_TABLE(of, moxtet_dt_ids); + +static struct spi_driver moxtet_driver = { + .driver = { + .name = "moxtet", + .of_match_table = moxtet_dt_ids, + }, + .probe = moxtet_probe, + .remove = moxtet_remove, +}; +module_spi_driver(moxtet_driver); + +static int __init moxtet_init(void) +{ + return bus_register(&moxtet_bus_type); +} + +postcore_initcall(moxtet_init); + +MODULE_AUTHOR("Marek Behun "); +MODULE_DESCRIPTION("CZ.NIC's Turris Mox module configuration bus"); +MODULE_LICENSE("GPL v2"); diff --git a/include/linux/mfd/moxtet.h b/include/linux/mfd/moxtet.h new file mode 100644 index 000000000000..f818039c2c2e --- /dev/null +++ b/include/linux/mfd/moxtet.h @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Turris Mox module configuration bus driver + * + * Copyright (C) 2018 Marek Behun + */ + +#ifndef __LINUX_MFD_MOXTET_H +#define __LINUX_MFD_MOXTET_H + +#include +#include + +#define TURRIS_MOX_MAX_MODULES 10 + +enum turris_mox_cpu_module_id { + TURRIS_MOX_CPU_ID_EMMC = 0x00, + TURRIS_MOX_CPU_ID_SD = 0x10, +}; + +enum turris_mox_module_id { + TURRIS_MOX_MODULE_SFP = 0x01, + TURRIS_MOX_MODULE_PCI = 0x02, + TURRIS_MOX_MODULE_TOPAZ = 0x03, + TURRIS_MOX_MODULE_PERIDOT = 0x04, +}; + +static inline const char *turris_mox_module_name(unsigned int id) +{ + switch (id) { + case TURRIS_MOX_MODULE_SFP: + return "sfp"; + case TURRIS_MOX_MODULE_PCI: + return "pci"; + case TURRIS_MOX_MODULE_TOPAZ: + return "topaz"; + case TURRIS_MOX_MODULE_PERIDOT: + return "peridot"; + default: + return "unknown"; + } +} + +extern struct bus_type moxtet_type; + +struct moxtet { + struct device *dev; + struct mutex lock; + u8 modules[TURRIS_MOX_MAX_MODULES]; + int count; + u8 tx[TURRIS_MOX_MAX_MODULES]; + char module_topology[128]; +}; + +struct moxtet_driver { + const enum turris_mox_module_id *id_table; + struct device_driver driver; +}; + +static inline struct moxtet_driver * +to_moxtet_driver(struct device_driver *drv) +{ + if (!drv) + return NULL; + return container_of(drv, struct moxtet_driver, driver); +} + +extern int __moxtet_register_driver(struct module *owner, + struct moxtet_driver *mdrv); + +static inline void moxtet_unregister_driver(struct moxtet_driver *mdrv) +{ + if (mdrv) + driver_unregister(&mdrv->driver); +} + +#define moxtet_register_driver(driver) \ + __moxtet_register_driver(THIS_MODULE, driver) + +#define module_moxtet_driver(__moxtet_driver) \ + module_driver(__moxtet_driver, moxtet_register_driver, \ + moxtet_unregister_driver) + +struct moxtet_device { + struct device dev; + struct moxtet *moxtet; + enum turris_mox_module_id id; + unsigned int idx; +}; + +extern int moxtet_device_read(struct device *dev); +extern int moxtet_device_write(struct device *dev, u8 val); +extern int moxtet_device_written(struct device *dev); + +static inline struct moxtet_device * +to_moxtet_device(struct device *dev) +{ + if (!dev) + return NULL; + return container_of(dev, struct moxtet_device, dev); +} + +#endif /* __LINUX_MFD_MOXTET_H */