industrialio-core.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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/mutex.h>
  27. #include <linux/iio/iio.h>
  28. #include "iio_core.h"
  29. #include "iio_core_trigger.h"
  30. #include <linux/iio/sysfs.h>
  31. #include <linux/iio/events.h>
  32. #include <linux/iio/buffer.h>
  33. #include <linux/iio/buffer_impl.h>
  34. /* IDA to assign each registered device a unique id */
  35. static DEFINE_IDA(iio_ida);
  36. static dev_t iio_devt;
  37. #define IIO_DEV_MAX 256
  38. struct bus_type iio_bus_type = {
  39. .name = "iio",
  40. };
  41. EXPORT_SYMBOL(iio_bus_type);
  42. static struct dentry *iio_debugfs_dentry;
  43. static const char * const iio_direction[] = {
  44. [0] = "in",
  45. [1] = "out",
  46. };
  47. static const char * const iio_chan_type_name_spec[] = {
  48. [IIO_VOLTAGE] = "voltage",
  49. [IIO_CURRENT] = "current",
  50. [IIO_POWER] = "power",
  51. [IIO_ACCEL] = "accel",
  52. [IIO_ANGL_VEL] = "anglvel",
  53. [IIO_MAGN] = "magn",
  54. [IIO_LIGHT] = "illuminance",
  55. [IIO_INTENSITY] = "intensity",
  56. [IIO_PROXIMITY] = "proximity",
  57. [IIO_TEMP] = "temp",
  58. [IIO_INCLI] = "incli",
  59. [IIO_ROT] = "rot",
  60. [IIO_ANGL] = "angl",
  61. [IIO_TIMESTAMP] = "timestamp",
  62. [IIO_CAPACITANCE] = "capacitance",
  63. [IIO_ALTVOLTAGE] = "altvoltage",
  64. [IIO_CCT] = "cct",
  65. [IIO_PRESSURE] = "pressure",
  66. [IIO_HUMIDITYRELATIVE] = "humidityrelative",
  67. [IIO_ACTIVITY] = "activity",
  68. [IIO_STEPS] = "steps",
  69. [IIO_ENERGY] = "energy",
  70. [IIO_DISTANCE] = "distance",
  71. [IIO_VELOCITY] = "velocity",
  72. [IIO_CONCENTRATION] = "concentration",
  73. [IIO_RESISTANCE] = "resistance",
  74. [IIO_PH] = "ph",
  75. [IIO_UVINDEX] = "uvindex",
  76. [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
  77. [IIO_COUNT] = "count",
  78. [IIO_INDEX] = "index",
  79. [IIO_GRAVITY] = "gravity",
  80. };
  81. static const char * const iio_modifier_names[] = {
  82. [IIO_MOD_X] = "x",
  83. [IIO_MOD_Y] = "y",
  84. [IIO_MOD_Z] = "z",
  85. [IIO_MOD_X_AND_Y] = "x&y",
  86. [IIO_MOD_X_AND_Z] = "x&z",
  87. [IIO_MOD_Y_AND_Z] = "y&z",
  88. [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
  89. [IIO_MOD_X_OR_Y] = "x|y",
  90. [IIO_MOD_X_OR_Z] = "x|z",
  91. [IIO_MOD_Y_OR_Z] = "y|z",
  92. [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
  93. [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
  94. [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
  95. [IIO_MOD_LIGHT_BOTH] = "both",
  96. [IIO_MOD_LIGHT_IR] = "ir",
  97. [IIO_MOD_LIGHT_CLEAR] = "clear",
  98. [IIO_MOD_LIGHT_RED] = "red",
  99. [IIO_MOD_LIGHT_GREEN] = "green",
  100. [IIO_MOD_LIGHT_BLUE] = "blue",
  101. [IIO_MOD_LIGHT_UV] = "uv",
  102. [IIO_MOD_QUATERNION] = "quaternion",
  103. [IIO_MOD_TEMP_AMBIENT] = "ambient",
  104. [IIO_MOD_TEMP_OBJECT] = "object",
  105. [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
  106. [IIO_MOD_NORTH_TRUE] = "from_north_true",
  107. [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
  108. [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
  109. [IIO_MOD_RUNNING] = "running",
  110. [IIO_MOD_JOGGING] = "jogging",
  111. [IIO_MOD_WALKING] = "walking",
  112. [IIO_MOD_STILL] = "still",
  113. [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
  114. [IIO_MOD_I] = "i",
  115. [IIO_MOD_Q] = "q",
  116. [IIO_MOD_CO2] = "co2",
  117. [IIO_MOD_VOC] = "voc",
  118. };
  119. /* relies on pairs of these shared then separate */
  120. static const char * const iio_chan_info_postfix[] = {
  121. [IIO_CHAN_INFO_RAW] = "raw",
  122. [IIO_CHAN_INFO_PROCESSED] = "input",
  123. [IIO_CHAN_INFO_SCALE] = "scale",
  124. [IIO_CHAN_INFO_OFFSET] = "offset",
  125. [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
  126. [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
  127. [IIO_CHAN_INFO_PEAK] = "peak_raw",
  128. [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
  129. [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
  130. [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
  131. [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
  132. = "filter_low_pass_3db_frequency",
  133. [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
  134. = "filter_high_pass_3db_frequency",
  135. [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
  136. [IIO_CHAN_INFO_FREQUENCY] = "frequency",
  137. [IIO_CHAN_INFO_PHASE] = "phase",
  138. [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
  139. [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
  140. [IIO_CHAN_INFO_INT_TIME] = "integration_time",
  141. [IIO_CHAN_INFO_ENABLE] = "en",
  142. [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
  143. [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
  144. [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
  145. [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
  146. [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
  147. [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
  148. };
  149. /**
  150. * iio_find_channel_from_si() - get channel from its scan index
  151. * @indio_dev: device
  152. * @si: scan index to match
  153. */
  154. const struct iio_chan_spec
  155. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
  156. {
  157. int i;
  158. for (i = 0; i < indio_dev->num_channels; i++)
  159. if (indio_dev->channels[i].scan_index == si)
  160. return &indio_dev->channels[i];
  161. return NULL;
  162. }
  163. /* This turns up an awful lot */
  164. ssize_t iio_read_const_attr(struct device *dev,
  165. struct device_attribute *attr,
  166. char *buf)
  167. {
  168. return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
  169. }
  170. EXPORT_SYMBOL(iio_read_const_attr);
  171. static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
  172. {
  173. int ret;
  174. const struct iio_event_interface *ev_int = indio_dev->event_interface;
  175. ret = mutex_lock_interruptible(&indio_dev->mlock);
  176. if (ret)
  177. return ret;
  178. if ((ev_int && iio_event_enabled(ev_int)) ||
  179. iio_buffer_enabled(indio_dev)) {
  180. mutex_unlock(&indio_dev->mlock);
  181. return -EBUSY;
  182. }
  183. indio_dev->clock_id = clock_id;
  184. mutex_unlock(&indio_dev->mlock);
  185. return 0;
  186. }
  187. /**
  188. * iio_get_time_ns() - utility function to get a time stamp for events etc
  189. * @indio_dev: device
  190. */
  191. s64 iio_get_time_ns(const struct iio_dev *indio_dev)
  192. {
  193. struct timespec tp;
  194. switch (iio_device_get_clock(indio_dev)) {
  195. case CLOCK_REALTIME:
  196. ktime_get_real_ts(&tp);
  197. break;
  198. case CLOCK_MONOTONIC:
  199. ktime_get_ts(&tp);
  200. break;
  201. case CLOCK_MONOTONIC_RAW:
  202. getrawmonotonic(&tp);
  203. break;
  204. case CLOCK_REALTIME_COARSE:
  205. tp = current_kernel_time();
  206. break;
  207. case CLOCK_MONOTONIC_COARSE:
  208. tp = get_monotonic_coarse();
  209. break;
  210. case CLOCK_BOOTTIME:
  211. get_monotonic_boottime(&tp);
  212. break;
  213. case CLOCK_TAI:
  214. timekeeping_clocktai(&tp);
  215. break;
  216. default:
  217. BUG();
  218. }
  219. return timespec_to_ns(&tp);
  220. }
  221. EXPORT_SYMBOL(iio_get_time_ns);
  222. /**
  223. * iio_get_time_res() - utility function to get time stamp clock resolution in
  224. * nano seconds.
  225. * @indio_dev: device
  226. */
  227. unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
  228. {
  229. switch (iio_device_get_clock(indio_dev)) {
  230. case CLOCK_REALTIME:
  231. case CLOCK_MONOTONIC:
  232. case CLOCK_MONOTONIC_RAW:
  233. case CLOCK_BOOTTIME:
  234. case CLOCK_TAI:
  235. return hrtimer_resolution;
  236. case CLOCK_REALTIME_COARSE:
  237. case CLOCK_MONOTONIC_COARSE:
  238. return LOW_RES_NSEC;
  239. default:
  240. BUG();
  241. }
  242. }
  243. EXPORT_SYMBOL(iio_get_time_res);
  244. static int __init iio_init(void)
  245. {
  246. int ret;
  247. /* Register sysfs bus */
  248. ret = bus_register(&iio_bus_type);
  249. if (ret < 0) {
  250. pr_err("could not register bus type\n");
  251. goto error_nothing;
  252. }
  253. ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
  254. if (ret < 0) {
  255. pr_err("failed to allocate char dev region\n");
  256. goto error_unregister_bus_type;
  257. }
  258. iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
  259. return 0;
  260. error_unregister_bus_type:
  261. bus_unregister(&iio_bus_type);
  262. error_nothing:
  263. return ret;
  264. }
  265. static void __exit iio_exit(void)
  266. {
  267. if (iio_devt)
  268. unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
  269. bus_unregister(&iio_bus_type);
  270. debugfs_remove(iio_debugfs_dentry);
  271. }
  272. #if defined(CONFIG_DEBUG_FS)
  273. static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
  274. size_t count, loff_t *ppos)
  275. {
  276. struct iio_dev *indio_dev = file->private_data;
  277. char buf[20];
  278. unsigned val = 0;
  279. ssize_t len;
  280. int ret;
  281. ret = indio_dev->info->debugfs_reg_access(indio_dev,
  282. indio_dev->cached_reg_addr,
  283. 0, &val);
  284. if (ret)
  285. dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
  286. len = snprintf(buf, sizeof(buf), "0x%X\n", val);
  287. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  288. }
  289. static ssize_t iio_debugfs_write_reg(struct file *file,
  290. const char __user *userbuf, size_t count, loff_t *ppos)
  291. {
  292. struct iio_dev *indio_dev = file->private_data;
  293. unsigned reg, val;
  294. char buf[80];
  295. int ret;
  296. count = min_t(size_t, count, (sizeof(buf)-1));
  297. if (copy_from_user(buf, userbuf, count))
  298. return -EFAULT;
  299. buf[count] = 0;
  300. ret = sscanf(buf, "%i %i", &reg, &val);
  301. switch (ret) {
  302. case 1:
  303. indio_dev->cached_reg_addr = reg;
  304. break;
  305. case 2:
  306. indio_dev->cached_reg_addr = reg;
  307. ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
  308. val, NULL);
  309. if (ret) {
  310. dev_err(indio_dev->dev.parent, "%s: write failed\n",
  311. __func__);
  312. return ret;
  313. }
  314. break;
  315. default:
  316. return -EINVAL;
  317. }
  318. return count;
  319. }
  320. static const struct file_operations iio_debugfs_reg_fops = {
  321. .open = simple_open,
  322. .read = iio_debugfs_read_reg,
  323. .write = iio_debugfs_write_reg,
  324. };
  325. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  326. {
  327. debugfs_remove_recursive(indio_dev->debugfs_dentry);
  328. }
  329. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  330. {
  331. struct dentry *d;
  332. if (indio_dev->info->debugfs_reg_access == NULL)
  333. return 0;
  334. if (!iio_debugfs_dentry)
  335. return 0;
  336. indio_dev->debugfs_dentry =
  337. debugfs_create_dir(dev_name(&indio_dev->dev),
  338. iio_debugfs_dentry);
  339. if (indio_dev->debugfs_dentry == NULL) {
  340. dev_warn(indio_dev->dev.parent,
  341. "Failed to create debugfs directory\n");
  342. return -EFAULT;
  343. }
  344. d = debugfs_create_file("direct_reg_access", 0644,
  345. indio_dev->debugfs_dentry,
  346. indio_dev, &iio_debugfs_reg_fops);
  347. if (!d) {
  348. iio_device_unregister_debugfs(indio_dev);
  349. return -ENOMEM;
  350. }
  351. return 0;
  352. }
  353. #else
  354. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  355. {
  356. return 0;
  357. }
  358. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  359. {
  360. }
  361. #endif /* CONFIG_DEBUG_FS */
  362. static ssize_t iio_read_channel_ext_info(struct device *dev,
  363. struct device_attribute *attr,
  364. char *buf)
  365. {
  366. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  367. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  368. const struct iio_chan_spec_ext_info *ext_info;
  369. ext_info = &this_attr->c->ext_info[this_attr->address];
  370. return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
  371. }
  372. static ssize_t iio_write_channel_ext_info(struct device *dev,
  373. struct device_attribute *attr,
  374. const char *buf,
  375. size_t len)
  376. {
  377. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  378. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  379. const struct iio_chan_spec_ext_info *ext_info;
  380. ext_info = &this_attr->c->ext_info[this_attr->address];
  381. return ext_info->write(indio_dev, ext_info->private,
  382. this_attr->c, buf, len);
  383. }
  384. ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
  385. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  386. {
  387. const struct iio_enum *e = (const struct iio_enum *)priv;
  388. unsigned int i;
  389. size_t len = 0;
  390. if (!e->num_items)
  391. return 0;
  392. for (i = 0; i < e->num_items; ++i)
  393. len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
  394. /* replace last space with a newline */
  395. buf[len - 1] = '\n';
  396. return len;
  397. }
  398. EXPORT_SYMBOL_GPL(iio_enum_available_read);
  399. ssize_t iio_enum_read(struct iio_dev *indio_dev,
  400. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  401. {
  402. const struct iio_enum *e = (const struct iio_enum *)priv;
  403. int i;
  404. if (!e->get)
  405. return -EINVAL;
  406. i = e->get(indio_dev, chan);
  407. if (i < 0)
  408. return i;
  409. else if (i >= e->num_items)
  410. return -EINVAL;
  411. return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
  412. }
  413. EXPORT_SYMBOL_GPL(iio_enum_read);
  414. ssize_t iio_enum_write(struct iio_dev *indio_dev,
  415. uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
  416. size_t len)
  417. {
  418. const struct iio_enum *e = (const struct iio_enum *)priv;
  419. unsigned int i;
  420. int ret;
  421. if (!e->set)
  422. return -EINVAL;
  423. for (i = 0; i < e->num_items; i++) {
  424. if (sysfs_streq(buf, e->items[i]))
  425. break;
  426. }
  427. if (i == e->num_items)
  428. return -EINVAL;
  429. ret = e->set(indio_dev, chan, i);
  430. return ret ? ret : len;
  431. }
  432. EXPORT_SYMBOL_GPL(iio_enum_write);
  433. static const struct iio_mount_matrix iio_mount_idmatrix = {
  434. .rotation = {
  435. "1", "0", "0",
  436. "0", "1", "0",
  437. "0", "0", "1"
  438. }
  439. };
  440. static int iio_setup_mount_idmatrix(const struct device *dev,
  441. struct iio_mount_matrix *matrix)
  442. {
  443. *matrix = iio_mount_idmatrix;
  444. dev_info(dev, "mounting matrix not found: using identity...\n");
  445. return 0;
  446. }
  447. ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
  448. const struct iio_chan_spec *chan, char *buf)
  449. {
  450. const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
  451. priv)(indio_dev, chan);
  452. if (IS_ERR(mtx))
  453. return PTR_ERR(mtx);
  454. if (!mtx)
  455. mtx = &iio_mount_idmatrix;
  456. return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
  457. mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
  458. mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
  459. mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
  460. }
  461. EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
  462. /**
  463. * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from
  464. * device-tree "mount-matrix" property
  465. * @dev: device the mounting matrix property is assigned to
  466. * @propname: device specific mounting matrix property name
  467. * @matrix: where to store retrieved matrix
  468. *
  469. * If device is assigned no mounting matrix property, a default 3x3 identity
  470. * matrix will be filled in.
  471. *
  472. * Return: 0 if success, or a negative error code on failure.
  473. */
  474. #ifdef CONFIG_OF
  475. int of_iio_read_mount_matrix(const struct device *dev,
  476. const char *propname,
  477. struct iio_mount_matrix *matrix)
  478. {
  479. if (dev->of_node) {
  480. int err = of_property_read_string_array(dev->of_node,
  481. propname, matrix->rotation,
  482. ARRAY_SIZE(iio_mount_idmatrix.rotation));
  483. if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation))
  484. return 0;
  485. if (err >= 0)
  486. /* Invalid number of matrix entries. */
  487. return -EINVAL;
  488. if (err != -EINVAL)
  489. /* Invalid matrix declaration format. */
  490. return err;
  491. }
  492. /* Matrix was not declared at all: fallback to identity. */
  493. return iio_setup_mount_idmatrix(dev, matrix);
  494. }
  495. #else
  496. int of_iio_read_mount_matrix(const struct device *dev,
  497. const char *propname,
  498. struct iio_mount_matrix *matrix)
  499. {
  500. return iio_setup_mount_idmatrix(dev, matrix);
  501. }
  502. #endif
  503. EXPORT_SYMBOL(of_iio_read_mount_matrix);
  504. static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
  505. int size, const int *vals)
  506. {
  507. unsigned long long tmp;
  508. int tmp0, tmp1;
  509. bool scale_db = false;
  510. switch (type) {
  511. case IIO_VAL_INT:
  512. return snprintf(buf, len, "%d", vals[0]);
  513. case IIO_VAL_INT_PLUS_MICRO_DB:
  514. scale_db = true;
  515. case IIO_VAL_INT_PLUS_MICRO:
  516. if (vals[1] < 0)
  517. return snprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
  518. -vals[1], scale_db ? " dB" : "");
  519. else
  520. return snprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
  521. scale_db ? " dB" : "");
  522. case IIO_VAL_INT_PLUS_NANO:
  523. if (vals[1] < 0)
  524. return snprintf(buf, len, "-%d.%09u", abs(vals[0]),
  525. -vals[1]);
  526. else
  527. return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
  528. case IIO_VAL_FRACTIONAL:
  529. tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
  530. tmp1 = vals[1];
  531. tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
  532. return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
  533. case IIO_VAL_FRACTIONAL_LOG2:
  534. tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
  535. tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
  536. return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
  537. case IIO_VAL_INT_MULTIPLE:
  538. {
  539. int i;
  540. int l = 0;
  541. for (i = 0; i < size; ++i) {
  542. l += snprintf(&buf[l], len - l, "%d ", vals[i]);
  543. if (l >= len)
  544. break;
  545. }
  546. return l;
  547. }
  548. default:
  549. return 0;
  550. }
  551. }
  552. /**
  553. * iio_format_value() - Formats a IIO value into its string representation
  554. * @buf: The buffer to which the formatted value gets written
  555. * which is assumed to be big enough (i.e. PAGE_SIZE).
  556. * @type: One of the IIO_VAL_... constants. This decides how the val
  557. * and val2 parameters are formatted.
  558. * @size: Number of IIO value entries contained in vals
  559. * @vals: Pointer to the values, exact meaning depends on the
  560. * type parameter.
  561. *
  562. * Return: 0 by default, a negative number on failure or the
  563. * total number of characters written for a type that belongs
  564. * to the IIO_VAL_... constant.
  565. */
  566. ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
  567. {
  568. ssize_t len;
  569. len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
  570. if (len >= PAGE_SIZE - 1)
  571. return -EFBIG;
  572. return len + sprintf(buf + len, "\n");
  573. }
  574. EXPORT_SYMBOL_GPL(iio_format_value);
  575. static ssize_t iio_read_channel_info(struct device *dev,
  576. struct device_attribute *attr,
  577. char *buf)
  578. {
  579. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  580. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  581. int vals[INDIO_MAX_RAW_ELEMENTS];
  582. int ret;
  583. int val_len = 2;
  584. if (indio_dev->info->read_raw_multi)
  585. ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
  586. INDIO_MAX_RAW_ELEMENTS,
  587. vals, &val_len,
  588. this_attr->address);
  589. else
  590. ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
  591. &vals[0], &vals[1], this_attr->address);
  592. if (ret < 0)
  593. return ret;
  594. return iio_format_value(buf, ret, val_len, vals);
  595. }
  596. static ssize_t iio_format_avail_list(char *buf, const int *vals,
  597. int type, int length)
  598. {
  599. int i;
  600. ssize_t len = 0;
  601. switch (type) {
  602. case IIO_VAL_INT:
  603. for (i = 0; i < length; i++) {
  604. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  605. type, 1, &vals[i]);
  606. if (len >= PAGE_SIZE)
  607. return -EFBIG;
  608. if (i < length - 1)
  609. len += snprintf(buf + len, PAGE_SIZE - len,
  610. " ");
  611. else
  612. len += snprintf(buf + len, PAGE_SIZE - len,
  613. "\n");
  614. if (len >= PAGE_SIZE)
  615. return -EFBIG;
  616. }
  617. break;
  618. default:
  619. for (i = 0; i < length / 2; i++) {
  620. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  621. type, 2, &vals[i * 2]);
  622. if (len >= PAGE_SIZE)
  623. return -EFBIG;
  624. if (i < length / 2 - 1)
  625. len += snprintf(buf + len, PAGE_SIZE - len,
  626. " ");
  627. else
  628. len += snprintf(buf + len, PAGE_SIZE - len,
  629. "\n");
  630. if (len >= PAGE_SIZE)
  631. return -EFBIG;
  632. }
  633. }
  634. return len;
  635. }
  636. static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
  637. {
  638. int i;
  639. ssize_t len;
  640. len = snprintf(buf, PAGE_SIZE, "[");
  641. switch (type) {
  642. case IIO_VAL_INT:
  643. for (i = 0; i < 3; i++) {
  644. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  645. type, 1, &vals[i]);
  646. if (len >= PAGE_SIZE)
  647. return -EFBIG;
  648. if (i < 2)
  649. len += snprintf(buf + len, PAGE_SIZE - len,
  650. " ");
  651. else
  652. len += snprintf(buf + len, PAGE_SIZE - len,
  653. "]\n");
  654. if (len >= PAGE_SIZE)
  655. return -EFBIG;
  656. }
  657. break;
  658. default:
  659. for (i = 0; i < 3; i++) {
  660. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  661. type, 2, &vals[i * 2]);
  662. if (len >= PAGE_SIZE)
  663. return -EFBIG;
  664. if (i < 2)
  665. len += snprintf(buf + len, PAGE_SIZE - len,
  666. " ");
  667. else
  668. len += snprintf(buf + len, PAGE_SIZE - len,
  669. "]\n");
  670. if (len >= PAGE_SIZE)
  671. return -EFBIG;
  672. }
  673. }
  674. return len;
  675. }
  676. static ssize_t iio_read_channel_info_avail(struct device *dev,
  677. struct device_attribute *attr,
  678. char *buf)
  679. {
  680. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  681. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  682. const int *vals;
  683. int ret;
  684. int length;
  685. int type;
  686. ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
  687. &vals, &type, &length,
  688. this_attr->address);
  689. if (ret < 0)
  690. return ret;
  691. switch (ret) {
  692. case IIO_AVAIL_LIST:
  693. return iio_format_avail_list(buf, vals, type, length);
  694. case IIO_AVAIL_RANGE:
  695. return iio_format_avail_range(buf, vals, type);
  696. default:
  697. return -EINVAL;
  698. }
  699. }
  700. /**
  701. * iio_str_to_fixpoint() - Parse a fixed-point number from a string
  702. * @str: The string to parse
  703. * @fract_mult: Multiplier for the first decimal place, should be a power of 10
  704. * @integer: The integer part of the number
  705. * @fract: The fractional part of the number
  706. *
  707. * Returns 0 on success, or a negative error code if the string could not be
  708. * parsed.
  709. */
  710. int iio_str_to_fixpoint(const char *str, int fract_mult,
  711. int *integer, int *fract)
  712. {
  713. int i = 0, f = 0;
  714. bool integer_part = true, negative = false;
  715. if (fract_mult == 0) {
  716. *fract = 0;
  717. return kstrtoint(str, 0, integer);
  718. }
  719. if (str[0] == '-') {
  720. negative = true;
  721. str++;
  722. } else if (str[0] == '+') {
  723. str++;
  724. }
  725. while (*str) {
  726. if ('0' <= *str && *str <= '9') {
  727. if (integer_part) {
  728. i = i * 10 + *str - '0';
  729. } else {
  730. f += fract_mult * (*str - '0');
  731. fract_mult /= 10;
  732. }
  733. } else if (*str == '\n') {
  734. if (*(str + 1) == '\0')
  735. break;
  736. else
  737. return -EINVAL;
  738. } else if (*str == '.' && integer_part) {
  739. integer_part = false;
  740. } else {
  741. return -EINVAL;
  742. }
  743. str++;
  744. }
  745. if (negative) {
  746. if (i)
  747. i = -i;
  748. else
  749. f = -f;
  750. }
  751. *integer = i;
  752. *fract = f;
  753. return 0;
  754. }
  755. EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
  756. static ssize_t iio_write_channel_info(struct device *dev,
  757. struct device_attribute *attr,
  758. const char *buf,
  759. size_t len)
  760. {
  761. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  762. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  763. int ret, fract_mult = 100000;
  764. int integer, fract;
  765. /* Assumes decimal - precision based on number of digits */
  766. if (!indio_dev->info->write_raw)
  767. return -EINVAL;
  768. if (indio_dev->info->write_raw_get_fmt)
  769. switch (indio_dev->info->write_raw_get_fmt(indio_dev,
  770. this_attr->c, this_attr->address)) {
  771. case IIO_VAL_INT:
  772. fract_mult = 0;
  773. break;
  774. case IIO_VAL_INT_PLUS_MICRO:
  775. fract_mult = 100000;
  776. break;
  777. case IIO_VAL_INT_PLUS_NANO:
  778. fract_mult = 100000000;
  779. break;
  780. default:
  781. return -EINVAL;
  782. }
  783. ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
  784. if (ret)
  785. return ret;
  786. ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
  787. integer, fract, this_attr->address);
  788. if (ret)
  789. return ret;
  790. return len;
  791. }
  792. static
  793. int __iio_device_attr_init(struct device_attribute *dev_attr,
  794. const char *postfix,
  795. struct iio_chan_spec const *chan,
  796. ssize_t (*readfunc)(struct device *dev,
  797. struct device_attribute *attr,
  798. char *buf),
  799. ssize_t (*writefunc)(struct device *dev,
  800. struct device_attribute *attr,
  801. const char *buf,
  802. size_t len),
  803. enum iio_shared_by shared_by)
  804. {
  805. int ret = 0;
  806. char *name = NULL;
  807. char *full_postfix;
  808. sysfs_attr_init(&dev_attr->attr);
  809. /* Build up postfix of <extend_name>_<modifier>_postfix */
  810. if (chan->modified && (shared_by == IIO_SEPARATE)) {
  811. if (chan->extend_name)
  812. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  813. iio_modifier_names[chan
  814. ->channel2],
  815. chan->extend_name,
  816. postfix);
  817. else
  818. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  819. iio_modifier_names[chan
  820. ->channel2],
  821. postfix);
  822. } else {
  823. if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
  824. full_postfix = kstrdup(postfix, GFP_KERNEL);
  825. else
  826. full_postfix = kasprintf(GFP_KERNEL,
  827. "%s_%s",
  828. chan->extend_name,
  829. postfix);
  830. }
  831. if (full_postfix == NULL)
  832. return -ENOMEM;
  833. if (chan->differential) { /* Differential can not have modifier */
  834. switch (shared_by) {
  835. case IIO_SHARED_BY_ALL:
  836. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  837. break;
  838. case IIO_SHARED_BY_DIR:
  839. name = kasprintf(GFP_KERNEL, "%s_%s",
  840. iio_direction[chan->output],
  841. full_postfix);
  842. break;
  843. case IIO_SHARED_BY_TYPE:
  844. name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
  845. iio_direction[chan->output],
  846. iio_chan_type_name_spec[chan->type],
  847. iio_chan_type_name_spec[chan->type],
  848. full_postfix);
  849. break;
  850. case IIO_SEPARATE:
  851. if (!chan->indexed) {
  852. WARN(1, "Differential channels must be indexed\n");
  853. ret = -EINVAL;
  854. goto error_free_full_postfix;
  855. }
  856. name = kasprintf(GFP_KERNEL,
  857. "%s_%s%d-%s%d_%s",
  858. iio_direction[chan->output],
  859. iio_chan_type_name_spec[chan->type],
  860. chan->channel,
  861. iio_chan_type_name_spec[chan->type],
  862. chan->channel2,
  863. full_postfix);
  864. break;
  865. }
  866. } else { /* Single ended */
  867. switch (shared_by) {
  868. case IIO_SHARED_BY_ALL:
  869. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  870. break;
  871. case IIO_SHARED_BY_DIR:
  872. name = kasprintf(GFP_KERNEL, "%s_%s",
  873. iio_direction[chan->output],
  874. full_postfix);
  875. break;
  876. case IIO_SHARED_BY_TYPE:
  877. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  878. iio_direction[chan->output],
  879. iio_chan_type_name_spec[chan->type],
  880. full_postfix);
  881. break;
  882. case IIO_SEPARATE:
  883. if (chan->indexed)
  884. name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  885. iio_direction[chan->output],
  886. iio_chan_type_name_spec[chan->type],
  887. chan->channel,
  888. full_postfix);
  889. else
  890. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  891. iio_direction[chan->output],
  892. iio_chan_type_name_spec[chan->type],
  893. full_postfix);
  894. break;
  895. }
  896. }
  897. if (name == NULL) {
  898. ret = -ENOMEM;
  899. goto error_free_full_postfix;
  900. }
  901. dev_attr->attr.name = name;
  902. if (readfunc) {
  903. dev_attr->attr.mode |= S_IRUGO;
  904. dev_attr->show = readfunc;
  905. }
  906. if (writefunc) {
  907. dev_attr->attr.mode |= S_IWUSR;
  908. dev_attr->store = writefunc;
  909. }
  910. error_free_full_postfix:
  911. kfree(full_postfix);
  912. return ret;
  913. }
  914. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  915. {
  916. kfree(dev_attr->attr.name);
  917. }
  918. int __iio_add_chan_devattr(const char *postfix,
  919. struct iio_chan_spec const *chan,
  920. ssize_t (*readfunc)(struct device *dev,
  921. struct device_attribute *attr,
  922. char *buf),
  923. ssize_t (*writefunc)(struct device *dev,
  924. struct device_attribute *attr,
  925. const char *buf,
  926. size_t len),
  927. u64 mask,
  928. enum iio_shared_by shared_by,
  929. struct device *dev,
  930. struct list_head *attr_list)
  931. {
  932. int ret;
  933. struct iio_dev_attr *iio_attr, *t;
  934. iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
  935. if (iio_attr == NULL)
  936. return -ENOMEM;
  937. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  938. postfix, chan,
  939. readfunc, writefunc, shared_by);
  940. if (ret)
  941. goto error_iio_dev_attr_free;
  942. iio_attr->c = chan;
  943. iio_attr->address = mask;
  944. list_for_each_entry(t, attr_list, l)
  945. if (strcmp(t->dev_attr.attr.name,
  946. iio_attr->dev_attr.attr.name) == 0) {
  947. if (shared_by == IIO_SEPARATE)
  948. dev_err(dev, "tried to double register : %s\n",
  949. t->dev_attr.attr.name);
  950. ret = -EBUSY;
  951. goto error_device_attr_deinit;
  952. }
  953. list_add(&iio_attr->l, attr_list);
  954. return 0;
  955. error_device_attr_deinit:
  956. __iio_device_attr_deinit(&iio_attr->dev_attr);
  957. error_iio_dev_attr_free:
  958. kfree(iio_attr);
  959. return ret;
  960. }
  961. static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
  962. struct iio_chan_spec const *chan,
  963. enum iio_shared_by shared_by,
  964. const long *infomask)
  965. {
  966. int i, ret, attrcount = 0;
  967. for_each_set_bit(i, infomask, sizeof(infomask)*8) {
  968. if (i >= ARRAY_SIZE(iio_chan_info_postfix))
  969. return -EINVAL;
  970. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  971. chan,
  972. &iio_read_channel_info,
  973. &iio_write_channel_info,
  974. i,
  975. shared_by,
  976. &indio_dev->dev,
  977. &indio_dev->channel_attr_list);
  978. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  979. continue;
  980. else if (ret < 0)
  981. return ret;
  982. attrcount++;
  983. }
  984. return attrcount;
  985. }
  986. static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
  987. struct iio_chan_spec const *chan,
  988. enum iio_shared_by shared_by,
  989. const long *infomask)
  990. {
  991. int i, ret, attrcount = 0;
  992. char *avail_postfix;
  993. for_each_set_bit(i, infomask, sizeof(infomask) * 8) {
  994. avail_postfix = kasprintf(GFP_KERNEL,
  995. "%s_available",
  996. iio_chan_info_postfix[i]);
  997. if (!avail_postfix)
  998. return -ENOMEM;
  999. ret = __iio_add_chan_devattr(avail_postfix,
  1000. chan,
  1001. &iio_read_channel_info_avail,
  1002. NULL,
  1003. i,
  1004. shared_by,
  1005. &indio_dev->dev,
  1006. &indio_dev->channel_attr_list);
  1007. kfree(avail_postfix);
  1008. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  1009. continue;
  1010. else if (ret < 0)
  1011. return ret;
  1012. attrcount++;
  1013. }
  1014. return attrcount;
  1015. }
  1016. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  1017. struct iio_chan_spec const *chan)
  1018. {
  1019. int ret, attrcount = 0;
  1020. const struct iio_chan_spec_ext_info *ext_info;
  1021. if (chan->channel < 0)
  1022. return 0;
  1023. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1024. IIO_SEPARATE,
  1025. &chan->info_mask_separate);
  1026. if (ret < 0)
  1027. return ret;
  1028. attrcount += ret;
  1029. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1030. IIO_SEPARATE,
  1031. &chan->
  1032. info_mask_separate_available);
  1033. if (ret < 0)
  1034. return ret;
  1035. attrcount += ret;
  1036. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1037. IIO_SHARED_BY_TYPE,
  1038. &chan->info_mask_shared_by_type);
  1039. if (ret < 0)
  1040. return ret;
  1041. attrcount += ret;
  1042. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1043. IIO_SHARED_BY_TYPE,
  1044. &chan->
  1045. info_mask_shared_by_type_available);
  1046. if (ret < 0)
  1047. return ret;
  1048. attrcount += ret;
  1049. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1050. IIO_SHARED_BY_DIR,
  1051. &chan->info_mask_shared_by_dir);
  1052. if (ret < 0)
  1053. return ret;
  1054. attrcount += ret;
  1055. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1056. IIO_SHARED_BY_DIR,
  1057. &chan->info_mask_shared_by_dir_available);
  1058. if (ret < 0)
  1059. return ret;
  1060. attrcount += ret;
  1061. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1062. IIO_SHARED_BY_ALL,
  1063. &chan->info_mask_shared_by_all);
  1064. if (ret < 0)
  1065. return ret;
  1066. attrcount += ret;
  1067. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1068. IIO_SHARED_BY_ALL,
  1069. &chan->info_mask_shared_by_all_available);
  1070. if (ret < 0)
  1071. return ret;
  1072. attrcount += ret;
  1073. if (chan->ext_info) {
  1074. unsigned int i = 0;
  1075. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  1076. ret = __iio_add_chan_devattr(ext_info->name,
  1077. chan,
  1078. ext_info->read ?
  1079. &iio_read_channel_ext_info : NULL,
  1080. ext_info->write ?
  1081. &iio_write_channel_ext_info : NULL,
  1082. i,
  1083. ext_info->shared,
  1084. &indio_dev->dev,
  1085. &indio_dev->channel_attr_list);
  1086. i++;
  1087. if (ret == -EBUSY && ext_info->shared)
  1088. continue;
  1089. if (ret)
  1090. return ret;
  1091. attrcount++;
  1092. }
  1093. }
  1094. return attrcount;
  1095. }
  1096. /**
  1097. * iio_free_chan_devattr_list() - Free a list of IIO device attributes
  1098. * @attr_list: List of IIO device attributes
  1099. *
  1100. * This function frees the memory allocated for each of the IIO device
  1101. * attributes in the list.
  1102. */
  1103. void iio_free_chan_devattr_list(struct list_head *attr_list)
  1104. {
  1105. struct iio_dev_attr *p, *n;
  1106. list_for_each_entry_safe(p, n, attr_list, l) {
  1107. kfree(p->dev_attr.attr.name);
  1108. list_del(&p->l);
  1109. kfree(p);
  1110. }
  1111. }
  1112. static ssize_t iio_show_dev_name(struct device *dev,
  1113. struct device_attribute *attr,
  1114. char *buf)
  1115. {
  1116. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1117. return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
  1118. }
  1119. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  1120. static ssize_t iio_show_timestamp_clock(struct device *dev,
  1121. struct device_attribute *attr,
  1122. char *buf)
  1123. {
  1124. const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1125. const clockid_t clk = iio_device_get_clock(indio_dev);
  1126. const char *name;
  1127. ssize_t sz;
  1128. switch (clk) {
  1129. case CLOCK_REALTIME:
  1130. name = "realtime\n";
  1131. sz = sizeof("realtime\n");
  1132. break;
  1133. case CLOCK_MONOTONIC:
  1134. name = "monotonic\n";
  1135. sz = sizeof("monotonic\n");
  1136. break;
  1137. case CLOCK_MONOTONIC_RAW:
  1138. name = "monotonic_raw\n";
  1139. sz = sizeof("monotonic_raw\n");
  1140. break;
  1141. case CLOCK_REALTIME_COARSE:
  1142. name = "realtime_coarse\n";
  1143. sz = sizeof("realtime_coarse\n");
  1144. break;
  1145. case CLOCK_MONOTONIC_COARSE:
  1146. name = "monotonic_coarse\n";
  1147. sz = sizeof("monotonic_coarse\n");
  1148. break;
  1149. case CLOCK_BOOTTIME:
  1150. name = "boottime\n";
  1151. sz = sizeof("boottime\n");
  1152. break;
  1153. case CLOCK_TAI:
  1154. name = "tai\n";
  1155. sz = sizeof("tai\n");
  1156. break;
  1157. default:
  1158. BUG();
  1159. }
  1160. memcpy(buf, name, sz);
  1161. return sz;
  1162. }
  1163. static ssize_t iio_store_timestamp_clock(struct device *dev,
  1164. struct device_attribute *attr,
  1165. const char *buf, size_t len)
  1166. {
  1167. clockid_t clk;
  1168. int ret;
  1169. if (sysfs_streq(buf, "realtime"))
  1170. clk = CLOCK_REALTIME;
  1171. else if (sysfs_streq(buf, "monotonic"))
  1172. clk = CLOCK_MONOTONIC;
  1173. else if (sysfs_streq(buf, "monotonic_raw"))
  1174. clk = CLOCK_MONOTONIC_RAW;
  1175. else if (sysfs_streq(buf, "realtime_coarse"))
  1176. clk = CLOCK_REALTIME_COARSE;
  1177. else if (sysfs_streq(buf, "monotonic_coarse"))
  1178. clk = CLOCK_MONOTONIC_COARSE;
  1179. else if (sysfs_streq(buf, "boottime"))
  1180. clk = CLOCK_BOOTTIME;
  1181. else if (sysfs_streq(buf, "tai"))
  1182. clk = CLOCK_TAI;
  1183. else
  1184. return -EINVAL;
  1185. ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
  1186. if (ret)
  1187. return ret;
  1188. return len;
  1189. }
  1190. static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
  1191. iio_show_timestamp_clock, iio_store_timestamp_clock);
  1192. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  1193. {
  1194. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  1195. struct iio_dev_attr *p;
  1196. struct attribute **attr, *clk = NULL;
  1197. /* First count elements in any existing group */
  1198. if (indio_dev->info->attrs) {
  1199. attr = indio_dev->info->attrs->attrs;
  1200. while (*attr++ != NULL)
  1201. attrcount_orig++;
  1202. }
  1203. attrcount = attrcount_orig;
  1204. /*
  1205. * New channel registration method - relies on the fact a group does
  1206. * not need to be initialized if its name is NULL.
  1207. */
  1208. if (indio_dev->channels)
  1209. for (i = 0; i < indio_dev->num_channels; i++) {
  1210. const struct iio_chan_spec *chan =
  1211. &indio_dev->channels[i];
  1212. if (chan->type == IIO_TIMESTAMP)
  1213. clk = &dev_attr_current_timestamp_clock.attr;
  1214. ret = iio_device_add_channel_sysfs(indio_dev, chan);
  1215. if (ret < 0)
  1216. goto error_clear_attrs;
  1217. attrcount += ret;
  1218. }
  1219. if (indio_dev->event_interface)
  1220. clk = &dev_attr_current_timestamp_clock.attr;
  1221. if (indio_dev->name)
  1222. attrcount++;
  1223. if (clk)
  1224. attrcount++;
  1225. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  1226. sizeof(indio_dev->chan_attr_group.attrs[0]),
  1227. GFP_KERNEL);
  1228. if (indio_dev->chan_attr_group.attrs == NULL) {
  1229. ret = -ENOMEM;
  1230. goto error_clear_attrs;
  1231. }
  1232. /* Copy across original attributes */
  1233. if (indio_dev->info->attrs)
  1234. memcpy(indio_dev->chan_attr_group.attrs,
  1235. indio_dev->info->attrs->attrs,
  1236. sizeof(indio_dev->chan_attr_group.attrs[0])
  1237. *attrcount_orig);
  1238. attrn = attrcount_orig;
  1239. /* Add all elements from the list. */
  1240. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  1241. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  1242. if (indio_dev->name)
  1243. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  1244. if (clk)
  1245. indio_dev->chan_attr_group.attrs[attrn++] = clk;
  1246. indio_dev->groups[indio_dev->groupcounter++] =
  1247. &indio_dev->chan_attr_group;
  1248. return 0;
  1249. error_clear_attrs:
  1250. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  1251. return ret;
  1252. }
  1253. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  1254. {
  1255. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  1256. kfree(indio_dev->chan_attr_group.attrs);
  1257. indio_dev->chan_attr_group.attrs = NULL;
  1258. }
  1259. static void iio_dev_release(struct device *device)
  1260. {
  1261. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  1262. if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
  1263. iio_device_unregister_trigger_consumer(indio_dev);
  1264. iio_device_unregister_eventset(indio_dev);
  1265. iio_device_unregister_sysfs(indio_dev);
  1266. iio_buffer_put(indio_dev->buffer);
  1267. ida_simple_remove(&iio_ida, indio_dev->id);
  1268. kfree(indio_dev);
  1269. }
  1270. struct device_type iio_device_type = {
  1271. .name = "iio_device",
  1272. .release = iio_dev_release,
  1273. };
  1274. /**
  1275. * iio_device_alloc() - allocate an iio_dev from a driver
  1276. * @sizeof_priv: Space to allocate for private structure.
  1277. **/
  1278. struct iio_dev *iio_device_alloc(int sizeof_priv)
  1279. {
  1280. struct iio_dev *dev;
  1281. size_t alloc_size;
  1282. alloc_size = sizeof(struct iio_dev);
  1283. if (sizeof_priv) {
  1284. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  1285. alloc_size += sizeof_priv;
  1286. }
  1287. /* ensure 32-byte alignment of whole construct ? */
  1288. alloc_size += IIO_ALIGN - 1;
  1289. dev = kzalloc(alloc_size, GFP_KERNEL);
  1290. if (dev) {
  1291. dev->dev.groups = dev->groups;
  1292. dev->dev.type = &iio_device_type;
  1293. dev->dev.bus = &iio_bus_type;
  1294. device_initialize(&dev->dev);
  1295. dev_set_drvdata(&dev->dev, (void *)dev);
  1296. mutex_init(&dev->mlock);
  1297. mutex_init(&dev->info_exist_lock);
  1298. INIT_LIST_HEAD(&dev->channel_attr_list);
  1299. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  1300. if (dev->id < 0) {
  1301. /* cannot use a dev_err as the name isn't available */
  1302. pr_err("failed to get device id\n");
  1303. kfree(dev);
  1304. return NULL;
  1305. }
  1306. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  1307. INIT_LIST_HEAD(&dev->buffer_list);
  1308. }
  1309. return dev;
  1310. }
  1311. EXPORT_SYMBOL(iio_device_alloc);
  1312. /**
  1313. * iio_device_free() - free an iio_dev from a driver
  1314. * @dev: the iio_dev associated with the device
  1315. **/
  1316. void iio_device_free(struct iio_dev *dev)
  1317. {
  1318. if (dev)
  1319. put_device(&dev->dev);
  1320. }
  1321. EXPORT_SYMBOL(iio_device_free);
  1322. static void devm_iio_device_release(struct device *dev, void *res)
  1323. {
  1324. iio_device_free(*(struct iio_dev **)res);
  1325. }
  1326. int devm_iio_device_match(struct device *dev, void *res, void *data)
  1327. {
  1328. struct iio_dev **r = res;
  1329. if (!r || !*r) {
  1330. WARN_ON(!r || !*r);
  1331. return 0;
  1332. }
  1333. return *r == data;
  1334. }
  1335. EXPORT_SYMBOL_GPL(devm_iio_device_match);
  1336. /**
  1337. * devm_iio_device_alloc - Resource-managed iio_device_alloc()
  1338. * @dev: Device to allocate iio_dev for
  1339. * @sizeof_priv: Space to allocate for private structure.
  1340. *
  1341. * Managed iio_device_alloc. iio_dev allocated with this function is
  1342. * automatically freed on driver detach.
  1343. *
  1344. * If an iio_dev allocated with this function needs to be freed separately,
  1345. * devm_iio_device_free() must be used.
  1346. *
  1347. * RETURNS:
  1348. * Pointer to allocated iio_dev on success, NULL on failure.
  1349. */
  1350. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  1351. {
  1352. struct iio_dev **ptr, *iio_dev;
  1353. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  1354. GFP_KERNEL);
  1355. if (!ptr)
  1356. return NULL;
  1357. iio_dev = iio_device_alloc(sizeof_priv);
  1358. if (iio_dev) {
  1359. *ptr = iio_dev;
  1360. devres_add(dev, ptr);
  1361. } else {
  1362. devres_free(ptr);
  1363. }
  1364. return iio_dev;
  1365. }
  1366. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  1367. /**
  1368. * devm_iio_device_free - Resource-managed iio_device_free()
  1369. * @dev: Device this iio_dev belongs to
  1370. * @iio_dev: the iio_dev associated with the device
  1371. *
  1372. * Free iio_dev allocated with devm_iio_device_alloc().
  1373. */
  1374. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  1375. {
  1376. int rc;
  1377. rc = devres_release(dev, devm_iio_device_release,
  1378. devm_iio_device_match, iio_dev);
  1379. WARN_ON(rc);
  1380. }
  1381. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  1382. /**
  1383. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  1384. * @inode: Inode structure for identifying the device in the file system
  1385. * @filp: File structure for iio device used to keep and later access
  1386. * private data
  1387. *
  1388. * Return: 0 on success or -EBUSY if the device is already opened
  1389. **/
  1390. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  1391. {
  1392. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  1393. struct iio_dev, chrdev);
  1394. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  1395. return -EBUSY;
  1396. iio_device_get(indio_dev);
  1397. filp->private_data = indio_dev;
  1398. return 0;
  1399. }
  1400. /**
  1401. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  1402. * @inode: Inode structure pointer for the char device
  1403. * @filp: File structure pointer for the char device
  1404. *
  1405. * Return: 0 for successful release
  1406. */
  1407. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  1408. {
  1409. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  1410. struct iio_dev, chrdev);
  1411. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  1412. iio_device_put(indio_dev);
  1413. return 0;
  1414. }
  1415. /* Somewhat of a cross file organization violation - ioctls here are actually
  1416. * event related */
  1417. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1418. {
  1419. struct iio_dev *indio_dev = filp->private_data;
  1420. int __user *ip = (int __user *)arg;
  1421. int fd;
  1422. if (!indio_dev->info)
  1423. return -ENODEV;
  1424. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  1425. fd = iio_event_getfd(indio_dev);
  1426. if (fd < 0)
  1427. return fd;
  1428. if (copy_to_user(ip, &fd, sizeof(fd)))
  1429. return -EFAULT;
  1430. return 0;
  1431. }
  1432. return -EINVAL;
  1433. }
  1434. static const struct file_operations iio_buffer_fileops = {
  1435. .read = iio_buffer_read_first_n_outer_addr,
  1436. .release = iio_chrdev_release,
  1437. .open = iio_chrdev_open,
  1438. .poll = iio_buffer_poll_addr,
  1439. .owner = THIS_MODULE,
  1440. .llseek = noop_llseek,
  1441. .unlocked_ioctl = iio_ioctl,
  1442. .compat_ioctl = iio_ioctl,
  1443. };
  1444. static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
  1445. {
  1446. int i, j;
  1447. const struct iio_chan_spec *channels = indio_dev->channels;
  1448. if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
  1449. return 0;
  1450. for (i = 0; i < indio_dev->num_channels - 1; i++) {
  1451. if (channels[i].scan_index < 0)
  1452. continue;
  1453. for (j = i + 1; j < indio_dev->num_channels; j++)
  1454. if (channels[i].scan_index == channels[j].scan_index) {
  1455. dev_err(&indio_dev->dev,
  1456. "Duplicate scan index %d\n",
  1457. channels[i].scan_index);
  1458. return -EINVAL;
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  1464. /**
  1465. * iio_device_register() - register a device with the IIO subsystem
  1466. * @indio_dev: Device structure filled by the device driver
  1467. **/
  1468. int iio_device_register(struct iio_dev *indio_dev)
  1469. {
  1470. int ret;
  1471. /* If the calling driver did not initialize of_node, do it here */
  1472. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  1473. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  1474. ret = iio_check_unique_scan_index(indio_dev);
  1475. if (ret < 0)
  1476. return ret;
  1477. /* configure elements for the chrdev */
  1478. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  1479. ret = iio_device_register_debugfs(indio_dev);
  1480. if (ret) {
  1481. dev_err(indio_dev->dev.parent,
  1482. "Failed to register debugfs interfaces\n");
  1483. return ret;
  1484. }
  1485. ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
  1486. if (ret) {
  1487. dev_err(indio_dev->dev.parent,
  1488. "Failed to create buffer sysfs interfaces\n");
  1489. goto error_unreg_debugfs;
  1490. }
  1491. ret = iio_device_register_sysfs(indio_dev);
  1492. if (ret) {
  1493. dev_err(indio_dev->dev.parent,
  1494. "Failed to register sysfs interfaces\n");
  1495. goto error_buffer_free_sysfs;
  1496. }
  1497. ret = iio_device_register_eventset(indio_dev);
  1498. if (ret) {
  1499. dev_err(indio_dev->dev.parent,
  1500. "Failed to register event set\n");
  1501. goto error_free_sysfs;
  1502. }
  1503. if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
  1504. iio_device_register_trigger_consumer(indio_dev);
  1505. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  1506. indio_dev->setup_ops == NULL)
  1507. indio_dev->setup_ops = &noop_ring_setup_ops;
  1508. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  1509. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  1510. ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev);
  1511. if (ret < 0)
  1512. goto error_unreg_eventset;
  1513. return 0;
  1514. error_unreg_eventset:
  1515. iio_device_unregister_eventset(indio_dev);
  1516. error_free_sysfs:
  1517. iio_device_unregister_sysfs(indio_dev);
  1518. error_buffer_free_sysfs:
  1519. iio_buffer_free_sysfs_and_mask(indio_dev);
  1520. error_unreg_debugfs:
  1521. iio_device_unregister_debugfs(indio_dev);
  1522. return ret;
  1523. }
  1524. EXPORT_SYMBOL(iio_device_register);
  1525. /**
  1526. * iio_device_unregister() - unregister a device from the IIO subsystem
  1527. * @indio_dev: Device structure representing the device.
  1528. **/
  1529. void iio_device_unregister(struct iio_dev *indio_dev)
  1530. {
  1531. mutex_lock(&indio_dev->info_exist_lock);
  1532. cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
  1533. iio_device_unregister_debugfs(indio_dev);
  1534. iio_disable_all_buffers(indio_dev);
  1535. indio_dev->info = NULL;
  1536. iio_device_wakeup_eventset(indio_dev);
  1537. iio_buffer_wakeup_poll(indio_dev);
  1538. mutex_unlock(&indio_dev->info_exist_lock);
  1539. iio_buffer_free_sysfs_and_mask(indio_dev);
  1540. }
  1541. EXPORT_SYMBOL(iio_device_unregister);
  1542. static void devm_iio_device_unreg(struct device *dev, void *res)
  1543. {
  1544. iio_device_unregister(*(struct iio_dev **)res);
  1545. }
  1546. /**
  1547. * devm_iio_device_register - Resource-managed iio_device_register()
  1548. * @dev: Device to allocate iio_dev for
  1549. * @indio_dev: Device structure filled by the device driver
  1550. *
  1551. * Managed iio_device_register. The IIO device registered with this
  1552. * function is automatically unregistered on driver detach. This function
  1553. * calls iio_device_register() internally. Refer to that function for more
  1554. * information.
  1555. *
  1556. * If an iio_dev registered with this function needs to be unregistered
  1557. * separately, devm_iio_device_unregister() must be used.
  1558. *
  1559. * RETURNS:
  1560. * 0 on success, negative error number on failure.
  1561. */
  1562. int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
  1563. {
  1564. struct iio_dev **ptr;
  1565. int ret;
  1566. ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
  1567. if (!ptr)
  1568. return -ENOMEM;
  1569. *ptr = indio_dev;
  1570. ret = iio_device_register(indio_dev);
  1571. if (!ret)
  1572. devres_add(dev, ptr);
  1573. else
  1574. devres_free(ptr);
  1575. return ret;
  1576. }
  1577. EXPORT_SYMBOL_GPL(devm_iio_device_register);
  1578. /**
  1579. * devm_iio_device_unregister - Resource-managed iio_device_unregister()
  1580. * @dev: Device this iio_dev belongs to
  1581. * @indio_dev: the iio_dev associated with the device
  1582. *
  1583. * Unregister iio_dev registered with devm_iio_device_register().
  1584. */
  1585. void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
  1586. {
  1587. int rc;
  1588. rc = devres_release(dev, devm_iio_device_unreg,
  1589. devm_iio_device_match, indio_dev);
  1590. WARN_ON(rc);
  1591. }
  1592. EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
  1593. /**
  1594. * iio_device_claim_direct_mode - Keep device in direct mode
  1595. * @indio_dev: the iio_dev associated with the device
  1596. *
  1597. * If the device is in direct mode it is guaranteed to stay
  1598. * that way until iio_device_release_direct_mode() is called.
  1599. *
  1600. * Use with iio_device_release_direct_mode()
  1601. *
  1602. * Returns: 0 on success, -EBUSY on failure
  1603. */
  1604. int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
  1605. {
  1606. mutex_lock(&indio_dev->mlock);
  1607. if (iio_buffer_enabled(indio_dev)) {
  1608. mutex_unlock(&indio_dev->mlock);
  1609. return -EBUSY;
  1610. }
  1611. return 0;
  1612. }
  1613. EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
  1614. /**
  1615. * iio_device_release_direct_mode - releases claim on direct mode
  1616. * @indio_dev: the iio_dev associated with the device
  1617. *
  1618. * Release the claim. Device is no longer guaranteed to stay
  1619. * in direct mode.
  1620. *
  1621. * Use with iio_device_claim_direct_mode()
  1622. */
  1623. void iio_device_release_direct_mode(struct iio_dev *indio_dev)
  1624. {
  1625. mutex_unlock(&indio_dev->mlock);
  1626. }
  1627. EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
  1628. subsys_initcall(iio_init);
  1629. module_exit(iio_exit);
  1630. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1631. MODULE_DESCRIPTION("Industrial I/O core");
  1632. MODULE_LICENSE("GPL");