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