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
33 * The pca9698 registers
36 #define PCA9698_REG_INPUT 0x00
37 #define PCA9698_REG_OUTPUT 0x08
38 #define PCA9698_REG_POLARITY 0x10
39 #define PCA9698_REG_CONFIG 0x18
41 #define PCA9698_BUFFER_SIZE 5
43 static int pca9698_read40(u8 chip, u8 offset, u8 *buffer)
45 u8 command = offset | 0x80; /* autoincrement */
47 return i2c_read(chip, command, 1, buffer, PCA9698_BUFFER_SIZE);
50 static int pca9698_write40(u8 chip, u8 offset, u8 *buffer)
52 u8 command = offset | 0x80; /* autoincrement */
54 return i2c_write(chip, command, 1, buffer, PCA9698_BUFFER_SIZE);
57 static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
59 unsigned byte = gpio / 8;
60 unsigned bit = gpio % 8;
63 buffer[byte] |= (1 << bit);
65 buffer[byte] &= ~(1 << bit);
68 int pca9698_direction_input(u8 chip, unsigned offset)
70 u8 data[PCA9698_BUFFER_SIZE];
73 res = pca9698_read40(chip, PCA9698_REG_CONFIG, data);
77 pca9698_set_bit(offset, data, 1);
78 return pca9698_write40(chip, PCA9698_REG_CONFIG, data);
81 int pca9698_direction_output(u8 chip, unsigned offset)
83 u8 data[PCA9698_BUFFER_SIZE];
86 res = pca9698_read40(chip, PCA9698_REG_CONFIG, data);
90 pca9698_set_bit(offset, data, 0);
91 return pca9698_write40(chip, PCA9698_REG_CONFIG, data);
94 int pca9698_get_input(u8 chip, unsigned offset)
96 unsigned config_byte = offset / 8;
97 unsigned config_bit = offset % 8;
99 u8 data[PCA9698_BUFFER_SIZE];
102 res = pca9698_read40(chip, PCA9698_REG_INPUT, data);
106 value = data[config_byte] & (1 << config_bit);
111 int pca9698_set_output(u8 chip, unsigned offset, int value)
113 u8 data[PCA9698_BUFFER_SIZE];
116 res = pca9698_read40(chip, PCA9698_REG_OUTPUT, data);
120 memset(data, sizeof(data), 0);
121 pca9698_set_bit(offset, data, value);
122 return pca9698_write40(chip, PCA9698_REG_OUTPUT, data);