Merge tag 'trace-v6.2-1' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux...
[sfrench/cifs-2.6.git] / include / linux / serdev.h
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
15 struct serdev_controller;
16 struct 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  */
29 struct serdev_device_ops {
30         int (*receive_buf)(struct serdev_device *, const unsigned char *, 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  */
43 struct 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 static inline struct serdev_device *to_serdev_device(struct device *d)
53 {
54         return container_of(d, struct serdev_device, dev);
55 }
56
57 /**
58  * struct serdev_device_driver - serdev slave device driver
59  * @driver:     serdev device drivers should initialize name field of this
60  *              structure.
61  * @probe:      binds this driver to a serdev device.
62  * @remove:     unbinds this driver from the serdev device.
63  */
64 struct serdev_device_driver {
65         struct device_driver driver;
66         int     (*probe)(struct serdev_device *);
67         void    (*remove)(struct serdev_device *);
68 };
69
70 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
71 {
72         return container_of(d, struct serdev_device_driver, driver);
73 }
74
75 enum serdev_parity {
76         SERDEV_PARITY_NONE,
77         SERDEV_PARITY_EVEN,
78         SERDEV_PARITY_ODD,
79 };
80
81 /*
82  * serdev controller structures
83  */
84 struct serdev_controller_ops {
85         int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
86         void (*write_flush)(struct serdev_controller *);
87         int (*write_room)(struct serdev_controller *);
88         int (*open)(struct serdev_controller *);
89         void (*close)(struct serdev_controller *);
90         void (*set_flow_control)(struct serdev_controller *, bool);
91         int (*set_parity)(struct serdev_controller *, enum serdev_parity);
92         unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
93         void (*wait_until_sent)(struct serdev_controller *, long);
94         int (*get_tiocm)(struct serdev_controller *);
95         int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
96 };
97
98 /**
99  * struct serdev_controller - interface to the serdev controller
100  * @dev:        Driver model representation of the device.
101  * @nr:         number identifier for this controller/bus.
102  * @serdev:     Pointer to slave device for this controller.
103  * @ops:        Controller operations.
104  */
105 struct serdev_controller {
106         struct device           dev;
107         unsigned int            nr;
108         struct serdev_device    *serdev;
109         const struct serdev_controller_ops *ops;
110 };
111
112 static inline struct serdev_controller *to_serdev_controller(struct device *d)
113 {
114         return container_of(d, struct serdev_controller, dev);
115 }
116
117 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
118 {
119         return dev_get_drvdata(&serdev->dev);
120 }
121
122 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
123 {
124         dev_set_drvdata(&serdev->dev, data);
125 }
126
127 /**
128  * serdev_device_put() - decrement serdev device refcount
129  * @serdev      serdev device.
130  */
131 static inline void serdev_device_put(struct serdev_device *serdev)
132 {
133         if (serdev)
134                 put_device(&serdev->dev);
135 }
136
137 static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
138                                               const struct serdev_device_ops *ops)
139 {
140         serdev->ops = ops;
141 }
142
143 static inline
144 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
145 {
146         return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
147 }
148
149 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
150                                                void *data)
151 {
152         dev_set_drvdata(&ctrl->dev, data);
153 }
154
155 /**
156  * serdev_controller_put() - decrement controller refcount
157  * @ctrl        serdev controller.
158  */
159 static inline void serdev_controller_put(struct serdev_controller *ctrl)
160 {
161         if (ctrl)
162                 put_device(&ctrl->dev);
163 }
164
165 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
166 int serdev_device_add(struct serdev_device *);
167 void serdev_device_remove(struct serdev_device *);
168
169 struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
170 int serdev_controller_add(struct serdev_controller *);
171 void serdev_controller_remove(struct serdev_controller *);
172
173 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
174 {
175         struct serdev_device *serdev = ctrl->serdev;
176
177         if (!serdev || !serdev->ops->write_wakeup)
178                 return;
179
180         serdev->ops->write_wakeup(serdev);
181 }
182
183 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
184                                               const unsigned char *data,
185                                               size_t count)
186 {
187         struct serdev_device *serdev = ctrl->serdev;
188
189         if (!serdev || !serdev->ops->receive_buf)
190                 return 0;
191
192         return serdev->ops->receive_buf(serdev, data, count);
193 }
194
195 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
196
197 int serdev_device_open(struct serdev_device *);
198 void serdev_device_close(struct serdev_device *);
199 int devm_serdev_device_open(struct device *, struct serdev_device *);
200 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
201 void serdev_device_set_flow_control(struct serdev_device *, bool);
202 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
203 void serdev_device_wait_until_sent(struct serdev_device *, long);
204 int serdev_device_get_tiocm(struct serdev_device *);
205 int serdev_device_set_tiocm(struct serdev_device *, int, int);
206 void serdev_device_write_wakeup(struct serdev_device *);
207 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, long);
208 void serdev_device_write_flush(struct serdev_device *);
209 int serdev_device_write_room(struct serdev_device *);
210
211 /*
212  * serdev device driver functions
213  */
214 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
215 #define serdev_device_driver_register(sdrv) \
216         __serdev_device_driver_register(sdrv, THIS_MODULE)
217
218 /**
219  * serdev_device_driver_unregister() - unregister an serdev client driver
220  * @sdrv:       the driver to unregister
221  */
222 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
223 {
224         if (sdrv)
225                 driver_unregister(&sdrv->driver);
226 }
227
228 #define module_serdev_device_driver(__serdev_device_driver) \
229         module_driver(__serdev_device_driver, serdev_device_driver_register, \
230                         serdev_device_driver_unregister)
231
232 #else
233
234 static inline int serdev_device_open(struct serdev_device *sdev)
235 {
236         return -ENODEV;
237 }
238 static inline void serdev_device_close(struct serdev_device *sdev) {}
239 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
240 {
241         return 0;
242 }
243 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
244 static inline int serdev_device_write_buf(struct serdev_device *serdev,
245                                           const unsigned char *buf,
246                                           size_t count)
247 {
248         return -ENODEV;
249 }
250 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
251 static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
252 {
253         return -ENOTSUPP;
254 }
255 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
256 {
257         return -ENOTSUPP;
258 }
259 static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
260                                       size_t count, unsigned long timeout)
261 {
262         return -ENODEV;
263 }
264 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
265 static inline int serdev_device_write_room(struct serdev_device *sdev)
266 {
267         return 0;
268 }
269
270 #define serdev_device_driver_register(x)
271 #define serdev_device_driver_unregister(x)
272
273 #endif /* CONFIG_SERIAL_DEV_BUS */
274
275 static inline bool serdev_device_get_cts(struct serdev_device *serdev)
276 {
277         int status = serdev_device_get_tiocm(serdev);
278         return !!(status & TIOCM_CTS);
279 }
280
281 static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
282 {
283         bool signal;
284
285         return readx_poll_timeout(serdev_device_get_cts, serdev, signal, signal == state,
286                                   2000, timeout_ms * 1000);
287 }
288
289 static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
290 {
291         if (enable)
292                 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
293         else
294                 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
295 }
296
297 int serdev_device_set_parity(struct serdev_device *serdev,
298                              enum serdev_parity parity);
299
300 /*
301  * serdev hooks into TTY core
302  */
303 struct tty_port;
304 struct tty_driver;
305
306 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
307 struct device *serdev_tty_port_register(struct tty_port *port,
308                                         struct device *parent,
309                                         struct tty_driver *drv, int idx);
310 int serdev_tty_port_unregister(struct tty_port *port);
311 #else
312 static inline struct device *serdev_tty_port_register(struct tty_port *port,
313                                            struct device *parent,
314                                            struct tty_driver *drv, int idx)
315 {
316         return ERR_PTR(-ENODEV);
317 }
318 static inline int serdev_tty_port_unregister(struct tty_port *port)
319 {
320         return -ENODEV;
321 }
322 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
323
324 struct acpi_resource;
325 struct acpi_resource_uart_serialbus;
326
327 #ifdef CONFIG_ACPI
328 bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
329                                    struct acpi_resource_uart_serialbus **uart);
330 #else
331 static inline bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
332                                                  struct acpi_resource_uart_serialbus **uart)
333 {
334         return false;
335 }
336 #endif /* CONFIG_ACPI */
337
338 #endif /*_LINUX_SERDEV_H */