hsi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. * @e_handler: Callback for handling port events (RX Wake High/Low)
  125. * @pclaimed: Keeps tracks if the clients claimed its associated HSI port
  126. * @nb: Notifier block for port events
  127. */
  128. struct hsi_client {
  129. struct device device;
  130. struct hsi_config tx_cfg;
  131. struct hsi_config rx_cfg;
  132. /* private: */
  133. void (*ehandler)(struct hsi_client *, unsigned long);
  134. unsigned int pclaimed:1;
  135. struct notifier_block nb;
  136. };
  137. #define to_hsi_client(dev) container_of(dev, struct hsi_client, device)
  138. static inline void hsi_client_set_drvdata(struct hsi_client *cl, void *data)
  139. {
  140. dev_set_drvdata(&cl->device, data);
  141. }
  142. static inline void *hsi_client_drvdata(struct hsi_client *cl)
  143. {
  144. return dev_get_drvdata(&cl->device);
  145. }
  146. int hsi_register_port_event(struct hsi_client *cl,
  147. void (*handler)(struct hsi_client *, unsigned long));
  148. int hsi_unregister_port_event(struct hsi_client *cl);
  149. /**
  150. * struct hsi_client_driver - Driver associated to an HSI client
  151. * @driver: Driver model representation of the driver
  152. */
  153. struct hsi_client_driver {
  154. struct device_driver driver;
  155. };
  156. #define to_hsi_client_driver(drv) container_of(drv, struct hsi_client_driver,\
  157. driver)
  158. int hsi_register_client_driver(struct hsi_client_driver *drv);
  159. static inline void hsi_unregister_client_driver(struct hsi_client_driver *drv)
  160. {
  161. driver_unregister(&drv->driver);
  162. }
  163. /**
  164. * struct hsi_msg - HSI message descriptor
  165. * @link: Free to use by the current descriptor owner
  166. * @cl: HSI device client that issues the transfer
  167. * @sgt: Head of the scatterlist array
  168. * @context: Client context data associated to the transfer
  169. * @complete: Transfer completion callback
  170. * @destructor: Destructor to free resources when flushing
  171. * @status: Status of the transfer when completed
  172. * @actual_len: Actual length of data transferred on completion
  173. * @channel: Channel were to TX/RX the message
  174. * @ttype: Transfer type (TX if set, RX otherwise)
  175. * @break_frame: if true HSI will send/receive a break frame. Data buffers are
  176. * ignored in the request.
  177. */
  178. struct hsi_msg {
  179. struct list_head link;
  180. struct hsi_client *cl;
  181. struct sg_table sgt;
  182. void *context;
  183. void (*complete)(struct hsi_msg *msg);
  184. void (*destructor)(struct hsi_msg *msg);
  185. int status;
  186. unsigned int actual_len;
  187. unsigned int channel;
  188. unsigned int ttype:1;
  189. unsigned int break_frame:1;
  190. };
  191. struct hsi_msg *hsi_alloc_msg(unsigned int n_frag, gfp_t flags);
  192. void hsi_free_msg(struct hsi_msg *msg);
  193. /**
  194. * struct hsi_port - HSI port device
  195. * @device: Driver model representation of the device
  196. * @tx_cfg: Current TX path configuration
  197. * @rx_cfg: Current RX path configuration
  198. * @num: Port number
  199. * @shared: Set when port can be shared by different clients
  200. * @claimed: Reference count of clients which claimed the port
  201. * @lock: Serialize port claim
  202. * @async: Asynchronous transfer callback
  203. * @setup: Callback to set the HSI client configuration
  204. * @flush: Callback to clean the HW state and destroy all pending transfers
  205. * @start_tx: Callback to inform that a client wants to TX data
  206. * @stop_tx: Callback to inform that a client no longer wishes to TX data
  207. * @release: Callback to inform that a client no longer uses the port
  208. * @n_head: Notifier chain for signaling port events to the clients.
  209. */
  210. struct hsi_port {
  211. struct device device;
  212. struct hsi_config tx_cfg;
  213. struct hsi_config rx_cfg;
  214. unsigned int num;
  215. unsigned int shared:1;
  216. int claimed;
  217. struct mutex lock;
  218. int (*async)(struct hsi_msg *msg);
  219. int (*setup)(struct hsi_client *cl);
  220. int (*flush)(struct hsi_client *cl);
  221. int (*start_tx)(struct hsi_client *cl);
  222. int (*stop_tx)(struct hsi_client *cl);
  223. int (*release)(struct hsi_client *cl);
  224. /* private */
  225. struct atomic_notifier_head n_head;
  226. };
  227. #define to_hsi_port(dev) container_of(dev, struct hsi_port, device)
  228. #define hsi_get_port(cl) to_hsi_port((cl)->device.parent)
  229. int hsi_event(struct hsi_port *port, unsigned long event);
  230. int hsi_claim_port(struct hsi_client *cl, unsigned int share);
  231. void hsi_release_port(struct hsi_client *cl);
  232. static inline int hsi_port_claimed(struct hsi_client *cl)
  233. {
  234. return cl->pclaimed;
  235. }
  236. static inline void hsi_port_set_drvdata(struct hsi_port *port, void *data)
  237. {
  238. dev_set_drvdata(&port->device, data);
  239. }
  240. static inline void *hsi_port_drvdata(struct hsi_port *port)
  241. {
  242. return dev_get_drvdata(&port->device);
  243. }
  244. /**
  245. * struct hsi_controller - HSI controller device
  246. * @device: Driver model representation of the device
  247. * @owner: Pointer to the module owning the controller
  248. * @id: HSI controller ID
  249. * @num_ports: Number of ports in the HSI controller
  250. * @port: Array of HSI ports
  251. */
  252. struct hsi_controller {
  253. struct device device;
  254. struct module *owner;
  255. unsigned int id;
  256. unsigned int num_ports;
  257. struct hsi_port **port;
  258. };
  259. #define to_hsi_controller(dev) container_of(dev, struct hsi_controller, device)
  260. struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags);
  261. void hsi_put_controller(struct hsi_controller *hsi);
  262. int hsi_register_controller(struct hsi_controller *hsi);
  263. void hsi_unregister_controller(struct hsi_controller *hsi);
  264. struct hsi_client *hsi_new_client(struct hsi_port *port,
  265. struct hsi_board_info *info);
  266. int hsi_remove_client(struct device *dev, void *data);
  267. void hsi_port_unregister_clients(struct hsi_port *port);
  268. #ifdef CONFIG_OF
  269. void hsi_add_clients_from_dt(struct hsi_port *port,
  270. struct device_node *clients);
  271. #else
  272. static inline void hsi_add_clients_from_dt(struct hsi_port *port,
  273. struct device_node *clients)
  274. {
  275. return;
  276. }
  277. #endif
  278. static inline void hsi_controller_set_drvdata(struct hsi_controller *hsi,
  279. void *data)
  280. {
  281. dev_set_drvdata(&hsi->device, data);
  282. }
  283. static inline void *hsi_controller_drvdata(struct hsi_controller *hsi)
  284. {
  285. return dev_get_drvdata(&hsi->device);
  286. }
  287. static inline struct hsi_port *hsi_find_port_num(struct hsi_controller *hsi,
  288. unsigned int num)
  289. {
  290. return (num < hsi->num_ports) ? hsi->port[num] : NULL;
  291. }
  292. /*
  293. * API for HSI clients
  294. */
  295. int hsi_async(struct hsi_client *cl, struct hsi_msg *msg);
  296. int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name);
  297. /**
  298. * hsi_id - Get HSI controller ID associated to a client
  299. * @cl: Pointer to a HSI client
  300. *
  301. * Return the controller id where the client is attached to
  302. */
  303. static inline unsigned int hsi_id(struct hsi_client *cl)
  304. {
  305. return to_hsi_controller(cl->device.parent->parent)->id;
  306. }
  307. /**
  308. * hsi_port_id - Gets the port number a client is attached to
  309. * @cl: Pointer to HSI client
  310. *
  311. * Return the port number associated to the client
  312. */
  313. static inline unsigned int hsi_port_id(struct hsi_client *cl)
  314. {
  315. return to_hsi_port(cl->device.parent)->num;
  316. }
  317. /**
  318. * hsi_setup - Configure the client's port
  319. * @cl: Pointer to the HSI client
  320. *
  321. * When sharing ports, clients should either relay on a single
  322. * client setup or have the same setup for all of them.
  323. *
  324. * Return -errno on failure, 0 on success
  325. */
  326. static inline int hsi_setup(struct hsi_client *cl)
  327. {
  328. if (!hsi_port_claimed(cl))
  329. return -EACCES;
  330. return hsi_get_port(cl)->setup(cl);
  331. }
  332. /**
  333. * hsi_flush - Flush all pending transactions on the client's port
  334. * @cl: Pointer to the HSI client
  335. *
  336. * This function will destroy all pending hsi_msg in the port and reset
  337. * the HW port so it is ready to receive and transmit from a clean state.
  338. *
  339. * Return -errno on failure, 0 on success
  340. */
  341. static inline int hsi_flush(struct hsi_client *cl)
  342. {
  343. if (!hsi_port_claimed(cl))
  344. return -EACCES;
  345. return hsi_get_port(cl)->flush(cl);
  346. }
  347. /**
  348. * hsi_async_read - Submit a read transfer
  349. * @cl: Pointer to the HSI client
  350. * @msg: HSI message descriptor of the transfer
  351. *
  352. * Return -errno on failure, 0 on success
  353. */
  354. static inline int hsi_async_read(struct hsi_client *cl, struct hsi_msg *msg)
  355. {
  356. msg->ttype = HSI_MSG_READ;
  357. return hsi_async(cl, msg);
  358. }
  359. /**
  360. * hsi_async_write - Submit a write transfer
  361. * @cl: Pointer to the HSI client
  362. * @msg: HSI message descriptor of the transfer
  363. *
  364. * Return -errno on failure, 0 on success
  365. */
  366. static inline int hsi_async_write(struct hsi_client *cl, struct hsi_msg *msg)
  367. {
  368. msg->ttype = HSI_MSG_WRITE;
  369. return hsi_async(cl, msg);
  370. }
  371. /**
  372. * hsi_start_tx - Signal the port that the client wants to start a TX
  373. * @cl: Pointer to the HSI client
  374. *
  375. * Return -errno on failure, 0 on success
  376. */
  377. static inline int hsi_start_tx(struct hsi_client *cl)
  378. {
  379. if (!hsi_port_claimed(cl))
  380. return -EACCES;
  381. return hsi_get_port(cl)->start_tx(cl);
  382. }
  383. /**
  384. * hsi_stop_tx - Signal the port that the client no longer wants to transmit
  385. * @cl: Pointer to the HSI client
  386. *
  387. * Return -errno on failure, 0 on success
  388. */
  389. static inline int hsi_stop_tx(struct hsi_client *cl)
  390. {
  391. if (!hsi_port_claimed(cl))
  392. return -EACCES;
  393. return hsi_get_port(cl)->stop_tx(cl);
  394. }
  395. #endif /* __LINUX_HSI_H__ */