blob: fe6d2c668070adcd111631ed69418ecd7a0b676e [file] [log] [blame]
Dirk Eibach486cad02011-10-03 23:13:51 +00001/*
2 * (C) Copyright 2011
3 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Driver for NXP's pca9698 40 bit I2C gpio expander
26 */
27
28#include <common.h>
29#include <i2c.h>
Dirk Eibach042f9f12011-10-31 09:39:12 +010030#include <asm/errno.h>
Dirk Eibach486cad02011-10-03 23:13:51 +000031#include <pca9698.h>
32
33/*
34 * The pca9698 registers
35 */
36
37#define PCA9698_REG_INPUT 0x00
38#define PCA9698_REG_OUTPUT 0x08
39#define PCA9698_REG_POLARITY 0x10
40#define PCA9698_REG_CONFIG 0x18
41
42#define PCA9698_BUFFER_SIZE 5
Dirk Eibach042f9f12011-10-31 09:39:12 +010043#define PCA9698_GPIO_COUNT 40
Dirk Eibach486cad02011-10-03 23:13:51 +000044
Dirk Eibach042f9f12011-10-31 09:39:12 +010045static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibach486cad02011-10-03 23:13:51 +000046{
47 u8 command = offset | 0x80; /* autoincrement */
48
Dirk Eibach042f9f12011-10-31 09:39:12 +010049 return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibach486cad02011-10-03 23:13:51 +000050}
51
Dirk Eibach042f9f12011-10-31 09:39:12 +010052static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibach486cad02011-10-03 23:13:51 +000053{
54 u8 command = offset | 0x80; /* autoincrement */
55
Dirk Eibach042f9f12011-10-31 09:39:12 +010056 return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibach486cad02011-10-03 23:13:51 +000057}
58
59static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
60{
61 unsigned byte = gpio / 8;
62 unsigned bit = gpio % 8;
63
64 if (value)
65 buffer[byte] |= (1 << bit);
66 else
67 buffer[byte] &= ~(1 << bit);
68}
69
Dirk Eibach042f9f12011-10-31 09:39:12 +010070int pca9698_request(unsigned gpio, const char *label)
71{
72 if (gpio >= PCA9698_GPIO_COUNT)
73 return -EINVAL;
74
75 return 0;
76}
77
78void pca9698_free(unsigned gpio)
79{
80}
81
82int pca9698_direction_input(u8 addr, unsigned gpio)
Dirk Eibach486cad02011-10-03 23:13:51 +000083{
84 u8 data[PCA9698_BUFFER_SIZE];
85 int res;
86
Dirk Eibach042f9f12011-10-31 09:39:12 +010087 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000088 if (res)
89 return res;
90
Dirk Eibach042f9f12011-10-31 09:39:12 +010091 pca9698_set_bit(gpio, data, 1);
92
93 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000094}
95
Dirk Eibach042f9f12011-10-31 09:39:12 +010096int pca9698_direction_output(u8 addr, unsigned gpio, int value)
Dirk Eibach486cad02011-10-03 23:13:51 +000097{
98 u8 data[PCA9698_BUFFER_SIZE];
99 int res;
100
Dirk Eibach042f9f12011-10-31 09:39:12 +0100101 res = pca9698_set_value(addr, gpio, value);
Dirk Eibach486cad02011-10-03 23:13:51 +0000102 if (res)
103 return res;
104
Dirk Eibach042f9f12011-10-31 09:39:12 +0100105 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
106 if (res)
107 return res;
108
109 pca9698_set_bit(gpio, data, 0);
110
111 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000112}
113
Dirk Eibach042f9f12011-10-31 09:39:12 +0100114int pca9698_get_value(u8 addr, unsigned gpio)
Dirk Eibach486cad02011-10-03 23:13:51 +0000115{
Dirk Eibach042f9f12011-10-31 09:39:12 +0100116 unsigned config_byte = gpio / 8;
117 unsigned config_bit = gpio % 8;
Dirk Eibach486cad02011-10-03 23:13:51 +0000118 unsigned value;
119 u8 data[PCA9698_BUFFER_SIZE];
120 int res;
121
Dirk Eibach042f9f12011-10-31 09:39:12 +0100122 res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000123 if (res)
124 return -1;
125
126 value = data[config_byte] & (1 << config_bit);
127
128 return !!value;
129}
130
Dirk Eibach042f9f12011-10-31 09:39:12 +0100131int pca9698_set_value(u8 addr, unsigned gpio, int value)
Dirk Eibach486cad02011-10-03 23:13:51 +0000132{
133 u8 data[PCA9698_BUFFER_SIZE];
134 int res;
135
Dirk Eibach042f9f12011-10-31 09:39:12 +0100136 res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000137 if (res)
138 return res;
139
Dirk Eibach042f9f12011-10-31 09:39:12 +0100140 pca9698_set_bit(gpio, data, value);
141
142 return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000143}