hsi.c 12 KB

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