spi: Drop owner assignment from spi_drivers
[linux-2.6/btrfs-unstable.git] / drivers / tty / serial / serial_mctrl_gpio.c
blob402f7fb541333a67a0383a4ac2fb3754ed2f395d
1 /*
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"
25 struct mctrl_gpios {
26 struct gpio_desc *gpio[UART_GPIO_MAX];
29 static 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, },
44 void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
46 enum mctrl_gpio_idx i;
47 struct gpio_desc *desc_array[UART_GPIO_MAX];
48 int value_array[UART_GPIO_MAX];
49 unsigned int count = 0;
51 for (i = 0; i < UART_GPIO_MAX; i++)
52 if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
53 desc_array[count] = gpios->gpio[i];
54 value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
55 count++;
57 gpiod_set_array_value(count, desc_array, value_array);
59 EXPORT_SYMBOL_GPL(mctrl_gpio_set);
61 struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
62 enum mctrl_gpio_idx gidx)
64 return gpios->gpio[gidx];
66 EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
68 unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
70 enum mctrl_gpio_idx i;
72 for (i = 0; i < UART_GPIO_MAX; i++) {
73 if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
74 if (gpiod_get_value(gpios->gpio[i]))
75 *mctrl |= mctrl_gpios_desc[i].mctrl;
76 else
77 *mctrl &= ~mctrl_gpios_desc[i].mctrl;
81 return *mctrl;
83 EXPORT_SYMBOL_GPL(mctrl_gpio_get);
85 struct mctrl_gpios *mctrl_gpio_init(struct device *dev, unsigned int idx)
87 struct mctrl_gpios *gpios;
88 enum mctrl_gpio_idx i;
90 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
91 if (!gpios)
92 return ERR_PTR(-ENOMEM);
94 for (i = 0; i < UART_GPIO_MAX; i++) {
95 enum gpiod_flags flags;
97 if (mctrl_gpios_desc[i].dir_out)
98 flags = GPIOD_OUT_LOW;
99 else
100 flags = GPIOD_IN;
102 gpios->gpio[i] =
103 devm_gpiod_get_index_optional(dev,
104 mctrl_gpios_desc[i].name,
105 idx, flags);
107 if (IS_ERR(gpios->gpio[i]))
108 return ERR_CAST(gpios->gpio[i]);
111 return gpios;
113 EXPORT_SYMBOL_GPL(mctrl_gpio_init);
115 void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
117 enum mctrl_gpio_idx i;
119 for (i = 0; i < UART_GPIO_MAX; i++)
120 if (gpios->gpio[i])
121 devm_gpiod_put(dev, gpios->gpio[i]);
122 devm_kfree(dev, gpios);
124 EXPORT_SYMBOL_GPL(mctrl_gpio_free);