hsi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * HSI core header file.
  4. *
  5. * Copyright (C) 2010 Nokia Corporation. All rights reserved.
  6. *
  7. * Contact: Carlos Chinea <carlos.chinea@nokia.com>
  8. */
  9. #ifndef __LINUX_HSI_H__
  10. #define __LINUX_HSI_H__
  11. #include <linux/device.h>
  12. #include <linux/mutex.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/notifier.h>
  17. /* HSI message ttype */
  18. #define HSI_MSG_READ 0
  19. #define HSI_MSG_WRITE 1
  20. /* HSI configuration values */
  21. enum {
  22. HSI_MODE_STREAM = 1,
  23. HSI_MODE_FRAME,
  24. };
  25. enum {
  26. HSI_FLOW_SYNC, /* Synchronized flow */
  27. HSI_FLOW_PIPE, /* Pipelined flow */
  28. };
  29. enum {
  30. HSI_ARB_RR, /* Round-robin arbitration */
  31. HSI_ARB_PRIO, /* Channel priority arbitration */
  32. };
  33. #define HSI_MAX_CHANNELS 16
  34. /* HSI message status codes */
  35. enum {
  36. HSI_STATUS_COMPLETED, /* Message transfer is completed */
  37. HSI_STATUS_PENDING, /* Message pending to be read/write (POLL) */
  38. HSI_STATUS_PROCEEDING, /* Message transfer is ongoing */
  39. HSI_STATUS_QUEUED, /* Message waiting to be served */
  40. HSI_STATUS_ERROR, /* Error when message transfer was ongoing */
  41. };
  42. /* HSI port event codes */
  43. enum {
  44. HSI_EVENT_START_RX,
  45. HSI_EVENT_STOP_RX,
  46. };
  47. /**
  48. * struct hsi_channel - channel resource used by the hsi clients
  49. * @id: Channel number
  50. * @name: Channel name
  51. */
  52. struct hsi_channel {
  53. unsigned int id;
  54. const char *name;
  55. };
  56. /**
  57. * struct hsi_config - Configuration for RX/TX HSI modules
  58. * @mode: Bit transmission mode (STREAM or FRAME)
  59. * @channels: Channel resources used by the client
  60. * @num_channels: Number of channel resources
  61. * @num_hw_channels: Number of channels the transceiver is configured for [1..16]
  62. * @speed: Max bit transmission speed (Kbit/s)
  63. * @flow: RX flow type (SYNCHRONIZED or PIPELINE)
  64. * @arb_mode: Arbitration mode for TX frame (Round robin, priority)
  65. */
  66. struct hsi_config {
  67. unsigned int mode;
  68. struct hsi_channel *channels;
  69. unsigned int num_channels;
  70. unsigned int num_hw_channels;
  71. unsigned int speed;
  72. union {
  73. unsigned int flow; /* RX only */
  74. unsigned int arb_mode; /* TX only */
  75. };
  76. };
  77. /**
  78. * struct hsi_board_info - HSI client board info
  79. * @name: Name for the HSI device
  80. * @hsi_id: HSI controller id where the client sits
  81. * @port: Port number in the controller where the client sits
  82. * @tx_cfg: HSI TX configuration
  83. * @rx_cfg: HSI RX configuration
  84. * @platform_data: Platform related data
  85. * @archdata: Architecture-dependent device data
  86. */
  87. struct hsi_board_info {
  88. const char *name;
  89. unsigned int hsi_id;
  90. unsigned int port;
  91. struct hsi_config tx_cfg;
  92. struct hsi_config rx_cfg;
  93. void *platform_data;
  94. struct dev_archdata *archdata;
  95. };
  96. #ifdef CONFIG_HSI_BOARDINFO
  97. extern int hsi_register_board_info(struct hsi_board_info const *info,
  98. unsigned int len);
  99. #else
  100. static inline int hsi_register_board_info(struct hsi_board_info const *info,
  101. unsigned int len)
  102. {
  103. return 0;
  104. }
  105. #endif /* CONFIG_HSI_BOARDINFO */
  106. /**
  107. * struct hsi_client - HSI client attached to an HSI port
  108. * @device: Driver model representation of the device
  109. * @tx_cfg: HSI TX configuration
  110. * @rx_cfg: HSI RX configuration
  111. */
  112. struct hsi_client {
  113. struct device device;
  114. struct hsi_config tx_cfg;
  115. struct hsi_config rx_cfg;
  116. /* private: */
  117. void (*ehandler)(struct hsi_client *, unsigned long);
  118. unsigned int pclaimed:1;
  119. struct notifier_block nb;
  120. };
  121. #define to_hsi_client(dev) container_of(dev, struct hsi_client, device)
  122. static inline void hsi_client_set_drvdata(struct hsi_client *cl, void *data)
  123. {
  124. dev_set_drvdata(&cl->device, data);
  125. }
  126. static inline void *hsi_client_drvdata(struct hsi_client *cl)
  127. {
  128. return dev_get_drvdata(&cl->device);
  129. }
  130. int hsi_register_port_event(struct hsi_client *cl,
  131. void (*handler)(struct hsi_client *, unsigned long));
  132. int hsi_unregister_port_event(struct hsi_client *cl);
  133. /**
  134. * struct hsi_client_driver - Driver associated to an HSI client
  135. * @driver: Driver model representation of the driver
  136. */
  137. struct hsi_client_driver {
  138. struct device_driver driver;
  139. };
  140. #define to_hsi_client_driver(drv) container_of(drv, struct hsi_client_driver,\
  141. driver)
  142. int hsi_register_client_driver(struct hsi_client_driver *drv);
  143. static inline void hsi_unregister_client_driver(struct hsi_client_driver *drv)
  144. {
  145. driver_unregister(&drv->driver);
  146. }
  147. /**
  148. * struct hsi_msg - HSI message descriptor
  149. * @link: Free to use by the current descriptor owner
  150. * @cl: HSI device client that issues the transfer
  151. * @sgt: Head of the scatterlist array
  152. * @context: Client context data associated to the transfer
  153. * @complete: Transfer completion callback
  154. * @destructor: Destructor to free resources when flushing
  155. * @status: Status of the transfer when completed
  156. * @actual_len: Actual length of data transferred on completion
  157. * @channel: Channel were to TX/RX the message
  158. * @ttype: Transfer type (TX if set, RX otherwise)
  159. * @break_frame: if true HSI will send/receive a break frame. Data buffers are
  160. * ignored in the request.
  161. */
  162. struct hsi_msg {
  163. struct list_head link;
  164. struct hsi_client *cl;
  165. struct sg_table sgt;
  166. void *context;
  167. void (*complete)(struct hsi_msg *msg);
  168. void (*destructor)(struct hsi_msg *msg);
  169. int status;
  170. unsigned int actual_len;
  171. unsigned int channel;
  172. unsigned int ttype:1;
  173. unsigned int break_frame:1;
  174. };
  175. struct hsi_msg *hsi_alloc_msg(unsigned int n_frag, gfp_t flags);
  176. void hsi_free_msg(struct hsi_msg *msg);
  177. /**
  178. * struct hsi_port - HSI port device
  179. * @device: Driver model representation of the device
  180. * @tx_cfg: Current TX path configuration
  181. * @rx_cfg: Current RX path configuration
  182. * @num: Port number
  183. * @shared: Set when port can be shared by different clients
  184. * @claimed: Reference count of clients which claimed the port
  185. * @lock: Serialize port claim
  186. * @async: Asynchronous transfer callback
  187. * @setup: Callback to set the HSI client configuration
  188. * @flush: Callback to clean the HW state and destroy all pending transfers
  189. * @start_tx: Callback to inform that a client wants to TX data
  190. * @stop_tx: Callback to inform that a client no longer wishes to TX data
  191. * @release: Callback to inform that a client no longer uses the port
  192. * @n_head: Notifier chain for signaling port events to the clients.
  193. */
  194. struct hsi_port {
  195. struct device device;
  196. struct hsi_config tx_cfg;
  197. struct hsi_config rx_cfg;
  198. unsigned int num;
  199. unsigned int shared:1;
  200. int claimed;
  201. struct mutex lock;
  202. int (*async)(struct hsi_msg *msg);
  203. int (*setup)(struct hsi_client *cl);
  204. int (*flush)(struct hsi_client *cl);
  205. int (*start_tx)(struct hsi_client *cl);
  206. int (*stop_tx)(struct hsi_client *cl);
  207. int (*release)(struct hsi_client *cl);
  208. /* private */
  209. struct blocking_notifier_head n_head;
  210. };
  211. #define to_hsi_port(dev) container_of(dev, struct hsi_port, device)
  212. #define hsi_get_port(cl) to_hsi_port((cl)->device.parent)
  213. int hsi_event(struct hsi_port *port, unsigned long event);
  214. int hsi_claim_port(struct hsi_client *cl, unsigned int share);
  215. void hsi_release_port(struct hsi_client *cl);
  216. static inline int hsi_port_claimed(struct hsi_client *cl)
  217. {
  218. return cl->pclaimed;
  219. }
  220. static inline void hsi_port_set_drvdata(struct hsi_port *port, void *data)
  221. {
  222. dev_set_drvdata(&port->device, data);
  223. }
  224. static inline void *hsi_port_drvdata(struct hsi_port *port)
  225. {
  226. return dev_get_drvdata(&port->device);
  227. }
  228. /**
  229. * struct hsi_controller - HSI controller device
  230. * @device: Driver model representation of the device
  231. * @owner: Pointer to the module owning the controller
  232. * @id: HSI controller ID
  233. * @num_ports: Number of ports in the HSI controller
  234. * @port: Array of HSI ports
  235. */
  236. struct hsi_controller {
  237. struct device device;
  238. struct module *owner;
  239. unsigned int id;
  240. unsigned int num_ports;
  241. struct hsi_port **port;
  242. };
  243. #define to_hsi_controller(dev) container_of(dev, struct hsi_controller, device)
  244. struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags);
  245. void hsi_put_controller(struct hsi_controller *hsi);
  246. int hsi_register_controller(struct hsi_controller *hsi);
  247. void hsi_unregister_controller(struct hsi_controller *hsi);
  248. struct hsi_client *hsi_new_client(struct hsi_port *port,
  249. struct hsi_board_info *info);
  250. int hsi_remove_client(struct device *dev, void *data);
  251. void hsi_port_unregister_clients(struct hsi_port *port);
  252. #ifdef CONFIG_OF
  253. void hsi_add_clients_from_dt(struct hsi_port *port,
  254. struct device_node *clients);
  255. #else
  256. static inline void hsi_add_clients_from_dt(struct hsi_port *port,
  257. struct device_node *clients)
  258. {
  259. return;
  260. }
  261. #endif
  262. static inline void hsi_controller_set_drvdata(struct hsi_controller *hsi,
  263. void *data)
  264. {
  265. dev_set_drvdata(&hsi->device, data);
  266. }
  267. static inline void *hsi_controller_drvdata(struct hsi_controller *hsi)
  268. {
  269. return dev_get_drvdata(&hsi->device);
  270. }
  271. static inline struct hsi_port *hsi_find_port_num(struct hsi_controller *hsi,
  272. unsigned int num)
  273. {
  274. return (num < hsi->num_ports) ? hsi->port[num] : NULL;
  275. }
  276. /*
  277. * API for HSI clients
  278. */
  279. int hsi_async(struct hsi_client *cl, struct hsi_msg *msg);
  280. int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name);
  281. /**
  282. * hsi_id - Get HSI controller ID associated to a client
  283. * @cl: Pointer to a HSI client
  284. *
  285. * Return the controller id where the client is attached to
  286. */
  287. static inline unsigned int hsi_id(struct hsi_client *cl)
  288. {
  289. return to_hsi_controller(cl->device.parent->parent)->id;
  290. }
  291. /**
  292. * hsi_port_id - Gets the port number a client is attached to
  293. * @cl: Pointer to HSI client
  294. *
  295. * Return the port number associated to the client
  296. */
  297. static inline unsigned int hsi_port_id(struct hsi_client *cl)
  298. {
  299. return to_hsi_port(cl->device.parent)->num;
  300. }
  301. /**
  302. * hsi_setup - Configure the client's port
  303. * @cl: Pointer to the HSI client
  304. *
  305. * When sharing ports, clients should either relay on a single
  306. * client setup or have the same setup for all of them.
  307. *
  308. * Return -errno on failure, 0 on success
  309. */
  310. static inline int hsi_setup(struct hsi_client *cl)
  311. {
  312. if (!hsi_port_claimed(cl))
  313. return -EACCES;
  314. return hsi_get_port(cl)->setup(cl);
  315. }
  316. /**
  317. * hsi_flush - Flush all pending transactions on the client's port
  318. * @cl: Pointer to the HSI client
  319. *
  320. * This function will destroy all pending hsi_msg in the port and reset
  321. * the HW port so it is ready to receive and transmit from a clean state.
  322. *
  323. * Return -errno on failure, 0 on success
  324. */
  325. static inline int hsi_flush(struct hsi_client *cl)
  326. {
  327. if (!hsi_port_claimed(cl))
  328. return -EACCES;
  329. return hsi_get_port(cl)->flush(cl);
  330. }
  331. /**
  332. * hsi_async_read - Submit a read transfer
  333. * @cl: Pointer to the HSI client
  334. * @msg: HSI message descriptor of the transfer
  335. *
  336. * Return -errno on failure, 0 on success
  337. */
  338. static inline int hsi_async_read(struct hsi_client *cl, struct hsi_msg *msg)
  339. {
  340. msg->ttype = HSI_MSG_READ;
  341. return hsi_async(cl, msg);
  342. }
  343. /**
  344. * hsi_async_write - Submit a write transfer
  345. * @cl: Pointer to the HSI client
  346. * @msg: HSI message descriptor of the transfer
  347. *
  348. * Return -errno on failure, 0 on success
  349. */
  350. static inline int hsi_async_write(struct hsi_client *cl, struct hsi_msg *msg)
  351. {
  352. msg->ttype = HSI_MSG_WRITE;
  353. return hsi_async(cl, msg);
  354. }
  355. /**
  356. * hsi_start_tx - Signal the port that the client wants to start a TX
  357. * @cl: Pointer to the HSI client
  358. *
  359. * Return -errno on failure, 0 on success
  360. */
  361. static inline int hsi_start_tx(struct hsi_client *cl)
  362. {
  363. if (!hsi_port_claimed(cl))
  364. return -EACCES;
  365. return hsi_get_port(cl)->start_tx(cl);
  366. }
  367. /**
  368. * hsi_stop_tx - Signal the port that the client no longer wants to transmit
  369. * @cl: Pointer to the HSI client
  370. *
  371. * Return -errno on failure, 0 on success
  372. */
  373. static inline int hsi_stop_tx(struct hsi_client *cl)
  374. {
  375. if (!hsi_port_claimed(cl))
  376. return -EACCES;
  377. return hsi_get_port(cl)->stop_tx(cl);
  378. }
  379. #endif /* __LINUX_HSI_H__ */