Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp3067513lqo; Tue, 21 May 2024 06:07:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV5eriujsfBqrc0cMTqhcBlPgi8WIaQ2ZqKlWpzVSZY9BOl36xZRZoWNNHwvyCU9jFHifwEOT4Iy6NGauxfizvyeifIRxFW3rVmrxycFw== X-Google-Smtp-Source: AGHT+IFdbVZMIAwg5rDEWJxXSyXaV2uGMqWnFJixrPR2d8PpUC9aIlXmn0jZzt46w7md7yFLW5PT X-Received: by 2002:a05:6871:7410:b0:23d:ebc3:f18e with SMTP id 586e51a60fabf-241721bb325mr45810932fac.0.1716296862186; Tue, 21 May 2024 06:07:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716296862; cv=pass; d=google.com; s=arc-20160816; b=UHKEWDz0s6JJvq6lJgaxJUo/YVvFpKlbsNEAs0mDW5j6QzHfodd1b5v51bHbLlx+UF 2Qc11aq/EDylBcZkDMYJAQMegv1fr7l67UX7SzvAyryIfoOeCB9zee1NNMDuL5rmWuYp ujAdZIuxVdI8oLTmz9G286ruLZmDHEy3sZzSRzS3DkgKwS4+kYWf9vefLqQNI79W1HXr VBUy96Qz7dTJVwInKzkLPnGKShO6F4prZu4z21GuNA2uyRQIOQrnifc3BzyzhEzTy8Gk VBbsqw06QHmh5UHqqaq7SVJpDbZqfsdaUlmPR687VWFwk+068efCrVXBzF6936Oo/Hqu lZhw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature:dkim-signature; bh=X3yNTbYwi/bZT7mcbADrinUah/SzdseYKKjStxK5dGo=; fh=Ck0fOcHdhRzgBaHwp6SaSYBo0j21rhywV4MXv2yQ9aM=; b=trMJOg0aFAWWsobDzoG94trrCslDy8uSyjZ+31j/YMFfbE7UHA59bHc2QX7I+Y7fkg hexhyRPEpD5GLbz8EtrVT4nq2/yHbwcZl0JfnvDTNYIn9PXnQYRd3BGykE2g5HtxiNK5 8hYGT51OQVHfPM5ufIfVyuXFcsN/RvONKMCTfRc6W27DGTxHwJDSEk+i3O7uSVr3urJR q12tt5SxM1NMYgtDURDbhxBfYEbfC3T1jFkiDSIM8exL8LX5bmRpHUG+GL1sfSaH+Esa rcPha5+ZmfPgTN/CcjcXw45/RM838FJ7bXJodse0XtWy0xxiJpVqs1qdNoRUe9Cd88AB Gp8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tq-group.com header.s=key1 header.b=g1LA3l6S; dkim=temperror (no key for signature) header.i=@ew.tq-group.com header.s=dkim header.b=i5egl6f4; arc=pass (i=1 spf=pass spfdomain=ew.tq-group.com dkim=pass dkdomain=tq-group.com dmarc=pass fromdomain=ew.tq-group.com); spf=pass (google.com: domain of linux-kernel+bounces-184989-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184989-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tq-group.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43e3a655fd3si7528151cf.807.2024.05.21.06.07.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 06:07:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184989-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@tq-group.com header.s=key1 header.b=g1LA3l6S; dkim=temperror (no key for signature) header.i=@ew.tq-group.com header.s=dkim header.b=i5egl6f4; arc=pass (i=1 spf=pass spfdomain=ew.tq-group.com dkim=pass dkdomain=tq-group.com dmarc=pass fromdomain=ew.tq-group.com); spf=pass (google.com: domain of linux-kernel+bounces-184989-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184989-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tq-group.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D15EE1C218D4 for ; Tue, 21 May 2024 13:07:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1D2CB7F48F; Tue, 21 May 2024 13:06:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.b="g1LA3l6S"; dkim=fail reason="key not found in DNS" (0-bit key) header.d=ew.tq-group.com header.i=@ew.tq-group.com header.b="i5egl6f4" Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 499F87F460; Tue, 21 May 2024 13:05:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.104.207.81 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716296760; cv=none; b=GfmB6/9Pq7BpqR/oUFWaJGAU488wiQSxn6xrdpf2TwsOU/XG8Prj0ySSw+OPO/cve9Z0Hgh3q1p+PnlpW+JcP2sGnuLqqGwq/+He9OLuTLsyuIFxCz8f/VzVQcbE1Z4VVqivnmOQGeVuYiY6YDI3kURKqqNjYmWL/yzRb1QDdD4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716296760; c=relaxed/simple; bh=7nni/fIxSK1yy+psJbMSJLGCnCaUVWMjlUN8sodihDk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FEjobdIMbcmKTYyBWWY1c74l/zh47uy72ZNArv/us1edAMO5ATwIDvqksde64P3WT0IW/K1a10XjekIhNjKXQhuYIJtRXZkiIKxL8sBONGS2DA0L3++aygovVkmPsRXWyL9IbD0K4Luvh+Y2nUkpAA345evSBd5yI1UrUr2W9nw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ew.tq-group.com; spf=pass smtp.mailfrom=ew.tq-group.com; dkim=pass (2048-bit key) header.d=tq-group.com header.i=@tq-group.com header.b=g1LA3l6S; dkim=fail (0-bit key) header.d=ew.tq-group.com header.i=@ew.tq-group.com header.b=i5egl6f4 reason="key not found in DNS"; arc=none smtp.client-ip=93.104.207.81 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ew.tq-group.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ew.tq-group.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1716296759; x=1747832759; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=X3yNTbYwi/bZT7mcbADrinUah/SzdseYKKjStxK5dGo=; b=g1LA3l6SMKvnTGtqbGU2Z+KTjtL4yDeEmSOQUqn2Nf0phIVOdH8LYeMd DDdxpxp3TigmCNUrCnVj/ctkRvXEyqksL0gc3f5IE9URxrAGjOYWTsA8R R+GnaySdbAOTcfHMRsdnxDn2L39iF03G5phjg5LGykzfGTU6Ok60PXqYU l9gcXppy084bl7wAMQY6BIA/w25EilB/UPe65zriVsnOK3G2FbeI+C15y XE6b3bb5eaAAYUVl4IOQ82bZZRwKpz7quNIUcOK23GM7JuWlwSqriJj8D AqPohVNOfwbK4PMEKDIvi+WZbFBGmMIxZjJDaO6F/NNmalE9sFVkcsaAr g==; X-CSE-ConnectionGUID: CL0f68JyQqKczBx6Emey9w== X-CSE-MsgGUID: 2wFsuN4kRMiWYbMFuwHqnw== X-IronPort-AV: E=Sophos;i="6.08,177,1712613600"; d="scan'208";a="36993993" Received: from vmailcow01.tq-net.de ([10.150.86.48]) by mx1.tq-group.com with ESMTP; 21 May 2024 15:05:56 +0200 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id BF6FE175CB5; Tue, 21 May 2024 15:05:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ew.tq-group.com; s=dkim; t=1716296752; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=X3yNTbYwi/bZT7mcbADrinUah/SzdseYKKjStxK5dGo=; b=i5egl6f4wuzolfs1R0CsognOwe9ofkCtMxKuqeFOU/d7NXPeB2H5LQcwHPmyXZ4eoNnKuc IKCeFCifZ22OKglGHzFEhKYCEVoDygYciC70TxSYqPljwMnlrO39scKLsLfcGuSOlm8eSt +bgKBJhCkPOECzkRct95e5CH1G7b0a1b+jEkQ64SVC06hkSUTRF+H8lE1huK62ZsNdpvPX dvzc5ieO6fROnEQgmUyuWXspB4UZE7UTqGyXw3lBxR5OWpZXUPtz4KbfAHMU8P6zd5Vknd OZCg2hv2QU5FnqQ0wpVVqhhiCZL3WPyWlQiq1CQ/Xc4b72IRJri4ZDICqQSe0w== From: Gregor Herburger Date: Tue, 21 May 2024 15:04:57 +0200 Subject: [PATCH v3 7/8] can: mcp251xfd: add gpio functionality Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240521-mcp251xfd-gpio-feature-v3-7-7f829fefefc2@ew.tq-group.com> References: <20240521-mcp251xfd-gpio-feature-v3-0-7f829fefefc2@ew.tq-group.com> In-Reply-To: <20240521-mcp251xfd-gpio-feature-v3-0-7f829fefefc2@ew.tq-group.com> To: Marc Kleine-Budde , Manivannan Sadhasivam , Thomas Kopp , Vincent Mailhol , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux@ew.tq-group.com, gregor.herburger@ew.tq-group.com X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1716296697; l=6907; i=gregor.herburger@ew.tq-group.com; s=20230829; h=from:subject:message-id; bh=7nni/fIxSK1yy+psJbMSJLGCnCaUVWMjlUN8sodihDk=; b=t34FnZBo3bwjAHDt9QO8jb+KXbNXGeh4QTCSJjxwmdh5zr5WZGuDrHgO9QMMPOYWvKaXDLYKR 0vCSQfJQedwCCThE0icz/BThEjE37Cjd74VVnYZyIWzmp8RFGF2+WQI X-Developer-Key: i=gregor.herburger@ew.tq-group.com; a=ed25519; pk=+eRxwX7ikXwazcRjlOjj2/tbDmfVZdDLoW+xLZbQ4h4= X-Last-TLS-Session-Version: TLSv1.3 The mcp251xfd devices allow two pins to be configured as gpio. Add this functionality to driver. Signed-off-by: Gregor Herburger --- drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 179 +++++++++++++++++++++++++ drivers/net/can/spi/mcp251xfd/mcp251xfd.h | 4 + 2 files changed, 183 insertions(+) diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c index d8d936576c94..363b554f8c2e 100644 --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -1762,6 +1763,178 @@ static int mcp251xfd_register_check_rx_int(struct mcp251xfd_priv *priv) return 0; } +#ifdef CONFIG_GPIOLIB +static const char * const mcp251xfd_gpio_names[] = { "GPIO0", "GPIO1" }; + +static int mcp251xfd_gpio_request(struct gpio_chip *chip, unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 pin_mask = MCP251XFD_REG_IOCON_PM(offset); + int ret; + + if (priv->rx_int && offset == 1) { + netdev_err(priv->ndev, "Can't use GPIO 1 with RX-INT!\n"); + return -EINVAL; + } + + ret = pm_runtime_resume_and_get(priv->ndev->dev.parent); + if (ret) + return ret; + + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + pin_mask, pin_mask); +} + +static void mcp251xfd_gpio_free(struct gpio_chip *chip, unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + + pm_runtime_put(priv->ndev->dev.parent); +} + +static int mcp251xfd_gpio_get_direction(struct gpio_chip *chip, + unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 mask = MCP251XFD_REG_IOCON_TRIS(offset); + u32 val; + int ret; + + ret = regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); + if (ret) + return ret; + + if (mask & val) + return GPIO_LINE_DIRECTION_IN; + + return GPIO_LINE_DIRECTION_OUT; +} + +static int mcp251xfd_gpio_get(struct gpio_chip *chip, unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 mask = MCP251XFD_REG_IOCON_GPIO(offset); + u32 val; + int ret; + + ret = regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); + if (ret) + return ret; + + return !!(mask & val); +} + +static int mcp251xfd_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask, + unsigned long *bit) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 val; + int ret; + + ret = regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); + if (ret) + return ret; + + *bit = FIELD_GET(MCP251XFD_REG_IOCON_GPIO_MASK, val) & *mask; + + return 0; +} + +static int mcp251xfd_gpio_direction_output(struct gpio_chip *chip, + unsigned int offset, int value) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 dir_mask = MCP251XFD_REG_IOCON_TRIS(offset); + u32 val_mask = MCP251XFD_REG_IOCON_LAT(offset); + u32 val; + + if (value) + val = val_mask; + else + val = 0; + + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + dir_mask | val_mask, val); +} + +static int mcp251xfd_gpio_direction_input(struct gpio_chip *chip, + unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 dir_mask = MCP251XFD_REG_IOCON_TRIS(offset); + + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + dir_mask, dir_mask); +} + +static void mcp251xfd_gpio_set(struct gpio_chip *chip, unsigned int offset, + int value) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 val_mask = MCP251XFD_REG_IOCON_LAT(offset); + u32 val; + int ret; + + if (value) + val = val_mask; + else + val = 0; + + ret = regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + val_mask, val); + if (ret) + dev_err(&priv->spi->dev, "Failed to set GPIO %u: %d\n", + offset, ret); +} + +static void mcp251xfd_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask, + unsigned long *bits) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 val; + int ret; + + val = FIELD_PREP(MCP251XFD_REG_IOCON_LAT_MASK, *bits); + + ret = regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + MCP251XFD_REG_IOCON_LAT_MASK, val); + if (ret) + dev_err(&priv->spi->dev, "Failed to set GPIOs %d\n", ret); +} + +static int mcp251fdx_gpio_setup(struct mcp251xfd_priv *priv) +{ + struct gpio_chip *gc = &priv->gc; + + if (!device_property_present(&priv->spi->dev, "gpio-controller")) + return 0; + + gc->label = dev_name(&priv->spi->dev); + gc->parent = &priv->spi->dev; + gc->owner = THIS_MODULE; + gc->request = mcp251xfd_gpio_request; + gc->free = mcp251xfd_gpio_free; + gc->get_direction = mcp251xfd_gpio_get_direction; + gc->direction_output = mcp251xfd_gpio_direction_output; + gc->direction_input = mcp251xfd_gpio_direction_input; + gc->get = mcp251xfd_gpio_get; + gc->get_multiple = mcp251xfd_gpio_get_multiple; + gc->set = mcp251xfd_gpio_set; + gc->set_multiple = mcp251xfd_gpio_set_multiple; + gc->base = -1; + gc->can_sleep = true; + gc->ngpio = ARRAY_SIZE(mcp251xfd_gpio_names); + gc->names = mcp251xfd_gpio_names; + + return devm_gpiochip_add_data(&priv->spi->dev, gc, priv); +} +#else +static inline int mcp251fdx_gpio_setup(struct mcp251xfd_priv *priv) +{ + return 0; +} +#endif + static int mcp251xfd_register_get_dev_id(const struct mcp251xfd_priv *priv, u32 *dev_id, u32 *effective_speed_hz_slow, @@ -2135,6 +2308,12 @@ static int mcp251xfd_probe(struct spi_device *spi) if (err) goto out_free_candev; + err = mcp251fdx_gpio_setup(priv); + if (err) { + dev_err_probe(&spi->dev, err, "Failed to register gpio-controller.\n"); + goto out_free_candev; + } + err = mcp251xfd_register(priv); if (err) { dev_err_probe(&spi->dev, err, "Failed to detect %s.\n", diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd.h b/drivers/net/can/spi/mcp251xfd/mcp251xfd.h index 78637223dbc8..4727a043fae5 100644 --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd.h +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -666,6 +667,9 @@ struct mcp251xfd_priv { struct mcp251xfd_devtype_data devtype_data; struct can_berr_counter bec; +#ifdef CONFIG_GPIOLIB + struct gpio_chip gc; +#endif }; #define MCP251XFD_IS(_model) \ -- 2.34.1