xref: /linux-6.15/include/linux/serdev.h (revision b3f80c8f)
1 /*
2  * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <[email protected]>
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>
18 
19 struct serdev_controller;
20 struct serdev_device;
21 
22 /*
23  * serdev device structures
24  */
25 
26 /**
27  * struct serdev_device_ops - Callback operations for a serdev device
28  * @receive_buf:	Function called with data received from device.
29  * @write_wakeup:	Function called when ready to transmit more data.
30  */
31 struct serdev_device_ops {
32 	int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t);
33 	void (*write_wakeup)(struct serdev_device *);
34 };
35 
36 /**
37  * struct serdev_device - Basic representation of an serdev device
38  * @dev:	Driver model representation of the device.
39  * @nr:		Device number on serdev bus.
40  * @ctrl:	serdev controller managing this device.
41  * @ops:	Device operations.
42  */
43 struct serdev_device {
44 	struct device dev;
45 	int nr;
46 	struct serdev_controller *ctrl;
47 	const struct serdev_device_ops *ops;
48 };
49 
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 
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 /*
74  * serdev controller structures
75  */
76 struct serdev_controller_ops {
77 	int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t);
78 	void (*write_flush)(struct serdev_controller *);
79 	int (*write_room)(struct serdev_controller *);
80 	int (*open)(struct serdev_controller *);
81 	void (*close)(struct serdev_controller *);
82 	void (*set_flow_control)(struct serdev_controller *, bool);
83 	unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int);
84 	void (*wait_until_sent)(struct serdev_controller *, long);
85 };
86 
87 /**
88  * struct serdev_controller - interface to the serdev controller
89  * @dev:	Driver model representation of the device.
90  * @nr:		number identifier for this controller/bus.
91  * @serdev:	Pointer to slave device for this controller.
92  * @ops:	Controller operations.
93  */
94 struct serdev_controller {
95 	struct device		dev;
96 	unsigned int		nr;
97 	struct serdev_device	*serdev;
98 	const struct serdev_controller_ops *ops;
99 };
100 
101 static inline struct serdev_controller *to_serdev_controller(struct device *d)
102 {
103 	return container_of(d, struct serdev_controller, dev);
104 }
105 
106 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev)
107 {
108 	return dev_get_drvdata(&serdev->dev);
109 }
110 
111 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data)
112 {
113 	dev_set_drvdata(&serdev->dev, data);
114 }
115 
116 /**
117  * serdev_device_put() - decrement serdev device refcount
118  * @serdev	serdev device.
119  */
120 static inline void serdev_device_put(struct serdev_device *serdev)
121 {
122 	if (serdev)
123 		put_device(&serdev->dev);
124 }
125 
126 static inline void serdev_device_set_client_ops(struct serdev_device *serdev,
127 					      const struct serdev_device_ops *ops)
128 {
129 	serdev->ops = ops;
130 }
131 
132 static inline
133 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl)
134 {
135 	return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL;
136 }
137 
138 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl,
139 					       void *data)
140 {
141 	dev_set_drvdata(&ctrl->dev, data);
142 }
143 
144 /**
145  * serdev_controller_put() - decrement controller refcount
146  * @ctrl	serdev controller.
147  */
148 static inline void serdev_controller_put(struct serdev_controller *ctrl)
149 {
150 	if (ctrl)
151 		put_device(&ctrl->dev);
152 }
153 
154 struct serdev_device *serdev_device_alloc(struct serdev_controller *);
155 int serdev_device_add(struct serdev_device *);
156 void serdev_device_remove(struct serdev_device *);
157 
158 struct serdev_controller *serdev_controller_alloc(struct device *, size_t);
159 int serdev_controller_add(struct serdev_controller *);
160 void serdev_controller_remove(struct serdev_controller *);
161 
162 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl)
163 {
164 	struct serdev_device *serdev = ctrl->serdev;
165 
166 	if (!serdev || !serdev->ops->write_wakeup)
167 		return;
168 
169 	serdev->ops->write_wakeup(ctrl->serdev);
170 }
171 
172 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl,
173 					      const unsigned char *data,
174 					      size_t count)
175 {
176 	struct serdev_device *serdev = ctrl->serdev;
177 
178 	if (!serdev || !serdev->ops->receive_buf)
179 		return -EINVAL;
180 
181 	return serdev->ops->receive_buf(ctrl->serdev, data, count);
182 }
183 
184 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
185 
186 int serdev_device_open(struct serdev_device *);
187 void serdev_device_close(struct serdev_device *);
188 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int);
189 void serdev_device_set_flow_control(struct serdev_device *, bool);
190 void serdev_device_wait_until_sent(struct serdev_device *, long);
191 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t);
192 void serdev_device_write_flush(struct serdev_device *);
193 int serdev_device_write_room(struct serdev_device *);
194 
195 /*
196  * serdev device driver functions
197  */
198 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *);
199 #define serdev_device_driver_register(sdrv) \
200 	__serdev_device_driver_register(sdrv, THIS_MODULE)
201 
202 /**
203  * serdev_device_driver_unregister() - unregister an serdev client driver
204  * @sdrv:	the driver to unregister
205  */
206 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv)
207 {
208 	if (sdrv)
209 		driver_unregister(&sdrv->driver);
210 }
211 
212 #define module_serdev_device_driver(__serdev_device_driver) \
213 	module_driver(__serdev_device_driver, serdev_device_driver_register, \
214 			serdev_device_driver_unregister)
215 
216 #else
217 
218 static inline int serdev_device_open(struct serdev_device *sdev)
219 {
220 	return -ENODEV;
221 }
222 static inline void serdev_device_close(struct serdev_device *sdev) {}
223 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate)
224 {
225 	return 0;
226 }
227 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {}
228 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {}
229 static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count)
230 {
231 	return -ENODEV;
232 }
233 static inline void serdev_device_write_flush(struct serdev_device *sdev) {}
234 static inline int serdev_device_write_room(struct serdev_device *sdev)
235 {
236 	return 0;
237 }
238 
239 #define serdev_device_driver_register(x)
240 #define serdev_device_driver_unregister(x)
241 
242 #endif /* CONFIG_SERIAL_DEV_BUS */
243 
244 /*
245  * serdev hooks into TTY core
246  */
247 struct tty_port;
248 struct tty_driver;
249 
250 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT
251 struct device *serdev_tty_port_register(struct tty_port *port,
252 					struct device *parent,
253 					struct tty_driver *drv, int idx);
254 void serdev_tty_port_unregister(struct tty_port *port);
255 #else
256 static inline struct device *serdev_tty_port_register(struct tty_port *port,
257 					   struct device *parent,
258 					   struct tty_driver *drv, int idx)
259 {
260 	return ERR_PTR(-ENODEV);
261 }
262 static inline void serdev_tty_port_unregister(struct tty_port *port) {}
263 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */
264 
265 #endif /*_LINUX_SERDEV_H */
266