hsi.c 18 KB

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