2017-11-06 17:11:51 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-05-13 18:20:43 +00:00
|
|
|
/*
|
|
|
|
* Helpers for controlling modem lines via GPIO
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Paratronic S.A.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/device.h>
|
2015-09-30 08:19:41 +00:00
|
|
|
#include <linux/irq.h>
|
2014-05-13 18:20:43 +00:00
|
|
|
#include <linux/gpio/consumer.h>
|
2014-09-20 05:34:45 +00:00
|
|
|
#include <linux/termios.h>
|
2015-09-30 08:19:41 +00:00
|
|
|
#include <linux/serial_core.h>
|
2016-02-23 14:54:54 +00:00
|
|
|
#include <linux/module.h>
|
serial: mctrl_gpio: Check if GPIO property exisits before requesting it
This patch adds a check for the GPIOs property existence, before the
GPIO is requested. This fixes an issue seen when the 8250 mctrl_gpio
support is added (2nd patch in this patch series) on x86 platforms using
ACPI.
Here Mika's comments from 2016-08-09:
"
I noticed that with v4.8-rc1 serial console of some of our Broxton
systems does not work properly anymore. I'm able to see output but input
does not work.
I bisected it down to commit 4ef03d328769eddbfeca1f1c958fdb181a69c341
("tty/serial/8250: use mctrl_gpio helpers").
The reason why it fails is that in ACPI we do not have names for GPIOs
(except when _DSD is used) so we use the "idx" to index into _CRS GPIO
resources. Now mctrl_gpio_init_noauto() goes through a list of GPIOs
calling devm_gpiod_get_index_optional() passing "idx" of 0 for each. The
UART device in Broxton has following (simplified) ACPI description:
Device (URT4)
{
...
Name (_CRS, ResourceTemplate () {
GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly,
"\\_SB.GPO0", 0x00, ResourceConsumer)
{
0x003A
}
GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly,
"\\_SB.GPO0", 0x00, ResourceConsumer)
{
0x003D
}
})
In this case it finds the first GPIO (0x003A which happens to be RX pin
for that UART), turns it into GPIO which then breaks input for the UART
device. This also breaks systems with bluetooth connected to UART (those
typically have some GPIOs in their _CRS).
Any ideas how to fix this?
We cannot just drop the _CRS index lookup fallback because that would
break many existing machines out there so maybe we can limit this to
only DT enabled machines. Or alternatively probe if the property first
exists before trying to acquire the GPIOs (using
device_property_present()).
"
This patch implements the fix suggested by Mika in his statement above.
Signed-off-by: Stefan Roese <sr@denx.de>
Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Yegor Yefremov <yegorslists@googlemail.com>
Cc: Mika Westerberg <mika.westerberg@linux.intel.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Yegor Yefremov <yegorslists@googlemail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Giulio Benetti <giulio.benetti@micronovasrl.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-20 06:24:19 +00:00
|
|
|
#include <linux/property.h>
|
2014-05-13 18:20:43 +00:00
|
|
|
|
|
|
|
#include "serial_mctrl_gpio.h"
|
|
|
|
|
|
|
|
struct mctrl_gpios {
|
2015-09-30 08:19:41 +00:00
|
|
|
struct uart_port *port;
|
2014-05-13 18:20:43 +00:00
|
|
|
struct gpio_desc *gpio[UART_GPIO_MAX];
|
2015-09-30 08:19:41 +00:00
|
|
|
int irq[UART_GPIO_MAX];
|
|
|
|
unsigned int mctrl_prev;
|
|
|
|
bool mctrl_on;
|
2014-05-13 18:20:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
unsigned int mctrl;
|
2019-08-14 14:07:59 +00:00
|
|
|
enum gpiod_flags flags;
|
2014-05-13 18:20:43 +00:00
|
|
|
} mctrl_gpios_desc[UART_GPIO_MAX] = {
|
2019-08-14 14:07:59 +00:00
|
|
|
{ "cts", TIOCM_CTS, GPIOD_IN, },
|
|
|
|
{ "dsr", TIOCM_DSR, GPIOD_IN, },
|
|
|
|
{ "dcd", TIOCM_CD, GPIOD_IN, },
|
|
|
|
{ "rng", TIOCM_RNG, GPIOD_IN, },
|
|
|
|
{ "rts", TIOCM_RTS, GPIOD_OUT_LOW, },
|
|
|
|
{ "dtr", TIOCM_DTR, GPIOD_OUT_LOW, },
|
2014-05-13 18:20:43 +00:00
|
|
|
};
|
|
|
|
|
2019-08-14 14:07:59 +00:00
|
|
|
static bool mctrl_gpio_flags_is_dir_out(unsigned int idx)
|
|
|
|
{
|
|
|
|
return mctrl_gpios_desc[idx].flags & GPIOD_FLAGS_BIT_DIR_OUT;
|
|
|
|
}
|
|
|
|
|
2014-05-13 18:20:43 +00:00
|
|
|
void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
|
|
|
|
{
|
|
|
|
enum mctrl_gpio_idx i;
|
2014-11-17 17:31:30 +00:00
|
|
|
struct gpio_desc *desc_array[UART_GPIO_MAX];
|
2018-09-05 21:50:05 +00:00
|
|
|
DECLARE_BITMAP(values, UART_GPIO_MAX);
|
2014-11-17 17:31:30 +00:00
|
|
|
unsigned int count = 0;
|
2014-05-13 18:20:43 +00:00
|
|
|
|
2016-05-31 08:59:17 +00:00
|
|
|
if (gpios == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-05-13 18:20:43 +00:00
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++)
|
2019-08-14 14:07:59 +00:00
|
|
|
if (gpios->gpio[i] && mctrl_gpio_flags_is_dir_out(i)) {
|
2014-11-17 17:31:30 +00:00
|
|
|
desc_array[count] = gpios->gpio[i];
|
2018-09-05 21:50:05 +00:00
|
|
|
__assign_bit(count, values,
|
|
|
|
mctrl & mctrl_gpios_desc[i].mctrl);
|
2014-11-17 17:31:30 +00:00
|
|
|
count++;
|
|
|
|
}
|
2018-09-05 21:50:07 +00:00
|
|
|
gpiod_set_array_value(count, desc_array, NULL, values);
|
2014-05-13 18:20:43 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_set);
|
|
|
|
|
|
|
|
struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
|
|
|
|
enum mctrl_gpio_idx gidx)
|
|
|
|
{
|
2019-10-06 16:33:11 +00:00
|
|
|
if (gpios == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-02-12 14:24:41 +00:00
|
|
|
return gpios->gpio[gidx];
|
2014-05-13 18:20:43 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
|
|
|
|
|
|
|
|
unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
|
|
|
|
{
|
|
|
|
enum mctrl_gpio_idx i;
|
|
|
|
|
2016-05-31 08:59:17 +00:00
|
|
|
if (gpios == NULL)
|
|
|
|
return *mctrl;
|
|
|
|
|
2014-05-13 18:20:43 +00:00
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++) {
|
2019-08-14 14:07:59 +00:00
|
|
|
if (gpios->gpio[i] && !mctrl_gpio_flags_is_dir_out(i)) {
|
2014-05-13 18:20:43 +00:00
|
|
|
if (gpiod_get_value(gpios->gpio[i]))
|
|
|
|
*mctrl |= mctrl_gpios_desc[i].mctrl;
|
|
|
|
else
|
|
|
|
*mctrl &= ~mctrl_gpios_desc[i].mctrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *mctrl;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_get);
|
|
|
|
|
2016-05-31 08:59:16 +00:00
|
|
|
unsigned int
|
|
|
|
mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
|
|
|
|
{
|
|
|
|
enum mctrl_gpio_idx i;
|
|
|
|
|
2016-05-31 08:59:17 +00:00
|
|
|
if (gpios == NULL)
|
|
|
|
return *mctrl;
|
|
|
|
|
2016-05-31 08:59:16 +00:00
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++) {
|
2019-08-14 14:07:59 +00:00
|
|
|
if (gpios->gpio[i] && mctrl_gpio_flags_is_dir_out(i)) {
|
2016-05-31 08:59:16 +00:00
|
|
|
if (gpiod_get_value(gpios->gpio[i]))
|
|
|
|
*mctrl |= mctrl_gpios_desc[i].mctrl;
|
|
|
|
else
|
|
|
|
*mctrl &= ~mctrl_gpios_desc[i].mctrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *mctrl;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_get_outputs);
|
|
|
|
|
2015-09-30 08:19:40 +00:00
|
|
|
struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
|
2014-05-13 18:20:43 +00:00
|
|
|
{
|
|
|
|
struct mctrl_gpios *gpios;
|
|
|
|
enum mctrl_gpio_idx i;
|
|
|
|
|
|
|
|
gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
|
|
|
|
if (!gpios)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++) {
|
serial: mctrl_gpio: Check if GPIO property exisits before requesting it
This patch adds a check for the GPIOs property existence, before the
GPIO is requested. This fixes an issue seen when the 8250 mctrl_gpio
support is added (2nd patch in this patch series) on x86 platforms using
ACPI.
Here Mika's comments from 2016-08-09:
"
I noticed that with v4.8-rc1 serial console of some of our Broxton
systems does not work properly anymore. I'm able to see output but input
does not work.
I bisected it down to commit 4ef03d328769eddbfeca1f1c958fdb181a69c341
("tty/serial/8250: use mctrl_gpio helpers").
The reason why it fails is that in ACPI we do not have names for GPIOs
(except when _DSD is used) so we use the "idx" to index into _CRS GPIO
resources. Now mctrl_gpio_init_noauto() goes through a list of GPIOs
calling devm_gpiod_get_index_optional() passing "idx" of 0 for each. The
UART device in Broxton has following (simplified) ACPI description:
Device (URT4)
{
...
Name (_CRS, ResourceTemplate () {
GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly,
"\\_SB.GPO0", 0x00, ResourceConsumer)
{
0x003A
}
GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly,
"\\_SB.GPO0", 0x00, ResourceConsumer)
{
0x003D
}
})
In this case it finds the first GPIO (0x003A which happens to be RX pin
for that UART), turns it into GPIO which then breaks input for the UART
device. This also breaks systems with bluetooth connected to UART (those
typically have some GPIOs in their _CRS).
Any ideas how to fix this?
We cannot just drop the _CRS index lookup fallback because that would
break many existing machines out there so maybe we can limit this to
only DT enabled machines. Or alternatively probe if the property first
exists before trying to acquire the GPIOs (using
device_property_present()).
"
This patch implements the fix suggested by Mika in his statement above.
Signed-off-by: Stefan Roese <sr@denx.de>
Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Yegor Yefremov <yegorslists@googlemail.com>
Cc: Mika Westerberg <mika.westerberg@linux.intel.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Yegor Yefremov <yegorslists@googlemail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Giulio Benetti <giulio.benetti@micronovasrl.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-20 06:24:19 +00:00
|
|
|
char *gpio_str;
|
|
|
|
bool present;
|
|
|
|
|
|
|
|
/* Check if GPIO property exists and continue if not */
|
|
|
|
gpio_str = kasprintf(GFP_KERNEL, "%s-gpios",
|
|
|
|
mctrl_gpios_desc[i].name);
|
|
|
|
if (!gpio_str)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
present = device_property_present(dev, gpio_str);
|
|
|
|
kfree(gpio_str);
|
|
|
|
if (!present)
|
|
|
|
continue;
|
2014-05-13 18:20:43 +00:00
|
|
|
|
2015-02-12 14:24:42 +00:00
|
|
|
gpios->gpio[i] =
|
|
|
|
devm_gpiod_get_index_optional(dev,
|
|
|
|
mctrl_gpios_desc[i].name,
|
2019-08-14 14:07:59 +00:00
|
|
|
idx,
|
|
|
|
mctrl_gpios_desc[i].flags);
|
2015-02-12 14:24:42 +00:00
|
|
|
|
|
|
|
if (IS_ERR(gpios->gpio[i]))
|
2015-03-10 15:23:18 +00:00
|
|
|
return ERR_CAST(gpios->gpio[i]);
|
2014-05-13 18:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gpios;
|
|
|
|
}
|
2015-09-30 08:19:40 +00:00
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
|
2014-05-13 18:20:43 +00:00
|
|
|
|
2015-09-30 08:19:41 +00:00
|
|
|
#define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
|
|
|
|
static irqreturn_t mctrl_gpio_irq_handle(int irq, void *context)
|
|
|
|
{
|
|
|
|
struct mctrl_gpios *gpios = context;
|
|
|
|
struct uart_port *port = gpios->port;
|
|
|
|
u32 mctrl = gpios->mctrl_prev;
|
|
|
|
u32 mctrl_diff;
|
2016-04-29 08:45:07 +00:00
|
|
|
unsigned long flags;
|
2015-09-30 08:19:41 +00:00
|
|
|
|
|
|
|
mctrl_gpio_get(gpios, &mctrl);
|
|
|
|
|
2016-04-29 08:45:07 +00:00
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
|
|
|
|
2015-09-30 08:19:41 +00:00
|
|
|
mctrl_diff = mctrl ^ gpios->mctrl_prev;
|
|
|
|
gpios->mctrl_prev = mctrl;
|
|
|
|
|
|
|
|
if (mctrl_diff & MCTRL_ANY_DELTA && port->state != NULL) {
|
|
|
|
if ((mctrl_diff & mctrl) & TIOCM_RI)
|
|
|
|
port->icount.rng++;
|
|
|
|
|
|
|
|
if ((mctrl_diff & mctrl) & TIOCM_DSR)
|
|
|
|
port->icount.dsr++;
|
|
|
|
|
|
|
|
if (mctrl_diff & TIOCM_CD)
|
|
|
|
uart_handle_dcd_change(port, mctrl & TIOCM_CD);
|
|
|
|
|
|
|
|
if (mctrl_diff & TIOCM_CTS)
|
|
|
|
uart_handle_cts_change(port, mctrl & TIOCM_CTS);
|
|
|
|
|
|
|
|
wake_up_interruptible(&port->state->port.delta_msr_wait);
|
|
|
|
}
|
|
|
|
|
2016-04-29 08:45:07 +00:00
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
|
|
|
|
2015-09-30 08:19:41 +00:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mctrl_gpios *mctrl_gpio_init(struct uart_port *port, unsigned int idx)
|
|
|
|
{
|
|
|
|
struct mctrl_gpios *gpios;
|
|
|
|
enum mctrl_gpio_idx i;
|
|
|
|
|
|
|
|
gpios = mctrl_gpio_init_noauto(port->dev, idx);
|
|
|
|
if (IS_ERR(gpios))
|
|
|
|
return gpios;
|
|
|
|
|
|
|
|
gpios->port = port;
|
|
|
|
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; ++i) {
|
|
|
|
int ret;
|
|
|
|
|
2019-08-14 14:07:59 +00:00
|
|
|
if (!gpios->gpio[i] || mctrl_gpio_flags_is_dir_out(i))
|
2015-09-30 08:19:41 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = gpiod_to_irq(gpios->gpio[i]);
|
2020-10-31 03:05:30 +00:00
|
|
|
if (ret < 0) {
|
2015-09-30 08:19:41 +00:00
|
|
|
dev_err(port->dev,
|
|
|
|
"failed to find corresponding irq for %s (idx=%d, err=%d)\n",
|
|
|
|
mctrl_gpios_desc[i].name, idx, ret);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
gpios->irq[i] = ret;
|
|
|
|
|
|
|
|
/* irqs should only be enabled in .enable_ms */
|
|
|
|
irq_set_status_flags(gpios->irq[i], IRQ_NOAUTOEN);
|
|
|
|
|
|
|
|
ret = devm_request_irq(port->dev, gpios->irq[i],
|
|
|
|
mctrl_gpio_irq_handle,
|
|
|
|
IRQ_TYPE_EDGE_BOTH, dev_name(port->dev),
|
|
|
|
gpios);
|
|
|
|
if (ret) {
|
|
|
|
/* alternatively implement polling */
|
|
|
|
dev_err(port->dev,
|
|
|
|
"failed to request irq for %s (idx=%d, err=%d)\n",
|
|
|
|
mctrl_gpios_desc[i].name, idx, ret);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gpios;
|
|
|
|
}
|
2015-12-13 10:30:02 +00:00
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_init);
|
2015-09-30 08:19:41 +00:00
|
|
|
|
2014-05-13 18:20:43 +00:00
|
|
|
void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
|
|
|
|
{
|
|
|
|
enum mctrl_gpio_idx i;
|
|
|
|
|
2016-05-31 08:59:17 +00:00
|
|
|
if (gpios == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-09-30 08:19:41 +00:00
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++) {
|
|
|
|
if (gpios->irq[i])
|
|
|
|
devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
|
|
|
|
|
2015-05-19 19:56:29 +00:00
|
|
|
if (gpios->gpio[i])
|
2014-05-13 18:20:43 +00:00
|
|
|
devm_gpiod_put(dev, gpios->gpio[i]);
|
2015-09-30 08:19:41 +00:00
|
|
|
}
|
2014-05-13 18:20:43 +00:00
|
|
|
devm_kfree(dev, gpios);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_free);
|
2015-09-30 08:19:41 +00:00
|
|
|
|
|
|
|
void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
|
|
|
|
{
|
|
|
|
enum mctrl_gpio_idx i;
|
|
|
|
|
2016-05-31 08:59:17 +00:00
|
|
|
if (gpios == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-09-30 08:19:41 +00:00
|
|
|
/* .enable_ms may be called multiple times */
|
|
|
|
if (gpios->mctrl_on)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gpios->mctrl_on = true;
|
|
|
|
|
|
|
|
/* get initial status of modem lines GPIOs */
|
|
|
|
mctrl_gpio_get(gpios, &gpios->mctrl_prev);
|
|
|
|
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; ++i) {
|
|
|
|
if (!gpios->irq[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
enable_irq(gpios->irq[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_enable_ms);
|
|
|
|
|
|
|
|
void mctrl_gpio_disable_ms(struct mctrl_gpios *gpios)
|
|
|
|
{
|
|
|
|
enum mctrl_gpio_idx i;
|
|
|
|
|
2016-05-31 08:59:17 +00:00
|
|
|
if (gpios == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-09-30 08:19:41 +00:00
|
|
|
if (!gpios->mctrl_on)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gpios->mctrl_on = false;
|
|
|
|
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; ++i) {
|
|
|
|
if (!gpios->irq[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
disable_irq(gpios->irq[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 10:30:02 +00:00
|
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
|
2016-02-23 14:54:54 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|