3 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * Driver for NXP's pca9698 40 bit I2C gpio expander
30 #include <asm/errno.h>
34 * The pca9698 registers
37 #define PCA9698_REG_INPUT 0x00
38 #define PCA9698_REG_OUTPUT 0x08
39 #define PCA9698_REG_POLARITY 0x10
40 #define PCA9698_REG_CONFIG 0x18
42 #define PCA9698_BUFFER_SIZE 5
43 #define PCA9698_GPIO_COUNT 40
45 static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
47 u8 command = offset | 0x80; /* autoincrement */
49 return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
52 static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
54 u8 command = offset | 0x80; /* autoincrement */
56 return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
59 static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
61 unsigned byte = gpio / 8;
62 unsigned bit = gpio % 8;
65 buffer[byte] |= (1 << bit);
67 buffer[byte] &= ~(1 << bit);
70 int pca9698_request(unsigned gpio, const char *label)
72 if (gpio >= PCA9698_GPIO_COUNT)
78 void pca9698_free(unsigned gpio)
82 int pca9698_direction_input(u8 addr, unsigned gpio)
84 u8 data[PCA9698_BUFFER_SIZE];
87 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
91 pca9698_set_bit(gpio, data, 1);
93 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
96 int pca9698_direction_output(u8 addr, unsigned gpio, int value)
98 u8 data[PCA9698_BUFFER_SIZE];
101 res = pca9698_set_value(addr, gpio, value);
105 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
109 pca9698_set_bit(gpio, data, 0);
111 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
114 int pca9698_get_value(u8 addr, unsigned gpio)
116 unsigned config_byte = gpio / 8;
117 unsigned config_bit = gpio % 8;
119 u8 data[PCA9698_BUFFER_SIZE];
122 res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
126 value = data[config_byte] & (1 << config_bit);
131 int pca9698_set_value(u8 addr, unsigned gpio, int value)
133 u8 data[PCA9698_BUFFER_SIZE];
136 res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
140 pca9698_set_bit(gpio, data, value);
142 return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);