blob: 3995c496aeed67f405e8df5d572e81ce41a60884 [file] [log] [blame]
Dirk Eibachc3213b82011-10-03 23:13:51 +00001/*
2 * (C) Copyright 2011
Mario Sixb4893582018-03-06 08:04:58 +01003 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
Dirk Eibachc3213b82011-10-03 23:13:51 +00004 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Dirk Eibachc3213b82011-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 Yamada56a931c2016-09-21 11:28:55 +090014#include <linux/errno.h>
Dirk Eibachc3213b82011-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 Eibachbae3a222011-10-31 09:39:12 +010027#define PCA9698_GPIO_COUNT 40
Dirk Eibachc3213b82011-10-03 23:13:51 +000028
Dirk Eibachbae3a222011-10-31 09:39:12 +010029static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibachc3213b82011-10-03 23:13:51 +000030{
31 u8 command = offset | 0x80; /* autoincrement */
32
Dirk Eibachbae3a222011-10-31 09:39:12 +010033 return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibachc3213b82011-10-03 23:13:51 +000034}
35
Dirk Eibachbae3a222011-10-31 09:39:12 +010036static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibachc3213b82011-10-03 23:13:51 +000037{
38 u8 command = offset | 0x80; /* autoincrement */
39
Dirk Eibachbae3a222011-10-31 09:39:12 +010040 return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibachc3213b82011-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 Eibachbae3a222011-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 Eibachc3213b82011-10-03 23:13:51 +000067{
68 u8 data[PCA9698_BUFFER_SIZE];
69 int res;
70
Dirk Eibachbae3a222011-10-31 09:39:12 +010071 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000072 if (res)
73 return res;
74
Dirk Eibachbae3a222011-10-31 09:39:12 +010075 pca9698_set_bit(gpio, data, 1);
76
77 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000078}
79
Dirk Eibachbae3a222011-10-31 09:39:12 +010080int pca9698_direction_output(u8 addr, unsigned gpio, int value)
Dirk Eibachc3213b82011-10-03 23:13:51 +000081{
82 u8 data[PCA9698_BUFFER_SIZE];
83 int res;
84
Dirk Eibachbae3a222011-10-31 09:39:12 +010085 res = pca9698_set_value(addr, gpio, value);
86 if (res)
87 return res;
88
89 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000090 if (res)
91 return res;
92
Dirk Eibachbae3a222011-10-31 09:39:12 +010093 pca9698_set_bit(gpio, data, 0);
94
95 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000096}
97
Dirk Eibachbae3a222011-10-31 09:39:12 +010098int pca9698_get_value(u8 addr, unsigned gpio)
Dirk Eibachc3213b82011-10-03 23:13:51 +000099{
Dirk Eibachbae3a222011-10-31 09:39:12 +0100100 unsigned config_byte = gpio / 8;
101 unsigned config_bit = gpio % 8;
Dirk Eibachc3213b82011-10-03 23:13:51 +0000102 unsigned value;
103 u8 data[PCA9698_BUFFER_SIZE];
104 int res;
105
Dirk Eibachbae3a222011-10-31 09:39:12 +0100106 res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
Dirk Eibachc3213b82011-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 Eibachbae3a222011-10-31 09:39:12 +0100115int pca9698_set_value(u8 addr, unsigned gpio, int value)
Dirk Eibachc3213b82011-10-03 23:13:51 +0000116{
117 u8 data[PCA9698_BUFFER_SIZE];
118 int res;
119
Dirk Eibachbae3a222011-10-31 09:39:12 +0100120 res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +0000121 if (res)
122 return res;
123
Dirk Eibachbae3a222011-10-31 09:39:12 +0100124 pca9698_set_bit(gpio, data, value);
125
126 return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +0000127}