blob: e29a270f603c20fb5ba6de6927afbcf0c17300d0 [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>
Rob Herringcd6484e2017-02-02 13:48:07 -060019
20struct serdev_controller;
21struct serdev_device;
22
23/*
24 * serdev device structures
25 */
26
27/**
28 * struct serdev_device_ops - Callback operations for a serdev device
29 * @receive_buf: Function called with data received from device.
30 * @write_wakeup: Function called when ready to transmit more data.
31 */
32struct serdev_device_ops {
33 int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
34 void (*write_wakeup)(struct serdev_device *);
35};
36
37/**
38 * struct serdev_device - Basic representation of an serdev device
39 * @dev: Driver model representation of the device.
40 * @nr: Device number on serdev bus.
41 * @ctrl: serdev controller managing this device.
42 * @ops: Device operations.
43 */
44struct serdev_device {
45 struct device dev;
46 int nr;
47 struct serdev_controller *ctrl;
48 const struct serdev_device_ops *ops;
49};
50
51static inline struct serdev_device *to_serdev_device(struct device *d)
52{
53 return container_of(d, struct serdev_device, dev);
54}
55
56/**
57 * struct serdev_device_driver - serdev slave device driver
58 * @driver: serdev device drivers should initialize name field of this
59 * structure.
60 * @probe: binds this driver to a serdev device.
61 * @remove: unbinds this driver from the serdev device.
62 */
63struct serdev_device_driver {
64 struct device_driver driver;
65 int (*probe)(struct serdev_device *);
66 void (*remove)(struct serdev_device *);
67};
68
69static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d)
70{
71 return container_of(d, struct serdev_device_driver, driver);
72}
73
74/*
75 * serdev controller structures
76 */
77struct serdev_controller_ops {
78 int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
79 void (*write_flush)(struct serdev_controller *);
80 int (*write_room)(struct serdev_controller *);
81 int (*open)(struct serdev_controller *);
82 void (*close)(struct serdev_controller *);
83 void (*set_flow_control)(struct serdev_controller *, bool);
84 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
Sebastian Reichelb3f80c82017-03-28 17:59:31 +020085 void (*wait_until_sent)(struct serdev_controller *, long);
Sebastian Reichel5659dab2017-03-28 17:59:32 +020086 int (*get_tiocm)(struct serdev_controller *);
87 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int);
Rob Herringcd6484e2017-02-02 13:48:07 -060088};
89
90/**
91 * struct serdev_controller - interface to the serdev controller
92 * @dev: Driver model representation of the device.
93 * @nr: number identifier for this controller/bus.
94 * @serdev: Pointer to slave device for this controller.
95 * @ops: Controller operations.
96 */
97struct serdev_controller {
98 struct device dev;
99 unsigned int nr;
100 struct serdev_device *serdev;
101 const struct serdev_controller_ops *ops;
102};
103
104static inline struct serdev_controller *to_serdev_controller(struct device *d)
105{
106 return container_of(d, struct serdev_controller, dev);
107}
108
109static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
110{
111 return dev_get_drvdata(&serdev->dev);
112}
113
114static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
115{
116 dev_set_drvdata(&serdev->dev, data);
117}
118
119/**
120 * serdev_device_put() - decrement serdev device refcount
121 * @serdev serdev device.
122 */
123static inline void serdev_device_put(struct serdev_device *serdev)
124{
125 if (serdev)
126 put_device(&serdev->dev);
127}
128
129static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
130 const struct serdev_device_ops *ops)
131{
132 serdev->ops = ops;
133}
134
135static inline
136void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
137{
138 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
139}
140
141static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
142 void *data)
143{
144 dev_set_drvdata(&ctrl->dev, data);
145}
146
147/**
148 * serdev_controller_put() - decrement controller refcount
149 * @ctrl serdev controller.
150 */
151static inline void serdev_controller_put(struct serdev_controller *ctrl)
152{
153 if (ctrl)
154 put_device(&ctrl->dev);
155}
156
157struct serdev_device *serdev_device_alloc(struct serdev_controller *);
158int serdev_device_add(struct serdev_device *);
159void serdev_device_remove(struct serdev_device *);
160
161struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
162int serdev_controller_add(struct serdev_controller *);
163void serdev_controller_remove(struct serdev_controller *);
164
165static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
166{
167 struct serdev_device *serdev = ctrl->serdev;
168
169 if (!serdev || !serdev->ops->write_wakeup)
170 return;
171
172 serdev->ops->write_wakeup(ctrl->serdev);
173}
174
175static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
176 const unsigned char *data,
177 size_t count)
178{
179 struct serdev_device *serdev = ctrl->serdev;
180
181 if (!serdev || !serdev->ops->receive_buf)
182 return -EINVAL;
183
184 return serdev->ops->receive_buf(ctrl->serdev, data, count);
185}
186
187#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
188
189int serdev_device_open(struct serdev_device *);
190void serdev_device_close(struct serdev_device *);
191unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
192void serdev_device_set_flow_control(struct serdev_device *, bool);
Sebastian Reichelb3f80c82017-03-28 17:59:31 +0200193void serdev_device_wait_until_sent(struct serdev_device *, long);
Sebastian Reichel5659dab2017-03-28 17:59:32 +0200194int serdev_device_get_tiocm(struct serdev_device *);
195int serdev_device_set_tiocm(struct serdev_device *, int, int);
Rob Herringcd6484e2017-02-02 13:48:07 -0600196int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
197void serdev_device_write_flush(struct serdev_device *);
198int serdev_device_write_room(struct serdev_device *);
199
200/*
201 * serdev device driver functions
202 */
203int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
204#define serdev_device_driver_register(sdrv) \
205 __serdev_device_driver_register(sdrv, THIS_MODULE)
206
207/**
208 * serdev_device_driver_unregister() - unregister an serdev client driver
209 * @sdrv: the driver to unregister
210 */
211static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
212{
213 if (sdrv)
214 driver_unregister(&sdrv->driver);
215}
216
217#define module_serdev_device_driver(__serdev_device_driver) \
218 module_driver(__serdev_device_driver, serdev_device_driver_register, \
219 serdev_device_driver_unregister)
220
221#else
222
223static inline int serdev_device_open(struct serdev_device *sdev)
224{
225 return -ENODEV;
226}
227static inline void serdev_device_close(struct serdev_device *sdev) {}
228static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
229{
230 return 0;
231}
232static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
Sebastian Reichelb3f80c82017-03-28 17:59:31 +0200233static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
Sebastian Reichel5659dab2017-03-28 17:59:32 +0200234static inline int serdev_device_get_tiocm(struct serdev_device *serdev)
235{
236 return -ENOTSUPP;
237}
238static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
239{
240 return -ENOTSUPP;
241}
Rob Herringcd6484e2017-02-02 13:48:07 -0600242static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count)
243{
244 return -ENODEV;
245}
246static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
247static inline int serdev_device_write_room(struct serdev_device *sdev)
248{
249 return 0;
250}
251
252#define serdev_device_driver_register(x)
253#define serdev_device_driver_unregister(x)
254
255#endif /* CONFIG_SERIAL_DEV_BUS */
256
Rob Herringbed35c62017-02-02 13:48:08 -0600257/*
258 * serdev hooks into TTY core
259 */
260struct tty_port;
261struct tty_driver;
262
263#ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
264struct device *serdev_tty_port_register(struct tty_port *port,
265 struct device *parent,
266 struct tty_driver *drv, int idx);
267void serdev_tty_port_unregister(struct tty_port *port);
268#else
269static inline struct device *serdev_tty_port_register(struct tty_port *port,
270 struct device *parent,
271 struct tty_driver *drv, int idx)
272{
273 return ERR_PTR(-ENODEV);
274}
275static inline void serdev_tty_port_unregister(struct tty_port *port) {}
276#endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
277
Rob Herringcd6484e2017-02-02 13:48:07 -0600278#endif /*_LINUX_SERDEV_H */