Changes in 4.9.199 dm snapshot: use mutex instead of rw_semaphore dm snapshot: introduce account_start_copy() and account_end_copy() dm snapshot: rework COW throttling to fix deadlock dm: Use kzalloc for all structs with embedded biosets/mempools sc16is7xx: Fix for "Unexpected interrupt: 8" HID: i2c-hid: add Direkt-Tek DTLAPY133-1 to descriptor override x86/cpu: Add Atom Tremont (Jacobsville) HID: i2c-hid: Add Odys Winbook 13 to descriptor override scripts/setlocalversion: Improve -dirty check with git-status --no-optional-locks usb: handle warm-reset port requests on hub resume rtc: pcf8523: set xtal load capacitance from DT exec: load_script: Do not exec truncated interpreter path iio: fix center temperature of bmc150-accel-core perf map: Fix overlapped map handling perf jevents: Fix period for Intel fixed counters staging: rtl8188eu: fix null dereference when kzalloc fails RDMA/iwcm: Fix a lock inversion issue gpio: max77620: Use correct unit for debounce times fs: cifs: mute -Wunused-const-variable message serial: mctrl_gpio: Check for NULL pointer efi/cper: Fix endianness of PCIe class code efi/x86: Do not clean dummy variable in kexec path ocfs2: clear zero in unaligned direct IO fs: ocfs2: fix possible null-pointer dereferences in ocfs2_xa_prepare_entry() fs: ocfs2: fix a possible null-pointer dereference in ocfs2_write_end_nolock() fs: ocfs2: fix a possible null-pointer dereference in ocfs2_info_scan_inode_alloc() MIPS: fw: sni: Fix out of bounds init of o32 stack NFSv4: Fix leak of clp->cl_acceptor string s390/uaccess: avoid (false positive) compiler warnings tracing: Initialize iter->seq after zeroing in tracing_read_pipe() USB: legousbtower: fix a signedness bug in tower_probe() thunderbolt: Use 32-bit writes when writing ring producer/consumer ath6kl: fix a NULL-ptr-deref bug in ath6kl_usb_alloc_urb_from_pipe() fuse: flush dirty data/metadata before non-truncate setattr fuse: truncate pending writes on O_TRUNC ALSA: bebob: Fix prototype of helper function to return negative value UAS: Revert commit 3ae62a42090f ("UAS: fix alignment of scatter/gather segments") USB: gadget: Reject endpoints with 0 maxpacket value usb-storage: Revert commit 747668dbc061 ("usb-storage: Set virt_boundary_mask to avoid SG overflows") USB: ldusb: fix ring-buffer locking USB: ldusb: fix control-message timeout USB: serial: whiteheat: fix potential slab corruption USB: serial: whiteheat: fix line-speed endianness HID: i2c-hid: add Trekstor Primebook C11B to descriptor override HID: Fix assumption that devices have inputs HID: fix error message in hid_open_report() nl80211: fix validation of mesh path nexthop s390/cmm: fix information leak in cmm_timeout_handler() rtlwifi: Fix potential overflow on P2P code dmaengine: cppi41: Fix cppi41_dma_prep_slave_sg() when idle llc: fix sk_buff leak in llc_sap_state_process() llc: fix sk_buff leak in llc_conn_service() bonding: fix potential NULL deref in bond_update_slave_arr net: usb: sr9800: fix uninitialized local variable sch_netem: fix rcu splat in netem_enqueue() sctp: fix the issue that flags are ignored when using kernel_connect sctp: not bind the socket in sctp_connect xfs: Correctly invert xfs_buftarg LRU isolation logic ALSA: timer: Follow standard EXPORT_SYMBOL() declarations ALSA: timer: Limit max instances per timer ALSA: timer: Simplify error path in snd_timer_open() ALSA: timer: Fix mutex deadlock at releasing card Revert "ALSA: hda: Flush interrupts on disabling" Linux 4.9.199 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
297 lines
6.6 KiB
C
297 lines
6.6 KiB
C
/*
|
|
* Helpers for controlling modem lines via GPIO
|
|
*
|
|
* Copyright (C) 2014 Paratronic S.A.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*/
|
|
|
|
#include <linux/err.h>
|
|
#include <linux/device.h>
|
|
#include <linux/irq.h>
|
|
#include <linux/gpio/consumer.h>
|
|
#include <linux/termios.h>
|
|
#include <linux/serial_core.h>
|
|
#include <linux/module.h>
|
|
|
|
#include "serial_mctrl_gpio.h"
|
|
|
|
struct mctrl_gpios {
|
|
struct uart_port *port;
|
|
struct gpio_desc *gpio[UART_GPIO_MAX];
|
|
int irq[UART_GPIO_MAX];
|
|
unsigned int mctrl_prev;
|
|
bool mctrl_on;
|
|
};
|
|
|
|
static const struct {
|
|
const char *name;
|
|
unsigned int mctrl;
|
|
bool dir_out;
|
|
} mctrl_gpios_desc[UART_GPIO_MAX] = {
|
|
{ "cts", TIOCM_CTS, false, },
|
|
{ "dsr", TIOCM_DSR, false, },
|
|
{ "dcd", TIOCM_CD, false, },
|
|
{ "rng", TIOCM_RNG, false, },
|
|
{ "rts", TIOCM_RTS, true, },
|
|
{ "dtr", TIOCM_DTR, true, },
|
|
};
|
|
|
|
void mctrl_gpio_set(struct mctrl_gpios *gpios, unsigned int mctrl)
|
|
{
|
|
enum mctrl_gpio_idx i;
|
|
struct gpio_desc *desc_array[UART_GPIO_MAX];
|
|
int value_array[UART_GPIO_MAX];
|
|
unsigned int count = 0;
|
|
|
|
if (gpios == NULL)
|
|
return;
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++)
|
|
if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
|
|
desc_array[count] = gpios->gpio[i];
|
|
value_array[count] = !!(mctrl & mctrl_gpios_desc[i].mctrl);
|
|
count++;
|
|
}
|
|
gpiod_set_array_value(count, desc_array, value_array);
|
|
}
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_set);
|
|
|
|
struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
|
|
enum mctrl_gpio_idx gidx)
|
|
{
|
|
if (gpios == NULL)
|
|
return NULL;
|
|
|
|
return gpios->gpio[gidx];
|
|
}
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_to_gpiod);
|
|
|
|
unsigned int mctrl_gpio_get(struct mctrl_gpios *gpios, unsigned int *mctrl)
|
|
{
|
|
enum mctrl_gpio_idx i;
|
|
|
|
if (gpios == NULL)
|
|
return *mctrl;
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++) {
|
|
if (gpios->gpio[i] && !mctrl_gpios_desc[i].dir_out) {
|
|
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);
|
|
|
|
unsigned int
|
|
mctrl_gpio_get_outputs(struct mctrl_gpios *gpios, unsigned int *mctrl)
|
|
{
|
|
enum mctrl_gpio_idx i;
|
|
|
|
if (gpios == NULL)
|
|
return *mctrl;
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++) {
|
|
if (gpios->gpio[i] && mctrl_gpios_desc[i].dir_out) {
|
|
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);
|
|
|
|
struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
|
|
{
|
|
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++) {
|
|
enum gpiod_flags flags;
|
|
|
|
if (mctrl_gpios_desc[i].dir_out)
|
|
flags = GPIOD_OUT_LOW;
|
|
else
|
|
flags = GPIOD_IN;
|
|
|
|
gpios->gpio[i] =
|
|
devm_gpiod_get_index_optional(dev,
|
|
mctrl_gpios_desc[i].name,
|
|
idx, flags);
|
|
|
|
if (IS_ERR(gpios->gpio[i]))
|
|
return ERR_CAST(gpios->gpio[i]);
|
|
}
|
|
|
|
return gpios;
|
|
}
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_init_noauto);
|
|
|
|
#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;
|
|
unsigned long flags;
|
|
|
|
mctrl_gpio_get(gpios, &mctrl);
|
|
|
|
spin_lock_irqsave(&port->lock, flags);
|
|
|
|
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);
|
|
}
|
|
|
|
spin_unlock_irqrestore(&port->lock, flags);
|
|
|
|
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;
|
|
|
|
if (!gpios->gpio[i] || mctrl_gpios_desc[i].dir_out)
|
|
continue;
|
|
|
|
ret = gpiod_to_irq(gpios->gpio[i]);
|
|
if (ret <= 0) {
|
|
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;
|
|
}
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_init);
|
|
|
|
void mctrl_gpio_free(struct device *dev, struct mctrl_gpios *gpios)
|
|
{
|
|
enum mctrl_gpio_idx i;
|
|
|
|
if (gpios == NULL)
|
|
return;
|
|
|
|
for (i = 0; i < UART_GPIO_MAX; i++) {
|
|
if (gpios->irq[i])
|
|
devm_free_irq(gpios->port->dev, gpios->irq[i], gpios);
|
|
|
|
if (gpios->gpio[i])
|
|
devm_gpiod_put(dev, gpios->gpio[i]);
|
|
}
|
|
devm_kfree(dev, gpios);
|
|
}
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_free);
|
|
|
|
void mctrl_gpio_enable_ms(struct mctrl_gpios *gpios)
|
|
{
|
|
enum mctrl_gpio_idx i;
|
|
|
|
if (gpios == NULL)
|
|
return;
|
|
|
|
/* .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;
|
|
|
|
if (gpios == NULL)
|
|
return;
|
|
|
|
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]);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(mctrl_gpio_disable_ms);
|
|
|
|
MODULE_LICENSE("GPL");
|