industrialio-core.c 45 KB

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