Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp379033lqp; Wed, 22 May 2024 07:18:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVc9Q4L1RhlngCKg316rctLTce9LyNPFD3KdYE/p8fBdMr6ibsrejnKzPH5TkkDvBa6/KJoOCOVNaLNrX+vLyaYodrJm8f2Kp8tuHh+vA== X-Google-Smtp-Source: AGHT+IE1veSdTDG5UreFfi56NaFcn+/gePUOfN+4QBJ3g1T7thelpDFrDh6+gaIp5j677gvMDGrA X-Received: by 2002:a17:902:8a8d:b0:1f3:1597:5e2 with SMTP id d9443c01a7336-1f31c97c11fmr18650195ad.19.1716387533724; Wed, 22 May 2024 07:18:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716387533; cv=pass; d=google.com; s=arc-20160816; b=yBt60XkfninGEIpzA2/JdinwP2hSKBOltopF+yxkqnp5c1eoaguZT82wIdowyoG3J6 W9cNTOgj/I+usfpYgbIyfeF3J6TW95B9Bi1Xk9EarxIFQBUKNrjCRGclJOsGUQz3N4yG Ye++KTPwxtokQOGyevx8Vz4jgcLdEbFERwyOBPf01sfnzEaw+poeKztB5Qj27bvMKIiP cl6D9qw8EkPWnJutyVbzkORBfWleLBPn68Cx3Am1NZopt6LlVn2eUISWGDBA6szlyeMt nCG7K1yrcLJA2avG5cPKO2GhQ5uBO4EeHr2heq0LRxEb5ISGXXx4tA3NBBZFZemC34a1 noiA== 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=P3sMAbFRIkWBmPB0o0w75mawDZ6JyCZpMjJ0oYP1Rdc=; b=Z9GF2uWl5Y+4YTIHQ9ItFY1oLCs3IIXh1E+hInmEuiRgrMKMdBCIYd0y48wl0JsWhI xoGme23LQNfJVX7VKDDOv3GAad7bo9gtr9KB9REQBSfcQT9ySp918zRsTlV5EjqBXUhF 8uGAryNVEGtlA7U09Nz9yXyEGuzC/yBGakban1VLla97ndap1XTwnng3WPneVRxejetJ RGGKhGEl2Ikslgv/loBSZRpyJ/7aCg0YddJ7RJkuKNAV7jsqb+DA0WkKrY3WTMSVZKgA sUjmxHsuc5JCuieV579LN9q81sWTnKpEnHRK5jfW3Nm1s0/AwT33mPzK4XCGm8jiZXtL +x+w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tq-group.com header.s=key1 header.b=c+AW49wK; dkim=temperror (no key for signature) header.i=@ew.tq-group.com header.s=dkim header.b=QACJblQ+; 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-186347-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186347-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 sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1f335686b7asi996095ad.540.2024.05.22.07.18.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 07:18:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-186347-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@tq-group.com header.s=key1 header.b=c+AW49wK; dkim=temperror (no key for signature) header.i=@ew.tq-group.com header.s=dkim header.b=QACJblQ+; 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-186347-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186347-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 631EC283491 for ; Wed, 22 May 2024 14:18:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 649311474CA; Wed, 22 May 2024 14:16:39 +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="c+AW49wK"; 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="QACJblQ+" 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 7DB18146D4C; Wed, 22 May 2024 14:16:35 +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=1716387398; cv=none; b=ArJJwkWTZbMNOQIoF5Ky/OK+jXpig2j7rPx2LvWsjbvBtsqW8Z4zv1lgwR9KIJGjUGa8rqEzmQroCVZblHjBj29oKxotlJEhzNmDPz5ma/RZknx6HL85m9CGIQaupIfppiT+a2ZKjLQdjDXyepiHaxUoiZidS25UHY/dELD/t+M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716387398; c=relaxed/simple; bh=7nni/fIxSK1yy+psJbMSJLGCnCaUVWMjlUN8sodihDk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=c2NycBc8RskhZDk9HtYuOT6Y+8eXVlmsVsKuw65Lw4HQJblf++tLAHKGKQYY/MBo38WezC5VXTp0GQr72FVI3poMyefaxVFO/i0Fn6x96XOD03Bes/JrgM4ZJ/KHET120q4y2C0OVRX1QKMe5b4SfE3xHFTyakMV0qEy00dK4os= 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=c+AW49wK; dkim=fail (0-bit key) header.d=ew.tq-group.com header.i=@ew.tq-group.com header.b=QACJblQ+ 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=1716387396; x=1747923396; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=X3yNTbYwi/bZT7mcbADrinUah/SzdseYKKjStxK5dGo=; b=c+AW49wKQFUwkGPCOOVPLyMiU0wRDpSPwWNIA663wVwIRW7V8gfxpcEQ 2hrHtvP9H8s2bGIF6Fv+pFig4kiXx7W0MU4v8gLx70P6sIoWheeq6jya5 uJg6OJ4dct3stV/sw6xfdR9+LmpJGdjgLhmEHg3eehj+Ys7J7C9qnqkY/ X9DDIe32JFXeKCT9DBBBvkDZpRUYA7iNEdDdPM94sC7k21/Uc/azvTS1i 0jXp4JJjB1fsckGvZCyrjDH1dquwNhZ+INOdBnOXoPJJ1wmrLtO/4Gpvk r2tkKWGt+qJElfY8tnNR77nHifPJ48eqh5onf7k5vVQibmLSUoTvZGjyn g==; X-CSE-ConnectionGUID: YVgkVSDSSdyYgZgdTXpsiw== X-CSE-MsgGUID: cgoPDferS0quiAtmnq1N+g== X-IronPort-AV: E=Sophos;i="6.08,179,1712613600"; d="scan'208";a="37017677" Received: from vmailcow01.tq-net.de ([10.150.86.48]) by mx1.tq-group.com with ESMTP; 22 May 2024 16:16:35 +0200 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 1E1B416E4D9; Wed, 22 May 2024 16:16:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ew.tq-group.com; s=dkim; t=1716387390; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=X3yNTbYwi/bZT7mcbADrinUah/SzdseYKKjStxK5dGo=; b=QACJblQ+P3Ql9GK5no1YGNlOVdCPujxF2dKcGFa7zXzX+/zfTuCUyCMY3B1chhagHoyA5y iAhAhDmNDIcXohhkqumG2qFl41dtDAlBcWoI4E3QBzklH5LjY2JuGNazHv3kRn3pW+eu4/ EWzg1a6j06LNfWhrMCEYLz3AVtf5c38HllPQrutF0lU+rY1EE3Fu0xrpG2sW8WBG3kyS14 lfJVrC4MsZqf6JxyrAZFkOuCXUC6LmKjmnPm/yNM+0pAxphcRZTYGmVNkKWVDo34dPr9sE AUE4dYv6H+BLDeWQE5+w7lDhQ3/x2xNqyBmzot8eWhcocnssFNBsYp1sNwRfPw== From: Gregor Herburger Date: Wed, 22 May 2024 16:15:24 +0200 Subject: [PATCH RESEND 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: <20240522-mcp251xfd-gpio-feature-v3-7-8829970269c5@ew.tq-group.com> References: <20240522-mcp251xfd-gpio-feature-v3-0-8829970269c5@ew.tq-group.com> In-Reply-To: <20240522-mcp251xfd-gpio-feature-v3-0-8829970269c5@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, Linus Walleij , Bartosz Golaszewski X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1716387339; l=6907; i=gregor.herburger@ew.tq-group.com; s=20230829; h=from:subject:message-id; bh=7nni/fIxSK1yy+psJbMSJLGCnCaUVWMjlUN8sodihDk=; b=XSohXvDnzt25+8jYbBb+pHwQNy5nDunZKSzEd6t6L10TOk/eYxgYwfKMffWunmZb7YZTGUdmr vO6yY/hD2UhDi6XIlUPG0iOJ29lqTcZs1tg+JIMS9UozELTfphXUIqa 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