From patchwork Fri Sep 21 13:40:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 147266 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp837460ljw; Fri, 21 Sep 2018 06:40:58 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZukmbbR89b3DOD5omM9n6if7e/zx/3BWDl2jO4yP9YjFi4Xva8onvhVOGs0y8Lz1lOrPP9 X-Received: by 2002:a63:2043:: with SMTP id r3-v6mr41431432pgm.105.1537537258692; Fri, 21 Sep 2018 06:40:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537537258; cv=none; d=google.com; s=arc-20160816; b=AZTpRnQzUjLA+AtJuUOb5XTOoxI50SU5s8smch9pOEeUtb2leG+HNTntliIc5CI29g B6QL5THH/2mejTbWAT57IdbXPV5gVtNlIS63v2N5AUH8XT+4kEBvPWsbnuwNrnSBg0JH zjm4ZxOiqKomfqLp+ouA1lmyvPLNKzictRenDcG2YS8G/cDJSXHhLTf2M0rDaUvRQJE1 ZxmRmPkDx/EJkfkewykwcRDpZT+r9dGY7gqgYKiMxAPEM+JFxPTfHl5FHh+W2AKE9nms cwOy0EP5/p9YgsNhbi7EtLbilNozh9HG5o7XedfZj3ROYK8Iul1rBGkalqEhCCro+srr q0Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=mHJaK/5Wii4q77d39T++u+f9ggbq7rkhl2Ebzv7oirA=; b=kf4jFlcj3nehju5gqjkX0ATufBfLaZ82NYSwSmjm2VWn3TXR0DN88IsmT8R9PpTYWO iCE3FCDQqXxcOuWPPGYH9e/tpDoMxedASpAKV4W9FYR9sOxqXrP4L+iB2ZW75Ve6yZEV n30Lvmnvxi/uR0hFXwMIpWBZJnbk+PuQ7f0Xb834wB6FeaG83Tatuhw3Qha1tm/+yRS6 LySukkSbfEn3NL6MB4OX+yvwWuB8VA6b2lf41MbMNc7CCzm+X/PgxQa1GSdAgrcE89SC CXFTwpEyOOxVvy3IXMV01nMD+a8kW9cgVhozjkO5ryO76L/ufHK7jrXp4hoomaZAxKi9 pYkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iyFdghxZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 64-v6si28876870pfs.7.2018.09.21.06.40.57; Fri, 21 Sep 2018 06:40:58 -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; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iyFdghxZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390322AbeIUT3u (ORCPT + 32 others); Fri, 21 Sep 2018 15:29:50 -0400 Received: from mail-io1-f43.google.com ([209.85.166.43]:33320 "EHLO mail-io1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727392AbeIUT3s (ORCPT ); Fri, 21 Sep 2018 15:29:48 -0400 Received: by mail-io1-f43.google.com with SMTP id r196-v6so12252546iod.0 for ; Fri, 21 Sep 2018 06:40:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mHJaK/5Wii4q77d39T++u+f9ggbq7rkhl2Ebzv7oirA=; b=iyFdghxZBnvMsQybolqnAmvVzfYhhceINI+QYgAHmUtSCrpj6WeVfYi/rP1lKswSqU imPx8WTXorzrGjIq6Oi9dl7RoZ/6cV9Q9y6j6+ZmznBj6yeVAZc4Z+UpH6o6hjkqmKeq qjGcqxKU46qaTK4n52i4yyf7FK/K66W5VEtFM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mHJaK/5Wii4q77d39T++u+f9ggbq7rkhl2Ebzv7oirA=; b=JwFRvGjpP0qbiHpN/2jQiSnZPC3HDJh2tQHN7+tnet6Ynqh5oRZZ1byYEZ9Ejqi/SQ OnY/vboEpPI/CKOGJYerksGJsIfOhc8on9StqXEYwyrMl0WEUCKi0bEujgBvz9WO7cf6 BT00sgsp3NFev2YjKHmEtDOZXHsC6WMjOtckXbHK91DSHxgCCEmEGpAdGmw8uFxr1n63 sPMidr+XIMVGj/nDKOIudfLsdpYm5DMLmGuzEtn33CYd37vlMirQ9JKoE8+8YfLY1LfD xZU0x5lOEZx6cJgYy8EAhEI+PVXcsVFw2uYu4MlImEkVXfntOEZfOIldfMpPpjEO7zZd /FDA== X-Gm-Message-State: APzg51DkJXKslLVn09mv+i78yt+mDs2LUxWCFi6NRM+dBYBrVsIBopZD bCx00weOPlkYu82FZKkI/ooK8w== X-Received: by 2002:a5e:8510:: with SMTP id i16-v6mr36127203ioj.301.1537537252662; Fri, 21 Sep 2018 06:40:52 -0700 (PDT) Received: from localhost.localdomain ([209.82.80.116]) by smtp.gmail.com with ESMTPSA id c12-v6sm9896298ioq.81.2018.09.21.06.40.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 06:40:52 -0700 (PDT) From: srinivas.kandagatla@linaro.org To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Bartosz Golaszewski , Srinivas Kandagatla Subject: [PATCH 16/26] nvmem: resolve cells from DT at registration time Date: Fri, 21 Sep 2018 06:40:16 -0700 Message-Id: <20180921134026.4596-17-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20180921134026.4596-1-srinivas.kandagatla@linaro.org> References: <20180921134026.4596-1-srinivas.kandagatla@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski Currently we're creating a new cell structure everytime a DT user calls nvmem_cell_get(). Change this behavior by resolving the cells during nvmem provider registration and adding all cells to the provider's list. Make of_nvmem_cell_get() just parse the phandle and look the cell up in the relevant provider's list. Don't drop the cell in nvmem_cell_put(). Signed-off-by: Bartosz Golaszewski Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 123 ++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 48 deletions(-) -- 2.19.0 diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 8e0108806e65..74b6b97680d5 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -456,6 +456,73 @@ static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) return rval; } +static struct nvmem_cell * +nvmem_find_cell_by_index(struct nvmem_device *nvmem, int index) +{ + struct nvmem_cell *cell = NULL; + int i = 0; + + mutex_lock(&nvmem_mutex); + list_for_each_entry(cell, &nvmem->cells, node) { + if (index == i++) + break; + } + mutex_unlock(&nvmem_mutex); + + return cell; +} + +static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) +{ + struct device_node *parent, *child; + struct device *dev = &nvmem->dev; + struct nvmem_cell *cell; + const __be32 *addr; + int len; + + parent = dev->of_node; + + for_each_child_of_node(parent, child) { + addr = of_get_property(child, "reg", &len); + if (!addr || (len < 2 * sizeof(u32))) { + dev_err(dev, "nvmem: invalid reg on %pOF\n", child); + return -EINVAL; + } + + cell = kzalloc(sizeof(*cell), GFP_KERNEL); + if (!cell) + return -ENOMEM; + + cell->nvmem = nvmem; + cell->offset = be32_to_cpup(addr++); + cell->bytes = be32_to_cpup(addr); + cell->name = child->name; + + addr = of_get_property(child, "bits", &len); + if (addr && len == (2 * sizeof(u32))) { + cell->bit_offset = be32_to_cpup(addr++); + cell->nbits = be32_to_cpup(addr); + } + + if (cell->nbits) + cell->bytes = DIV_ROUND_UP( + cell->nbits + cell->bit_offset, + BITS_PER_BYTE); + + if (!IS_ALIGNED(cell->offset, nvmem->stride)) { + dev_err(dev, "cell %s unaligned to nvmem stride %d\n", + cell->name, nvmem->stride); + /* Cells already added will be freed later. */ + kfree(cell); + return -EINVAL; + } + + nvmem_cell_add(cell); + } + + return 0; +} + /** * nvmem_register() - Register a nvmem device for given nvmem_config. * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem @@ -546,6 +613,10 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_cells; + rval = nvmem_add_cells_from_of(nvmem); + if (rval) + goto err_remove_cells; + return nvmem; err_remove_cells: @@ -848,10 +919,8 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *name) { struct device_node *cell_np, *nvmem_np; - struct nvmem_cell *cell; struct nvmem_device *nvmem; - const __be32 *addr; - int rval, len; + struct nvmem_cell *cell; int index = 0; /* if cell name exists, find index to the name */ @@ -871,54 +940,13 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, if (IS_ERR(nvmem)) return ERR_CAST(nvmem); - addr = of_get_property(cell_np, "reg", &len); - if (!addr || (len < 2 * sizeof(u32))) { - dev_err(&nvmem->dev, "nvmem: invalid reg on %pOF\n", - cell_np); - rval = -EINVAL; - goto err_mem; - } - - cell = kzalloc(sizeof(*cell), GFP_KERNEL); + cell = nvmem_find_cell_by_index(nvmem, index); if (!cell) { - rval = -ENOMEM; - goto err_mem; - } - - cell->nvmem = nvmem; - cell->offset = be32_to_cpup(addr++); - cell->bytes = be32_to_cpup(addr); - cell->name = cell_np->name; - - addr = of_get_property(cell_np, "bits", &len); - if (addr && len == (2 * sizeof(u32))) { - cell->bit_offset = be32_to_cpup(addr++); - cell->nbits = be32_to_cpup(addr); - } - - if (cell->nbits) - cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, - BITS_PER_BYTE); - - if (!IS_ALIGNED(cell->offset, nvmem->stride)) { - dev_err(&nvmem->dev, - "cell %s unaligned to nvmem stride %d\n", - cell->name, nvmem->stride); - rval = -EINVAL; - goto err_sanity; + __nvmem_device_put(nvmem); + return ERR_PTR(-ENOENT); } - nvmem_cell_add(cell); - return cell; - -err_sanity: - kfree(cell); - -err_mem: - __nvmem_device_put(nvmem); - - return ERR_PTR(rval); } EXPORT_SYMBOL_GPL(of_nvmem_cell_get); #endif @@ -1024,7 +1052,6 @@ void nvmem_cell_put(struct nvmem_cell *cell) struct nvmem_device *nvmem = cell->nvmem; __nvmem_device_put(nvmem); - nvmem_cell_drop(cell); } EXPORT_SYMBOL_GPL(nvmem_cell_put);