industrialio-core.c 42 KB

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