hsi.h 12 KB

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