2 * Helpers for controlling modem lines via GPIO
4 * Copyright (C) 2014 Paratronic S.A.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/err.h>
19 #include <linux/device.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/termios.h>
23 #include "serial_mctrl_gpio.h"
26 struct gpio_desc *gpio[UART_GPIO_MAX];
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, },
44 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
46 enum mctrl_gpio_idx i;
48 if (IS_ERR_OR_NULL(gpios))
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));
57 EXPORT_SYMBOL_GPL(mctrl_gpio_set);
59 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
60 enum mctrl_gpio_idx gidx)
62 if (!IS_ERR_OR_NULL(gpios) && !IS_ERR_OR_NULL(gpios->gpio[gidx]))
63 return gpios->gpio[gidx];
67 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
69 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
71 enum mctrl_gpio_idx i;
74 * return it unchanged if the structure is not allocated
76 if (IS_ERR_OR_NULL(gpios))
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;
85 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
91 EXPORT_SYMBOL_GPL(mctrl_gpio_get);
93 struct mctrl_gpios *mctrl_gpio_init(struct device *dev, unsigned int idx)
95 struct mctrl_gpios *gpios;
96 enum mctrl_gpio_idx i;
99 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
101 return ERR_PTR(-ENOMEM);
103 for (i = 0; i < UART_GPIO_MAX; i++) {
104 gpios->gpio[i] = devm_gpiod_get_index(dev,
105 mctrl_gpios_desc[i].name,
109 * The GPIOs are maybe not all filled,
110 * this is not an error.
112 if (IS_ERR_OR_NULL(gpios->gpio[i]))
115 if (mctrl_gpios_desc[i].dir_out)
116 err = gpiod_direction_output(gpios->gpio[i], 0);
118 err = gpiod_direction_input(gpios->gpio[i]);
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;
129 EXPORT_SYMBOL_GPL(mctrl_gpio_init);
131 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
133 enum mctrl_gpio_idx i;
135 if (IS_ERR_OR_NULL(gpios))
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);
143 EXPORT_SYMBOL_GPL(mctrl_gpio_free);