hsi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. put_device(&cl->device);
  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)
  124. {
  125. struct hsi_controller *hsi = to_hsi_controller(dev);
  126. kfree(hsi->port);
  127. kfree(hsi);
  128. }
  129. static void hsi_port_release(struct device *dev)
  130. {
  131. kfree(to_hsi_port(dev));
  132. }
  133. /**
  134. * hsi_unregister_controller - Unregister an HSI controller
  135. * @hsi: The HSI controller to register
  136. */
  137. void hsi_unregister_controller(struct hsi_controller *hsi)
  138. {
  139. device_for_each_child(&hsi->device, NULL, hsi_remove_port);
  140. device_unregister(&hsi->device);
  141. }
  142. EXPORT_SYMBOL_GPL(hsi_unregister_controller);
  143. /**
  144. * hsi_register_controller - Register an HSI controller and its ports
  145. * @hsi: The HSI controller to register
  146. *
  147. * Returns -errno on failure, 0 on success.
  148. */
  149. int hsi_register_controller(struct hsi_controller *hsi)
  150. {
  151. unsigned int i;
  152. int err;
  153. hsi->device.type = &hsi_ctrl;
  154. hsi->device.bus = &hsi_bus_type;
  155. err = device_add(&hsi->device);
  156. if (err < 0)
  157. return err;
  158. for (i = 0; i < hsi->num_ports; i++) {
  159. hsi->port[i]->device.parent = &hsi->device;
  160. hsi->port[i]->device.bus = &hsi_bus_type;
  161. hsi->port[i]->device.type = &hsi_port;
  162. err = device_add(&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. while (i-- > 0)
  171. device_del(&hsi->port[i]->device);
  172. device_del(&hsi->device);
  173. return err;
  174. }
  175. EXPORT_SYMBOL_GPL(hsi_register_controller);
  176. /**
  177. * hsi_register_client_driver - Register an HSI client to the HSI bus
  178. * @drv: HSI client driver to register
  179. *
  180. * Returns -errno on failure, 0 on success.
  181. */
  182. int hsi_register_client_driver(struct hsi_client_driver *drv)
  183. {
  184. drv->driver.bus = &hsi_bus_type;
  185. return driver_register(&drv->driver);
  186. }
  187. EXPORT_SYMBOL_GPL(hsi_register_client_driver);
  188. static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
  189. {
  190. return 0;
  191. }
  192. static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
  193. {
  194. return 0;
  195. }
  196. /**
  197. * hsi_put_controller - Free an HSI controller
  198. *
  199. * @hsi: Pointer to the HSI controller to freed
  200. *
  201. * HSI controller drivers should only use this function if they need
  202. * to free their allocated hsi_controller structures before a successful
  203. * call to hsi_register_controller. Other use is not allowed.
  204. */
  205. void hsi_put_controller(struct hsi_controller *hsi)
  206. {
  207. unsigned int i;
  208. if (!hsi)
  209. return;
  210. for (i = 0; i < hsi->num_ports; i++)
  211. if (hsi->port && hsi->port[i])
  212. put_device(&hsi->port[i]->device);
  213. put_device(&hsi->device);
  214. }
  215. EXPORT_SYMBOL_GPL(hsi_put_controller);
  216. /**
  217. * hsi_alloc_controller - Allocate an HSI controller and its ports
  218. * @n_ports: Number of ports on the HSI controller
  219. * @flags: Kernel allocation flags
  220. *
  221. * Return NULL on failure or a pointer to an hsi_controller on success.
  222. */
  223. struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
  224. {
  225. struct hsi_controller *hsi;
  226. struct hsi_port **port;
  227. unsigned int i;
  228. if (!n_ports)
  229. return NULL;
  230. hsi = kzalloc(sizeof(*hsi), flags);
  231. if (!hsi)
  232. return NULL;
  233. port = kzalloc(sizeof(*port)*n_ports, flags);
  234. if (!port) {
  235. kfree(hsi);
  236. return NULL;
  237. }
  238. hsi->num_ports = n_ports;
  239. hsi->port = port;
  240. hsi->device.release = hsi_controller_release;
  241. device_initialize(&hsi->device);
  242. for (i = 0; i < n_ports; i++) {
  243. port[i] = kzalloc(sizeof(**port), flags);
  244. if (port[i] == NULL)
  245. goto out;
  246. port[i]->num = i;
  247. port[i]->async = hsi_dummy_msg;
  248. port[i]->setup = hsi_dummy_cl;
  249. port[i]->flush = hsi_dummy_cl;
  250. port[i]->start_tx = hsi_dummy_cl;
  251. port[i]->stop_tx = hsi_dummy_cl;
  252. port[i]->release = hsi_dummy_cl;
  253. mutex_init(&port[i]->lock);
  254. INIT_LIST_HEAD(&hsi->port[i]->clients);
  255. spin_lock_init(&hsi->port[i]->clock);
  256. dev_set_name(&port[i]->device, "port%d", i);
  257. hsi->port[i]->device.release = hsi_port_release;
  258. device_initialize(&hsi->port[i]->device);
  259. }
  260. return hsi;
  261. out:
  262. hsi_put_controller(hsi);
  263. return NULL;
  264. }
  265. EXPORT_SYMBOL_GPL(hsi_alloc_controller);
  266. /**
  267. * hsi_free_msg - Free an HSI message
  268. * @msg: Pointer to the HSI message
  269. *
  270. * Client is responsible to free the buffers pointed by the scatterlists.
  271. */
  272. void hsi_free_msg(struct hsi_msg *msg)
  273. {
  274. if (!msg)
  275. return;
  276. sg_free_table(&msg->sgt);
  277. kfree(msg);
  278. }
  279. EXPORT_SYMBOL_GPL(hsi_free_msg);
  280. /**
  281. * hsi_alloc_msg - Allocate an HSI message
  282. * @nents: Number of memory entries
  283. * @flags: Kernel allocation flags
  284. *
  285. * nents can be 0. This mainly makes sense for read transfer.
  286. * In that case, HSI drivers will call the complete callback when
  287. * there is data to be read without consuming it.
  288. *
  289. * Return NULL on failure or a pointer to an hsi_msg on success.
  290. */
  291. struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
  292. {
  293. struct hsi_msg *msg;
  294. int err;
  295. msg = kzalloc(sizeof(*msg), flags);
  296. if (!msg)
  297. return NULL;
  298. if (!nents)
  299. return msg;
  300. err = sg_alloc_table(&msg->sgt, nents, flags);
  301. if (unlikely(err)) {
  302. kfree(msg);
  303. msg = NULL;
  304. }
  305. return msg;
  306. }
  307. EXPORT_SYMBOL_GPL(hsi_alloc_msg);
  308. /**
  309. * hsi_async - Submit an HSI transfer to the controller
  310. * @cl: HSI client sending the transfer
  311. * @msg: The HSI transfer passed to controller
  312. *
  313. * The HSI message must have the channel, ttype, complete and destructor
  314. * fields set beforehand. If nents > 0 then the client has to initialize
  315. * also the scatterlists to point to the buffers to write to or read from.
  316. *
  317. * HSI controllers relay on pre-allocated buffers from their clients and they
  318. * do not allocate buffers on their own.
  319. *
  320. * Once the HSI message transfer finishes, the HSI controller calls the
  321. * complete callback with the status and actual_len fields of the HSI message
  322. * updated. The complete callback can be called before returning from
  323. * hsi_async.
  324. *
  325. * Returns -errno on failure or 0 on success
  326. */
  327. int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
  328. {
  329. struct hsi_port *port = hsi_get_port(cl);
  330. if (!hsi_port_claimed(cl))
  331. return -EACCES;
  332. WARN_ON_ONCE(!msg->destructor || !msg->complete);
  333. msg->cl = cl;
  334. return port->async(msg);
  335. }
  336. EXPORT_SYMBOL_GPL(hsi_async);
  337. /**
  338. * hsi_claim_port - Claim the HSI client's port
  339. * @cl: HSI client that wants to claim its port
  340. * @share: Flag to indicate if the client wants to share the port or not.
  341. *
  342. * Returns -errno on failure, 0 on success.
  343. */
  344. int hsi_claim_port(struct hsi_client *cl, unsigned int share)
  345. {
  346. struct hsi_port *port = hsi_get_port(cl);
  347. int err = 0;
  348. mutex_lock(&port->lock);
  349. if ((port->claimed) && (!port->shared || !share)) {
  350. err = -EBUSY;
  351. goto out;
  352. }
  353. if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
  354. err = -ENODEV;
  355. goto out;
  356. }
  357. port->claimed++;
  358. port->shared = !!share;
  359. cl->pclaimed = 1;
  360. out:
  361. mutex_unlock(&port->lock);
  362. return err;
  363. }
  364. EXPORT_SYMBOL_GPL(hsi_claim_port);
  365. /**
  366. * hsi_release_port - Release the HSI client's port
  367. * @cl: HSI client which previously claimed its port
  368. */
  369. void hsi_release_port(struct hsi_client *cl)
  370. {
  371. struct hsi_port *port = hsi_get_port(cl);
  372. mutex_lock(&port->lock);
  373. /* Allow HW driver to do some cleanup */
  374. port->release(cl);
  375. if (cl->pclaimed)
  376. port->claimed--;
  377. BUG_ON(port->claimed < 0);
  378. cl->pclaimed = 0;
  379. if (!port->claimed)
  380. port->shared = 0;
  381. module_put(to_hsi_controller(port->device.parent)->owner);
  382. mutex_unlock(&port->lock);
  383. }
  384. EXPORT_SYMBOL_GPL(hsi_release_port);
  385. static int hsi_start_rx(struct hsi_client *cl, void *data __maybe_unused)
  386. {
  387. if (cl->hsi_start_rx)
  388. (*cl->hsi_start_rx)(cl);
  389. return 0;
  390. }
  391. static int hsi_stop_rx(struct hsi_client *cl, void *data __maybe_unused)
  392. {
  393. if (cl->hsi_stop_rx)
  394. (*cl->hsi_stop_rx)(cl);
  395. return 0;
  396. }
  397. static int hsi_port_for_each_client(struct hsi_port *port, void *data,
  398. int (*fn)(struct hsi_client *cl, void *data))
  399. {
  400. struct hsi_client *cl;
  401. spin_lock(&port->clock);
  402. list_for_each_entry(cl, &port->clients, link) {
  403. spin_unlock(&port->clock);
  404. (*fn)(cl, data);
  405. spin_lock(&port->clock);
  406. }
  407. spin_unlock(&port->clock);
  408. return 0;
  409. }
  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. void hsi_event(struct hsi_port *port, unsigned int event)
  424. {
  425. int (*fn)(struct hsi_client *cl, void *data);
  426. switch (event) {
  427. case HSI_EVENT_START_RX:
  428. fn = hsi_start_rx;
  429. break;
  430. case HSI_EVENT_STOP_RX:
  431. fn = hsi_stop_rx;
  432. break;
  433. default:
  434. return;
  435. }
  436. hsi_port_for_each_client(port, NULL, fn);
  437. }
  438. EXPORT_SYMBOL_GPL(hsi_event);
  439. static int __init hsi_init(void)
  440. {
  441. return bus_register(&hsi_bus_type);
  442. }
  443. postcore_initcall(hsi_init);
  444. static void __exit hsi_exit(void)
  445. {
  446. bus_unregister(&hsi_bus_type);
  447. }
  448. module_exit(hsi_exit);
  449. MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
  450. MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
  451. MODULE_LICENSE("GPL v2");