industrialio-core.c 45 KB

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