hsi.c 12 KB

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