1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/err.h>
19#include <linux/device.h>
20#include <linux/gpio/consumer.h>
21#include <linux/termios.h>
22
23#include "serial_mctrl_gpio.h"
24
25struct mctrl_gpios {
26 struct gpio_desc *gpio[UART_GPIO_MAX];
27};
28
29static const struct {
30 const char *name;
31 unsigned int mctrl;
32 bool dir_out;
33} mctrl_gpios_desc[UART_GPIO_MAX] = {
34 { "cts", TIOCM_CTS, false, },
35 { "dsr", TIOCM_DSR, false, },
36 { "dcd", TIOCM_CD, false, },
37 { "rng", TIOCM_RNG, false, },
38 { "rts", TIOCM_RTS, true, },
39 { "dtr", TIOCM_DTR, true, },
40 { "out1", TIOCM_OUT1, true, },
41 { "out2", TIOCM_OUT2, true, },
42};
43
44void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
45{
46 enum mctrl_gpio_idx i;
47
48 if (IS_ERR_OR_NULL(gpios))
49 return;
50
51 for (i = 0; i < UART_GPIO_MAX; i++)
52 if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
53 mctrl_gpios_desc[i].dir_out)
54 gpiod_set_value(gpios->gpio[i],
55 !!(mctrl & mctrl_gpios_desc[i].mctrl));
56}
57EXPORT_SYMBOL_GPL(mctrl_gpio_set);
58
59struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
60 enum mctrl_gpio_idx gidx)
61{
62 if (!IS_ERR_OR_NULL(gpios) && !IS_ERR_OR_NULL(gpios->gpio[gidx]))
63 return gpios->gpio[gidx];
64 else
65 return NULL;
66}
67EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
68
69unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
70{
71 enum mctrl_gpio_idx i;
72
73
74
75
76 if (IS_ERR_OR_NULL(gpios))
77 return *mctrl;
78
79 for (i = 0; i < UART_GPIO_MAX; i++) {
80 if (!IS_ERR_OR_NULL(gpios->gpio[i]) &&
81 !mctrl_gpios_desc[i].dir_out) {
82 if (gpiod_get_value(gpios->gpio[i]))
83 *mctrl |= mctrl_gpios_desc[i].mctrl;
84 else
85 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
86 }
87 }
88
89 return *mctrl;
90}
91EXPORT_SYMBOL_GPL(mctrl_gpio_get);
92
93struct mctrl_gpios *mctrl_gpio_init(struct device *dev, unsigned int idx)
94{
95 struct mctrl_gpios *gpios;
96 enum mctrl_gpio_idx i;
97 int err;
98
99 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
100 if (!gpios)
101 return ERR_PTR(-ENOMEM);
102
103 for (i = 0; i < UART_GPIO_MAX; i++) {
104 gpios->gpio[i] = devm_gpiod_get_index(dev,
105 mctrl_gpios_desc[i].name,
106 idx);
107
108
109
110
111
112 if (IS_ERR_OR_NULL(gpios->gpio[i]))
113 continue;
114
115 if (mctrl_gpios_desc[i].dir_out)
116 err = gpiod_direction_output(gpios->gpio[i], 0);
117 else
118 err = gpiod_direction_input(gpios->gpio[i]);
119 if (err) {
120 dev_dbg(dev, "Unable to set direction for %s GPIO",
121 mctrl_gpios_desc[i].name);
122 devm_gpiod_put(dev, gpios->gpio[i]);
123 gpios->gpio[i] = NULL;
124 }
125 }
126
127 return gpios;
128}
129EXPORT_SYMBOL_GPL(mctrl_gpio_init);
130
131void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
132{
133 enum mctrl_gpio_idx i;
134
135 if (IS_ERR_OR_NULL(gpios))
136 return;
137
138 for (i = 0; i < UART_GPIO_MAX; i++)
139 if (!IS_ERR_OR_NULL(gpios->gpio[i]))
140 devm_gpiod_put(dev, gpios->gpio[i]);
141 devm_kfree(dev, gpios);
142}
143EXPORT_SYMBOL_GPL(mctrl_gpio_free);
144