Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp3604806imm; Wed, 5 Sep 2018 03:00:12 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbxWyOmjcHcbveax1VT7aVUfjM+rKEULkxLX7YaVQW1Ri9u4IysoHeemEGmION+cVCoyk8/ X-Received: by 2002:a62:5247:: with SMTP id g68-v6mr5612634pfb.264.1536141612645; Wed, 05 Sep 2018 03:00:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536141612; cv=none; d=google.com; s=arc-20160816; b=gj6jn0huk60gbKWz2G58EjrnQsANh2bAjISa7bDYI8oQEWPr9pVe9tIaS06AfCeyBY RHni+veGtR3TpDCaNweSqdILJkOxsQAgBTR5cxN0ZjTsH88rQZhYjpuHSk9n4gZ1zX3m pNzNS/oR3ZcjG4kJEKU2CQ2vtefkQHeB17bZCbWQ0KDJ1cESgBFysCD7XDgfE/Hx6OnV OQkGntqCjsD2c5nOlnWeG3hg6347TUiTVyGvxCx7FztH6ZaStOfu3oIaFb6TmVALKNu2 jye/Nto8gnmzpdOg23M+BbOkUhxZ0l9BkDQiLj+jTl9jRpnokELyIfpmYiCZg44kZ8lQ SV5g== 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=WJJykhItDGy0gN6YBeA8zfc/w+bKn7To1re4h0kJVQI=; b=zj9P9wbM3iE/g/XaEkzKLGZXdKdpG3da8DM/X20GoTbtnz35yYRDyaYZndBVlxUCzq zWmro2jN9hGHxu1AaiC+qQtCjPrKiouPSaeGMkYini00oES0SwP8idkzMPbiPISWjXxh BxAd+S6E5HH+ADBSf7OhqBRpv9zvwrMx6SEAX2zJBY+I7vPAa+8k1sUWk2ebdBqnENm3 9hxuJnjO0aQtEWYx1Aaz9yXC0CZMav8F7BS02ipmUc/TIQORtJOl/8Bh1YI5xHWlWrr8 e5bI9auyTUOuYRHD1ac2urGagXGOiPCkE+PIMbayCr8M7TY/DSsblBzFqWke2CyqP8/x ol8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=1YaFD3CO; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g2-v6si1561907pgg.83.2018.09.05.02.59.57; Wed, 05 Sep 2018 03:00:12 -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=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=1YaFD3CO; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728390AbeIEO1q (ORCPT + 99 others); Wed, 5 Sep 2018 10:27:46 -0400 Received: from mail-wr1-f52.google.com ([209.85.221.52]:41079 "EHLO mail-wr1-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728364AbeIEO1p (ORCPT ); Wed, 5 Sep 2018 10:27:45 -0400 Received: by mail-wr1-f52.google.com with SMTP id z96-v6so6957512wrb.8 for ; Wed, 05 Sep 2018 02:58:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=WJJykhItDGy0gN6YBeA8zfc/w+bKn7To1re4h0kJVQI=; b=1YaFD3COT/uRZpNlxRTljza+p4lrXxbbJ6qXxSydu8sqRAOfR1QaEVsiPtP8crDM4p Y6a0oY68K6+md1MEwzn8E8Vy/ykydPFewR+6oEkEjb54JGmRCw1ehGnxwJM8CQGOSs3C 4/fHpj0wLIWZEV6zkwztSyj8DESo27qY98RswMpGytaiebZ29e+1T/g74fGpPKdMomVc vDzM8iEaIx3oDcBpX0NqaQFhPXVp/0tsQqQ8fKP0DAwNQsyLnCfPwqx7UimVJFP+YGI8 39ASaVVTkYQd9ORNUnfHVRTi/kIE7mcfJ5MxTxg1kT4pjRqbrsoc2v8k6DpTaYyCQ5Bd 9qJQ== 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=WJJykhItDGy0gN6YBeA8zfc/w+bKn7To1re4h0kJVQI=; b=JyexpRNzg4VmZ9eX3ZC/lIO5Rd2GbE1aGTTKwKEESIO3XcsFizi3O0v9w/+wfrXU0v n/eLuSGP8T49iULeDs75FHWRU0GfubI5Z1JzZ0Cmr7VGRLArRBs5e1+/18l+CwutFH84 aXDigCSlyS1Bjx3ky/pFR13l82smoHTXi2LRGsAEYA2sRMBaywhv1rEcnn1j1cnlchvA 3ekLGLAqRK+ZPF0ptBympxTzEWidSzGbGJ/dixpelWHFDYKK8JeTIzwXjKGAOIFhjGIG K5oDDbCVQjQxViEK1Oe1VAvxNploER+ILNoAMWxFpsHRxvtsDOy0E12b0u6PS6ylpWyh P69g== X-Gm-Message-State: APzg51B6esE24KzmrURpqGFoPumvVRtvvUD0OGz6dN3e03Chyabj4A8M e2sPW8kfZBQ2Bw9pMyhs69ppEw== X-Received: by 2002:adf:8b98:: with SMTP id o24-v6mr25121594wra.110.1536141496711; Wed, 05 Sep 2018 02:58:16 -0700 (PDT) Received: from brgl-bgdev.baylibre.local (AStLambert-681-1-33-235.w90-86.abo.wanadoo.fr. [90.86.152.235]) by smtp.gmail.com with ESMTPSA id l12-v6sm2127815wrv.29.2018.09.05.02.58.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Sep 2018 02:58:16 -0700 (PDT) From: Bartosz Golaszewski To: Srinivas Kandagatla , Joachim Eastwood , "David S . Miller" , Mauro Carvalho Chehab , Greg Kroah-Hartman , Andrew Morton , Arnd Bergmann , Jonathan Corbet , Sekhar Nori , Kevin Hilman , David Lechner , Boris Brezillon , Andrew Lunn , Alban Bedel , Maxime Ripard Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Bartosz Golaszewski Subject: [PATCH 10/13] nvmem: resolve cells from DT at registration time Date: Wed, 5 Sep 2018 11:57:35 +0200 Message-Id: <20180905095738.26406-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180905095738.26406-1-brgl@bgdev.pl> References: <20180905095738.26406-1-brgl@bgdev.pl> 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 users 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 --- drivers/nvmem/core.c | 122 ++++++++++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 48 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 854baa0559a1..da7a9d5beb33 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -405,6 +405,73 @@ static int nvmem_add_cells_from_list(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_notifier() - Register a notifier block for nvmem events. * @@ -514,6 +581,9 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) rval = nvmem_add_cells_from_list(nvmem); if (rval) goto err_teardown_compat; + rval = nvmem_add_cells_from_of(nvmem); + if (rval) + goto err_remove_cells; rval = blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); if (rval) @@ -811,10 +881,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 */ @@ -834,54 +902,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 @@ -978,7 +1005,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); -- 2.18.0