Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
4 */
5#ifndef _LINUX_SERDEV_H
6#define _LINUX_SERDEV_H
7
8#include <linux/types.h>
9#include <linux/device.h>
10#include <linux/iopoll.h>
11#include <linux/uaccess.h>
12#include <linux/termios.h>
13#include <linux/delay.h>
14
15struct serdev_controller;
16struct serdev_device;
17
18/*
19 * serdev device structures
20 */
21
22/**
23 * struct serdev_device_ops - Callback operations for a serdev device
24 * @receive_buf: Function called with data received from device;
25 * returns number of bytes accepted; may sleep.
26 * @write_wakeup: Function called when ready to transmit more data; must
27 * not sleep.
28 */
29struct serdev_device_ops {
30 size_t (*receive_buf)(struct serdev_device *, const u8 *, size_t);
31 void (*write_wakeup)(struct serdev_device *);
32};
33
34/**
35 * struct serdev_device - Basic representation of an serdev device
36 * @dev: Driver model representation of the device.
37 * @nr: Device number on serdev bus.
38 * @ctrl: serdev controller managing this device.
39 * @ops: Device operations.
40 * @write_comp Completion used by serdev_device_write() internally
41 * @write_lock Lock to serialize access when writing data
42 */
43struct serdev_device {
44 struct device dev;
45 int nr;
46 struct serdev_controller *ctrl;
47 const struct serdev_device_ops *ops;
48 struct completion write_comp;
49 struct mutex write_lock;
50};
51
52#define to_serdev_device(d) container_of_const(d, struct serdev_device, dev)
53
54/**
55 * struct serdev_device_driver - serdev slave device driver
56 * @driver: serdev device drivers should initialize name field of this
57 * structure.
58 * @probe: binds this driver to a serdev device.
59 * @remove: unbinds this driver from the serdev device.
60 */
61struct serdev_device_driver {
62 struct device_driver driver;
63 int (*probe)(struct serdev_device *);
64 void (*remove)(struct serdev_device *);
65 void (*shutdown)(struct serdev_device *);
66};
67
68#define to_serdev_device_driver(d) container_of_const(d, struct serdev_device_driver, driver)
69
70enum serdev_parity {
71 SERDEV_PARITY_NONE,
72 SERDEV_PARITY_EVEN,
73 SERDEV_PARITY_ODD,
74};
75
76/*
77 * serdev controller structures
78 */
79struct serdev_controller_ops {
80 ssize_t (*write_buf)(struct serdev_controller *, const u8 *, size_t);
81 void (*write_flush)(struct serdev_controller *);
82 int (*open)(struct serdev_controller *);
83 void (*close)(struct serdev_controller *);
84 void (*set_flow_control)(struct serdev_controller *, bool);
85 int (*set_parity)(struct serdev_controller *, enum serdev_parity);
86 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
87 void (*wait_until_sent)(struct serdev_controller *, long);
88 int (*get_tiocm)(struct serdev_controller *);
89 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
90 int (*break_ctl)(struct serdev_controller *ctrl, unsigned int break_state);
91};
92
93/**
94 * struct serdev_controller - interface to the serdev controller
95 * @dev: Driver model representation of the device.
96 * @host: Serial port hardware controller device
97 * @nr: number identifier for this controller/bus.
98 * @serdev: Pointer to slave device for this controller.
99 * @ops: Controller operations.
100 */
101struct serdev_controller {
102 struct device dev;
103 struct device *host;
104 unsigned int nr;
105 struct serdev_device *serdev;
106 const struct serdev_controller_ops *ops;
107};
108
109#define to_serdev_controller(d) container_of_const(d, struct serdev_controller, dev)
110
111static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
112{
113 return dev_get_drvdata(&serdev->dev);
114}
115
116static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
117{
118 dev_set_drvdata(&serdev->dev, data);
119}
120
121/**
122 * serdev_device_put() - decrement serdev device refcount
123 * @serdev serdev device.
124 */
125static inline void serdev_device_put(struct serdev_device *serdev)
126{
127 if (serdev)
128 put_device(&serdev->dev);
129}
130
131static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
132 const struct serdev_device_ops *ops)
133{
134 serdev->ops = ops;
135}
136
137static inline
138void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
139{
140 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
141}
142
143static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
144 void *data)
145{
146 dev_set_drvdata(&ctrl->dev, data);
147}
148
149/**
150 * serdev_controller_put() - decrement controller refcount
151 * @ctrl serdev controller.
152 */
153static inline void serdev_controller_put(struct serdev_controller *ctrl)
154{
155 if (ctrl)
156 put_device(&ctrl->dev);
157}
158
159struct serdev_device *serdev_device_alloc(struct serdev_controller *);
160int serdev_device_add(struct serdev_device *);
161void serdev_device_remove(struct serdev_device *);
162
163struct serdev_controller *serdev_controller_alloc(struct device *host,
164 struct device *parent,
165 size_t size);
166int serdev_controller_add(struct serdev_controller *);
167void serdev_controller_remove(struct serdev_controller *);
168
169static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
170{
171 struct serdev_device *serdev = ctrl->serdev;
172
173 if (!serdev || !serdev->ops->write_wakeup)
174 return;
175
176 serdev->ops->write_wakeup(serdev);
177}
178
179static inline size_t serdev_controller_receive_buf(struct serdev_controller *ctrl,
180 const u8 *data,
181 size_t count)
182{
183 struct serdev_device *serdev = ctrl->serdev;
184
185 if (!serdev || !serdev->ops->receive_buf)
186 return 0;
187
188 return serdev->ops->receive_buf(serdev, data, count);
189}
190
191#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
192
193int serdev_device_open(struct serdev_device *);
194void serdev_device_close(struct serdev_device *);
195int devm_serdev_device_open(struct device *, struct serdev_device *);
196unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
197void serdev_device_set_flow_control(struct serdev_device *, bool);
198int serdev_device_write_buf(struct serdev_device *, const u8 *, size_t);
199void serdev_device_wait_until_sent(struct serdev_device *, long);
200int serdev_device_get_tiocm(struct serdev_device *);
201int serdev_device_set_tiocm(struct serdev_device *, int, int);
202int serdev_device_break_ctl(struct serdev_device *serdev, int break_state);
203void serdev_device_write_wakeup(struct serdev_device *);
204ssize_t serdev_device_write(struct serdev_device *, const u8 *, size_t, long);
205void serdev_device_write_flush(struct serdev_device *);
206
207/*
208 * serdev device driver functions
209 */
210int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
211#define serdev_device_driver_register(sdrv) \
212 __serdev_device_driver_register(sdrv, THIS_MODULE)
213
214/**
215 * serdev_device_driver_unregister() - unregister an serdev client driver
216 * @sdrv: the driver to unregister
217 */
218static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
219{
220 if (sdrv)
221 driver_unregister(&sdrv->driver);
222}
223
224#define module_serdev_device_driver(__serdev_device_driver) \
225 module_driver(__serdev_device_driver, serdev_device_driver_register, \
226 serdev_device_driver_unregister)
227
228#else
229
230static inline int serdev_device_open(struct serdev_device *sdev)
231{
232 return -ENODEV;
233}
234static inline void serdev_device_close(struct serdev_device *sdev) {}
235static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
236{
237 return 0;
238}
239static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
240static inline int serdev_device_write_buf(struct serdev_device *serdev,
241 const u8 *buf,
242 size_t count)
243{
244 return -ENODEV;
245}
246static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
247static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
248{
249 return -EOPNOTSUPP;
250}
251static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
252{
253 return -EOPNOTSUPP;
254}
255static inline int serdev_device_break_ctl(struct serdev_device *serdev, int break_state)
256{
257 return -EOPNOTSUPP;
258}
259static inline ssize_t serdev_device_write(struct serdev_device *sdev,
260 const u8 *buf, size_t count,
261 unsigned long timeout)
262{
263 return -ENODEV;
264}
265static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
266
267#define serdev_device_driver_register(x)
268#define serdev_device_driver_unregister(x)
269
270#endif /* CONFIG_SERIAL_DEV_BUS */
271
272static inline bool serdev_device_get_cts(struct serdev_device *serdev)
273{
274 int status = serdev_device_get_tiocm(serdev);
275 return !!(status & TIOCM_CTS);
276}
277
278static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
279{
280 bool signal;
281
282 return readx_poll_timeout(serdev_device_get_cts, serdev, signal, signal == state,
283 2000, timeout_ms * 1000);
284}
285
286static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
287{
288 if (enable)
289 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
290 else
291 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
292}
293
294int serdev_device_set_parity(struct serdev_device *serdev,
295 enum serdev_parity parity);
296
297/*
298 * serdev hooks into TTY core
299 */
300struct tty_port;
301struct tty_driver;
302
303#ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
304struct device *serdev_tty_port_register(struct tty_port *port,
305 struct device *host,
306 struct device *parent,
307 struct tty_driver *drv, int idx);
308int serdev_tty_port_unregister(struct tty_port *port);
309#else
310static inline struct device *serdev_tty_port_register(struct tty_port *port,
311 struct device *host,
312 struct device *parent,
313 struct tty_driver *drv, int idx)
314{
315 return ERR_PTR(-ENODEV);
316}
317static inline int serdev_tty_port_unregister(struct tty_port *port)
318{
319 return -ENODEV;
320}
321#endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
322
323struct acpi_resource;
324struct acpi_resource_uart_serialbus;
325
326#ifdef CONFIG_ACPI
327bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
328 struct acpi_resource_uart_serialbus **uart);
329#else
330static inline bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
331 struct acpi_resource_uart_serialbus **uart)
332{
333 return false;
334}
335#endif /* CONFIG_ACPI */
336
337#ifdef CONFIG_OF
338struct serdev_controller *of_find_serdev_controller_by_node(struct device_node *node);
339#else
340static inline struct serdev_controller *of_find_serdev_controller_by_node(struct device_node *node)
341{
342 return NULL;
343}
344#endif /* CONFIG_OF */
345
346#endif /*_LINUX_SERDEV_H */