-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
gpio_cy8c95xx.c
314 lines (260 loc) · 7.6 KB
/
gpio_cy8c95xx.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2021 Synopsys
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT cypress_cy8c95xx_gpio_port
#include <errno.h>
#include <kernel.h>
#include <device.h>
#include <init.h>
#include <drivers/gpio.h>
#include <drivers/i2c.h>
#include <sys/byteorder.h>
#include <sys/util.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(cy8c95xx, CONFIG_GPIO_LOG_LEVEL);
#include "gpio_utils.h"
/** Cache of the output configuration and data of the pins. */
struct cy8c95xx_pin_state {
uint8_t dir;
uint8_t data_out;
uint8_t pull_up;
uint8_t pull_down;
};
/** Runtime driver data */
struct cy8c95xx_drv_data {
/* gpio_driver_data needs to be first */
struct gpio_driver_data common;
struct cy8c95xx_pin_state pin_state;
struct k_sem *lock;
};
/** Configuration data */
struct cy8c95xx_config {
/* gpio_driver_config needs to be first */
struct gpio_driver_config common;
const struct device *i2c_master;
uint16_t i2c_slave_addr;
uint8_t port_num;
};
#define CY8C95XX_REG_INPUT_DATA0 0x00
#define CY8C95XX_REG_OUTPUT_DATA0 0x08
#define CY8C95XX_REG_PORT_SELECT 0x18
#define CY8C95XX_REG_DIR 0x1C
#define CY8C95XX_REG_PULL_UP 0x1D
#define CY8C95XX_REG_PULL_DOWN 0x1E
#define CY8C95XX_REG_ID 0x2E
static int write_pin_state(const struct cy8c95xx_config *cfg,
struct cy8c95xx_drv_data *drv_data,
struct cy8c95xx_pin_state *pins)
{
int rc;
rc = i2c_reg_write_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_OUTPUT_DATA0 + cfg->port_num, pins->data_out);
if (rc) {
return rc;
}
rc = i2c_reg_write_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_PORT_SELECT, cfg->port_num);
if (rc) {
return rc;
}
rc = i2c_reg_write_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_DIR, pins->dir);
if (rc) {
return rc;
}
rc = i2c_reg_write_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_PULL_UP, pins->pull_up);
if (rc) {
return rc;
}
rc = i2c_reg_write_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_PULL_DOWN, pins->pull_down);
return rc;
}
static int cy8c95xx_config(const struct device *dev,
gpio_pin_t pin,
gpio_flags_t flags)
{
const struct cy8c95xx_config *cfg = dev->config;
struct cy8c95xx_drv_data *drv_data = dev->data;
struct cy8c95xx_pin_state *pins = &drv_data->pin_state;
int rc = 0;
uint32_t io_flags;
/* Can't do I2C bus operations from an ISR */
if (k_is_in_isr()) {
return -EWOULDBLOCK;
}
/* Strengths not implemented */
if ((flags & (GPIO_DS_ALT_LOW | GPIO_DS_ALT_HIGH)) != 0) {
return -ENOTSUP;
}
/* Open-drain not implemented */
if ((flags & GPIO_SINGLE_ENDED) != 0U) {
return -ENOTSUP;
}
WRITE_BIT(pins->pull_up, pin, (flags & GPIO_PULL_UP) != 0U);
WRITE_BIT(pins->pull_down, pin, (flags & GPIO_PULL_DOWN) != 0U);
/* Disconnected pin not implemented*/
io_flags = flags & (GPIO_INPUT | GPIO_OUTPUT);
if (io_flags == GPIO_DISCONNECTED) {
return -ENOTSUP;
}
k_sem_take(drv_data->lock, K_FOREVER);
if ((flags & GPIO_OUTPUT) != 0) {
pins->dir &= ~BIT(pin);
if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
pins->data_out &= ~BIT(pin);
} else if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
pins->data_out |= BIT(pin);
}
} else {
pins->dir |= BIT(pin);
}
LOG_DBG("CFG %u %x : DIR %04x ; DAT %04x",
pin, flags, pins->dir, pins->data_out);
rc = write_pin_state(cfg, drv_data, pins);
k_sem_give(drv_data->lock);
return rc;
}
static int port_get(const struct device *dev,
gpio_port_value_t *value)
{
const struct cy8c95xx_config *cfg = dev->config;
struct cy8c95xx_drv_data *drv_data = dev->data;
uint8_t pin_data = 0;
int rc = 0;
/* Can't do I2C bus operations from an ISR */
if (k_is_in_isr()) {
return -EWOULDBLOCK;
}
k_sem_take(drv_data->lock, K_FOREVER);
rc = i2c_reg_read_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_INPUT_DATA0 + cfg->port_num, &pin_data);
if (rc == 0) {
*value = pin_data;
}
k_sem_give(drv_data->lock);
return rc;
}
static int port_write(const struct device *dev,
gpio_port_pins_t mask,
gpio_port_value_t value,
gpio_port_value_t toggle)
{
const struct cy8c95xx_config *cfg = dev->config;
struct cy8c95xx_drv_data *drv_data = dev->data;
uint8_t *outp = &drv_data->pin_state.data_out;
uint8_t out = ((*outp & ~mask) | (value & mask)) ^ toggle;
/* Can't do I2C bus operations from an ISR */
if (k_is_in_isr()) {
return -EWOULDBLOCK;
}
k_sem_take(drv_data->lock, K_FOREVER);
int rc = i2c_reg_write_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_OUTPUT_DATA0 + cfg->port_num, out);
if (rc == 0) {
*outp = out;
}
k_sem_give(drv_data->lock);
LOG_DBG("write msk %04x val %04x tog %04x => %04x: %d",
mask, value, toggle, out, rc);
return rc;
}
static int port_set_masked(const struct device *dev,
gpio_port_pins_t mask,
gpio_port_value_t value)
{
return port_write(dev, mask, value, 0);
}
static int port_set_bits(const struct device *dev,
gpio_port_pins_t pins)
{
return port_write(dev, pins, pins, 0);
}
static int port_clear_bits(const struct device *dev,
gpio_port_pins_t pins)
{
return port_write(dev, pins, 0, 0);
}
static int port_toggle_bits(const struct device *dev,
gpio_port_pins_t pins)
{
return port_write(dev, 0, 0, pins);
}
static int pin_interrupt_configure(const struct device *dev,
gpio_pin_t pin,
enum gpio_int_mode mode,
enum gpio_int_trig trig)
{
return -ENOTSUP;
}
/**
* @brief Initialization function of CY8C95XX
*
* @param dev Device struct
* @return 0 if successful, failed otherwise.
*/
static int cy8c95xx_init(const struct device *dev)
{
const struct cy8c95xx_config *cfg = dev->config;
struct cy8c95xx_drv_data *drv_data = dev->data;
int rc;
uint8_t data = 0;
k_sem_take(drv_data->lock, K_FOREVER);
if (!device_is_ready(cfg->i2c_master)) {
LOG_ERR("%s is not ready", cfg->i2c_master->name);
rc = -ENODEV;
goto out;
}
i2c_reg_read_byte(cfg->i2c_master, cfg->i2c_slave_addr,
CY8C95XX_REG_ID, &data);
LOG_DBG("cy8c95xx device ID %02X", data & 0xF0);
if ((data & 0xF0) != 0x20) {
LOG_WRN("driver only support [0-2] ports operations");
}
/* Reset state mediated by initial configuration */
drv_data->pin_state = (struct cy8c95xx_pin_state) {
.dir = 0xFF,
.data_out = 0xFF,
.pull_up = 0xFF,
.pull_down = 0x00,
};
rc = write_pin_state(cfg, drv_data, &drv_data->pin_state);
out:
if (rc != 0) {
LOG_ERR("%s init failed: %d", dev->name, rc);
} else {
LOG_INF("%s init ok", dev->name);
}
k_sem_give(drv_data->lock);
return rc;
}
static const struct gpio_driver_api api_table = {
.pin_configure = cy8c95xx_config,
.port_get_raw = port_get,
.port_set_masked_raw = port_set_masked,
.port_set_bits_raw = port_set_bits,
.port_clear_bits_raw = port_clear_bits,
.port_toggle_bits = port_toggle_bits,
.pin_interrupt_configure = pin_interrupt_configure,
};
static struct k_sem cy8c95xx_lock = Z_SEM_INITIALIZER(cy8c95xx_lock, 1, 1);
#define GPIO_PORT_INIT(idx) \
static const struct cy8c95xx_config cy8c95xx_##idx##_cfg = { \
.common = { \
.port_pin_mask = 0xFF, \
}, \
.i2c_master = DEVICE_DT_GET(DT_BUS(DT_INST(0, cypress_cy8c95xx_gpio))), \
.i2c_slave_addr = DT_REG_ADDR_BY_IDX(DT_INST(0, cypress_cy8c95xx_gpio), 0), \
.port_num = DT_INST_REG_ADDR(idx), \
}; \
static struct cy8c95xx_drv_data cy8c95xx_##idx##_drvdata = { \
.lock = &cy8c95xx_lock, \
}; \
DEVICE_DT_INST_DEFINE(idx, cy8c95xx_init, NULL, \
&cy8c95xx_##idx##_drvdata, &cy8c95xx_##idx##_cfg, \
POST_KERNEL, CONFIG_GPIO_CY8C95XX_INIT_PRIORITY, \
&api_table);
DT_INST_FOREACH_STATUS_OKAY(GPIO_PORT_INIT)