inkern.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* The industrial I/O core in kernel channel mapping
  3. *
  4. * Copyright (c) 2011 Jonathan Cameron
  5. */
  6. #include <linux/cleanup.h>
  7. #include <linux/err.h>
  8. #include <linux/export.h>
  9. #include <linux/minmax.h>
  10. #include <linux/mm.h>
  11. #include <linux/mutex.h>
  12. #include <linux/property.h>
  13. #include <linux/slab.h>
  14. #include <linux/units.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/iio-opaque.h>
  17. #include "iio_core.h"
  18. #include <linux/iio/machine.h>
  19. #include <linux/iio/driver.h>
  20. #include <linux/iio/consumer.h>
  21. struct iio_map_internal {
  22. struct iio_dev *indio_dev;
  23. const struct iio_map *map;
  24. struct list_head l;
  25. };
  26. static LIST_HEAD(iio_map_list);
  27. static DEFINE_MUTEX(iio_map_list_lock);
  28. static int iio_map_array_unregister_locked(struct iio_dev *indio_dev)
  29. {
  30. int ret = -ENODEV;
  31. struct iio_map_internal *mapi, *next;
  32. list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
  33. if (indio_dev == mapi->indio_dev) {
  34. list_del(&mapi->l);
  35. kfree(mapi);
  36. ret = 0;
  37. }
  38. }
  39. return ret;
  40. }
  41. int iio_map_array_register(struct iio_dev *indio_dev, const struct iio_map *maps)
  42. {
  43. struct iio_map_internal *mapi;
  44. int i = 0;
  45. int ret;
  46. if (!maps)
  47. return 0;
  48. guard(mutex)(&iio_map_list_lock);
  49. while (maps[i].consumer_dev_name) {
  50. mapi = kzalloc_obj(*mapi);
  51. if (!mapi) {
  52. ret = -ENOMEM;
  53. goto error_ret;
  54. }
  55. mapi->map = &maps[i];
  56. mapi->indio_dev = indio_dev;
  57. list_add_tail(&mapi->l, &iio_map_list);
  58. i++;
  59. }
  60. return 0;
  61. error_ret:
  62. iio_map_array_unregister_locked(indio_dev);
  63. return ret;
  64. }
  65. EXPORT_SYMBOL_GPL(iio_map_array_register);
  66. /*
  67. * Remove all map entries associated with the given iio device
  68. */
  69. int iio_map_array_unregister(struct iio_dev *indio_dev)
  70. {
  71. guard(mutex)(&iio_map_list_lock);
  72. return iio_map_array_unregister_locked(indio_dev);
  73. }
  74. EXPORT_SYMBOL_GPL(iio_map_array_unregister);
  75. static void iio_map_array_unregister_cb(void *indio_dev)
  76. {
  77. iio_map_array_unregister(indio_dev);
  78. }
  79. int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev,
  80. const struct iio_map *maps)
  81. {
  82. int ret;
  83. ret = iio_map_array_register(indio_dev, maps);
  84. if (ret)
  85. return ret;
  86. return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev);
  87. }
  88. EXPORT_SYMBOL_GPL(devm_iio_map_array_register);
  89. static const struct iio_chan_spec
  90. *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
  91. {
  92. int i;
  93. const struct iio_chan_spec *chan = NULL;
  94. for (i = 0; i < indio_dev->num_channels; i++)
  95. if (indio_dev->channels[i].datasheet_name &&
  96. strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
  97. chan = &indio_dev->channels[i];
  98. break;
  99. }
  100. return chan;
  101. }
  102. /**
  103. * __fwnode_iio_simple_xlate - translate iiospec to the IIO channel index
  104. * @indio_dev: pointer to the iio_dev structure
  105. * @iiospec: IIO specifier as found in the device tree
  106. *
  107. * This is simple translation function, suitable for the most 1:1 mapped
  108. * channels in IIO chips. This function performs only one sanity check:
  109. * whether IIO index is less than num_channels (that is specified in the
  110. * iio_dev).
  111. */
  112. static int __fwnode_iio_simple_xlate(struct iio_dev *indio_dev,
  113. const struct fwnode_reference_args *iiospec)
  114. {
  115. if (!iiospec->nargs)
  116. return 0;
  117. if (iiospec->args[0] >= indio_dev->num_channels) {
  118. dev_err(&indio_dev->dev, "invalid channel index %llu\n",
  119. iiospec->args[0]);
  120. return -EINVAL;
  121. }
  122. return iiospec->args[0];
  123. }
  124. static int __fwnode_iio_channel_get(struct iio_channel *channel,
  125. struct fwnode_handle *fwnode, int index)
  126. {
  127. struct fwnode_reference_args iiospec;
  128. struct device *idev;
  129. struct iio_dev *indio_dev;
  130. int err;
  131. err = fwnode_property_get_reference_args(fwnode, "io-channels",
  132. "#io-channel-cells", 0,
  133. index, &iiospec);
  134. if (err)
  135. return err;
  136. idev = bus_find_device_by_fwnode(&iio_bus_type, iiospec.fwnode);
  137. if (!idev) {
  138. fwnode_handle_put(iiospec.fwnode);
  139. return -EPROBE_DEFER;
  140. }
  141. indio_dev = dev_to_iio_dev(idev);
  142. channel->indio_dev = indio_dev;
  143. if (indio_dev->info->fwnode_xlate)
  144. index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
  145. else
  146. index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
  147. fwnode_handle_put(iiospec.fwnode);
  148. if (index < 0)
  149. goto err_put;
  150. channel->channel = &indio_dev->channels[index];
  151. return 0;
  152. err_put:
  153. iio_device_put(indio_dev);
  154. return index;
  155. }
  156. static struct iio_channel *fwnode_iio_channel_get(struct fwnode_handle *fwnode,
  157. int index)
  158. {
  159. int err;
  160. if (index < 0)
  161. return ERR_PTR(-EINVAL);
  162. struct iio_channel *channel __free(kfree) =
  163. kzalloc_obj(*channel);
  164. if (!channel)
  165. return ERR_PTR(-ENOMEM);
  166. err = __fwnode_iio_channel_get(channel, fwnode, index);
  167. if (err)
  168. return ERR_PTR(err);
  169. return_ptr(channel);
  170. }
  171. static struct iio_channel *
  172. __fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode, const char *name)
  173. {
  174. struct iio_channel *chan;
  175. int index = 0;
  176. /*
  177. * For named iio channels, first look up the name in the
  178. * "io-channel-names" property. If it cannot be found, the
  179. * index will be an error code, and fwnode_iio_channel_get()
  180. * will fail.
  181. */
  182. if (name)
  183. index = fwnode_property_match_string(fwnode, "io-channel-names",
  184. name);
  185. chan = fwnode_iio_channel_get(fwnode, index);
  186. if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
  187. return chan;
  188. if (name) {
  189. if (index >= 0) {
  190. pr_err("ERROR: could not get IIO channel %pfw:%s(%i)\n",
  191. fwnode, name, index);
  192. /*
  193. * In this case, we found 'name' in 'io-channel-names'
  194. * but somehow we still fail so that we should not proceed
  195. * with any other lookup. Hence, explicitly return -EINVAL
  196. * (maybe not the better error code) so that the caller
  197. * won't do a system lookup.
  198. */
  199. return ERR_PTR(-EINVAL);
  200. }
  201. /*
  202. * If index < 0, then fwnode_property_get_reference_args() fails
  203. * with -EINVAL or -ENOENT (ACPI case) which is expected. We
  204. * should not proceed if we get any other error.
  205. */
  206. if (PTR_ERR(chan) != -EINVAL && PTR_ERR(chan) != -ENOENT)
  207. return chan;
  208. } else if (PTR_ERR(chan) != -ENOENT) {
  209. /*
  210. * if !name, then we should only proceed the lookup if
  211. * fwnode_property_get_reference_args() returns -ENOENT.
  212. */
  213. return chan;
  214. }
  215. /* so we continue the lookup */
  216. return ERR_PTR(-ENODEV);
  217. }
  218. struct iio_channel *fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode,
  219. const char *name)
  220. {
  221. struct fwnode_handle *parent;
  222. struct iio_channel *chan;
  223. /* Walk up the tree of devices looking for a matching iio channel */
  224. chan = __fwnode_iio_channel_get_by_name(fwnode, name);
  225. if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV)
  226. return chan;
  227. /*
  228. * No matching IIO channel found on this node.
  229. * If the parent node has a "io-channel-ranges" property,
  230. * then we can try one of its channels.
  231. */
  232. fwnode_for_each_parent_node(fwnode, parent) {
  233. if (!fwnode_property_present(parent, "io-channel-ranges")) {
  234. fwnode_handle_put(parent);
  235. return ERR_PTR(-ENODEV);
  236. }
  237. chan = __fwnode_iio_channel_get_by_name(parent, name);
  238. if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV) {
  239. fwnode_handle_put(parent);
  240. return chan;
  241. }
  242. }
  243. return ERR_PTR(-ENODEV);
  244. }
  245. EXPORT_SYMBOL_GPL(fwnode_iio_channel_get_by_name);
  246. static struct iio_channel *fwnode_iio_channel_get_all(struct device *dev)
  247. {
  248. struct fwnode_handle *fwnode = dev_fwnode(dev);
  249. int i, mapind, nummaps = 0;
  250. int ret;
  251. do {
  252. ret = fwnode_property_get_reference_args(fwnode, "io-channels",
  253. "#io-channel-cells", 0,
  254. nummaps, NULL);
  255. if (ret < 0)
  256. break;
  257. } while (++nummaps);
  258. if (nummaps == 0)
  259. return ERR_PTR(-ENODEV);
  260. /* NULL terminated array to save passing size */
  261. struct iio_channel *chans __free(kfree) =
  262. kzalloc_objs(*chans, nummaps + 1);
  263. if (!chans)
  264. return ERR_PTR(-ENOMEM);
  265. /* Search for FW matches */
  266. for (mapind = 0; mapind < nummaps; mapind++) {
  267. ret = __fwnode_iio_channel_get(&chans[mapind], fwnode, mapind);
  268. if (ret)
  269. goto error_free_chans;
  270. }
  271. return_ptr(chans);
  272. error_free_chans:
  273. for (i = 0; i < mapind; i++)
  274. iio_device_put(chans[i].indio_dev);
  275. return ERR_PTR(ret);
  276. }
  277. static struct iio_channel *iio_channel_get_sys(const char *name,
  278. const char *channel_name)
  279. {
  280. struct iio_map_internal *c_i = NULL, *c = NULL;
  281. int err;
  282. if (!(name || channel_name))
  283. return ERR_PTR(-ENODEV);
  284. /* first find matching entry the channel map */
  285. scoped_guard(mutex, &iio_map_list_lock) {
  286. list_for_each_entry(c_i, &iio_map_list, l) {
  287. if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
  288. (channel_name &&
  289. strcmp(channel_name, c_i->map->consumer_channel) != 0))
  290. continue;
  291. c = c_i;
  292. iio_device_get(c->indio_dev);
  293. break;
  294. }
  295. }
  296. if (!c)
  297. return ERR_PTR(-ENODEV);
  298. struct iio_channel *channel __free(kfree) =
  299. kzalloc(sizeof(*channel), GFP_KERNEL);
  300. if (!channel) {
  301. err = -ENOMEM;
  302. goto error_no_mem;
  303. }
  304. channel->indio_dev = c->indio_dev;
  305. if (c->map->adc_channel_label) {
  306. channel->channel =
  307. iio_chan_spec_from_name(channel->indio_dev,
  308. c->map->adc_channel_label);
  309. if (!channel->channel) {
  310. err = -EINVAL;
  311. goto error_no_mem;
  312. }
  313. }
  314. return_ptr(channel);
  315. error_no_mem:
  316. iio_device_put(c->indio_dev);
  317. return ERR_PTR(err);
  318. }
  319. struct iio_channel *iio_channel_get(struct device *dev,
  320. const char *channel_name)
  321. {
  322. const char *name = dev ? dev_name(dev) : NULL;
  323. struct iio_channel *channel;
  324. if (dev) {
  325. channel = fwnode_iio_channel_get_by_name(dev_fwnode(dev),
  326. channel_name);
  327. if (!IS_ERR(channel) || PTR_ERR(channel) != -ENODEV)
  328. return channel;
  329. }
  330. return iio_channel_get_sys(name, channel_name);
  331. }
  332. EXPORT_SYMBOL_GPL(iio_channel_get);
  333. void iio_channel_release(struct iio_channel *channel)
  334. {
  335. if (!channel)
  336. return;
  337. iio_device_put(channel->indio_dev);
  338. kfree(channel);
  339. }
  340. EXPORT_SYMBOL_GPL(iio_channel_release);
  341. static void devm_iio_channel_free(void *iio_channel)
  342. {
  343. iio_channel_release(iio_channel);
  344. }
  345. struct iio_channel *devm_iio_channel_get(struct device *dev,
  346. const char *channel_name)
  347. {
  348. struct iio_channel *channel;
  349. int ret;
  350. channel = iio_channel_get(dev, channel_name);
  351. if (IS_ERR(channel))
  352. return channel;
  353. ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
  354. if (ret)
  355. return ERR_PTR(ret);
  356. return channel;
  357. }
  358. EXPORT_SYMBOL_GPL(devm_iio_channel_get);
  359. struct iio_channel *devm_fwnode_iio_channel_get_by_name(struct device *dev,
  360. struct fwnode_handle *fwnode,
  361. const char *channel_name)
  362. {
  363. struct iio_channel *channel;
  364. int ret;
  365. channel = fwnode_iio_channel_get_by_name(fwnode, channel_name);
  366. if (IS_ERR(channel))
  367. return channel;
  368. ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
  369. if (ret)
  370. return ERR_PTR(ret);
  371. return channel;
  372. }
  373. EXPORT_SYMBOL_GPL(devm_fwnode_iio_channel_get_by_name);
  374. struct iio_channel *iio_channel_get_all(struct device *dev)
  375. {
  376. const char *name;
  377. struct iio_map_internal *c = NULL;
  378. struct iio_channel *fw_chans;
  379. int nummaps = 0;
  380. int mapind = 0;
  381. int i, ret;
  382. if (!dev)
  383. return ERR_PTR(-EINVAL);
  384. fw_chans = fwnode_iio_channel_get_all(dev);
  385. /*
  386. * We only want to carry on if the error is -ENODEV. Anything else
  387. * should be reported up the stack.
  388. */
  389. if (!IS_ERR(fw_chans) || PTR_ERR(fw_chans) != -ENODEV)
  390. return fw_chans;
  391. name = dev_name(dev);
  392. guard(mutex)(&iio_map_list_lock);
  393. /* first count the matching maps */
  394. list_for_each_entry(c, &iio_map_list, l)
  395. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  396. continue;
  397. else
  398. nummaps++;
  399. if (nummaps == 0)
  400. return ERR_PTR(-ENODEV);
  401. /* NULL terminated array to save passing size */
  402. struct iio_channel *chans __free(kfree) =
  403. kzalloc_objs(*chans, nummaps + 1);
  404. if (!chans)
  405. return ERR_PTR(-ENOMEM);
  406. /* for each map fill in the chans element */
  407. list_for_each_entry(c, &iio_map_list, l) {
  408. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  409. continue;
  410. chans[mapind].indio_dev = c->indio_dev;
  411. chans[mapind].data = c->map->consumer_data;
  412. chans[mapind].channel =
  413. iio_chan_spec_from_name(chans[mapind].indio_dev,
  414. c->map->adc_channel_label);
  415. if (!chans[mapind].channel) {
  416. ret = -EINVAL;
  417. goto error_free_chans;
  418. }
  419. iio_device_get(chans[mapind].indio_dev);
  420. mapind++;
  421. }
  422. if (mapind == 0) {
  423. ret = -ENODEV;
  424. goto error_free_chans;
  425. }
  426. return_ptr(chans);
  427. error_free_chans:
  428. for (i = 0; i < mapind; i++)
  429. iio_device_put(chans[i].indio_dev);
  430. return ERR_PTR(ret);
  431. }
  432. EXPORT_SYMBOL_GPL(iio_channel_get_all);
  433. void iio_channel_release_all(struct iio_channel *channels)
  434. {
  435. struct iio_channel *chan = &channels[0];
  436. while (chan->indio_dev) {
  437. iio_device_put(chan->indio_dev);
  438. chan++;
  439. }
  440. kfree(channels);
  441. }
  442. EXPORT_SYMBOL_GPL(iio_channel_release_all);
  443. static void devm_iio_channel_free_all(void *iio_channels)
  444. {
  445. iio_channel_release_all(iio_channels);
  446. }
  447. struct iio_channel *devm_iio_channel_get_all(struct device *dev)
  448. {
  449. struct iio_channel *channels;
  450. int ret;
  451. channels = iio_channel_get_all(dev);
  452. if (IS_ERR(channels))
  453. return channels;
  454. ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
  455. channels);
  456. if (ret)
  457. return ERR_PTR(ret);
  458. return channels;
  459. }
  460. EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
  461. static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
  462. enum iio_chan_info_enum info)
  463. {
  464. const struct iio_info *iio_info = chan->indio_dev->info;
  465. int unused;
  466. int vals[INDIO_MAX_RAW_ELEMENTS];
  467. int ret;
  468. int val_len = 2;
  469. if (!val2)
  470. val2 = &unused;
  471. if (!iio_channel_has_info(chan->channel, info))
  472. return -EINVAL;
  473. if (iio_info->read_raw_multi) {
  474. ret = iio_info->read_raw_multi(chan->indio_dev,
  475. chan->channel,
  476. INDIO_MAX_RAW_ELEMENTS,
  477. vals, &val_len, info);
  478. *val = vals[0];
  479. *val2 = vals[1];
  480. } else if (iio_info->read_raw) {
  481. ret = iio_info->read_raw(chan->indio_dev,
  482. chan->channel, val, val2, info);
  483. } else {
  484. return -EINVAL;
  485. }
  486. return ret;
  487. }
  488. int iio_read_channel_raw(struct iio_channel *chan, int *val)
  489. {
  490. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  491. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  492. if (!chan->indio_dev->info)
  493. return -ENODEV;
  494. return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  495. }
  496. EXPORT_SYMBOL_GPL(iio_read_channel_raw);
  497. int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
  498. {
  499. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  500. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  501. if (!chan->indio_dev->info)
  502. return -ENODEV;
  503. return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
  504. }
  505. EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
  506. int iio_multiply_value(int *result, s64 multiplier,
  507. unsigned int type, int val, int val2)
  508. {
  509. s64 denominator;
  510. switch (type) {
  511. case IIO_VAL_INT:
  512. *result = multiplier * val;
  513. return IIO_VAL_INT;
  514. case IIO_VAL_INT_PLUS_MICRO:
  515. case IIO_VAL_INT_PLUS_NANO:
  516. switch (type) {
  517. case IIO_VAL_INT_PLUS_MICRO:
  518. denominator = MICRO;
  519. break;
  520. case IIO_VAL_INT_PLUS_NANO:
  521. denominator = NANO;
  522. break;
  523. }
  524. *result = multiplier * abs(val);
  525. *result += div_s64(multiplier * abs(val2), denominator);
  526. if (val < 0 || val2 < 0)
  527. *result *= -1;
  528. return IIO_VAL_INT;
  529. case IIO_VAL_FRACTIONAL:
  530. *result = div_s64(multiplier * val, val2);
  531. return IIO_VAL_INT;
  532. case IIO_VAL_FRACTIONAL_LOG2:
  533. *result = (multiplier * val) >> val2;
  534. return IIO_VAL_INT;
  535. default:
  536. return -EINVAL;
  537. }
  538. }
  539. EXPORT_SYMBOL_NS_GPL(iio_multiply_value, "IIO_UNIT_TEST");
  540. static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
  541. int raw, int *processed,
  542. unsigned int scale)
  543. {
  544. int scale_type, scale_val, scale_val2;
  545. int offset_type, offset_val, offset_val2;
  546. s64 raw64 = raw;
  547. int ret;
  548. offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
  549. IIO_CHAN_INFO_OFFSET);
  550. if (offset_type >= 0) {
  551. switch (offset_type) {
  552. case IIO_VAL_INT:
  553. break;
  554. case IIO_VAL_INT_PLUS_MICRO:
  555. case IIO_VAL_INT_PLUS_NANO:
  556. /*
  557. * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
  558. * implicitely truncate the offset to it's integer form.
  559. */
  560. break;
  561. case IIO_VAL_FRACTIONAL:
  562. offset_val /= offset_val2;
  563. break;
  564. case IIO_VAL_FRACTIONAL_LOG2:
  565. offset_val >>= offset_val2;
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. raw64 += offset_val;
  571. }
  572. scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
  573. IIO_CHAN_INFO_SCALE);
  574. if (scale_type < 0) {
  575. /*
  576. * If no channel scaling is available apply consumer scale to
  577. * raw value and return.
  578. */
  579. *processed = raw64 * scale;
  580. return 0;
  581. }
  582. ret = iio_multiply_value(processed, raw64 * scale,
  583. scale_type, scale_val, scale_val2);
  584. if (ret < 0)
  585. return ret;
  586. return 0;
  587. }
  588. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  589. int *processed, unsigned int scale)
  590. {
  591. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  592. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  593. if (!chan->indio_dev->info)
  594. return -ENODEV;
  595. return iio_convert_raw_to_processed_unlocked(chan, raw, processed,
  596. scale);
  597. }
  598. EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
  599. int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
  600. enum iio_chan_info_enum attribute)
  601. {
  602. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  603. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  604. if (!chan->indio_dev->info)
  605. return -ENODEV;
  606. return iio_channel_read(chan, val, val2, attribute);
  607. }
  608. EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
  609. int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
  610. {
  611. return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
  612. }
  613. EXPORT_SYMBOL_GPL(iio_read_channel_offset);
  614. int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
  615. unsigned int scale)
  616. {
  617. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  618. int ret, pval, pval2;
  619. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  620. if (!chan->indio_dev->info)
  621. return -ENODEV;
  622. if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
  623. ret = iio_channel_read(chan, &pval, &pval2,
  624. IIO_CHAN_INFO_PROCESSED);
  625. if (ret < 0)
  626. return ret;
  627. return iio_multiply_value(val, scale, ret, pval, pval2);
  628. } else {
  629. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  630. if (ret < 0)
  631. return ret;
  632. return iio_convert_raw_to_processed_unlocked(chan, *val, val,
  633. scale);
  634. }
  635. }
  636. EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
  637. int iio_read_channel_processed(struct iio_channel *chan, int *val)
  638. {
  639. /* This is just a special case with scale factor 1 */
  640. return iio_read_channel_processed_scale(chan, val, 1);
  641. }
  642. EXPORT_SYMBOL_GPL(iio_read_channel_processed);
  643. int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
  644. {
  645. return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
  646. }
  647. EXPORT_SYMBOL_GPL(iio_read_channel_scale);
  648. static int iio_channel_read_avail(struct iio_channel *chan,
  649. const int **vals, int *type, int *length,
  650. enum iio_chan_info_enum info)
  651. {
  652. const struct iio_info *iio_info = chan->indio_dev->info;
  653. if (!iio_channel_has_available(chan->channel, info))
  654. return -EINVAL;
  655. if (iio_info->read_avail)
  656. return iio_info->read_avail(chan->indio_dev, chan->channel,
  657. vals, type, length, info);
  658. return -EINVAL;
  659. }
  660. int iio_read_avail_channel_attribute(struct iio_channel *chan,
  661. const int **vals, int *type, int *length,
  662. enum iio_chan_info_enum attribute)
  663. {
  664. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  665. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  666. if (!chan->indio_dev->info)
  667. return -ENODEV;
  668. return iio_channel_read_avail(chan, vals, type, length, attribute);
  669. }
  670. EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
  671. int iio_read_avail_channel_raw(struct iio_channel *chan,
  672. const int **vals, int *length)
  673. {
  674. int ret;
  675. int type;
  676. ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
  677. IIO_CHAN_INFO_RAW);
  678. if (ret >= 0 && type != IIO_VAL_INT)
  679. /* raw values are assumed to be IIO_VAL_INT */
  680. ret = -EINVAL;
  681. return ret;
  682. }
  683. EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
  684. static int iio_channel_read_max(struct iio_channel *chan,
  685. int *val, int *val2, int *type,
  686. enum iio_chan_info_enum info)
  687. {
  688. const int *vals;
  689. int length;
  690. int ret;
  691. ret = iio_channel_read_avail(chan, &vals, type, &length, info);
  692. if (ret < 0)
  693. return ret;
  694. switch (ret) {
  695. case IIO_AVAIL_RANGE:
  696. switch (*type) {
  697. case IIO_VAL_INT:
  698. *val = vals[2];
  699. break;
  700. default:
  701. *val = vals[4];
  702. if (val2)
  703. *val2 = vals[5];
  704. }
  705. return 0;
  706. case IIO_AVAIL_LIST:
  707. if (length <= 0)
  708. return -EINVAL;
  709. switch (*type) {
  710. case IIO_VAL_INT:
  711. *val = max_array(vals, length);
  712. break;
  713. default:
  714. /* TODO: learn about max for other iio values */
  715. return -EINVAL;
  716. }
  717. return 0;
  718. default:
  719. return -EINVAL;
  720. }
  721. }
  722. int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
  723. {
  724. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  725. int type;
  726. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  727. if (!chan->indio_dev->info)
  728. return -ENODEV;
  729. return iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
  730. }
  731. EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
  732. static int iio_channel_read_min(struct iio_channel *chan,
  733. int *val, int *val2, int *type,
  734. enum iio_chan_info_enum info)
  735. {
  736. const int *vals;
  737. int length;
  738. int ret;
  739. ret = iio_channel_read_avail(chan, &vals, type, &length, info);
  740. if (ret < 0)
  741. return ret;
  742. switch (ret) {
  743. case IIO_AVAIL_RANGE:
  744. switch (*type) {
  745. case IIO_VAL_INT:
  746. *val = vals[0];
  747. break;
  748. default:
  749. *val = vals[0];
  750. if (val2)
  751. *val2 = vals[1];
  752. }
  753. return 0;
  754. case IIO_AVAIL_LIST:
  755. if (length <= 0)
  756. return -EINVAL;
  757. switch (*type) {
  758. case IIO_VAL_INT:
  759. *val = min_array(vals, length);
  760. break;
  761. default:
  762. /* TODO: learn about min for other iio values */
  763. return -EINVAL;
  764. }
  765. return 0;
  766. default:
  767. return -EINVAL;
  768. }
  769. }
  770. int iio_read_min_channel_raw(struct iio_channel *chan, int *val)
  771. {
  772. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  773. int type;
  774. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  775. if (!chan->indio_dev->info)
  776. return -ENODEV;
  777. return iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
  778. }
  779. EXPORT_SYMBOL_GPL(iio_read_min_channel_raw);
  780. int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
  781. {
  782. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  783. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  784. if (!chan->indio_dev->info)
  785. return -ENODEV;
  786. *type = chan->channel->type;
  787. return 0;
  788. }
  789. EXPORT_SYMBOL_GPL(iio_get_channel_type);
  790. static int iio_channel_write(struct iio_channel *chan, int val, int val2,
  791. enum iio_chan_info_enum info)
  792. {
  793. const struct iio_info *iio_info = chan->indio_dev->info;
  794. if (iio_info->write_raw)
  795. return iio_info->write_raw(chan->indio_dev,
  796. chan->channel, val, val2, info);
  797. return -EINVAL;
  798. }
  799. int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
  800. enum iio_chan_info_enum attribute)
  801. {
  802. struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
  803. guard(mutex)(&iio_dev_opaque->info_exist_lock);
  804. if (!chan->indio_dev->info)
  805. return -ENODEV;
  806. return iio_channel_write(chan, val, val2, attribute);
  807. }
  808. EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
  809. int iio_write_channel_raw(struct iio_channel *chan, int val)
  810. {
  811. return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
  812. }
  813. EXPORT_SYMBOL_GPL(iio_write_channel_raw);
  814. unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
  815. {
  816. const struct iio_chan_spec_ext_info *ext_info;
  817. unsigned int i = 0;
  818. if (!chan->channel->ext_info)
  819. return i;
  820. for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
  821. ++i;
  822. return i;
  823. }
  824. EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
  825. static const struct iio_chan_spec_ext_info *
  826. iio_lookup_ext_info(const struct iio_channel *chan, const char *attr)
  827. {
  828. const struct iio_chan_spec_ext_info *ext_info;
  829. if (!chan->channel->ext_info)
  830. return NULL;
  831. for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
  832. if (!strcmp(attr, ext_info->name))
  833. return ext_info;
  834. }
  835. return NULL;
  836. }
  837. ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
  838. const char *attr, char *buf)
  839. {
  840. const struct iio_chan_spec_ext_info *ext_info;
  841. if (!buf || offset_in_page(buf)) {
  842. pr_err("iio: invalid ext_info read buffer\n");
  843. return -EINVAL;
  844. }
  845. ext_info = iio_lookup_ext_info(chan, attr);
  846. if (!ext_info)
  847. return -EINVAL;
  848. return ext_info->read(chan->indio_dev, ext_info->private,
  849. chan->channel, buf);
  850. }
  851. EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
  852. ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
  853. const char *buf, size_t len)
  854. {
  855. const struct iio_chan_spec_ext_info *ext_info;
  856. ext_info = iio_lookup_ext_info(chan, attr);
  857. if (!ext_info)
  858. return -EINVAL;
  859. return ext_info->write(chan->indio_dev, ext_info->private,
  860. chan->channel, buf, len);
  861. }
  862. EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
  863. ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf)
  864. {
  865. if (!buf || offset_in_page(buf)) {
  866. pr_err("iio: invalid label read buffer\n");
  867. return -EINVAL;
  868. }
  869. return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf);
  870. }
  871. EXPORT_SYMBOL_GPL(iio_read_channel_label);