Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 1 | /* |
| 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 Reichel | 5659dab | 2017-03-28 17:59:32 +0200 | [diff] [blame] | 18 | #include <linux/termios.h> |
Sebastian Reichel | 756db77 | 2017-03-28 17:59:33 +0200 | [diff] [blame^] | 19 | #include <linux/delay.h> |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 20 | |
| 21 | struct serdev_controller; |
| 22 | struct serdev_device; |
| 23 | |
| 24 | /* |
| 25 | * serdev device structures |
| 26 | */ |
| 27 | |
| 28 | /** |
| 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. |
| 32 | */ |
| 33 | struct serdev_device_ops { |
| 34 | int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t); |
| 35 | void (*write_wakeup)(struct serdev_device *); |
| 36 | }; |
| 37 | |
| 38 | /** |
| 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 | */ |
| 45 | struct serdev_device { |
| 46 | struct device dev; |
| 47 | int nr; |
| 48 | struct serdev_controller *ctrl; |
| 49 | const struct serdev_device_ops *ops; |
| 50 | }; |
| 51 | |
| 52 | static inline struct serdev_device *to_serdev_device(struct device *d) |
| 53 | { |
| 54 | return container_of(d, struct serdev_device, dev); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * struct serdev_device_driver - serdev slave device driver |
| 59 | * @driver: serdev device drivers should initialize name field of this |
| 60 | * structure. |
| 61 | * @probe: binds this driver to a serdev device. |
| 62 | * @remove: unbinds this driver from the serdev device. |
| 63 | */ |
| 64 | struct serdev_device_driver { |
| 65 | struct device_driver driver; |
| 66 | int (*probe)(struct serdev_device *); |
| 67 | void (*remove)(struct serdev_device *); |
| 68 | }; |
| 69 | |
| 70 | static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d) |
| 71 | { |
| 72 | return container_of(d, struct serdev_device_driver, driver); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * serdev controller structures |
| 77 | */ |
| 78 | struct serdev_controller_ops { |
| 79 | int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t); |
| 80 | void (*write_flush)(struct serdev_controller *); |
| 81 | int (*write_room)(struct serdev_controller *); |
| 82 | int (*open)(struct serdev_controller *); |
| 83 | void (*close)(struct serdev_controller *); |
| 84 | void (*set_flow_control)(struct serdev_controller *, bool); |
| 85 | unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int); |
Sebastian Reichel | b3f80c8 | 2017-03-28 17:59:31 +0200 | [diff] [blame] | 86 | void (*wait_until_sent)(struct serdev_controller *, long); |
Sebastian Reichel | 5659dab | 2017-03-28 17:59:32 +0200 | [diff] [blame] | 87 | int (*get_tiocm)(struct serdev_controller *); |
| 88 | int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int); |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * struct serdev_controller - interface to the serdev controller |
| 93 | * @dev: Driver model representation of the device. |
| 94 | * @nr: number identifier for this controller/bus. |
| 95 | * @serdev: Pointer to slave device for this controller. |
| 96 | * @ops: Controller operations. |
| 97 | */ |
| 98 | struct serdev_controller { |
| 99 | struct device dev; |
| 100 | unsigned int nr; |
| 101 | struct serdev_device *serdev; |
| 102 | const struct serdev_controller_ops *ops; |
| 103 | }; |
| 104 | |
| 105 | static inline struct serdev_controller *to_serdev_controller(struct device *d) |
| 106 | { |
| 107 | return container_of(d, struct serdev_controller, dev); |
| 108 | } |
| 109 | |
| 110 | static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev) |
| 111 | { |
| 112 | return dev_get_drvdata(&serdev->dev); |
| 113 | } |
| 114 | |
| 115 | static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data) |
| 116 | { |
| 117 | dev_set_drvdata(&serdev->dev, data); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * serdev_device_put() - decrement serdev device refcount |
| 122 | * @serdev serdev device. |
| 123 | */ |
| 124 | static inline void serdev_device_put(struct serdev_device *serdev) |
| 125 | { |
| 126 | if (serdev) |
| 127 | put_device(&serdev->dev); |
| 128 | } |
| 129 | |
| 130 | static inline void serdev_device_set_client_ops(struct serdev_device *serdev, |
| 131 | const struct serdev_device_ops *ops) |
| 132 | { |
| 133 | serdev->ops = ops; |
| 134 | } |
| 135 | |
| 136 | static inline |
| 137 | void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl) |
| 138 | { |
| 139 | return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL; |
| 140 | } |
| 141 | |
| 142 | static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl, |
| 143 | void *data) |
| 144 | { |
| 145 | dev_set_drvdata(&ctrl->dev, data); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * serdev_controller_put() - decrement controller refcount |
| 150 | * @ctrl serdev controller. |
| 151 | */ |
| 152 | static inline void serdev_controller_put(struct serdev_controller *ctrl) |
| 153 | { |
| 154 | if (ctrl) |
| 155 | put_device(&ctrl->dev); |
| 156 | } |
| 157 | |
| 158 | struct serdev_device *serdev_device_alloc(struct serdev_controller *); |
| 159 | int serdev_device_add(struct serdev_device *); |
| 160 | void serdev_device_remove(struct serdev_device *); |
| 161 | |
| 162 | struct serdev_controller *serdev_controller_alloc(struct device *, size_t); |
| 163 | int serdev_controller_add(struct serdev_controller *); |
| 164 | void serdev_controller_remove(struct serdev_controller *); |
| 165 | |
| 166 | static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl) |
| 167 | { |
| 168 | struct serdev_device *serdev = ctrl->serdev; |
| 169 | |
| 170 | if (!serdev || !serdev->ops->write_wakeup) |
| 171 | return; |
| 172 | |
| 173 | serdev->ops->write_wakeup(ctrl->serdev); |
| 174 | } |
| 175 | |
| 176 | static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl, |
| 177 | const unsigned char *data, |
| 178 | size_t count) |
| 179 | { |
| 180 | struct serdev_device *serdev = ctrl->serdev; |
| 181 | |
| 182 | if (!serdev || !serdev->ops->receive_buf) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | return serdev->ops->receive_buf(ctrl->serdev, data, count); |
| 186 | } |
| 187 | |
| 188 | #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) |
| 189 | |
| 190 | int serdev_device_open(struct serdev_device *); |
| 191 | void serdev_device_close(struct serdev_device *); |
| 192 | unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int); |
| 193 | void serdev_device_set_flow_control(struct serdev_device *, bool); |
Sebastian Reichel | b3f80c8 | 2017-03-28 17:59:31 +0200 | [diff] [blame] | 194 | void serdev_device_wait_until_sent(struct serdev_device *, long); |
Sebastian Reichel | 5659dab | 2017-03-28 17:59:32 +0200 | [diff] [blame] | 195 | int serdev_device_get_tiocm(struct serdev_device *); |
| 196 | int serdev_device_set_tiocm(struct serdev_device *, int, int); |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 197 | int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t); |
| 198 | void serdev_device_write_flush(struct serdev_device *); |
| 199 | int serdev_device_write_room(struct serdev_device *); |
| 200 | |
| 201 | /* |
| 202 | * serdev device driver functions |
| 203 | */ |
| 204 | int __serdev_device_driver_register(struct serdev_device_driver *, struct module *); |
| 205 | #define serdev_device_driver_register(sdrv) \ |
| 206 | __serdev_device_driver_register(sdrv, THIS_MODULE) |
| 207 | |
| 208 | /** |
| 209 | * serdev_device_driver_unregister() - unregister an serdev client driver |
| 210 | * @sdrv: the driver to unregister |
| 211 | */ |
| 212 | static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv) |
| 213 | { |
| 214 | if (sdrv) |
| 215 | driver_unregister(&sdrv->driver); |
| 216 | } |
| 217 | |
| 218 | #define module_serdev_device_driver(__serdev_device_driver) \ |
| 219 | module_driver(__serdev_device_driver, serdev_device_driver_register, \ |
| 220 | serdev_device_driver_unregister) |
| 221 | |
| 222 | #else |
| 223 | |
| 224 | static inline int serdev_device_open(struct serdev_device *sdev) |
| 225 | { |
| 226 | return -ENODEV; |
| 227 | } |
| 228 | static inline void serdev_device_close(struct serdev_device *sdev) {} |
| 229 | static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate) |
| 230 | { |
| 231 | return 0; |
| 232 | } |
| 233 | static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {} |
Sebastian Reichel | b3f80c8 | 2017-03-28 17:59:31 +0200 | [diff] [blame] | 234 | static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {} |
Sebastian Reichel | 5659dab | 2017-03-28 17:59:32 +0200 | [diff] [blame] | 235 | static inline int serdev_device_get_tiocm(struct serdev_device *serdev) |
| 236 | { |
| 237 | return -ENOTSUPP; |
| 238 | } |
| 239 | static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) |
| 240 | { |
| 241 | return -ENOTSUPP; |
| 242 | } |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 243 | static inline int serdev_device_write_buf(struct serdev_device *sdev, const unsigned char *buf, size_t count) |
| 244 | { |
| 245 | return -ENODEV; |
| 246 | } |
| 247 | static inline void serdev_device_write_flush(struct serdev_device *sdev) {} |
| 248 | static inline int serdev_device_write_room(struct serdev_device *sdev) |
| 249 | { |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | #define serdev_device_driver_register(x) |
| 254 | #define serdev_device_driver_unregister(x) |
| 255 | |
| 256 | #endif /* CONFIG_SERIAL_DEV_BUS */ |
| 257 | |
Sebastian Reichel | 756db77 | 2017-03-28 17:59:33 +0200 | [diff] [blame^] | 258 | static inline bool serdev_device_get_cts(struct serdev_device *serdev) |
| 259 | { |
| 260 | int status = serdev_device_get_tiocm(serdev); |
| 261 | return !!(status & TIOCM_CTS); |
| 262 | } |
| 263 | |
| 264 | static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms) |
| 265 | { |
| 266 | unsigned long timeout; |
| 267 | bool signal; |
| 268 | |
| 269 | timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 270 | while (time_is_after_jiffies(timeout)) { |
| 271 | signal = serdev_device_get_cts(serdev); |
| 272 | if (signal == state) |
| 273 | return 0; |
| 274 | usleep_range(1000, 2000); |
| 275 | } |
| 276 | |
| 277 | return -ETIMEDOUT; |
| 278 | } |
| 279 | |
| 280 | static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable) |
| 281 | { |
| 282 | if (enable) |
| 283 | return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0); |
| 284 | else |
| 285 | return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS); |
| 286 | } |
| 287 | |
Rob Herring | bed35c6 | 2017-02-02 13:48:08 -0600 | [diff] [blame] | 288 | /* |
| 289 | * serdev hooks into TTY core |
| 290 | */ |
| 291 | struct tty_port; |
| 292 | struct tty_driver; |
| 293 | |
| 294 | #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT |
| 295 | struct device *serdev_tty_port_register(struct tty_port *port, |
| 296 | struct device *parent, |
| 297 | struct tty_driver *drv, int idx); |
| 298 | void serdev_tty_port_unregister(struct tty_port *port); |
| 299 | #else |
| 300 | static inline struct device *serdev_tty_port_register(struct tty_port *port, |
| 301 | struct device *parent, |
| 302 | struct tty_driver *drv, int idx) |
| 303 | { |
| 304 | return ERR_PTR(-ENODEV); |
| 305 | } |
| 306 | static inline void serdev_tty_port_unregister(struct tty_port *port) {} |
| 307 | #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */ |
| 308 | |
Rob Herring | cd6484e | 2017-02-02 13:48:07 -0600 | [diff] [blame] | 309 | #endif /*_LINUX_SERDEV_H */ |