industrialio-trigger.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* The industrial I/O core, trigger handling functions
  3. *
  4. * Copyright (c) 2008 Jonathan Cameron
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/idr.h>
  8. #include <linux/err.h>
  9. #include <linux/device.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/list.h>
  12. #include <linux/slab.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/trigger.h>
  15. #include "iio_core.h"
  16. #include "iio_core_trigger.h"
  17. #include <linux/iio/trigger_consumer.h>
  18. /* RFC - Question of approach
  19. * Make the common case (single sensor single trigger)
  20. * simple by starting trigger capture from when first sensors
  21. * is added.
  22. *
  23. * Complex simultaneous start requires use of 'hold' functionality
  24. * of the trigger. (not implemented)
  25. *
  26. * Any other suggestions?
  27. */
  28. static DEFINE_IDA(iio_trigger_ida);
  29. /* Single list of all available triggers */
  30. static LIST_HEAD(iio_trigger_list);
  31. static DEFINE_MUTEX(iio_trigger_list_lock);
  32. /**
  33. * iio_trigger_read_name() - retrieve useful identifying name
  34. * @dev: device associated with the iio_trigger
  35. * @attr: pointer to the device_attribute structure that is
  36. * being processed
  37. * @buf: buffer to print the name into
  38. *
  39. * Return: a negative number on failure or the number of written
  40. * characters on success.
  41. */
  42. static ssize_t iio_trigger_read_name(struct device *dev,
  43. struct device_attribute *attr,
  44. char *buf)
  45. {
  46. struct iio_trigger *trig = to_iio_trigger(dev);
  47. return sprintf(buf, "%s\n", trig->name);
  48. }
  49. static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
  50. static struct attribute *iio_trig_dev_attrs[] = {
  51. &dev_attr_name.attr,
  52. NULL,
  53. };
  54. ATTRIBUTE_GROUPS(iio_trig_dev);
  55. static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
  56. int __iio_trigger_register(struct iio_trigger *trig_info,
  57. struct module *this_mod)
  58. {
  59. int ret;
  60. trig_info->owner = this_mod;
  61. trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
  62. if (trig_info->id < 0)
  63. return trig_info->id;
  64. /* Set the name used for the sysfs directory etc */
  65. dev_set_name(&trig_info->dev, "trigger%ld",
  66. (unsigned long) trig_info->id);
  67. ret = device_add(&trig_info->dev);
  68. if (ret)
  69. goto error_unregister_id;
  70. /* Add to list of available triggers held by the IIO core */
  71. mutex_lock(&iio_trigger_list_lock);
  72. if (__iio_trigger_find_by_name(trig_info->name)) {
  73. pr_err("Duplicate trigger name '%s'\n", trig_info->name);
  74. ret = -EEXIST;
  75. goto error_device_del;
  76. }
  77. list_add_tail(&trig_info->list, &iio_trigger_list);
  78. mutex_unlock(&iio_trigger_list_lock);
  79. return 0;
  80. error_device_del:
  81. mutex_unlock(&iio_trigger_list_lock);
  82. device_del(&trig_info->dev);
  83. error_unregister_id:
  84. ida_simple_remove(&iio_trigger_ida, trig_info->id);
  85. return ret;
  86. }
  87. EXPORT_SYMBOL(__iio_trigger_register);
  88. void iio_trigger_unregister(struct iio_trigger *trig_info)
  89. {
  90. mutex_lock(&iio_trigger_list_lock);
  91. list_del(&trig_info->list);
  92. mutex_unlock(&iio_trigger_list_lock);
  93. ida_simple_remove(&iio_trigger_ida, trig_info->id);
  94. /* Possible issue in here */
  95. device_del(&trig_info->dev);
  96. }
  97. EXPORT_SYMBOL(iio_trigger_unregister);
  98. int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
  99. {
  100. if (!indio_dev || !trig)
  101. return -EINVAL;
  102. mutex_lock(&indio_dev->mlock);
  103. WARN_ON(indio_dev->trig_readonly);
  104. indio_dev->trig = iio_trigger_get(trig);
  105. indio_dev->trig_readonly = true;
  106. mutex_unlock(&indio_dev->mlock);
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(iio_trigger_set_immutable);
  110. /* Search for trigger by name, assuming iio_trigger_list_lock held */
  111. static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
  112. {
  113. struct iio_trigger *iter;
  114. list_for_each_entry(iter, &iio_trigger_list, list)
  115. if (!strcmp(iter->name, name))
  116. return iter;
  117. return NULL;
  118. }
  119. static struct iio_trigger *iio_trigger_acquire_by_name(const char *name)
  120. {
  121. struct iio_trigger *trig = NULL, *iter;
  122. mutex_lock(&iio_trigger_list_lock);
  123. list_for_each_entry(iter, &iio_trigger_list, list)
  124. if (sysfs_streq(iter->name, name)) {
  125. trig = iter;
  126. iio_trigger_get(trig);
  127. break;
  128. }
  129. mutex_unlock(&iio_trigger_list_lock);
  130. return trig;
  131. }
  132. void iio_trigger_poll(struct iio_trigger *trig)
  133. {
  134. int i;
  135. if (!atomic_read(&trig->use_count)) {
  136. atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  137. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  138. if (trig->subirqs[i].enabled)
  139. generic_handle_irq(trig->subirq_base + i);
  140. else
  141. iio_trigger_notify_done(trig);
  142. }
  143. }
  144. }
  145. EXPORT_SYMBOL(iio_trigger_poll);
  146. irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
  147. {
  148. iio_trigger_poll(private);
  149. return IRQ_HANDLED;
  150. }
  151. EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
  152. void iio_trigger_poll_chained(struct iio_trigger *trig)
  153. {
  154. int i;
  155. if (!atomic_read(&trig->use_count)) {
  156. atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  157. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  158. if (trig->subirqs[i].enabled)
  159. handle_nested_irq(trig->subirq_base + i);
  160. else
  161. iio_trigger_notify_done(trig);
  162. }
  163. }
  164. }
  165. EXPORT_SYMBOL(iio_trigger_poll_chained);
  166. void iio_trigger_notify_done(struct iio_trigger *trig)
  167. {
  168. if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
  169. trig->ops->try_reenable)
  170. if (trig->ops->try_reenable(trig))
  171. /* Missed an interrupt so launch new poll now */
  172. iio_trigger_poll(trig);
  173. }
  174. EXPORT_SYMBOL(iio_trigger_notify_done);
  175. /* Trigger Consumer related functions */
  176. static int iio_trigger_get_irq(struct iio_trigger *trig)
  177. {
  178. int ret;
  179. mutex_lock(&trig->pool_lock);
  180. ret = bitmap_find_free_region(trig->pool,
  181. CONFIG_IIO_CONSUMERS_PER_TRIGGER,
  182. ilog2(1));
  183. mutex_unlock(&trig->pool_lock);
  184. if (ret >= 0)
  185. ret += trig->subirq_base;
  186. return ret;
  187. }
  188. static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
  189. {
  190. mutex_lock(&trig->pool_lock);
  191. clear_bit(irq - trig->subirq_base, trig->pool);
  192. mutex_unlock(&trig->pool_lock);
  193. }
  194. /* Complexity in here. With certain triggers (datardy) an acknowledgement
  195. * may be needed if the pollfuncs do not include the data read for the
  196. * triggering device.
  197. * This is not currently handled. Alternative of not enabling trigger unless
  198. * the relevant function is in there may be the best option.
  199. */
  200. /* Worth protecting against double additions? */
  201. int iio_trigger_attach_poll_func(struct iio_trigger *trig,
  202. struct iio_poll_func *pf)
  203. {
  204. int ret = 0;
  205. bool notinuse
  206. = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  207. /* Prevent the module from being removed whilst attached to a trigger */
  208. __module_get(pf->indio_dev->driver_module);
  209. /* Get irq number */
  210. pf->irq = iio_trigger_get_irq(trig);
  211. if (pf->irq < 0) {
  212. pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
  213. trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  214. goto out_put_module;
  215. }
  216. /* Request irq */
  217. ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
  218. pf->type, pf->name,
  219. pf);
  220. if (ret < 0)
  221. goto out_put_irq;
  222. /* Enable trigger in driver */
  223. if (trig->ops && trig->ops->set_trigger_state && notinuse) {
  224. ret = trig->ops->set_trigger_state(trig, true);
  225. if (ret < 0)
  226. goto out_free_irq;
  227. }
  228. /*
  229. * Check if we just registered to our own trigger: we determine that
  230. * this is the case if the IIO device and the trigger device share the
  231. * same parent device.
  232. */
  233. if (pf->indio_dev->dev.parent == trig->dev.parent)
  234. trig->attached_own_device = true;
  235. return ret;
  236. out_free_irq:
  237. free_irq(pf->irq, pf);
  238. out_put_irq:
  239. iio_trigger_put_irq(trig, pf->irq);
  240. out_put_module:
  241. module_put(pf->indio_dev->driver_module);
  242. return ret;
  243. }
  244. int iio_trigger_detach_poll_func(struct iio_trigger *trig,
  245. struct iio_poll_func *pf)
  246. {
  247. int ret = 0;
  248. bool no_other_users
  249. = (bitmap_weight(trig->pool,
  250. CONFIG_IIO_CONSUMERS_PER_TRIGGER)
  251. == 1);
  252. if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
  253. ret = trig->ops->set_trigger_state(trig, false);
  254. if (ret)
  255. return ret;
  256. }
  257. if (pf->indio_dev->dev.parent == trig->dev.parent)
  258. trig->attached_own_device = false;
  259. iio_trigger_put_irq(trig, pf->irq);
  260. free_irq(pf->irq, pf);
  261. module_put(pf->indio_dev->driver_module);
  262. return ret;
  263. }
  264. irqreturn_t iio_pollfunc_store_time(int irq, void *p)
  265. {
  266. struct iio_poll_func *pf = p;
  267. pf->timestamp = iio_get_time_ns(pf->indio_dev);
  268. return IRQ_WAKE_THREAD;
  269. }
  270. EXPORT_SYMBOL(iio_pollfunc_store_time);
  271. struct iio_poll_func
  272. *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
  273. irqreturn_t (*thread)(int irq, void *p),
  274. int type,
  275. struct iio_dev *indio_dev,
  276. const char *fmt,
  277. ...)
  278. {
  279. va_list vargs;
  280. struct iio_poll_func *pf;
  281. pf = kmalloc(sizeof *pf, GFP_KERNEL);
  282. if (pf == NULL)
  283. return NULL;
  284. va_start(vargs, fmt);
  285. pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
  286. va_end(vargs);
  287. if (pf->name == NULL) {
  288. kfree(pf);
  289. return NULL;
  290. }
  291. pf->h = h;
  292. pf->thread = thread;
  293. pf->type = type;
  294. pf->indio_dev = indio_dev;
  295. return pf;
  296. }
  297. EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
  298. void iio_dealloc_pollfunc(struct iio_poll_func *pf)
  299. {
  300. kfree(pf->name);
  301. kfree(pf);
  302. }
  303. EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
  304. /**
  305. * iio_trigger_read_current() - trigger consumer sysfs query current trigger
  306. * @dev: device associated with an industrial I/O device
  307. * @attr: pointer to the device_attribute structure that
  308. * is being processed
  309. * @buf: buffer where the current trigger name will be printed into
  310. *
  311. * For trigger consumers the current_trigger interface allows the trigger
  312. * used by the device to be queried.
  313. *
  314. * Return: a negative number on failure, the number of characters written
  315. * on success or 0 if no trigger is available
  316. */
  317. static ssize_t iio_trigger_read_current(struct device *dev,
  318. struct device_attribute *attr,
  319. char *buf)
  320. {
  321. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  322. if (indio_dev->trig)
  323. return sprintf(buf, "%s\n", indio_dev->trig->name);
  324. return 0;
  325. }
  326. /**
  327. * iio_trigger_write_current() - trigger consumer sysfs set current trigger
  328. * @dev: device associated with an industrial I/O device
  329. * @attr: device attribute that is being processed
  330. * @buf: string buffer that holds the name of the trigger
  331. * @len: length of the trigger name held by buf
  332. *
  333. * For trigger consumers the current_trigger interface allows the trigger
  334. * used for this device to be specified at run time based on the trigger's
  335. * name.
  336. *
  337. * Return: negative error code on failure or length of the buffer
  338. * on success
  339. */
  340. static ssize_t iio_trigger_write_current(struct device *dev,
  341. struct device_attribute *attr,
  342. const char *buf,
  343. size_t len)
  344. {
  345. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  346. struct iio_trigger *oldtrig = indio_dev->trig;
  347. struct iio_trigger *trig;
  348. int ret;
  349. mutex_lock(&indio_dev->mlock);
  350. if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
  351. mutex_unlock(&indio_dev->mlock);
  352. return -EBUSY;
  353. }
  354. if (indio_dev->trig_readonly) {
  355. mutex_unlock(&indio_dev->mlock);
  356. return -EPERM;
  357. }
  358. mutex_unlock(&indio_dev->mlock);
  359. trig = iio_trigger_acquire_by_name(buf);
  360. if (oldtrig == trig) {
  361. ret = len;
  362. goto out_trigger_put;
  363. }
  364. if (trig && indio_dev->info->validate_trigger) {
  365. ret = indio_dev->info->validate_trigger(indio_dev, trig);
  366. if (ret)
  367. goto out_trigger_put;
  368. }
  369. if (trig && trig->ops && trig->ops->validate_device) {
  370. ret = trig->ops->validate_device(trig, indio_dev);
  371. if (ret)
  372. goto out_trigger_put;
  373. }
  374. indio_dev->trig = trig;
  375. if (oldtrig) {
  376. if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
  377. iio_trigger_detach_poll_func(oldtrig,
  378. indio_dev->pollfunc_event);
  379. iio_trigger_put(oldtrig);
  380. }
  381. if (indio_dev->trig) {
  382. if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
  383. iio_trigger_attach_poll_func(indio_dev->trig,
  384. indio_dev->pollfunc_event);
  385. }
  386. return len;
  387. out_trigger_put:
  388. if (trig)
  389. iio_trigger_put(trig);
  390. return ret;
  391. }
  392. static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
  393. iio_trigger_read_current,
  394. iio_trigger_write_current);
  395. static struct attribute *iio_trigger_consumer_attrs[] = {
  396. &dev_attr_current_trigger.attr,
  397. NULL,
  398. };
  399. static const struct attribute_group iio_trigger_consumer_attr_group = {
  400. .name = "trigger",
  401. .attrs = iio_trigger_consumer_attrs,
  402. };
  403. static void iio_trig_release(struct device *device)
  404. {
  405. struct iio_trigger *trig = to_iio_trigger(device);
  406. int i;
  407. if (trig->subirq_base) {
  408. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  409. irq_modify_status(trig->subirq_base + i,
  410. IRQ_NOAUTOEN,
  411. IRQ_NOREQUEST | IRQ_NOPROBE);
  412. irq_set_chip(trig->subirq_base + i,
  413. NULL);
  414. irq_set_handler(trig->subirq_base + i,
  415. NULL);
  416. }
  417. irq_free_descs(trig->subirq_base,
  418. CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  419. }
  420. kfree(trig->name);
  421. kfree(trig);
  422. }
  423. static const struct device_type iio_trig_type = {
  424. .release = iio_trig_release,
  425. .groups = iio_trig_dev_groups,
  426. };
  427. static void iio_trig_subirqmask(struct irq_data *d)
  428. {
  429. struct irq_chip *chip = irq_data_get_irq_chip(d);
  430. struct iio_trigger *trig
  431. = container_of(chip,
  432. struct iio_trigger, subirq_chip);
  433. trig->subirqs[d->irq - trig->subirq_base].enabled = false;
  434. }
  435. static void iio_trig_subirqunmask(struct irq_data *d)
  436. {
  437. struct irq_chip *chip = irq_data_get_irq_chip(d);
  438. struct iio_trigger *trig
  439. = container_of(chip,
  440. struct iio_trigger, subirq_chip);
  441. trig->subirqs[d->irq - trig->subirq_base].enabled = true;
  442. }
  443. static __printf(1, 0)
  444. struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
  445. {
  446. struct iio_trigger *trig;
  447. int i;
  448. trig = kzalloc(sizeof *trig, GFP_KERNEL);
  449. if (!trig)
  450. return NULL;
  451. trig->dev.type = &iio_trig_type;
  452. trig->dev.bus = &iio_bus_type;
  453. device_initialize(&trig->dev);
  454. mutex_init(&trig->pool_lock);
  455. trig->subirq_base = irq_alloc_descs(-1, 0,
  456. CONFIG_IIO_CONSUMERS_PER_TRIGGER,
  457. 0);
  458. if (trig->subirq_base < 0)
  459. goto free_trig;
  460. trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
  461. if (trig->name == NULL)
  462. goto free_descs;
  463. trig->subirq_chip.name = trig->name;
  464. trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
  465. trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
  466. for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
  467. irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
  468. irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
  469. irq_modify_status(trig->subirq_base + i,
  470. IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
  471. }
  472. return trig;
  473. free_descs:
  474. irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
  475. free_trig:
  476. kfree(trig);
  477. return NULL;
  478. }
  479. struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
  480. {
  481. struct iio_trigger *trig;
  482. va_list vargs;
  483. va_start(vargs, fmt);
  484. trig = viio_trigger_alloc(fmt, vargs);
  485. va_end(vargs);
  486. return trig;
  487. }
  488. EXPORT_SYMBOL(iio_trigger_alloc);
  489. void iio_trigger_free(struct iio_trigger *trig)
  490. {
  491. if (trig)
  492. put_device(&trig->dev);
  493. }
  494. EXPORT_SYMBOL(iio_trigger_free);
  495. static void devm_iio_trigger_release(struct device *dev, void *res)
  496. {
  497. iio_trigger_free(*(struct iio_trigger **)res);
  498. }
  499. /**
  500. * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
  501. * @dev: Device to allocate iio_trigger for
  502. * @fmt: trigger name format. If it includes format
  503. * specifiers, the additional arguments following
  504. * format are formatted and inserted in the resulting
  505. * string replacing their respective specifiers.
  506. *
  507. * Managed iio_trigger_alloc. iio_trigger allocated with this function is
  508. * automatically freed on driver detach.
  509. *
  510. * RETURNS:
  511. * Pointer to allocated iio_trigger on success, NULL on failure.
  512. */
  513. struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
  514. const char *fmt, ...)
  515. {
  516. struct iio_trigger **ptr, *trig;
  517. va_list vargs;
  518. ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
  519. GFP_KERNEL);
  520. if (!ptr)
  521. return NULL;
  522. /* use raw alloc_dr for kmalloc caller tracing */
  523. va_start(vargs, fmt);
  524. trig = viio_trigger_alloc(fmt, vargs);
  525. va_end(vargs);
  526. if (trig) {
  527. *ptr = trig;
  528. devres_add(dev, ptr);
  529. } else {
  530. devres_free(ptr);
  531. }
  532. return trig;
  533. }
  534. EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
  535. static void devm_iio_trigger_unreg(struct device *dev, void *res)
  536. {
  537. iio_trigger_unregister(*(struct iio_trigger **)res);
  538. }
  539. /**
  540. * __devm_iio_trigger_register - Resource-managed iio_trigger_register()
  541. * @dev: device this trigger was allocated for
  542. * @trig_info: trigger to register
  543. * @this_mod: module registering the trigger
  544. *
  545. * Managed iio_trigger_register(). The IIO trigger registered with this
  546. * function is automatically unregistered on driver detach. This function
  547. * calls iio_trigger_register() internally. Refer to that function for more
  548. * information.
  549. *
  550. * RETURNS:
  551. * 0 on success, negative error number on failure.
  552. */
  553. int __devm_iio_trigger_register(struct device *dev,
  554. struct iio_trigger *trig_info,
  555. struct module *this_mod)
  556. {
  557. struct iio_trigger **ptr;
  558. int ret;
  559. ptr = devres_alloc(devm_iio_trigger_unreg, sizeof(*ptr), GFP_KERNEL);
  560. if (!ptr)
  561. return -ENOMEM;
  562. *ptr = trig_info;
  563. ret = __iio_trigger_register(trig_info, this_mod);
  564. if (!ret)
  565. devres_add(dev, ptr);
  566. else
  567. devres_free(ptr);
  568. return ret;
  569. }
  570. EXPORT_SYMBOL_GPL(__devm_iio_trigger_register);
  571. bool iio_trigger_using_own(struct iio_dev *indio_dev)
  572. {
  573. return indio_dev->trig->attached_own_device;
  574. }
  575. EXPORT_SYMBOL(iio_trigger_using_own);
  576. /**
  577. * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
  578. * the same device
  579. * @trig: The IIO trigger to check
  580. * @indio_dev: the IIO device to check
  581. *
  582. * This function can be used as the validate_device callback for triggers that
  583. * can only be attached to their own device.
  584. *
  585. * Return: 0 if both the trigger and the IIO device belong to the same
  586. * device, -EINVAL otherwise.
  587. */
  588. int iio_trigger_validate_own_device(struct iio_trigger *trig,
  589. struct iio_dev *indio_dev)
  590. {
  591. if (indio_dev->dev.parent != trig->dev.parent)
  592. return -EINVAL;
  593. return 0;
  594. }
  595. EXPORT_SYMBOL(iio_trigger_validate_own_device);
  596. void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
  597. {
  598. indio_dev->groups[indio_dev->groupcounter++] =
  599. &iio_trigger_consumer_attr_group;
  600. }
  601. void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
  602. {
  603. /* Clean up an associated but not attached trigger reference */
  604. if (indio_dev->trig)
  605. iio_trigger_put(indio_dev->trig);
  606. }