blob: d2663f7331aa5fa78bd25e6d11f4a4cb61a1563c [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Dirk Eibach486cad02011-10-03 23:13:51 +00006 */
7
8/*
9 * Driver for NXP's pca9698 40 bit I2C gpio expander
10 */
11
12#include <common.h>
13#include <i2c.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090014#include <linux/errno.h>
Dirk Eibach486cad02011-10-03 23:13:51 +000015#include <pca9698.h>
16
17/*
18 * The pca9698 registers
19 */
20
21#define PCA9698_REG_INPUT 0x00
22#define PCA9698_REG_OUTPUT 0x08
23#define PCA9698_REG_POLARITY 0x10
24#define PCA9698_REG_CONFIG 0x18
25
26#define PCA9698_BUFFER_SIZE 5
Dirk Eibach042f9f12011-10-31 09:39:12 +010027#define PCA9698_GPIO_COUNT 40
Dirk Eibach486cad02011-10-03 23:13:51 +000028
Dirk Eibach042f9f12011-10-31 09:39:12 +010029static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibach486cad02011-10-03 23:13:51 +000030{
31 u8 command = offset | 0x80; /* autoincrement */
32
Dirk Eibach042f9f12011-10-31 09:39:12 +010033 return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibach486cad02011-10-03 23:13:51 +000034}
35
Dirk Eibach042f9f12011-10-31 09:39:12 +010036static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibach486cad02011-10-03 23:13:51 +000037{
38 u8 command = offset | 0x80; /* autoincrement */
39
Dirk Eibach042f9f12011-10-31 09:39:12 +010040 return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibach486cad02011-10-03 23:13:51 +000041}
42
43static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
44{
45 unsigned byte = gpio / 8;
46 unsigned bit = gpio % 8;
47
48 if (value)
49 buffer[byte] |= (1 << bit);
50 else
51 buffer[byte] &= ~(1 << bit);
52}
53
Dirk Eibach042f9f12011-10-31 09:39:12 +010054int pca9698_request(unsigned gpio, const char *label)
55{
56 if (gpio >= PCA9698_GPIO_COUNT)
57 return -EINVAL;
58
59 return 0;
60}
61
62void pca9698_free(unsigned gpio)
63{
64}
65
66int pca9698_direction_input(u8 addr, unsigned gpio)
Dirk Eibach486cad02011-10-03 23:13:51 +000067{
68 u8 data[PCA9698_BUFFER_SIZE];
69 int res;
70
Dirk Eibach042f9f12011-10-31 09:39:12 +010071 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000072 if (res)
73 return res;
74
Dirk Eibach042f9f12011-10-31 09:39:12 +010075 pca9698_set_bit(gpio, data, 1);
76
77 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000078}
79
Dirk Eibach042f9f12011-10-31 09:39:12 +010080int pca9698_direction_output(u8 addr, unsigned gpio, int value)
Dirk Eibach486cad02011-10-03 23:13:51 +000081{
82 u8 data[PCA9698_BUFFER_SIZE];
83 int res;
84
Dirk Eibach042f9f12011-10-31 09:39:12 +010085 res = pca9698_set_value(addr, gpio, value);
Dirk Eibach486cad02011-10-03 23:13:51 +000086 if (res)
87 return res;
88
Dirk Eibach042f9f12011-10-31 09:39:12 +010089 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
90 if (res)
91 return res;
92
93 pca9698_set_bit(gpio, data, 0);
94
95 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000096}
97
Dirk Eibach042f9f12011-10-31 09:39:12 +010098int pca9698_get_value(u8 addr, unsigned gpio)
Dirk Eibach486cad02011-10-03 23:13:51 +000099{
Dirk Eibach042f9f12011-10-31 09:39:12 +0100100 unsigned config_byte = gpio / 8;
101 unsigned config_bit = gpio % 8;
Dirk Eibach486cad02011-10-03 23:13:51 +0000102 unsigned value;
103 u8 data[PCA9698_BUFFER_SIZE];
104 int res;
105
Dirk Eibach042f9f12011-10-31 09:39:12 +0100106 res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000107 if (res)
108 return -1;
109
110 value = data[config_byte] & (1 << config_bit);
111
112 return !!value;
113}
114
Dirk Eibach042f9f12011-10-31 09:39:12 +0100115int pca9698_set_value(u8 addr, unsigned gpio, int value)
Dirk Eibach486cad02011-10-03 23:13:51 +0000116{
117 u8 data[PCA9698_BUFFER_SIZE];
118 int res;
119
Dirk Eibach042f9f12011-10-31 09:39:12 +0100120 res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000121 if (res)
122 return res;
123
Dirk Eibach042f9f12011-10-31 09:39:12 +0100124 pca9698_set_bit(gpio, data, value);
125
126 return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000127}