From: Zhang Xiaoxu <zhangxiaoxu@huawecloud.com>
To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com,
linux-kernel@vger.kernel.org, broonie@kernel.org,
rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com,
linux-spi@vger.kernel.org
Subject: [PATCH v4 -next 2/4] spi: mockup: Add register spi device support
Date: Sat, 18 Nov 2023 18:44:40 +0800 [thread overview]
Message-ID: <20231118104442.861759-3-zhangxiaoxu@huawecloud.com> (raw)
In-Reply-To: <20231118104442.861759-1-zhangxiaoxu@huawecloud.com>
From: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>
This enable register the spi device to mockup host, it depends
configfs. After enable the mockup host, register the device by
executing the following command:
$ mkdir /config/spi-mockup/spi0/max31722
$ echo -n max31722 > /config/spi-mockup/spi0/max31722/device_id
$ echo 1 > /config/spi-mockup/spi0/max31722/live
Unregister by:
$ echo 0 > /config/spi-mockup/spi0/max31722/live
$ rmdir /config/spi-mockup/spi0/max31722
Signed-off-by: Zhang Xiaoxu <zhangxiaoxu@huawei.com>
---
drivers/spi/spi-mockup.c | 208 +++++++++++++++++++++++++++++++++++++++
1 file changed, 208 insertions(+)
diff --git a/drivers/spi/spi-mockup.c b/drivers/spi/spi-mockup.c
index acb502c14676..f053579cc37f 100644
--- a/drivers/spi/spi-mockup.c
+++ b/drivers/spi/spi-mockup.c
@@ -18,6 +18,7 @@
struct spi_mockup_host {
struct config_group group;
+ struct config_group targets_group;
struct mutex lock;
@@ -29,6 +30,7 @@ struct spi_mockup_host {
struct platform_device *pdev;
struct spi_controller *ctrl;
+ unsigned long bitmap[BITS_TO_LONGS(MOCKUP_CHIPSELECT_MAX)];
};
static struct spi_mockup_host *to_spi_mockup_host(struct config_item *item)
@@ -38,6 +40,205 @@ static struct spi_mockup_host *to_spi_mockup_host(struct config_item *item)
return container_of(group, struct spi_mockup_host, group);
}
+static struct spi_mockup_host *
+to_spi_mockup_host_from_targets(struct config_group *targets_group)
+{
+ return container_of(targets_group,
+ struct spi_mockup_host, targets_group);
+}
+
+struct spi_mockup_target {
+ struct config_group group;
+ unsigned short chip;
+ char device_id[SPI_NAME_SIZE];
+ struct spi_device *spi;
+ struct spi_mockup_host *host;
+};
+
+static struct spi_mockup_target *to_spi_mockup_target(struct config_item *item)
+{
+ struct config_group *group = to_config_group(item);
+
+ return container_of(group, struct spi_mockup_target, group);
+}
+
+static ssize_t
+spi_mockup_target_device_id_show(struct config_item *item, char *buf)
+{
+ struct spi_mockup_target *target = to_spi_mockup_target(item);
+
+ return sprintf(buf, "%s\n", target->device_id);
+}
+
+static ssize_t
+spi_mockup_target_device_id_store(struct config_item *item,
+ const char *buf, size_t len)
+{
+ struct spi_mockup_target *target = to_spi_mockup_target(item);
+
+ if (len > SPI_NAME_SIZE)
+ return -EINVAL;
+
+ memcpy(target->device_id, buf, len);
+
+ return len;
+}
+CONFIGFS_ATTR(spi_mockup_target_, device_id);
+
+static int __target_online(struct spi_mockup_target *target)
+{
+ struct spi_board_info info = {0};
+ struct device *dev;
+
+ if (target->spi)
+ return -EBUSY;
+
+ if (!target->host->pdev)
+ return -ENODEV;
+
+ target->chip = find_first_zero_bit(target->host->bitmap,
+ MOCKUP_CHIPSELECT_MAX);
+ if (target->chip < 0)
+ return target->chip;
+
+ if (target->chip > target->host->num_cs)
+ return -EBUSY;
+
+ info.chip_select = target->chip;
+ strncpy(info.modalias, target->device_id,
+ strlen(target->device_id));
+
+ target->spi = spi_new_device(target->host->ctrl, &info);
+ if (!target->spi)
+ return -ENOMEM;
+
+ bitmap_set(target->host->bitmap, target->chip, 1);
+
+ dev = &target->host->ctrl->dev;
+ dev_info(dev, "Instantiated device %s at 0x%02x\n",
+ info.modalias, info.chip_select);
+
+ return 0;
+}
+
+static int __target_offline(struct spi_mockup_target *target)
+{
+ struct device *dev;
+
+ if (!target->spi)
+ return -ENODEV;
+
+ dev = &target->host->ctrl->dev;
+ dev_info(dev, "Deleting device %s at 0x%02hx\n",
+ dev_name(&target->spi->dev), target->chip);
+
+ spi_unregister_device(target->spi);
+ target->spi = NULL;
+
+ bitmap_clear(target->host->bitmap, target->chip, 1);
+
+
+ return 0;
+}
+
+static ssize_t
+spi_mockup_target_live_store(struct config_item *item,
+ const char *buf, size_t len)
+{
+ struct spi_mockup_target *target = to_spi_mockup_target(item);
+ int ret;
+ bool res;
+
+ ret = kstrtobool(buf, &res);
+ if (ret)
+ return -EINVAL;
+
+ if (!strlen(target->device_id))
+ return -EINVAL;
+
+ mutex_lock(&target->host->lock);
+ if (res)
+ ret = __target_online(target);
+ else
+ ret = __target_offline(target);
+ mutex_unlock(&target->host->lock);
+
+ return ret ? ret : len;
+}
+
+static ssize_t
+spi_mockup_target_live_show(struct config_item *item, char *buf)
+{
+ struct spi_mockup_target *target = to_spi_mockup_target(item);
+
+ return sprintf(buf, "%s\n", (target->spi) ? "true" : "false");
+}
+CONFIGFS_ATTR(spi_mockup_target_, live);
+
+
+static struct configfs_attribute *spi_mockup_target_attrs[] = {
+ &spi_mockup_target_attr_device_id,
+ &spi_mockup_target_attr_live,
+ NULL,
+};
+
+static void spi_mockup_target_release(struct config_item *item)
+{
+ struct spi_mockup_target *target = to_spi_mockup_target(item);
+
+ __target_offline(target);
+ kfree(target);
+}
+
+static struct configfs_item_operations spi_mockup_target_item_ops = {
+ .release = spi_mockup_target_release,
+};
+
+static const struct config_item_type spi_mockup_target_item_type = {
+ .ct_owner = THIS_MODULE,
+ .ct_attrs = spi_mockup_target_attrs,
+ .ct_item_ops = &spi_mockup_target_item_ops,
+};
+
+static struct config_group *
+spi_mockup_target_make_group(struct config_group *group, const char *name)
+{
+ struct spi_mockup_target *target;
+ struct spi_mockup_host *host = to_spi_mockup_host_from_targets(group);
+
+ target = kzalloc(sizeof(*target), GFP_KERNEL);
+ if (!target)
+ return ERR_PTR(-ENOMEM);
+
+ target->host = host;
+
+ config_group_init_type_name(&target->group, name,
+ &spi_mockup_target_item_type);
+
+ return &target->group;
+}
+
+static struct configfs_group_operations spi_mockup_targets_group_ops = {
+ .make_group = spi_mockup_target_make_group,
+};
+
+static void spi_mockup_targets_group_release(struct config_item *item)
+{
+ struct spi_mockup_target *target = to_spi_mockup_target(item);
+
+ kfree(target);
+}
+
+static struct configfs_item_operations spi_mockup_targets_group_item_ops = {
+ .release = spi_mockup_targets_group_release,
+};
+
+static const struct config_item_type spi_mockup_target_type = {
+ .ct_owner = THIS_MODULE,
+ .ct_group_ops = &spi_mockup_targets_group_ops,
+ .ct_item_ops = &spi_mockup_targets_group_item_ops,
+};
+
static ssize_t __host_online(struct spi_mockup_host *host)
{
int ret;
@@ -69,6 +270,9 @@ static ssize_t __host_offline(struct spi_mockup_host *host)
if (!host->pdev)
return -ENODEV;
+ if (!bitmap_empty(host->bitmap, host->num_cs))
+ return -EBUSY;
+
platform_device_unregister(host->pdev);
host->pdev = NULL;
host->ctrl = NULL;
@@ -192,6 +396,10 @@ spi_mockup_host_make_group(struct config_group *group, const char *name)
config_group_init_type_name(&host->group, name,
&spi_mockup_host_config_group_type);
+ config_group_init_type_name(&host->targets_group, "targets",
+ &spi_mockup_target_type);
+ configfs_add_default_group(&host->targets_group, &host->group);
+
return &host->group;
}
--
2.34.1
next prev parent reply other threads:[~2023-11-18 10:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-18 10:44 [PATCH v4 -next 0/4] spi: Introduce BPF based SPI mockup controller Zhang Xiaoxu
2023-11-18 10:44 ` [PATCH v4 -next 1/4] spi: mockup: Add SPI controller testing driver Zhang Xiaoxu
2023-11-18 10:44 ` Zhang Xiaoxu [this message]
2023-11-19 17:04 ` [PATCH v4 -next 2/4] spi: mockup: Add register spi device support kernel test robot
2023-11-18 10:44 ` [PATCH v4 -next 3/4] spi: mockup: Add writeable tracepoint for spi transfer Zhang Xiaoxu
2023-11-18 10:44 ` [PATCH v4 -next 4/4] spi: mockup: Add documentation Zhang Xiaoxu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231118104442.861759-3-zhangxiaoxu@huawecloud.com \
--to=zhangxiaoxu@huawecloud.com \
--cc=broonie@kernel.org \
--cc=frowand.list@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=rostedt@goodmis.org \
--cc=weiyongjun1@huawei.com \
--cc=zhangxiaoxu5@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).