]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - include/linux/serdev.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
5 #ifndef _LINUX_SERDEV_H
6 #define _LINUX_SERDEV_H
8 #include <linux/types.h>
9 #include <linux/device.h>
10 #include <linux/termios.h>
11 #include <linux/delay.h>
13 struct serdev_controller
;
17 * serdev device structures
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
27 struct serdev_device_ops
{
28 int (*receive_buf
)(struct serdev_device
*, const unsigned char *, size_t);
29 void (*write_wakeup
)(struct serdev_device
*);
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
41 struct serdev_device
{
44 struct serdev_controller
*ctrl
;
45 const struct serdev_device_ops
*ops
;
46 struct completion write_comp
;
47 struct mutex write_lock
;
50 static inline struct serdev_device
*to_serdev_device(struct device
*d
)
52 return container_of(d
, struct serdev_device
, dev
);
56 * struct serdev_device_driver - serdev slave device driver
57 * @driver: serdev device drivers should initialize name field of this
59 * @probe: binds this driver to a serdev device.
60 * @remove: unbinds this driver from the serdev device.
62 struct serdev_device_driver
{
63 struct device_driver driver
;
64 int (*probe
)(struct serdev_device
*);
65 void (*remove
)(struct serdev_device
*);
68 static inline struct serdev_device_driver
*to_serdev_device_driver(struct device_driver
*d
)
70 return container_of(d
, struct serdev_device_driver
, driver
);
80 * serdev controller structures
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);
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.
103 struct serdev_controller
{
106 struct serdev_device
*serdev
;
107 const struct serdev_controller_ops
*ops
;
110 static inline struct serdev_controller
*to_serdev_controller(struct device
*d
)
112 return container_of(d
, struct serdev_controller
, dev
);
115 static inline void *serdev_device_get_drvdata(const struct serdev_device
*serdev
)
117 return dev_get_drvdata(&serdev
->dev
);
120 static inline void serdev_device_set_drvdata(struct serdev_device
*serdev
, void *data
)
122 dev_set_drvdata(&serdev
->dev
, data
);
126 * serdev_device_put() - decrement serdev device refcount
127 * @serdev serdev device.
129 static inline void serdev_device_put(struct serdev_device
*serdev
)
132 put_device(&serdev
->dev
);
135 static inline void serdev_device_set_client_ops(struct serdev_device
*serdev
,
136 const struct serdev_device_ops
*ops
)
142 void *serdev_controller_get_drvdata(const struct serdev_controller
*ctrl
)
144 return ctrl
? dev_get_drvdata(&ctrl
->dev
) : NULL
;
147 static inline void serdev_controller_set_drvdata(struct serdev_controller
*ctrl
,
150 dev_set_drvdata(&ctrl
->dev
, data
);
154 * serdev_controller_put() - decrement controller refcount
155 * @ctrl serdev controller.
157 static inline void serdev_controller_put(struct serdev_controller
*ctrl
)
160 put_device(&ctrl
->dev
);
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
*);
167 struct serdev_controller
*serdev_controller_alloc(struct device
*, size_t);
168 int serdev_controller_add(struct serdev_controller
*);
169 void serdev_controller_remove(struct serdev_controller
*);
171 static inline void serdev_controller_write_wakeup(struct serdev_controller
*ctrl
)
173 struct serdev_device
*serdev
= ctrl
->serdev
;
175 if (!serdev
|| !serdev
->ops
->write_wakeup
)
178 serdev
->ops
->write_wakeup(serdev
);
181 static inline int serdev_controller_receive_buf(struct serdev_controller
*ctrl
,
182 const unsigned char *data
,
185 struct serdev_device
*serdev
= ctrl
->serdev
;
187 if (!serdev
|| !serdev
->ops
->receive_buf
)
190 return serdev
->ops
->receive_buf(serdev
, data
, count
);
193 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
195 int serdev_device_open(struct serdev_device
*);
196 void serdev_device_close(struct serdev_device
*);
197 int devm_serdev_device_open(struct device
*, struct serdev_device
*);
198 unsigned int serdev_device_set_baudrate(struct serdev_device
*, unsigned int);
199 void serdev_device_set_flow_control(struct serdev_device
*, bool);
200 int serdev_device_write_buf(struct serdev_device
*, const unsigned char *, size_t);
201 void serdev_device_wait_until_sent(struct serdev_device
*, long);
202 int serdev_device_get_tiocm(struct serdev_device
*);
203 int serdev_device_set_tiocm(struct serdev_device
*, int, int);
204 void serdev_device_write_wakeup(struct serdev_device
*);
205 int serdev_device_write(struct serdev_device
*, const unsigned char *, size_t, long);
206 void serdev_device_write_flush(struct serdev_device
*);
207 int serdev_device_write_room(struct serdev_device
*);
210 * serdev device driver functions
212 int __serdev_device_driver_register(struct serdev_device_driver
*, struct module
*);
213 #define serdev_device_driver_register(sdrv) \
214 __serdev_device_driver_register(sdrv, THIS_MODULE)
217 * serdev_device_driver_unregister() - unregister an serdev client driver
218 * @sdrv: the driver to unregister
220 static inline void serdev_device_driver_unregister(struct serdev_device_driver
*sdrv
)
223 driver_unregister(&sdrv
->driver
);
226 #define module_serdev_device_driver(__serdev_device_driver) \
227 module_driver(__serdev_device_driver, serdev_device_driver_register, \
228 serdev_device_driver_unregister)
232 static inline int serdev_device_open(struct serdev_device
*sdev
)
236 static inline void serdev_device_close(struct serdev_device
*sdev
) {}
237 static inline unsigned int serdev_device_set_baudrate(struct serdev_device
*sdev
, unsigned int baudrate
)
241 static inline void serdev_device_set_flow_control(struct serdev_device
*sdev
, bool enable
) {}
242 static inline int serdev_device_write_buf(struct serdev_device
*serdev
,
243 const unsigned char *buf
,
248 static inline void serdev_device_wait_until_sent(struct serdev_device
*sdev
, long timeout
) {}
249 static inline int serdev_device_get_tiocm(struct serdev_device
*serdev
)
253 static inline int serdev_device_set_tiocm(struct serdev_device
*serdev
, int set
, int clear
)
257 static inline int serdev_device_write(struct serdev_device
*sdev
, const unsigned char *buf
,
258 size_t count
, unsigned long timeout
)
262 static inline void serdev_device_write_flush(struct serdev_device
*sdev
) {}
263 static inline int serdev_device_write_room(struct serdev_device
*sdev
)
268 #define serdev_device_driver_register(x)
269 #define serdev_device_driver_unregister(x)
271 #endif /* CONFIG_SERIAL_DEV_BUS */
273 static inline bool serdev_device_get_cts(struct serdev_device
*serdev
)
275 int status
= serdev_device_get_tiocm(serdev
);
276 return !!(status
& TIOCM_CTS
);
279 static inline int serdev_device_wait_for_cts(struct serdev_device
*serdev
, bool state
, int timeout_ms
)
281 unsigned long timeout
;
284 timeout
= jiffies
+ msecs_to_jiffies(timeout_ms
);
285 while (time_is_after_jiffies(timeout
)) {
286 signal
= serdev_device_get_cts(serdev
);
289 usleep_range(1000, 2000);
295 static inline int serdev_device_set_rts(struct serdev_device
*serdev
, bool enable
)
298 return serdev_device_set_tiocm(serdev
, TIOCM_RTS
, 0);
300 return serdev_device_set_tiocm(serdev
, 0, TIOCM_RTS
);
303 int serdev_device_set_parity(struct serdev_device
*serdev
,
304 enum serdev_parity parity
);
307 * serdev hooks into TTY core
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
);
318 static inline struct device
*serdev_tty_port_register(struct tty_port
*port
,
319 struct device
*parent
,
320 struct tty_driver
*drv
, int idx
)
322 return ERR_PTR(-ENODEV
);
324 static inline int serdev_tty_port_unregister(struct tty_port
*port
)
328 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
330 #endif /*_LINUX_SERDEV_H */