industrialio-core.c 46 KB

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