2 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
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.
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.
13 #ifndef _LINUX_SERDEV_H
14 #define _LINUX_SERDEV_H
16 #include <linux/types.h>
17 #include <linux/device.h>
18 #include <linux/termios.h>
19 #include <linux/delay.h>
21 struct serdev_controller;
25 * serdev device structures
29 * struct serdev_device_ops - Callback operations for a serdev device
30 * @receive_buf: Function called with data received from device.
31 * @write_wakeup: Function called when ready to transmit more data.
33 struct serdev_device_ops {
34 int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
35 void (*write_wakeup)(struct serdev_device *);
39 * struct serdev_device - Basic representation of an serdev device
40 * @dev: Driver model representation of the device.
41 * @nr: Device number on serdev bus.
42 * @ctrl: serdev controller managing this device.
43 * @ops: Device operations.
44 * @write_comp Completion used by serdev_device_write() internally
45 * @write_lock Lock to serialize access when writing data
47 struct serdev_device {
50 struct serdev_controller *ctrl;
51 const struct serdev_device_ops *ops;
52 struct completion write_comp;
53 struct mutex write_lock;
56 static inline struct serdev_device *to_serdev_device(struct device *d)
58 return container_of(d, struct serdev_device, dev);
62 * struct serdev_device_driver - serdev slave device driver
63 * @driver: serdev device drivers should initialize name field of this
65 * @probe: binds this driver to a serdev device.
66 * @remove: unbinds this driver from the serdev device.
68 struct serdev_device_driver {
69 struct device_driver driver;
70 int (*probe)(struct serdev_device *);
71 void (*remove)(struct serdev_device *);
74 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
76 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 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
90 void (*wait_until_sent)(struct serdev_controller *, long);
91 int (*get_tiocm)(struct serdev_controller *);
92 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
96 * struct serdev_controller - interface to the serdev controller
97 * @dev: Driver model representation of the device.
98 * @nr: number identifier for this controller/bus.
99 * @serdev: Pointer to slave device for this controller.
100 * @ops: Controller operations.
102 struct serdev_controller {
105 struct serdev_device *serdev;
106 const struct serdev_controller_ops *ops;
109 static inline struct serdev_controller *to_serdev_controller(struct device *d)
111 return container_of(d, struct serdev_controller, dev);
114 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
116 return dev_get_drvdata(&serdev->dev);
119 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
121 dev_set_drvdata(&serdev->dev, data);
125 * serdev_device_put() - decrement serdev device refcount
126 * @serdev serdev device.
128 static inline void serdev_device_put(struct serdev_device *serdev)
131 put_device(&serdev->dev);
134 static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
135 const struct serdev_device_ops *ops)
141 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
143 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
146 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
149 dev_set_drvdata(&ctrl->dev, data);
153 * serdev_controller_put() - decrement controller refcount
154 * @ctrl serdev controller.
156 static inline void serdev_controller_put(struct serdev_controller *ctrl)
159 put_device(&ctrl->dev);
162 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
163 int serdev_device_add(struct serdev_device *);
164 void serdev_device_remove(struct serdev_device *);
166 struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
167 int serdev_controller_add(struct serdev_controller *);
168 void serdev_controller_remove(struct serdev_controller *);
170 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
172 struct serdev_device *serdev = ctrl->serdev;
174 if (!serdev || !serdev->ops->write_wakeup)
177 serdev->ops->write_wakeup(serdev);
180 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
181 const unsigned char *data,
184 struct serdev_device *serdev = ctrl->serdev;
186 if (!serdev || !serdev->ops->receive_buf)
189 return serdev->ops->receive_buf(serdev, data, count);
192 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
194 int serdev_device_open(struct serdev_device *);
195 void serdev_device_close(struct serdev_device *);
196 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
197 void serdev_device_set_flow_control(struct serdev_device *, bool);
198 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
199 void serdev_device_wait_until_sent(struct serdev_device *, long);
200 int serdev_device_get_tiocm(struct serdev_device *);
201 int serdev_device_set_tiocm(struct serdev_device *, int, int);
202 void serdev_device_write_wakeup(struct serdev_device *);
203 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
204 void serdev_device_write_flush(struct serdev_device *);
205 int serdev_device_write_room(struct serdev_device *);
208 * serdev device driver functions
210 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
211 #define serdev_device_driver_register(sdrv) \
212 __serdev_device_driver_register(sdrv, THIS_MODULE)
215 * serdev_device_driver_unregister() - unregister an serdev client driver
216 * @sdrv: the driver to unregister
218 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
221 driver_unregister(&sdrv->driver);
224 #define module_serdev_device_driver(__serdev_device_driver) \
225 module_driver(__serdev_device_driver, serdev_device_driver_register, \
226 serdev_device_driver_unregister)
230 static inline int serdev_device_open(struct serdev_device *sdev)
234 static inline void serdev_device_close(struct serdev_device *sdev) {}
235 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
239 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
240 static inline int serdev_device_write_buf(struct serdev_device *serdev,
241 const unsigned char *buf,
246 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
247 static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
251 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
255 static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
256 size_t count, unsigned long timeout)
260 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
261 static inline int serdev_device_write_room(struct serdev_device *sdev)
266 #define serdev_device_driver_register(x)
267 #define serdev_device_driver_unregister(x)
269 #endif /* CONFIG_SERIAL_DEV_BUS */
271 static inline bool serdev_device_get_cts(struct serdev_device *serdev)
273 int status = serdev_device_get_tiocm(serdev);
274 return !!(status & TIOCM_CTS);
277 static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
279 unsigned long timeout;
282 timeout = jiffies + msecs_to_jiffies(timeout_ms);
283 while (time_is_after_jiffies(timeout)) {
284 signal = serdev_device_get_cts(serdev);
287 usleep_range(1000, 2000);
293 static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
296 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
298 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
302 * serdev hooks into TTY core
307 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
308 struct device *serdev_tty_port_register(struct tty_port *port,
309 struct device *parent,
310 struct tty_driver *drv, int idx);
311 int serdev_tty_port_unregister(struct tty_port *port);
313 static inline struct device *serdev_tty_port_register(struct tty_port *port,
314 struct device *parent,
315 struct tty_driver *drv, int idx)
317 return ERR_PTR(-ENODEV);
319 static inline int serdev_tty_port_unregister(struct tty_port *port)
323 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
325 #endif /*_LINUX_SERDEV_H */