industrialio-core.c 42 KB

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