industrialio-core.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* The industrial I/O core
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * Based on elements of hwmon and input subsystems.
  10. */
  11. #define pr_fmt(fmt) "iio-core: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/idr.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/err.h>
  17. #include <linux/device.h>
  18. #include <linux/fs.h>
  19. #include <linux/poll.h>
  20. #include <linux/sched.h>
  21. #include <linux/wait.h>
  22. #include <linux/cdev.h>
  23. #include <linux/slab.h>
  24. #include <linux/anon_inodes.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/iio/iio.h>
  27. #include "iio_core.h"
  28. #include "iio_core_trigger.h"
  29. #include <linux/iio/sysfs.h>
  30. #include <linux/iio/events.h>
  31. #include <linux/iio/buffer.h>
  32. /* IDA to assign each registered device a unique id */
  33. static DEFINE_IDA(iio_ida);
  34. static dev_t iio_devt;
  35. #define IIO_DEV_MAX 256
  36. struct bus_type iio_bus_type = {
  37. .name = "iio",
  38. };
  39. EXPORT_SYMBOL(iio_bus_type);
  40. static struct dentry *iio_debugfs_dentry;
  41. static const char * const iio_direction[] = {
  42. [0] = "in",
  43. [1] = "out",
  44. };
  45. static const char * const iio_chan_type_name_spec[] = {
  46. [IIO_VOLTAGE] = "voltage",
  47. [IIO_CURRENT] = "current",
  48. [IIO_POWER] = "power",
  49. [IIO_ACCEL] = "accel",
  50. [IIO_ANGL_VEL] = "anglvel",
  51. [IIO_MAGN] = "magn",
  52. [IIO_LIGHT] = "illuminance",
  53. [IIO_INTENSITY] = "intensity",
  54. [IIO_PROXIMITY] = "proximity",
  55. [IIO_TEMP] = "temp",
  56. [IIO_INCLI] = "incli",
  57. [IIO_ROT] = "rot",
  58. [IIO_ANGL] = "angl",
  59. [IIO_TIMESTAMP] = "timestamp",
  60. [IIO_CAPACITANCE] = "capacitance",
  61. [IIO_ALTVOLTAGE] = "altvoltage",
  62. [IIO_CCT] = "cct",
  63. [IIO_PRESSURE] = "pressure",
  64. [IIO_HUMIDITYRELATIVE] = "humidityrelative",
  65. };
  66. static const char * const iio_modifier_names[] = {
  67. [IIO_MOD_X] = "x",
  68. [IIO_MOD_Y] = "y",
  69. [IIO_MOD_Z] = "z",
  70. [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
  71. [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
  72. [IIO_MOD_LIGHT_BOTH] = "both",
  73. [IIO_MOD_LIGHT_IR] = "ir",
  74. [IIO_MOD_LIGHT_CLEAR] = "clear",
  75. [IIO_MOD_LIGHT_RED] = "red",
  76. [IIO_MOD_LIGHT_GREEN] = "green",
  77. [IIO_MOD_LIGHT_BLUE] = "blue",
  78. [IIO_MOD_QUATERNION] = "quaternion",
  79. [IIO_MOD_TEMP_AMBIENT] = "ambient",
  80. [IIO_MOD_TEMP_OBJECT] = "object",
  81. [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
  82. [IIO_MOD_NORTH_TRUE] = "from_north_true",
  83. [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
  84. [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
  85. };
  86. /* relies on pairs of these shared then separate */
  87. static const char * const iio_chan_info_postfix[] = {
  88. [IIO_CHAN_INFO_RAW] = "raw",
  89. [IIO_CHAN_INFO_PROCESSED] = "input",
  90. [IIO_CHAN_INFO_SCALE] = "scale",
  91. [IIO_CHAN_INFO_OFFSET] = "offset",
  92. [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
  93. [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
  94. [IIO_CHAN_INFO_PEAK] = "peak_raw",
  95. [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
  96. [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
  97. [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
  98. [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
  99. = "filter_low_pass_3db_frequency",
  100. [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
  101. [IIO_CHAN_INFO_FREQUENCY] = "frequency",
  102. [IIO_CHAN_INFO_PHASE] = "phase",
  103. [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
  104. [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
  105. [IIO_CHAN_INFO_INT_TIME] = "integration_time",
  106. };
  107. /**
  108. * iio_find_channel_from_si() - get channel from its scan index
  109. * @indio_dev: device
  110. * @si: scan index to match
  111. */
  112. const struct iio_chan_spec
  113. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
  114. {
  115. int i;
  116. for (i = 0; i < indio_dev->num_channels; i++)
  117. if (indio_dev->channels[i].scan_index == si)
  118. return &indio_dev->channels[i];
  119. return NULL;
  120. }
  121. /* This turns up an awful lot */
  122. ssize_t iio_read_const_attr(struct device *dev,
  123. struct device_attribute *attr,
  124. char *buf)
  125. {
  126. return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
  127. }
  128. EXPORT_SYMBOL(iio_read_const_attr);
  129. static int __init iio_init(void)
  130. {
  131. int ret;
  132. /* Register sysfs bus */
  133. ret = bus_register(&iio_bus_type);
  134. if (ret < 0) {
  135. pr_err("could not register bus type\n");
  136. goto error_nothing;
  137. }
  138. ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
  139. if (ret < 0) {
  140. pr_err("failed to allocate char dev region\n");
  141. goto error_unregister_bus_type;
  142. }
  143. iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
  144. return 0;
  145. error_unregister_bus_type:
  146. bus_unregister(&iio_bus_type);
  147. error_nothing:
  148. return ret;
  149. }
  150. static void __exit iio_exit(void)
  151. {
  152. if (iio_devt)
  153. unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
  154. bus_unregister(&iio_bus_type);
  155. debugfs_remove(iio_debugfs_dentry);
  156. }
  157. #if defined(CONFIG_DEBUG_FS)
  158. static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
  159. size_t count, loff_t *ppos)
  160. {
  161. struct iio_dev *indio_dev = file->private_data;
  162. char buf[20];
  163. unsigned val = 0;
  164. ssize_t len;
  165. int ret;
  166. ret = indio_dev->info->debugfs_reg_access(indio_dev,
  167. indio_dev->cached_reg_addr,
  168. 0, &val);
  169. if (ret)
  170. dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
  171. len = snprintf(buf, sizeof(buf), "0x%X\n", val);
  172. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  173. }
  174. static ssize_t iio_debugfs_write_reg(struct file *file,
  175. const char __user *userbuf, size_t count, loff_t *ppos)
  176. {
  177. struct iio_dev *indio_dev = file->private_data;
  178. unsigned reg, val;
  179. char buf[80];
  180. int ret;
  181. count = min_t(size_t, count, (sizeof(buf)-1));
  182. if (copy_from_user(buf, userbuf, count))
  183. return -EFAULT;
  184. buf[count] = 0;
  185. ret = sscanf(buf, "%i %i", &reg, &val);
  186. switch (ret) {
  187. case 1:
  188. indio_dev->cached_reg_addr = reg;
  189. break;
  190. case 2:
  191. indio_dev->cached_reg_addr = reg;
  192. ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
  193. val, NULL);
  194. if (ret) {
  195. dev_err(indio_dev->dev.parent, "%s: write failed\n",
  196. __func__);
  197. return ret;
  198. }
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. return count;
  204. }
  205. static const struct file_operations iio_debugfs_reg_fops = {
  206. .open = simple_open,
  207. .read = iio_debugfs_read_reg,
  208. .write = iio_debugfs_write_reg,
  209. };
  210. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  211. {
  212. debugfs_remove_recursive(indio_dev->debugfs_dentry);
  213. }
  214. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  215. {
  216. struct dentry *d;
  217. if (indio_dev->info->debugfs_reg_access == NULL)
  218. return 0;
  219. if (!iio_debugfs_dentry)
  220. return 0;
  221. indio_dev->debugfs_dentry =
  222. debugfs_create_dir(dev_name(&indio_dev->dev),
  223. iio_debugfs_dentry);
  224. if (indio_dev->debugfs_dentry == NULL) {
  225. dev_warn(indio_dev->dev.parent,
  226. "Failed to create debugfs directory\n");
  227. return -EFAULT;
  228. }
  229. d = debugfs_create_file("direct_reg_access", 0644,
  230. indio_dev->debugfs_dentry,
  231. indio_dev, &iio_debugfs_reg_fops);
  232. if (!d) {
  233. iio_device_unregister_debugfs(indio_dev);
  234. return -ENOMEM;
  235. }
  236. return 0;
  237. }
  238. #else
  239. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  240. {
  241. return 0;
  242. }
  243. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  244. {
  245. }
  246. #endif /* CONFIG_DEBUG_FS */
  247. static ssize_t iio_read_channel_ext_info(struct device *dev,
  248. struct device_attribute *attr,
  249. char *buf)
  250. {
  251. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  252. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  253. const struct iio_chan_spec_ext_info *ext_info;
  254. ext_info = &this_attr->c->ext_info[this_attr->address];
  255. return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
  256. }
  257. static ssize_t iio_write_channel_ext_info(struct device *dev,
  258. struct device_attribute *attr,
  259. const char *buf,
  260. size_t len)
  261. {
  262. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  263. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  264. const struct iio_chan_spec_ext_info *ext_info;
  265. ext_info = &this_attr->c->ext_info[this_attr->address];
  266. return ext_info->write(indio_dev, ext_info->private,
  267. this_attr->c, buf, len);
  268. }
  269. ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
  270. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  271. {
  272. const struct iio_enum *e = (const struct iio_enum *)priv;
  273. unsigned int i;
  274. size_t len = 0;
  275. if (!e->num_items)
  276. return 0;
  277. for (i = 0; i < e->num_items; ++i)
  278. len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
  279. /* replace last space with a newline */
  280. buf[len - 1] = '\n';
  281. return len;
  282. }
  283. EXPORT_SYMBOL_GPL(iio_enum_available_read);
  284. ssize_t iio_enum_read(struct iio_dev *indio_dev,
  285. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  286. {
  287. const struct iio_enum *e = (const struct iio_enum *)priv;
  288. int i;
  289. if (!e->get)
  290. return -EINVAL;
  291. i = e->get(indio_dev, chan);
  292. if (i < 0)
  293. return i;
  294. else if (i >= e->num_items)
  295. return -EINVAL;
  296. return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
  297. }
  298. EXPORT_SYMBOL_GPL(iio_enum_read);
  299. ssize_t iio_enum_write(struct iio_dev *indio_dev,
  300. uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
  301. size_t len)
  302. {
  303. const struct iio_enum *e = (const struct iio_enum *)priv;
  304. unsigned int i;
  305. int ret;
  306. if (!e->set)
  307. return -EINVAL;
  308. for (i = 0; i < e->num_items; i++) {
  309. if (sysfs_streq(buf, e->items[i]))
  310. break;
  311. }
  312. if (i == e->num_items)
  313. return -EINVAL;
  314. ret = e->set(indio_dev, chan, i);
  315. return ret ? ret : len;
  316. }
  317. EXPORT_SYMBOL_GPL(iio_enum_write);
  318. /**
  319. * iio_format_value() - Formats a IIO value into its string representation
  320. * @buf: The buffer to which the formated value gets written
  321. * @type: One of the IIO_VAL_... constants. This decides how the val and val2
  322. * parameters are formatted.
  323. * @vals: pointer to the values, exact meaning depends on the type parameter.
  324. */
  325. ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
  326. {
  327. unsigned long long tmp;
  328. bool scale_db = false;
  329. switch (type) {
  330. case IIO_VAL_INT:
  331. return sprintf(buf, "%d\n", vals[0]);
  332. case IIO_VAL_INT_PLUS_MICRO_DB:
  333. scale_db = true;
  334. case IIO_VAL_INT_PLUS_MICRO:
  335. if (vals[1] < 0)
  336. return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
  337. -vals[1],
  338. scale_db ? " dB" : "");
  339. else
  340. return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
  341. scale_db ? " dB" : "");
  342. case IIO_VAL_INT_PLUS_NANO:
  343. if (vals[1] < 0)
  344. return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
  345. -vals[1]);
  346. else
  347. return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
  348. case IIO_VAL_FRACTIONAL:
  349. tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
  350. vals[1] = do_div(tmp, 1000000000LL);
  351. vals[0] = tmp;
  352. return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
  353. case IIO_VAL_FRACTIONAL_LOG2:
  354. tmp = (s64)vals[0] * 1000000000LL >> vals[1];
  355. vals[1] = do_div(tmp, 1000000000LL);
  356. vals[0] = tmp;
  357. return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
  358. case IIO_VAL_INT_MULTIPLE:
  359. {
  360. int i;
  361. int len = 0;
  362. for (i = 0; i < size; ++i)
  363. len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
  364. vals[i]);
  365. len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
  366. return len;
  367. }
  368. default:
  369. return 0;
  370. }
  371. }
  372. static ssize_t iio_read_channel_info(struct device *dev,
  373. struct device_attribute *attr,
  374. char *buf)
  375. {
  376. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  377. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  378. int vals[INDIO_MAX_RAW_ELEMENTS];
  379. int ret;
  380. int val_len = 2;
  381. if (indio_dev->info->read_raw_multi)
  382. ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
  383. INDIO_MAX_RAW_ELEMENTS,
  384. vals, &val_len,
  385. this_attr->address);
  386. else
  387. ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
  388. &vals[0], &vals[1], this_attr->address);
  389. if (ret < 0)
  390. return ret;
  391. return iio_format_value(buf, ret, val_len, vals);
  392. }
  393. /**
  394. * iio_str_to_fixpoint() - Parse a fixed-point number from a string
  395. * @str: The string to parse
  396. * @fract_mult: Multiplier for the first decimal place, should be a power of 10
  397. * @integer: The integer part of the number
  398. * @fract: The fractional part of the number
  399. *
  400. * Returns 0 on success, or a negative error code if the string could not be
  401. * parsed.
  402. */
  403. int iio_str_to_fixpoint(const char *str, int fract_mult,
  404. int *integer, int *fract)
  405. {
  406. int i = 0, f = 0;
  407. bool integer_part = true, negative = false;
  408. if (str[0] == '-') {
  409. negative = true;
  410. str++;
  411. } else if (str[0] == '+') {
  412. str++;
  413. }
  414. while (*str) {
  415. if ('0' <= *str && *str <= '9') {
  416. if (integer_part) {
  417. i = i * 10 + *str - '0';
  418. } else {
  419. f += fract_mult * (*str - '0');
  420. fract_mult /= 10;
  421. }
  422. } else if (*str == '\n') {
  423. if (*(str + 1) == '\0')
  424. break;
  425. else
  426. return -EINVAL;
  427. } else if (*str == '.' && integer_part) {
  428. integer_part = false;
  429. } else {
  430. return -EINVAL;
  431. }
  432. str++;
  433. }
  434. if (negative) {
  435. if (i)
  436. i = -i;
  437. else
  438. f = -f;
  439. }
  440. *integer = i;
  441. *fract = f;
  442. return 0;
  443. }
  444. EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
  445. static ssize_t iio_write_channel_info(struct device *dev,
  446. struct device_attribute *attr,
  447. const char *buf,
  448. size_t len)
  449. {
  450. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  451. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  452. int ret, fract_mult = 100000;
  453. int integer, fract;
  454. /* Assumes decimal - precision based on number of digits */
  455. if (!indio_dev->info->write_raw)
  456. return -EINVAL;
  457. if (indio_dev->info->write_raw_get_fmt)
  458. switch (indio_dev->info->write_raw_get_fmt(indio_dev,
  459. this_attr->c, this_attr->address)) {
  460. case IIO_VAL_INT_PLUS_MICRO:
  461. fract_mult = 100000;
  462. break;
  463. case IIO_VAL_INT_PLUS_NANO:
  464. fract_mult = 100000000;
  465. break;
  466. default:
  467. return -EINVAL;
  468. }
  469. ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
  470. if (ret)
  471. return ret;
  472. ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
  473. integer, fract, this_attr->address);
  474. if (ret)
  475. return ret;
  476. return len;
  477. }
  478. static
  479. int __iio_device_attr_init(struct device_attribute *dev_attr,
  480. const char *postfix,
  481. struct iio_chan_spec const *chan,
  482. ssize_t (*readfunc)(struct device *dev,
  483. struct device_attribute *attr,
  484. char *buf),
  485. ssize_t (*writefunc)(struct device *dev,
  486. struct device_attribute *attr,
  487. const char *buf,
  488. size_t len),
  489. enum iio_shared_by shared_by)
  490. {
  491. int ret = 0;
  492. char *name = NULL;
  493. char *full_postfix;
  494. sysfs_attr_init(&dev_attr->attr);
  495. /* Build up postfix of <extend_name>_<modifier>_postfix */
  496. if (chan->modified && (shared_by == IIO_SEPARATE)) {
  497. if (chan->extend_name)
  498. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  499. iio_modifier_names[chan
  500. ->channel2],
  501. chan->extend_name,
  502. postfix);
  503. else
  504. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  505. iio_modifier_names[chan
  506. ->channel2],
  507. postfix);
  508. } else {
  509. if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
  510. full_postfix = kstrdup(postfix, GFP_KERNEL);
  511. else
  512. full_postfix = kasprintf(GFP_KERNEL,
  513. "%s_%s",
  514. chan->extend_name,
  515. postfix);
  516. }
  517. if (full_postfix == NULL)
  518. return -ENOMEM;
  519. if (chan->differential) { /* Differential can not have modifier */
  520. switch (shared_by) {
  521. case IIO_SHARED_BY_ALL:
  522. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  523. break;
  524. case IIO_SHARED_BY_DIR:
  525. name = kasprintf(GFP_KERNEL, "%s_%s",
  526. iio_direction[chan->output],
  527. full_postfix);
  528. break;
  529. case IIO_SHARED_BY_TYPE:
  530. name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
  531. iio_direction[chan->output],
  532. iio_chan_type_name_spec[chan->type],
  533. iio_chan_type_name_spec[chan->type],
  534. full_postfix);
  535. break;
  536. case IIO_SEPARATE:
  537. if (!chan->indexed) {
  538. WARN_ON("Differential channels must be indexed\n");
  539. ret = -EINVAL;
  540. goto error_free_full_postfix;
  541. }
  542. name = kasprintf(GFP_KERNEL,
  543. "%s_%s%d-%s%d_%s",
  544. iio_direction[chan->output],
  545. iio_chan_type_name_spec[chan->type],
  546. chan->channel,
  547. iio_chan_type_name_spec[chan->type],
  548. chan->channel2,
  549. full_postfix);
  550. break;
  551. }
  552. } else { /* Single ended */
  553. switch (shared_by) {
  554. case IIO_SHARED_BY_ALL:
  555. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  556. break;
  557. case IIO_SHARED_BY_DIR:
  558. name = kasprintf(GFP_KERNEL, "%s_%s",
  559. iio_direction[chan->output],
  560. full_postfix);
  561. break;
  562. case IIO_SHARED_BY_TYPE:
  563. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  564. iio_direction[chan->output],
  565. iio_chan_type_name_spec[chan->type],
  566. full_postfix);
  567. break;
  568. case IIO_SEPARATE:
  569. if (chan->indexed)
  570. name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  571. iio_direction[chan->output],
  572. iio_chan_type_name_spec[chan->type],
  573. chan->channel,
  574. full_postfix);
  575. else
  576. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  577. iio_direction[chan->output],
  578. iio_chan_type_name_spec[chan->type],
  579. full_postfix);
  580. break;
  581. }
  582. }
  583. if (name == NULL) {
  584. ret = -ENOMEM;
  585. goto error_free_full_postfix;
  586. }
  587. dev_attr->attr.name = name;
  588. if (readfunc) {
  589. dev_attr->attr.mode |= S_IRUGO;
  590. dev_attr->show = readfunc;
  591. }
  592. if (writefunc) {
  593. dev_attr->attr.mode |= S_IWUSR;
  594. dev_attr->store = writefunc;
  595. }
  596. error_free_full_postfix:
  597. kfree(full_postfix);
  598. return ret;
  599. }
  600. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  601. {
  602. kfree(dev_attr->attr.name);
  603. }
  604. int __iio_add_chan_devattr(const char *postfix,
  605. struct iio_chan_spec const *chan,
  606. ssize_t (*readfunc)(struct device *dev,
  607. struct device_attribute *attr,
  608. char *buf),
  609. ssize_t (*writefunc)(struct device *dev,
  610. struct device_attribute *attr,
  611. const char *buf,
  612. size_t len),
  613. u64 mask,
  614. enum iio_shared_by shared_by,
  615. struct device *dev,
  616. struct list_head *attr_list)
  617. {
  618. int ret;
  619. struct iio_dev_attr *iio_attr, *t;
  620. iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
  621. if (iio_attr == NULL)
  622. return -ENOMEM;
  623. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  624. postfix, chan,
  625. readfunc, writefunc, shared_by);
  626. if (ret)
  627. goto error_iio_dev_attr_free;
  628. iio_attr->c = chan;
  629. iio_attr->address = mask;
  630. list_for_each_entry(t, attr_list, l)
  631. if (strcmp(t->dev_attr.attr.name,
  632. iio_attr->dev_attr.attr.name) == 0) {
  633. if (shared_by == IIO_SEPARATE)
  634. dev_err(dev, "tried to double register : %s\n",
  635. t->dev_attr.attr.name);
  636. ret = -EBUSY;
  637. goto error_device_attr_deinit;
  638. }
  639. list_add(&iio_attr->l, attr_list);
  640. return 0;
  641. error_device_attr_deinit:
  642. __iio_device_attr_deinit(&iio_attr->dev_attr);
  643. error_iio_dev_attr_free:
  644. kfree(iio_attr);
  645. return ret;
  646. }
  647. static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
  648. struct iio_chan_spec const *chan,
  649. enum iio_shared_by shared_by,
  650. const long *infomask)
  651. {
  652. int i, ret, attrcount = 0;
  653. for_each_set_bit(i, infomask, sizeof(infomask)*8) {
  654. if (i >= ARRAY_SIZE(iio_chan_info_postfix))
  655. return -EINVAL;
  656. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  657. chan,
  658. &iio_read_channel_info,
  659. &iio_write_channel_info,
  660. i,
  661. shared_by,
  662. &indio_dev->dev,
  663. &indio_dev->channel_attr_list);
  664. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  665. continue;
  666. else if (ret < 0)
  667. return ret;
  668. attrcount++;
  669. }
  670. return attrcount;
  671. }
  672. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  673. struct iio_chan_spec const *chan)
  674. {
  675. int ret, attrcount = 0;
  676. const struct iio_chan_spec_ext_info *ext_info;
  677. if (chan->channel < 0)
  678. return 0;
  679. ret = iio_device_add_info_mask_type(indio_dev, chan,
  680. IIO_SEPARATE,
  681. &chan->info_mask_separate);
  682. if (ret < 0)
  683. return ret;
  684. attrcount += ret;
  685. ret = iio_device_add_info_mask_type(indio_dev, chan,
  686. IIO_SHARED_BY_TYPE,
  687. &chan->info_mask_shared_by_type);
  688. if (ret < 0)
  689. return ret;
  690. attrcount += ret;
  691. ret = iio_device_add_info_mask_type(indio_dev, chan,
  692. IIO_SHARED_BY_DIR,
  693. &chan->info_mask_shared_by_dir);
  694. if (ret < 0)
  695. return ret;
  696. attrcount += ret;
  697. ret = iio_device_add_info_mask_type(indio_dev, chan,
  698. IIO_SHARED_BY_ALL,
  699. &chan->info_mask_shared_by_all);
  700. if (ret < 0)
  701. return ret;
  702. attrcount += ret;
  703. if (chan->ext_info) {
  704. unsigned int i = 0;
  705. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  706. ret = __iio_add_chan_devattr(ext_info->name,
  707. chan,
  708. ext_info->read ?
  709. &iio_read_channel_ext_info : NULL,
  710. ext_info->write ?
  711. &iio_write_channel_ext_info : NULL,
  712. i,
  713. ext_info->shared,
  714. &indio_dev->dev,
  715. &indio_dev->channel_attr_list);
  716. i++;
  717. if (ret == -EBUSY && ext_info->shared)
  718. continue;
  719. if (ret)
  720. return ret;
  721. attrcount++;
  722. }
  723. }
  724. return attrcount;
  725. }
  726. /**
  727. * iio_free_chan_devattr_list() - Free a list of IIO device attributes
  728. * @attr_list: List of IIO device attributes
  729. *
  730. * This function frees the memory allocated for each of the IIO device
  731. * attributes in the list.
  732. */
  733. void iio_free_chan_devattr_list(struct list_head *attr_list)
  734. {
  735. struct iio_dev_attr *p, *n;
  736. list_for_each_entry_safe(p, n, attr_list, l) {
  737. kfree(p->dev_attr.attr.name);
  738. list_del(&p->l);
  739. kfree(p);
  740. }
  741. }
  742. static ssize_t iio_show_dev_name(struct device *dev,
  743. struct device_attribute *attr,
  744. char *buf)
  745. {
  746. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  747. return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
  748. }
  749. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  750. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  751. {
  752. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  753. struct iio_dev_attr *p;
  754. struct attribute **attr;
  755. /* First count elements in any existing group */
  756. if (indio_dev->info->attrs) {
  757. attr = indio_dev->info->attrs->attrs;
  758. while (*attr++ != NULL)
  759. attrcount_orig++;
  760. }
  761. attrcount = attrcount_orig;
  762. /*
  763. * New channel registration method - relies on the fact a group does
  764. * not need to be initialized if its name is NULL.
  765. */
  766. if (indio_dev->channels)
  767. for (i = 0; i < indio_dev->num_channels; i++) {
  768. ret = iio_device_add_channel_sysfs(indio_dev,
  769. &indio_dev
  770. ->channels[i]);
  771. if (ret < 0)
  772. goto error_clear_attrs;
  773. attrcount += ret;
  774. }
  775. if (indio_dev->name)
  776. attrcount++;
  777. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  778. sizeof(indio_dev->chan_attr_group.attrs[0]),
  779. GFP_KERNEL);
  780. if (indio_dev->chan_attr_group.attrs == NULL) {
  781. ret = -ENOMEM;
  782. goto error_clear_attrs;
  783. }
  784. /* Copy across original attributes */
  785. if (indio_dev->info->attrs)
  786. memcpy(indio_dev->chan_attr_group.attrs,
  787. indio_dev->info->attrs->attrs,
  788. sizeof(indio_dev->chan_attr_group.attrs[0])
  789. *attrcount_orig);
  790. attrn = attrcount_orig;
  791. /* Add all elements from the list. */
  792. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  793. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  794. if (indio_dev->name)
  795. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  796. indio_dev->groups[indio_dev->groupcounter++] =
  797. &indio_dev->chan_attr_group;
  798. return 0;
  799. error_clear_attrs:
  800. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  801. return ret;
  802. }
  803. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  804. {
  805. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  806. kfree(indio_dev->chan_attr_group.attrs);
  807. indio_dev->chan_attr_group.attrs = NULL;
  808. }
  809. static void iio_dev_release(struct device *device)
  810. {
  811. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  812. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  813. iio_device_unregister_trigger_consumer(indio_dev);
  814. iio_device_unregister_eventset(indio_dev);
  815. iio_device_unregister_sysfs(indio_dev);
  816. iio_buffer_put(indio_dev->buffer);
  817. ida_simple_remove(&iio_ida, indio_dev->id);
  818. kfree(indio_dev);
  819. }
  820. struct device_type iio_device_type = {
  821. .name = "iio_device",
  822. .release = iio_dev_release,
  823. };
  824. /**
  825. * iio_device_alloc() - allocate an iio_dev from a driver
  826. * @sizeof_priv: Space to allocate for private structure.
  827. **/
  828. struct iio_dev *iio_device_alloc(int sizeof_priv)
  829. {
  830. struct iio_dev *dev;
  831. size_t alloc_size;
  832. alloc_size = sizeof(struct iio_dev);
  833. if (sizeof_priv) {
  834. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  835. alloc_size += sizeof_priv;
  836. }
  837. /* ensure 32-byte alignment of whole construct ? */
  838. alloc_size += IIO_ALIGN - 1;
  839. dev = kzalloc(alloc_size, GFP_KERNEL);
  840. if (dev) {
  841. dev->dev.groups = dev->groups;
  842. dev->dev.type = &iio_device_type;
  843. dev->dev.bus = &iio_bus_type;
  844. device_initialize(&dev->dev);
  845. dev_set_drvdata(&dev->dev, (void *)dev);
  846. mutex_init(&dev->mlock);
  847. mutex_init(&dev->info_exist_lock);
  848. INIT_LIST_HEAD(&dev->channel_attr_list);
  849. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  850. if (dev->id < 0) {
  851. /* cannot use a dev_err as the name isn't available */
  852. pr_err("failed to get device id\n");
  853. kfree(dev);
  854. return NULL;
  855. }
  856. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  857. INIT_LIST_HEAD(&dev->buffer_list);
  858. }
  859. return dev;
  860. }
  861. EXPORT_SYMBOL(iio_device_alloc);
  862. /**
  863. * iio_device_free() - free an iio_dev from a driver
  864. * @dev: the iio_dev associated with the device
  865. **/
  866. void iio_device_free(struct iio_dev *dev)
  867. {
  868. if (dev)
  869. put_device(&dev->dev);
  870. }
  871. EXPORT_SYMBOL(iio_device_free);
  872. static void devm_iio_device_release(struct device *dev, void *res)
  873. {
  874. iio_device_free(*(struct iio_dev **)res);
  875. }
  876. static int devm_iio_device_match(struct device *dev, void *res, void *data)
  877. {
  878. struct iio_dev **r = res;
  879. if (!r || !*r) {
  880. WARN_ON(!r || !*r);
  881. return 0;
  882. }
  883. return *r == data;
  884. }
  885. /**
  886. * devm_iio_device_alloc - Resource-managed iio_device_alloc()
  887. * @dev: Device to allocate iio_dev for
  888. * @sizeof_priv: Space to allocate for private structure.
  889. *
  890. * Managed iio_device_alloc. iio_dev allocated with this function is
  891. * automatically freed on driver detach.
  892. *
  893. * If an iio_dev allocated with this function needs to be freed separately,
  894. * devm_iio_device_free() must be used.
  895. *
  896. * RETURNS:
  897. * Pointer to allocated iio_dev on success, NULL on failure.
  898. */
  899. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  900. {
  901. struct iio_dev **ptr, *iio_dev;
  902. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  903. GFP_KERNEL);
  904. if (!ptr)
  905. return NULL;
  906. /* use raw alloc_dr for kmalloc caller tracing */
  907. iio_dev = iio_device_alloc(sizeof_priv);
  908. if (iio_dev) {
  909. *ptr = iio_dev;
  910. devres_add(dev, ptr);
  911. } else {
  912. devres_free(ptr);
  913. }
  914. return iio_dev;
  915. }
  916. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  917. /**
  918. * devm_iio_device_free - Resource-managed iio_device_free()
  919. * @dev: Device this iio_dev belongs to
  920. * @iio_dev: the iio_dev associated with the device
  921. *
  922. * Free iio_dev allocated with devm_iio_device_alloc().
  923. */
  924. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  925. {
  926. int rc;
  927. rc = devres_release(dev, devm_iio_device_release,
  928. devm_iio_device_match, iio_dev);
  929. WARN_ON(rc);
  930. }
  931. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  932. /**
  933. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  934. **/
  935. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  936. {
  937. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  938. struct iio_dev, chrdev);
  939. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  940. return -EBUSY;
  941. iio_device_get(indio_dev);
  942. filp->private_data = indio_dev;
  943. return 0;
  944. }
  945. /**
  946. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  947. **/
  948. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  949. {
  950. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  951. struct iio_dev, chrdev);
  952. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  953. iio_device_put(indio_dev);
  954. return 0;
  955. }
  956. /* Somewhat of a cross file organization violation - ioctls here are actually
  957. * event related */
  958. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  959. {
  960. struct iio_dev *indio_dev = filp->private_data;
  961. int __user *ip = (int __user *)arg;
  962. int fd;
  963. if (!indio_dev->info)
  964. return -ENODEV;
  965. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  966. fd = iio_event_getfd(indio_dev);
  967. if (copy_to_user(ip, &fd, sizeof(fd)))
  968. return -EFAULT;
  969. return 0;
  970. }
  971. return -EINVAL;
  972. }
  973. static const struct file_operations iio_buffer_fileops = {
  974. .read = iio_buffer_read_first_n_outer_addr,
  975. .release = iio_chrdev_release,
  976. .open = iio_chrdev_open,
  977. .poll = iio_buffer_poll_addr,
  978. .owner = THIS_MODULE,
  979. .llseek = noop_llseek,
  980. .unlocked_ioctl = iio_ioctl,
  981. .compat_ioctl = iio_ioctl,
  982. };
  983. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  984. /**
  985. * iio_device_register() - register a device with the IIO subsystem
  986. * @indio_dev: Device structure filled by the device driver
  987. **/
  988. int iio_device_register(struct iio_dev *indio_dev)
  989. {
  990. int ret;
  991. /* If the calling driver did not initialize of_node, do it here */
  992. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  993. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  994. /* configure elements for the chrdev */
  995. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  996. ret = iio_device_register_debugfs(indio_dev);
  997. if (ret) {
  998. dev_err(indio_dev->dev.parent,
  999. "Failed to register debugfs interfaces\n");
  1000. return ret;
  1001. }
  1002. ret = iio_device_register_sysfs(indio_dev);
  1003. if (ret) {
  1004. dev_err(indio_dev->dev.parent,
  1005. "Failed to register sysfs interfaces\n");
  1006. goto error_unreg_debugfs;
  1007. }
  1008. ret = iio_device_register_eventset(indio_dev);
  1009. if (ret) {
  1010. dev_err(indio_dev->dev.parent,
  1011. "Failed to register event set\n");
  1012. goto error_free_sysfs;
  1013. }
  1014. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  1015. iio_device_register_trigger_consumer(indio_dev);
  1016. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  1017. indio_dev->setup_ops == NULL)
  1018. indio_dev->setup_ops = &noop_ring_setup_ops;
  1019. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  1020. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  1021. indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
  1022. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  1023. if (ret < 0)
  1024. goto error_unreg_eventset;
  1025. ret = device_add(&indio_dev->dev);
  1026. if (ret < 0)
  1027. goto error_cdev_del;
  1028. return 0;
  1029. error_cdev_del:
  1030. cdev_del(&indio_dev->chrdev);
  1031. error_unreg_eventset:
  1032. iio_device_unregister_eventset(indio_dev);
  1033. error_free_sysfs:
  1034. iio_device_unregister_sysfs(indio_dev);
  1035. error_unreg_debugfs:
  1036. iio_device_unregister_debugfs(indio_dev);
  1037. return ret;
  1038. }
  1039. EXPORT_SYMBOL(iio_device_register);
  1040. /**
  1041. * iio_device_unregister() - unregister a device from the IIO subsystem
  1042. * @indio_dev: Device structure representing the device.
  1043. **/
  1044. void iio_device_unregister(struct iio_dev *indio_dev)
  1045. {
  1046. mutex_lock(&indio_dev->info_exist_lock);
  1047. device_del(&indio_dev->dev);
  1048. if (indio_dev->chrdev.dev)
  1049. cdev_del(&indio_dev->chrdev);
  1050. iio_device_unregister_debugfs(indio_dev);
  1051. iio_disable_all_buffers(indio_dev);
  1052. indio_dev->info = NULL;
  1053. iio_device_wakeup_eventset(indio_dev);
  1054. iio_buffer_wakeup_poll(indio_dev);
  1055. mutex_unlock(&indio_dev->info_exist_lock);
  1056. }
  1057. EXPORT_SYMBOL(iio_device_unregister);
  1058. static void devm_iio_device_unreg(struct device *dev, void *res)
  1059. {
  1060. iio_device_unregister(*(struct iio_dev **)res);
  1061. }
  1062. /**
  1063. * devm_iio_device_register - Resource-managed iio_device_register()
  1064. * @dev: Device to allocate iio_dev for
  1065. * @indio_dev: Device structure filled by the device driver
  1066. *
  1067. * Managed iio_device_register. The IIO device registered with this
  1068. * function is automatically unregistered on driver detach. This function
  1069. * calls iio_device_register() internally. Refer to that function for more
  1070. * information.
  1071. *
  1072. * If an iio_dev registered with this function needs to be unregistered
  1073. * separately, devm_iio_device_unregister() must be used.
  1074. *
  1075. * RETURNS:
  1076. * 0 on success, negative error number on failure.
  1077. */
  1078. int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
  1079. {
  1080. struct iio_dev **ptr;
  1081. int ret;
  1082. ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
  1083. if (!ptr)
  1084. return -ENOMEM;
  1085. *ptr = indio_dev;
  1086. ret = iio_device_register(indio_dev);
  1087. if (!ret)
  1088. devres_add(dev, ptr);
  1089. else
  1090. devres_free(ptr);
  1091. return ret;
  1092. }
  1093. EXPORT_SYMBOL_GPL(devm_iio_device_register);
  1094. /**
  1095. * devm_iio_device_unregister - Resource-managed iio_device_unregister()
  1096. * @dev: Device this iio_dev belongs to
  1097. * @indio_dev: the iio_dev associated with the device
  1098. *
  1099. * Unregister iio_dev registered with devm_iio_device_register().
  1100. */
  1101. void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
  1102. {
  1103. int rc;
  1104. rc = devres_release(dev, devm_iio_device_unreg,
  1105. devm_iio_device_match, indio_dev);
  1106. WARN_ON(rc);
  1107. }
  1108. EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
  1109. subsys_initcall(iio_init);
  1110. module_exit(iio_exit);
  1111. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1112. MODULE_DESCRIPTION("Industrial I/O core");
  1113. MODULE_LICENSE("GPL");