Merge tag 'drm-msm-fixes-2023-05-17' of https://gitlab.freedesktop.org/drm/msm into...
[linux-2.6-microblaze.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         int (*break_ctl)(struct serdev_controller *ctrl, unsigned int break_state);
97 };
98
99 /**
100  * struct serdev_controller - interface to the serdev controller
101  * @dev:        Driver model representation of the device.
102  * @nr:         number identifier for this controller/bus.
103  * @serdev:     Pointer to slave device for this controller.
104  * @ops:        Controller operations.
105  */
106 struct serdev_controller {
107         struct device           dev;
108         unsigned int            nr;
109         struct serdev_device    *serdev;
110         const struct serdev_controller_ops *ops;
111 };
112
113 static inline struct serdev_controller *to_serdev_controller(struct device *d)
114 {
115         return container_of(d, struct serdev_controller, dev);
116 }
117
118 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
119 {
120         return dev_get_drvdata(&serdev->dev);
121 }
122
123 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
124 {
125         dev_set_drvdata(&serdev->dev, data);
126 }
127
128 /**
129  * serdev_device_put() - decrement serdev device refcount
130  * @serdev      serdev device.
131  */
132 static inline void serdev_device_put(struct serdev_device *serdev)
133 {
134         if (serdev)
135                 put_device(&serdev->dev);
136 }
137
138 static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
139                                               const struct serdev_device_ops *ops)
140 {
141         serdev->ops = ops;
142 }
143
144 static inline
145 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
146 {
147         return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
148 }
149
150 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
151                                                void *data)
152 {
153         dev_set_drvdata(&ctrl->dev, data);
154 }
155
156 /**
157  * serdev_controller_put() - decrement controller refcount
158  * @ctrl        serdev controller.
159  */
160 static inline void serdev_controller_put(struct serdev_controller *ctrl)
161 {
162         if (ctrl)
163                 put_device(&ctrl->dev);
164 }
165
166 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
167 int serdev_device_add(struct serdev_device *);
168 void serdev_device_remove(struct serdev_device *);
169
170 struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
171 int serdev_controller_add(struct serdev_controller *);
172 void serdev_controller_remove(struct serdev_controller *);
173
174 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
175 {
176         struct serdev_device *serdev = ctrl->serdev;
177
178         if (!serdev || !serdev->ops->write_wakeup)
179                 return;
180
181         serdev->ops->write_wakeup(serdev);
182 }
183
184 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
185                                               const unsigned char *data,
186                                               size_t count)
187 {
188         struct serdev_device *serdev = ctrl->serdev;
189
190         if (!serdev || !serdev->ops->receive_buf)
191                 return 0;
192
193         return serdev->ops->receive_buf(serdev, data, count);
194 }
195
196 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
197
198 int serdev_device_open(struct serdev_device *);
199 void serdev_device_close(struct serdev_device *);
200 int devm_serdev_device_open(struct device *, struct serdev_device *);
201 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
202 void serdev_device_set_flow_control(struct serdev_device *, bool);
203 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
204 void serdev_device_wait_until_sent(struct serdev_device *, long);
205 int serdev_device_get_tiocm(struct serdev_device *);
206 int serdev_device_set_tiocm(struct serdev_device *, int, int);
207 int serdev_device_break_ctl(struct serdev_device *serdev, int break_state);
208 void serdev_device_write_wakeup(struct serdev_device *);
209 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, long);
210 void serdev_device_write_flush(struct serdev_device *);
211 int serdev_device_write_room(struct serdev_device *);
212
213 /*
214  * serdev device driver functions
215  */
216 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
217 #define serdev_device_driver_register(sdrv) \
218         __serdev_device_driver_register(sdrv, THIS_MODULE)
219
220 /**
221  * serdev_device_driver_unregister() - unregister an serdev client driver
222  * @sdrv:       the driver to unregister
223  */
224 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
225 {
226         if (sdrv)
227                 driver_unregister(&sdrv->driver);
228 }
229
230 #define module_serdev_device_driver(__serdev_device_driver) \
231         module_driver(__serdev_device_driver, serdev_device_driver_register, \
232                         serdev_device_driver_unregister)
233
234 #else
235
236 static inline int serdev_device_open(struct serdev_device *sdev)
237 {
238         return -ENODEV;
239 }
240 static inline void serdev_device_close(struct serdev_device *sdev) {}
241 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
242 {
243         return 0;
244 }
245 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
246 static inline int serdev_device_write_buf(struct serdev_device *serdev,
247                                           const unsigned char *buf,
248                                           size_t count)
249 {
250         return -ENODEV;
251 }
252 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
253 static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
254 {
255         return -EOPNOTSUPP;
256 }
257 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
258 {
259         return -EOPNOTSUPP;
260 }
261 static inline int serdev_device_break_ctl(struct serdev_device *serdev, int break_state)
262 {
263         return -EOPNOTSUPP;
264 }
265 static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
266                                       size_t count, unsigned long timeout)
267 {
268         return -ENODEV;
269 }
270 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
271 static inline int serdev_device_write_room(struct serdev_device *sdev)
272 {
273         return 0;
274 }
275
276 #define serdev_device_driver_register(x)
277 #define serdev_device_driver_unregister(x)
278
279 #endif /* CONFIG_SERIAL_DEV_BUS */
280
281 static inline bool serdev_device_get_cts(struct serdev_device *serdev)
282 {
283         int status = serdev_device_get_tiocm(serdev);
284         return !!(status & TIOCM_CTS);
285 }
286
287 static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
288 {
289         bool signal;
290
291         return readx_poll_timeout(serdev_device_get_cts, serdev, signal, signal == state,
292                                   2000, timeout_ms * 1000);
293 }
294
295 static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
296 {
297         if (enable)
298                 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
299         else
300                 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
301 }
302
303 int serdev_device_set_parity(struct serdev_device *serdev,
304                              enum serdev_parity parity);
305
306 /*
307  * serdev hooks into TTY core
308  */
309 struct tty_port;
310 struct tty_driver;
311
312 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
313 struct device *serdev_tty_port_register(struct tty_port *port,
314                                         struct device *parent,
315                                         struct tty_driver *drv, int idx);
316 int serdev_tty_port_unregister(struct tty_port *port);
317 #else
318 static inline struct device *serdev_tty_port_register(struct tty_port *port,
319                                            struct device *parent,
320                                            struct tty_driver *drv, int idx)
321 {
322         return ERR_PTR(-ENODEV);
323 }
324 static inline int serdev_tty_port_unregister(struct tty_port *port)
325 {
326         return -ENODEV;
327 }
328 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
329
330 struct acpi_resource;
331 struct acpi_resource_uart_serialbus;
332
333 #ifdef CONFIG_ACPI
334 bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
335                                    struct acpi_resource_uart_serialbus **uart);
336 #else
337 static inline bool serdev_acpi_get_uart_resource(struct acpi_resource *ares,
338                                                  struct acpi_resource_uart_serialbus **uart)
339 {
340         return false;
341 }
342 #endif /* CONFIG_ACPI */
343
344 #endif /*_LINUX_SERDEV_H */