1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <[email protected]> 4 */ 5 #ifndef _LINUX_SERDEV_H 6 #define _LINUX_SERDEV_H 7 8 #include <linux/types.h> 9 #include <linux/device.h> 10 #include <linux/termios.h> 11 #include <linux/delay.h> 12 13 struct serdev_controller; 14 struct serdev_device; 15 16 /* 17 * serdev device structures 18 */ 19 20 /** 21 * struct serdev_device_ops - Callback operations for a serdev device 22 * @error: Function called with errors received from device; 23 * may sleep. 24 * @receive_buf: Function called with data received from device; 25 * returns number of bytes accepted; may sleep. 26 * @write_wakeup: Function called when ready to transmit more data; must 27 * not sleep. 28 */ 29 struct serdev_device_ops { 30 void (*error)(struct serdev_device *, unsigned long); 31 int (*receive_buf)(struct serdev_device *, const unsigned char *, size_t); 32 void (*write_wakeup)(struct serdev_device *); 33 }; 34 35 /** 36 * struct serdev_device - Basic representation of an serdev device 37 * @dev: Driver model representation of the device. 38 * @nr: Device number on serdev bus. 39 * @ctrl: serdev controller managing this device. 40 * @ops: Device operations. 41 * @write_comp Completion used by serdev_device_write() internally 42 * @write_lock Lock to serialize access when writing data 43 */ 44 struct serdev_device { 45 struct device dev; 46 int nr; 47 struct serdev_controller *ctrl; 48 const struct serdev_device_ops *ops; 49 struct completion write_comp; 50 struct mutex write_lock; 51 }; 52 53 static inline struct serdev_device *to_serdev_device(struct device *d) 54 { 55 return container_of(d, struct serdev_device, dev); 56 } 57 58 /** 59 * struct serdev_device_driver - serdev slave device driver 60 * @driver: serdev device drivers should initialize name field of this 61 * structure. 62 * @probe: binds this driver to a serdev device. 63 * @remove: unbinds this driver from the serdev device. 64 */ 65 struct serdev_device_driver { 66 struct device_driver driver; 67 int (*probe)(struct serdev_device *); 68 void (*remove)(struct serdev_device *); 69 }; 70 71 static inline struct serdev_device_driver *to_serdev_device_driver(struct device_driver *d) 72 { 73 return container_of(d, struct serdev_device_driver, driver); 74 } 75 76 enum serdev_parity { 77 SERDEV_PARITY_NONE, 78 SERDEV_PARITY_EVEN, 79 SERDEV_PARITY_ODD, 80 }; 81 82 #define SERDEV_ERROR_BREAK 0 83 #define SERDEV_ERROR_FRAME 1 84 #define SERDEV_ERROR_PARITY 2 85 #define SERDEV_ERROR_OVERRUN 3 86 87 /* 88 * serdev controller structures 89 */ 90 struct serdev_controller_ops { 91 int (*write_buf)(struct serdev_controller *, const unsigned char *, size_t); 92 void (*write_flush)(struct serdev_controller *); 93 int (*write_room)(struct serdev_controller *); 94 int (*open)(struct serdev_controller *); 95 void (*close)(struct serdev_controller *); 96 void (*set_error_mask)(struct serdev_controller *, unsigned long); 97 void (*set_flow_control)(struct serdev_controller *, bool); 98 int (*set_parity)(struct serdev_controller *, enum serdev_parity); 99 unsigned int (*set_baudrate)(struct serdev_controller *, unsigned int); 100 void (*wait_until_sent)(struct serdev_controller *, long); 101 int (*get_tiocm)(struct serdev_controller *); 102 int (*set_tiocm)(struct serdev_controller *, unsigned int, unsigned int); 103 }; 104 105 /** 106 * struct serdev_controller - interface to the serdev controller 107 * @dev: Driver model representation of the device. 108 * @nr: number identifier for this controller/bus. 109 * @serdev: Pointer to slave device for this controller. 110 * @ops: Controller operations. 111 */ 112 struct serdev_controller { 113 struct device dev; 114 unsigned int nr; 115 struct serdev_device *serdev; 116 const struct serdev_controller_ops *ops; 117 }; 118 119 static inline struct serdev_controller *to_serdev_controller(struct device *d) 120 { 121 return container_of(d, struct serdev_controller, dev); 122 } 123 124 static inline void *serdev_device_get_drvdata(const struct serdev_device *serdev) 125 { 126 return dev_get_drvdata(&serdev->dev); 127 } 128 129 static inline void serdev_device_set_drvdata(struct serdev_device *serdev, void *data) 130 { 131 dev_set_drvdata(&serdev->dev, data); 132 } 133 134 /** 135 * serdev_device_put() - decrement serdev device refcount 136 * @serdev serdev device. 137 */ 138 static inline void serdev_device_put(struct serdev_device *serdev) 139 { 140 if (serdev) 141 put_device(&serdev->dev); 142 } 143 144 static inline void serdev_device_set_client_ops(struct serdev_device *serdev, 145 const struct serdev_device_ops *ops) 146 { 147 serdev->ops = ops; 148 } 149 150 static inline 151 void *serdev_controller_get_drvdata(const struct serdev_controller *ctrl) 152 { 153 return ctrl ? dev_get_drvdata(&ctrl->dev) : NULL; 154 } 155 156 static inline void serdev_controller_set_drvdata(struct serdev_controller *ctrl, 157 void *data) 158 { 159 dev_set_drvdata(&ctrl->dev, data); 160 } 161 162 /** 163 * serdev_controller_put() - decrement controller refcount 164 * @ctrl serdev controller. 165 */ 166 static inline void serdev_controller_put(struct serdev_controller *ctrl) 167 { 168 if (ctrl) 169 put_device(&ctrl->dev); 170 } 171 172 struct serdev_device *serdev_device_alloc(struct serdev_controller *); 173 int serdev_device_add(struct serdev_device *); 174 void serdev_device_remove(struct serdev_device *); 175 176 struct serdev_controller *serdev_controller_alloc(struct device *, size_t); 177 int serdev_controller_add(struct serdev_controller *); 178 void serdev_controller_remove(struct serdev_controller *); 179 180 static inline void serdev_controller_write_wakeup(struct serdev_controller *ctrl) 181 { 182 struct serdev_device *serdev = ctrl->serdev; 183 184 if (!serdev || !serdev->ops->write_wakeup) 185 return; 186 187 serdev->ops->write_wakeup(serdev); 188 } 189 190 static inline int serdev_controller_receive_buf(struct serdev_controller *ctrl, 191 const unsigned char *data, 192 size_t count) 193 { 194 struct serdev_device *serdev = ctrl->serdev; 195 196 if (!serdev || !serdev->ops->receive_buf) 197 return 0; 198 199 return serdev->ops->receive_buf(serdev, data, count); 200 } 201 202 static inline void serdev_controller_error(struct serdev_controller *ctrl, 203 unsigned long errors) 204 { 205 struct serdev_device *serdev = ctrl->serdev; 206 207 if (!serdev || !serdev->ops->error) 208 return; 209 210 serdev->ops->error(serdev, errors); 211 } 212 213 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS) 214 215 int serdev_device_open(struct serdev_device *); 216 void serdev_device_close(struct serdev_device *); 217 int devm_serdev_device_open(struct device *, struct serdev_device *); 218 unsigned int serdev_device_set_baudrate(struct serdev_device *, unsigned int); 219 void serdev_device_set_error_mask(struct serdev_device *, unsigned long); 220 void serdev_device_set_flow_control(struct serdev_device *, bool); 221 int serdev_device_write_buf(struct serdev_device *, const unsigned char *, size_t); 222 void serdev_device_wait_until_sent(struct serdev_device *, long); 223 int serdev_device_get_tiocm(struct serdev_device *); 224 int serdev_device_set_tiocm(struct serdev_device *, int, int); 225 void serdev_device_write_wakeup(struct serdev_device *); 226 int serdev_device_write(struct serdev_device *, const unsigned char *, size_t, long); 227 void serdev_device_write_flush(struct serdev_device *); 228 int serdev_device_write_room(struct serdev_device *); 229 230 /* 231 * serdev device driver functions 232 */ 233 int __serdev_device_driver_register(struct serdev_device_driver *, struct module *); 234 #define serdev_device_driver_register(sdrv) \ 235 __serdev_device_driver_register(sdrv, THIS_MODULE) 236 237 /** 238 * serdev_device_driver_unregister() - unregister an serdev client driver 239 * @sdrv: the driver to unregister 240 */ 241 static inline void serdev_device_driver_unregister(struct serdev_device_driver *sdrv) 242 { 243 if (sdrv) 244 driver_unregister(&sdrv->driver); 245 } 246 247 #define module_serdev_device_driver(__serdev_device_driver) \ 248 module_driver(__serdev_device_driver, serdev_device_driver_register, \ 249 serdev_device_driver_unregister) 250 251 #else 252 253 static inline int serdev_device_open(struct serdev_device *sdev) 254 { 255 return -ENODEV; 256 } 257 static inline void serdev_device_close(struct serdev_device *sdev) {} 258 static inline unsigned int serdev_device_set_baudrate(struct serdev_device *sdev, unsigned int baudrate) 259 { 260 return 0; 261 } 262 static inline void serdev_device_set_error_mask(struct serdev_device *sdev, unsigned long mask) {} 263 static inline void serdev_device_set_flow_control(struct serdev_device *sdev, bool enable) {} 264 static inline int serdev_device_write_buf(struct serdev_device *serdev, 265 const unsigned char *buf, 266 size_t count) 267 { 268 return -ENODEV; 269 } 270 static inline void serdev_device_wait_until_sent(struct serdev_device *sdev, long timeout) {} 271 static inline int serdev_device_get_tiocm(struct serdev_device *serdev) 272 { 273 return -ENOTSUPP; 274 } 275 static inline int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear) 276 { 277 return -ENOTSUPP; 278 } 279 static inline int serdev_device_write(struct serdev_device *sdev, const unsigned char *buf, 280 size_t count, unsigned long timeout) 281 { 282 return -ENODEV; 283 } 284 static inline void serdev_device_write_flush(struct serdev_device *sdev) {} 285 static inline int serdev_device_write_room(struct serdev_device *sdev) 286 { 287 return 0; 288 } 289 290 #define serdev_device_driver_register(x) 291 #define serdev_device_driver_unregister(x) 292 293 #endif /* CONFIG_SERIAL_DEV_BUS */ 294 295 static inline bool serdev_device_get_cts(struct serdev_device *serdev) 296 { 297 int status = serdev_device_get_tiocm(serdev); 298 return !!(status & TIOCM_CTS); 299 } 300 301 static inline int serdev_device_wait_for_cts(struct serdev_device *serdev, bool state, int timeout_ms) 302 { 303 unsigned long timeout; 304 bool signal; 305 306 timeout = jiffies + msecs_to_jiffies(timeout_ms); 307 while (time_is_after_jiffies(timeout)) { 308 signal = serdev_device_get_cts(serdev); 309 if (signal == state) 310 return 0; 311 usleep_range(1000, 2000); 312 } 313 314 return -ETIMEDOUT; 315 } 316 317 static inline int serdev_device_set_rts(struct serdev_device *serdev, bool enable) 318 { 319 if (enable) 320 return serdev_device_set_tiocm(serdev, TIOCM_RTS, 0); 321 else 322 return serdev_device_set_tiocm(serdev, 0, TIOCM_RTS); 323 } 324 325 int serdev_device_set_parity(struct serdev_device *serdev, 326 enum serdev_parity parity); 327 328 /* 329 * serdev hooks into TTY core 330 */ 331 struct tty_port; 332 struct tty_driver; 333 334 #ifdef CONFIG_SERIAL_DEV_CTRL_TTYPORT 335 struct device *serdev_tty_port_register(struct tty_port *port, 336 struct device *parent, 337 struct tty_driver *drv, int idx); 338 int serdev_tty_port_unregister(struct tty_port *port); 339 #else 340 static inline struct device *serdev_tty_port_register(struct tty_port *port, 341 struct device *parent, 342 struct tty_driver *drv, int idx) 343 { 344 return ERR_PTR(-ENODEV); 345 } 346 static inline int serdev_tty_port_unregister(struct tty_port *port) 347 { 348 return -ENODEV; 349 } 350 #endif /* CONFIG_SERIAL_DEV_CTRL_TTYPORT */ 351 352 struct acpi_resource; 353 struct acpi_resource_uart_serialbus; 354 355 #ifdef CONFIG_ACPI 356 bool serdev_acpi_get_uart_resource(struct acpi_resource *ares, 357 struct acpi_resource_uart_serialbus **uart); 358 #else 359 static inline bool serdev_acpi_get_uart_resource(struct acpi_resource *ares, 360 struct acpi_resource_uart_serialbus **uart) 361 { 362 return false; 363 } 364 #endif /* CONFIG_ACPI */ 365 366 #endif /*_LINUX_SERDEV_H */ 367