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