hsi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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/list.h>
  25. #include <linux/kobject.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <linux/notifier.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include "hsi_core.h"
  32. static ssize_t modalias_show(struct device *dev,
  33. struct device_attribute *a __maybe_unused, char *buf)
  34. {
  35. return sprintf(buf, "hsi:%s\n", dev_name(dev));
  36. }
  37. static DEVICE_ATTR_RO(modalias);
  38. static struct attribute *hsi_bus_dev_attrs[] = {
  39. &dev_attr_modalias.attr,
  40. NULL,
  41. };
  42. ATTRIBUTE_GROUPS(hsi_bus_dev);
  43. static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  44. {
  45. add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
  46. return 0;
  47. }
  48. static int hsi_bus_match(struct device *dev, struct device_driver *driver)
  49. {
  50. if (of_driver_match_device(dev, driver))
  51. return true;
  52. if (strcmp(dev_name(dev), driver->name) == 0)
  53. return true;
  54. return false;
  55. }
  56. static struct bus_type hsi_bus_type = {
  57. .name = "hsi",
  58. .dev_groups = hsi_bus_dev_groups,
  59. .match = hsi_bus_match,
  60. .uevent = hsi_bus_uevent,
  61. };
  62. static void hsi_client_release(struct device *dev)
  63. {
  64. struct hsi_client *cl = to_hsi_client(dev);
  65. kfree(cl->tx_cfg.channels);
  66. kfree(cl->rx_cfg.channels);
  67. kfree(cl);
  68. }
  69. struct hsi_client *hsi_new_client(struct hsi_port *port,
  70. struct hsi_board_info *info)
  71. {
  72. struct hsi_client *cl;
  73. size_t size;
  74. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  75. if (!cl)
  76. return NULL;
  77. cl->tx_cfg = info->tx_cfg;
  78. if (cl->tx_cfg.channels) {
  79. size = cl->tx_cfg.num_channels * sizeof(*cl->tx_cfg.channels);
  80. cl->tx_cfg.channels = kzalloc(size , GFP_KERNEL);
  81. memcpy(cl->tx_cfg.channels, info->tx_cfg.channels, size);
  82. }
  83. cl->rx_cfg = info->rx_cfg;
  84. if (cl->rx_cfg.channels) {
  85. size = cl->rx_cfg.num_channels * sizeof(*cl->rx_cfg.channels);
  86. cl->rx_cfg.channels = kzalloc(size , GFP_KERNEL);
  87. memcpy(cl->rx_cfg.channels, info->rx_cfg.channels, size);
  88. }
  89. cl->device.bus = &hsi_bus_type;
  90. cl->device.parent = &port->device;
  91. cl->device.release = hsi_client_release;
  92. dev_set_name(&cl->device, "%s", info->name);
  93. cl->device.platform_data = info->platform_data;
  94. if (info->archdata)
  95. cl->device.archdata = *info->archdata;
  96. if (device_register(&cl->device) < 0) {
  97. pr_err("hsi: failed to register client: %s\n", info->name);
  98. put_device(&cl->device);
  99. }
  100. return cl;
  101. }
  102. EXPORT_SYMBOL_GPL(hsi_new_client);
  103. static void hsi_scan_board_info(struct hsi_controller *hsi)
  104. {
  105. struct hsi_cl_info *cl_info;
  106. struct hsi_port *p;
  107. list_for_each_entry(cl_info, &hsi_board_list, list)
  108. if (cl_info->info.hsi_id == hsi->id) {
  109. p = hsi_find_port_num(hsi, cl_info->info.port);
  110. if (!p)
  111. continue;
  112. hsi_new_client(p, &cl_info->info);
  113. }
  114. }
  115. #ifdef CONFIG_OF
  116. static struct hsi_board_info hsi_char_dev_info = {
  117. .name = "hsi_char",
  118. };
  119. static int hsi_of_property_parse_mode(struct device_node *client, char *name,
  120. unsigned int *result)
  121. {
  122. const char *mode;
  123. int err;
  124. err = of_property_read_string(client, name, &mode);
  125. if (err < 0)
  126. return err;
  127. if (strcmp(mode, "stream") == 0)
  128. *result = HSI_MODE_STREAM;
  129. else if (strcmp(mode, "frame") == 0)
  130. *result = HSI_MODE_FRAME;
  131. else
  132. return -EINVAL;
  133. return 0;
  134. }
  135. static int hsi_of_property_parse_flow(struct device_node *client, char *name,
  136. unsigned int *result)
  137. {
  138. const char *flow;
  139. int err;
  140. err = of_property_read_string(client, name, &flow);
  141. if (err < 0)
  142. return err;
  143. if (strcmp(flow, "synchronized") == 0)
  144. *result = HSI_FLOW_SYNC;
  145. else if (strcmp(flow, "pipeline") == 0)
  146. *result = HSI_FLOW_PIPE;
  147. else
  148. return -EINVAL;
  149. return 0;
  150. }
  151. static int hsi_of_property_parse_arb_mode(struct device_node *client,
  152. char *name, unsigned int *result)
  153. {
  154. const char *arb_mode;
  155. int err;
  156. err = of_property_read_string(client, name, &arb_mode);
  157. if (err < 0)
  158. return err;
  159. if (strcmp(arb_mode, "round-robin") == 0)
  160. *result = HSI_ARB_RR;
  161. else if (strcmp(arb_mode, "priority") == 0)
  162. *result = HSI_ARB_PRIO;
  163. else
  164. return -EINVAL;
  165. return 0;
  166. }
  167. static void hsi_add_client_from_dt(struct hsi_port *port,
  168. struct device_node *client)
  169. {
  170. struct hsi_client *cl;
  171. struct hsi_channel channel;
  172. struct property *prop;
  173. char name[32];
  174. int length, cells, err, i, max_chan, mode;
  175. cl = kzalloc(sizeof(*cl), GFP_KERNEL);
  176. if (!cl)
  177. return;
  178. err = of_modalias_node(client, name, sizeof(name));
  179. if (err)
  180. goto err;
  181. dev_set_name(&cl->device, "%s", name);
  182. err = hsi_of_property_parse_mode(client, "hsi-mode", &mode);
  183. if (err) {
  184. err = hsi_of_property_parse_mode(client, "hsi-rx-mode",
  185. &cl->rx_cfg.mode);
  186. if (err)
  187. goto err;
  188. err = hsi_of_property_parse_mode(client, "hsi-tx-mode",
  189. &cl->tx_cfg.mode);
  190. if (err)
  191. goto err;
  192. } else {
  193. cl->rx_cfg.mode = mode;
  194. cl->tx_cfg.mode = mode;
  195. }
  196. err = of_property_read_u32(client, "hsi-speed-kbps",
  197. &cl->tx_cfg.speed);
  198. if (err)
  199. goto err;
  200. cl->rx_cfg.speed = cl->tx_cfg.speed;
  201. err = hsi_of_property_parse_flow(client, "hsi-flow",
  202. &cl->rx_cfg.flow);
  203. if (err)
  204. goto err;
  205. err = hsi_of_property_parse_arb_mode(client, "hsi-arb-mode",
  206. &cl->rx_cfg.arb_mode);
  207. if (err)
  208. goto err;
  209. prop = of_find_property(client, "hsi-channel-ids", &length);
  210. if (!prop) {
  211. err = -EINVAL;
  212. goto err;
  213. }
  214. cells = length / sizeof(u32);
  215. cl->rx_cfg.num_channels = cells;
  216. cl->tx_cfg.num_channels = cells;
  217. cl->rx_cfg.channels = kzalloc(cells * sizeof(channel), GFP_KERNEL);
  218. if (!cl->rx_cfg.channels) {
  219. err = -ENOMEM;
  220. goto err;
  221. }
  222. cl->tx_cfg.channels = kzalloc(cells * sizeof(channel), GFP_KERNEL);
  223. if (!cl->tx_cfg.channels) {
  224. err = -ENOMEM;
  225. goto err2;
  226. }
  227. max_chan = 0;
  228. for (i = 0; i < cells; i++) {
  229. err = of_property_read_u32_index(client, "hsi-channel-ids", i,
  230. &channel.id);
  231. if (err)
  232. goto err3;
  233. err = of_property_read_string_index(client, "hsi-channel-names",
  234. i, &channel.name);
  235. if (err)
  236. channel.name = NULL;
  237. if (channel.id > max_chan)
  238. max_chan = channel.id;
  239. cl->rx_cfg.channels[i] = channel;
  240. cl->tx_cfg.channels[i] = channel;
  241. }
  242. cl->rx_cfg.num_hw_channels = max_chan + 1;
  243. cl->tx_cfg.num_hw_channels = max_chan + 1;
  244. cl->device.bus = &hsi_bus_type;
  245. cl->device.parent = &port->device;
  246. cl->device.release = hsi_client_release;
  247. cl->device.of_node = client;
  248. if (device_register(&cl->device) < 0) {
  249. pr_err("hsi: failed to register client: %s\n", name);
  250. put_device(&cl->device);
  251. goto err3;
  252. }
  253. return;
  254. err3:
  255. kfree(cl->tx_cfg.channels);
  256. err2:
  257. kfree(cl->rx_cfg.channels);
  258. err:
  259. kfree(cl);
  260. pr_err("hsi client: missing or incorrect of property: err=%d\n", err);
  261. }
  262. void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients)
  263. {
  264. struct device_node *child;
  265. /* register hsi-char device */
  266. hsi_new_client(port, &hsi_char_dev_info);
  267. for_each_available_child_of_node(clients, child)
  268. hsi_add_client_from_dt(port, child);
  269. }
  270. EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt);
  271. #endif
  272. int hsi_remove_client(struct device *dev, void *data __maybe_unused)
  273. {
  274. device_unregister(dev);
  275. return 0;
  276. }
  277. EXPORT_SYMBOL_GPL(hsi_remove_client);
  278. static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
  279. {
  280. device_for_each_child(dev, NULL, hsi_remove_client);
  281. device_unregister(dev);
  282. return 0;
  283. }
  284. static void hsi_controller_release(struct device *dev)
  285. {
  286. struct hsi_controller *hsi = to_hsi_controller(dev);
  287. kfree(hsi->port);
  288. kfree(hsi);
  289. }
  290. static void hsi_port_release(struct device *dev)
  291. {
  292. kfree(to_hsi_port(dev));
  293. }
  294. /**
  295. * hsi_unregister_port - Unregister an HSI port
  296. * @port: The HSI port to unregister
  297. */
  298. void hsi_port_unregister_clients(struct hsi_port *port)
  299. {
  300. device_for_each_child(&port->device, NULL, hsi_remove_client);
  301. }
  302. EXPORT_SYMBOL_GPL(hsi_port_unregister_clients);
  303. /**
  304. * hsi_unregister_controller - Unregister an HSI controller
  305. * @hsi: The HSI controller to register
  306. */
  307. void hsi_unregister_controller(struct hsi_controller *hsi)
  308. {
  309. device_for_each_child(&hsi->device, NULL, hsi_remove_port);
  310. device_unregister(&hsi->device);
  311. }
  312. EXPORT_SYMBOL_GPL(hsi_unregister_controller);
  313. /**
  314. * hsi_register_controller - Register an HSI controller and its ports
  315. * @hsi: The HSI controller to register
  316. *
  317. * Returns -errno on failure, 0 on success.
  318. */
  319. int hsi_register_controller(struct hsi_controller *hsi)
  320. {
  321. unsigned int i;
  322. int err;
  323. err = device_add(&hsi->device);
  324. if (err < 0)
  325. return err;
  326. for (i = 0; i < hsi->num_ports; i++) {
  327. hsi->port[i]->device.parent = &hsi->device;
  328. err = device_add(&hsi->port[i]->device);
  329. if (err < 0)
  330. goto out;
  331. }
  332. /* Populate HSI bus with HSI clients */
  333. hsi_scan_board_info(hsi);
  334. return 0;
  335. out:
  336. while (i-- > 0)
  337. device_del(&hsi->port[i]->device);
  338. device_del(&hsi->device);
  339. return err;
  340. }
  341. EXPORT_SYMBOL_GPL(hsi_register_controller);
  342. /**
  343. * hsi_register_client_driver - Register an HSI client to the HSI bus
  344. * @drv: HSI client driver to register
  345. *
  346. * Returns -errno on failure, 0 on success.
  347. */
  348. int hsi_register_client_driver(struct hsi_client_driver *drv)
  349. {
  350. drv->driver.bus = &hsi_bus_type;
  351. return driver_register(&drv->driver);
  352. }
  353. EXPORT_SYMBOL_GPL(hsi_register_client_driver);
  354. static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
  355. {
  356. return 0;
  357. }
  358. static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
  359. {
  360. return 0;
  361. }
  362. /**
  363. * hsi_put_controller - Free an HSI controller
  364. *
  365. * @hsi: Pointer to the HSI controller to freed
  366. *
  367. * HSI controller drivers should only use this function if they need
  368. * to free their allocated hsi_controller structures before a successful
  369. * call to hsi_register_controller. Other use is not allowed.
  370. */
  371. void hsi_put_controller(struct hsi_controller *hsi)
  372. {
  373. unsigned int i;
  374. if (!hsi)
  375. return;
  376. for (i = 0; i < hsi->num_ports; i++)
  377. if (hsi->port && hsi->port[i])
  378. put_device(&hsi->port[i]->device);
  379. put_device(&hsi->device);
  380. }
  381. EXPORT_SYMBOL_GPL(hsi_put_controller);
  382. /**
  383. * hsi_alloc_controller - Allocate an HSI controller and its ports
  384. * @n_ports: Number of ports on the HSI controller
  385. * @flags: Kernel allocation flags
  386. *
  387. * Return NULL on failure or a pointer to an hsi_controller on success.
  388. */
  389. struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
  390. {
  391. struct hsi_controller *hsi;
  392. struct hsi_port **port;
  393. unsigned int i;
  394. if (!n_ports)
  395. return NULL;
  396. hsi = kzalloc(sizeof(*hsi), flags);
  397. if (!hsi)
  398. return NULL;
  399. port = kzalloc(sizeof(*port)*n_ports, flags);
  400. if (!port) {
  401. kfree(hsi);
  402. return NULL;
  403. }
  404. hsi->num_ports = n_ports;
  405. hsi->port = port;
  406. hsi->device.release = hsi_controller_release;
  407. device_initialize(&hsi->device);
  408. for (i = 0; i < n_ports; i++) {
  409. port[i] = kzalloc(sizeof(**port), flags);
  410. if (port[i] == NULL)
  411. goto out;
  412. port[i]->num = i;
  413. port[i]->async = hsi_dummy_msg;
  414. port[i]->setup = hsi_dummy_cl;
  415. port[i]->flush = hsi_dummy_cl;
  416. port[i]->start_tx = hsi_dummy_cl;
  417. port[i]->stop_tx = hsi_dummy_cl;
  418. port[i]->release = hsi_dummy_cl;
  419. mutex_init(&port[i]->lock);
  420. ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head);
  421. dev_set_name(&port[i]->device, "port%d", i);
  422. hsi->port[i]->device.release = hsi_port_release;
  423. device_initialize(&hsi->port[i]->device);
  424. }
  425. return hsi;
  426. out:
  427. hsi_put_controller(hsi);
  428. return NULL;
  429. }
  430. EXPORT_SYMBOL_GPL(hsi_alloc_controller);
  431. /**
  432. * hsi_free_msg - Free an HSI message
  433. * @msg: Pointer to the HSI message
  434. *
  435. * Client is responsible to free the buffers pointed by the scatterlists.
  436. */
  437. void hsi_free_msg(struct hsi_msg *msg)
  438. {
  439. if (!msg)
  440. return;
  441. sg_free_table(&msg->sgt);
  442. kfree(msg);
  443. }
  444. EXPORT_SYMBOL_GPL(hsi_free_msg);
  445. /**
  446. * hsi_alloc_msg - Allocate an HSI message
  447. * @nents: Number of memory entries
  448. * @flags: Kernel allocation flags
  449. *
  450. * nents can be 0. This mainly makes sense for read transfer.
  451. * In that case, HSI drivers will call the complete callback when
  452. * there is data to be read without consuming it.
  453. *
  454. * Return NULL on failure or a pointer to an hsi_msg on success.
  455. */
  456. struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
  457. {
  458. struct hsi_msg *msg;
  459. int err;
  460. msg = kzalloc(sizeof(*msg), flags);
  461. if (!msg)
  462. return NULL;
  463. if (!nents)
  464. return msg;
  465. err = sg_alloc_table(&msg->sgt, nents, flags);
  466. if (unlikely(err)) {
  467. kfree(msg);
  468. msg = NULL;
  469. }
  470. return msg;
  471. }
  472. EXPORT_SYMBOL_GPL(hsi_alloc_msg);
  473. /**
  474. * hsi_async - Submit an HSI transfer to the controller
  475. * @cl: HSI client sending the transfer
  476. * @msg: The HSI transfer passed to controller
  477. *
  478. * The HSI message must have the channel, ttype, complete and destructor
  479. * fields set beforehand. If nents > 0 then the client has to initialize
  480. * also the scatterlists to point to the buffers to write to or read from.
  481. *
  482. * HSI controllers relay on pre-allocated buffers from their clients and they
  483. * do not allocate buffers on their own.
  484. *
  485. * Once the HSI message transfer finishes, the HSI controller calls the
  486. * complete callback with the status and actual_len fields of the HSI message
  487. * updated. The complete callback can be called before returning from
  488. * hsi_async.
  489. *
  490. * Returns -errno on failure or 0 on success
  491. */
  492. int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
  493. {
  494. struct hsi_port *port = hsi_get_port(cl);
  495. if (!hsi_port_claimed(cl))
  496. return -EACCES;
  497. WARN_ON_ONCE(!msg->destructor || !msg->complete);
  498. msg->cl = cl;
  499. return port->async(msg);
  500. }
  501. EXPORT_SYMBOL_GPL(hsi_async);
  502. /**
  503. * hsi_claim_port - Claim the HSI client's port
  504. * @cl: HSI client that wants to claim its port
  505. * @share: Flag to indicate if the client wants to share the port or not.
  506. *
  507. * Returns -errno on failure, 0 on success.
  508. */
  509. int hsi_claim_port(struct hsi_client *cl, unsigned int share)
  510. {
  511. struct hsi_port *port = hsi_get_port(cl);
  512. int err = 0;
  513. mutex_lock(&port->lock);
  514. if ((port->claimed) && (!port->shared || !share)) {
  515. err = -EBUSY;
  516. goto out;
  517. }
  518. if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
  519. err = -ENODEV;
  520. goto out;
  521. }
  522. port->claimed++;
  523. port->shared = !!share;
  524. cl->pclaimed = 1;
  525. out:
  526. mutex_unlock(&port->lock);
  527. return err;
  528. }
  529. EXPORT_SYMBOL_GPL(hsi_claim_port);
  530. /**
  531. * hsi_release_port - Release the HSI client's port
  532. * @cl: HSI client which previously claimed its port
  533. */
  534. void hsi_release_port(struct hsi_client *cl)
  535. {
  536. struct hsi_port *port = hsi_get_port(cl);
  537. mutex_lock(&port->lock);
  538. /* Allow HW driver to do some cleanup */
  539. port->release(cl);
  540. if (cl->pclaimed)
  541. port->claimed--;
  542. BUG_ON(port->claimed < 0);
  543. cl->pclaimed = 0;
  544. if (!port->claimed)
  545. port->shared = 0;
  546. module_put(to_hsi_controller(port->device.parent)->owner);
  547. mutex_unlock(&port->lock);
  548. }
  549. EXPORT_SYMBOL_GPL(hsi_release_port);
  550. static int hsi_event_notifier_call(struct notifier_block *nb,
  551. unsigned long event, void *data __maybe_unused)
  552. {
  553. struct hsi_client *cl = container_of(nb, struct hsi_client, nb);
  554. (*cl->ehandler)(cl, event);
  555. return 0;
  556. }
  557. /**
  558. * hsi_register_port_event - Register a client to receive port events
  559. * @cl: HSI client that wants to receive port events
  560. * @handler: Event handler callback
  561. *
  562. * Clients should register a callback to be able to receive
  563. * events from the ports. Registration should happen after
  564. * claiming the port.
  565. * The handler can be called in interrupt context.
  566. *
  567. * Returns -errno on error, or 0 on success.
  568. */
  569. int hsi_register_port_event(struct hsi_client *cl,
  570. void (*handler)(struct hsi_client *, unsigned long))
  571. {
  572. struct hsi_port *port = hsi_get_port(cl);
  573. if (!handler || cl->ehandler)
  574. return -EINVAL;
  575. if (!hsi_port_claimed(cl))
  576. return -EACCES;
  577. cl->ehandler = handler;
  578. cl->nb.notifier_call = hsi_event_notifier_call;
  579. return atomic_notifier_chain_register(&port->n_head, &cl->nb);
  580. }
  581. EXPORT_SYMBOL_GPL(hsi_register_port_event);
  582. /**
  583. * hsi_unregister_port_event - Stop receiving port events for a client
  584. * @cl: HSI client that wants to stop receiving port events
  585. *
  586. * Clients should call this function before releasing their associated
  587. * port.
  588. *
  589. * Returns -errno on error, or 0 on success.
  590. */
  591. int hsi_unregister_port_event(struct hsi_client *cl)
  592. {
  593. struct hsi_port *port = hsi_get_port(cl);
  594. int err;
  595. WARN_ON(!hsi_port_claimed(cl));
  596. err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb);
  597. if (!err)
  598. cl->ehandler = NULL;
  599. return err;
  600. }
  601. EXPORT_SYMBOL_GPL(hsi_unregister_port_event);
  602. /**
  603. * hsi_event - Notifies clients about port events
  604. * @port: Port where the event occurred
  605. * @event: The event type
  606. *
  607. * Clients should not be concerned about wake line behavior. However, due
  608. * to a race condition in HSI HW protocol, clients need to be notified
  609. * about wake line changes, so they can implement a workaround for it.
  610. *
  611. * Events:
  612. * HSI_EVENT_START_RX - Incoming wake line high
  613. * HSI_EVENT_STOP_RX - Incoming wake line down
  614. *
  615. * Returns -errno on error, or 0 on success.
  616. */
  617. int hsi_event(struct hsi_port *port, unsigned long event)
  618. {
  619. return atomic_notifier_call_chain(&port->n_head, event, NULL);
  620. }
  621. EXPORT_SYMBOL_GPL(hsi_event);
  622. /**
  623. * hsi_get_channel_id_by_name - acquire channel id by channel name
  624. * @cl: HSI client, which uses the channel
  625. * @name: name the channel is known under
  626. *
  627. * Clients can call this function to get the hsi channel ids similar to
  628. * requesting IRQs or GPIOs by name. This function assumes the same
  629. * channel configuration is used for RX and TX.
  630. *
  631. * Returns -errno on error or channel id on success.
  632. */
  633. int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name)
  634. {
  635. int i;
  636. if (!cl->rx_cfg.channels)
  637. return -ENOENT;
  638. for (i = 0; i < cl->rx_cfg.num_channels; i++)
  639. if (!strcmp(cl->rx_cfg.channels[i].name, name))
  640. return cl->rx_cfg.channels[i].id;
  641. return -ENXIO;
  642. }
  643. EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name);
  644. static int __init hsi_init(void)
  645. {
  646. return bus_register(&hsi_bus_type);
  647. }
  648. postcore_initcall(hsi_init);
  649. static void __exit hsi_exit(void)
  650. {
  651. bus_unregister(&hsi_bus_type);
  652. }
  653. module_exit(hsi_exit);
  654. MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
  655. MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
  656. MODULE_LICENSE("GPL v2");