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: 48867 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f72.google.com (mail-wg0-f72.google.com [74.125.82.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A633221411 for ; Thu, 21 May 2015 16:45:33 +0000 (UTC) Received: by wgtl5 with SMTP id l5sf28310650wgt.1 for ; Thu, 21 May 2015 09:45:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=hMgMU23TDB5iC43ozVXPplon422SBExUDyRd5Ju0VLU=; b=Si31A5jvgEWj8Qon457OMqd/Jy07OwctJvlXStyp9uYGljqbYOY+BjcNEpcYZgYa/o E9uOimFdxA7H89oB1dNpbK+qXaABfe7t24WvlpCaJJcU0EZbkNCjyhwb+VyYQhJLHF13 pEuQZ56ZENA4snuRlgaXl9NU+wcsfRq2dH1qpX8dF2bHs/SwJcZK31UsYnywZOnbsuHu WJm2Y/mCzQ3Xe9DtU/77ffSD5BqjdSRhF32WSxj6pxes/1YokPUTcZsmpmhWIlu+7V4n Odmzvx5ZkIJe3fOwRJ51nhJXaDSj3TX9oKyM0t0sC0z1ucBfRQNT6LqV8VEHjDiyC8of rA7A== X-Gm-Message-State: ALoCoQmpd3Io1d+Jt8VKFRKWcFQq5nnydElzAmacEqVwD9LBKJ3CA/UweUU4N0UfWaRUBgaScQ/5 X-Received: by 10.112.83.172 with SMTP id r12mr3413588lby.23.1432226732706; Thu, 21 May 2015 09:45:32 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.19.231 with SMTP id i7ls355543lae.85.gmail; Thu, 21 May 2015 09:45:32 -0700 (PDT) X-Received: by 10.152.5.225 with SMTP id v1mr2968664lav.111.1432226732396; Thu, 21 May 2015 09:45:32 -0700 (PDT) Received: from mail-la0-f45.google.com (mail-la0-f45.google.com. [209.85.215.45]) by mx.google.com with ESMTPS id xu7si13586459lbb.139.2015.05.21.09.45.32 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 May 2015 09:45:32 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.45 as permitted sender) client-ip=209.85.215.45; Received: by laat2 with SMTP id t2so106905601laa.1 for ; Thu, 21 May 2015 09:45:32 -0700 (PDT) X-Received: by 10.153.6.36 with SMTP id cr4mr3087031lad.56.1432226732159; Thu, 21 May 2015 09:45:32 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp723472lbb; Thu, 21 May 2015 09:45:30 -0700 (PDT) X-Received: by 10.66.240.37 with SMTP id vx5mr7454103pac.16.1432226730163; Thu, 21 May 2015 09:45:30 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id pe8si7072535pac.157.2015.05.21.09.45.00; Thu, 21 May 2015 09:45:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756816AbbEUQoQ (ORCPT + 28 others); Thu, 21 May 2015 12:44:16 -0400 Received: from mail-wi0-f182.google.com ([209.85.212.182]:37260 "EHLO mail-wi0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756734AbbEUQna (ORCPT ); Thu, 21 May 2015 12:43:30 -0400 Received: by wibt6 with SMTP id t6so20477805wib.0 for ; Thu, 21 May 2015 09:43:28 -0700 (PDT) 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-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: srinivas.kandagatla@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.45 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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;