From patchwork Thu May 20 11:28:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Vaittinen, Matti" X-Patchwork-Id: 443583 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B300C433ED for ; Thu, 20 May 2021 11:30:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0329C61279 for ; Thu, 20 May 2021 11:30:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238649AbhETLcG (ORCPT ); Thu, 20 May 2021 07:32:06 -0400 Received: from mail-lf1-f52.google.com ([209.85.167.52]:44742 "EHLO mail-lf1-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241981AbhETL3v (ORCPT ); Thu, 20 May 2021 07:29:51 -0400 Received: by mail-lf1-f52.google.com with SMTP id j6so21299209lfr.11; Thu, 20 May 2021 04:28:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=NABjH9mhLGUTJZ1B1wVw40uqQXDgbe7ohEozQt28Ijw=; b=hEXnj8NHqHHqimrz/o4LcTg/9wxdWyxIKqiPMxlA9HmgdgHZvdcQEfXJIMiSf2sgSY KuF59eakFXeVh2EZRp1CydKN99qmP7qP7/P+kPzfQ5dU6guYlk3mtx12yXATXkNiBRKj oN4k8kS58RqQRVK0KP5kDG5ofn3F6CjeeJ4Km3XwPoiG+02QF+Ssnx0tt9CN03NCteL3 KRZpVut/UtTCMsuI4cUjJs30O4+E3WIXBC6dKbatVl1++GYFrq90TZwiuB007zdkOlPF 43J7eE+cNOnsKYGEdYjKz1vIaA+A0MqalB1hpyPyBOGaXVtMfANPepYmViNUZmQ/8mzf wUgQ== X-Gm-Message-State: AOAM532/0a4M5OtJyrBnB6vG75H33jgon6K8uXki7oWfdfZ8DudO+94k AeBIsYfojqC/TBzQ5EBUoPk= X-Google-Smtp-Source: ABdhPJzDYNyphgHQ173kl+pG4fSUC3kqNxaY08KcogcWxHXYdEzc2JECql+4kPD513XUMrSKATWZUw== X-Received: by 2002:a05:6512:34c7:: with SMTP id w7mr3178479lfr.82.1621510107771; Thu, 20 May 2021 04:28:27 -0700 (PDT) Received: from localhost.localdomain (dc7vkhyyyyyyyyyyyyyby-3.rev.dnainternet.fi. [2001:14ba:16e2:8300::2]) by smtp.gmail.com with ESMTPSA id m4sm244855ljc.20.2021.05.20.04.28.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 04:28:27 -0700 (PDT) Date: Thu, 20 May 2021 14:28:20 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Linus Walleij , Bartosz Golaszewski , Matti Vaittinen , Michael Walle , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-power@fi.rohmeurope.com Subject: [PATCH 1/2] gpio: regmap: Support few IC specific operations Message-ID: <09091e75157ea28dcad1605008532016304356a4.1621509932.git.matti.vaittinen@fi.rohmeurope.com> MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org The set_config and init_valid_mask GPIO operations are usually very IC specific. Allow IC drivers to provide these custom operations at gpio-regmap registration. Signed-off-by: Matti Vaittinen --- drivers/gpio/gpio-regmap.c | 49 +++++++++++++++++++++++++++++++++++++ include/linux/gpio/regmap.h | 13 ++++++++++ 2 files changed, 62 insertions(+) base-commit: d07f6ca923ea0927a1024dfccafc5b53b61cfecc diff --git a/drivers/gpio/gpio-regmap.c b/drivers/gpio/gpio-regmap.c index 134cedf151a7..315285cacd3f 100644 --- a/drivers/gpio/gpio-regmap.c +++ b/drivers/gpio/gpio-regmap.c @@ -27,6 +27,10 @@ struct gpio_regmap { int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base, unsigned int offset, unsigned int *reg, unsigned int *mask); + int (*set_config)(struct regmap *regmap, void *drvdata, + unsigned int offset, unsigned long config); + int (*init_valid_mask)(struct regmap *regmap, void *drvdata, + unsigned long *valid_mask, unsigned int ngpios); void *driver_data; }; @@ -39,6 +43,43 @@ static unsigned int gpio_regmap_addr(unsigned int addr) return addr; } +static int regmap_gpio_init_valid_mask(struct gpio_chip *gc, + unsigned long *valid_mask, + unsigned int ngpios) +{ + struct gpio_regmap *gpio; + void *drvdata; + + gpio = gpiochip_get_data(gc); + + if (!gpio->init_valid_mask) { + WARN_ON(!gpio->init_valid_mask); + return -EINVAL; + } + + drvdata = gpio_regmap_get_drvdata(gpio); + + return gpio->init_valid_mask(gpio->regmap, drvdata, valid_mask, ngpios); +} + +static int gpio_regmap_set_config(struct gpio_chip *gc, unsigned int offset, + unsigned long config) +{ + struct gpio_regmap *gpio; + void *drvdata; + + gpio = gpiochip_get_data(gc); + + if (!gpio->set_config) { + WARN_ON(!gpio->set_config); + return -EINVAL; + } + + drvdata = gpio_regmap_get_drvdata(gpio); + + return gpio->set_config(gpio->regmap, drvdata, offset, config); +} + static int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, unsigned int base, unsigned int offset, unsigned int *reg, unsigned int *mask) @@ -235,6 +276,8 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config gpio->reg_clr_base = config->reg_clr_base; gpio->reg_dir_in_base = config->reg_dir_in_base; gpio->reg_dir_out_base = config->reg_dir_out_base; + gpio->set_config = config->set_config; + gpio->init_valid_mask = config->init_valid_mask; /* if not set, assume there is only one register */ if (!gpio->ngpio_per_reg) @@ -253,6 +296,10 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config chip->ngpio = config->ngpio; chip->names = config->names; chip->label = config->label ?: dev_name(config->parent); + if (gpio->set_config) + chip->set_config = gpio_regmap_set_config; + if (gpio->init_valid_mask) + chip->init_valid_mask = regmap_gpio_init_valid_mask; #if defined(CONFIG_OF_GPIO) /* gpiolib will use of_node of the parent if chip->of_node is NULL */ @@ -280,6 +327,8 @@ struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config chip->direction_output = gpio_regmap_direction_output; } + gpio_regmap_set_drvdata(gpio, config->drvdata); + ret = gpiochip_add_data(chip, gpio); if (ret < 0) goto err_free_gpio; diff --git a/include/linux/gpio/regmap.h b/include/linux/gpio/regmap.h index 334dd928042b..c382a3caefc3 100644 --- a/include/linux/gpio/regmap.h +++ b/include/linux/gpio/regmap.h @@ -33,10 +33,18 @@ struct regmap; * @ngpio_per_reg: Number of GPIOs per register * @irq_domain: (Optional) IRQ domain if the controller is * interrupt-capable + * @drvdata: (Optional) Pointer to IC specific data which is + * not used by gpio-remap but is provided "as is" to + * the driver callback(s). + * * @reg_mask_xlate: (Optional) Translates base address and GPIO * offset to a register/bitmask pair. If not * given the default gpio_regmap_simple_xlate() * is used. + * @set_config: (Optional) hook for all kinds of settings. Uses + * the same packed config format as generic pinconf. + * @init_valid_mask: (Optional) routine to initialize @valid_mask, to + * be used if not all GPIOs are valid. * * The ->reg_mask_xlate translates a given base address and GPIO offset to * register and mask pair. The base address is one of the given register @@ -74,10 +82,15 @@ struct gpio_regmap_config { int reg_stride; int ngpio_per_reg; struct irq_domain *irq_domain; + void *drvdata; int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base, unsigned int offset, unsigned int *reg, unsigned int *mask); + int (*set_config)(struct regmap *regmap, void *drvdata, + unsigned int offset, unsigned long config); + int (*init_valid_mask)(struct regmap *regmap, void *drvdata, + unsigned long *valid_mask, unsigned int ngpios); }; struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config); From patchwork Thu May 20 11:29:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Vaittinen, Matti" X-Patchwork-Id: 444912 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7826C433B4 for ; Thu, 20 May 2021 11:32:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B6DD66105A for ; Thu, 20 May 2021 11:32:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241487AbhETLd2 (ORCPT ); Thu, 20 May 2021 07:33:28 -0400 Received: from mail-lj1-f174.google.com ([209.85.208.174]:34482 "EHLO mail-lj1-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240815AbhETLb2 (ORCPT ); Thu, 20 May 2021 07:31:28 -0400 Received: by mail-lj1-f174.google.com with SMTP id b12so12040557ljp.1; Thu, 20 May 2021 04:30:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=U9v2XFXhxWrJhhTytw+psilkDCTZjJus/6PodmNXrec=; b=HItdze0PUMMV9yizKnC0DZQAUQ+cMYOFfoiZ9WZ9SKkJJLfubXbpkFAVl6n5rG319+ KjqAqtdl1XQi4ZeTsSER/BaKiIz7FSd2WES1cZaA5yaM5rTByL2TLfA7FBQh3klFQ0+B TgG7iRFXFas7QJ7xpOwPITNlOp0DH8RAdMUfyJ9qC6DOvAF/xfdWWmHAN7mb5VCszHYx EqsIQcynBKOPMQTPEb110Zch87UX4/kBHamKxgH3R/yi7V/ygVo1k5FRP1VVVKzxxClx 8nHklP150UQWoGxjn40+jJsF1DEuflG0eZq/qEXQV5Cu8BB7nuiuLlZkTSYcn5JQu/R4 KmZQ== X-Gm-Message-State: AOAM531YZ7IzugmqVlUcoAX6Y91ZZsUAywZoprhXgm8udXtpVZJaObX0 wzEitKwfTvFSDmm96E63X3s= X-Google-Smtp-Source: ABdhPJwdYh5+eDjhh/s7gGLVPa8CI29IYjav5VMGf846yjxHbKRHlLhc75ZQ+QTKXlajNlYIoA2UFA== X-Received: by 2002:a2e:a314:: with SMTP id l20mr2634956lje.167.1621510205827; Thu, 20 May 2021 04:30:05 -0700 (PDT) Received: from localhost.localdomain (dc7vkhyyyyyyyyyyyyyby-3.rev.dnainternet.fi. [2001:14ba:16e2:8300::2]) by smtp.gmail.com with ESMTPSA id q3sm236113ljp.67.2021.05.20.04.30.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 04:30:05 -0700 (PDT) Date: Thu, 20 May 2021 14:29:57 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Linus Walleij , Bartosz Golaszewski , Matti Vaittinen , Michael Walle , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-power@fi.rohmeurope.com Subject: [PATCH 2/2] gpio: bd71815: Use gpio-regmap Message-ID: <7f19bf2bb61ad64a9df94ab16bc1cca08e8efe43.1621509932.git.matti.vaittinen@fi.rohmeurope.com> References: <09091e75157ea28dcad1605008532016304356a4.1621509932.git.matti.vaittinen@fi.rohmeurope.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <09091e75157ea28dcad1605008532016304356a4.1621509932.git.matti.vaittinen@fi.rohmeurope.com> Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Utilize the gpio-regmap helper and drop the custom functions Signed-off-by: Matti Vaittinen Reviewed-by: Linus Walleij --- drivers/gpio/Kconfig | 1 + drivers/gpio/gpio-bd71815.c | 106 ++++++++++-------------------------- 2 files changed, 29 insertions(+), 78 deletions(-) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 1dd0ec6727fd..97e1348cd410 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -1120,6 +1120,7 @@ config GPIO_BD70528 config GPIO_BD71815 tristate "ROHM BD71815 PMIC GPIO support" depends on MFD_ROHM_BD71828 + select GPIO_REGMAP help Support for GPO(s) on ROHM BD71815 PMIC. There are two GPOs available on the ROHM PMIC. diff --git a/drivers/gpio/gpio-bd71815.c b/drivers/gpio/gpio-bd71815.c index 08ff2857256f..a241c01e08d1 100644 --- a/drivers/gpio/gpio-bd71815.c +++ b/drivers/gpio/gpio-bd71815.c @@ -9,6 +9,7 @@ */ #include +#include #include #include #include @@ -18,81 +19,33 @@ #include struct bd71815_gpio { - /* chip.parent points the MFD which provides DT node and regmap */ - struct gpio_chip chip; - /* dev points to the platform device for devm and prints */ struct device *dev; - struct regmap *regmap; }; -static int bd71815gpo_get(struct gpio_chip *chip, unsigned int offset) -{ - struct bd71815_gpio *bd71815 = gpiochip_get_data(chip); - int ret, val; - - ret = regmap_read(bd71815->regmap, BD71815_REG_GPO, &val); - if (ret) - return ret; - - return (val >> offset) & 1; -} - -static void bd71815gpo_set(struct gpio_chip *chip, unsigned int offset, - int value) -{ - struct bd71815_gpio *bd71815 = gpiochip_get_data(chip); - int ret, bit; - - bit = BIT(offset); - - if (value) - ret = regmap_set_bits(bd71815->regmap, BD71815_REG_GPO, bit); - else - ret = regmap_clear_bits(bd71815->regmap, BD71815_REG_GPO, bit); - - if (ret) - dev_warn(bd71815->dev, "failed to toggle GPO\n"); -} - -static int bd71815_gpio_set_config(struct gpio_chip *chip, unsigned int offset, +static int bd71815_gpio_set_config(struct regmap *regmap, void *drvdata, + unsigned int offset, unsigned long config) { - struct bd71815_gpio *bdgpio = gpiochip_get_data(chip); + struct bd71815_gpio *bdgpio = (struct bd71815_gpio *)drvdata; switch (pinconf_to_config_param(config)) { case PIN_CONFIG_DRIVE_OPEN_DRAIN: - return regmap_update_bits(bdgpio->regmap, + return regmap_update_bits(regmap, BD71815_REG_GPO, BD71815_GPIO_DRIVE_MASK << offset, BD71815_GPIO_OPEN_DRAIN << offset); case PIN_CONFIG_DRIVE_PUSH_PULL: - return regmap_update_bits(bdgpio->regmap, + return regmap_update_bits(regmap, BD71815_REG_GPO, BD71815_GPIO_DRIVE_MASK << offset, BD71815_GPIO_CMOS << offset); default: + dev_err(bdgpio->dev, "Unsupported config (0x%lx)\n", config); break; } return -ENOTSUPP; } -/* BD71815 GPIO is actually GPO */ -static int bd71815gpo_direction_get(struct gpio_chip *gc, unsigned int offset) -{ - return GPIO_LINE_DIRECTION_OUT; -} - -/* Template for GPIO chip */ -static const struct gpio_chip bd71815gpo_chip = { - .label = "bd71815", - .owner = THIS_MODULE, - .get = bd71815gpo_get, - .get_direction = bd71815gpo_direction_get, - .set = bd71815gpo_set, - .set_config = bd71815_gpio_set_config, - .can_sleep = true, -}; - #define BD71815_TWO_GPIOS GENMASK(1, 0) #define BD71815_ONE_GPIO BIT(0) @@ -111,14 +64,16 @@ static const struct gpio_chip bd71815gpo_chip = { * but allows using it by providing the DT property * "rohm,enable-hidden-gpo". */ -static int bd71815_init_valid_mask(struct gpio_chip *gc, +static int bd71815_init_valid_mask(struct regmap *regmap, void *drvdata, unsigned long *valid_mask, unsigned int ngpios) { + struct bd71815_gpio *bdgpio = (struct bd71815_gpio *)drvdata; + if (ngpios != 2) return 0; - if (gc->parent && device_property_present(gc->parent, + if (bdgpio->dev && device_property_present(bdgpio->dev->parent, "rohm,enable-hidden-gpo")) *valid_mask = BD71815_TWO_GPIOS; else @@ -127,9 +82,19 @@ static int bd71815_init_valid_mask(struct gpio_chip *gc, return 0; } +/* Template for regmap gpio config */ +static const struct gpio_regmap_config gpio_cfg_template = { + .label = "bd71815", + .reg_set_base = BD71815_REG_GPO, + .ngpio = 2, + .set_config = bd71815_gpio_set_config, + .init_valid_mask = bd71815_init_valid_mask, +}; + static int gpo_bd71815_probe(struct platform_device *pdev) { struct bd71815_gpio *g; + struct gpio_regmap_config cfg; struct device *parent, *dev; /* @@ -144,30 +109,15 @@ static int gpo_bd71815_probe(struct platform_device *pdev) if (!g) return -ENOMEM; - g->chip = bd71815gpo_chip; - - /* - * FIXME: As writing of this the sysfs interface for GPIO control does - * not respect the valid_mask. Do not trust it but rather set the ngpios - * to 1 if "rohm,enable-hidden-gpo" is not given. - * - * This check can be removed later if the sysfs export is fixed and - * if the fix is backported. - * - * For now it is safest to just set the ngpios though. - */ - if (device_property_present(parent, "rohm,enable-hidden-gpo")) - g->chip.ngpio = 2; - else - g->chip.ngpio = 1; - - g->chip.init_valid_mask = bd71815_init_valid_mask; - g->chip.base = -1; - g->chip.parent = parent; - g->regmap = dev_get_regmap(parent, NULL); g->dev = dev; - return devm_gpiochip_add_data(dev, &g->chip, g); + cfg = gpio_cfg_template; + cfg.parent = parent; + cfg.regmap = dev_get_regmap(parent, NULL); + cfg.fwnode = dev_fwnode(dev); + cfg.drvdata = g; + + return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &cfg)); } static struct platform_driver gpo_bd71815_driver = {