blob: ab0c4c1b971f9d673e345603a7641673ac72b7ae [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Eibachc3213b82011-10-03 23:13:51 +00002/*
3 * (C) Copyright 2011
Mario Sixb4893582018-03-06 08:04:58 +01004 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
Dirk Eibachc3213b82011-10-03 23:13:51 +00005 */
6
7/*
8 * Driver for NXP's pca9698 40 bit I2C gpio expander
9 */
10
11#include <common.h>
12#include <i2c.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Dirk Eibachc3213b82011-10-03 23:13:51 +000014#include <pca9698.h>
15
16/*
17 * The pca9698 registers
18 */
19
20#define PCA9698_REG_INPUT 0x00
21#define PCA9698_REG_OUTPUT 0x08
22#define PCA9698_REG_POLARITY 0x10
23#define PCA9698_REG_CONFIG 0x18
24
25#define PCA9698_BUFFER_SIZE 5
Dirk Eibachbae3a222011-10-31 09:39:12 +010026#define PCA9698_GPIO_COUNT 40
Dirk Eibachc3213b82011-10-03 23:13:51 +000027
Dirk Eibachbae3a222011-10-31 09:39:12 +010028static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibachc3213b82011-10-03 23:13:51 +000029{
30 u8 command = offset | 0x80; /* autoincrement */
31
Dirk Eibachbae3a222011-10-31 09:39:12 +010032 return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibachc3213b82011-10-03 23:13:51 +000033}
34
Dirk Eibachbae3a222011-10-31 09:39:12 +010035static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibachc3213b82011-10-03 23:13:51 +000036{
37 u8 command = offset | 0x80; /* autoincrement */
38
Dirk Eibachbae3a222011-10-31 09:39:12 +010039 return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibachc3213b82011-10-03 23:13:51 +000040}
41
42static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
43{
44 unsigned byte = gpio / 8;
45 unsigned bit = gpio % 8;
46
47 if (value)
48 buffer[byte] |= (1 << bit);
49 else
50 buffer[byte] &= ~(1 << bit);
51}
52
Dirk Eibachbae3a222011-10-31 09:39:12 +010053int pca9698_request(unsigned gpio, const char *label)
54{
55 if (gpio >= PCA9698_GPIO_COUNT)
56 return -EINVAL;
57
58 return 0;
59}
60
61void pca9698_free(unsigned gpio)
62{
63}
64
65int pca9698_direction_input(u8 addr, unsigned gpio)
Dirk Eibachc3213b82011-10-03 23:13:51 +000066{
67 u8 data[PCA9698_BUFFER_SIZE];
68 int res;
69
Dirk Eibachbae3a222011-10-31 09:39:12 +010070 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000071 if (res)
72 return res;
73
Dirk Eibachbae3a222011-10-31 09:39:12 +010074 pca9698_set_bit(gpio, data, 1);
75
76 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000077}
78
Dirk Eibachbae3a222011-10-31 09:39:12 +010079int pca9698_direction_output(u8 addr, unsigned gpio, int value)
Dirk Eibachc3213b82011-10-03 23:13:51 +000080{
81 u8 data[PCA9698_BUFFER_SIZE];
82 int res;
83
Dirk Eibachbae3a222011-10-31 09:39:12 +010084 res = pca9698_set_value(addr, gpio, value);
85 if (res)
86 return res;
87
88 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000089 if (res)
90 return res;
91
Dirk Eibachbae3a222011-10-31 09:39:12 +010092 pca9698_set_bit(gpio, data, 0);
93
94 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +000095}
96
Dirk Eibachbae3a222011-10-31 09:39:12 +010097int pca9698_get_value(u8 addr, unsigned gpio)
Dirk Eibachc3213b82011-10-03 23:13:51 +000098{
Dirk Eibachbae3a222011-10-31 09:39:12 +010099 unsigned config_byte = gpio / 8;
100 unsigned config_bit = gpio % 8;
Dirk Eibachc3213b82011-10-03 23:13:51 +0000101 unsigned value;
102 u8 data[PCA9698_BUFFER_SIZE];
103 int res;
104
Dirk Eibachbae3a222011-10-31 09:39:12 +0100105 res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +0000106 if (res)
107 return -1;
108
109 value = data[config_byte] & (1 << config_bit);
110
111 return !!value;
112}
113
Dirk Eibachbae3a222011-10-31 09:39:12 +0100114int pca9698_set_value(u8 addr, unsigned gpio, int value)
Dirk Eibachc3213b82011-10-03 23:13:51 +0000115{
116 u8 data[PCA9698_BUFFER_SIZE];
117 int res;
118
Dirk Eibachbae3a222011-10-31 09:39:12 +0100119 res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +0000120 if (res)
121 return res;
122
Dirk Eibachbae3a222011-10-31 09:39:12 +0100123 pca9698_set_bit(gpio, data, value);
124
125 return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibachc3213b82011-10-03 23:13:51 +0000126}