blob: ab0c4c1b971f9d673e345603a7641673ac72b7ae [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Eibach486cad02011-10-03 23:13:51 +00002/*
3 * (C) Copyright 2011
Mario Sixd38826a2018-03-06 08:04:58 +01004 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
Dirk Eibach486cad02011-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 Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Dirk Eibach486cad02011-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 Eibach042f9f12011-10-31 09:39:12 +010026#define PCA9698_GPIO_COUNT 40
Dirk Eibach486cad02011-10-03 23:13:51 +000027
Dirk Eibach042f9f12011-10-31 09:39:12 +010028static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibach486cad02011-10-03 23:13:51 +000029{
30 u8 command = offset | 0x80; /* autoincrement */
31
Dirk Eibach042f9f12011-10-31 09:39:12 +010032 return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibach486cad02011-10-03 23:13:51 +000033}
34
Dirk Eibach042f9f12011-10-31 09:39:12 +010035static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
Dirk Eibach486cad02011-10-03 23:13:51 +000036{
37 u8 command = offset | 0x80; /* autoincrement */
38
Dirk Eibach042f9f12011-10-31 09:39:12 +010039 return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
Dirk Eibach486cad02011-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 Eibach042f9f12011-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 Eibach486cad02011-10-03 23:13:51 +000066{
67 u8 data[PCA9698_BUFFER_SIZE];
68 int res;
69
Dirk Eibach042f9f12011-10-31 09:39:12 +010070 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000071 if (res)
72 return res;
73
Dirk Eibach042f9f12011-10-31 09:39:12 +010074 pca9698_set_bit(gpio, data, 1);
75
76 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000077}
78
Dirk Eibach042f9f12011-10-31 09:39:12 +010079int pca9698_direction_output(u8 addr, unsigned gpio, int value)
Dirk Eibach486cad02011-10-03 23:13:51 +000080{
81 u8 data[PCA9698_BUFFER_SIZE];
82 int res;
83
Dirk Eibach042f9f12011-10-31 09:39:12 +010084 res = pca9698_set_value(addr, gpio, value);
Dirk Eibach486cad02011-10-03 23:13:51 +000085 if (res)
86 return res;
87
Dirk Eibach042f9f12011-10-31 09:39:12 +010088 res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
89 if (res)
90 return res;
91
92 pca9698_set_bit(gpio, data, 0);
93
94 return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
Dirk Eibach486cad02011-10-03 23:13:51 +000095}
96
Dirk Eibach042f9f12011-10-31 09:39:12 +010097int pca9698_get_value(u8 addr, unsigned gpio)
Dirk Eibach486cad02011-10-03 23:13:51 +000098{
Dirk Eibach042f9f12011-10-31 09:39:12 +010099 unsigned config_byte = gpio / 8;
100 unsigned config_bit = gpio % 8;
Dirk Eibach486cad02011-10-03 23:13:51 +0000101 unsigned value;
102 u8 data[PCA9698_BUFFER_SIZE];
103 int res;
104
Dirk Eibach042f9f12011-10-31 09:39:12 +0100105 res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
Dirk Eibach486cad02011-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 Eibach042f9f12011-10-31 09:39:12 +0100114int pca9698_set_value(u8 addr, unsigned gpio, int value)
Dirk Eibach486cad02011-10-03 23:13:51 +0000115{
116 u8 data[PCA9698_BUFFER_SIZE];
117 int res;
118
Dirk Eibach042f9f12011-10-31 09:39:12 +0100119 res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000120 if (res)
121 return res;
122
Dirk Eibach042f9f12011-10-31 09:39:12 +0100123 pca9698_set_bit(gpio, data, value);
124
125 return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);
Dirk Eibach486cad02011-10-03 23:13:51 +0000126}