Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp945380rdf; Sat, 4 Nov 2023 00:02:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEx9BGcVzbj9UkLTiZ07/ibbwGmecARsj3GOTizvOZ9dGfgoaBcq16B1aIN2qx8vKePFriD X-Received: by 2002:a17:903:1c8:b0:1cc:58f6:5347 with SMTP id e8-20020a17090301c800b001cc58f65347mr5474349plh.34.1699081363555; Sat, 04 Nov 2023 00:02:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699081363; cv=none; d=google.com; s=arc-20160816; b=RvD2GOU9BhCeJHT6eqdedBGM+FtRL2g/FhDeyDXaOOmMx0SGcUwyCWbdlUcE+L+JrX Xj4fp8UrqJTghZKBpyRyRIXbMTGMjO6Al29G9Yy2hASE3Qm0ZQLj80KH+vb20sUb/yTS DYwD3brVhZsuGWpc0Hm9v4VdYrouNc8qEBtUEfBWNtVQ5y9N5cH1/onryy24fzrswAbp 2EH0JMquBGUY3yUjXc7L5W6h/WAOPh+peqtA60lfTTw11L2wzbzrj0xyP5RhYfcewBGm c4dq8Q/8hwrjjqC1nMAQSdswVvPIu4WpZs48AHk/AM06nBMqDzQBCF/MyAIPdN2SFXyU UxMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from; bh=t/+XTXkAd7XRWC+3TX5BTDFRqYIgCGwH/MYgjs3Ceoc=; fh=d3qej8N8oEwoDi4946F/IhSPaeU492mqImsNcFeFjcI=; b=tiw2+3kgOPJRWDdmuyx9usjaJsqyYuaIZRPJekggacFgQAqnD0ue6XseLb0S+Pqgd0 lMhEFE1NsFpW9yZSQQ8XALYxrBaQoZ3jc7GoEa23O7hdHV+WauyomLfdpdqr8U0nxF4y QxE+hST+GV16A5binWEn+hxGBdfcgwuZWpa4W0/IIu9duDNOVPtMFb7lr1SzPuxeH2f2 2dScIOx6e1yJA8KBo2CortWA/FEXl4Ok2jb2tLET01479NivmyOyePInjifg1xY3WyrY sP4oyJXDjwo6hhnpN4Zm5uJg1V6soTUj2nKahc69/TGCNeSjS0m0wVddf78diG+ejCKu obLg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id i5-20020a170902cf0500b001ca33ce21fesi3560489plg.58.2023.11.04.00.02.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Nov 2023 00:02:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id D675A807C843; Sat, 4 Nov 2023 00:01:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233764AbjKDGrm (ORCPT + 99 others); Sat, 4 Nov 2023 02:47:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231264AbjKDGr2 (ORCPT ); Sat, 4 Nov 2023 02:47:28 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ECD6D51; Fri, 3 Nov 2023 23:47:24 -0700 (PDT) Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4SMp7j5y6Hz4f3jMf; Sat, 4 Nov 2023 14:47:13 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 234261A016E; Sat, 4 Nov 2023 14:47:20 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.107]) by APP4 (Coremail) with SMTP id gCh0CgDnfd3z6EVlglQtEw--.50758S5; Sat, 04 Nov 2023 14:47:19 +0800 (CST) From: Zhang Xiaoxu To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com, broonie@kernel.org, rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com, linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 -next 1/5] spi: mockup: Add SPI controller testing driver Date: Sat, 4 Nov 2023 14:46:46 +0800 Message-Id: <20231104064650.972687-2-zhangxiaoxu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> References: <20231104064650.972687-1-zhangxiaoxu@huaweicloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: gCh0CgDnfd3z6EVlglQtEw--.50758S5 X-Coremail-Antispam: 1UD129KBjvJXoW3Gw15Cr4xAFyxur43KFyrtFb_yoW3CrWUpF yrJay7JrW8GF13uF1S9rW8uFy5Ww1kCayxt3yfJa4Ik3yfXFyDXa4ktryYvay5JFyDXFyU ZFWa9a48CFWUZF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvGb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr 0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY 17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcV C0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY 6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa 73UjIFyTuYvjxUzUUUUUUUU X-CM-SenderInfo: x2kd0wp0ld053x6k3tpzhluzxrxghudrp/ X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sat, 04 Nov 2023 00:01:58 -0700 (PDT) From: Zhang Xiaoxu This enables SPI controller Testing driver, which provides a way to test SPI subsystem. This is accomplished by executing the following command: $ echo adcxx1s 0 > /sys/class/spi_master/spi0/new_device The name of the target driver and its chip select were used to instantiate the device. $ ls /sys/bus/spi/devices/spi0.0/hwmon/hwmon0/ -l total 0 lrwxrwxrwx 1 root root 0 Aug 10 08:58 device -> ../../../spi0.0 drwxr-xr-x 2 root root 0 Aug 10 08:58 power lrwxrwxrwx 1 root root 0 Aug 10 08:58 subsystem -> ../../../../../../../../class/hwmon -rw-r--r-- 1 root root 4096 Aug 10 08:58 uevent Remove target device by executing the following command: $ echo 0 > /sys/class/spi_master/spi0/delete_device Signed-off-by: Wei Yongjun Signed-off-by: Zhang Xiaoxu --- drivers/spi/Kconfig | 12 +++ drivers/spi/Makefile | 1 + drivers/spi/spi-mockup.c | 211 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 drivers/spi/spi-mockup.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 2c21d5b96fdc..9169081cfecb 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1218,6 +1218,18 @@ config SPI_TLE62X0 sysfs interface, with each line presented as a kind of GPIO exposing both switch control and diagnostic feedback. +config SPI_MOCKUP + tristate "SPI controller Testing Driver" + depends on OF + help + This enables SPI controller testing driver, which provides a way to + test SPI subsystem. + + If you do build this module, be sure to read the notes and warnings + in . + + If you don't know what to do here, definitely say N. + # # Add new SPI protocol masters in alphabetical order above this line # diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 6af54842b9fa..f28074e61df9 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -12,6 +12,7 @@ obj-$(CONFIG_SPI_MEM) += spi-mem.o obj-$(CONFIG_SPI_MUX) += spi-mux.o obj-$(CONFIG_SPI_SPIDEV) += spidev.o obj-$(CONFIG_SPI_LOOPBACK_TEST) += spi-loopback-test.o +obj-$(CONFIG_SPI_MOCKUP) += spi-mockup.o # SPI master controller drivers (bus) obj-$(CONFIG_SPI_ALTERA) += spi-altera-platform.o diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c new file mode 100644 index 000000000000..683a0fc43f0d --- /dev/null +++ b/drivers/spi/spi-mockup.c @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * SPI controller Testing Driver + * + * Copyright(c) 2022 Huawei Technologies Co., Ltd. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#define MOCKUP_CHIPSELECT_MAX 8 + +struct mockup_spi { + struct mutex lock; + struct spi_device *devs[MOCKUP_CHIPSELECT_MAX]; +}; + +static struct spi_controller *to_spi_controller(struct device *dev) +{ + return container_of(dev, struct spi_controller, dev); +} + +static ssize_t +new_device_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct spi_controller *ctrl = to_spi_controller(dev); + struct spi_board_info info; + struct mockup_spi *mock; + struct spi_device *spi; + char *blank, end; + int status; + + memset(&info, 0, sizeof(struct spi_board_info)); + + blank = strchr(buf, ' '); + if (!blank) { + dev_err(dev, "%s: Extra parameters\n", "new_device"); + return -EINVAL; + } + + if (blank - buf > SPI_NAME_SIZE - 1) { + dev_err(dev, "%s: Invalid device name\n", "new_device"); + return -EINVAL; + } + + memcpy(info.modalias, buf, blank - buf); + + status = sscanf(++blank, "%hi%c", &info.chip_select, &end); + if (status < 1) { + dev_err(dev, "%s: Can't parse SPI chipselect\n", "new_device"); + return -EINVAL; + } + + if (status > 1 && end != '\n') { + dev_err(dev, "%s: Extra parameters\n", "new_device"); + return -EINVAL; + } + + if (info.chip_select >= ctrl->num_chipselect) { + dev_err(dev, "%s: Invalid chip_select\n", "new_device"); + return -EINVAL; + } + + mock = spi_controller_get_devdata(ctrl); + mutex_lock(&mock->lock); + + if (mock->devs[info.chip_select]) { + dev_err(dev, "%s: Chipselect %d already in use\n", + "new_device", info.chip_select); + mutex_unlock(&mock->lock); + return -EINVAL; + } + + spi = spi_new_device(ctrl, &info); + if (!spi) { + mutex_unlock(&mock->lock); + return -ENOMEM; + } + mock->devs[info.chip_select] = spi; + + mutex_unlock(&mock->lock); + + dev_info(dev, "%s: Instantiated device %s at 0x%02x\n", "new_device", + info.modalias, info.chip_select); + + return count; +} +static DEVICE_ATTR_WO(new_device); + +static ssize_t +delete_device_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct spi_controller *ctrl = to_spi_controller(dev); + struct mockup_spi *mock; + struct spi_device *spi; + unsigned short chip; + char end; + int res; + + /* Parse parameters, reject extra parameters */ + res = sscanf(buf, "%hi%c", &chip, &end); + if (res < 1) { + dev_err(dev, "%s: Can't parse SPI address\n", "delete_device"); + return -EINVAL; + } + if (res > 1 && end != '\n') { + dev_err(dev, "%s: Extra parameters\n", "delete_device"); + return -EINVAL; + } + + if (chip >= ctrl->num_chipselect) { + dev_err(dev, "%s: Invalid chip_select\n", "delete_device"); + return -EINVAL; + } + + mock = spi_controller_get_devdata(ctrl); + mutex_lock(&mock->lock); + + spi = mock->devs[chip]; + if (!spi) { + mutex_unlock(&mock->lock); + dev_err(dev, "%s: Invalid chip_select\n", "delete_device"); + return -ENOENT; + } + + dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", "delete_device", + dev_name(&spi->dev), chip); + + spi_unregister_device(spi); + mock->devs[chip] = NULL; + + mutex_unlock(&mock->lock); + + return count; +} +static DEVICE_ATTR_WO(delete_device); + +static struct attribute *spi_mockup_attrs[] = { + &dev_attr_new_device.attr, + &dev_attr_delete_device.attr, + NULL +}; +ATTRIBUTE_GROUPS(spi_mockup); + +static int spi_mockup_transfer(struct spi_controller *ctrl, + struct spi_message *msg) +{ + msg->status = 0; + spi_finalize_current_message(ctrl); + + return 0; +} + +static int spi_mockup_probe(struct platform_device *pdev) +{ + int ret; + struct mockup_spi *mock; + struct spi_controller *ctrl; + + ctrl = spi_alloc_host(&pdev->dev, sizeof(struct mockup_spi)); + if (!ctrl) { + pr_err("failed to alloc spi controller\n"); + return -ENOMEM; + } + + platform_set_drvdata(pdev, ctrl); + + ctrl->dev.of_node = pdev->dev.of_node; + ctrl->dev.groups = spi_mockup_groups; + ctrl->num_chipselect = MOCKUP_CHIPSELECT_MAX; + ctrl->mode_bits = SPI_MODE_USER_MASK; + ctrl->bus_num = 0; + ctrl->transfer_one_message = spi_mockup_transfer; + + mock = spi_controller_get_devdata(ctrl); + mutex_init(&mock->lock); + + ret = devm_spi_register_controller(&pdev->dev, ctrl); + if (ret) { + spi_controller_put(ctrl); + return ret; + } + + return 0; +} + +static const struct of_device_id spi_mockup_match[] = { + { .compatible = "spi-mockup", }, + { } +}; +MODULE_DEVICE_TABLE(of, spi_mockup_match); + +static struct platform_driver spi_mockup_driver = { + .probe = spi_mockup_probe, + .driver = { + .name = "spi-mockup", + .of_match_table = spi_mockup_match, + }, +}; +module_platform_driver(spi_mockup_driver); + +MODULE_AUTHOR("Wei Yongjun "); +MODULE_DESCRIPTION("SPI controller Testing Driver"); +MODULE_LICENSE("GPL"); -- 2.34.1