From patchwork Mon Dec 6 21:48:42 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 74467 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 856C3B7043 for ; Tue, 7 Dec 2010 08:48:54 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B49792814C; Mon, 6 Dec 2010 22:48:52 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9vBSWfFzzYjg; Mon, 6 Dec 2010 22:48:52 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B1B2228135; Mon, 6 Dec 2010 22:48:50 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 191B028135 for ; Mon, 6 Dec 2010 22:48:48 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8iMY9swYrPNc for ; Mon, 6 Dec 2010 22:48:46 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-qy0-f172.google.com (mail-qy0-f172.google.com [209.85.216.172]) by theia.denx.de (Postfix) with ESMTPS id 06CAB2811D for ; Mon, 6 Dec 2010 22:48:44 +0100 (CET) Received: by qyk34 with SMTP id 34so3971324qyk.3 for ; Mon, 06 Dec 2010 13:48:43 -0800 (PST) MIME-Version: 1.0 Received: by 10.229.82.70 with SMTP id a6mr5014910qcl.9.1291672122681; Mon, 06 Dec 2010 13:48:42 -0800 (PST) Received: by 10.229.80.135 with HTTP; Mon, 6 Dec 2010 13:48:42 -0800 (PST) Date: Tue, 7 Dec 2010 10:48:42 +1300 Message-ID: From: Chris Packham To: u-boot@lists.denx.de Subject: [U-Boot] [PATCH] gpio: add driver for PCA9539 16-bit I2C gpio expander X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de This adds support for the PCA9539 family of gpio devices which have 16 output pins. The devices are similar to chips that use the pca953x driver except the register map is expanded (in a non-backwards compatible manner) to allow for the extra 8 pins. This driver has one gotcha that you can only set the top or bottom 8 bits at a time. For example to set the 16 pins to be outputs you need to make the function calls pca9539_set_dir(PCA9539_ADDR, 0xff00, 0x0000); pca9539_set_dir(PCA9539_ADDR, 0x00ff, 0x0000); Signed-off-by: Chris Packham --- Hi, I have a couple of questions related to my patch. Firstly is anyone interested in support for these gpio devices? Secondly is my gotcha acceptable or should I put some effort into making it handle a 16-bit argument in 1 hit? I toyed with the idea of making this part of the pca953x driver. I didn't because the register layout is different so I'd still need to define PCA9539 specific functions and macros (although pca953x_reg_write could be re-used). I also figured that some people may want to define only CONFIG_PCA9539 (that is the case for me at the moment but the overhead of CONFIG_PCA953X wouldn't push the size of my image over any boundary). drivers/gpio/Makefile | 1 + drivers/gpio/pca9539.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ include/pca9539.h | 36 +++++++++++++++++++ 3 files changed, 129 insertions(+), 0 deletions(-) create mode 100644 drivers/gpio/pca9539.c create mode 100644 include/pca9539.h diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 398024c..3d7bde5 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -29,6 +29,7 @@ COBJS-$(CONFIG_AT91_GPIO) += at91_gpio.o COBJS-$(CONFIG_KIRKWOOD_GPIO) += kw_gpio.o COBJS-$(CONFIG_MXC_GPIO) += mxc_gpio.o COBJS-$(CONFIG_PCA953X) += pca953x.o +COBJS-$(CONFIG_PCA9539) += pca9539.o COBJS-$(CONFIG_S5P) += s5p_gpio.o COBJS := $(COBJS-y) diff --git a/drivers/gpio/pca9539.c b/drivers/gpio/pca9539.c new file mode 100644 index 0000000..f27ee1b --- /dev/null +++ b/drivers/gpio/pca9539.c @@ -0,0 +1,92 @@ +/* + * Support for NXP's 16-bit I2C gpio expander + * + * Copyright (c) 2010 Allied Telesis Labs + * + * Based on pca953x.c + * Copyright 2008 Extreme Engineering Solutions, Inc. + * + * This file is licensed under the terms of the GNU General Public License + * version 2. This program is licensed "as is" without any warranty of any + * kind, whether express or implied. + */ +#include +#include +#include + +static int pca9539_reg_write(uint8_t chip, uint addr, uint mask, uint data) +{ + uint8_t val; + + if (i2c_read(chip, addr, 1, &val, 1)) + return -1; + + val &= ~mask; + val |= data; + + return i2c_write(chip, addr, 1, &val, 1); +} + +/* + * Set output value of IO pins in 'mask' to corresponding value in 'data' + * 0 = low, 1 = high + */ +int pca9539_set_val(uint8_t chip, uint mask, uint data) +{ + uint addr = PCA9539_OUT0; + if (mask & 0xff00) { + addr = PCA9539_OUT1; + mask = mask >> 8; + data = data >> 8; + } + return pca9539_reg_write(chip, addr, mask, data); +} + +/* + * Set read polarity of IO pins in 'mask' to corresponding value in 'data' + * 0 = read pin value, 1 = read inverted pin value + */ +int pca9539_set_pol(uint8_t chip, uint mask, uint data) +{ + uint addr = PCA9539_POL0; + if (mask & 0xff00) { + addr = PCA9539_POL1; + mask = mask >> 8; + data = data >> 8; + } + return pca9539_reg_write(chip, addr, mask, data); +} + +/* + * Set direction of IO pins in 'mask' to corresponding value in 'data' + * 0 = output, 1 = input + */ +int pca9539_set_dir(uint8_t chip, uint mask, uint data) +{ + uint addr = PCA9539_CONF0; + if (mask & 0xff00) { + addr = PCA9539_CONF1; + mask = mask >> 8; + data = data >> 8; + } + return pca9539_reg_write(chip, addr, mask, data); +} + +/* + * Read current logic level of all IO pins + */ +int pca9539_get_val(uint8_t chip) +{ + uint8_t val; + int value; + + if (i2c_read(chip, PCA9539_IN0, 1, &val, 1)) + return -1; + value = val; + + if (i2c_read(chip, PCA9539_IN1, 1, &val, 1)) + return -1; + value |= val << 8; + + return value; +} diff --git a/include/pca9539.h b/include/pca9539.h new file mode 100644 index 0000000..83793b1 --- /dev/null +++ b/include/pca9539.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2010 Allied Telesis Labs + * + * Based on pca953x.h + * Copyright 2008 Extreme Engineering Solutions, Inc. + * + * This file is licensed under the terms of the GNU General Public License + * version 2. This program is licensed "as is" without any warranty of any + * kind, whether express or implied. + */ + +#ifndef __PCA9539_H_ +#define __PCA9539_H_ + +#define PCA9539_IN0 0x00 +#define PCA9539_IN1 0x01 +#define PCA9539_OUT0 0x02 +#define PCA9539_OUT1 0x03 +#define PCA9539_POL0 0x04 +#define PCA9539_POL1 0x05 +#define PCA9539_CONF0 0x06 +#define PCA9539_CONF1 0x07 + +#define PCA9539_OUT_LOW 0 +#define PCA9539_OUT_HIGH 1 +#define PCA9539_POL_NORMAL 0 +#define PCA9539_POL_INVERT 1 +#define PCA9539_DIR_OUT 0 +#define PCA9539_DIR_IN 1 + +int pca9539_set_val(u8 chip, uint mask, uint data); +int pca9539_set_pol(u8 chip, uint mask, uint data); +int pca9539_set_dir(u8 chip, uint mask, uint data); +int pca9539_get_val(u8 chip); + +#endif /* __PCA9539_H_ */