Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp345510pxb; Mon, 25 Oct 2021 09:23:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxf3tcRXgghBEFkxswIKRrRRMqA0RyuL+d9cHMtEQbDWtvKVmzEtBkrWgeI+jxutH31d8Ws X-Received: by 2002:a63:4d20:: with SMTP id a32mr14237703pgb.247.1635179009080; Mon, 25 Oct 2021 09:23:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635179009; cv=none; d=google.com; s=arc-20160816; b=kit9s0caDGxie69vfHCqac4PKzdknqxe2chJcoG+PCSiVk+pQHJBvZ1rZqBUfiesqs MBfBhRvgWezUxAPh+OewDMjX6ZwXiP5axJG1ZFLfMniFpPqZnspf1v5rUcFgm4STxA40 oi9ii0YxioMuhW7BK8UbQjcKYrDvdMShJqn12h7Te1PrWtnrVWlBAjTk9dzTLiqMn+AM /K84zAFh19WC5DAVluD9wjH/8NLOJxC8+DSiMR7fdzrEnD3PeqkQGUYeKQ2LZs5HBXEe Xm87RJzKjVobsLs+kvWM5Ub8sAhN73TKsXRayelBjNx2EvZcS+EknmCNwJJjosdils1R 9uQA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=aX2fK5PJ5chQlm9aTDAWMmbeUkrMH8hpUlQBdO8wrNA=; b=DYvkyujKInhT1y4L5JlCYltC1mlt6yM7q/DJTe8zv55dcD9r+S7R5tcw9BpUZaEuXE yVpeZWJquz7uAJWWIm+0lfm2ZAyiQUZRLXxP2gu/IBcCP5VHuI6Gcr95vMKKV1c6ERIk gyXsA4n4i1JhzuRKwZflBEE801eqRAEPo9oWIK/15QmGXmlamnkQ+cY5Bx/7RtWfy2A4 BK4QMPmCidCSOVqm+V7r1hC5QlqNyKF+KRLt0PPgBABtuTAjMCdzzMz9f4zyMzC41JsX WK14lRcI16r8Y5mrqJ8byMhfwU5RMOZTM8SPDeiBGjOCkFKk+Kwe/UkT5buj8QGwRpFT KFYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@axis.com header.s=axis-central1 header.b=MoF6cIfJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=axis.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b11si2120396pjg.36.2021.10.25.09.23.04; Mon, 25 Oct 2021 09:23:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@axis.com header.s=axis-central1 header.b=MoF6cIfJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=axis.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233443AbhJYQYA (ORCPT + 99 others); Mon, 25 Oct 2021 12:24:00 -0400 Received: from smtp2.axis.com ([195.60.68.18]:4631 "EHLO smtp2.axis.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232362AbhJYQX6 (ORCPT ); Mon, 25 Oct 2021 12:23:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1635178897; x=1666714897; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=aX2fK5PJ5chQlm9aTDAWMmbeUkrMH8hpUlQBdO8wrNA=; b=MoF6cIfJiGJgmou3JcVdQKWPsua0W8eN7ljcQHM64+UwByoEkxIT63Rk z6FxW9oGykxniOMVqhQvxUq4zSdYYXON7ZlB5wsht5Oo0PTlrGenNNA0c UNugGBd/UcNJoDW7m+H3Owz0iFQf5qE1wOap34IkDdRYvYswQvcvDgSqC u13IazsBl6VJcTNeyTI+1Kzrm87Juw1uZ6Hy3nUalsowTPCM8TqM0vUMx uJ2s3R/qt0sKpEnrmr77Kuq7ZzgUmWg3sLRThx0YIg8AwhfYmg8m54mYb K/7/qz7V3VS1TdgOVdXNj4iiLRstKwrg7q6mr70WP+TR6Hkty+tCgk+Iq Q==; From: =?UTF-8?q?Ludvig=20P=C3=A4rsson?= To: Srinivas Kandagatla CC: , =?UTF-8?q?Ludvig=20P=C3=A4rsson?= , Subject: [PATCH] nvmem: Add mockup nvmem driver Date: Mon, 25 Oct 2021 18:21:22 +0200 Message-ID: <20211025162123.5086-1-ludvig.parsson@axis.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This driver provides an easy way to test other drivers that use nvmem. E.g. Testing that an nvmem-driver that reads from OTP memory works as intended for all possible values without having to actually write all the values to OTP. Swapping the nvmem-driver reading the OTP memory for this dummy driver makes it easy to manipulate the values to test. Signed-off-by: Ludvig Pärsson --- drivers/nvmem/Kconfig | 5 +++ drivers/nvmem/Makefile | 2 ++ drivers/nvmem/mockup.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 drivers/nvmem/mockup.c diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index da414617a54d..b0caad053823 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -300,4 +300,9 @@ config NVMEM_BRCM_NVRAM This driver provides support for Broadcom's NVRAM that can be accessed using I/O mapping. +config NVMEM_MOCKUP + tristate "Mockup nvmem support" + help + This driver can be used to test other drivers that use nvmem. + endif diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index dcbbde35b6a8..1b8353f54ca2 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -61,3 +61,5 @@ obj-$(CONFIG_NVMEM_RMEM) += nvmem-rmem.o nvmem-rmem-y := rmem.o obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_brcm_nvram.o nvmem_brcm_nvram-y := brcm_nvram.o +obj-$(CONFIG_NVMEM_MOCKUP) += nvmem-mockup.o +nvmem-mockup-y := mockup.o diff --git a/drivers/nvmem/mockup.c b/drivers/nvmem/mockup.c new file mode 100644 index 000000000000..194113197c43 --- /dev/null +++ b/drivers/nvmem/mockup.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2021 Axis Communications AB + */ + +#include +#include +#include +#include + +#define SIZE_OF_MEM (64) + +struct nvmem_mockup { + struct device *dev; + u8 mem[SIZE_OF_MEM]; +}; + +static int nvmem_mockup_read(void *context, unsigned int offset, + void *val, size_t bytes) +{ + struct nvmem_mockup *priv = context; + + if (bytes + offset > SIZE_OF_MEM || bytes + offset < offset) + return -EINVAL; + + memcpy(val, &priv->mem[offset], bytes); + + return 0; +} + +static int nvmem_mockup_write(void *context, unsigned int offset, + void *val, size_t bytes) +{ + struct nvmem_mockup *priv = context; + + if (bytes + offset > SIZE_OF_MEM || bytes + offset < offset) + return -EINVAL; + + memcpy(&priv->mem[offset], val, bytes); + + return 0; +} + +static int nvmem_mockup_probe(struct platform_device *pdev) +{ + struct nvmem_config config = { }; + struct device *dev = &pdev->dev; + struct nvmem_mockup *priv; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + priv->dev = dev; + + config.dev = dev; + config.priv = priv; + config.name = "nvmem-mockup"; + config.size = SIZE_OF_MEM; + config.reg_read = nvmem_mockup_read; + config.reg_write = nvmem_mockup_write; + + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); +} + +static const struct of_device_id nvmem_mockup_match[] = { + { .compatible = "nvmem-mockup", }, + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(of, nvmem_mockup_match); + +static struct platform_driver nvmem_mockup_driver = { + .probe = nvmem_mockup_probe, + .driver = { + .name = "nvmem-mockup", + .of_match_table = nvmem_mockup_match, + }, +}; +module_platform_driver(nvmem_mockup_driver); + +MODULE_LICENSE("GPL v2"); -- 2.20.1