checkpatch: two spelling fixes
[sfrench/cifs-2.6.git] / include / linux / serdev.h
1 /*
2  * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 #ifndef _LINUX_SERDEV_H
14 #define _LINUX_SERDEV_H
15
16 #include <linux/types.h>
17 #include <linux/device.h>
18 #include <linux/termios.h>
19 #include <linux/delay.h>
20
21 struct serdev_controller;
22 struct serdev_device;
23
24 /*
25  * serdev device structures
26  */
27
28 /**
29  * struct serdev_device_ops - Callback operations for a serdev device
30  * @receive_buf:        Function called with data received from device;
31  *                      returns number of bytes accepted; may sleep.
32  * @write_wakeup:       Function called when ready to transmit more data; must
33  *                      not sleep.
34  */
35 struct serdev_device_ops {
36         int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
37         void (*write_wakeup)(struct serdev_device *);
38 };
39
40 /**
41  * struct serdev_device - Basic representation of an serdev device
42  * @dev:        Driver model representation of the device.
43  * @nr:         Device number on serdev bus.
44  * @ctrl:       serdev controller managing this device.
45  * @ops:        Device operations.
46  * @write_comp  Completion used by serdev_device_write() internally
47  * @write_lock  Lock to serialize access when writing data
48  */
49 struct serdev_device {
50         struct device dev;
51         int nr;
52         struct serdev_controller *ctrl;
53         const struct serdev_device_ops *ops;
54         struct completion write_comp;
55         struct mutex write_lock;
56 };
57
58 static inline struct serdev_device *to_serdev_device(struct device *d)
59 {
60         return container_of(d, struct serdev_device, dev);
61 }
62
63 /**
64  * struct serdev_device_driver - serdev slave device driver
65  * @driver:     serdev device drivers should initialize name field of this
66  *              structure.
67  * @probe:      binds this driver to a serdev device.
68  * @remove:     unbinds this driver from the serdev device.
69  */
70 struct serdev_device_driver {
71         struct device_driver driver;
72         int     (*probe)(struct serdev_device *);
73         void    (*remove)(struct serdev_device *);
74 };
75
76 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
77 {
78         return container_of(d, struct serdev_device_driver, driver);
79 }
80
81 enum serdev_parity {
82         SERDEV_PARITY_NONE,
83         SERDEV_PARITY_EVEN,
84         SERDEV_PARITY_ODD,
85 };
86
87 /*
88  * serdev controller structures
89  */
90 struct serdev_controller_ops {
91         int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
92         void (*write_flush)(struct serdev_controller *);
93         int (*write_room)(struct serdev_controller *);
94         int (*open)(struct serdev_controller *);
95         void (*close)(struct serdev_controller *);
96         void (*set_flow_control)(struct serdev_controller *, bool);
97         int (*set_parity)(struct serdev_controller *, enum serdev_parity);
98         unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
99         void (*wait_until_sent)(struct serdev_controller *, long);
100         int (*get_tiocm)(struct serdev_controller *);
101         int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
102 };
103
104 /**
105  * struct serdev_controller - interface to the serdev controller
106  * @dev:        Driver model representation of the device.
107  * @nr:         number identifier for this controller/bus.
108  * @serdev:     Pointer to slave device for this controller.
109  * @ops:        Controller operations.
110  */
111 struct serdev_controller {
112         struct device           dev;
113         unsigned int            nr;
114         struct serdev_device    *serdev;
115         const struct serdev_controller_ops *ops;
116 };
117
118 static inline struct serdev_controller *to_serdev_controller(struct device *d)
119 {
120         return container_of(d, struct serdev_controller, dev);
121 }
122
123 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
124 {
125         return dev_get_drvdata(&serdev->dev);
126 }
127
128 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
129 {
130         dev_set_drvdata(&serdev->dev, data);
131 }
132
133 /**
134  * serdev_device_put() - decrement serdev device refcount
135  * @serdev      serdev device.
136  */
137 static inline void serdev_device_put(struct serdev_device *serdev)
138 {
139         if (serdev)
140                 put_device(&serdev->dev);
141 }
142
143 static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
144                                               const struct serdev_device_ops *ops)
145 {
146         serdev->ops = ops;
147 }
148
149 static inline
150 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
151 {
152         return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
153 }
154
155 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
156                                                void *data)
157 {
158         dev_set_drvdata(&ctrl->dev, data);
159 }
160
161 /**
162  * serdev_controller_put() - decrement controller refcount
163  * @ctrl        serdev controller.
164  */
165 static inline void serdev_controller_put(struct serdev_controller *ctrl)
166 {
167         if (ctrl)
168                 put_device(&ctrl->dev);
169 }
170
171 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
172 int serdev_device_add(struct serdev_device *);
173 void serdev_device_remove(struct serdev_device *);
174
175 struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
176 int serdev_controller_add(struct serdev_controller *);
177 void serdev_controller_remove(struct serdev_controller *);
178
179 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
180 {
181         struct serdev_device *serdev = ctrl->serdev;
182
183         if (!serdev || !serdev->ops->write_wakeup)
184                 return;
185
186         serdev->ops->write_wakeup(serdev);
187 }
188
189 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
190                                               const unsigned char *data,
191                                               size_t count)
192 {
193         struct serdev_device *serdev = ctrl->serdev;
194
195         if (!serdev || !serdev->ops->receive_buf)
196                 return 0;
197
198         return serdev->ops->receive_buf(serdev, data, count);
199 }
200
201 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
202
203 int serdev_device_open(struct serdev_device *);
204 void serdev_device_close(struct serdev_device *);
205 int devm_serdev_device_open(struct device *, struct serdev_device *);
206 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
207 void serdev_device_set_flow_control(struct serdev_device *, bool);
208 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
209 void serdev_device_wait_until_sent(struct serdev_device *, long);
210 int serdev_device_get_tiocm(struct serdev_device *);
211 int serdev_device_set_tiocm(struct serdev_device *, int, int);
212 void serdev_device_write_wakeup(struct serdev_device *);
213 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
214 void serdev_device_write_flush(struct serdev_device *);
215 int serdev_device_write_room(struct serdev_device *);
216
217 /*
218  * serdev device driver functions
219  */
220 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
221 #define serdev_device_driver_register(sdrv) \
222         __serdev_device_driver_register(sdrv, THIS_MODULE)
223
224 /**
225  * serdev_device_driver_unregister() - unregister an serdev client driver
226  * @sdrv:       the driver to unregister
227  */
228 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
229 {
230         if (sdrv)
231                 driver_unregister(&sdrv->driver);
232 }
233
234 #define module_serdev_device_driver(__serdev_device_driver) \
235         module_driver(__serdev_device_driver, serdev_device_driver_register, \
236                         serdev_device_driver_unregister)
237
238 #else
239
240 static inline int serdev_device_open(struct serdev_device *sdev)
241 {
242         return -ENODEV;
243 }
244 static inline void serdev_device_close(struct serdev_device *sdev) {}
245 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
246 {
247         return 0;
248 }
249 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
250 static inline int serdev_device_write_buf(struct serdev_device *serdev,
251                                           const unsigned char *buf,
252                                           size_t count)
253 {
254         return -ENODEV;
255 }
256 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
257 static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
258 {
259         return -ENOTSUPP;
260 }
261 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
262 {
263         return -ENOTSUPP;
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         unsigned long timeout;
290         bool signal;
291
292         timeout = jiffies + msecs_to_jiffies(timeout_ms);
293         while (time_is_after_jiffies(timeout)) {
294                 signal = serdev_device_get_cts(serdev);
295                 if (signal == state)
296                         return 0;
297                 usleep_range(1000, 2000);
298         }
299
300         return -ETIMEDOUT;
301 }
302
303 static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
304 {
305         if (enable)
306                 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
307         else
308                 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
309 }
310
311 int serdev_device_set_parity(struct serdev_device *serdev,
312                              enum serdev_parity parity);
313
314 /*
315  * serdev hooks into TTY core
316  */
317 struct tty_port;
318 struct tty_driver;
319
320 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
321 struct device *serdev_tty_port_register(struct tty_port *port,
322                                         struct device *parent,
323                                         struct tty_driver *drv, int idx);
324 int serdev_tty_port_unregister(struct tty_port *port);
325 #else
326 static inline struct device *serdev_tty_port_register(struct tty_port *port,
327                                            struct device *parent,
328                                            struct tty_driver *drv, int idx)
329 {
330         return ERR_PTR(-ENODEV);
331 }
332 static inline int serdev_tty_port_unregister(struct tty_port *port)
333 {
334         return -ENODEV;
335 }
336 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
337
338 #endif /*_LINUX_SERDEV_H */