2019-05-29 17:17:58 +03:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2017-02-02 22:48:07 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
|
|
|
|
*/
|
|
|
|
#ifndef _LINUX_SERDEV_H
|
|
|
|
#define _LINUX_SERDEV_H
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/device.h>
|
2022-11-14 18:18:13 +03:00
|
|
|
#include <linux/iopoll.h>
|
2022-08-10 02:41:40 +03:00
|
|
|
#include <linux/uaccess.h>
|
2017-03-28 18:59:32 +03:00
|
|
|
#include <linux/termios.h>
|
2017-03-28 18:59:33 +03:00
|
|
|
#include <linux/delay.h>
|
2017-02-02 22:48:07 +03:00
|
|
|
|
|
|
|
struct serdev_controller;
|
|
|
|
struct serdev_device;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* serdev device structures
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct serdev_device_ops - Callback operations for a serdev device
|
2017-11-03 17:30:54 +03:00
|
|
|
* @receive_buf: Function called with data received from device;
|
|
|
|
* returns number of bytes accepted; may sleep.
|
|
|
|
* @write_wakeup: Function called when ready to transmit more data; must
|
|
|
|
* not sleep.
|
2017-02-02 22:48:07 +03:00
|
|
|
*/
|
|
|
|
struct serdev_device_ops {
|
|
|
|
int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
|
|
|
|
void (*write_wakeup)(struct serdev_device *);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct serdev_device - Basic representation of an serdev device
|
|
|
|
* @dev: Driver model representation of the device.
|
|
|
|
* @nr: Device number on serdev bus.
|
|
|
|
* @ctrl: serdev controller managing this device.
|
|
|
|
* @ops: Device operations.
|
2017-04-04 17:22:33 +03:00
|
|
|
* @write_comp Completion used by serdev_device_write() internally
|
|
|
|
* @write_lock Lock to serialize access when writing data
|
2017-02-02 22:48:07 +03:00
|
|
|
*/
|
|
|
|
struct serdev_device {
|
|
|
|
struct device dev;
|
|
|
|
int nr;
|
|
|
|
struct serdev_controller *ctrl;
|
|
|
|
const struct serdev_device_ops *ops;
|
2017-04-04 17:22:33 +03:00
|
|
|
struct completion write_comp;
|
|
|
|
struct mutex write_lock;
|
2017-02-02 22:48:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct serdev_device *to_serdev_device(struct device *d)
|
|
|
|
{
|
|
|
|
return container_of(d, struct serdev_device, dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct serdev_device_driver - serdev slave device driver
|
|
|
|
* @driver: serdev device drivers should initialize name field of this
|
|
|
|
* structure.
|
|
|
|
* @probe: binds this driver to a serdev device.
|
|
|
|
* @remove: unbinds this driver from the serdev device.
|
|
|
|
*/
|
|
|
|
struct serdev_device_driver {
|
|
|
|
struct device_driver driver;
|
|
|
|
int (*probe)(struct serdev_device *);
|
|
|
|
void (*remove)(struct serdev_device *);
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
|
|
|
|
{
|
|
|
|
return container_of(d, struct serdev_device_driver, driver);
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:56:32 +03:00
|
|
|
enum serdev_parity {
|
|
|
|
SERDEV_PARITY_NONE,
|
|
|
|
SERDEV_PARITY_EVEN,
|
|
|
|
SERDEV_PARITY_ODD,
|
|
|
|
};
|
|
|
|
|
2017-02-02 22:48:07 +03:00
|
|
|
/*
|
|
|
|
* serdev controller structures
|
|
|
|
*/
|
|
|
|
struct serdev_controller_ops {
|
|
|
|
int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
|
|
|
|
void (*write_flush)(struct serdev_controller *);
|
|
|
|
int (*write_room)(struct serdev_controller *);
|
|
|
|
int (*open)(struct serdev_controller *);
|
|
|
|
void (*close)(struct serdev_controller *);
|
|
|
|
void (*set_flow_control)(struct serdev_controller *, bool);
|
2018-01-22 20:56:32 +03:00
|
|
|
int (*set_parity)(struct serdev_controller *, enum serdev_parity);
|
2017-02-02 22:48:07 +03:00
|
|
|
unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
|
2017-03-28 18:59:31 +03:00
|
|
|
void (*wait_until_sent)(struct serdev_controller *, long);
|
2017-03-28 18:59:32 +03:00
|
|
|
int (*get_tiocm)(struct serdev_controller *);
|
|
|
|
int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
|
2023-03-16 20:22:12 +03:00
|
|
|
int (*break_ctl)(struct serdev_controller *ctrl, unsigned int break_state);
|
2017-02-02 22:48:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct serdev_controller - interface to the serdev controller
|
|
|
|
* @dev: Driver model representation of the device.
|
|
|
|
* @nr: number identifier for this controller/bus.
|
|
|
|
* @serdev: Pointer to slave device for this controller.
|
|
|
|
* @ops: Controller operations.
|
|
|
|
*/
|
|
|
|
struct serdev_controller {
|
|
|
|
struct device dev;
|
|
|
|
unsigned int nr;
|
|
|
|
struct serdev_device *serdev;
|
|
|
|
const struct serdev_controller_ops *ops;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct serdev_controller *to_serdev_controller(struct device *d)
|
|
|
|
{
|
|
|
|
return container_of(d, struct serdev_controller, dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
|
|
|
|
{
|
|
|
|
return dev_get_drvdata(&serdev->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
|
|
|
|
{
|
|
|
|
dev_set_drvdata(&serdev->dev, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* serdev_device_put() - decrement serdev device refcount
|
|
|
|
* @serdev serdev device.
|
|
|
|
*/
|
|
|
|
static inline void serdev_device_put(struct serdev_device *serdev)
|
|
|
|
{
|
|
|
|
if (serdev)
|
|
|
|
put_device(&serdev->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
|
|
|
|
const struct serdev_device_ops *ops)
|
|
|
|
{
|
|
|
|
serdev->ops = ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
|
|
|
|
{
|
|
|
|
return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
dev_set_drvdata(&ctrl->dev, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* serdev_controller_put() - decrement controller refcount
|
|
|
|
* @ctrl serdev controller.
|
|
|
|
*/
|
|
|
|
static inline void serdev_controller_put(struct serdev_controller *ctrl)
|
|
|
|
{
|
|
|
|
if (ctrl)
|
|
|
|
put_device(&ctrl->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct serdev_device *serdev_device_alloc(struct serdev_controller *);
|
|
|
|
int serdev_device_add(struct serdev_device *);
|
|
|
|
void serdev_device_remove(struct serdev_device *);
|
|
|
|
|
|
|
|
struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
|
|
|
|
int serdev_controller_add(struct serdev_controller *);
|
|
|
|
void serdev_controller_remove(struct serdev_controller *);
|
|
|
|
|
|
|
|
static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
|
|
|
|
{
|
|
|
|
struct serdev_device *serdev = ctrl->serdev;
|
|
|
|
|
|
|
|
if (!serdev || !serdev->ops->write_wakeup)
|
|
|
|
return;
|
|
|
|
|
2017-03-16 18:14:16 +03:00
|
|
|
serdev->ops->write_wakeup(serdev);
|
2017-02-02 22:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t count)
|
|
|
|
{
|
|
|
|
struct serdev_device *serdev = ctrl->serdev;
|
|
|
|
|
|
|
|
if (!serdev || !serdev->ops->receive_buf)
|
2017-11-03 17:30:53 +03:00
|
|
|
return 0;
|
2017-02-02 22:48:07 +03:00
|
|
|
|
2017-03-16 18:14:16 +03:00
|
|
|
return serdev->ops->receive_buf(serdev, data, count);
|
2017-02-02 22:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
|
|
|
|
|
|
|
|
int serdev_device_open(struct serdev_device *);
|
|
|
|
void serdev_device_close(struct serdev_device *);
|
2017-12-21 09:51:15 +03:00
|
|
|
int devm_serdev_device_open(struct device *, struct serdev_device *);
|
2017-02-02 22:48:07 +03:00
|
|
|
unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
|
|
|
|
void serdev_device_set_flow_control(struct serdev_device *, bool);
|
2017-04-28 14:47:21 +03:00
|
|
|
int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
|
2017-03-28 18:59:31 +03:00
|
|
|
void serdev_device_wait_until_sent(struct serdev_device *, long);
|
2017-03-28 18:59:32 +03:00
|
|
|
int serdev_device_get_tiocm(struct serdev_device *);
|
|
|
|
int serdev_device_set_tiocm(struct serdev_device *, int, int);
|
2023-03-16 20:22:12 +03:00
|
|
|
int serdev_device_break_ctl(struct serdev_device *serdev, int break_state);
|
2017-04-04 17:22:33 +03:00
|
|
|
void serdev_device_write_wakeup(struct serdev_device *);
|
2018-11-14 18:09:03 +03:00
|
|
|
int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, long);
|
2017-02-02 22:48:07 +03:00
|
|
|
void serdev_device_write_flush(struct serdev_device *);
|
|
|
|
int serdev_device_write_room(struct serdev_device *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* serdev device driver functions
|
|
|
|
*/
|
|
|
|
int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
|
|
|
|
#define serdev_device_driver_register(sdrv) \
|
|
|
|
__serdev_device_driver_register(sdrv, THIS_MODULE)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* serdev_device_driver_unregister() - unregister an serdev client driver
|
|
|
|
* @sdrv: the driver to unregister
|
|
|
|
*/
|
|
|
|
static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
|
|
|
|
{
|
|
|
|
if (sdrv)
|
|
|
|
driver_unregister(&sdrv->driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define module_serdev_device_driver(__serdev_device_driver) \
|
|
|
|
module_driver(__serdev_device_driver, serdev_device_driver_register, \
|
|
|
|
serdev_device_driver_unregister)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static inline int serdev_device_open(struct serdev_device *sdev)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
static inline void serdev_device_close(struct serdev_device *sdev) {}
|
|
|
|
static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
|
2017-04-28 14:47:21 +03:00
|
|
|
static inline int serdev_device_write_buf(struct serdev_device *serdev,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t count)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2017-03-28 18:59:31 +03:00
|
|
|
static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
|
2017-03-28 18:59:32 +03:00
|
|
|
static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
|
|
|
|
{
|
2023-03-16 20:22:11 +03:00
|
|
|
return -EOPNOTSUPP;
|
2017-03-28 18:59:32 +03:00
|
|
|
}
|
|
|
|
static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
|
|
|
|
{
|
2023-03-16 20:22:11 +03:00
|
|
|
return -EOPNOTSUPP;
|
2017-03-28 18:59:32 +03:00
|
|
|
}
|
2023-03-16 20:22:12 +03:00
|
|
|
static inline int serdev_device_break_ctl(struct serdev_device *serdev, int break_state)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2017-04-04 17:22:33 +03:00
|
|
|
static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
|
|
|
|
size_t count, unsigned long timeout)
|
2017-02-02 22:48:07 +03:00
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
|
|
|
|
static inline int serdev_device_write_room(struct serdev_device *sdev)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define serdev_device_driver_register(x)
|
|
|
|
#define serdev_device_driver_unregister(x)
|
|
|
|
|
|
|
|
#endif /* CONFIG_SERIAL_DEV_BUS */
|
|
|
|
|
2017-03-28 18:59:33 +03:00
|
|
|
static inline bool serdev_device_get_cts(struct serdev_device *serdev)
|
|
|
|
{
|
|
|
|
int status = serdev_device_get_tiocm(serdev);
|
|
|
|
return !!(status & TIOCM_CTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
|
|
|
|
{
|
|
|
|
bool signal;
|
|
|
|
|
2022-11-14 18:18:13 +03:00
|
|
|
return readx_poll_timeout(serdev_device_get_cts, serdev, signal, signal == state,
|
|
|
|
2000, timeout_ms * 1000);
|
2017-03-28 18:59:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
|
|
|
|
else
|
|
|
|
return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:56:32 +03:00
|
|
|
int serdev_device_set_parity(struct serdev_device *serdev,
|
|
|
|
enum serdev_parity parity);
|
|
|
|
|
2017-02-02 22:48:08 +03:00
|
|
|
/*
|
|
|
|
* serdev hooks into TTY core
|
|
|
|
*/
|
|
|
|
struct tty_port;
|
|
|
|
struct tty_driver;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
|
|
|
|
struct device *serdev_tty_port_register(struct tty_port *port,
|
|
|
|
struct device *parent,
|
|
|
|
struct tty_driver *drv, int idx);
|
2017-05-18 18:33:00 +03:00
|
|
|
int serdev_tty_port_unregister(struct tty_port *port);
|
2017-02-02 22:48:08 +03:00
|
|
|
#else
|
|
|
|
static inline struct device *serdev_tty_port_register(struct tty_port *port,
|
|
|
|
struct device *parent,
|
|
|
|
struct tty_driver *drv, int idx)
|
|
|
|
{
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
}
|
2017-05-18 18:33:00 +03:00
|
|
|
static inline int serdev_tty_port_unregister(struct tty_port *port)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2017-02-02 22:48:08 +03:00
|
|
|
#endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
|
|
|
|
|
2021-08-06 14:17:35 +03:00
|
|
|
struct acpi_resource;
|
|
|
|
struct acpi_resource_uart_serialbus;
|
|
|
|
|
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
|
|
|
|
struct acpi_resource_uart_serialbus **uart);
|
|
|
|
#else
|
|
|
|
static inline bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
|
|
|
|
struct acpi_resource_uart_serialbus **uart)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_ACPI */
|
|
|
|
|
2017-02-02 22:48:07 +03:00
|
|
|
#endif /*_LINUX_SERDEV_H */
|