blob: d7a9713e98832e1d943188dab5f32591ada3b837 [file] [log] [blame]
Rob Herringcd6484e2017-02-02 13:48:07 -06001/*
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>
Sebastian Reichel5659dab2017-03-28 17:59:32 +020018#include <linux/termios.h>
Sebastian Reichel756db772017-03-28 17:59:33 +020019#include <linux/delay.h>
Rob Herringcd6484e2017-02-02 13:48:07 -060020
21struct serdev_controller;
22struct serdev_device;
23
24/*
25 * serdev device structures
26 */
27
28/**
29 * struct serdev_device_ops - Callback operations for a serdev device
Johan Hovold10f9e032017-11-03 15:30:54 +010030 * @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.
Rob Herringcd6484e2017-02-02 13:48:07 -060034 */
35struct 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.
Andrey Smirnov6fe729c2017-04-04 07:22:33 -070046 * @write_comp Completion used by serdev_device_write() internally
47 * @write_lock Lock to serialize access when writing data
Rob Herringcd6484e2017-02-02 13:48:07 -060048 */
49struct serdev_device {
50 struct device dev;
51 int nr;
52 struct serdev_controller *ctrl;
53 const struct serdev_device_ops *ops;
Andrey Smirnov6fe729c2017-04-04 07:22:33 -070054 struct completion write_comp;
55 struct mutex write_lock;
Rob Herringcd6484e2017-02-02 13:48:07 -060056};
57
58static 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 */
70struct serdev_device_driver {
71 struct device_driver driver;
72 int (*probe)(struct serdev_device *);
73 void (*remove)(struct serdev_device *);
74};
75
76static 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/*
82 * serdev controller structures
83 */
84struct serdev_controller_ops {
85 int (*write_buf)(struct serdev_controller *, const unsigned char *, 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 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
Sebastian Reichelb3f80c82017-03-28 17:59:31 +020092 void (*wait_until_sent)(struct serdev_controller *, long);
Sebastian Reichel5659dab2017-03-28 17:59:32 +020093 int (*get_tiocm)(struct serdev_controller *);
94 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
Rob Herringcd6484e2017-02-02 13:48:07 -060095};
96
97/**
98 * struct serdev_controller - interface to the serdev controller
99 * @dev: Driver model representation of the device.
100 * @nr: number identifier for this controller/bus.
101 * @serdev: Pointer to slave device for this controller.
102 * @ops: Controller operations.
103 */
104struct serdev_controller {
105 struct device dev;
106 unsigned int nr;
107 struct serdev_device *serdev;
108 const struct serdev_controller_ops *ops;
109};
110
111static inline struct serdev_controller *to_serdev_controller(struct device *d)
112{
113 return container_of(d, struct serdev_controller, dev);
114}
115
116static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
117{
118 return dev_get_drvdata(&serdev->dev);
119}
120
121static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
122{
123 dev_set_drvdata(&serdev->dev, data);
124}
125
126/**
127 * serdev_device_put() - decrement serdev device refcount
128 * @serdev serdev device.
129 */
130static inline void serdev_device_put(struct serdev_device *serdev)
131{
132 if (serdev)
133 put_device(&serdev->dev);
134}
135
136static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
137 const struct serdev_device_ops *ops)
138{
139 serdev->ops = ops;
140}
141
142static inline
143void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
144{
145 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
146}
147
148static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
149 void *data)
150{
151 dev_set_drvdata(&ctrl->dev, data);
152}
153
154/**
155 * serdev_controller_put() - decrement controller refcount
156 * @ctrl serdev controller.
157 */
158static inline void serdev_controller_put(struct serdev_controller *ctrl)
159{
160 if (ctrl)
161 put_device(&ctrl->dev);
162}
163
164struct serdev_device *serdev_device_alloc(struct serdev_controller *);
165int serdev_device_add(struct serdev_device *);
166void serdev_device_remove(struct serdev_device *);
167
168struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
169int serdev_controller_add(struct serdev_controller *);
170void serdev_controller_remove(struct serdev_controller *);
171
172static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
173{
174 struct serdev_device *serdev = ctrl->serdev;
175
176 if (!serdev || !serdev->ops->write_wakeup)
177 return;
178
Andrey Smirnov9d1d9942017-03-16 08:14:16 -0700179 serdev->ops->write_wakeup(serdev);
Rob Herringcd6484e2017-02-02 13:48:07 -0600180}
181
182static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
183 const unsigned char *data,
184 size_t count)
185{
186 struct serdev_device *serdev = ctrl->serdev;
187
188 if (!serdev || !serdev->ops->receive_buf)
189 return -EINVAL;
190
Andrey Smirnov9d1d9942017-03-16 08:14:16 -0700191 return serdev->ops->receive_buf(serdev, data, count);
Rob Herringcd6484e2017-02-02 13:48:07 -0600192}
193
194#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
195
196int serdev_device_open(struct serdev_device *);
197void serdev_device_close(struct serdev_device *);
Andrey Smirnov525ba622017-11-09 08:05:53 -0800198int devm_serdev_device_open(struct device *, struct serdev_device *);
Rob Herringcd6484e2017-02-02 13:48:07 -0600199unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
200void serdev_device_set_flow_control(struct serdev_device *, bool);
Stefan Wahren6bdc00d2017-04-28 13:47:21 +0200201int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
Sebastian Reichelb3f80c82017-03-28 17:59:31 +0200202void serdev_device_wait_until_sent(struct serdev_device *, long);
Sebastian Reichel5659dab2017-03-28 17:59:32 +0200203int serdev_device_get_tiocm(struct serdev_device *);
204int serdev_device_set_tiocm(struct serdev_device *, int, int);
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700205void serdev_device_write_wakeup(struct serdev_device *);
206int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, unsigned long);
Rob Herringcd6484e2017-02-02 13:48:07 -0600207void serdev_device_write_flush(struct serdev_device *);
208int serdev_device_write_room(struct serdev_device *);
209
210/*
211 * serdev device driver functions
212 */
213int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
214#define serdev_device_driver_register(sdrv) \
215 __serdev_device_driver_register(sdrv, THIS_MODULE)
216
217/**
218 * serdev_device_driver_unregister() - unregister an serdev client driver
219 * @sdrv: the driver to unregister
220 */
221static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
222{
223 if (sdrv)
224 driver_unregister(&sdrv->driver);
225}
226
227#define module_serdev_device_driver(__serdev_device_driver) \
228 module_driver(__serdev_device_driver, serdev_device_driver_register, \
229 serdev_device_driver_unregister)
230
231#else
232
233static inline int serdev_device_open(struct serdev_device *sdev)
234{
235 return -ENODEV;
236}
237static inline void serdev_device_close(struct serdev_device *sdev) {}
238static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
239{
240 return 0;
241}
242static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
Stefan Wahren6bdc00d2017-04-28 13:47:21 +0200243static inline int serdev_device_write_buf(struct serdev_device *serdev,
244 const unsigned char *buf,
245 size_t count)
246{
247 return -ENODEV;
248}
Sebastian Reichelb3f80c82017-03-28 17:59:31 +0200249static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
Sebastian Reichel5659dab2017-03-28 17:59:32 +0200250static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
251{
252 return -ENOTSUPP;
253}
254static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
255{
256 return -ENOTSUPP;
257}
Andrey Smirnov6fe729c2017-04-04 07:22:33 -0700258static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf,
259 size_t count, unsigned long timeout)
Rob Herringcd6484e2017-02-02 13:48:07 -0600260{
261 return -ENODEV;
262}
263static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
264static inline int serdev_device_write_room(struct serdev_device *sdev)
265{
266 return 0;
267}
268
269#define serdev_device_driver_register(x)
270#define serdev_device_driver_unregister(x)
271
272#endif /* CONFIG_SERIAL_DEV_BUS */
273
Sebastian Reichel756db772017-03-28 17:59:33 +0200274static inline bool serdev_device_get_cts(struct serdev_device *serdev)
275{
276 int status = serdev_device_get_tiocm(serdev);
277 return !!(status & TIOCM_CTS);
278}
279
280static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms)
281{
282 unsigned long timeout;
283 bool signal;
284
285 timeout = jiffies + msecs_to_jiffies(timeout_ms);
286 while (time_is_after_jiffies(timeout)) {
287 signal = serdev_device_get_cts(serdev);
288 if (signal == state)
289 return 0;
290 usleep_range(1000, 2000);
291 }
292
293 return -ETIMEDOUT;
294}
295
296static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable)
297{
298 if (enable)
299 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0);
300 else
301 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS);
302}
303
Rob Herringbed35c62017-02-02 13:48:08 -0600304/*
305 * serdev hooks into TTY core
306 */
307struct tty_port;
308struct tty_driver;
309
310#ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
311struct device *serdev_tty_port_register(struct tty_port *port,
312 struct device *parent,
313 struct tty_driver *drv, int idx);
Johan Hovold8cde11b2017-05-18 17:33:00 +0200314int serdev_tty_port_unregister(struct tty_port *port);
Rob Herringbed35c62017-02-02 13:48:08 -0600315#else
316static inline struct device *serdev_tty_port_register(struct tty_port *port,
317 struct device *parent,
318 struct tty_driver *drv, int idx)
319{
320 return ERR_PTR(-ENODEV);
321}
Johan Hovold8cde11b2017-05-18 17:33:00 +0200322static inline int serdev_tty_port_unregister(struct tty_port *port)
323{
324 return -ENODEV;
325}
Rob Herringbed35c62017-02-02 13:48:08 -0600326#endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
327
Rob Herringcd6484e2017-02-02 13:48:07 -0600328#endif /*_LINUX_SERDEV_H */