industrialio-backend.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Framework to handle complex IIO aggregate devices.
  4. *
  5. * The typical architecture is to have one device as the frontend device which
  6. * can be "linked" against one or multiple backend devices. All the IIO and
  7. * userspace interface is expected to be registers/managed by the frontend
  8. * device which will callback into the backends when needed (to get/set some
  9. * configuration that it does not directly control).
  10. *
  11. * -------------------------------------------------------
  12. * ------------------ | ------------ ------------ ------- FPGA|
  13. * | ADC |------------------------| | ADC CORE |---------| DMA CORE |------| RAM | |
  14. * | (Frontend/IIO) | Serial Data (eg: LVDS) | |(backend) |---------| |------| | |
  15. * | |------------------------| ------------ ------------ ------- |
  16. * ------------------ -------------------------------------------------------
  17. *
  18. * The framework interface is pretty simple:
  19. * - Backends should register themselves with devm_iio_backend_register()
  20. * - Frontend devices should get backends with devm_iio_backend_get()
  21. *
  22. * Also to note that the primary target for this framework are converters like
  23. * ADC/DACs so iio_backend_ops will have some operations typical of converter
  24. * devices. On top of that, this is "generic" for all IIO which means any kind
  25. * of device can make use of the framework. That said, If the iio_backend_ops
  26. * struct begins to grow out of control, we can always refactor things so that
  27. * the industrialio-backend.c is only left with the really generic stuff. Then,
  28. * we can build on top of it depending on the needs.
  29. *
  30. * Copyright (C) 2023-2024 Analog Devices Inc.
  31. */
  32. #define dev_fmt(fmt) "iio-backend: " fmt
  33. #include <linux/cleanup.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/device.h>
  36. #include <linux/err.h>
  37. #include <linux/errno.h>
  38. #include <linux/list.h>
  39. #include <linux/module.h>
  40. #include <linux/mutex.h>
  41. #include <linux/property.h>
  42. #include <linux/slab.h>
  43. #include <linux/stringify.h>
  44. #include <linux/types.h>
  45. #include <linux/iio/backend.h>
  46. #include <linux/iio/iio.h>
  47. struct iio_backend {
  48. struct list_head entry;
  49. const struct iio_backend_ops *ops;
  50. struct device *frontend_dev;
  51. struct device *dev;
  52. struct module *owner;
  53. void *priv;
  54. const char *name;
  55. unsigned int cached_reg_addr;
  56. /*
  57. * This index is relative to the frontend. Meaning that for
  58. * frontends with multiple backends, this will be the index of this
  59. * backend. Used for the debugfs directory name.
  60. */
  61. u8 idx;
  62. };
  63. /*
  64. * Helper struct for requesting buffers. This ensures that we have all data
  65. * that we need to free the buffer in a device managed action.
  66. */
  67. struct iio_backend_buffer_pair {
  68. struct iio_backend *back;
  69. struct iio_buffer *buffer;
  70. };
  71. static LIST_HEAD(iio_back_list);
  72. static DEFINE_MUTEX(iio_back_lock);
  73. /*
  74. * Helper macros to call backend ops. Makes sure the option is supported.
  75. */
  76. #define iio_backend_check_op(back, op) ({ \
  77. struct iio_backend *____back = back; \
  78. int ____ret = 0; \
  79. \
  80. if (!____back->ops->op) \
  81. ____ret = -EOPNOTSUPP; \
  82. \
  83. ____ret; \
  84. })
  85. #define iio_backend_op_call(back, op, args...) ({ \
  86. struct iio_backend *__back = back; \
  87. int __ret; \
  88. \
  89. __ret = iio_backend_check_op(__back, op); \
  90. if (!__ret) \
  91. __ret = __back->ops->op(__back, ##args); \
  92. \
  93. __ret; \
  94. })
  95. #define iio_backend_ptr_op_call(back, op, args...) ({ \
  96. struct iio_backend *__back = back; \
  97. void *ptr_err; \
  98. int __ret; \
  99. \
  100. __ret = iio_backend_check_op(__back, op); \
  101. if (__ret) \
  102. ptr_err = ERR_PTR(__ret); \
  103. else \
  104. ptr_err = __back->ops->op(__back, ##args); \
  105. \
  106. ptr_err; \
  107. })
  108. #define iio_backend_void_op_call(back, op, args...) { \
  109. struct iio_backend *__back = back; \
  110. int __ret; \
  111. \
  112. __ret = iio_backend_check_op(__back, op); \
  113. if (!__ret) \
  114. __back->ops->op(__back, ##args); \
  115. else \
  116. dev_dbg(__back->dev, "Op(%s) not implemented\n",\
  117. __stringify(op)); \
  118. }
  119. static ssize_t iio_backend_debugfs_read_reg(struct file *file,
  120. char __user *userbuf,
  121. size_t count, loff_t *ppos)
  122. {
  123. struct iio_backend *back = file->private_data;
  124. char read_buf[20];
  125. unsigned int val;
  126. int ret, len;
  127. ret = iio_backend_op_call(back, debugfs_reg_access,
  128. back->cached_reg_addr, 0, &val);
  129. if (ret)
  130. return ret;
  131. len = scnprintf(read_buf, sizeof(read_buf), "0x%X\n", val);
  132. return simple_read_from_buffer(userbuf, count, ppos, read_buf, len);
  133. }
  134. static ssize_t iio_backend_debugfs_write_reg(struct file *file,
  135. const char __user *userbuf,
  136. size_t count, loff_t *ppos)
  137. {
  138. struct iio_backend *back = file->private_data;
  139. unsigned int val;
  140. char buf[80];
  141. ssize_t rc;
  142. int ret;
  143. if (count >= sizeof(buf))
  144. return -ENOSPC;
  145. rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count);
  146. if (rc < 0)
  147. return rc;
  148. buf[rc] = '\0';
  149. ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);
  150. switch (ret) {
  151. case 1:
  152. return count;
  153. case 2:
  154. ret = iio_backend_op_call(back, debugfs_reg_access,
  155. back->cached_reg_addr, val, NULL);
  156. if (ret)
  157. return ret;
  158. return count;
  159. default:
  160. return -EINVAL;
  161. }
  162. }
  163. static const struct file_operations iio_backend_debugfs_reg_fops = {
  164. .open = simple_open,
  165. .read = iio_backend_debugfs_read_reg,
  166. .write = iio_backend_debugfs_write_reg,
  167. };
  168. static ssize_t iio_backend_debugfs_read_name(struct file *file,
  169. char __user *userbuf,
  170. size_t count, loff_t *ppos)
  171. {
  172. struct iio_backend *back = file->private_data;
  173. char name[128];
  174. int len;
  175. len = scnprintf(name, sizeof(name), "%s\n", back->name);
  176. return simple_read_from_buffer(userbuf, count, ppos, name, len);
  177. }
  178. static const struct file_operations iio_backend_debugfs_name_fops = {
  179. .open = simple_open,
  180. .read = iio_backend_debugfs_read_name,
  181. };
  182. /**
  183. * iio_backend_debugfs_add - Add debugfs interfaces for Backends
  184. * @back: Backend device
  185. * @indio_dev: IIO device
  186. */
  187. void iio_backend_debugfs_add(struct iio_backend *back,
  188. struct iio_dev *indio_dev)
  189. {
  190. struct dentry *d = iio_get_debugfs_dentry(indio_dev);
  191. struct dentry *back_d;
  192. char name[128];
  193. if (!IS_ENABLED(CONFIG_DEBUG_FS) || !d)
  194. return;
  195. if (!back->ops->debugfs_reg_access && !back->name)
  196. return;
  197. snprintf(name, sizeof(name), "backend%d", back->idx);
  198. back_d = debugfs_create_dir(name, d);
  199. if (IS_ERR(back_d))
  200. return;
  201. if (back->ops->debugfs_reg_access)
  202. debugfs_create_file("direct_reg_access", 0600, back_d, back,
  203. &iio_backend_debugfs_reg_fops);
  204. if (back->name)
  205. debugfs_create_file("name", 0400, back_d, back,
  206. &iio_backend_debugfs_name_fops);
  207. }
  208. EXPORT_SYMBOL_NS_GPL(iio_backend_debugfs_add, "IIO_BACKEND");
  209. /**
  210. * iio_backend_debugfs_print_chan_status - Print channel status
  211. * @back: Backend device
  212. * @chan: Channel number
  213. * @buf: Buffer where to print the status
  214. * @len: Available space
  215. *
  216. * One usecase where this is useful is for testing test tones in a digital
  217. * interface and "ask" the backend to dump more details on why a test tone might
  218. * have errors.
  219. *
  220. * RETURNS:
  221. * Number of copied bytes on success, negative error code on failure.
  222. */
  223. ssize_t iio_backend_debugfs_print_chan_status(struct iio_backend *back,
  224. unsigned int chan, char *buf,
  225. size_t len)
  226. {
  227. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  228. return -ENODEV;
  229. return iio_backend_op_call(back, debugfs_print_chan_status, chan, buf,
  230. len);
  231. }
  232. EXPORT_SYMBOL_NS_GPL(iio_backend_debugfs_print_chan_status, "IIO_BACKEND");
  233. /**
  234. * iio_backend_chan_enable - Enable a backend channel
  235. * @back: Backend device
  236. * @chan: Channel number
  237. *
  238. * RETURNS:
  239. * 0 on success, negative error number on failure.
  240. */
  241. int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan)
  242. {
  243. return iio_backend_op_call(back, chan_enable, chan);
  244. }
  245. EXPORT_SYMBOL_NS_GPL(iio_backend_chan_enable, "IIO_BACKEND");
  246. /**
  247. * iio_backend_chan_disable - Disable a backend channel
  248. * @back: Backend device
  249. * @chan: Channel number
  250. *
  251. * RETURNS:
  252. * 0 on success, negative error number on failure.
  253. */
  254. int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan)
  255. {
  256. return iio_backend_op_call(back, chan_disable, chan);
  257. }
  258. EXPORT_SYMBOL_NS_GPL(iio_backend_chan_disable, "IIO_BACKEND");
  259. static void __iio_backend_disable(void *back)
  260. {
  261. iio_backend_void_op_call(back, disable);
  262. }
  263. /**
  264. * iio_backend_disable - Backend disable
  265. * @back: Backend device
  266. */
  267. void iio_backend_disable(struct iio_backend *back)
  268. {
  269. __iio_backend_disable(back);
  270. }
  271. EXPORT_SYMBOL_NS_GPL(iio_backend_disable, "IIO_BACKEND");
  272. /**
  273. * iio_backend_enable - Backend enable
  274. * @back: Backend device
  275. *
  276. * RETURNS:
  277. * 0 on success, negative error number on failure.
  278. */
  279. int iio_backend_enable(struct iio_backend *back)
  280. {
  281. return iio_backend_op_call(back, enable);
  282. }
  283. EXPORT_SYMBOL_NS_GPL(iio_backend_enable, "IIO_BACKEND");
  284. /**
  285. * devm_iio_backend_enable - Device managed backend enable
  286. * @dev: Consumer device for the backend
  287. * @back: Backend device
  288. *
  289. * RETURNS:
  290. * 0 on success, negative error number on failure.
  291. */
  292. int devm_iio_backend_enable(struct device *dev, struct iio_backend *back)
  293. {
  294. int ret;
  295. ret = iio_backend_enable(back);
  296. if (ret)
  297. return ret;
  298. return devm_add_action_or_reset(dev, __iio_backend_disable, back);
  299. }
  300. EXPORT_SYMBOL_NS_GPL(devm_iio_backend_enable, "IIO_BACKEND");
  301. /**
  302. * iio_backend_data_format_set - Configure the channel data format
  303. * @back: Backend device
  304. * @chan: Channel number
  305. * @data: Data format
  306. *
  307. * Properly configure a channel with respect to the expected data format. A
  308. * @struct iio_backend_data_fmt must be passed with the settings.
  309. *
  310. * RETURNS:
  311. * 0 on success, negative error number on failure.
  312. */
  313. int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan,
  314. const struct iio_backend_data_fmt *data)
  315. {
  316. if (!data || data->type >= IIO_BACKEND_DATA_TYPE_MAX)
  317. return -EINVAL;
  318. return iio_backend_op_call(back, data_format_set, chan, data);
  319. }
  320. EXPORT_SYMBOL_NS_GPL(iio_backend_data_format_set, "IIO_BACKEND");
  321. /**
  322. * iio_backend_data_source_set - Select data source
  323. * @back: Backend device
  324. * @chan: Channel number
  325. * @data: Data source
  326. *
  327. * A given backend may have different sources to stream/sync data. This allows
  328. * to choose that source.
  329. *
  330. * RETURNS:
  331. * 0 on success, negative error number on failure.
  332. */
  333. int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan,
  334. enum iio_backend_data_source data)
  335. {
  336. if (data >= IIO_BACKEND_DATA_SOURCE_MAX)
  337. return -EINVAL;
  338. return iio_backend_op_call(back, data_source_set, chan, data);
  339. }
  340. EXPORT_SYMBOL_NS_GPL(iio_backend_data_source_set, "IIO_BACKEND");
  341. /**
  342. * iio_backend_data_source_get - Get current data source
  343. * @back: Backend device
  344. * @chan: Channel number
  345. * @data: Pointer to receive the current source value
  346. *
  347. * A given backend may have different sources to stream/sync data. This allows
  348. * to know what source is in use.
  349. *
  350. * RETURNS:
  351. * 0 on success, negative error number on failure.
  352. */
  353. int iio_backend_data_source_get(struct iio_backend *back, unsigned int chan,
  354. enum iio_backend_data_source *data)
  355. {
  356. int ret;
  357. ret = iio_backend_op_call(back, data_source_get, chan, data);
  358. if (ret)
  359. return ret;
  360. if (*data >= IIO_BACKEND_DATA_SOURCE_MAX)
  361. return -EINVAL;
  362. return 0;
  363. }
  364. EXPORT_SYMBOL_NS_GPL(iio_backend_data_source_get, "IIO_BACKEND");
  365. /**
  366. * iio_backend_set_sampling_freq - Set channel sampling rate
  367. * @back: Backend device
  368. * @chan: Channel number
  369. * @sample_rate_hz: Sample rate
  370. *
  371. * RETURNS:
  372. * 0 on success, negative error number on failure.
  373. */
  374. int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan,
  375. u64 sample_rate_hz)
  376. {
  377. return iio_backend_op_call(back, set_sample_rate, chan, sample_rate_hz);
  378. }
  379. EXPORT_SYMBOL_NS_GPL(iio_backend_set_sampling_freq, "IIO_BACKEND");
  380. /**
  381. * iio_backend_test_pattern_set - Configure a test pattern
  382. * @back: Backend device
  383. * @chan: Channel number
  384. * @pattern: Test pattern
  385. *
  386. * Configure a test pattern on the backend. This is typically used for
  387. * calibrating the timings on the data digital interface.
  388. *
  389. * RETURNS:
  390. * 0 on success, negative error number on failure.
  391. */
  392. int iio_backend_test_pattern_set(struct iio_backend *back,
  393. unsigned int chan,
  394. enum iio_backend_test_pattern pattern)
  395. {
  396. if (pattern >= IIO_BACKEND_TEST_PATTERN_MAX)
  397. return -EINVAL;
  398. return iio_backend_op_call(back, test_pattern_set, chan, pattern);
  399. }
  400. EXPORT_SYMBOL_NS_GPL(iio_backend_test_pattern_set, "IIO_BACKEND");
  401. /**
  402. * iio_backend_chan_status - Get the channel status
  403. * @back: Backend device
  404. * @chan: Channel number
  405. * @error: Error indication
  406. *
  407. * Get the current state of the backend channel. Typically used to check if
  408. * there were any errors sending/receiving data.
  409. *
  410. * RETURNS:
  411. * 0 on success, negative error number on failure.
  412. */
  413. int iio_backend_chan_status(struct iio_backend *back, unsigned int chan,
  414. bool *error)
  415. {
  416. return iio_backend_op_call(back, chan_status, chan, error);
  417. }
  418. EXPORT_SYMBOL_NS_GPL(iio_backend_chan_status, "IIO_BACKEND");
  419. /**
  420. * iio_backend_iodelay_set - Set digital I/O delay
  421. * @back: Backend device
  422. * @lane: Lane number
  423. * @taps: Number of taps
  424. *
  425. * Controls delays on sending/receiving data. One usecase for this is to
  426. * calibrate the data digital interface so we get the best results when
  427. * transferring data. Note that @taps has no unit since the actual delay per tap
  428. * is very backend specific. Hence, frontend devices typically should go through
  429. * an array of @taps (the size of that array should typically match the size of
  430. * calibration points on the frontend device) and call this API.
  431. *
  432. * RETURNS:
  433. * 0 on success, negative error number on failure.
  434. */
  435. int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane,
  436. unsigned int taps)
  437. {
  438. return iio_backend_op_call(back, iodelay_set, lane, taps);
  439. }
  440. EXPORT_SYMBOL_NS_GPL(iio_backend_iodelay_set, "IIO_BACKEND");
  441. /**
  442. * iio_backend_data_sample_trigger - Control when to sample data
  443. * @back: Backend device
  444. * @trigger: Data trigger
  445. *
  446. * Mostly useful for input backends. Configures the backend for when to sample
  447. * data (eg: rising vs falling edge).
  448. *
  449. * RETURNS:
  450. * 0 on success, negative error number on failure.
  451. */
  452. int iio_backend_data_sample_trigger(struct iio_backend *back,
  453. enum iio_backend_sample_trigger trigger)
  454. {
  455. if (trigger >= IIO_BACKEND_SAMPLE_TRIGGER_MAX)
  456. return -EINVAL;
  457. return iio_backend_op_call(back, data_sample_trigger, trigger);
  458. }
  459. EXPORT_SYMBOL_NS_GPL(iio_backend_data_sample_trigger, "IIO_BACKEND");
  460. static void iio_backend_free_buffer(void *arg)
  461. {
  462. struct iio_backend_buffer_pair *pair = arg;
  463. iio_backend_void_op_call(pair->back, free_buffer, pair->buffer);
  464. }
  465. /**
  466. * devm_iio_backend_request_buffer - Device managed buffer request
  467. * @dev: Consumer device for the backend
  468. * @back: Backend device
  469. * @indio_dev: IIO device
  470. *
  471. * Request an IIO buffer from the backend. The type of the buffer (typically
  472. * INDIO_BUFFER_HARDWARE) is up to the backend to decide. This is because,
  473. * normally, the backend dictates what kind of buffering we can get.
  474. *
  475. * The backend .free_buffer() hooks is automatically called on @dev detach.
  476. *
  477. * RETURNS:
  478. * 0 on success, negative error number on failure.
  479. */
  480. int devm_iio_backend_request_buffer(struct device *dev,
  481. struct iio_backend *back,
  482. struct iio_dev *indio_dev)
  483. {
  484. struct iio_backend_buffer_pair *pair;
  485. struct iio_buffer *buffer;
  486. pair = devm_kzalloc(dev, sizeof(*pair), GFP_KERNEL);
  487. if (!pair)
  488. return -ENOMEM;
  489. buffer = iio_backend_ptr_op_call(back, request_buffer, indio_dev);
  490. if (IS_ERR(buffer))
  491. return PTR_ERR(buffer);
  492. /* weak reference should be all what we need */
  493. pair->back = back;
  494. pair->buffer = buffer;
  495. return devm_add_action_or_reset(dev, iio_backend_free_buffer, pair);
  496. }
  497. EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, "IIO_BACKEND");
  498. /**
  499. * iio_backend_read_raw - Read a channel attribute from a backend device.
  500. * @back: Backend device
  501. * @chan: IIO channel reference
  502. * @val: First returned value
  503. * @val2: Second returned value
  504. * @mask: Specify the attribute to return
  505. *
  506. * RETURNS:
  507. * 0 on success, negative error number on failure.
  508. */
  509. int iio_backend_read_raw(struct iio_backend *back,
  510. struct iio_chan_spec const *chan, int *val, int *val2,
  511. long mask)
  512. {
  513. return iio_backend_op_call(back, read_raw, chan, val, val2, mask);
  514. }
  515. EXPORT_SYMBOL_NS_GPL(iio_backend_read_raw, "IIO_BACKEND");
  516. static struct iio_backend *iio_backend_from_indio_dev_parent(const struct device *dev)
  517. {
  518. struct iio_backend *back = ERR_PTR(-ENODEV), *iter;
  519. /*
  520. * We deliberately go through all backends even after finding a match.
  521. * The reason is that we want to catch frontend devices which have more
  522. * than one backend in which case returning the first we find is bogus.
  523. * For those cases, frontends need to explicitly define
  524. * get_iio_backend() in struct iio_info.
  525. */
  526. guard(mutex)(&iio_back_lock);
  527. list_for_each_entry(iter, &iio_back_list, entry) {
  528. if (dev == iter->frontend_dev) {
  529. if (!IS_ERR(back)) {
  530. dev_warn(dev,
  531. "Multiple backends! get_iio_backend() needs to be implemented");
  532. return ERR_PTR(-ENODEV);
  533. }
  534. back = iter;
  535. }
  536. }
  537. return back;
  538. }
  539. /**
  540. * iio_backend_ext_info_get - IIO ext_info read callback
  541. * @indio_dev: IIO device
  542. * @private: Data private to the driver
  543. * @chan: IIO channel
  544. * @buf: Buffer where to place the attribute data
  545. *
  546. * This helper is intended to be used by backends that extend an IIO channel
  547. * (through iio_backend_extend_chan_spec()) with extended info. In that case,
  548. * backends are not supposed to give their own callbacks (as they would not have
  549. * a way to get the backend from indio_dev). This is the getter.
  550. *
  551. * RETURNS:
  552. * Number of bytes written to buf, negative error number on failure.
  553. */
  554. ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private,
  555. const struct iio_chan_spec *chan, char *buf)
  556. {
  557. struct iio_backend *back;
  558. /*
  559. * The below should work for the majority of the cases. It will not work
  560. * when one frontend has multiple backends in which case we'll need a
  561. * new callback in struct iio_info so we can directly request the proper
  562. * backend from the frontend. Anyways, let's only introduce new options
  563. * when really needed...
  564. */
  565. back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent);
  566. if (IS_ERR(back))
  567. return PTR_ERR(back);
  568. return iio_backend_op_call(back, ext_info_get, private, chan, buf);
  569. }
  570. EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_get, "IIO_BACKEND");
  571. /**
  572. * iio_backend_ext_info_set - IIO ext_info write callback
  573. * @indio_dev: IIO device
  574. * @private: Data private to the driver
  575. * @chan: IIO channel
  576. * @buf: Buffer holding the sysfs attribute
  577. * @len: Buffer length
  578. *
  579. * This helper is intended to be used by backends that extend an IIO channel
  580. * (trough iio_backend_extend_chan_spec()) with extended info. In that case,
  581. * backends are not supposed to give their own callbacks (as they would not have
  582. * a way to get the backend from indio_dev). This is the setter.
  583. *
  584. * RETURNS:
  585. * Buffer length on success, negative error number on failure.
  586. */
  587. ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private,
  588. const struct iio_chan_spec *chan,
  589. const char *buf, size_t len)
  590. {
  591. struct iio_backend *back;
  592. back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent);
  593. if (IS_ERR(back))
  594. return PTR_ERR(back);
  595. return iio_backend_op_call(back, ext_info_set, private, chan, buf, len);
  596. }
  597. EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_set, "IIO_BACKEND");
  598. /**
  599. * iio_backend_interface_type_get - get the interface type used.
  600. * @back: Backend device
  601. * @type: Interface type
  602. *
  603. * RETURNS:
  604. * 0 on success, negative error number on failure.
  605. */
  606. int iio_backend_interface_type_get(struct iio_backend *back,
  607. enum iio_backend_interface_type *type)
  608. {
  609. int ret;
  610. ret = iio_backend_op_call(back, interface_type_get, type);
  611. if (ret)
  612. return ret;
  613. if (*type >= IIO_BACKEND_INTERFACE_MAX)
  614. return -EINVAL;
  615. return 0;
  616. }
  617. EXPORT_SYMBOL_NS_GPL(iio_backend_interface_type_get, "IIO_BACKEND");
  618. /**
  619. * iio_backend_data_size_set - set the data width/size in the data bus.
  620. * @back: Backend device
  621. * @size: Size in bits
  622. *
  623. * Some frontend devices can dynamically control the word/data size on the
  624. * interface/data bus. Hence, the backend device needs to be aware of it so
  625. * data can be correctly transferred.
  626. *
  627. * RETURNS:
  628. * 0 on success, negative error number on failure.
  629. */
  630. int iio_backend_data_size_set(struct iio_backend *back, unsigned int size)
  631. {
  632. if (!size)
  633. return -EINVAL;
  634. return iio_backend_op_call(back, data_size_set, size);
  635. }
  636. EXPORT_SYMBOL_NS_GPL(iio_backend_data_size_set, "IIO_BACKEND");
  637. /**
  638. * iio_backend_oversampling_ratio_set - set the oversampling ratio
  639. * @back: Backend device
  640. * @chan: Channel number
  641. * @ratio: The oversampling ratio - value 1 corresponds to no oversampling.
  642. *
  643. * RETURNS:
  644. * 0 on success, negative error number on failure.
  645. */
  646. int iio_backend_oversampling_ratio_set(struct iio_backend *back,
  647. unsigned int chan,
  648. unsigned int ratio)
  649. {
  650. return iio_backend_op_call(back, oversampling_ratio_set, chan, ratio);
  651. }
  652. EXPORT_SYMBOL_NS_GPL(iio_backend_oversampling_ratio_set, "IIO_BACKEND");
  653. /**
  654. * iio_backend_extend_chan_spec - Extend an IIO channel
  655. * @back: Backend device
  656. * @chan: IIO channel
  657. *
  658. * Some backends may have their own functionalities and hence capable of
  659. * extending a frontend's channel.
  660. *
  661. * RETURNS:
  662. * 0 on success, negative error number on failure.
  663. */
  664. int iio_backend_extend_chan_spec(struct iio_backend *back,
  665. struct iio_chan_spec *chan)
  666. {
  667. const struct iio_chan_spec_ext_info *frontend_ext_info = chan->ext_info;
  668. const struct iio_chan_spec_ext_info *back_ext_info;
  669. int ret;
  670. ret = iio_backend_op_call(back, extend_chan_spec, chan);
  671. if (ret)
  672. return ret;
  673. /*
  674. * Let's keep things simple for now. Don't allow to overwrite the
  675. * frontend's extended info. If ever needed, we can support appending
  676. * it.
  677. */
  678. if (frontend_ext_info && chan->ext_info != frontend_ext_info)
  679. return -EOPNOTSUPP;
  680. if (!chan->ext_info)
  681. return 0;
  682. /* Don't allow backends to get creative and force their own handlers */
  683. for (back_ext_info = chan->ext_info; back_ext_info->name; back_ext_info++) {
  684. if (back_ext_info->read != iio_backend_ext_info_get)
  685. return -EINVAL;
  686. if (back_ext_info->write != iio_backend_ext_info_set)
  687. return -EINVAL;
  688. }
  689. return 0;
  690. }
  691. EXPORT_SYMBOL_NS_GPL(iio_backend_extend_chan_spec, "IIO_BACKEND");
  692. static void iio_backend_release(void *arg)
  693. {
  694. struct iio_backend *back = arg;
  695. module_put(back->owner);
  696. }
  697. static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back)
  698. {
  699. struct device_link *link;
  700. int ret;
  701. /*
  702. * Make sure the provider cannot be unloaded before the consumer module.
  703. * Note that device_links would still guarantee that nothing is
  704. * accessible (and breaks) but this makes it explicit that the consumer
  705. * module must be also unloaded.
  706. */
  707. if (!try_module_get(back->owner))
  708. return dev_err_probe(dev, -ENODEV,
  709. "Cannot get module reference\n");
  710. ret = devm_add_action_or_reset(dev, iio_backend_release, back);
  711. if (ret)
  712. return ret;
  713. link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
  714. if (!link)
  715. return dev_err_probe(dev, -EINVAL,
  716. "Could not link to supplier(%s)\n",
  717. dev_name(back->dev));
  718. back->frontend_dev = dev;
  719. dev_dbg(dev, "Found backend(%s) device\n", dev_name(back->dev));
  720. return 0;
  721. }
  722. /**
  723. * iio_backend_filter_type_set - Set filter type
  724. * @back: Backend device
  725. * @type: Filter type.
  726. *
  727. * RETURNS:
  728. * 0 on success, negative error number on failure.
  729. */
  730. int iio_backend_filter_type_set(struct iio_backend *back,
  731. enum iio_backend_filter_type type)
  732. {
  733. if (type >= IIO_BACKEND_FILTER_TYPE_MAX)
  734. return -EINVAL;
  735. return iio_backend_op_call(back, filter_type_set, type);
  736. }
  737. EXPORT_SYMBOL_NS_GPL(iio_backend_filter_type_set, "IIO_BACKEND");
  738. /**
  739. * iio_backend_interface_data_align - Perform the data alignment process.
  740. * @back: Backend device
  741. * @timeout_us: Timeout value in us.
  742. *
  743. * When activated, it initates a proccess that aligns the sample's most
  744. * significant bit (MSB) based solely on the captured data, without
  745. * considering any other external signals.
  746. *
  747. * The timeout_us value must be greater than 0.
  748. *
  749. * RETURNS:
  750. * 0 on success, negative error number on failure.
  751. */
  752. int iio_backend_interface_data_align(struct iio_backend *back, u32 timeout_us)
  753. {
  754. if (!timeout_us)
  755. return -EINVAL;
  756. return iio_backend_op_call(back, interface_data_align, timeout_us);
  757. }
  758. EXPORT_SYMBOL_NS_GPL(iio_backend_interface_data_align, "IIO_BACKEND");
  759. /**
  760. * iio_backend_num_lanes_set - Number of lanes enabled.
  761. * @back: Backend device
  762. * @num_lanes: Number of lanes.
  763. *
  764. * RETURNS:
  765. * 0 on success, negative error number on failure.
  766. */
  767. int iio_backend_num_lanes_set(struct iio_backend *back, unsigned int num_lanes)
  768. {
  769. if (!num_lanes)
  770. return -EINVAL;
  771. return iio_backend_op_call(back, num_lanes_set, num_lanes);
  772. }
  773. EXPORT_SYMBOL_NS_GPL(iio_backend_num_lanes_set, "IIO_BACKEND");
  774. /**
  775. * iio_backend_ddr_enable - Enable interface DDR (Double Data Rate) mode
  776. * @back: Backend device
  777. *
  778. * Enable DDR, data is generated by the IP at each front (raising and falling)
  779. * of the bus clock signal.
  780. *
  781. * RETURNS:
  782. * 0 on success, negative error number on failure.
  783. */
  784. int iio_backend_ddr_enable(struct iio_backend *back)
  785. {
  786. return iio_backend_op_call(back, ddr_enable);
  787. }
  788. EXPORT_SYMBOL_NS_GPL(iio_backend_ddr_enable, "IIO_BACKEND");
  789. /**
  790. * iio_backend_ddr_disable - Disable interface DDR (Double Data Rate) mode
  791. * @back: Backend device
  792. *
  793. * Disable DDR, setting into SDR mode (Single Data Rate).
  794. *
  795. * RETURNS:
  796. * 0 on success, negative error number on failure.
  797. */
  798. int iio_backend_ddr_disable(struct iio_backend *back)
  799. {
  800. return iio_backend_op_call(back, ddr_disable);
  801. }
  802. EXPORT_SYMBOL_NS_GPL(iio_backend_ddr_disable, "IIO_BACKEND");
  803. /**
  804. * iio_backend_data_stream_enable - Enable data stream
  805. * @back: Backend device
  806. *
  807. * Enable data stream over the bus interface.
  808. *
  809. * RETURNS:
  810. * 0 on success, negative error number on failure.
  811. */
  812. int iio_backend_data_stream_enable(struct iio_backend *back)
  813. {
  814. return iio_backend_op_call(back, data_stream_enable);
  815. }
  816. EXPORT_SYMBOL_NS_GPL(iio_backend_data_stream_enable, "IIO_BACKEND");
  817. /**
  818. * iio_backend_data_stream_disable - Disable data stream
  819. * @back: Backend device
  820. *
  821. * Disable data stream over the bus interface.
  822. *
  823. * RETURNS:
  824. * 0 on success, negative error number on failure.
  825. */
  826. int iio_backend_data_stream_disable(struct iio_backend *back)
  827. {
  828. return iio_backend_op_call(back, data_stream_disable);
  829. }
  830. EXPORT_SYMBOL_NS_GPL(iio_backend_data_stream_disable, "IIO_BACKEND");
  831. /**
  832. * iio_backend_data_transfer_addr - Set data address.
  833. * @back: Backend device
  834. * @address: Data register address
  835. *
  836. * Some devices may need to inform the backend about an address
  837. * where to read or write the data.
  838. *
  839. * RETURNS:
  840. * 0 on success, negative error number on failure.
  841. */
  842. int iio_backend_data_transfer_addr(struct iio_backend *back, u32 address)
  843. {
  844. return iio_backend_op_call(back, data_transfer_addr, address);
  845. }
  846. EXPORT_SYMBOL_NS_GPL(iio_backend_data_transfer_addr, "IIO_BACKEND");
  847. static struct iio_backend *__devm_iio_backend_fwnode_get(struct device *dev, const char *name,
  848. struct fwnode_handle *fwnode)
  849. {
  850. struct fwnode_handle *fwnode_back;
  851. struct iio_backend *back;
  852. unsigned int index;
  853. int ret;
  854. if (name) {
  855. ret = device_property_match_string(dev, "io-backend-names",
  856. name);
  857. if (ret < 0)
  858. return ERR_PTR(ret);
  859. index = ret;
  860. } else {
  861. index = 0;
  862. }
  863. fwnode_back = fwnode_find_reference(fwnode, "io-backends", index);
  864. if (IS_ERR(fwnode_back))
  865. return dev_err_cast_probe(dev, fwnode_back,
  866. "Cannot get Firmware reference\n");
  867. guard(mutex)(&iio_back_lock);
  868. list_for_each_entry(back, &iio_back_list, entry) {
  869. if (!device_match_fwnode(back->dev, fwnode_back))
  870. continue;
  871. fwnode_handle_put(fwnode_back);
  872. ret = __devm_iio_backend_get(dev, back);
  873. if (ret)
  874. return ERR_PTR(ret);
  875. if (name)
  876. back->idx = index;
  877. return back;
  878. }
  879. fwnode_handle_put(fwnode_back);
  880. return ERR_PTR(-EPROBE_DEFER);
  881. }
  882. /**
  883. * devm_iio_backend_get - Device managed backend device get
  884. * @dev: Consumer device for the backend
  885. * @name: Backend name
  886. *
  887. * Get's the backend associated with @dev.
  888. *
  889. * RETURNS:
  890. * A backend pointer, negative error pointer otherwise.
  891. */
  892. struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
  893. {
  894. return __devm_iio_backend_fwnode_get(dev, name, dev_fwnode(dev));
  895. }
  896. EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, "IIO_BACKEND");
  897. /**
  898. * devm_iio_backend_fwnode_get - Device managed backend firmware node get
  899. * @dev: Consumer device for the backend
  900. * @name: Backend name
  901. * @fwnode: Firmware node of the backend consumer
  902. *
  903. * Get's the backend associated with a firmware node.
  904. *
  905. * RETURNS:
  906. * A backend pointer, negative error pointer otherwise.
  907. */
  908. struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev,
  909. const char *name,
  910. struct fwnode_handle *fwnode)
  911. {
  912. return __devm_iio_backend_fwnode_get(dev, name, fwnode);
  913. }
  914. EXPORT_SYMBOL_NS_GPL(devm_iio_backend_fwnode_get, "IIO_BACKEND");
  915. /**
  916. * __devm_iio_backend_get_from_fwnode_lookup - Device managed fwnode backend device get
  917. * @dev: Consumer device for the backend
  918. * @fwnode: Firmware node of the backend device
  919. *
  920. * Search the backend list for a device matching @fwnode.
  921. * This API should not be used and it's only present for preventing the first
  922. * user of this framework to break it's DT ABI.
  923. *
  924. * RETURNS:
  925. * A backend pointer, negative error pointer otherwise.
  926. */
  927. struct iio_backend *
  928. __devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
  929. struct fwnode_handle *fwnode)
  930. {
  931. struct iio_backend *back;
  932. int ret;
  933. guard(mutex)(&iio_back_lock);
  934. list_for_each_entry(back, &iio_back_list, entry) {
  935. if (!device_match_fwnode(back->dev, fwnode))
  936. continue;
  937. ret = __devm_iio_backend_get(dev, back);
  938. if (ret)
  939. return ERR_PTR(ret);
  940. return back;
  941. }
  942. return ERR_PTR(-EPROBE_DEFER);
  943. }
  944. EXPORT_SYMBOL_NS_GPL(__devm_iio_backend_get_from_fwnode_lookup, "IIO_BACKEND");
  945. /**
  946. * iio_backend_get_priv - Get driver private data
  947. * @back: Backend device
  948. *
  949. * RETURNS:
  950. * Pointer to the driver private data associated with the backend.
  951. */
  952. void *iio_backend_get_priv(const struct iio_backend *back)
  953. {
  954. return back->priv;
  955. }
  956. EXPORT_SYMBOL_NS_GPL(iio_backend_get_priv, "IIO_BACKEND");
  957. static void iio_backend_unregister(void *arg)
  958. {
  959. struct iio_backend *back = arg;
  960. guard(mutex)(&iio_back_lock);
  961. list_del(&back->entry);
  962. }
  963. /**
  964. * devm_iio_backend_register - Device managed backend device register
  965. * @dev: Backend device being registered
  966. * @info: Backend info
  967. * @priv: Device private data
  968. *
  969. * @info is mandatory. Not providing it results in -EINVAL.
  970. *
  971. * RETURNS:
  972. * 0 on success, negative error number on failure.
  973. */
  974. int devm_iio_backend_register(struct device *dev,
  975. const struct iio_backend_info *info, void *priv)
  976. {
  977. struct iio_backend *back;
  978. if (!info || !info->ops)
  979. return dev_err_probe(dev, -EINVAL, "No backend ops given\n");
  980. /*
  981. * Through device_links, we guarantee that a frontend device cannot be
  982. * bound/exist if the backend driver is not around. Hence, we can bind
  983. * the backend object lifetime with the device being passed since
  984. * removing it will tear the frontend/consumer down.
  985. */
  986. back = devm_kzalloc(dev, sizeof(*back), GFP_KERNEL);
  987. if (!back)
  988. return -ENOMEM;
  989. back->ops = info->ops;
  990. back->name = info->name;
  991. back->owner = dev->driver->owner;
  992. back->dev = dev;
  993. back->priv = priv;
  994. scoped_guard(mutex, &iio_back_lock)
  995. list_add(&back->entry, &iio_back_list);
  996. return devm_add_action_or_reset(dev, iio_backend_unregister, back);
  997. }
  998. EXPORT_SYMBOL_NS_GPL(devm_iio_backend_register, "IIO_BACKEND");
  999. MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
  1000. MODULE_DESCRIPTION("Framework to handle complex IIO aggregate devices");
  1001. MODULE_LICENSE("GPL");