1 /** @file
2 * @brief Modem pin setup for modem context driver
3 *
4 * GPIO-based pin handling for the modem context driver
5 */
6
7 /*
8 * Copyright (c) 2019 Foundries.io
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13 #include <zephyr/types.h>
14 #include <device.h>
15 #include <drivers/gpio.h>
16
17 #include "modem_context.h"
18
modem_pin_read(struct modem_context * ctx,uint32_t pin)19 int modem_pin_read(struct modem_context *ctx, uint32_t pin)
20 {
21 if (pin >= ctx->pins_len) {
22 return -ENODEV;
23 }
24
25 return gpio_pin_get(ctx->pins[pin].gpio_port_dev,
26 ctx->pins[pin].pin);
27 }
28
modem_pin_write(struct modem_context * ctx,uint32_t pin,uint32_t value)29 int modem_pin_write(struct modem_context *ctx, uint32_t pin, uint32_t value)
30 {
31 if (pin >= ctx->pins_len) {
32 return -ENODEV;
33 }
34
35 return gpio_pin_set(ctx->pins[pin].gpio_port_dev,
36 ctx->pins[pin].pin, value);
37 }
38
modem_pin_config(struct modem_context * ctx,uint32_t pin,bool enable)39 int modem_pin_config(struct modem_context *ctx, uint32_t pin, bool enable)
40 {
41 if (pin >= ctx->pins_len) {
42 return -ENODEV;
43 }
44
45 return gpio_pin_configure(ctx->pins[pin].gpio_port_dev,
46 ctx->pins[pin].pin,
47 enable ? ctx->pins[pin].init_flags :
48 GPIO_INPUT);
49 }
50
modem_pin_init(struct modem_context * ctx)51 int modem_pin_init(struct modem_context *ctx)
52 {
53 int i, ret;
54
55 /* setup port devices and pin directions */
56 for (i = 0; i < ctx->pins_len; i++) {
57 ctx->pins[i].gpio_port_dev =
58 device_get_binding(ctx->pins[i].dev_name);
59 if (!ctx->pins[i].gpio_port_dev) {
60 return -ENODEV;
61 }
62
63 ret = modem_pin_config(ctx, i, true);
64 if (ret < 0) {
65 return ret;
66 }
67 }
68
69 return 0;
70 }
71