summaryrefslogtreecommitdiff
path: root/drivers/gpio/pca9698.c
blob: b946efa7a98379e6de82e44cb7b299f343cc90af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 <common.h>
#include <i2c.h>
#include <pca9698.h>

/*
 * 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, sizeof(data), 0);
	pca9698_set_bit(offset, data, value);
	return pca9698_write40(chip, PCA9698_REG_OUTPUT, data);
}