1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
5 #ifndef _LINUX_SERDEV_H
6 #define _LINUX_SERDEV_H
8 #include <linux/types.h>
9 #include <linux/device.h>
10 #include <linux/uaccess.h>
11 #include <linux/termios.h>
12 #include <linux/delay.h>
14 struct serdev_controller;
18 * serdev device structures
22 * struct serdev_device_ops - Callback operations for a serdev device
23 * @receive_buf: Function called with data received from device;
24 * returns number of bytes accepted; may sleep.
25 * @write_wakeup: Function called when ready to transmit more data; must
28 struct serdev_device_ops {
29 int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
30 void (*write_wakeup)(struct serdev_device *);
34 * struct serdev_device - Basic representation of an serdev device
35 * @dev: Driver model representation of the device.
36 * @nr: Device number on serdev bus.
37 * @ctrl: serdev controller managing this device.
38 * @ops: Device operations.
39 * @write_comp Completion used by serdev_device_write() internally
40 * @write_lock Lock to serialize access when writing data
42 struct serdev_device {
45 struct serdev_controller *ctrl;
46 const struct serdev_device_ops *ops;
47 struct completion write_comp;
48 struct mutex write_lock;
51 static inline struct serdev_device *to_serdev_device(struct device *d)
53 return container_of(d, struct serdev_device, dev);
57 * struct serdev_device_driver - serdev slave device driver
58 * @driver: serdev device drivers should initialize name field of this
60 * @probe: binds this driver to a serdev device.
61 * @remove: unbinds this driver from the serdev device.
63 struct serdev_device_driver {
64 struct device_driver driver;
65 int (*probe)(struct serdev_device *);
66 void (*remove)(struct serdev_device *);
69 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
71 return container_of(d, struct serdev_device_driver, driver);
81 * serdev controller structures
83 struct serdev_controller_ops {
84 int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
85 void (*write_flush)(struct serdev_controller *);
86 int (*write_room)(struct serdev_controller *);
87 int (*open)(struct serdev_controller *);
88 void (*close)(struct serdev_controller *);
89 void (*set_flow_control)(struct serdev_controller *, bool);
90 int (*set_parity)(struct serdev_controller *, enum serdev_parity);
91 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
92 void (*wait_until_sent)(struct serdev_controller *, long);
93 int (*get_tiocm)(struct serdev_controller *);
94 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
98 * struct serdev_controller - interface to the serdev controller
99 * @dev: Driver model representation of the device.
100 * @nr: number identifier for this controller/bus.
101 * @serdev: Pointer to slave device for this controller.
102 * @ops: Controller operations.
104 struct serdev_controller {
107 struct serdev_device *serdev;
108 const struct serdev_controller_ops *ops;
111 static inline struct serdev_controller *to_serdev_controller(struct device *d)
113 return container_of(d, struct serdev_controller, dev);
116 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
118 return dev_get_drvdata(&serdev->dev);
121 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
123 dev_set_drvdata(&serdev->dev, data);
127 * serdev_device_put() - decrement serdev device refcount
128 * @serdev serdev device.
130 static inline void serdev_device_put(struct serdev_device *serdev)
133 put_device(&serdev->dev);
136 static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
137 const struct serdev_device_ops *ops)
143 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
145 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
148 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
151 dev_set_drvdata(&ctrl->dev, data);
155 * serdev_controller_put() - decrement controller refcount
156 * @ctrl serdev controller.
158 static inline void serdev_controller_put(struct serdev_controller *ctrl)
161 put_device(&ctrl->dev);
164 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
165 int serdev_device_add(struct serdev_device *);
166 void serdev_device_remove(struct serdev_device *);
168 struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
169 int serdev_controller_add(struct serdev_controller *);
170 void serdev_controller_remove(struct serdev_controller *);
172 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
174 struct serdev_device *serdev = ctrl->serdev;
176 if (!serdev || !serdev->ops->write_wakeup)
179 serdev->ops->write_wakeup(serdev);
182 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
183 const unsigned char *data,
186 struct serdev_device *serdev = ctrl->serdev;
188 if (!serdev || !serdev->ops->receive_buf)
191 return serdev->ops->receive_buf(serdev, data, count);
194 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
196 int serdev_device_open(struct serdev_device *);
197 void serdev_device_close(struct serdev_device *);
198 int devm_serdev_device_open(struct device *, struct serdev_device *);
199 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
200 void serdev_device_set_flow_control(struct serdev_device *, bool);
201 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
202 void serdev_device_wait_until_sent(struct serdev_device *, long);
203 int serdev_device_get_tiocm(struct serdev_device *);
204 int serdev_device_set_tiocm(struct serdev_device *, int, int);
205 void serdev_device_write_wakeup(struct serdev_device *);
206 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, long);
207 void serdev_device_write_flush(struct serdev_device *);
208 int serdev_device_write_room(struct serdev_device *);
211 * serdev device driver functions
213 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
214 #define serdev_device_driver_register(sdrv) \
215 __serdev_device_driver_register(sdrv, THIS_MODULE)
218 * serdev_device_driver_unregister() - unregister an serdev client driver
219 * @sdrv: the driver to unregister
221 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
224 driver_unregister(&sdrv->driver);
227 #define module_serdev_device_driver(__serdev_device_driver) \
228 module_driver(__serdev_device_driver, serdev_device_driver_register, \
229 serdev_device_driver_unregister)
233 static inline int serdev_device_open(struct serdev_device *sdev)
237 static inline void serdev_device_close(struct serdev_device *sdev) {}
238 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
242 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
243 static inline int serdev_device_write_buf(struct serdev_device *serdev,
244 const unsigned char *buf,
249 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
250 static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
254 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
258 static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
259 size_t count, unsigned long timeout)
263 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
264 static inline int serdev_device_write_room(struct serdev_device *sdev)
269 #define serdev_device_driver_register(x)
270 #define serdev_device_driver_unregister(x)
272 #endif /* CONFIG_SERIAL_DEV_BUS */
274 static inline bool serdev_device_get_cts(struct serdev_device *serdev)
276 int status = serdev_device_get_tiocm(serdev);
277 return !!(status & TIOCM_CTS);
280 static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
282 unsigned long timeout;
285 timeout = jiffies + msecs_to_jiffies(timeout_ms);
286 while (time_is_after_jiffies(timeout)) {
287 signal = serdev_device_get_cts(serdev);
290 usleep_range(1000, 2000);
296 static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
299 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
301 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
304 int serdev_device_set_parity(struct serdev_device *serdev,
305 enum serdev_parity parity);
308 * serdev hooks into TTY core
313 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
314 struct device *serdev_tty_port_register(struct tty_port *port,
315 struct device *parent,
316 struct tty_driver *drv, int idx);
317 int serdev_tty_port_unregister(struct tty_port *port);
319 static inline struct device *serdev_tty_port_register(struct tty_port *port,
320 struct device *parent,
321 struct tty_driver *drv, int idx)
323 return ERR_PTR(-ENODEV);
325 static inline int serdev_tty_port_unregister(struct tty_port *port)
329 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
331 struct acpi_resource;
332 struct acpi_resource_uart_serialbus;
335 bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
336 struct acpi_resource_uart_serialbus **uart);
338 static inline bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
339 struct acpi_resource_uart_serialbus **uart)
343 #endif /* CONFIG_ACPI */
345 #endif /*_LINUX_SERDEV_H */