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: 6457641 Return-Path: X-Original-To: patchwork-linux-arm-msm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 668A6C0020 for ; Thu, 21 May 2015 16:46:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 50AFB20499 for ; Thu, 21 May 2015 16:46:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2099A20495 for ; Thu, 21 May 2015 16:46:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754764AbbEUQoO (ORCPT ); Thu, 21 May 2015 12:44:14 -0400 Received: from mail-wg0-f48.google.com ([74.125.82.48]:35262 "EHLO mail-wg0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755960AbbEUQna (ORCPT ); Thu, 21 May 2015 12:43:30 -0400 Received: by wgfl8 with SMTP id l8so91553354wgf.2 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=McatwPXIa4psDup+jLpkOiQ9YEdP53b6x9PXlZ6T1QfGmCKuJXz1xe+i30uK3+cmZa h78y8+7yYWtEhi1iQ0sxY6/UmEbLoD9LBzElX/JaUuXNcasJXXBa/4u4PtTLP3SGvgXl jMBsaQnHTwGU2JBOOEHiIXFv0BeWhytkt1+TOJdhB8K+W2uZMWmaWPwIrVdw+dlwGPWb r8dgbLDnbdmZYT6QKbgnbMD7bHcjiWS6wymI0WmkJQC9NAUDWigixkkQftENDH4tqqqV kngODROcvmST3OUdcakHBDinDf5JNvv8sMmrjHV1fRyKEu4mdIYgjcql+o5YUUZIYkui mX1A== X-Gm-Message-State: ALoCoQkwsEIKYCIPN6seTf7wHXXUHHzaq/zkW0GGTmY/JgKbgcuELphJdL0C4h3ms3+GxXYwI79p 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 Cc: Maxime Ripard , Rob Herring , Kumar Gala , Mark Brown , s.hauer@pengutronix.de, Greg Kroah-Hartman , linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-msm@vger.kernel.org, arnd@arndb.de, sboyd@codeaurora.org, Srinivas Kandagatla , pantelis.antoniou@konsulko.com, mporter@konsulko.com 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> Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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;