From patchwork Thu Apr 25 23:20:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serge Semin X-Patchwork-Id: 1091120 X-Patchwork-Delegate: patchwork@peda.user.lysator.liu.se Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="esWJp5u6"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44qtVN0Rlwz9s3q for ; Fri, 26 Apr 2019 09:21:32 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727691AbfDYXVa (ORCPT ); Thu, 25 Apr 2019 19:21:30 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:41748 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726380AbfDYXV2 (ORCPT ); Thu, 25 Apr 2019 19:21:28 -0400 Received: by mail-lj1-f193.google.com with SMTP id k8so1158567lja.8; Thu, 25 Apr 2019 16:21:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5unOTGTYjPq/YaacusOqWe2upWmJasD+CYebIMJG8+g=; b=esWJp5u6w8pDArcVpK1/APIfZnNz/YtZ5u1KosRDaxyCB+IN0VHVmCLnvsSg+TOq2S Oiyr5MM2WiZELobv/w4cxZxQ0xhm70wJKkEBGbNxVsqT+QU7ASI4oZmdHLdDZYxqI8/2 h/A8Y73wztld8L1RzwYEmC/dItCIUE6hJ2h9E+g2zbDb+v6JblXawQS+exGKTzhZrUp0 ++kCXBHuvLNlkmxb5njUvleaioZG7nrgZwf6VM3hZplFI9eVnztsqc7tRLREaFbM4SsQ JGGREHMhXEBUmR49x6Ye9R+xeefs2bBeeFuyC8WCunI9AFZTW1XQE4zPzVTc6OrYERH2 9o/A== 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:mime-version:content-transfer-encoding; bh=5unOTGTYjPq/YaacusOqWe2upWmJasD+CYebIMJG8+g=; b=s02qRfG0+4ZSGhhdMeh/gp5vvhu28XtTv6WE/qhAcM6bVZFZKMojtSYdk38pJ9I4yk zOd7SMUgDe6EY1O1IjZfP7t3cGEuuVl2WEsdZ1GUny1/VG+U+8BYRdoaT0FEwrI4/bge WR1fKSZmAz/zw0+QrJ6QN84dw2BLM3YDjJCcO6sDYhpqdc8WXJUdrFokfelx9bbx+DI1 DSsh3WSCdUkY7HgaamNN96H7TDRqaekdjfzEQe0buoSH7pRyu88roJTzKWlEhWVvU6E3 OJB9f3CCKlAzDkxPbkmZVJlq4iQb64O3H1OnSbmQ083k+FGZ9Rq3Y/rEGp1Q0Y+j39a/ T/+Q== X-Gm-Message-State: APjAAAUPZJKj8YDNRajCX1l9sOPYCBrznTy7gKjL5uj6ZLBTOoKXiHpz hwdPdvv4fkw+4v59dgeyLvSRShkE X-Google-Smtp-Source: APXvYqz0FQn2S7CvjThUHNDl5l4gT6GXw6Xfi+FKBtPYljGFXLEGYzIrmiYOZecyGQgsMjaS7n+VPw== X-Received: by 2002:a2e:9895:: with SMTP id b21mr22777681ljj.183.1556234485729; Thu, 25 Apr 2019 16:21:25 -0700 (PDT) Received: from localhost.localdomain ([5.164.240.123]) by smtp.gmail.com with ESMTPSA id j9sm4919609lja.92.2019.04.25.16.21.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Apr 2019 16:21:25 -0700 (PDT) From: Serge Semin To: Peter Korsgaard , Peter Rosin Cc: Serge Semin , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/3] i2c-mux-gpio: Unpin a platform-based device initialization Date: Fri, 26 Apr 2019 02:20:26 +0300 Message-Id: <20190425232028.9333-2-fancer.lancer@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190425232028.9333-1-fancer.lancer@gmail.com> References: <20190425232028.9333-1-fancer.lancer@gmail.com> MIME-Version: 1.0 Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org We can unpin a code specific for i2c-mux-gpio device declared as platform device. In this case the platform data just needs to be copied to the private storage and if GPIO chip pointer is referring to a valid GPIO chip descriptor save it' base number for further GPIOs request and initialization. The rest of the code is common for both platform and OF-based setups. It's also pointless and might be even errors prone to proceed with further initialization if OF kernel config is disabled and plat-based initialization isn't defined. Just return an error in this case. Signed-off-by: Serge Semin --- Changelog v2 - Return an error if OF kconfig is disabled while dt-based GPIOs probe is called. --- drivers/i2c/muxes/i2c-mux-gpio.c | 69 ++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c index 13882a2a4f60..54158b825acd 100644 --- a/drivers/i2c/muxes/i2c-mux-gpio.c +++ b/drivers/i2c/muxes/i2c-mux-gpio.c @@ -132,48 +132,55 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, struct platform_device *pdev) { - return 0; + return -EINVAL; } #endif +static int i2c_mux_gpio_probe_plat(struct gpiomux *mux, + struct platform_device *pdev) +{ + struct i2c_mux_gpio_platform_data *data = dev_get_platdata(&pdev->dev); + struct gpio_chip *gpio; + + /* + * If a GPIO chip name is provided, the GPIO pin numbers provided are + * relative to its base GPIO number. Otherwise they are absolute. + */ + if (data->gpio_chip) { + gpio = gpiochip_find(data->gpio_chip, + match_gpio_chip_by_label); + if (!gpio) + return -EPROBE_DEFER; + + mux->gpio_base = gpio->base; + } else { + mux->gpio_base = 0; + } + + memcpy(&mux->data, data, sizeof(mux->data)); + + return 0; +} + static int i2c_mux_gpio_probe(struct platform_device *pdev) { struct i2c_mux_core *muxc; struct gpiomux *mux; struct i2c_adapter *parent; struct i2c_adapter *root; - unsigned initial_state, gpio_base; + unsigned initial_state; int i, ret; mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); if (!mux) return -ENOMEM; - if (!dev_get_platdata(&pdev->dev)) { + if (!dev_get_platdata(&pdev->dev)) ret = i2c_mux_gpio_probe_dt(mux, pdev); - if (ret < 0) - return ret; - } else { - memcpy(&mux->data, dev_get_platdata(&pdev->dev), - sizeof(mux->data)); - } - - /* - * If a GPIO chip name is provided, the GPIO pin numbers provided are - * relative to its base GPIO number. Otherwise they are absolute. - */ - if (mux->data.gpio_chip) { - struct gpio_chip *gpio; - - gpio = gpiochip_find(mux->data.gpio_chip, - match_gpio_chip_by_label); - if (!gpio) - return -EPROBE_DEFER; - - gpio_base = gpio->base; - } else { - gpio_base = 0; - } + else + ret = i2c_mux_gpio_probe_plat(mux, pdev); + if (ret) + return ret; parent = i2c_get_adapter(mux->data.parent); if (!parent) @@ -194,7 +201,6 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) root = i2c_root_adapter(&parent->dev); muxc->mux_locked = true; - mux->gpio_base = gpio_base; if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { initial_state = mux->data.idle; @@ -207,14 +213,15 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) struct device *gpio_dev; struct gpio_desc *gpio_desc; - ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio"); + ret = gpio_request(mux->gpio_base + mux->data.gpios[i], + "i2c-mux-gpio"); if (ret) { dev_err(&pdev->dev, "Failed to request GPIO %d\n", mux->data.gpios[i]); goto err_request_gpio; } - ret = gpio_direction_output(gpio_base + mux->data.gpios[i], + ret = gpio_direction_output(mux->gpio_base + mux->data.gpios[i], initial_state & (1 << i)); if (ret) { dev_err(&pdev->dev, @@ -224,7 +231,7 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) goto err_request_gpio; } - gpio_desc = gpio_to_desc(gpio_base + mux->data.gpios[i]); + gpio_desc = gpio_to_desc(mux->gpio_base + mux->data.gpios[i]); mux->gpios[i] = gpio_desc; if (!muxc->mux_locked) @@ -256,7 +263,7 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) i = mux->data.n_gpios; err_request_gpio: for (; i > 0; i--) - gpio_free(gpio_base + mux->data.gpios[i - 1]); + gpio_free(mux->gpio_base + mux->data.gpios[i - 1]); alloc_failed: i2c_put_adapter(parent); From patchwork Thu Apr 25 23:20:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serge Semin X-Patchwork-Id: 1091122 X-Patchwork-Delegate: patchwork@peda.user.lysator.liu.se Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="XWbfiphh"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44qtVg0Q25z9s3q for ; Fri, 26 Apr 2019 09:21:47 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728247AbfDYXVb (ORCPT ); Thu, 25 Apr 2019 19:21:31 -0400 Received: from mail-lf1-f67.google.com ([209.85.167.67]:33619 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726088AbfDYXV3 (ORCPT ); Thu, 25 Apr 2019 19:21:29 -0400 Received: by mail-lf1-f67.google.com with SMTP id j11so1074989lfm.0; Thu, 25 Apr 2019 16:21:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bZ1cEh8dKU062ETNm3DtfpguUM23925KSmCWyDQ/Img=; b=XWbfiphhjdzGFtP6X3JDbtURo/p83L212o4xwHrJ5ocxR31x9ibk7UuXKQuYNRsLGJ XIP5Nm5GGwOzgPJy7e8SZslHfPgGawU1oWjIK93qQzeXqj0ZiDta3CAfoK7fwV0Ml51M 4xZ1khuM/SiQFkQoNspJUK53RMn2uVJaKUuNNbddDj1hqdDJZdZR2lq4S743vs+Ea6Xg 8F4+Se+khx1OzCwCybVTncUO1jIHrdHJqwyIvMSF0/ZcILtQHBkwZhT74GyqHt9BNRNa J5fGC/xkA6ZdfV4FPTo/qwibjQSdHNK1f+vKE21vkPOkx9sxKt9pjqcecGNMTwRKp9EX 3sjg== 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:mime-version:content-transfer-encoding; bh=bZ1cEh8dKU062ETNm3DtfpguUM23925KSmCWyDQ/Img=; b=j3xRdpj4jbfLl91C40n5BinQpHP8QwQxW1kvzBSFe0QQaP97Q6qZxSy38aOShLJuRJ CYSaZrTLAsytFl7eLYQ4NaGYLrl+2u4eu0Odx0lHkAFJSwu8dbP8UA9gzgFQ2+cJisdM dnLrYcYZa0nGO5iDE/sKoCiRX6hC2H5pCmeOn0kqKOrXosKhLbK9qK25SKOj/BRfof3H McbZqlXF3Nq2SR8vdl9bCfxOouzGN9FKeH+uKV9i3JsqWOitAdLxxm/S3YNCnYTqz8qM TGwjJ+GHgRK8mxNPA6UjIT0gd8hUM1qHhy/01tnWokEJfRyPiilez9WNVkdhJhB65zhL rNSA== X-Gm-Message-State: APjAAAVWv9z6s+VDgpj7u++aFx3YP5I21d71rn1t2h7u9WCdwaMncarO bcensUl37aRACwJo/dgOX24= X-Google-Smtp-Source: APXvYqyGvQEQUnKZvX5LPloduK3uYey8I/iniZKH0l1tgyTjSnc4yuhVB9+4W3Tol4N9aWJE2eI5wQ== X-Received: by 2002:ac2:523a:: with SMTP id i26mr17951604lfl.118.1556234487429; Thu, 25 Apr 2019 16:21:27 -0700 (PDT) Received: from localhost.localdomain ([5.164.240.123]) by smtp.gmail.com with ESMTPSA id j9sm4919609lja.92.2019.04.25.16.21.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Apr 2019 16:21:26 -0700 (PDT) From: Serge Semin To: Peter Korsgaard , Peter Rosin Cc: Serge Semin , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/3] i2c-mux-gpio: Unpin the platform-specific GPIOs request code Date: Fri, 26 Apr 2019 02:20:27 +0300 Message-Id: <20190425232028.9333-3-fancer.lancer@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190425232028.9333-1-fancer.lancer@gmail.com> References: <20190425232028.9333-1-fancer.lancer@gmail.com> MIME-Version: 1.0 Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org The GPIOs request loop can be safely moved to a separate function. First of all it shall improve the code readability. Secondly the initialization loop at this point is used for both of- and platform_data-based initialization paths, but it will be changed in the next patch, so by isolating the code we'll simplify the future work. Signed-off-by: Serge Semin --- Changelog v2 - Create a dedicated initial_state field in the "gpiomux" structure to keep an initial channel selector state. --- drivers/i2c/muxes/i2c-mux-gpio.c | 113 +++++++++++++++++++------------ 1 file changed, 68 insertions(+), 45 deletions(-) diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c index 54158b825acd..e10f72706b99 100644 --- a/drivers/i2c/muxes/i2c-mux-gpio.c +++ b/drivers/i2c/muxes/i2c-mux-gpio.c @@ -20,7 +20,8 @@ struct gpiomux { struct i2c_mux_gpio_platform_data data; - unsigned gpio_base; + unsigned int gpio_base; + unsigned int initial_state; struct gpio_desc **gpios; }; @@ -162,13 +163,68 @@ static int i2c_mux_gpio_probe_plat(struct gpiomux *mux, return 0; } +static int i2c_mux_gpio_request_plat(struct gpiomux *mux, + struct platform_device *pdev) +{ + struct i2c_mux_core *muxc = platform_get_drvdata(pdev); + struct gpio_desc *gpio_desc; + struct i2c_adapter *root; + struct device *gpio_dev; + int i, ret; + + root = i2c_root_adapter(&muxc->parent->dev); + + for (i = 0; i < mux->data.n_gpios; i++) { + ret = gpio_request(mux->gpio_base + mux->data.gpios[i], + "i2c-mux-gpio"); + if (ret) { + dev_err(&pdev->dev, "Failed to request GPIO %d\n", + mux->data.gpios[i]); + goto err_request_gpio; + } + + ret = gpio_direction_output(mux->gpio_base + mux->data.gpios[i], + mux->initial_state & (1 << i)); + if (ret) { + dev_err(&pdev->dev, + "Failed to set direction of GPIO %d to output\n", + mux->data.gpios[i]); + i++; /* gpio_request above succeeded, so must free */ + goto err_request_gpio; + } + + gpio_desc = gpio_to_desc(mux->gpio_base + mux->data.gpios[i]); + mux->gpios[i] = gpio_desc; + + if (!muxc->mux_locked) + continue; + + gpio_dev = &gpio_desc->gdev->dev; + muxc->mux_locked = i2c_root_adapter(gpio_dev) == root; + } + + return 0; + +err_request_gpio: + for (; i > 0; i--) + gpio_free(mux->gpio_base + mux->data.gpios[i - 1]); + + return ret; +} + +static void i2c_mux_gpio_free(struct gpiomux *mux) +{ + int i; + + for (i = 0; i < mux->data.n_gpios; i++) + gpiod_free(mux->gpios[i]); +} + static int i2c_mux_gpio_probe(struct platform_device *pdev) { struct i2c_mux_core *muxc; struct gpiomux *mux; struct i2c_adapter *parent; - struct i2c_adapter *root; - unsigned initial_state; int i, ret; mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); @@ -198,48 +254,18 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) platform_set_drvdata(pdev, muxc); - root = i2c_root_adapter(&parent->dev); - muxc->mux_locked = true; if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { - initial_state = mux->data.idle; + mux->initial_state = mux->data.idle; muxc->deselect = i2c_mux_gpio_deselect; } else { - initial_state = mux->data.values[0]; + mux->initial_state = mux->data.values[0]; } - for (i = 0; i < mux->data.n_gpios; i++) { - struct device *gpio_dev; - struct gpio_desc *gpio_desc; - - ret = gpio_request(mux->gpio_base + mux->data.gpios[i], - "i2c-mux-gpio"); - if (ret) { - dev_err(&pdev->dev, "Failed to request GPIO %d\n", - mux->data.gpios[i]); - goto err_request_gpio; - } - - ret = gpio_direction_output(mux->gpio_base + mux->data.gpios[i], - initial_state & (1 << i)); - if (ret) { - dev_err(&pdev->dev, - "Failed to set direction of GPIO %d to output\n", - mux->data.gpios[i]); - i++; /* gpio_request above succeeded, so must free */ - goto err_request_gpio; - } - - gpio_desc = gpio_to_desc(mux->gpio_base + mux->data.gpios[i]); - mux->gpios[i] = gpio_desc; - - if (!muxc->mux_locked) - continue; - - gpio_dev = &gpio_desc->gdev->dev; - muxc->mux_locked = i2c_root_adapter(gpio_dev) == root; - } + ret = i2c_mux_gpio_request_plat(mux, pdev); + if (ret) + goto alloc_failed; if (muxc->mux_locked) dev_info(&pdev->dev, "mux-locked i2c mux\n"); @@ -260,10 +286,9 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) add_adapter_failed: i2c_mux_del_adapters(muxc); - i = mux->data.n_gpios; -err_request_gpio: - for (; i > 0; i--) - gpio_free(mux->gpio_base + mux->data.gpios[i - 1]); + + i2c_mux_gpio_free(mux); + alloc_failed: i2c_put_adapter(parent); @@ -274,12 +299,10 @@ static int i2c_mux_gpio_remove(struct platform_device *pdev) { struct i2c_mux_core *muxc = platform_get_drvdata(pdev); struct gpiomux *mux = i2c_mux_priv(muxc); - int i; i2c_mux_del_adapters(muxc); - for (i = 0; i < mux->data.n_gpios; i++) - gpio_free(mux->gpio_base + mux->data.gpios[i]); + i2c_mux_gpio_free(mux); i2c_put_adapter(muxc->parent); From patchwork Thu Apr 25 23:20:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serge Semin X-Patchwork-Id: 1091121 X-Patchwork-Delegate: patchwork@peda.user.lysator.liu.se Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GjVd32B2"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 44qtVY2cfZz9s3q for ; Fri, 26 Apr 2019 09:21:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728853AbfDYXVk (ORCPT ); Thu, 25 Apr 2019 19:21:40 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:37569 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727575AbfDYXVb (ORCPT ); Thu, 25 Apr 2019 19:21:31 -0400 Received: by mail-lj1-f194.google.com with SMTP id v13so1181798ljk.4; Thu, 25 Apr 2019 16:21:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VCyHOO8OquTiABOe6VlVOA3htMwlp2az2GZDhqfzYwQ=; b=GjVd32B2uoFlmJyGwMAGI6NKXBsvTLouutAr8dtKxAf4A5zEaQwZt5Br+sXcgkQXDq ehFa3QzT2LQR0FNBEIGyD+FXsITfMcLSvbAnhlqkTsv/8KyntugWo4PUDgQmGrLEFo8i WGiPUmeyLwNoax6Oxhdztb53Vrv6yfC0vXeGBTYhGLctVU3JSh110EknX80M6F9s1WuR k9jGQ7ItYXX0rCut+D/ovIYm6SkA6ZXPnbDFsgugl4hiUbVw/bI+e8LN275c/2EiERdP 8e8/1nb/3JImzrG73CM8r2tGD6TTyiz/yAAUJ+aHJx9VX6o7+RRqosM8waHpeJEhOZPB Eizw== 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:mime-version:content-transfer-encoding; bh=VCyHOO8OquTiABOe6VlVOA3htMwlp2az2GZDhqfzYwQ=; b=iNRxoY/zlN6nDbvoRdsU8OoO5xUuHJ8zBI9jBXQ6UUcNTOe75dSCOL0g5Snzz3rlyr GfKeZw6QUHYcw6Z5qwrId54lN0VumHM/RGwPO5famek38pzDbZXltGVybObOYsZTlpny uuK2manPTAiNr/3kA1aWYHcqT47oeFVz29nEoJz1MagORojJjOCHhMkVeshMkKvU1gb1 mmLh4DTXRRweLz78Q+cljo3ZaEWfs247DllUpvRaX/z7dA4hZpmSM5CThbkKhJWtj3FJ Xt6ieQVPkm2uvjC3Meh1EFML4psd/DXaXD7mcz1QriITuoNgE/yT8e7MXl5Ep9D4v09i I5vw== X-Gm-Message-State: APjAAAXFsX7aR/asmp/C6YasTC7IPl3HMT8O7Jmayj4yuVmaSKtUo7WU jl8vPdn/sRyavoDN3k476V0= X-Google-Smtp-Source: APXvYqxe2s+PqUbQpV6u8+eSfi29gUEboiE/DAz/gatqI66k5SfZTlTjJ7Ohke1SOvQHdKoBpSChRw== X-Received: by 2002:a2e:8e93:: with SMTP id z19mr3510568ljk.159.1556234488910; Thu, 25 Apr 2019 16:21:28 -0700 (PDT) Received: from localhost.localdomain ([5.164.240.123]) by smtp.gmail.com with ESMTPSA id j9sm4919609lja.92.2019.04.25.16.21.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Apr 2019 16:21:28 -0700 (PDT) From: Serge Semin To: Peter Korsgaard , Peter Rosin Cc: Serge Semin , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] i2c-mux-gpio: Create of-based GPIOs request method Date: Fri, 26 Apr 2019 02:20:28 +0300 Message-Id: <20190425232028.9333-4-fancer.lancer@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190425232028.9333-1-fancer.lancer@gmail.com> References: <20190425232028.9333-1-fancer.lancer@gmail.com> MIME-Version: 1.0 Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Most modern platforms provide a dts with description of the devices available in the system. It may also include i2c-gpio-mux'es. Up until now the i2c-mux-gpio driver supported it' dts nodes, but performed the GPIOs request by means of legacy GPIO API, which by design and due to being legacy doesn't know anything about of/dtb/fdt/dts stuff. It means even though the i2c-gpio-mux dts nodes are successfully mapped to the kernel i2c-mux devices, the GPIOs used for initialization are requested without OF_GPIO_* flags setup. It causes problems on the platforms which fully rely on dts and reside, for instance, i2c-gpio-muxes with active low, push-pull, open drain or open source GPIOs connected. It is fixed by implementing a dedicated method for full dts-based GPIOs requests. It is mostly similar to the platform one, but utilizes the gpiod_get_from_of_node() method to request the GPIOs. Signed-off-by: Serge Semin --- Changelog v2 - Remove fallback pattern when selecting the dt- or plat-based GPIOs request methods. - Use a dedicated initial_state field in the "gpiomux" structure to select a proper channel initially. --- drivers/i2c/muxes/i2c-mux-gpio.c | 68 ++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c index e10f72706b99..d1a9c56fa1ec 100644 --- a/drivers/i2c/muxes/i2c-mux-gpio.c +++ b/drivers/i2c/muxes/i2c-mux-gpio.c @@ -66,8 +66,8 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, struct device_node *np = pdev->dev.of_node; struct device_node *adapter_np, *child; struct i2c_adapter *adapter; - unsigned *values, *gpios; - int i = 0, ret; + unsigned int *values; + int i = 0; if (!np) return -ENODEV; @@ -110,24 +110,48 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, return -EINVAL; } - gpios = devm_kcalloc(&pdev->dev, - mux->data.n_gpios, sizeof(*mux->data.gpios), - GFP_KERNEL); - if (!gpios) { - dev_err(&pdev->dev, "Cannot allocate gpios array"); - return -ENOMEM; - } + return 0; +} + +static int i2c_mux_gpio_request_dt(struct gpiomux *mux, + struct platform_device *pdev) +{ + struct i2c_mux_core *muxc = platform_get_drvdata(pdev); + struct device_node *np = pdev->dev.of_node; + struct i2c_adapter *root; + struct device *gpio_dev; + enum gpiod_flags dflags; + int i, ret; + + root = i2c_root_adapter(&muxc->parent->dev); for (i = 0; i < mux->data.n_gpios; i++) { - ret = of_get_named_gpio(np, "mux-gpios", i); - if (ret < 0) - return ret; - gpios[i] = ret; - } + if (mux->initial_state & (1 << i)) + dflags = GPIOD_OUT_HIGH; + else + dflags = GPIOD_OUT_LOW; + + mux->gpios[i] = gpiod_get_from_of_node(np, "mux-gpios", i, + dflags, "i2c-mux-gpio"); + if (IS_ERR(mux->gpios[i])) { + ret = PTR_ERR(mux->gpios[i]); + goto err_request_gpio; + } + + if (!muxc->mux_locked) + continue; - mux->data.gpios = gpios; + gpio_dev = &mux->gpios[i]->gdev->dev; + muxc->mux_locked = i2c_root_adapter(gpio_dev) == root; + } return 0; + +err_request_gpio: + for (i--; i >= 0; i--) + gpiod_free(mux->gpios[i]); + + return ret; } #else static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, @@ -135,6 +159,12 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, { return -EINVAL; } + +static int i2c_mux_gpio_request_dt(struct gpiomux *mux, + struct platform_device *pdev) +{ + return -EINVAL; +} #endif static int i2c_mux_gpio_probe_plat(struct gpiomux *mux, @@ -172,6 +202,9 @@ static int i2c_mux_gpio_request_plat(struct gpiomux *mux, struct device *gpio_dev; int i, ret; + if (!mux->data.gpios) + return -EINVAL; + root = i2c_root_adapter(&muxc->parent->dev); for (i = 0; i < mux->data.n_gpios; i++) { @@ -263,7 +296,10 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) mux->initial_state = mux->data.values[0]; } - ret = i2c_mux_gpio_request_plat(mux, pdev); + if (!dev_get_platdata(&pdev->dev)) + ret = i2c_mux_gpio_request_dt(mux, pdev); + else + ret = i2c_mux_gpio_request_plat(mux, pdev); if (ret) goto alloc_failed;