hsi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/spinlock.h>
  28. #include <linux/list.h>
  29. #include <linux/module.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. * @hsi_start_rx: Called after incoming wake line goes high
  112. * @hsi_stop_rx: Called after incoming wake line goes low
  113. */
  114. struct hsi_client {
  115. struct device device;
  116. struct hsi_config tx_cfg;
  117. struct hsi_config rx_cfg;
  118. void (*hsi_start_rx)(struct hsi_client *cl);
  119. void (*hsi_stop_rx)(struct hsi_client *cl);
  120. /* private: */
  121. unsigned int pclaimed:1;
  122. struct list_head link;
  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. /**
  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 transfered 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. * @clients: List of hsi_clients using the port.
  193. * @clock: Lock to serialize access to the clients list.
  194. */
  195. struct hsi_port {
  196. struct device device;
  197. struct hsi_config tx_cfg;
  198. struct hsi_config rx_cfg;
  199. unsigned int num;
  200. unsigned int shared:1;
  201. int claimed;
  202. struct mutex lock;
  203. int (*async)(struct hsi_msg *msg);
  204. int (*setup)(struct hsi_client *cl);
  205. int (*flush)(struct hsi_client *cl);
  206. int (*start_tx)(struct hsi_client *cl);
  207. int (*stop_tx)(struct hsi_client *cl);
  208. int (*release)(struct hsi_client *cl);
  209. struct list_head clients;
  210. spinlock_t clock;
  211. };
  212. #define to_hsi_port(dev) container_of(dev, struct hsi_port, device)
  213. #define hsi_get_port(cl) to_hsi_port((cl)->device.parent)
  214. void hsi_event(struct hsi_port *port, unsigned int event);
  215. int hsi_claim_port(struct hsi_client *cl, unsigned int share);
  216. void hsi_release_port(struct hsi_client *cl);
  217. static inline int hsi_port_claimed(struct hsi_client *cl)
  218. {
  219. return cl->pclaimed;
  220. }
  221. static inline void hsi_port_set_drvdata(struct hsi_port *port, void *data)
  222. {
  223. dev_set_drvdata(&port->device, data);
  224. }
  225. static inline void *hsi_port_drvdata(struct hsi_port *port)
  226. {
  227. return dev_get_drvdata(&port->device);
  228. }
  229. /**
  230. * struct hsi_controller - HSI controller device
  231. * @device: Driver model representation of the device
  232. * @owner: Pointer to the module owning the controller
  233. * @id: HSI controller ID
  234. * @num_ports: Number of ports in the HSI controller
  235. * @port: Array of HSI ports
  236. */
  237. struct hsi_controller {
  238. struct device device;
  239. struct module *owner;
  240. unsigned int id;
  241. unsigned int num_ports;
  242. struct hsi_port *port;
  243. };
  244. #define to_hsi_controller(dev) container_of(dev, struct hsi_controller, device)
  245. struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags);
  246. void hsi_free_controller(struct hsi_controller *hsi);
  247. int hsi_register_controller(struct hsi_controller *hsi);
  248. void hsi_unregister_controller(struct hsi_controller *hsi);
  249. static inline void hsi_controller_set_drvdata(struct hsi_controller *hsi,
  250. void *data)
  251. {
  252. dev_set_drvdata(&hsi->device, data);
  253. }
  254. static inline void *hsi_controller_drvdata(struct hsi_controller *hsi)
  255. {
  256. return dev_get_drvdata(&hsi->device);
  257. }
  258. static inline struct hsi_port *hsi_find_port_num(struct hsi_controller *hsi,
  259. unsigned int num)
  260. {
  261. return (num < hsi->num_ports) ? &hsi->port[num] : NULL;
  262. }
  263. /*
  264. * API for HSI clients
  265. */
  266. int hsi_async(struct hsi_client *cl, struct hsi_msg *msg);
  267. /**
  268. * hsi_id - Get HSI controller ID associated to a client
  269. * @cl: Pointer to a HSI client
  270. *
  271. * Return the controller id where the client is attached to
  272. */
  273. static inline unsigned int hsi_id(struct hsi_client *cl)
  274. {
  275. return to_hsi_controller(cl->device.parent->parent)->id;
  276. }
  277. /**
  278. * hsi_port_id - Gets the port number a client is attached to
  279. * @cl: Pointer to HSI client
  280. *
  281. * Return the port number associated to the client
  282. */
  283. static inline unsigned int hsi_port_id(struct hsi_client *cl)
  284. {
  285. return to_hsi_port(cl->device.parent)->num;
  286. }
  287. /**
  288. * hsi_setup - Configure the client's port
  289. * @cl: Pointer to the HSI client
  290. *
  291. * When sharing ports, clients should either relay on a single
  292. * client setup or have the same setup for all of them.
  293. *
  294. * Return -errno on failure, 0 on success
  295. */
  296. static inline int hsi_setup(struct hsi_client *cl)
  297. {
  298. if (!hsi_port_claimed(cl))
  299. return -EACCES;
  300. return hsi_get_port(cl)->setup(cl);
  301. }
  302. /**
  303. * hsi_flush - Flush all pending transactions on the client's port
  304. * @cl: Pointer to the HSI client
  305. *
  306. * This function will destroy all pending hsi_msg in the port and reset
  307. * the HW port so it is ready to receive and transmit from a clean state.
  308. *
  309. * Return -errno on failure, 0 on success
  310. */
  311. static inline int hsi_flush(struct hsi_client *cl)
  312. {
  313. if (!hsi_port_claimed(cl))
  314. return -EACCES;
  315. return hsi_get_port(cl)->flush(cl);
  316. }
  317. /**
  318. * hsi_async_read - Submit a read transfer
  319. * @cl: Pointer to the HSI client
  320. * @msg: HSI message descriptor of the transfer
  321. *
  322. * Return -errno on failure, 0 on success
  323. */
  324. static inline int hsi_async_read(struct hsi_client *cl, struct hsi_msg *msg)
  325. {
  326. msg->ttype = HSI_MSG_READ;
  327. return hsi_async(cl, msg);
  328. }
  329. /**
  330. * hsi_async_write - Submit a write transfer
  331. * @cl: Pointer to the HSI client
  332. * @msg: HSI message descriptor of the transfer
  333. *
  334. * Return -errno on failure, 0 on success
  335. */
  336. static inline int hsi_async_write(struct hsi_client *cl, struct hsi_msg *msg)
  337. {
  338. msg->ttype = HSI_MSG_WRITE;
  339. return hsi_async(cl, msg);
  340. }
  341. /**
  342. * hsi_start_tx - Signal the port that the client wants to start a TX
  343. * @cl: Pointer to the HSI client
  344. *
  345. * Return -errno on failure, 0 on success
  346. */
  347. static inline int hsi_start_tx(struct hsi_client *cl)
  348. {
  349. if (!hsi_port_claimed(cl))
  350. return -EACCES;
  351. return hsi_get_port(cl)->start_tx(cl);
  352. }
  353. /**
  354. * hsi_stop_tx - Signal the port that the client no longer wants to transmit
  355. * @cl: Pointer to the HSI client
  356. *
  357. * Return -errno on failure, 0 on success
  358. */
  359. static inline int hsi_stop_tx(struct hsi_client *cl)
  360. {
  361. if (!hsi_port_claimed(cl))
  362. return -EACCES;
  363. return hsi_get_port(cl)->stop_tx(cl);
  364. }
  365. #endif /* __LINUX_HSI_H__ */