From patchwork Wed Oct 19 06:34:00 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dirk Eibach X-Patchwork-Id: 120560 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 32E75B7105 for ; Wed, 19 Oct 2011 17:30:14 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BBD9328B90; Wed, 19 Oct 2011 08:30:11 +0200 (CEST) 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 DUrUsqaqn0hm; Wed, 19 Oct 2011 08:30:08 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 2908928B64; Wed, 19 Oct 2011 08:30:08 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id EE88628B64 for ; Wed, 19 Oct 2011 08:30:03 +0200 (CEST) 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 hDIxNjXtgNK6 for ; Wed, 19 Oct 2011 08:30:01 +0200 (CEST) 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 sputnik.urmx.de (sputnik.urmx.de [88.198.51.18]) by theia.denx.de (Postfix) with ESMTP id D115328B5D for ; Wed, 19 Oct 2011 08:30:00 +0200 (CEST) Received: from mailrelay.gdsys.de (unknown [217.6.197.20]) by sputnik.urmx.de (Postfix) with ESMTP id DD34360000A4 for ; Wed, 19 Oct 2011 08:29:59 +0200 (CEST) Received: from mailsgw.gdsys.de (localhost [127.0.0.1]) by mailsgw.gdsys.de (Postfix) with ESMTP id 0291C6699D for ; Wed, 19 Oct 2011 08:30:00 +0200 (CEST) From: Dirk Eibach To: u-boot@lists.denx.de Date: Wed, 19 Oct 2011 08:34:00 +0200 Message-Id: <1319006040-25380-1-git-send-email-eibach@gdsys.de> X-SafeGuard_MailGateway: Version: 5.60.3.11066 SGMG Date: 20111019062959Z X-AntiVirus: checked by AntiVir MailGate (version: 2.0.2-5; AVE: 7.9.6.84; VDF: 7.11.16.60; host: mailrelay) Cc: sr@denx.de Subject: [U-Boot] [PATCH v2] gpio: Add PCA9698 40-bit I2C I/O port 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: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Signed-off-by: Dirk Eibach --- Changes for v2: - fixed parameters for memset() in pca9698_set_output() drivers/gpio/Makefile | 1 + drivers/gpio/pca9698.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ include/pca9698.h | 9 ++++ 3 files changed, 133 insertions(+), 0 deletions(-) create mode 100644 drivers/gpio/pca9698.c create mode 100644 include/pca9698.h diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 62ec97d..38a62c3 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -30,6 +30,7 @@ COBJS-$(CONFIG_KIRKWOOD_GPIO) += kw_gpio.o COBJS-$(CONFIG_MARVELL_MFP) += mvmfp.o COBJS-$(CONFIG_MXC_GPIO) += mxc_gpio.o COBJS-$(CONFIG_PCA953X) += pca953x.o +COBJS-$(CONFIG_PCA9698) += pca9698.o COBJS-$(CONFIG_S5P) += s5p_gpio.o COBJS-$(CONFIG_TEGRA2_GPIO) += tegra2_gpio.o COBJS-$(CONFIG_DA8XX_GPIO) += da8xx_gpio.o diff --git a/drivers/gpio/pca9698.c b/drivers/gpio/pca9698.c new file mode 100644 index 0000000..a98cd0e --- /dev/null +++ b/drivers/gpio/pca9698.c @@ -0,0 +1,123 @@ +/* + * (C) Copyright 2011 + * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +/* + * Driver for NXP's pca9698 40 bit I2C gpio expander + */ + +#include +#include +#include + +/* + * The pca9698 registers + */ + +#define PCA9698_REG_INPUT 0x00 +#define PCA9698_REG_OUTPUT 0x08 +#define PCA9698_REG_POLARITY 0x10 +#define PCA9698_REG_CONFIG 0x18 + +#define PCA9698_BUFFER_SIZE 5 + +static int pca9698_read40(u8 chip, u8 offset, u8 *buffer) +{ + u8 command = offset | 0x80; /* autoincrement */ + + return i2c_read(chip, command, 1, buffer, PCA9698_BUFFER_SIZE); +} + +static int pca9698_write40(u8 chip, u8 offset, u8 *buffer) +{ + u8 command = offset | 0x80; /* autoincrement */ + + return i2c_write(chip, command, 1, buffer, PCA9698_BUFFER_SIZE); +} + +static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value) +{ + unsigned byte = gpio / 8; + unsigned bit = gpio % 8; + + if (value) + buffer[byte] |= (1 << bit); + else + buffer[byte] &= ~(1 << bit); +} + +int pca9698_direction_input(u8 chip, unsigned offset) +{ + u8 data[PCA9698_BUFFER_SIZE]; + int res; + + res = pca9698_read40(chip, PCA9698_REG_CONFIG, data); + if (res) + return res; + + pca9698_set_bit(offset, data, 1); + return pca9698_write40(chip, PCA9698_REG_CONFIG, data); +} + +int pca9698_direction_output(u8 chip, unsigned offset) +{ + u8 data[PCA9698_BUFFER_SIZE]; + int res; + + res = pca9698_read40(chip, PCA9698_REG_CONFIG, data); + if (res) + return res; + + pca9698_set_bit(offset, data, 0); + return pca9698_write40(chip, PCA9698_REG_CONFIG, data); +} + +int pca9698_get_input(u8 chip, unsigned offset) +{ + unsigned config_byte = offset / 8; + unsigned config_bit = offset % 8; + unsigned value; + u8 data[PCA9698_BUFFER_SIZE]; + int res; + + res = pca9698_read40(chip, PCA9698_REG_INPUT, data); + if (res) + return -1; + + value = data[config_byte] & (1 << config_bit); + + return !!value; +} + +int pca9698_set_output(u8 chip, unsigned offset, int value) +{ + u8 data[PCA9698_BUFFER_SIZE]; + int res; + + res = pca9698_read40(chip, PCA9698_REG_OUTPUT, data); + if (res) + return res; + + memset(data, 0, sizeof(data)); + pca9698_set_bit(offset, data, value); + return pca9698_write40(chip, PCA9698_REG_OUTPUT, data); +} diff --git a/include/pca9698.h b/include/pca9698.h new file mode 100644 index 0000000..2506088 --- /dev/null +++ b/include/pca9698.h @@ -0,0 +1,9 @@ +#ifndef __PCA9698_H_ +#define __PCA9698_H_ + +int pca9698_direction_input(u8 chip, unsigned offset); +int pca9698_direction_output(u8 chip, unsigned offset); +int pca9698_get_input(u8 chip, unsigned offset); +int pca9698_set_output(u8 chip, unsigned offset, int value); + +#endif /* __PCA9698_H_ */