hsi.h 11 KB

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