Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 606DBC433EF for ; Tue, 22 Mar 2022 14:39:15 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 871B583AF9; Tue, 22 Mar 2022 15:39:12 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=brickedbrain.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=aruba.it header.i=@aruba.it header.b="NCzQXwY1"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2D7CD83B32; Tue, 22 Mar 2022 15:38:55 +0100 (CET) Received: from smtpcmd0871.aruba.it (smtpcmd0871.aruba.it [62.149.156.71]) by phobos.denx.de (Postfix) with ESMTP id BC31E83B38 for ; Tue, 22 Mar 2022 15:38:46 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=brickedbrain.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=luca.ellero@brickedbrain.com Received: from asem-TANK-H57.asem.intra ([151.1.184.193]) by Aruba Outgoing Smtp with ESMTPSA id WfennPxbNsXrPWfennPkkl; Tue, 22 Mar 2022 15:38:46 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=aruba.it; s=a1; t=1647959926; bh=jSOt9bXxDVBc9p60qK5uEmiiSvZdxYzJWK1rJSG8ozs=; h=From:To:Subject:Date; b=NCzQXwY1+yg9yoT+CzTuCzBpa0hK72QkeIdzKvshD37n6LqcmG3FPxI200I/lZu0X XggqZquMRTuhU/SOnhCVcmHuXiXPYY02eQSsJOk02Vdh0g1IilQk+B64kon5E9+Ytw YrMuA9CNTLQVzseOsOKRW4a2C5LhybU5eJITRNiobaCpGie+UWwkBggdPuIfOlfCoo w1WTmIIMQXglB0v2EGf6+nRhjqenA0UFgPlcAk1R6QITfQtKrSZhOMSYyn9+aoErDq aG519wxa9GTIBkTkStvXFcZs3aoL8dNfqaMwzjXzgm1VGObSN4n0PYhA1C6G1m3s9l 2wossQUHnRMlA== From: Luca Ellero To: u-boot@lists.denx.de Cc: van.freenix@gmail.com, sjg@chromium.org, mario.six@gdsys.cc, Luca Ellero Subject: [PATCH] gpio: pca953x_gpio: add PCA957X support Date: Tue, 22 Mar 2022 15:38:36 +0100 Message-Id: <20220322143836.10595-1-luca.ellero@brickedbrain.com> X-Mailer: git-send-email 2.17.1 X-CMAE-Envelope: MS4wfDmc3EjfkJjJABE6nkBKMbnLATa9p+gFLUOKO8Lyt86lC2uCZZPICPIYmSY0b6O+4uD7ioMb13leOiAEjUYzT09o7dJwZoT9iUHttADq0A+JTavXCABf KRu5QxUk1/jUlbu+TOsE8Hbz95n9T6GC78dX2EXv0MZ66yvGEt3fvldqAufbQxfACSDf5g+GcjVs5oRG6n+m4g2lkuFWBmGTVks03vGi68eZdiQoa2PfyLcV wc2MbsZ2IyIBYMMJLPN95A30OSRcP74u6cbOqJ4KhbUcOFDk68f00cDLiJQZM7qX1UaKbUoBZMCaQij/GxLt1w== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean From: Luca Ellero Take as reference Linux kernel code: drivers/gpio/gpio-pca953x.c Signed-off-by: Luca Ellero --- drivers/gpio/pca953x_gpio.c | 50 +++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/drivers/gpio/pca953x_gpio.c b/drivers/gpio/pca953x_gpio.c index dc8911a8eb..f571c6f3b0 100644 --- a/drivers/gpio/pca953x_gpio.c +++ b/drivers/gpio/pca953x_gpio.c @@ -35,6 +35,12 @@ #define PCA953X_INVERT 2 #define PCA953X_DIRECTION 3 +#define PCA957X_INPUT 0 +#define PCA957X_OUTPUT 5 +#define PCA957X_INVERT 1 +#define PCA957X_DIRECTION 4 + + #define PCA_GPIO_MASK 0x00FF #define PCA_INT 0x0100 #define PCA953X_TYPE 0x1000 @@ -50,8 +56,29 @@ enum { #define MAX_BANK 5 #define BANK_SZ 8 +struct pca95xx_reg { + int input; + int output; + int invert; + int direction; +}; + +static const struct pca95xx_reg pca953x_regs = { + .direction = PCA953X_DIRECTION, + .output = PCA953X_OUTPUT, + .input = PCA953X_INPUT, + .invert = PCA953X_INVERT, +}; + +static const struct pca95xx_reg pca957x_regs = { + .direction = PCA957X_DIRECTION, + .output = PCA957X_OUTPUT, + .input = PCA957X_INPUT, + .invert = PCA957X_INVERT, +}; + /* - * struct pca953x_info - Data for pca953x + * struct pca953x_info - Data for pca953x/pca957x * * @dev: udevice structure for the device * @addr: i2c slave address @@ -61,6 +88,7 @@ enum { * @bank_count: the number of banks that the device supports * @reg_output: array to hold the value of output registers * @reg_direction: array to hold the value of direction registers + * @regs: struct to hold the registers addresses */ struct pca953x_info { struct udevice *dev; @@ -71,6 +99,7 @@ struct pca953x_info { int bank_count; u8 reg_output[MAX_BANK]; u8 reg_direction[MAX_BANK]; + const struct pca95xx_reg *regs; }; static int pca953x_write_single(struct udevice *dev, int reg, u8 val, @@ -171,12 +200,13 @@ static int pca953x_is_output(struct udevice *dev, int offset) static int pca953x_get_value(struct udevice *dev, uint offset) { + struct pca953x_info *info = dev_get_platdata(dev); int ret; u8 val = 0; int off = offset % BANK_SZ; - ret = pca953x_read_single(dev, PCA953X_INPUT, &val, offset); + ret = pca953x_read_single(dev, info->regs->input, &val, offset); if (ret) return ret; @@ -196,7 +226,7 @@ static int pca953x_set_value(struct udevice *dev, uint offset, int value) else val = info->reg_output[bank] & ~(1 << off); - ret = pca953x_write_single(dev, PCA953X_OUTPUT, val, offset); + ret = pca953x_write_single(dev, info->regs->output, val, offset); if (ret) return ret; @@ -218,7 +248,7 @@ static int pca953x_set_direction(struct udevice *dev, uint offset, int dir) else val = info->reg_direction[bank] & ~(1 << off); - ret = pca953x_write_single(dev, PCA953X_DIRECTION, val, offset); + ret = pca953x_write_single(dev, info->regs->direction, val, offset); if (ret) return ret; @@ -296,14 +326,14 @@ static int pca953x_probe(struct udevice *dev) } info->chip_type = PCA_CHIP_TYPE(driver_data); - if (info->chip_type != PCA953X_TYPE) { - dev_err(dev, "Only support PCA953X chip type now.\n"); - return -EINVAL; - } + if (info->chip_type == PCA953X_TYPE) + info->regs = &pca953x_regs; + else + info->regs = &pca957x_regs; info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ); - ret = pca953x_read_regs(dev, PCA953X_OUTPUT, info->reg_output); + ret = pca953x_read_regs(dev, info->regs->output, info->reg_output); if (ret) { dev_err(dev, "Error reading output register\n"); return ret; @@ -327,7 +357,7 @@ static int pca953x_probe(struct udevice *dev) /* Clear the polarity registers to no invert */ memset(val, 0, MAX_BANK); - ret = pca953x_write_regs(dev, PCA953X_INVERT, val); + ret = pca953x_write_regs(dev, info->regs->invert, val); if (ret < 0) { dev_err(dev, "Error writing invert register\n"); return ret; -- 2.17.1