industrialio-core.c 46 KB

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