blob: 1cdedd3bfb438686e1025b300b73324e66fc15e0 [file] [log] [blame]
Richard Genoud84130aa2014-05-13 20:20:43 +02001/*
2 * Helpers for controlling modem lines via GPIO
3 *
4 * Copyright (C) 2014 Paratronic S.A.
5 *
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.
10 *
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.
15 *
16 */
17
18#include <linux/err.h>
19#include <linux/device.h>
20#include <linux/gpio/consumer.h>
Alexander Shiyan93b88772014-09-20 09:34:45 +040021#include <linux/termios.h>
Richard Genoud84130aa2014-05-13 20:20:43 +020022
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;
Rojhalat Ibrahim834296a2014-11-17 18:31:30 +010047 struct gpio_desc *desc_array[UART_GPIO_MAX];
48 int value_array[UART_GPIO_MAX];
49 unsigned int count = 0;
Richard Genoud84130aa2014-05-13 20:20:43 +020050
Richard Genoud84130aa2014-05-13 20:20:43 +020051 for (i = 0; i < UART_GPIO_MAX; i++)
Uwe Kleine-König445df7f2015-05-19 21:56:29 +020052 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
Rojhalat Ibrahim834296a2014-11-17 18:31:30 +010053 desc_array[count] = gpios->gpio[i];
54 value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
55 count++;
56 }
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +020057 gpiod_set_array_value(count, desc_array, value_array);
Richard Genoud84130aa2014-05-13 20:20:43 +020058}
59EXPORT_SYMBOL_GPL(mctrl_gpio_set);
60
61struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
62 enum mctrl_gpio_idx gidx)
63{
Uwe Kleine-König9e9f0792015-02-12 15:24:41 +010064 return gpios->gpio[gidx];
Richard Genoud84130aa2014-05-13 20:20:43 +020065}
66EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
67
68unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
69{
70 enum mctrl_gpio_idx i;
71
Richard Genoud84130aa2014-05-13 20:20:43 +020072 for (i = 0; i < UART_GPIO_MAX; i++) {
Uwe Kleine-König9e9f0792015-02-12 15:24:41 +010073 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
Richard Genoud84130aa2014-05-13 20:20:43 +020074 if (gpiod_get_value(gpios->gpio[i]))
75 *mctrl |= mctrl_gpios_desc[i].mctrl;
76 else
77 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
78 }
79 }
80
81 return *mctrl;
82}
83EXPORT_SYMBOL_GPL(mctrl_gpio_get);
84
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +020085struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
Richard Genoud84130aa2014-05-13 20:20:43 +020086{
87 struct mctrl_gpios *gpios;
88 enum mctrl_gpio_idx i;
Richard Genoud84130aa2014-05-13 20:20:43 +020089
90 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
91 if (!gpios)
92 return ERR_PTR(-ENOMEM);
93
94 for (i = 0; i < UART_GPIO_MAX; i++) {
Uwe Kleine-König1d267ea2015-02-12 15:24:42 +010095 enum gpiod_flags flags;
Richard Genoud84130aa2014-05-13 20:20:43 +020096
97 if (mctrl_gpios_desc[i].dir_out)
Uwe Kleine-König1d267ea2015-02-12 15:24:42 +010098 flags = GPIOD_OUT_LOW;
Richard Genoud84130aa2014-05-13 20:20:43 +020099 else
Uwe Kleine-König1d267ea2015-02-12 15:24:42 +0100100 flags = GPIOD_IN;
101
102 gpios->gpio[i] =
103 devm_gpiod_get_index_optional(dev,
104 mctrl_gpios_desc[i].name,
105 idx, flags);
106
107 if (IS_ERR(gpios->gpio[i]))
Fabio Estevam13bc2bb2015-03-10 12:23:18 -0300108 return ERR_CAST(gpios->gpio[i]);
Richard Genoud84130aa2014-05-13 20:20:43 +0200109 }
110
111 return gpios;
112}
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +0200113EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
Richard Genoud84130aa2014-05-13 20:20:43 +0200114
115void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
116{
117 enum mctrl_gpio_idx i;
118
Richard Genoud84130aa2014-05-13 20:20:43 +0200119 for (i = 0; i < UART_GPIO_MAX; i++)
Uwe Kleine-König445df7f2015-05-19 21:56:29 +0200120 if (gpios->gpio[i])
Richard Genoud84130aa2014-05-13 20:20:43 +0200121 devm_gpiod_put(dev, gpios->gpio[i]);
122 devm_kfree(dev, gpios);
123}
124EXPORT_SYMBOL_GPL(mctrl_gpio_free);