hte.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2021-2022 NVIDIA Corporation
  4. *
  5. * Author: Dipen Patel <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/err.h>
  10. #include <linux/slab.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/mutex.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/hte.h>
  16. #include <linux/delay.h>
  17. #include <linux/debugfs.h>
  18. #define HTE_TS_NAME_LEN 10
  19. /* Global list of the HTE devices */
  20. static DEFINE_SPINLOCK(hte_lock);
  21. static LIST_HEAD(hte_devices);
  22. enum {
  23. HTE_TS_REGISTERED,
  24. HTE_TS_REQ,
  25. HTE_TS_DISABLE,
  26. HTE_TS_QUEUE_WK,
  27. };
  28. /**
  29. * struct hte_ts_info - Information related to requested timestamp.
  30. *
  31. * @xlated_id: Timestamp ID as understood between HTE subsys and HTE provider,
  32. * See xlate callback API.
  33. * @flags: Flags holding state information.
  34. * @hte_cb_flags: Callback related flags.
  35. * @seq: Timestamp sequence counter.
  36. * @line_name: HTE allocated line name.
  37. * @free_attr_name: If set, free the attr name.
  38. * @cb: A nonsleeping callback function provided by clients.
  39. * @tcb: A secondary sleeping callback function provided by clients.
  40. * @dropped_ts: Dropped timestamps.
  41. * @slock: Spin lock to synchronize between disable/enable,
  42. * request/release APIs.
  43. * @cb_work: callback workqueue, used when tcb is specified.
  44. * @req_mlock: Lock during timestamp request/release APIs.
  45. * @ts_dbg_root: Root for the debug fs.
  46. * @gdev: HTE abstract device that this timestamp information belongs to.
  47. * @cl_data: Client specific data.
  48. */
  49. struct hte_ts_info {
  50. u32 xlated_id;
  51. unsigned long flags;
  52. unsigned long hte_cb_flags;
  53. u64 seq;
  54. char *line_name;
  55. bool free_attr_name;
  56. hte_ts_cb_t cb;
  57. hte_ts_sec_cb_t tcb;
  58. atomic_t dropped_ts;
  59. spinlock_t slock;
  60. struct work_struct cb_work;
  61. struct mutex req_mlock;
  62. struct dentry *ts_dbg_root;
  63. struct hte_device *gdev;
  64. void *cl_data;
  65. };
  66. /**
  67. * struct hte_device - HTE abstract device
  68. * @nlines: Number of entities this device supports.
  69. * @ts_req: Total number of entities requested.
  70. * @sdev: Device used at various debug prints.
  71. * @dbg_root: Root directory for debug fs.
  72. * @list: List node to store hte_device for each provider.
  73. * @chip: HTE chip providing this HTE device.
  74. * @owner: helps prevent removal of modules when in use.
  75. * @ei: Timestamp information.
  76. */
  77. struct hte_device {
  78. u32 nlines;
  79. atomic_t ts_req;
  80. struct device *sdev;
  81. struct dentry *dbg_root;
  82. struct list_head list;
  83. struct hte_chip *chip;
  84. struct module *owner;
  85. struct hte_ts_info ei[];
  86. };
  87. #ifdef CONFIG_DEBUG_FS
  88. static struct dentry *hte_root;
  89. static int __init hte_subsys_dbgfs_init(void)
  90. {
  91. /* creates /sys/kernel/debug/hte/ */
  92. hte_root = debugfs_create_dir("hte", NULL);
  93. return 0;
  94. }
  95. subsys_initcall(hte_subsys_dbgfs_init);
  96. static void hte_chip_dbgfs_init(struct hte_device *gdev)
  97. {
  98. const struct hte_chip *chip = gdev->chip;
  99. const char *name = chip->name ? chip->name : dev_name(chip->dev);
  100. gdev->dbg_root = debugfs_create_dir(name, hte_root);
  101. debugfs_create_atomic_t("ts_requested", 0444, gdev->dbg_root,
  102. &gdev->ts_req);
  103. debugfs_create_u32("total_ts", 0444, gdev->dbg_root,
  104. &gdev->nlines);
  105. }
  106. static void hte_ts_dbgfs_init(const char *name, struct hte_ts_info *ei)
  107. {
  108. if (!ei->gdev->dbg_root || !name)
  109. return;
  110. ei->ts_dbg_root = debugfs_create_dir(name, ei->gdev->dbg_root);
  111. debugfs_create_atomic_t("dropped_timestamps", 0444, ei->ts_dbg_root,
  112. &ei->dropped_ts);
  113. }
  114. #else
  115. static void hte_chip_dbgfs_init(struct hte_device *gdev)
  116. {
  117. }
  118. static void hte_ts_dbgfs_init(const char *name, struct hte_ts_info *ei)
  119. {
  120. }
  121. #endif
  122. /**
  123. * hte_ts_put() - Release and disable timestamp for the given desc.
  124. *
  125. * @desc: timestamp descriptor.
  126. *
  127. * Context: debugfs_remove_recursive() function call may use sleeping locks,
  128. * not suitable from atomic context.
  129. * Returns: 0 on success or a negative error code on failure.
  130. */
  131. int hte_ts_put(struct hte_ts_desc *desc)
  132. {
  133. int ret = 0;
  134. unsigned long flag;
  135. struct hte_device *gdev;
  136. struct hte_ts_info *ei;
  137. if (!desc)
  138. return -EINVAL;
  139. ei = desc->hte_data;
  140. if (!ei || !ei->gdev)
  141. return -EINVAL;
  142. gdev = ei->gdev;
  143. mutex_lock(&ei->req_mlock);
  144. if (unlikely(!test_bit(HTE_TS_REQ, &ei->flags) &&
  145. !test_bit(HTE_TS_REGISTERED, &ei->flags))) {
  146. dev_info(gdev->sdev, "id:%d is not requested\n",
  147. desc->attr.line_id);
  148. ret = -EINVAL;
  149. goto unlock;
  150. }
  151. if (unlikely(!test_bit(HTE_TS_REQ, &ei->flags) &&
  152. test_bit(HTE_TS_REGISTERED, &ei->flags))) {
  153. dev_info(gdev->sdev, "id:%d is registered but not requested\n",
  154. desc->attr.line_id);
  155. ret = -EINVAL;
  156. goto unlock;
  157. }
  158. if (test_bit(HTE_TS_REQ, &ei->flags) &&
  159. !test_bit(HTE_TS_REGISTERED, &ei->flags)) {
  160. clear_bit(HTE_TS_REQ, &ei->flags);
  161. desc->hte_data = NULL;
  162. ret = 0;
  163. goto mod_put;
  164. }
  165. ret = gdev->chip->ops->release(gdev->chip, desc, ei->xlated_id);
  166. if (ret) {
  167. dev_err(gdev->sdev, "id: %d free failed\n",
  168. desc->attr.line_id);
  169. goto unlock;
  170. }
  171. kfree(ei->line_name);
  172. if (ei->free_attr_name)
  173. kfree_const(desc->attr.name);
  174. debugfs_remove_recursive(ei->ts_dbg_root);
  175. spin_lock_irqsave(&ei->slock, flag);
  176. if (test_bit(HTE_TS_QUEUE_WK, &ei->flags)) {
  177. spin_unlock_irqrestore(&ei->slock, flag);
  178. flush_work(&ei->cb_work);
  179. spin_lock_irqsave(&ei->slock, flag);
  180. }
  181. atomic_dec(&gdev->ts_req);
  182. atomic_set(&ei->dropped_ts, 0);
  183. ei->seq = 1;
  184. ei->flags = 0;
  185. desc->hte_data = NULL;
  186. spin_unlock_irqrestore(&ei->slock, flag);
  187. ei->cb = NULL;
  188. ei->tcb = NULL;
  189. ei->cl_data = NULL;
  190. mod_put:
  191. module_put(gdev->owner);
  192. unlock:
  193. mutex_unlock(&ei->req_mlock);
  194. dev_dbg(gdev->sdev, "release id: %d\n", desc->attr.line_id);
  195. return ret;
  196. }
  197. EXPORT_SYMBOL_GPL(hte_ts_put);
  198. static int hte_ts_dis_en_common(struct hte_ts_desc *desc, bool en)
  199. {
  200. u32 ts_id;
  201. struct hte_device *gdev;
  202. struct hte_ts_info *ei;
  203. int ret;
  204. unsigned long flag;
  205. if (!desc)
  206. return -EINVAL;
  207. ei = desc->hte_data;
  208. if (!ei || !ei->gdev)
  209. return -EINVAL;
  210. gdev = ei->gdev;
  211. ts_id = desc->attr.line_id;
  212. mutex_lock(&ei->req_mlock);
  213. if (!test_bit(HTE_TS_REGISTERED, &ei->flags)) {
  214. dev_dbg(gdev->sdev, "id:%d is not registered", ts_id);
  215. ret = -EUSERS;
  216. goto out;
  217. }
  218. spin_lock_irqsave(&ei->slock, flag);
  219. if (en) {
  220. if (!test_bit(HTE_TS_DISABLE, &ei->flags)) {
  221. ret = 0;
  222. goto out_unlock;
  223. }
  224. spin_unlock_irqrestore(&ei->slock, flag);
  225. ret = gdev->chip->ops->enable(gdev->chip, ei->xlated_id);
  226. if (ret) {
  227. dev_warn(gdev->sdev, "id: %d enable failed\n",
  228. ts_id);
  229. goto out;
  230. }
  231. spin_lock_irqsave(&ei->slock, flag);
  232. clear_bit(HTE_TS_DISABLE, &ei->flags);
  233. } else {
  234. if (test_bit(HTE_TS_DISABLE, &ei->flags)) {
  235. ret = 0;
  236. goto out_unlock;
  237. }
  238. spin_unlock_irqrestore(&ei->slock, flag);
  239. ret = gdev->chip->ops->disable(gdev->chip, ei->xlated_id);
  240. if (ret) {
  241. dev_warn(gdev->sdev, "id: %d disable failed\n",
  242. ts_id);
  243. goto out;
  244. }
  245. spin_lock_irqsave(&ei->slock, flag);
  246. set_bit(HTE_TS_DISABLE, &ei->flags);
  247. }
  248. out_unlock:
  249. spin_unlock_irqrestore(&ei->slock, flag);
  250. out:
  251. mutex_unlock(&ei->req_mlock);
  252. return ret;
  253. }
  254. /**
  255. * hte_disable_ts() - Disable timestamp on given descriptor.
  256. *
  257. * The API does not release any resources associated with desc.
  258. *
  259. * @desc: ts descriptor, this is the same as returned by the request API.
  260. *
  261. * Context: Holds mutex lock, not suitable from atomic context.
  262. * Returns: 0 on success or a negative error code on failure.
  263. */
  264. int hte_disable_ts(struct hte_ts_desc *desc)
  265. {
  266. return hte_ts_dis_en_common(desc, false);
  267. }
  268. EXPORT_SYMBOL_GPL(hte_disable_ts);
  269. /**
  270. * hte_enable_ts() - Enable timestamp on given descriptor.
  271. *
  272. * @desc: ts descriptor, this is the same as returned by the request API.
  273. *
  274. * Context: Holds mutex lock, not suitable from atomic context.
  275. * Returns: 0 on success or a negative error code on failure.
  276. */
  277. int hte_enable_ts(struct hte_ts_desc *desc)
  278. {
  279. return hte_ts_dis_en_common(desc, true);
  280. }
  281. EXPORT_SYMBOL_GPL(hte_enable_ts);
  282. static void hte_do_cb_work(struct work_struct *w)
  283. {
  284. unsigned long flag;
  285. struct hte_ts_info *ei = container_of(w, struct hte_ts_info, cb_work);
  286. if (unlikely(!ei->tcb))
  287. return;
  288. ei->tcb(ei->cl_data);
  289. spin_lock_irqsave(&ei->slock, flag);
  290. clear_bit(HTE_TS_QUEUE_WK, &ei->flags);
  291. spin_unlock_irqrestore(&ei->slock, flag);
  292. }
  293. static int __hte_req_ts(struct hte_ts_desc *desc, hte_ts_cb_t cb,
  294. hte_ts_sec_cb_t tcb, void *data)
  295. {
  296. int ret;
  297. struct hte_device *gdev;
  298. struct hte_ts_info *ei = desc->hte_data;
  299. gdev = ei->gdev;
  300. /*
  301. * There is a chance that multiple consumers requesting same entity,
  302. * lock here.
  303. */
  304. mutex_lock(&ei->req_mlock);
  305. if (test_bit(HTE_TS_REGISTERED, &ei->flags) ||
  306. !test_bit(HTE_TS_REQ, &ei->flags)) {
  307. dev_dbg(gdev->chip->dev, "id:%u req failed\n",
  308. desc->attr.line_id);
  309. ret = -EUSERS;
  310. goto unlock;
  311. }
  312. ei->cb = cb;
  313. ei->tcb = tcb;
  314. if (tcb)
  315. INIT_WORK(&ei->cb_work, hte_do_cb_work);
  316. ret = gdev->chip->ops->request(gdev->chip, desc, ei->xlated_id);
  317. if (ret < 0) {
  318. dev_err(gdev->chip->dev, "ts request failed\n");
  319. goto unlock;
  320. }
  321. ei->cl_data = data;
  322. ei->seq = 1;
  323. atomic_inc(&gdev->ts_req);
  324. ei->line_name = NULL;
  325. if (!desc->attr.name) {
  326. ei->line_name = kzalloc(HTE_TS_NAME_LEN, GFP_KERNEL);
  327. if (ei->line_name)
  328. scnprintf(ei->line_name, HTE_TS_NAME_LEN, "ts_%u",
  329. desc->attr.line_id);
  330. }
  331. hte_ts_dbgfs_init(desc->attr.name == NULL ?
  332. ei->line_name : desc->attr.name, ei);
  333. set_bit(HTE_TS_REGISTERED, &ei->flags);
  334. dev_dbg(gdev->chip->dev, "id: %u, xlated id:%u",
  335. desc->attr.line_id, ei->xlated_id);
  336. ret = 0;
  337. unlock:
  338. mutex_unlock(&ei->req_mlock);
  339. return ret;
  340. }
  341. static int hte_bind_ts_info_locked(struct hte_ts_info *ei,
  342. struct hte_ts_desc *desc, u32 x_id)
  343. {
  344. int ret = 0;
  345. mutex_lock(&ei->req_mlock);
  346. if (test_bit(HTE_TS_REQ, &ei->flags)) {
  347. dev_dbg(ei->gdev->chip->dev, "id:%u is already requested\n",
  348. desc->attr.line_id);
  349. ret = -EUSERS;
  350. goto out;
  351. }
  352. set_bit(HTE_TS_REQ, &ei->flags);
  353. desc->hte_data = ei;
  354. ei->xlated_id = x_id;
  355. out:
  356. mutex_unlock(&ei->req_mlock);
  357. return ret;
  358. }
  359. static struct hte_device *of_node_to_htedevice(struct device_node *np)
  360. {
  361. struct hte_device *gdev;
  362. spin_lock(&hte_lock);
  363. list_for_each_entry(gdev, &hte_devices, list)
  364. if (gdev->chip && gdev->chip->dev &&
  365. gdev->chip->dev->of_node == np) {
  366. spin_unlock(&hte_lock);
  367. return gdev;
  368. }
  369. spin_unlock(&hte_lock);
  370. return ERR_PTR(-ENODEV);
  371. }
  372. static struct hte_device *hte_find_dev_from_linedata(struct hte_ts_desc *desc)
  373. {
  374. struct hte_device *gdev;
  375. spin_lock(&hte_lock);
  376. list_for_each_entry(gdev, &hte_devices, list)
  377. if (gdev->chip && gdev->chip->match_from_linedata) {
  378. if (!gdev->chip->match_from_linedata(gdev->chip, desc))
  379. continue;
  380. spin_unlock(&hte_lock);
  381. return gdev;
  382. }
  383. spin_unlock(&hte_lock);
  384. return ERR_PTR(-ENODEV);
  385. }
  386. /**
  387. * of_hte_req_count - Return the number of entities to timestamp.
  388. *
  389. * The function returns the total count of the requested entities to timestamp
  390. * by parsing device tree.
  391. *
  392. * @dev: The HTE consumer.
  393. *
  394. * Returns: Positive number on success, -ENOENT if no entries,
  395. * -EINVAL for other errors.
  396. */
  397. int of_hte_req_count(struct device *dev)
  398. {
  399. int count;
  400. if (!dev || !dev->of_node)
  401. return -EINVAL;
  402. count = of_count_phandle_with_args(dev->of_node, "timestamps",
  403. "#timestamp-cells");
  404. return count ? count : -ENOENT;
  405. }
  406. EXPORT_SYMBOL_GPL(of_hte_req_count);
  407. static inline struct hte_device *hte_get_dev(struct hte_ts_desc *desc)
  408. {
  409. return hte_find_dev_from_linedata(desc);
  410. }
  411. static struct hte_device *hte_of_get_dev(struct device *dev,
  412. struct hte_ts_desc *desc,
  413. int index,
  414. struct of_phandle_args *args,
  415. bool *free_name)
  416. {
  417. int ret;
  418. struct device_node *np;
  419. char *temp;
  420. if (!dev->of_node)
  421. return ERR_PTR(-EINVAL);
  422. np = dev->of_node;
  423. if (!of_find_property(np, "timestamp-names", NULL)) {
  424. /* Let hte core construct it during request time */
  425. desc->attr.name = NULL;
  426. } else {
  427. ret = of_property_read_string_index(np, "timestamp-names",
  428. index, &desc->attr.name);
  429. if (ret) {
  430. pr_err("can't parse \"timestamp-names\" property\n");
  431. return ERR_PTR(ret);
  432. }
  433. *free_name = false;
  434. if (desc->attr.name) {
  435. temp = skip_spaces(desc->attr.name);
  436. if (!*temp)
  437. desc->attr.name = NULL;
  438. }
  439. }
  440. ret = of_parse_phandle_with_args(np, "timestamps", "#timestamp-cells",
  441. index, args);
  442. if (ret) {
  443. pr_err("%s(): can't parse \"timestamps\" property\n",
  444. __func__);
  445. return ERR_PTR(ret);
  446. }
  447. of_node_put(args->np);
  448. return of_node_to_htedevice(args->np);
  449. }
  450. /**
  451. * hte_ts_get() - The function to initialize and obtain HTE desc.
  452. *
  453. * The function initializes the consumer provided HTE descriptor. If consumer
  454. * has device tree node, index is used to parse the line id and other details.
  455. * The function needs to be called before using any request APIs.
  456. *
  457. * @dev: HTE consumer/client device, used in case of parsing device tree node.
  458. * @desc: Pre-allocated timestamp descriptor.
  459. * @index: The index will be used as an index to parse line_id from the
  460. * device tree node if node is present.
  461. *
  462. * Context: Holds mutex lock.
  463. * Returns: Returns 0 on success or negative error code on failure.
  464. */
  465. int hte_ts_get(struct device *dev, struct hte_ts_desc *desc, int index)
  466. {
  467. struct hte_device *gdev;
  468. struct hte_ts_info *ei;
  469. const struct fwnode_handle *fwnode;
  470. struct of_phandle_args args;
  471. u32 xlated_id;
  472. int ret;
  473. bool free_name = false;
  474. if (!desc)
  475. return -EINVAL;
  476. fwnode = dev ? dev_fwnode(dev) : NULL;
  477. if (is_of_node(fwnode))
  478. gdev = hte_of_get_dev(dev, desc, index, &args, &free_name);
  479. else
  480. gdev = hte_get_dev(desc);
  481. if (IS_ERR(gdev)) {
  482. pr_err("%s() no hte dev found\n", __func__);
  483. return PTR_ERR(gdev);
  484. }
  485. if (!try_module_get(gdev->owner))
  486. return -ENODEV;
  487. if (!gdev->chip) {
  488. pr_err("%s(): requested id does not have provider\n",
  489. __func__);
  490. ret = -ENODEV;
  491. goto put;
  492. }
  493. if (is_of_node(fwnode)) {
  494. if (!gdev->chip->xlate_of)
  495. ret = -EINVAL;
  496. else
  497. ret = gdev->chip->xlate_of(gdev->chip, &args,
  498. desc, &xlated_id);
  499. } else {
  500. if (!gdev->chip->xlate_plat)
  501. ret = -EINVAL;
  502. else
  503. ret = gdev->chip->xlate_plat(gdev->chip, desc,
  504. &xlated_id);
  505. }
  506. if (ret < 0)
  507. goto put;
  508. ei = &gdev->ei[xlated_id];
  509. ret = hte_bind_ts_info_locked(ei, desc, xlated_id);
  510. if (ret)
  511. goto put;
  512. ei->free_attr_name = free_name;
  513. return 0;
  514. put:
  515. module_put(gdev->owner);
  516. return ret;
  517. }
  518. EXPORT_SYMBOL_GPL(hte_ts_get);
  519. static void __devm_hte_release_ts(void *res)
  520. {
  521. hte_ts_put(res);
  522. }
  523. /**
  524. * hte_request_ts_ns() - The API to request and enable hardware timestamp in
  525. * nanoseconds.
  526. *
  527. * The entity is provider specific for example, GPIO lines, signals, buses
  528. * etc...The API allocates necessary resources and enables the timestamp.
  529. *
  530. * @desc: Pre-allocated and initialized timestamp descriptor.
  531. * @cb: Callback to push the timestamp data to consumer.
  532. * @tcb: Optional callback. If its provided, subsystem initializes
  533. * workqueue. It is called when cb returns HTE_RUN_SECOND_CB.
  534. * @data: Client data, used during cb and tcb callbacks.
  535. *
  536. * Context: Holds mutex lock.
  537. * Returns: Returns 0 on success or negative error code on failure.
  538. */
  539. int hte_request_ts_ns(struct hte_ts_desc *desc, hte_ts_cb_t cb,
  540. hte_ts_sec_cb_t tcb, void *data)
  541. {
  542. int ret;
  543. struct hte_ts_info *ei;
  544. if (!desc || !desc->hte_data || !cb)
  545. return -EINVAL;
  546. ei = desc->hte_data;
  547. if (!ei || !ei->gdev)
  548. return -EINVAL;
  549. ret = __hte_req_ts(desc, cb, tcb, data);
  550. if (ret < 0) {
  551. dev_err(ei->gdev->chip->dev,
  552. "failed to request id: %d\n", desc->attr.line_id);
  553. return ret;
  554. }
  555. return 0;
  556. }
  557. EXPORT_SYMBOL_GPL(hte_request_ts_ns);
  558. /**
  559. * devm_hte_request_ts_ns() - Resource managed API to request and enable
  560. * hardware timestamp in nanoseconds.
  561. *
  562. * The entity is provider specific for example, GPIO lines, signals, buses
  563. * etc...The API allocates necessary resources and enables the timestamp. It
  564. * deallocates and disables automatically when the consumer exits.
  565. *
  566. * @dev: HTE consumer/client device.
  567. * @desc: Pre-allocated and initialized timestamp descriptor.
  568. * @cb: Callback to push the timestamp data to consumer.
  569. * @tcb: Optional callback. If its provided, subsystem initializes
  570. * workqueue. It is called when cb returns HTE_RUN_SECOND_CB.
  571. * @data: Client data, used during cb and tcb callbacks.
  572. *
  573. * Context: Holds mutex lock.
  574. * Returns: Returns 0 on success or negative error code on failure.
  575. */
  576. int devm_hte_request_ts_ns(struct device *dev, struct hte_ts_desc *desc,
  577. hte_ts_cb_t cb, hte_ts_sec_cb_t tcb,
  578. void *data)
  579. {
  580. int err;
  581. if (!dev)
  582. return -EINVAL;
  583. err = hte_request_ts_ns(desc, cb, tcb, data);
  584. if (err)
  585. return err;
  586. err = devm_add_action_or_reset(dev, __devm_hte_release_ts, desc);
  587. if (err)
  588. return err;
  589. return 0;
  590. }
  591. EXPORT_SYMBOL_GPL(devm_hte_request_ts_ns);
  592. /**
  593. * hte_init_line_attr() - Initialize line attributes.
  594. *
  595. * Zeroes out line attributes and initializes with provided arguments.
  596. * The function needs to be called before calling any consumer facing
  597. * functions.
  598. *
  599. * @desc: Pre-allocated timestamp descriptor.
  600. * @line_id: line id.
  601. * @edge_flags: edge flags related to line_id.
  602. * @name: name of the line.
  603. * @data: line data related to line_id.
  604. *
  605. * Context: Any.
  606. * Returns: 0 on success or negative error code for the failure.
  607. */
  608. int hte_init_line_attr(struct hte_ts_desc *desc, u32 line_id,
  609. unsigned long edge_flags, const char *name, void *data)
  610. {
  611. if (!desc)
  612. return -EINVAL;
  613. memset(&desc->attr, 0, sizeof(desc->attr));
  614. desc->attr.edge_flags = edge_flags;
  615. desc->attr.line_id = line_id;
  616. desc->attr.line_data = data;
  617. if (name) {
  618. name = kstrdup_const(name, GFP_KERNEL);
  619. if (!name)
  620. return -ENOMEM;
  621. }
  622. desc->attr.name = name;
  623. return 0;
  624. }
  625. EXPORT_SYMBOL_GPL(hte_init_line_attr);
  626. /**
  627. * hte_get_clk_src_info() - Get the clock source information for a ts
  628. * descriptor.
  629. *
  630. * @desc: ts descriptor, same as returned from request API.
  631. * @ci: The API fills this structure with the clock information data.
  632. *
  633. * Context: Any context.
  634. * Returns: 0 on success else negative error code on failure.
  635. */
  636. int hte_get_clk_src_info(const struct hte_ts_desc *desc,
  637. struct hte_clk_info *ci)
  638. {
  639. struct hte_chip *chip;
  640. struct hte_ts_info *ei;
  641. if (!desc || !desc->hte_data || !ci) {
  642. pr_debug("%s:%d\n", __func__, __LINE__);
  643. return -EINVAL;
  644. }
  645. ei = desc->hte_data;
  646. if (!ei->gdev || !ei->gdev->chip)
  647. return -EINVAL;
  648. chip = ei->gdev->chip;
  649. if (!chip->ops->get_clk_src_info)
  650. return -EOPNOTSUPP;
  651. return chip->ops->get_clk_src_info(chip, ci);
  652. }
  653. EXPORT_SYMBOL_GPL(hte_get_clk_src_info);
  654. /**
  655. * hte_push_ts_ns() - Push timestamp data in nanoseconds.
  656. *
  657. * It is used by the provider to push timestamp data.
  658. *
  659. * @chip: The HTE chip, used during the registration.
  660. * @xlated_id: entity id understood by both subsystem and provider, this is
  661. * obtained from xlate callback during request API.
  662. * @data: timestamp data.
  663. *
  664. * Returns: 0 on success or a negative error code on failure.
  665. */
  666. int hte_push_ts_ns(const struct hte_chip *chip, u32 xlated_id,
  667. struct hte_ts_data *data)
  668. {
  669. enum hte_return ret;
  670. int st = 0;
  671. struct hte_ts_info *ei;
  672. unsigned long flag;
  673. if (!chip || !data || !chip->gdev)
  674. return -EINVAL;
  675. if (xlated_id >= chip->nlines)
  676. return -EINVAL;
  677. ei = &chip->gdev->ei[xlated_id];
  678. spin_lock_irqsave(&ei->slock, flag);
  679. /* timestamp sequence counter */
  680. data->seq = ei->seq++;
  681. if (!test_bit(HTE_TS_REGISTERED, &ei->flags) ||
  682. test_bit(HTE_TS_DISABLE, &ei->flags)) {
  683. dev_dbg(chip->dev, "Unknown timestamp push\n");
  684. atomic_inc(&ei->dropped_ts);
  685. st = -EINVAL;
  686. goto unlock;
  687. }
  688. ret = ei->cb(data, ei->cl_data);
  689. if (ret == HTE_RUN_SECOND_CB && ei->tcb) {
  690. queue_work(system_unbound_wq, &ei->cb_work);
  691. set_bit(HTE_TS_QUEUE_WK, &ei->flags);
  692. }
  693. unlock:
  694. spin_unlock_irqrestore(&ei->slock, flag);
  695. return st;
  696. }
  697. EXPORT_SYMBOL_GPL(hte_push_ts_ns);
  698. static int hte_register_chip(struct hte_chip *chip)
  699. {
  700. struct hte_device *gdev;
  701. u32 i;
  702. if (!chip || !chip->dev || !chip->dev->of_node)
  703. return -EINVAL;
  704. if (!chip->ops || !chip->ops->request || !chip->ops->release) {
  705. dev_err(chip->dev, "Driver needs to provide ops\n");
  706. return -EINVAL;
  707. }
  708. gdev = kzalloc(struct_size(gdev, ei, chip->nlines), GFP_KERNEL);
  709. if (!gdev)
  710. return -ENOMEM;
  711. gdev->chip = chip;
  712. chip->gdev = gdev;
  713. gdev->nlines = chip->nlines;
  714. gdev->sdev = chip->dev;
  715. for (i = 0; i < chip->nlines; i++) {
  716. gdev->ei[i].gdev = gdev;
  717. mutex_init(&gdev->ei[i].req_mlock);
  718. spin_lock_init(&gdev->ei[i].slock);
  719. }
  720. if (chip->dev->driver)
  721. gdev->owner = chip->dev->driver->owner;
  722. else
  723. gdev->owner = THIS_MODULE;
  724. of_node_get(chip->dev->of_node);
  725. INIT_LIST_HEAD(&gdev->list);
  726. spin_lock(&hte_lock);
  727. list_add_tail(&gdev->list, &hte_devices);
  728. spin_unlock(&hte_lock);
  729. hte_chip_dbgfs_init(gdev);
  730. dev_dbg(chip->dev, "Added hte chip\n");
  731. return 0;
  732. }
  733. static int hte_unregister_chip(struct hte_chip *chip)
  734. {
  735. struct hte_device *gdev;
  736. if (!chip)
  737. return -EINVAL;
  738. gdev = chip->gdev;
  739. spin_lock(&hte_lock);
  740. list_del(&gdev->list);
  741. spin_unlock(&hte_lock);
  742. gdev->chip = NULL;
  743. of_node_put(chip->dev->of_node);
  744. debugfs_remove_recursive(gdev->dbg_root);
  745. kfree(gdev);
  746. dev_dbg(chip->dev, "Removed hte chip\n");
  747. return 0;
  748. }
  749. static void _hte_devm_unregister_chip(void *chip)
  750. {
  751. hte_unregister_chip(chip);
  752. }
  753. /**
  754. * devm_hte_register_chip() - Resource managed API to register HTE chip.
  755. *
  756. * It is used by the provider to register itself with the HTE subsystem.
  757. * The unregistration is done automatically when the provider exits.
  758. *
  759. * @chip: the HTE chip to add to subsystem.
  760. *
  761. * Returns: 0 on success or a negative error code on failure.
  762. */
  763. int devm_hte_register_chip(struct hte_chip *chip)
  764. {
  765. int err;
  766. err = hte_register_chip(chip);
  767. if (err)
  768. return err;
  769. err = devm_add_action_or_reset(chip->dev, _hte_devm_unregister_chip,
  770. chip);
  771. if (err)
  772. return err;
  773. return 0;
  774. }
  775. EXPORT_SYMBOL_GPL(devm_hte_register_chip);