From patchwork Thu May 21 16:43:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 6457691 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5F01D9F399 for ; Thu, 21 May 2015 16:47:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4A7902015A for ; Thu, 21 May 2015 16:47:35 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 36979201BB for ; Thu, 21 May 2015 16:47:34 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YvTaO-0003bv-UY; Thu, 21 May 2015 16:44:44 +0000 Received: from mail-wg0-f49.google.com ([74.125.82.49]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YvTZW-00037z-S1 for linux-arm-kernel@lists.infradead.org; Thu, 21 May 2015 16:43:52 +0000 Received: by wgez8 with SMTP id z8so1640543wge.0 for ; Thu, 21 May 2015 09:43:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qn8twaCHnw5DcJlKOi/1c5LP1PSRQ/jXgK9ujx8FGcU=; b=dl1Z7dVLGU6G4M5UGSymuXZHXaGe85nyCFcTWNFZwdQ1x6tEfs+Ul/Zh47CQxYkYm5 juow0M/3et1Snyj/4uQeXfhTk3F3YvHe/MIOdQ46pZGhLrNR4foTeIxTrqxYsyYV0xPZ BLhSo+IPZ9WK9579El22T5bodKRbrD0WapJfSYMs29cplMazeacEWgAY9zkcvKASKaBv A5NIjCR4xfaXM/jkhWEUaEOH49rffH6zZR10rwZPtcFZLAMYmF/ZjbJo0nIjC+8yRBjB SGdj/II8jOqTTcq1UhIqgiIQf2NtHYXn+mkwwP+cXExxqiNeGk2iGAXIvi1N3+ATqxP+ rL+g== X-Gm-Message-State: ALoCoQmYtT8ZGnWp4o+HIs1YbfcopGhbBoIm9iL+KT0gyi4NxvrtpZbFeAZsjIvzjqixRMs0bnh0 X-Received: by 10.194.157.228 with SMTP id wp4mr6705129wjb.79.1432226608776; Thu, 21 May 2015 09:43:28 -0700 (PDT) Received: from srini-ThinkPad-X1-Carbon-2nd.dlink.com (host-2-96-95-164.as13285.net. [2.96.95.164]) by mx.google.com with ESMTPSA id bg4sm32922222wjc.10.2015.05.21.09.43.25 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 21 May 2015 09:43:28 -0700 (PDT) From: Srinivas Kandagatla To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v5 05/11] nvmem: Add nvmem_device based consumer apis. Date: Thu, 21 May 2015 17:43:23 +0100 Message-Id: <1432226603-8863-1-git-send-email-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1432226535-8640-1-git-send-email-srinivas.kandagatla@linaro.org> References: <1432226535-8640-1-git-send-email-srinivas.kandagatla@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150521_094351_097997_075ADA98 X-CRM114-Status: GOOD ( 16.42 ) X-Spam-Score: -0.7 (/) Cc: devicetree@vger.kernel.org, arnd@arndb.de, Greg Kroah-Hartman , s.hauer@pengutronix.de, sboyd@codeaurora.org, linux-kernel@vger.kernel.org, pantelis.antoniou@konsulko.com, Rob Herring , Srinivas Kandagatla , Mark Brown , Kumar Gala , mporter@konsulko.com, Maxime Ripard , linux-api@vger.kernel.org, linux-arm-msm@vger.kernel.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds read/write apis which are based on nvmem_device. It is common that the drivers like omap cape manager or qcom cpr driver to access bytes directly at particular offset in the eeprom and not from nvmem cell info in DT. These driver would need to get access to the nvmem directly, which is what these new APIS provide. These wrapper apis would help such users to avoid code duplication in there drivers and also avoid them reading a big eeprom blob and parsing it internally in there driver. Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 120 +++++++++++++++++++++++++++++++++++++++++ include/linux/nvmem-consumer.h | 57 ++++++++++++++++++++ include/linux/nvmem-provider.h | 10 +--- 3 files changed, 179 insertions(+), 8 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 8a4b358..68ee8d1 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -436,6 +436,51 @@ static int __nvmem_device_put(struct nvmem_device *nvmem) return 0; } +static int nvmem_match(struct device *dev, const void *data) +{ + return !strcmp(dev_name(dev), (const char *)data); +} + +static struct nvmem_device *nvmem_find(const char *name) +{ + struct device *d; + + d = class_find_device(&nvmem_class, NULL, (void *)name, nvmem_match); + + return d ? to_nvmem_device(d) : NULL; +} + +struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name) +{ + struct device_node *nvmem_np, *np = dev->of_node; + struct nvmem_device *nvmem; + int index; + + if (np) { /* try dt first */ + index = of_property_match_string(np, "nvmem-names", dev_name); + + nvmem_np = of_parse_phandle(np, "nvmem", index); + if (!nvmem_np) + return ERR_PTR(-EINVAL); + + nvmem = __nvmem_device_get(nvmem_np, NULL, NULL); + + if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) + return nvmem; + + } + + return nvmem_find(dev_name); + +} +EXPORT_SYMBOL_GPL(nvmem_device_get); + +void nvmem_device_put(struct nvmem_device *nvmem) +{ + __nvmem_device_put(nvmem); +} +EXPORT_SYMBOL_GPL(nvmem_device_put); + static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id) { struct nvmem_cell *cell = NULL; @@ -725,6 +770,81 @@ int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len) } EXPORT_SYMBOL_GPL(nvmem_cell_write); +int nvmem_device_cell_read(struct nvmem_device *nvmem, + struct nvmem_cell_info *info, void *buf) +{ + struct nvmem_cell cell; + int rc, len; + + if (!nvmem || !nvmem->regmap) + return -EINVAL; + + rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); + if (IS_ERR_VALUE(rc)) + return rc; + + rc = __nvmem_cell_read(nvmem, &cell, buf, &len); + if (IS_ERR_VALUE(rc)) + return rc; + + return len; +} +EXPORT_SYMBOL_GPL(nvmem_device_cell_read); + +int nvmem_device_cell_write(struct nvmem_device *nvmem, + struct nvmem_cell_info *info, void *buf) +{ + struct nvmem_cell cell; + int rc; + + if (!nvmem || !nvmem->regmap) + return -EINVAL; + + rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); + if (IS_ERR_VALUE(rc)) + return rc; + + return nvmem_cell_write(&cell, buf, cell.bytes); +} +EXPORT_SYMBOL_GPL(nvmem_device_cell_write); + +int nvmem_device_read(struct nvmem_device *nvmem, + unsigned int offset, + size_t bytes, void *buf) +{ + int rc; + + if (!nvmem || !nvmem->regmap) + return -EINVAL; + + rc = regmap_raw_read(nvmem->regmap, offset, buf, bytes); + + if (IS_ERR_VALUE(rc)) + return rc; + + return bytes; +} +EXPORT_SYMBOL_GPL(nvmem_device_read); + +int nvmem_device_write(struct nvmem_device *nvmem, + unsigned int offset, + size_t bytes, void *buf) +{ + int rc; + + if (!nvmem || !nvmem->regmap) + return -EINVAL; + + rc = regmap_raw_write(nvmem->regmap, offset, buf, bytes); + + if (IS_ERR_VALUE(rc)) + return rc; + + + return bytes; +} +EXPORT_SYMBOL_GPL(nvmem_device_write); + static int nvmem_init(void) { return class_register(&nvmem_class); diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h index c3fa8c7..66c67ba 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -14,6 +14,15 @@ /* consumer cookie */ struct nvmem_cell; +struct nvmem_device; + +struct nvmem_cell_info { + const char *name; + int offset; + int bytes; + int bit_offset; + int nbits; +}; #if IS_ENABLED(CONFIG_NVMEM) @@ -23,6 +32,18 @@ void nvmem_cell_put(struct nvmem_cell *cell); void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len); int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len); +/* direct nvmem device read/write interface */ +struct nvmem_device *nvmem_device_get(struct device *dev, const char *name); +void nvmem_device_put(struct nvmem_device *nvmem); +int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset, + size_t bytes, void *buf); +int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset, + size_t bytes, void *buf); +int nvmem_device_cell_read(struct nvmem_device *nvmem, + struct nvmem_cell_info *info, void *buf); +int nvmem_device_cell_write(struct nvmem_device *nvmem, + struct nvmem_cell_info *info, void *buf); + #else struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name) @@ -44,6 +65,42 @@ static inline int nvmem_cell_write(struct nvmem_cell *cell, { return -ENOSYS; } + +static inline struct nvmem_device *nvmem_device_get(struct device *dev, + const char *name) +{ + return ERR_PTR(-ENOSYS); +} + +static inline void nvmem_device_put(struct nvmem_device *nvmem) +{ +} + +static inline int nvmem_device_cell_read(struct nvmem_device *nvmem, + struct nvmem_cell_info *info, + void *buf) +{ + return -ENOSYS; +} + +static inline int nvmem_device_cell_write(struct nvmem_device *nvmem, + struct nvmem_cell_info *info, + void *buf) +{ + return -ENOSYS; +} + +static inline int nvmem_device_read(struct nvmem_device *nvmem, + unsigned int offset, size_t bytes, void *buf) +{ + return -ENOSYS; +} + +static inline int nvmem_device_write(struct nvmem_device *nvmem, + unsigned int offset, size_t bytes, void *buf) +{ + return -ENOSYS; +} #endif /* CONFIG_NVMEM */ #endif /* ifndef _LINUX_NVMEM_CONSUMER_H */ diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index 4908b37..7a982cd 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -12,15 +12,9 @@ #ifndef _LINUX_NVMEM_PROVIDER_H #define _LINUX_NVMEM_PROVIDER_H -struct nvmem_device; +#include -struct nvmem_cell_info { - const char *name; - int offset; - int bytes; - int bit_offset; - int nbits; -}; +struct nvmem_device; struct nvmem_config { struct device *dev;