hsi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * HSI core.
  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. #include <linux/hsi/hsi.h>
  23. #include <linux/compiler.h>
  24. #include <linux/rwsem.h>
  25. #include <linux/list.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/kobject.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include "hsi_core.h"
  31. static struct device_type hsi_ctrl = {
  32. .name = "hsi_controller",
  33. };
  34. static struct device_type hsi_cl = {
  35. .name = "hsi_client",
  36. };
  37. static struct device_type hsi_port = {
  38. .name = "hsi_port",
  39. };
  40. static ssize_t modalias_show(struct device *dev,
  41. struct device_attribute *a __maybe_unused, char *buf)
  42. {
  43. return sprintf(buf, "hsi:%s\n", dev_name(dev));
  44. }
  45. static struct device_attribute hsi_bus_dev_attrs[] = {
  46. __ATTR_RO(modalias),
  47. __ATTR_NULL,
  48. };
  49. static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  50. {
  51. if (dev->type == &hsi_cl)
  52. add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
  53. return 0;
  54. }
  55. static int hsi_bus_match(struct device *dev, struct device_driver *driver)
  56. {
  57. return strcmp(dev_name(dev), driver->name) == 0;
  58. }
  59. static struct bus_type hsi_bus_type = {
  60. .name = "hsi",
  61. .dev_attrs = hsi_bus_dev_attrs,
  62. .match = hsi_bus_match,
  63. .uevent = hsi_bus_uevent,
  64. };
  65. static void hsi_client_release(struct device *dev)
  66. {
  67. kfree(to_hsi_client(dev));
  68. }
  69. static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info)
  70. {
  71. struct hsi_client *cl;
  72. unsigned long flags;
  73. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  74. if (!cl)
  75. return;
  76. cl->device.type = &hsi_cl;
  77. cl->tx_cfg = info->tx_cfg;
  78. cl->rx_cfg = info->rx_cfg;
  79. cl->device.bus = &hsi_bus_type;
  80. cl->device.parent = &port->device;
  81. cl->device.release = hsi_client_release;
  82. dev_set_name(&cl->device, info->name);
  83. cl->device.platform_data = info->platform_data;
  84. spin_lock_irqsave(&port->clock, flags);
  85. list_add_tail(&cl->link, &port->clients);
  86. spin_unlock_irqrestore(&port->clock, flags);
  87. if (info->archdata)
  88. cl->device.archdata = *info->archdata;
  89. if (device_register(&cl->device) < 0) {
  90. pr_err("hsi: failed to register client: %s\n", info->name);
  91. kfree(cl);
  92. }
  93. }
  94. static void hsi_scan_board_info(struct hsi_controller *hsi)
  95. {
  96. struct hsi_cl_info *cl_info;
  97. struct hsi_port *p;
  98. list_for_each_entry(cl_info, &hsi_board_list, list)
  99. if (cl_info->info.hsi_id == hsi->id) {
  100. p = hsi_find_port_num(hsi, cl_info->info.port);
  101. if (!p)
  102. continue;
  103. hsi_new_client(p, &cl_info->info);
  104. }
  105. }
  106. static int hsi_remove_client(struct device *dev, void *data __maybe_unused)
  107. {
  108. struct hsi_client *cl = to_hsi_client(dev);
  109. struct hsi_port *port = to_hsi_port(dev->parent);
  110. unsigned long flags;
  111. spin_lock_irqsave(&port->clock, flags);
  112. list_del(&cl->link);
  113. spin_unlock_irqrestore(&port->clock, flags);
  114. device_unregister(dev);
  115. return 0;
  116. }
  117. static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
  118. {
  119. device_for_each_child(dev, NULL, hsi_remove_client);
  120. device_unregister(dev);
  121. return 0;
  122. }
  123. static void hsi_controller_release(struct device *dev __maybe_unused)
  124. {
  125. }
  126. static void hsi_port_release(struct device *dev __maybe_unused)
  127. {
  128. }
  129. /**
  130. * hsi_unregister_controller - Unregister an HSI controller
  131. * @hsi: The HSI controller to register
  132. */
  133. void hsi_unregister_controller(struct hsi_controller *hsi)
  134. {
  135. device_for_each_child(&hsi->device, NULL, hsi_remove_port);
  136. device_unregister(&hsi->device);
  137. }
  138. EXPORT_SYMBOL_GPL(hsi_unregister_controller);
  139. /**
  140. * hsi_register_controller - Register an HSI controller and its ports
  141. * @hsi: The HSI controller to register
  142. *
  143. * Returns -errno on failure, 0 on success.
  144. */
  145. int hsi_register_controller(struct hsi_controller *hsi)
  146. {
  147. unsigned int i;
  148. int err;
  149. hsi->device.type = &hsi_ctrl;
  150. hsi->device.bus = &hsi_bus_type;
  151. hsi->device.release = hsi_controller_release;
  152. err = device_register(&hsi->device);
  153. if (err < 0)
  154. return err;
  155. for (i = 0; i < hsi->num_ports; i++) {
  156. hsi->port[i].device.parent = &hsi->device;
  157. hsi->port[i].device.bus = &hsi_bus_type;
  158. hsi->port[i].device.release = hsi_port_release;
  159. hsi->port[i].device.type = &hsi_port;
  160. INIT_LIST_HEAD(&hsi->port[i].clients);
  161. spin_lock_init(&hsi->port[i].clock);
  162. err = device_register(&hsi->port[i].device);
  163. if (err < 0)
  164. goto out;
  165. }
  166. /* Populate HSI bus with HSI clients */
  167. hsi_scan_board_info(hsi);
  168. return 0;
  169. out:
  170. hsi_unregister_controller(hsi);
  171. return err;
  172. }
  173. EXPORT_SYMBOL_GPL(hsi_register_controller);
  174. /**
  175. * hsi_register_client_driver - Register an HSI client to the HSI bus
  176. * @drv: HSI client driver to register
  177. *
  178. * Returns -errno on failure, 0 on success.
  179. */
  180. int hsi_register_client_driver(struct hsi_client_driver *drv)
  181. {
  182. drv->driver.bus = &hsi_bus_type;
  183. return driver_register(&drv->driver);
  184. }
  185. EXPORT_SYMBOL_GPL(hsi_register_client_driver);
  186. static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
  187. {
  188. return 0;
  189. }
  190. static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
  191. {
  192. return 0;
  193. }
  194. /**
  195. * hsi_alloc_controller - Allocate an HSI controller and its ports
  196. * @n_ports: Number of ports on the HSI controller
  197. * @flags: Kernel allocation flags
  198. *
  199. * Return NULL on failure or a pointer to an hsi_controller on success.
  200. */
  201. struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
  202. {
  203. struct hsi_controller *hsi;
  204. struct hsi_port *port;
  205. unsigned int i;
  206. if (!n_ports)
  207. return NULL;
  208. port = kzalloc(sizeof(*port)*n_ports, flags);
  209. if (!port)
  210. return NULL;
  211. hsi = kzalloc(sizeof(*hsi), flags);
  212. if (!hsi)
  213. goto out;
  214. for (i = 0; i < n_ports; i++) {
  215. dev_set_name(&port[i].device, "port%d", i);
  216. port[i].num = i;
  217. port[i].async = hsi_dummy_msg;
  218. port[i].setup = hsi_dummy_cl;
  219. port[i].flush = hsi_dummy_cl;
  220. port[i].start_tx = hsi_dummy_cl;
  221. port[i].stop_tx = hsi_dummy_cl;
  222. port[i].release = hsi_dummy_cl;
  223. mutex_init(&port[i].lock);
  224. }
  225. hsi->num_ports = n_ports;
  226. hsi->port = port;
  227. return hsi;
  228. out:
  229. kfree(port);
  230. return NULL;
  231. }
  232. EXPORT_SYMBOL_GPL(hsi_alloc_controller);
  233. /**
  234. * hsi_free_controller - Free an HSI controller
  235. * @hsi: Pointer to HSI controller
  236. */
  237. void hsi_free_controller(struct hsi_controller *hsi)
  238. {
  239. if (!hsi)
  240. return;
  241. kfree(hsi->port);
  242. kfree(hsi);
  243. }
  244. EXPORT_SYMBOL_GPL(hsi_free_controller);
  245. /**
  246. * hsi_free_msg - Free an HSI message
  247. * @msg: Pointer to the HSI message
  248. *
  249. * Client is responsible to free the buffers pointed by the scatterlists.
  250. */
  251. void hsi_free_msg(struct hsi_msg *msg)
  252. {
  253. if (!msg)
  254. return;
  255. sg_free_table(&msg->sgt);
  256. kfree(msg);
  257. }
  258. EXPORT_SYMBOL_GPL(hsi_free_msg);
  259. /**
  260. * hsi_alloc_msg - Allocate an HSI message
  261. * @nents: Number of memory entries
  262. * @flags: Kernel allocation flags
  263. *
  264. * nents can be 0. This mainly makes sense for read transfer.
  265. * In that case, HSI drivers will call the complete callback when
  266. * there is data to be read without consuming it.
  267. *
  268. * Return NULL on failure or a pointer to an hsi_msg on success.
  269. */
  270. struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
  271. {
  272. struct hsi_msg *msg;
  273. int err;
  274. msg = kzalloc(sizeof(*msg), flags);
  275. if (!msg)
  276. return NULL;
  277. if (!nents)
  278. return msg;
  279. err = sg_alloc_table(&msg->sgt, nents, flags);
  280. if (unlikely(err)) {
  281. kfree(msg);
  282. msg = NULL;
  283. }
  284. return msg;
  285. }
  286. EXPORT_SYMBOL_GPL(hsi_alloc_msg);
  287. /**
  288. * hsi_async - Submit an HSI transfer to the controller
  289. * @cl: HSI client sending the transfer
  290. * @msg: The HSI transfer passed to controller
  291. *
  292. * The HSI message must have the channel, ttype, complete and destructor
  293. * fields set beforehand. If nents > 0 then the client has to initialize
  294. * also the scatterlists to point to the buffers to write to or read from.
  295. *
  296. * HSI controllers relay on pre-allocated buffers from their clients and they
  297. * do not allocate buffers on their own.
  298. *
  299. * Once the HSI message transfer finishes, the HSI controller calls the
  300. * complete callback with the status and actual_len fields of the HSI message
  301. * updated. The complete callback can be called before returning from
  302. * hsi_async.
  303. *
  304. * Returns -errno on failure or 0 on success
  305. */
  306. int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
  307. {
  308. struct hsi_port *port = hsi_get_port(cl);
  309. if (!hsi_port_claimed(cl))
  310. return -EACCES;
  311. WARN_ON_ONCE(!msg->destructor || !msg->complete);
  312. msg->cl = cl;
  313. return port->async(msg);
  314. }
  315. EXPORT_SYMBOL_GPL(hsi_async);
  316. /**
  317. * hsi_claim_port - Claim the HSI client's port
  318. * @cl: HSI client that wants to claim its port
  319. * @share: Flag to indicate if the client wants to share the port or not.
  320. *
  321. * Returns -errno on failure, 0 on success.
  322. */
  323. int hsi_claim_port(struct hsi_client *cl, unsigned int share)
  324. {
  325. struct hsi_port *port = hsi_get_port(cl);
  326. int err = 0;
  327. mutex_lock(&port->lock);
  328. if ((port->claimed) && (!port->shared || !share)) {
  329. err = -EBUSY;
  330. goto out;
  331. }
  332. if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
  333. err = -ENODEV;
  334. goto out;
  335. }
  336. port->claimed++;
  337. port->shared = !!share;
  338. cl->pclaimed = 1;
  339. out:
  340. mutex_unlock(&port->lock);
  341. return err;
  342. }
  343. EXPORT_SYMBOL_GPL(hsi_claim_port);
  344. /**
  345. * hsi_release_port - Release the HSI client's port
  346. * @cl: HSI client which previously claimed its port
  347. */
  348. void hsi_release_port(struct hsi_client *cl)
  349. {
  350. struct hsi_port *port = hsi_get_port(cl);
  351. mutex_lock(&port->lock);
  352. /* Allow HW driver to do some cleanup */
  353. port->release(cl);
  354. if (cl->pclaimed)
  355. port->claimed--;
  356. BUG_ON(port->claimed < 0);
  357. cl->pclaimed = 0;
  358. if (!port->claimed)
  359. port->shared = 0;
  360. module_put(to_hsi_controller(port->device.parent)->owner);
  361. mutex_unlock(&port->lock);
  362. }
  363. EXPORT_SYMBOL_GPL(hsi_release_port);
  364. static int hsi_start_rx(struct hsi_client *cl, void *data __maybe_unused)
  365. {
  366. if (cl->hsi_start_rx)
  367. (*cl->hsi_start_rx)(cl);
  368. return 0;
  369. }
  370. static int hsi_stop_rx(struct hsi_client *cl, void *data __maybe_unused)
  371. {
  372. if (cl->hsi_stop_rx)
  373. (*cl->hsi_stop_rx)(cl);
  374. return 0;
  375. }
  376. static int hsi_port_for_each_client(struct hsi_port *port, void *data,
  377. int (*fn)(struct hsi_client *cl, void *data))
  378. {
  379. struct hsi_client *cl;
  380. spin_lock(&port->clock);
  381. list_for_each_entry(cl, &port->clients, link) {
  382. spin_unlock(&port->clock);
  383. (*fn)(cl, data);
  384. spin_lock(&port->clock);
  385. }
  386. spin_unlock(&port->clock);
  387. return 0;
  388. }
  389. /**
  390. * hsi_event -Notifies clients about port events
  391. * @port: Port where the event occurred
  392. * @event: The event type
  393. *
  394. * Clients should not be concerned about wake line behavior. However, due
  395. * to a race condition in HSI HW protocol, clients need to be notified
  396. * about wake line changes, so they can implement a workaround for it.
  397. *
  398. * Events:
  399. * HSI_EVENT_START_RX - Incoming wake line high
  400. * HSI_EVENT_STOP_RX - Incoming wake line down
  401. */
  402. void hsi_event(struct hsi_port *port, unsigned int event)
  403. {
  404. int (*fn)(struct hsi_client *cl, void *data);
  405. switch (event) {
  406. case HSI_EVENT_START_RX:
  407. fn = hsi_start_rx;
  408. break;
  409. case HSI_EVENT_STOP_RX:
  410. fn = hsi_stop_rx;
  411. break;
  412. default:
  413. return;
  414. }
  415. hsi_port_for_each_client(port, NULL, fn);
  416. }
  417. EXPORT_SYMBOL_GPL(hsi_event);
  418. static int __init hsi_init(void)
  419. {
  420. return bus_register(&hsi_bus_type);
  421. }
  422. postcore_initcall(hsi_init);
  423. static void __exit hsi_exit(void)
  424. {
  425. bus_unregister(&hsi_bus_type);
  426. }
  427. module_exit(hsi_exit);
  428. MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
  429. MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
  430. MODULE_LICENSE("GPL v2");