1 /*
2 * Copyright (c) 2018 Peter Bigot Consulting, LLC
3 * Copyright (c) 2018 Aapo Vienamo
4 * Copyright (c) 2019 Nordic Semiconductor ASA
5 * Copyright (c) 2019 Vestas Wind Systems A/S
6 * Copyright (c) 2020 ZedBlox Ltd.
7 * Copyright (c) 2021 Laird Connectivity
8 *
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12 #include <errno.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/device.h>
15 #include <zephyr/init.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <zephyr/drivers/i2c.h>
18 #include <zephyr/sys/byteorder.h>
19 #include <zephyr/sys/util.h>
20
21 #include <zephyr/logging/log.h>
22 LOG_MODULE_REGISTER(pca953x, CONFIG_GPIO_LOG_LEVEL);
23
24 #include <zephyr/drivers/gpio/gpio_utils.h>
25
26 /* PCA953X Register addresses */
27 #define PCA953X_INPUT_PORT 0x00
28 #define PCA953X_OUTPUT_PORT 0x01
29 #define PCA953X_CONFIGURATION 0x03
30 #define REG_INPUT_LATCH_PORT0 0x42
31 #define REG_INT_MASK_PORT0 0x45
32
33 /* Number of pins supported by the device */
34 #define NUM_PINS 8
35
36 /* Max to select all pins supported on the device. */
37 #define ALL_PINS ((uint8_t)BIT_MASK(NUM_PINS))
38
39 /** Cache of the output configuration and data of the pins. */
40 struct pca953x_pin_state {
41 uint8_t dir;
42 uint8_t input;
43 uint8_t output;
44 };
45
46 struct pca953x_irq_state {
47 uint8_t rising;
48 uint8_t falling;
49 };
50
51 /** Runtime driver data */
52 struct pca953x_drv_data {
53 /* gpio_driver_data needs to be first */
54 struct gpio_driver_data common;
55 struct pca953x_pin_state pin_state;
56 struct k_sem lock;
57 struct gpio_callback gpio_cb;
58 struct k_work work;
59 struct pca953x_irq_state irq_state;
60 const struct device *dev;
61 /* user ISR cb */
62 sys_slist_t cb;
63 };
64
65 /** Configuration data */
66 struct pca953x_config {
67 /* gpio_driver_config needs to be first */
68 struct gpio_driver_config common;
69 struct i2c_dt_spec i2c;
70 const struct gpio_dt_spec gpio_int;
71 bool interrupt_enabled;
72 int interrupt_mask;
73 int input_latch;
74 };
75
76 /**
77 * @brief Gets the state of input pins of the PCA953X I/O Port and
78 * stores in driver data struct.
79 *
80 * @param dev Pointer to the device structure for the driver instance.
81 *
82 * @retval 0 If successful.
83 * @retval Negative value for error code.
84 */
update_input(const struct device * dev)85 static int update_input(const struct device *dev)
86 {
87 const struct pca953x_config *cfg = dev->config;
88 struct pca953x_drv_data *drv_data = dev->data;
89 uint8_t input_states;
90 int rc = 0;
91
92 rc = i2c_reg_read_byte_dt(&cfg->i2c, PCA953X_INPUT_PORT, &input_states);
93
94 if (rc == 0) {
95 drv_data->pin_state.input = input_states;
96 }
97 return rc;
98 }
99
100 /**
101 * @brief Handles interrupt triggered by the interrupt pin of PCA953X I/O Port.
102 *
103 * If nint_gpios is configured in device tree then this will be triggered each
104 * time a gpio configured as an input changes state. The gpio input states are
105 * read in this function which clears the interrupt.
106 *
107 * @param dev Pointer to the device structure for the driver instance.
108 */
gpio_pca953x_handle_interrupt(const struct device * dev)109 static void gpio_pca953x_handle_interrupt(const struct device *dev)
110 {
111 struct pca953x_drv_data *drv_data = dev->data;
112 struct pca953x_irq_state *irq_state = &drv_data->irq_state;
113 int rc;
114 uint8_t previous_state;
115 uint8_t current_state;
116 uint8_t transitioned_pins;
117 uint8_t interrupt_status = 0;
118
119 k_sem_take(&drv_data->lock, K_FOREVER);
120
121 /* Any interrupts enabled? */
122 if (!irq_state->rising && !irq_state->falling) {
123 rc = -EINVAL;
124 goto out;
125 }
126
127 /* Store previous input state then read new value */
128 previous_state = drv_data->pin_state.input;
129 rc = update_input(dev);
130 if (rc != 0) {
131 goto out;
132 }
133
134 /* Find out which input pins have changed state */
135 current_state = drv_data->pin_state.input;
136 transitioned_pins = previous_state ^ current_state;
137
138 /* Mask gpio transactions with rising/falling edge interrupt config */
139 interrupt_status = (irq_state->rising & transitioned_pins &
140 current_state);
141 interrupt_status |= (irq_state->falling & transitioned_pins &
142 previous_state);
143
144 out:
145 k_sem_give(&drv_data->lock);
146
147 if ((rc == 0) && (interrupt_status)) {
148 gpio_fire_callbacks(&drv_data->cb, dev, interrupt_status);
149 }
150 }
151
152 /**
153 * @brief Work handler for PCA953X interrupt
154 *
155 * @param work Work struct that contains pointer to interrupt handler function
156 */
gpio_pca953x_work_handler(struct k_work * work)157 static void gpio_pca953x_work_handler(struct k_work *work)
158 {
159 struct pca953x_drv_data *drv_data =
160 CONTAINER_OF(work, struct pca953x_drv_data, work);
161
162 gpio_pca953x_handle_interrupt(drv_data->dev);
163 }
164
165 /**
166 * @brief ISR for interrupt pin of PCA953X
167 *
168 * @param dev Pointer to the device structure for the driver instance.
169 * @param gpio_cb Pointer to callback function struct
170 * @param pins Bitmask of pins that triggered interrupt
171 */
gpio_pca953x_init_cb(const struct device * dev,struct gpio_callback * gpio_cb,uint32_t pins)172 static void gpio_pca953x_init_cb(const struct device *dev,
173 struct gpio_callback *gpio_cb, uint32_t pins)
174 {
175 struct pca953x_drv_data *drv_data =
176 CONTAINER_OF(gpio_cb, struct pca953x_drv_data, gpio_cb);
177
178 ARG_UNUSED(pins);
179
180 k_work_submit(&drv_data->work);
181 }
182
gpio_pca953x_config(const struct device * dev,gpio_pin_t pin,gpio_flags_t flags)183 static int gpio_pca953x_config(const struct device *dev, gpio_pin_t pin,
184 gpio_flags_t flags)
185 {
186 const struct pca953x_config *cfg = dev->config;
187 struct pca953x_drv_data *drv_data = dev->data;
188 struct pca953x_pin_state *pins = &drv_data->pin_state;
189 int rc = 0;
190 bool data_first = false;
191
192 /* Can't do I2C bus operations from an ISR */
193 if (k_is_in_isr()) {
194 return -EWOULDBLOCK;
195 }
196
197 /* Single Ended lines (Open drain and open source) not supported */
198 if ((flags & GPIO_SINGLE_ENDED) != 0) {
199 return -ENOTSUP;
200 }
201
202 /* The PCA953X has no internal pull up support */
203 if (((flags & GPIO_PULL_UP) != 0) || ((flags & GPIO_PULL_DOWN) != 0)) {
204 return -ENOTSUP;
205 }
206
207 /* Simultaneous input & output mode not supported */
208 if (((flags & GPIO_INPUT) != 0) && ((flags & GPIO_OUTPUT) != 0)) {
209 return -ENOTSUP;
210 }
211
212 k_sem_take(&drv_data->lock, K_FOREVER);
213
214 /* Ensure either Output or Input is specified */
215 if ((flags & GPIO_OUTPUT) != 0) {
216 pins->dir &= ~BIT(pin);
217 if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
218 pins->output &= ~BIT(pin);
219 data_first = true;
220 } else if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
221 pins->output |= BIT(pin);
222 data_first = true;
223 }
224 } else if ((flags & GPIO_INPUT) != 0) {
225 pins->dir |= BIT(pin);
226 } else {
227 rc = -ENOTSUP;
228 goto out;
229 }
230
231 /* Set output values */
232 if (data_first) {
233 rc = i2c_reg_write_byte_dt(&cfg->i2c, PCA953X_OUTPUT_PORT, pins->output);
234 }
235
236 if (rc == 0) {
237 /* Set pin directions */
238 rc = i2c_reg_write_byte_dt(&cfg->i2c, PCA953X_CONFIGURATION, pins->dir);
239 }
240
241 if (rc == 0) {
242 /* Refresh input status */
243 rc = update_input(dev);
244 }
245
246 out:
247 k_sem_give(&drv_data->lock);
248 return rc;
249 }
250
gpio_pca953x_port_read(const struct device * dev,gpio_port_value_t * value)251 static int gpio_pca953x_port_read(const struct device *dev,
252 gpio_port_value_t *value)
253 {
254 const struct pca953x_config *cfg = dev->config;
255 struct pca953x_drv_data *drv_data = dev->data;
256 uint8_t input_pin_data;
257 int rc = 0;
258
259 /* Can't do I2C bus operations from an ISR */
260 if (k_is_in_isr()) {
261 return -EWOULDBLOCK;
262 }
263
264 k_sem_take(&drv_data->lock, K_FOREVER);
265
266 /* Read Input Register */
267 rc = i2c_reg_read_byte_dt(&cfg->i2c, PCA953X_INPUT_PORT, &input_pin_data);
268
269 LOG_DBG("read %x got %d", input_pin_data, rc);
270
271 if (rc == 0) {
272 drv_data->pin_state.input = input_pin_data;
273 *value = (gpio_port_value_t)(drv_data->pin_state.input);
274 }
275
276 k_sem_give(&drv_data->lock);
277 return rc;
278 }
279
gpio_pca953x_port_write(const struct device * dev,gpio_port_pins_t mask,gpio_port_value_t value,gpio_port_value_t toggle)280 static int gpio_pca953x_port_write(const struct device *dev,
281 gpio_port_pins_t mask,
282 gpio_port_value_t value,
283 gpio_port_value_t toggle)
284 {
285 const struct pca953x_config *cfg = dev->config;
286 struct pca953x_drv_data *drv_data = dev->data;
287 uint8_t *outp = &drv_data->pin_state.output;
288 int rc;
289 uint8_t orig_out;
290 uint8_t out;
291
292 /* Can't do I2C bus operations from an ISR */
293 if (k_is_in_isr()) {
294 return -EWOULDBLOCK;
295 }
296
297 k_sem_take(&drv_data->lock, K_FOREVER);
298
299 orig_out = *outp;
300 out = ((orig_out & ~mask) | (value & mask)) ^ toggle;
301
302 rc = i2c_reg_write_byte_dt(&cfg->i2c, PCA953X_OUTPUT_PORT, out);
303
304 if (rc == 0) {
305 *outp = out;
306 }
307
308 k_sem_give(&drv_data->lock);
309
310 LOG_DBG("write %x msk %08x val %08x => %x: %d", orig_out, mask,
311 value, out, rc);
312
313 return rc;
314 }
315
gpio_pca953x_port_set_masked(const struct device * dev,gpio_port_pins_t mask,gpio_port_value_t value)316 static int gpio_pca953x_port_set_masked(const struct device *dev,
317 gpio_port_pins_t mask,
318 gpio_port_value_t value)
319 {
320 return gpio_pca953x_port_write(dev, mask, value, 0);
321 }
322
gpio_pca953x_port_set_bits(const struct device * dev,gpio_port_pins_t pins)323 static int gpio_pca953x_port_set_bits(const struct device *dev,
324 gpio_port_pins_t pins)
325 {
326 return gpio_pca953x_port_write(dev, pins, pins, 0);
327 }
328
gpio_pca953x_port_clear_bits(const struct device * dev,gpio_port_pins_t pins)329 static int gpio_pca953x_port_clear_bits(const struct device *dev,
330 gpio_port_pins_t pins)
331 {
332 return gpio_pca953x_port_write(dev, pins, 0, 0);
333 }
334
gpio_pca953x_port_toggle_bits(const struct device * dev,gpio_port_pins_t pins)335 static int gpio_pca953x_port_toggle_bits(const struct device *dev,
336 gpio_port_pins_t pins)
337 {
338 return gpio_pca953x_port_write(dev, 0, 0, pins);
339 }
340
gpio_pca953x_pin_interrupt_configure(const struct device * dev,gpio_pin_t pin,enum gpio_int_mode mode,enum gpio_int_trig trig)341 static int gpio_pca953x_pin_interrupt_configure(const struct device *dev,
342 gpio_pin_t pin,
343 enum gpio_int_mode mode,
344 enum gpio_int_trig trig)
345 {
346 const struct pca953x_config *cfg = dev->config;
347 struct pca953x_drv_data *drv_data = dev->data;
348 struct pca953x_irq_state *irq = &drv_data->irq_state;
349
350 if (!cfg->interrupt_enabled) {
351 return -ENOTSUP;
352 }
353 /* Device does not support level-triggered interrupts. */
354 if (mode == GPIO_INT_MODE_LEVEL) {
355 return -ENOTSUP;
356 }
357
358 k_sem_take(&drv_data->lock, K_FOREVER);
359
360 if (mode == GPIO_INT_MODE_DISABLED) {
361 irq->falling &= ~BIT(pin);
362 irq->rising &= ~BIT(pin);
363 } else { /* GPIO_INT_MODE_EDGE */
364 if (trig == GPIO_INT_TRIG_BOTH) {
365 irq->falling |= BIT(pin);
366 irq->rising |= BIT(pin);
367 } else if (trig == GPIO_INT_TRIG_LOW) {
368 irq->falling |= BIT(pin);
369 irq->rising &= ~BIT(pin);
370 } else if (trig == GPIO_INT_TRIG_HIGH) {
371 irq->falling &= ~BIT(pin);
372 irq->rising |= BIT(pin);
373 }
374 }
375
376 k_sem_give(&drv_data->lock);
377
378 return 0;
379 }
380
gpio_pca953x_manage_callback(const struct device * dev,struct gpio_callback * callback,bool set)381 static int gpio_pca953x_manage_callback(const struct device *dev,
382 struct gpio_callback *callback,
383 bool set)
384 {
385 struct pca953x_drv_data *data = dev->data;
386
387 return gpio_manage_callback(&data->cb, callback, set);
388 }
389
390 /**
391 * @brief Initialization function of PCA953X
392 *
393 * This sets initial input/ output configuration and output states.
394 * The interrupt is configured if this is enabled.
395 *
396 * @param dev Device struct
397 * @return 0 if successful, failed otherwise.
398 */
gpio_pca953x_init(const struct device * dev)399 static int gpio_pca953x_init(const struct device *dev)
400 {
401 const struct pca953x_config *cfg = dev->config;
402 struct pca953x_drv_data *drv_data = dev->data;
403 int rc = 0;
404
405 if (!device_is_ready(cfg->i2c.bus)) {
406 LOG_ERR("I2C bus device not found");
407 goto out;
408 }
409
410 /* Do an initial read, this clears the interrupt pin and sets
411 * up the initial value of the pin state input data.
412 */
413 rc = update_input(dev);
414 if (rc) {
415 goto out;
416 }
417
418 if (cfg->interrupt_enabled) {
419 if (!gpio_is_ready_dt(&cfg->gpio_int)) {
420 LOG_ERR("Cannot get pointer to gpio interrupt device");
421 rc = -EINVAL;
422 goto out;
423 }
424
425 drv_data->dev = dev;
426
427 k_work_init(&drv_data->work, gpio_pca953x_work_handler);
428
429 rc = gpio_pin_configure_dt(&cfg->gpio_int, GPIO_INPUT);
430 if (rc) {
431 goto out;
432 }
433
434 rc = gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
435 GPIO_INT_EDGE_TO_ACTIVE);
436 if (rc) {
437 goto out;
438 }
439
440 gpio_init_callback(&drv_data->gpio_cb,
441 gpio_pca953x_init_cb,
442 BIT(cfg->gpio_int.pin));
443
444 rc = gpio_add_callback(cfg->gpio_int.port,
445 &drv_data->gpio_cb);
446
447 if (rc) {
448 goto out;
449 }
450
451 /* This may not present on all variants of device */
452 if (cfg->input_latch > -1) {
453 rc = i2c_reg_write_byte_dt(&cfg->i2c, REG_INPUT_LATCH_PORT0,
454 cfg->input_latch);
455 }
456 if (cfg->interrupt_mask > -1) {
457 rc = i2c_reg_write_byte_dt(&cfg->i2c, REG_INT_MASK_PORT0,
458 cfg->interrupt_mask);
459 }
460 }
461 out:
462 if (rc) {
463 LOG_ERR("%s init failed: %d", dev->name, rc);
464 } else {
465 LOG_INF("%s init ok", dev->name);
466 }
467 return rc;
468 }
469
470 static const struct gpio_driver_api api_table = {
471 .pin_configure = gpio_pca953x_config,
472 .port_get_raw = gpio_pca953x_port_read,
473 .port_set_masked_raw = gpio_pca953x_port_set_masked,
474 .port_set_bits_raw = gpio_pca953x_port_set_bits,
475 .port_clear_bits_raw = gpio_pca953x_port_clear_bits,
476 .port_toggle_bits = gpio_pca953x_port_toggle_bits,
477 .pin_interrupt_configure = gpio_pca953x_pin_interrupt_configure,
478 .manage_callback = gpio_pca953x_manage_callback,
479 };
480
481 #define GPIO_PCA953X_INIT(n) \
482 static const struct pca953x_config pca953x_cfg_##n = { \
483 .i2c = I2C_DT_SPEC_INST_GET(n), \
484 .common = { \
485 .port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
486 }, \
487 .interrupt_enabled = DT_INST_NODE_HAS_PROP(n, nint_gpios), \
488 .gpio_int = GPIO_DT_SPEC_INST_GET_OR(n, nint_gpios, {0}), \
489 .interrupt_mask = DT_INST_PROP_OR(n, interrupt_mask, -1), \
490 .input_latch = DT_INST_PROP_OR(n, input_latch, -1), \
491 }; \
492 \
493 static struct pca953x_drv_data pca953x_drvdata_##n = { \
494 .lock = Z_SEM_INITIALIZER(pca953x_drvdata_##n.lock, 1, 1), \
495 .pin_state.dir = ALL_PINS, \
496 .pin_state.output = ALL_PINS, \
497 }; \
498 DEVICE_DT_INST_DEFINE(n, \
499 gpio_pca953x_init, \
500 NULL, \
501 &pca953x_drvdata_##n, \
502 &pca953x_cfg_##n, \
503 POST_KERNEL, \
504 CONFIG_GPIO_PCA953X_INIT_PRIORITY, \
505 &api_table);
506
507 #define DT_DRV_COMPAT ti_tca9538
508 DT_INST_FOREACH_STATUS_OKAY(GPIO_PCA953X_INIT)
509