industrialio-core.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. Note: if you want to reuse the list after calling
  732. * this function you have to reinitialize it using INIT_LIST_HEAD().
  733. */
  734. void iio_free_chan_devattr_list(struct list_head *attr_list)
  735. {
  736. struct iio_dev_attr *p, *n;
  737. list_for_each_entry_safe(p, n, attr_list, l) {
  738. kfree(p->dev_attr.attr.name);
  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. }
  808. static void iio_dev_release(struct device *device)
  809. {
  810. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  811. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  812. iio_device_unregister_trigger_consumer(indio_dev);
  813. iio_device_unregister_eventset(indio_dev);
  814. iio_device_unregister_sysfs(indio_dev);
  815. iio_buffer_put(indio_dev->buffer);
  816. ida_simple_remove(&iio_ida, indio_dev->id);
  817. kfree(indio_dev);
  818. }
  819. struct device_type iio_device_type = {
  820. .name = "iio_device",
  821. .release = iio_dev_release,
  822. };
  823. /**
  824. * iio_device_alloc() - allocate an iio_dev from a driver
  825. * @sizeof_priv: Space to allocate for private structure.
  826. **/
  827. struct iio_dev *iio_device_alloc(int sizeof_priv)
  828. {
  829. struct iio_dev *dev;
  830. size_t alloc_size;
  831. alloc_size = sizeof(struct iio_dev);
  832. if (sizeof_priv) {
  833. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  834. alloc_size += sizeof_priv;
  835. }
  836. /* ensure 32-byte alignment of whole construct ? */
  837. alloc_size += IIO_ALIGN - 1;
  838. dev = kzalloc(alloc_size, GFP_KERNEL);
  839. if (dev) {
  840. dev->dev.groups = dev->groups;
  841. dev->dev.type = &iio_device_type;
  842. dev->dev.bus = &iio_bus_type;
  843. device_initialize(&dev->dev);
  844. dev_set_drvdata(&dev->dev, (void *)dev);
  845. mutex_init(&dev->mlock);
  846. mutex_init(&dev->info_exist_lock);
  847. INIT_LIST_HEAD(&dev->channel_attr_list);
  848. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  849. if (dev->id < 0) {
  850. /* cannot use a dev_err as the name isn't available */
  851. pr_err("failed to get device id\n");
  852. kfree(dev);
  853. return NULL;
  854. }
  855. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  856. INIT_LIST_HEAD(&dev->buffer_list);
  857. }
  858. return dev;
  859. }
  860. EXPORT_SYMBOL(iio_device_alloc);
  861. /**
  862. * iio_device_free() - free an iio_dev from a driver
  863. * @dev: the iio_dev associated with the device
  864. **/
  865. void iio_device_free(struct iio_dev *dev)
  866. {
  867. if (dev)
  868. put_device(&dev->dev);
  869. }
  870. EXPORT_SYMBOL(iio_device_free);
  871. static void devm_iio_device_release(struct device *dev, void *res)
  872. {
  873. iio_device_free(*(struct iio_dev **)res);
  874. }
  875. static int devm_iio_device_match(struct device *dev, void *res, void *data)
  876. {
  877. struct iio_dev **r = res;
  878. if (!r || !*r) {
  879. WARN_ON(!r || !*r);
  880. return 0;
  881. }
  882. return *r == data;
  883. }
  884. /**
  885. * devm_iio_device_alloc - Resource-managed iio_device_alloc()
  886. * @dev: Device to allocate iio_dev for
  887. * @sizeof_priv: Space to allocate for private structure.
  888. *
  889. * Managed iio_device_alloc. iio_dev allocated with this function is
  890. * automatically freed on driver detach.
  891. *
  892. * If an iio_dev allocated with this function needs to be freed separately,
  893. * devm_iio_device_free() must be used.
  894. *
  895. * RETURNS:
  896. * Pointer to allocated iio_dev on success, NULL on failure.
  897. */
  898. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  899. {
  900. struct iio_dev **ptr, *iio_dev;
  901. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  902. GFP_KERNEL);
  903. if (!ptr)
  904. return NULL;
  905. /* use raw alloc_dr for kmalloc caller tracing */
  906. iio_dev = iio_device_alloc(sizeof_priv);
  907. if (iio_dev) {
  908. *ptr = iio_dev;
  909. devres_add(dev, ptr);
  910. } else {
  911. devres_free(ptr);
  912. }
  913. return iio_dev;
  914. }
  915. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  916. /**
  917. * devm_iio_device_free - Resource-managed iio_device_free()
  918. * @dev: Device this iio_dev belongs to
  919. * @iio_dev: the iio_dev associated with the device
  920. *
  921. * Free iio_dev allocated with devm_iio_device_alloc().
  922. */
  923. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  924. {
  925. int rc;
  926. rc = devres_release(dev, devm_iio_device_release,
  927. devm_iio_device_match, iio_dev);
  928. WARN_ON(rc);
  929. }
  930. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  931. /**
  932. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  933. **/
  934. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  935. {
  936. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  937. struct iio_dev, chrdev);
  938. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  939. return -EBUSY;
  940. iio_device_get(indio_dev);
  941. filp->private_data = indio_dev;
  942. return 0;
  943. }
  944. /**
  945. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  946. **/
  947. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  948. {
  949. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  950. struct iio_dev, chrdev);
  951. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  952. iio_device_put(indio_dev);
  953. return 0;
  954. }
  955. /* Somewhat of a cross file organization violation - ioctls here are actually
  956. * event related */
  957. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  958. {
  959. struct iio_dev *indio_dev = filp->private_data;
  960. int __user *ip = (int __user *)arg;
  961. int fd;
  962. if (!indio_dev->info)
  963. return -ENODEV;
  964. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  965. fd = iio_event_getfd(indio_dev);
  966. if (copy_to_user(ip, &fd, sizeof(fd)))
  967. return -EFAULT;
  968. return 0;
  969. }
  970. return -EINVAL;
  971. }
  972. static const struct file_operations iio_buffer_fileops = {
  973. .read = iio_buffer_read_first_n_outer_addr,
  974. .release = iio_chrdev_release,
  975. .open = iio_chrdev_open,
  976. .poll = iio_buffer_poll_addr,
  977. .owner = THIS_MODULE,
  978. .llseek = noop_llseek,
  979. .unlocked_ioctl = iio_ioctl,
  980. .compat_ioctl = iio_ioctl,
  981. };
  982. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  983. /**
  984. * iio_device_register() - register a device with the IIO subsystem
  985. * @indio_dev: Device structure filled by the device driver
  986. **/
  987. int iio_device_register(struct iio_dev *indio_dev)
  988. {
  989. int ret;
  990. /* If the calling driver did not initialize of_node, do it here */
  991. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  992. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  993. /* configure elements for the chrdev */
  994. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  995. ret = iio_device_register_debugfs(indio_dev);
  996. if (ret) {
  997. dev_err(indio_dev->dev.parent,
  998. "Failed to register debugfs interfaces\n");
  999. return ret;
  1000. }
  1001. ret = iio_device_register_sysfs(indio_dev);
  1002. if (ret) {
  1003. dev_err(indio_dev->dev.parent,
  1004. "Failed to register sysfs interfaces\n");
  1005. goto error_unreg_debugfs;
  1006. }
  1007. ret = iio_device_register_eventset(indio_dev);
  1008. if (ret) {
  1009. dev_err(indio_dev->dev.parent,
  1010. "Failed to register event set\n");
  1011. goto error_free_sysfs;
  1012. }
  1013. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  1014. iio_device_register_trigger_consumer(indio_dev);
  1015. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  1016. indio_dev->setup_ops == NULL)
  1017. indio_dev->setup_ops = &noop_ring_setup_ops;
  1018. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  1019. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  1020. indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
  1021. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  1022. if (ret < 0)
  1023. goto error_unreg_eventset;
  1024. ret = device_add(&indio_dev->dev);
  1025. if (ret < 0)
  1026. goto error_cdev_del;
  1027. return 0;
  1028. error_cdev_del:
  1029. cdev_del(&indio_dev->chrdev);
  1030. error_unreg_eventset:
  1031. iio_device_unregister_eventset(indio_dev);
  1032. error_free_sysfs:
  1033. iio_device_unregister_sysfs(indio_dev);
  1034. error_unreg_debugfs:
  1035. iio_device_unregister_debugfs(indio_dev);
  1036. return ret;
  1037. }
  1038. EXPORT_SYMBOL(iio_device_register);
  1039. /**
  1040. * iio_device_unregister() - unregister a device from the IIO subsystem
  1041. * @indio_dev: Device structure representing the device.
  1042. **/
  1043. void iio_device_unregister(struct iio_dev *indio_dev)
  1044. {
  1045. mutex_lock(&indio_dev->info_exist_lock);
  1046. device_del(&indio_dev->dev);
  1047. if (indio_dev->chrdev.dev)
  1048. cdev_del(&indio_dev->chrdev);
  1049. iio_device_unregister_debugfs(indio_dev);
  1050. iio_disable_all_buffers(indio_dev);
  1051. indio_dev->info = NULL;
  1052. iio_device_wakeup_eventset(indio_dev);
  1053. iio_buffer_wakeup_poll(indio_dev);
  1054. mutex_unlock(&indio_dev->info_exist_lock);
  1055. }
  1056. EXPORT_SYMBOL(iio_device_unregister);
  1057. static void devm_iio_device_unreg(struct device *dev, void *res)
  1058. {
  1059. iio_device_unregister(*(struct iio_dev **)res);
  1060. }
  1061. /**
  1062. * devm_iio_device_register - Resource-managed iio_device_register()
  1063. * @dev: Device to allocate iio_dev for
  1064. * @indio_dev: Device structure filled by the device driver
  1065. *
  1066. * Managed iio_device_register. The IIO device registered with this
  1067. * function is automatically unregistered on driver detach. This function
  1068. * calls iio_device_register() internally. Refer to that function for more
  1069. * information.
  1070. *
  1071. * If an iio_dev registered with this function needs to be unregistered
  1072. * separately, devm_iio_device_unregister() must be used.
  1073. *
  1074. * RETURNS:
  1075. * 0 on success, negative error number on failure.
  1076. */
  1077. int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
  1078. {
  1079. struct iio_dev **ptr;
  1080. int ret;
  1081. ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
  1082. if (!ptr)
  1083. return -ENOMEM;
  1084. *ptr = indio_dev;
  1085. ret = iio_device_register(indio_dev);
  1086. if (!ret)
  1087. devres_add(dev, ptr);
  1088. else
  1089. devres_free(ptr);
  1090. return ret;
  1091. }
  1092. EXPORT_SYMBOL_GPL(devm_iio_device_register);
  1093. /**
  1094. * devm_iio_device_unregister - Resource-managed iio_device_unregister()
  1095. * @dev: Device this iio_dev belongs to
  1096. * @indio_dev: the iio_dev associated with the device
  1097. *
  1098. * Unregister iio_dev registered with devm_iio_device_register().
  1099. */
  1100. void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
  1101. {
  1102. int rc;
  1103. rc = devres_release(dev, devm_iio_device_unreg,
  1104. devm_iio_device_match, indio_dev);
  1105. WARN_ON(rc);
  1106. }
  1107. EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
  1108. subsys_initcall(iio_init);
  1109. module_exit(iio_exit);
  1110. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1111. MODULE_DESCRIPTION("Industrial I/O core");
  1112. MODULE_LICENSE("GPL");