serdev.h 9.8 KB

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