industrialio-core.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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 = (s64)vals[0] * 1000000000LL >> vals[1];
  535. tmp1 = do_div(tmp, 1000000000LL);
  536. tmp0 = tmp;
  537. return snprintf(buf, len, "%d.%09u", tmp0, tmp1);
  538. case IIO_VAL_INT_MULTIPLE:
  539. {
  540. int i;
  541. int l = 0;
  542. for (i = 0; i < size; ++i) {
  543. l += snprintf(&buf[l], len - l, "%d ", vals[i]);
  544. if (l >= len)
  545. break;
  546. }
  547. return l;
  548. }
  549. default:
  550. return 0;
  551. }
  552. }
  553. /**
  554. * iio_format_value() - Formats a IIO value into its string representation
  555. * @buf: The buffer to which the formatted value gets written
  556. * which is assumed to be big enough (i.e. PAGE_SIZE).
  557. * @type: One of the IIO_VAL_... constants. This decides how the val
  558. * and val2 parameters are formatted.
  559. * @size: Number of IIO value entries contained in vals
  560. * @vals: Pointer to the values, exact meaning depends on the
  561. * type parameter.
  562. *
  563. * Return: 0 by default, a negative number on failure or the
  564. * total number of characters written for a type that belongs
  565. * to the IIO_VAL_... constant.
  566. */
  567. ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
  568. {
  569. ssize_t len;
  570. len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
  571. if (len >= PAGE_SIZE - 1)
  572. return -EFBIG;
  573. return len + sprintf(buf + len, "\n");
  574. }
  575. EXPORT_SYMBOL_GPL(iio_format_value);
  576. static ssize_t iio_read_channel_info(struct device *dev,
  577. struct device_attribute *attr,
  578. char *buf)
  579. {
  580. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  581. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  582. int vals[INDIO_MAX_RAW_ELEMENTS];
  583. int ret;
  584. int val_len = 2;
  585. if (indio_dev->info->read_raw_multi)
  586. ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
  587. INDIO_MAX_RAW_ELEMENTS,
  588. vals, &val_len,
  589. this_attr->address);
  590. else
  591. ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
  592. &vals[0], &vals[1], this_attr->address);
  593. if (ret < 0)
  594. return ret;
  595. return iio_format_value(buf, ret, val_len, vals);
  596. }
  597. static ssize_t iio_format_avail_list(char *buf, const int *vals,
  598. int type, int length)
  599. {
  600. int i;
  601. ssize_t len = 0;
  602. switch (type) {
  603. case IIO_VAL_INT:
  604. for (i = 0; i < length; i++) {
  605. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  606. type, 1, &vals[i]);
  607. if (len >= PAGE_SIZE)
  608. return -EFBIG;
  609. if (i < length - 1)
  610. len += snprintf(buf + len, PAGE_SIZE - len,
  611. " ");
  612. else
  613. len += snprintf(buf + len, PAGE_SIZE - len,
  614. "\n");
  615. if (len >= PAGE_SIZE)
  616. return -EFBIG;
  617. }
  618. break;
  619. default:
  620. for (i = 0; i < length / 2; i++) {
  621. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  622. type, 2, &vals[i * 2]);
  623. if (len >= PAGE_SIZE)
  624. return -EFBIG;
  625. if (i < length / 2 - 1)
  626. len += snprintf(buf + len, PAGE_SIZE - len,
  627. " ");
  628. else
  629. len += snprintf(buf + len, PAGE_SIZE - len,
  630. "\n");
  631. if (len >= PAGE_SIZE)
  632. return -EFBIG;
  633. }
  634. }
  635. return len;
  636. }
  637. static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
  638. {
  639. int i;
  640. ssize_t len;
  641. len = snprintf(buf, PAGE_SIZE, "[");
  642. switch (type) {
  643. case IIO_VAL_INT:
  644. for (i = 0; i < 3; i++) {
  645. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  646. type, 1, &vals[i]);
  647. if (len >= PAGE_SIZE)
  648. return -EFBIG;
  649. if (i < 2)
  650. len += snprintf(buf + len, PAGE_SIZE - len,
  651. " ");
  652. else
  653. len += snprintf(buf + len, PAGE_SIZE - len,
  654. "]\n");
  655. if (len >= PAGE_SIZE)
  656. return -EFBIG;
  657. }
  658. break;
  659. default:
  660. for (i = 0; i < 3; i++) {
  661. len += __iio_format_value(buf + len, PAGE_SIZE - len,
  662. type, 2, &vals[i * 2]);
  663. if (len >= PAGE_SIZE)
  664. return -EFBIG;
  665. if (i < 2)
  666. len += snprintf(buf + len, PAGE_SIZE - len,
  667. " ");
  668. else
  669. len += snprintf(buf + len, PAGE_SIZE - len,
  670. "]\n");
  671. if (len >= PAGE_SIZE)
  672. return -EFBIG;
  673. }
  674. }
  675. return len;
  676. }
  677. static ssize_t iio_read_channel_info_avail(struct device *dev,
  678. struct device_attribute *attr,
  679. char *buf)
  680. {
  681. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  682. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  683. const int *vals;
  684. int ret;
  685. int length;
  686. int type;
  687. ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
  688. &vals, &type, &length,
  689. this_attr->address);
  690. if (ret < 0)
  691. return ret;
  692. switch (ret) {
  693. case IIO_AVAIL_LIST:
  694. return iio_format_avail_list(buf, vals, type, length);
  695. case IIO_AVAIL_RANGE:
  696. return iio_format_avail_range(buf, vals, type);
  697. default:
  698. return -EINVAL;
  699. }
  700. }
  701. /**
  702. * iio_str_to_fixpoint() - Parse a fixed-point number from a string
  703. * @str: The string to parse
  704. * @fract_mult: Multiplier for the first decimal place, should be a power of 10
  705. * @integer: The integer part of the number
  706. * @fract: The fractional part of the number
  707. *
  708. * Returns 0 on success, or a negative error code if the string could not be
  709. * parsed.
  710. */
  711. int iio_str_to_fixpoint(const char *str, int fract_mult,
  712. int *integer, int *fract)
  713. {
  714. int i = 0, f = 0;
  715. bool integer_part = true, negative = false;
  716. if (fract_mult == 0) {
  717. *fract = 0;
  718. return kstrtoint(str, 0, integer);
  719. }
  720. if (str[0] == '-') {
  721. negative = true;
  722. str++;
  723. } else if (str[0] == '+') {
  724. str++;
  725. }
  726. while (*str) {
  727. if ('0' <= *str && *str <= '9') {
  728. if (integer_part) {
  729. i = i * 10 + *str - '0';
  730. } else {
  731. f += fract_mult * (*str - '0');
  732. fract_mult /= 10;
  733. }
  734. } else if (*str == '\n') {
  735. if (*(str + 1) == '\0')
  736. break;
  737. else
  738. return -EINVAL;
  739. } else if (*str == '.' && integer_part) {
  740. integer_part = false;
  741. } else {
  742. return -EINVAL;
  743. }
  744. str++;
  745. }
  746. if (negative) {
  747. if (i)
  748. i = -i;
  749. else
  750. f = -f;
  751. }
  752. *integer = i;
  753. *fract = f;
  754. return 0;
  755. }
  756. EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
  757. static ssize_t iio_write_channel_info(struct device *dev,
  758. struct device_attribute *attr,
  759. const char *buf,
  760. size_t len)
  761. {
  762. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  763. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  764. int ret, fract_mult = 100000;
  765. int integer, fract;
  766. /* Assumes decimal - precision based on number of digits */
  767. if (!indio_dev->info->write_raw)
  768. return -EINVAL;
  769. if (indio_dev->info->write_raw_get_fmt)
  770. switch (indio_dev->info->write_raw_get_fmt(indio_dev,
  771. this_attr->c, this_attr->address)) {
  772. case IIO_VAL_INT:
  773. fract_mult = 0;
  774. break;
  775. case IIO_VAL_INT_PLUS_MICRO:
  776. fract_mult = 100000;
  777. break;
  778. case IIO_VAL_INT_PLUS_NANO:
  779. fract_mult = 100000000;
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
  785. if (ret)
  786. return ret;
  787. ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
  788. integer, fract, this_attr->address);
  789. if (ret)
  790. return ret;
  791. return len;
  792. }
  793. static
  794. int __iio_device_attr_init(struct device_attribute *dev_attr,
  795. const char *postfix,
  796. struct iio_chan_spec const *chan,
  797. ssize_t (*readfunc)(struct device *dev,
  798. struct device_attribute *attr,
  799. char *buf),
  800. ssize_t (*writefunc)(struct device *dev,
  801. struct device_attribute *attr,
  802. const char *buf,
  803. size_t len),
  804. enum iio_shared_by shared_by)
  805. {
  806. int ret = 0;
  807. char *name = NULL;
  808. char *full_postfix;
  809. sysfs_attr_init(&dev_attr->attr);
  810. /* Build up postfix of <extend_name>_<modifier>_postfix */
  811. if (chan->modified && (shared_by == IIO_SEPARATE)) {
  812. if (chan->extend_name)
  813. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  814. iio_modifier_names[chan
  815. ->channel2],
  816. chan->extend_name,
  817. postfix);
  818. else
  819. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  820. iio_modifier_names[chan
  821. ->channel2],
  822. postfix);
  823. } else {
  824. if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
  825. full_postfix = kstrdup(postfix, GFP_KERNEL);
  826. else
  827. full_postfix = kasprintf(GFP_KERNEL,
  828. "%s_%s",
  829. chan->extend_name,
  830. postfix);
  831. }
  832. if (full_postfix == NULL)
  833. return -ENOMEM;
  834. if (chan->differential) { /* Differential can not have modifier */
  835. switch (shared_by) {
  836. case IIO_SHARED_BY_ALL:
  837. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  838. break;
  839. case IIO_SHARED_BY_DIR:
  840. name = kasprintf(GFP_KERNEL, "%s_%s",
  841. iio_direction[chan->output],
  842. full_postfix);
  843. break;
  844. case IIO_SHARED_BY_TYPE:
  845. name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
  846. iio_direction[chan->output],
  847. iio_chan_type_name_spec[chan->type],
  848. iio_chan_type_name_spec[chan->type],
  849. full_postfix);
  850. break;
  851. case IIO_SEPARATE:
  852. if (!chan->indexed) {
  853. WARN(1, "Differential channels must be indexed\n");
  854. ret = -EINVAL;
  855. goto error_free_full_postfix;
  856. }
  857. name = kasprintf(GFP_KERNEL,
  858. "%s_%s%d-%s%d_%s",
  859. iio_direction[chan->output],
  860. iio_chan_type_name_spec[chan->type],
  861. chan->channel,
  862. iio_chan_type_name_spec[chan->type],
  863. chan->channel2,
  864. full_postfix);
  865. break;
  866. }
  867. } else { /* Single ended */
  868. switch (shared_by) {
  869. case IIO_SHARED_BY_ALL:
  870. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  871. break;
  872. case IIO_SHARED_BY_DIR:
  873. name = kasprintf(GFP_KERNEL, "%s_%s",
  874. iio_direction[chan->output],
  875. full_postfix);
  876. break;
  877. case IIO_SHARED_BY_TYPE:
  878. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  879. iio_direction[chan->output],
  880. iio_chan_type_name_spec[chan->type],
  881. full_postfix);
  882. break;
  883. case IIO_SEPARATE:
  884. if (chan->indexed)
  885. name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  886. iio_direction[chan->output],
  887. iio_chan_type_name_spec[chan->type],
  888. chan->channel,
  889. full_postfix);
  890. else
  891. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  892. iio_direction[chan->output],
  893. iio_chan_type_name_spec[chan->type],
  894. full_postfix);
  895. break;
  896. }
  897. }
  898. if (name == NULL) {
  899. ret = -ENOMEM;
  900. goto error_free_full_postfix;
  901. }
  902. dev_attr->attr.name = name;
  903. if (readfunc) {
  904. dev_attr->attr.mode |= S_IRUGO;
  905. dev_attr->show = readfunc;
  906. }
  907. if (writefunc) {
  908. dev_attr->attr.mode |= S_IWUSR;
  909. dev_attr->store = writefunc;
  910. }
  911. error_free_full_postfix:
  912. kfree(full_postfix);
  913. return ret;
  914. }
  915. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  916. {
  917. kfree(dev_attr->attr.name);
  918. }
  919. int __iio_add_chan_devattr(const char *postfix,
  920. struct iio_chan_spec const *chan,
  921. ssize_t (*readfunc)(struct device *dev,
  922. struct device_attribute *attr,
  923. char *buf),
  924. ssize_t (*writefunc)(struct device *dev,
  925. struct device_attribute *attr,
  926. const char *buf,
  927. size_t len),
  928. u64 mask,
  929. enum iio_shared_by shared_by,
  930. struct device *dev,
  931. struct list_head *attr_list)
  932. {
  933. int ret;
  934. struct iio_dev_attr *iio_attr, *t;
  935. iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
  936. if (iio_attr == NULL)
  937. return -ENOMEM;
  938. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  939. postfix, chan,
  940. readfunc, writefunc, shared_by);
  941. if (ret)
  942. goto error_iio_dev_attr_free;
  943. iio_attr->c = chan;
  944. iio_attr->address = mask;
  945. list_for_each_entry(t, attr_list, l)
  946. if (strcmp(t->dev_attr.attr.name,
  947. iio_attr->dev_attr.attr.name) == 0) {
  948. if (shared_by == IIO_SEPARATE)
  949. dev_err(dev, "tried to double register : %s\n",
  950. t->dev_attr.attr.name);
  951. ret = -EBUSY;
  952. goto error_device_attr_deinit;
  953. }
  954. list_add(&iio_attr->l, attr_list);
  955. return 0;
  956. error_device_attr_deinit:
  957. __iio_device_attr_deinit(&iio_attr->dev_attr);
  958. error_iio_dev_attr_free:
  959. kfree(iio_attr);
  960. return ret;
  961. }
  962. static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
  963. struct iio_chan_spec const *chan,
  964. enum iio_shared_by shared_by,
  965. const long *infomask)
  966. {
  967. int i, ret, attrcount = 0;
  968. for_each_set_bit(i, infomask, sizeof(infomask)*8) {
  969. if (i >= ARRAY_SIZE(iio_chan_info_postfix))
  970. return -EINVAL;
  971. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  972. chan,
  973. &iio_read_channel_info,
  974. &iio_write_channel_info,
  975. i,
  976. shared_by,
  977. &indio_dev->dev,
  978. &indio_dev->channel_attr_list);
  979. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  980. continue;
  981. else if (ret < 0)
  982. return ret;
  983. attrcount++;
  984. }
  985. return attrcount;
  986. }
  987. static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
  988. struct iio_chan_spec const *chan,
  989. enum iio_shared_by shared_by,
  990. const long *infomask)
  991. {
  992. int i, ret, attrcount = 0;
  993. char *avail_postfix;
  994. for_each_set_bit(i, infomask, sizeof(infomask) * 8) {
  995. avail_postfix = kasprintf(GFP_KERNEL,
  996. "%s_available",
  997. iio_chan_info_postfix[i]);
  998. if (!avail_postfix)
  999. return -ENOMEM;
  1000. ret = __iio_add_chan_devattr(avail_postfix,
  1001. chan,
  1002. &iio_read_channel_info_avail,
  1003. NULL,
  1004. i,
  1005. shared_by,
  1006. &indio_dev->dev,
  1007. &indio_dev->channel_attr_list);
  1008. kfree(avail_postfix);
  1009. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  1010. continue;
  1011. else if (ret < 0)
  1012. return ret;
  1013. attrcount++;
  1014. }
  1015. return attrcount;
  1016. }
  1017. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  1018. struct iio_chan_spec const *chan)
  1019. {
  1020. int ret, attrcount = 0;
  1021. const struct iio_chan_spec_ext_info *ext_info;
  1022. if (chan->channel < 0)
  1023. return 0;
  1024. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1025. IIO_SEPARATE,
  1026. &chan->info_mask_separate);
  1027. if (ret < 0)
  1028. return ret;
  1029. attrcount += ret;
  1030. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1031. IIO_SEPARATE,
  1032. &chan->
  1033. info_mask_separate_available);
  1034. if (ret < 0)
  1035. return ret;
  1036. attrcount += ret;
  1037. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1038. IIO_SHARED_BY_TYPE,
  1039. &chan->info_mask_shared_by_type);
  1040. if (ret < 0)
  1041. return ret;
  1042. attrcount += ret;
  1043. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1044. IIO_SHARED_BY_TYPE,
  1045. &chan->
  1046. info_mask_shared_by_type_available);
  1047. if (ret < 0)
  1048. return ret;
  1049. attrcount += ret;
  1050. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1051. IIO_SHARED_BY_DIR,
  1052. &chan->info_mask_shared_by_dir);
  1053. if (ret < 0)
  1054. return ret;
  1055. attrcount += ret;
  1056. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1057. IIO_SHARED_BY_DIR,
  1058. &chan->info_mask_shared_by_dir_available);
  1059. if (ret < 0)
  1060. return ret;
  1061. attrcount += ret;
  1062. ret = iio_device_add_info_mask_type(indio_dev, chan,
  1063. IIO_SHARED_BY_ALL,
  1064. &chan->info_mask_shared_by_all);
  1065. if (ret < 0)
  1066. return ret;
  1067. attrcount += ret;
  1068. ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
  1069. IIO_SHARED_BY_ALL,
  1070. &chan->info_mask_shared_by_all_available);
  1071. if (ret < 0)
  1072. return ret;
  1073. attrcount += ret;
  1074. if (chan->ext_info) {
  1075. unsigned int i = 0;
  1076. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  1077. ret = __iio_add_chan_devattr(ext_info->name,
  1078. chan,
  1079. ext_info->read ?
  1080. &iio_read_channel_ext_info : NULL,
  1081. ext_info->write ?
  1082. &iio_write_channel_ext_info : NULL,
  1083. i,
  1084. ext_info->shared,
  1085. &indio_dev->dev,
  1086. &indio_dev->channel_attr_list);
  1087. i++;
  1088. if (ret == -EBUSY && ext_info->shared)
  1089. continue;
  1090. if (ret)
  1091. return ret;
  1092. attrcount++;
  1093. }
  1094. }
  1095. return attrcount;
  1096. }
  1097. /**
  1098. * iio_free_chan_devattr_list() - Free a list of IIO device attributes
  1099. * @attr_list: List of IIO device attributes
  1100. *
  1101. * This function frees the memory allocated for each of the IIO device
  1102. * attributes in the list.
  1103. */
  1104. void iio_free_chan_devattr_list(struct list_head *attr_list)
  1105. {
  1106. struct iio_dev_attr *p, *n;
  1107. list_for_each_entry_safe(p, n, attr_list, l) {
  1108. kfree(p->dev_attr.attr.name);
  1109. list_del(&p->l);
  1110. kfree(p);
  1111. }
  1112. }
  1113. static ssize_t iio_show_dev_name(struct device *dev,
  1114. struct device_attribute *attr,
  1115. char *buf)
  1116. {
  1117. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1118. return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
  1119. }
  1120. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  1121. static ssize_t iio_show_timestamp_clock(struct device *dev,
  1122. struct device_attribute *attr,
  1123. char *buf)
  1124. {
  1125. const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  1126. const clockid_t clk = iio_device_get_clock(indio_dev);
  1127. const char *name;
  1128. ssize_t sz;
  1129. switch (clk) {
  1130. case CLOCK_REALTIME:
  1131. name = "realtime\n";
  1132. sz = sizeof("realtime\n");
  1133. break;
  1134. case CLOCK_MONOTONIC:
  1135. name = "monotonic\n";
  1136. sz = sizeof("monotonic\n");
  1137. break;
  1138. case CLOCK_MONOTONIC_RAW:
  1139. name = "monotonic_raw\n";
  1140. sz = sizeof("monotonic_raw\n");
  1141. break;
  1142. case CLOCK_REALTIME_COARSE:
  1143. name = "realtime_coarse\n";
  1144. sz = sizeof("realtime_coarse\n");
  1145. break;
  1146. case CLOCK_MONOTONIC_COARSE:
  1147. name = "monotonic_coarse\n";
  1148. sz = sizeof("monotonic_coarse\n");
  1149. break;
  1150. case CLOCK_BOOTTIME:
  1151. name = "boottime\n";
  1152. sz = sizeof("boottime\n");
  1153. break;
  1154. case CLOCK_TAI:
  1155. name = "tai\n";
  1156. sz = sizeof("tai\n");
  1157. break;
  1158. default:
  1159. BUG();
  1160. }
  1161. memcpy(buf, name, sz);
  1162. return sz;
  1163. }
  1164. static ssize_t iio_store_timestamp_clock(struct device *dev,
  1165. struct device_attribute *attr,
  1166. const char *buf, size_t len)
  1167. {
  1168. clockid_t clk;
  1169. int ret;
  1170. if (sysfs_streq(buf, "realtime"))
  1171. clk = CLOCK_REALTIME;
  1172. else if (sysfs_streq(buf, "monotonic"))
  1173. clk = CLOCK_MONOTONIC;
  1174. else if (sysfs_streq(buf, "monotonic_raw"))
  1175. clk = CLOCK_MONOTONIC_RAW;
  1176. else if (sysfs_streq(buf, "realtime_coarse"))
  1177. clk = CLOCK_REALTIME_COARSE;
  1178. else if (sysfs_streq(buf, "monotonic_coarse"))
  1179. clk = CLOCK_MONOTONIC_COARSE;
  1180. else if (sysfs_streq(buf, "boottime"))
  1181. clk = CLOCK_BOOTTIME;
  1182. else if (sysfs_streq(buf, "tai"))
  1183. clk = CLOCK_TAI;
  1184. else
  1185. return -EINVAL;
  1186. ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
  1187. if (ret)
  1188. return ret;
  1189. return len;
  1190. }
  1191. static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
  1192. iio_show_timestamp_clock, iio_store_timestamp_clock);
  1193. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  1194. {
  1195. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  1196. struct iio_dev_attr *p;
  1197. struct attribute **attr, *clk = NULL;
  1198. /* First count elements in any existing group */
  1199. if (indio_dev->info->attrs) {
  1200. attr = indio_dev->info->attrs->attrs;
  1201. while (*attr++ != NULL)
  1202. attrcount_orig++;
  1203. }
  1204. attrcount = attrcount_orig;
  1205. /*
  1206. * New channel registration method - relies on the fact a group does
  1207. * not need to be initialized if its name is NULL.
  1208. */
  1209. if (indio_dev->channels)
  1210. for (i = 0; i < indio_dev->num_channels; i++) {
  1211. const struct iio_chan_spec *chan =
  1212. &indio_dev->channels[i];
  1213. if (chan->type == IIO_TIMESTAMP)
  1214. clk = &dev_attr_current_timestamp_clock.attr;
  1215. ret = iio_device_add_channel_sysfs(indio_dev, chan);
  1216. if (ret < 0)
  1217. goto error_clear_attrs;
  1218. attrcount += ret;
  1219. }
  1220. if (indio_dev->event_interface)
  1221. clk = &dev_attr_current_timestamp_clock.attr;
  1222. if (indio_dev->name)
  1223. attrcount++;
  1224. if (clk)
  1225. attrcount++;
  1226. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  1227. sizeof(indio_dev->chan_attr_group.attrs[0]),
  1228. GFP_KERNEL);
  1229. if (indio_dev->chan_attr_group.attrs == NULL) {
  1230. ret = -ENOMEM;
  1231. goto error_clear_attrs;
  1232. }
  1233. /* Copy across original attributes */
  1234. if (indio_dev->info->attrs)
  1235. memcpy(indio_dev->chan_attr_group.attrs,
  1236. indio_dev->info->attrs->attrs,
  1237. sizeof(indio_dev->chan_attr_group.attrs[0])
  1238. *attrcount_orig);
  1239. attrn = attrcount_orig;
  1240. /* Add all elements from the list. */
  1241. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  1242. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  1243. if (indio_dev->name)
  1244. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  1245. if (clk)
  1246. indio_dev->chan_attr_group.attrs[attrn++] = clk;
  1247. indio_dev->groups[indio_dev->groupcounter++] =
  1248. &indio_dev->chan_attr_group;
  1249. return 0;
  1250. error_clear_attrs:
  1251. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  1252. return ret;
  1253. }
  1254. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  1255. {
  1256. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  1257. kfree(indio_dev->chan_attr_group.attrs);
  1258. indio_dev->chan_attr_group.attrs = NULL;
  1259. }
  1260. static void iio_dev_release(struct device *device)
  1261. {
  1262. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  1263. if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
  1264. iio_device_unregister_trigger_consumer(indio_dev);
  1265. iio_device_unregister_eventset(indio_dev);
  1266. iio_device_unregister_sysfs(indio_dev);
  1267. iio_buffer_put(indio_dev->buffer);
  1268. ida_simple_remove(&iio_ida, indio_dev->id);
  1269. kfree(indio_dev);
  1270. }
  1271. struct device_type iio_device_type = {
  1272. .name = "iio_device",
  1273. .release = iio_dev_release,
  1274. };
  1275. /**
  1276. * iio_device_alloc() - allocate an iio_dev from a driver
  1277. * @sizeof_priv: Space to allocate for private structure.
  1278. **/
  1279. struct iio_dev *iio_device_alloc(int sizeof_priv)
  1280. {
  1281. struct iio_dev *dev;
  1282. size_t alloc_size;
  1283. alloc_size = sizeof(struct iio_dev);
  1284. if (sizeof_priv) {
  1285. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  1286. alloc_size += sizeof_priv;
  1287. }
  1288. /* ensure 32-byte alignment of whole construct ? */
  1289. alloc_size += IIO_ALIGN - 1;
  1290. dev = kzalloc(alloc_size, GFP_KERNEL);
  1291. if (dev) {
  1292. dev->dev.groups = dev->groups;
  1293. dev->dev.type = &iio_device_type;
  1294. dev->dev.bus = &iio_bus_type;
  1295. device_initialize(&dev->dev);
  1296. dev_set_drvdata(&dev->dev, (void *)dev);
  1297. mutex_init(&dev->mlock);
  1298. mutex_init(&dev->info_exist_lock);
  1299. INIT_LIST_HEAD(&dev->channel_attr_list);
  1300. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  1301. if (dev->id < 0) {
  1302. /* cannot use a dev_err as the name isn't available */
  1303. pr_err("failed to get device id\n");
  1304. kfree(dev);
  1305. return NULL;
  1306. }
  1307. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  1308. INIT_LIST_HEAD(&dev->buffer_list);
  1309. }
  1310. return dev;
  1311. }
  1312. EXPORT_SYMBOL(iio_device_alloc);
  1313. /**
  1314. * iio_device_free() - free an iio_dev from a driver
  1315. * @dev: the iio_dev associated with the device
  1316. **/
  1317. void iio_device_free(struct iio_dev *dev)
  1318. {
  1319. if (dev)
  1320. put_device(&dev->dev);
  1321. }
  1322. EXPORT_SYMBOL(iio_device_free);
  1323. static void devm_iio_device_release(struct device *dev, void *res)
  1324. {
  1325. iio_device_free(*(struct iio_dev **)res);
  1326. }
  1327. int devm_iio_device_match(struct device *dev, void *res, void *data)
  1328. {
  1329. struct iio_dev **r = res;
  1330. if (!r || !*r) {
  1331. WARN_ON(!r || !*r);
  1332. return 0;
  1333. }
  1334. return *r == data;
  1335. }
  1336. EXPORT_SYMBOL_GPL(devm_iio_device_match);
  1337. /**
  1338. * devm_iio_device_alloc - Resource-managed iio_device_alloc()
  1339. * @dev: Device to allocate iio_dev for
  1340. * @sizeof_priv: Space to allocate for private structure.
  1341. *
  1342. * Managed iio_device_alloc. iio_dev allocated with this function is
  1343. * automatically freed on driver detach.
  1344. *
  1345. * If an iio_dev allocated with this function needs to be freed separately,
  1346. * devm_iio_device_free() must be used.
  1347. *
  1348. * RETURNS:
  1349. * Pointer to allocated iio_dev on success, NULL on failure.
  1350. */
  1351. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  1352. {
  1353. struct iio_dev **ptr, *iio_dev;
  1354. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  1355. GFP_KERNEL);
  1356. if (!ptr)
  1357. return NULL;
  1358. iio_dev = iio_device_alloc(sizeof_priv);
  1359. if (iio_dev) {
  1360. *ptr = iio_dev;
  1361. devres_add(dev, ptr);
  1362. } else {
  1363. devres_free(ptr);
  1364. }
  1365. return iio_dev;
  1366. }
  1367. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  1368. /**
  1369. * devm_iio_device_free - Resource-managed iio_device_free()
  1370. * @dev: Device this iio_dev belongs to
  1371. * @iio_dev: the iio_dev associated with the device
  1372. *
  1373. * Free iio_dev allocated with devm_iio_device_alloc().
  1374. */
  1375. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  1376. {
  1377. int rc;
  1378. rc = devres_release(dev, devm_iio_device_release,
  1379. devm_iio_device_match, iio_dev);
  1380. WARN_ON(rc);
  1381. }
  1382. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  1383. /**
  1384. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  1385. * @inode: Inode structure for identifying the device in the file system
  1386. * @filp: File structure for iio device used to keep and later access
  1387. * private data
  1388. *
  1389. * Return: 0 on success or -EBUSY if the device is already opened
  1390. **/
  1391. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  1392. {
  1393. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  1394. struct iio_dev, chrdev);
  1395. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  1396. return -EBUSY;
  1397. iio_device_get(indio_dev);
  1398. filp->private_data = indio_dev;
  1399. return 0;
  1400. }
  1401. /**
  1402. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  1403. * @inode: Inode structure pointer for the char device
  1404. * @filp: File structure pointer for the char device
  1405. *
  1406. * Return: 0 for successful release
  1407. */
  1408. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  1409. {
  1410. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  1411. struct iio_dev, chrdev);
  1412. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  1413. iio_device_put(indio_dev);
  1414. return 0;
  1415. }
  1416. /* Somewhat of a cross file organization violation - ioctls here are actually
  1417. * event related */
  1418. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1419. {
  1420. struct iio_dev *indio_dev = filp->private_data;
  1421. int __user *ip = (int __user *)arg;
  1422. int fd;
  1423. if (!indio_dev->info)
  1424. return -ENODEV;
  1425. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  1426. fd = iio_event_getfd(indio_dev);
  1427. if (fd < 0)
  1428. return fd;
  1429. if (copy_to_user(ip, &fd, sizeof(fd)))
  1430. return -EFAULT;
  1431. return 0;
  1432. }
  1433. return -EINVAL;
  1434. }
  1435. static const struct file_operations iio_buffer_fileops = {
  1436. .read = iio_buffer_read_first_n_outer_addr,
  1437. .release = iio_chrdev_release,
  1438. .open = iio_chrdev_open,
  1439. .poll = iio_buffer_poll_addr,
  1440. .owner = THIS_MODULE,
  1441. .llseek = noop_llseek,
  1442. .unlocked_ioctl = iio_ioctl,
  1443. .compat_ioctl = iio_ioctl,
  1444. };
  1445. static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
  1446. {
  1447. int i, j;
  1448. const struct iio_chan_spec *channels = indio_dev->channels;
  1449. if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
  1450. return 0;
  1451. for (i = 0; i < indio_dev->num_channels - 1; i++) {
  1452. if (channels[i].scan_index < 0)
  1453. continue;
  1454. for (j = i + 1; j < indio_dev->num_channels; j++)
  1455. if (channels[i].scan_index == channels[j].scan_index) {
  1456. dev_err(&indio_dev->dev,
  1457. "Duplicate scan index %d\n",
  1458. channels[i].scan_index);
  1459. return -EINVAL;
  1460. }
  1461. }
  1462. return 0;
  1463. }
  1464. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  1465. /**
  1466. * iio_device_register() - register a device with the IIO subsystem
  1467. * @indio_dev: Device structure filled by the device driver
  1468. **/
  1469. int iio_device_register(struct iio_dev *indio_dev)
  1470. {
  1471. int ret;
  1472. /* If the calling driver did not initialize of_node, do it here */
  1473. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  1474. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  1475. ret = iio_check_unique_scan_index(indio_dev);
  1476. if (ret < 0)
  1477. return ret;
  1478. /* configure elements for the chrdev */
  1479. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  1480. ret = iio_device_register_debugfs(indio_dev);
  1481. if (ret) {
  1482. dev_err(indio_dev->dev.parent,
  1483. "Failed to register debugfs interfaces\n");
  1484. return ret;
  1485. }
  1486. ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
  1487. if (ret) {
  1488. dev_err(indio_dev->dev.parent,
  1489. "Failed to create buffer sysfs interfaces\n");
  1490. goto error_unreg_debugfs;
  1491. }
  1492. ret = iio_device_register_sysfs(indio_dev);
  1493. if (ret) {
  1494. dev_err(indio_dev->dev.parent,
  1495. "Failed to register sysfs interfaces\n");
  1496. goto error_buffer_free_sysfs;
  1497. }
  1498. ret = iio_device_register_eventset(indio_dev);
  1499. if (ret) {
  1500. dev_err(indio_dev->dev.parent,
  1501. "Failed to register event set\n");
  1502. goto error_free_sysfs;
  1503. }
  1504. if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
  1505. iio_device_register_trigger_consumer(indio_dev);
  1506. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  1507. indio_dev->setup_ops == NULL)
  1508. indio_dev->setup_ops = &noop_ring_setup_ops;
  1509. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  1510. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  1511. indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
  1512. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  1513. if (ret < 0)
  1514. goto error_unreg_eventset;
  1515. ret = device_add(&indio_dev->dev);
  1516. if (ret < 0)
  1517. goto error_cdev_del;
  1518. return 0;
  1519. error_cdev_del:
  1520. cdev_del(&indio_dev->chrdev);
  1521. error_unreg_eventset:
  1522. iio_device_unregister_eventset(indio_dev);
  1523. error_free_sysfs:
  1524. iio_device_unregister_sysfs(indio_dev);
  1525. error_buffer_free_sysfs:
  1526. iio_buffer_free_sysfs_and_mask(indio_dev);
  1527. error_unreg_debugfs:
  1528. iio_device_unregister_debugfs(indio_dev);
  1529. return ret;
  1530. }
  1531. EXPORT_SYMBOL(iio_device_register);
  1532. /**
  1533. * iio_device_unregister() - unregister a device from the IIO subsystem
  1534. * @indio_dev: Device structure representing the device.
  1535. **/
  1536. void iio_device_unregister(struct iio_dev *indio_dev)
  1537. {
  1538. mutex_lock(&indio_dev->info_exist_lock);
  1539. device_del(&indio_dev->dev);
  1540. if (indio_dev->chrdev.dev)
  1541. cdev_del(&indio_dev->chrdev);
  1542. iio_device_unregister_debugfs(indio_dev);
  1543. iio_disable_all_buffers(indio_dev);
  1544. indio_dev->info = NULL;
  1545. iio_device_wakeup_eventset(indio_dev);
  1546. iio_buffer_wakeup_poll(indio_dev);
  1547. mutex_unlock(&indio_dev->info_exist_lock);
  1548. iio_buffer_free_sysfs_and_mask(indio_dev);
  1549. }
  1550. EXPORT_SYMBOL(iio_device_unregister);
  1551. static void devm_iio_device_unreg(struct device *dev, void *res)
  1552. {
  1553. iio_device_unregister(*(struct iio_dev **)res);
  1554. }
  1555. /**
  1556. * devm_iio_device_register - Resource-managed iio_device_register()
  1557. * @dev: Device to allocate iio_dev for
  1558. * @indio_dev: Device structure filled by the device driver
  1559. *
  1560. * Managed iio_device_register. The IIO device registered with this
  1561. * function is automatically unregistered on driver detach. This function
  1562. * calls iio_device_register() internally. Refer to that function for more
  1563. * information.
  1564. *
  1565. * If an iio_dev registered with this function needs to be unregistered
  1566. * separately, devm_iio_device_unregister() must be used.
  1567. *
  1568. * RETURNS:
  1569. * 0 on success, negative error number on failure.
  1570. */
  1571. int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
  1572. {
  1573. struct iio_dev **ptr;
  1574. int ret;
  1575. ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
  1576. if (!ptr)
  1577. return -ENOMEM;
  1578. *ptr = indio_dev;
  1579. ret = iio_device_register(indio_dev);
  1580. if (!ret)
  1581. devres_add(dev, ptr);
  1582. else
  1583. devres_free(ptr);
  1584. return ret;
  1585. }
  1586. EXPORT_SYMBOL_GPL(devm_iio_device_register);
  1587. /**
  1588. * devm_iio_device_unregister - Resource-managed iio_device_unregister()
  1589. * @dev: Device this iio_dev belongs to
  1590. * @indio_dev: the iio_dev associated with the device
  1591. *
  1592. * Unregister iio_dev registered with devm_iio_device_register().
  1593. */
  1594. void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
  1595. {
  1596. int rc;
  1597. rc = devres_release(dev, devm_iio_device_unreg,
  1598. devm_iio_device_match, indio_dev);
  1599. WARN_ON(rc);
  1600. }
  1601. EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
  1602. /**
  1603. * iio_device_claim_direct_mode - Keep device in direct mode
  1604. * @indio_dev: the iio_dev associated with the device
  1605. *
  1606. * If the device is in direct mode it is guaranteed to stay
  1607. * that way until iio_device_release_direct_mode() is called.
  1608. *
  1609. * Use with iio_device_release_direct_mode()
  1610. *
  1611. * Returns: 0 on success, -EBUSY on failure
  1612. */
  1613. int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
  1614. {
  1615. mutex_lock(&indio_dev->mlock);
  1616. if (iio_buffer_enabled(indio_dev)) {
  1617. mutex_unlock(&indio_dev->mlock);
  1618. return -EBUSY;
  1619. }
  1620. return 0;
  1621. }
  1622. EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
  1623. /**
  1624. * iio_device_release_direct_mode - releases claim on direct mode
  1625. * @indio_dev: the iio_dev associated with the device
  1626. *
  1627. * Release the claim. Device is no longer guaranteed to stay
  1628. * in direct mode.
  1629. *
  1630. * Use with iio_device_claim_direct_mode()
  1631. */
  1632. void iio_device_release_direct_mode(struct iio_dev *indio_dev)
  1633. {
  1634. mutex_unlock(&indio_dev->mlock);
  1635. }
  1636. EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
  1637. subsys_initcall(iio_init);
  1638. module_exit(iio_exit);
  1639. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1640. MODULE_DESCRIPTION("Industrial I/O core");
  1641. MODULE_LICENSE("GPL");