inkern.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* The industrial I/O core in kernel channel mapping
  2. *
  3. * Copyright (c) 2011 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. #include <linux/err.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of.h>
  14. #include <linux/iio/iio.h>
  15. #include "iio_core.h"
  16. #include <linux/iio/machine.h>
  17. #include <linux/iio/driver.h>
  18. #include <linux/iio/consumer.h>
  19. struct iio_map_internal {
  20. struct iio_dev *indio_dev;
  21. struct iio_map *map;
  22. struct list_head l;
  23. };
  24. static LIST_HEAD(iio_map_list);
  25. static DEFINE_MUTEX(iio_map_list_lock);
  26. int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
  27. {
  28. int i = 0, ret = 0;
  29. struct iio_map_internal *mapi;
  30. if (maps == NULL)
  31. return 0;
  32. mutex_lock(&iio_map_list_lock);
  33. while (maps[i].consumer_dev_name != NULL) {
  34. mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
  35. if (mapi == NULL) {
  36. ret = -ENOMEM;
  37. goto error_ret;
  38. }
  39. mapi->map = &maps[i];
  40. mapi->indio_dev = indio_dev;
  41. list_add_tail(&mapi->l, &iio_map_list);
  42. i++;
  43. }
  44. error_ret:
  45. mutex_unlock(&iio_map_list_lock);
  46. return ret;
  47. }
  48. EXPORT_SYMBOL_GPL(iio_map_array_register);
  49. /*
  50. * Remove all map entries associated with the given iio device
  51. */
  52. int iio_map_array_unregister(struct iio_dev *indio_dev)
  53. {
  54. int ret = -ENODEV;
  55. struct iio_map_internal *mapi, *next;
  56. mutex_lock(&iio_map_list_lock);
  57. list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
  58. if (indio_dev == mapi->indio_dev) {
  59. list_del(&mapi->l);
  60. kfree(mapi);
  61. ret = 0;
  62. }
  63. }
  64. mutex_unlock(&iio_map_list_lock);
  65. return ret;
  66. }
  67. EXPORT_SYMBOL_GPL(iio_map_array_unregister);
  68. static const struct iio_chan_spec
  69. *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
  70. {
  71. int i;
  72. const struct iio_chan_spec *chan = NULL;
  73. for (i = 0; i < indio_dev->num_channels; i++)
  74. if (indio_dev->channels[i].datasheet_name &&
  75. strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
  76. chan = &indio_dev->channels[i];
  77. break;
  78. }
  79. return chan;
  80. }
  81. #ifdef CONFIG_OF
  82. static int iio_dev_node_match(struct device *dev, void *data)
  83. {
  84. return dev->of_node == data && dev->type == &iio_device_type;
  85. }
  86. /**
  87. * __of_iio_simple_xlate - translate iiospec to the IIO channel index
  88. * @indio_dev: pointer to the iio_dev structure
  89. * @iiospec: IIO specifier as found in the device tree
  90. *
  91. * This is simple translation function, suitable for the most 1:1 mapped
  92. * channels in IIO chips. This function performs only one sanity check:
  93. * whether IIO index is less than num_channels (that is specified in the
  94. * iio_dev).
  95. */
  96. static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
  97. const struct of_phandle_args *iiospec)
  98. {
  99. if (!iiospec->args_count)
  100. return 0;
  101. if (iiospec->args[0] >= indio_dev->num_channels) {
  102. dev_err(&indio_dev->dev, "invalid channel index %u\n",
  103. iiospec->args[0]);
  104. return -EINVAL;
  105. }
  106. return iiospec->args[0];
  107. }
  108. static int __of_iio_channel_get(struct iio_channel *channel,
  109. struct device_node *np, int index)
  110. {
  111. struct device *idev;
  112. struct iio_dev *indio_dev;
  113. int err;
  114. struct of_phandle_args iiospec;
  115. err = of_parse_phandle_with_args(np, "io-channels",
  116. "#io-channel-cells",
  117. index, &iiospec);
  118. if (err)
  119. return err;
  120. idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
  121. iio_dev_node_match);
  122. of_node_put(iiospec.np);
  123. if (idev == NULL)
  124. return -EPROBE_DEFER;
  125. indio_dev = dev_to_iio_dev(idev);
  126. channel->indio_dev = indio_dev;
  127. if (indio_dev->info->of_xlate)
  128. index = indio_dev->info->of_xlate(indio_dev, &iiospec);
  129. else
  130. index = __of_iio_simple_xlate(indio_dev, &iiospec);
  131. if (index < 0)
  132. goto err_put;
  133. channel->channel = &indio_dev->channels[index];
  134. return 0;
  135. err_put:
  136. iio_device_put(indio_dev);
  137. return index;
  138. }
  139. static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
  140. {
  141. struct iio_channel *channel;
  142. int err;
  143. if (index < 0)
  144. return ERR_PTR(-EINVAL);
  145. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  146. if (channel == NULL)
  147. return ERR_PTR(-ENOMEM);
  148. err = __of_iio_channel_get(channel, np, index);
  149. if (err)
  150. goto err_free_channel;
  151. return channel;
  152. err_free_channel:
  153. kfree(channel);
  154. return ERR_PTR(err);
  155. }
  156. static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
  157. const char *name)
  158. {
  159. struct iio_channel *chan = NULL;
  160. /* Walk up the tree of devices looking for a matching iio channel */
  161. while (np) {
  162. int index = 0;
  163. /*
  164. * For named iio channels, first look up the name in the
  165. * "io-channel-names" property. If it cannot be found, the
  166. * index will be an error code, and of_iio_channel_get()
  167. * will fail.
  168. */
  169. if (name)
  170. index = of_property_match_string(np, "io-channel-names",
  171. name);
  172. chan = of_iio_channel_get(np, index);
  173. if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
  174. break;
  175. else if (name && index >= 0) {
  176. pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
  177. np, name ? name : "", index);
  178. return NULL;
  179. }
  180. /*
  181. * No matching IIO channel found on this node.
  182. * If the parent node has a "io-channel-ranges" property,
  183. * then we can try one of its channels.
  184. */
  185. np = np->parent;
  186. if (np && !of_get_property(np, "io-channel-ranges", NULL))
  187. return NULL;
  188. }
  189. return chan;
  190. }
  191. static struct iio_channel *of_iio_channel_get_all(struct device *dev)
  192. {
  193. struct iio_channel *chans;
  194. int i, mapind, nummaps = 0;
  195. int ret;
  196. do {
  197. ret = of_parse_phandle_with_args(dev->of_node,
  198. "io-channels",
  199. "#io-channel-cells",
  200. nummaps, NULL);
  201. if (ret < 0)
  202. break;
  203. } while (++nummaps);
  204. if (nummaps == 0) /* no error, return NULL to search map table */
  205. return NULL;
  206. /* NULL terminated array to save passing size */
  207. chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
  208. if (chans == NULL)
  209. return ERR_PTR(-ENOMEM);
  210. /* Search for OF matches */
  211. for (mapind = 0; mapind < nummaps; mapind++) {
  212. ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
  213. mapind);
  214. if (ret)
  215. goto error_free_chans;
  216. }
  217. return chans;
  218. error_free_chans:
  219. for (i = 0; i < mapind; i++)
  220. iio_device_put(chans[i].indio_dev);
  221. kfree(chans);
  222. return ERR_PTR(ret);
  223. }
  224. #else /* CONFIG_OF */
  225. static inline struct iio_channel *
  226. of_iio_channel_get_by_name(struct device_node *np, const char *name)
  227. {
  228. return NULL;
  229. }
  230. static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
  231. {
  232. return NULL;
  233. }
  234. #endif /* CONFIG_OF */
  235. static struct iio_channel *iio_channel_get_sys(const char *name,
  236. const char *channel_name)
  237. {
  238. struct iio_map_internal *c_i = NULL, *c = NULL;
  239. struct iio_channel *channel;
  240. int err;
  241. if (name == NULL && channel_name == NULL)
  242. return ERR_PTR(-ENODEV);
  243. /* first find matching entry the channel map */
  244. mutex_lock(&iio_map_list_lock);
  245. list_for_each_entry(c_i, &iio_map_list, l) {
  246. if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
  247. (channel_name &&
  248. strcmp(channel_name, c_i->map->consumer_channel) != 0))
  249. continue;
  250. c = c_i;
  251. iio_device_get(c->indio_dev);
  252. break;
  253. }
  254. mutex_unlock(&iio_map_list_lock);
  255. if (c == NULL)
  256. return ERR_PTR(-ENODEV);
  257. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  258. if (channel == NULL) {
  259. err = -ENOMEM;
  260. goto error_no_mem;
  261. }
  262. channel->indio_dev = c->indio_dev;
  263. if (c->map->adc_channel_label) {
  264. channel->channel =
  265. iio_chan_spec_from_name(channel->indio_dev,
  266. c->map->adc_channel_label);
  267. if (channel->channel == NULL) {
  268. err = -EINVAL;
  269. goto error_no_chan;
  270. }
  271. }
  272. return channel;
  273. error_no_chan:
  274. kfree(channel);
  275. error_no_mem:
  276. iio_device_put(c->indio_dev);
  277. return ERR_PTR(err);
  278. }
  279. struct iio_channel *iio_channel_get(struct device *dev,
  280. const char *channel_name)
  281. {
  282. const char *name = dev ? dev_name(dev) : NULL;
  283. struct iio_channel *channel;
  284. if (dev) {
  285. channel = of_iio_channel_get_by_name(dev->of_node,
  286. channel_name);
  287. if (channel != NULL)
  288. return channel;
  289. }
  290. return iio_channel_get_sys(name, channel_name);
  291. }
  292. EXPORT_SYMBOL_GPL(iio_channel_get);
  293. void iio_channel_release(struct iio_channel *channel)
  294. {
  295. if (!channel)
  296. return;
  297. iio_device_put(channel->indio_dev);
  298. kfree(channel);
  299. }
  300. EXPORT_SYMBOL_GPL(iio_channel_release);
  301. static void devm_iio_channel_free(struct device *dev, void *res)
  302. {
  303. struct iio_channel *channel = *(struct iio_channel **)res;
  304. iio_channel_release(channel);
  305. }
  306. static int devm_iio_channel_match(struct device *dev, void *res, void *data)
  307. {
  308. struct iio_channel **r = res;
  309. if (!r || !*r) {
  310. WARN_ON(!r || !*r);
  311. return 0;
  312. }
  313. return *r == data;
  314. }
  315. struct iio_channel *devm_iio_channel_get(struct device *dev,
  316. const char *channel_name)
  317. {
  318. struct iio_channel **ptr, *channel;
  319. ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
  320. if (!ptr)
  321. return ERR_PTR(-ENOMEM);
  322. channel = iio_channel_get(dev, channel_name);
  323. if (IS_ERR(channel)) {
  324. devres_free(ptr);
  325. return channel;
  326. }
  327. *ptr = channel;
  328. devres_add(dev, ptr);
  329. return channel;
  330. }
  331. EXPORT_SYMBOL_GPL(devm_iio_channel_get);
  332. void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
  333. {
  334. WARN_ON(devres_release(dev, devm_iio_channel_free,
  335. devm_iio_channel_match, channel));
  336. }
  337. EXPORT_SYMBOL_GPL(devm_iio_channel_release);
  338. struct iio_channel *iio_channel_get_all(struct device *dev)
  339. {
  340. const char *name;
  341. struct iio_channel *chans;
  342. struct iio_map_internal *c = NULL;
  343. int nummaps = 0;
  344. int mapind = 0;
  345. int i, ret;
  346. if (dev == NULL)
  347. return ERR_PTR(-EINVAL);
  348. chans = of_iio_channel_get_all(dev);
  349. if (chans)
  350. return chans;
  351. name = dev_name(dev);
  352. mutex_lock(&iio_map_list_lock);
  353. /* first count the matching maps */
  354. list_for_each_entry(c, &iio_map_list, l)
  355. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  356. continue;
  357. else
  358. nummaps++;
  359. if (nummaps == 0) {
  360. ret = -ENODEV;
  361. goto error_ret;
  362. }
  363. /* NULL terminated array to save passing size */
  364. chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
  365. if (chans == NULL) {
  366. ret = -ENOMEM;
  367. goto error_ret;
  368. }
  369. /* for each map fill in the chans element */
  370. list_for_each_entry(c, &iio_map_list, l) {
  371. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  372. continue;
  373. chans[mapind].indio_dev = c->indio_dev;
  374. chans[mapind].data = c->map->consumer_data;
  375. chans[mapind].channel =
  376. iio_chan_spec_from_name(chans[mapind].indio_dev,
  377. c->map->adc_channel_label);
  378. if (chans[mapind].channel == NULL) {
  379. ret = -EINVAL;
  380. goto error_free_chans;
  381. }
  382. iio_device_get(chans[mapind].indio_dev);
  383. mapind++;
  384. }
  385. if (mapind == 0) {
  386. ret = -ENODEV;
  387. goto error_free_chans;
  388. }
  389. mutex_unlock(&iio_map_list_lock);
  390. return chans;
  391. error_free_chans:
  392. for (i = 0; i < nummaps; i++)
  393. iio_device_put(chans[i].indio_dev);
  394. kfree(chans);
  395. error_ret:
  396. mutex_unlock(&iio_map_list_lock);
  397. return ERR_PTR(ret);
  398. }
  399. EXPORT_SYMBOL_GPL(iio_channel_get_all);
  400. void iio_channel_release_all(struct iio_channel *channels)
  401. {
  402. struct iio_channel *chan = &channels[0];
  403. while (chan->indio_dev) {
  404. iio_device_put(chan->indio_dev);
  405. chan++;
  406. }
  407. kfree(channels);
  408. }
  409. EXPORT_SYMBOL_GPL(iio_channel_release_all);
  410. static void devm_iio_channel_free_all(struct device *dev, void *res)
  411. {
  412. struct iio_channel *channels = *(struct iio_channel **)res;
  413. iio_channel_release_all(channels);
  414. }
  415. struct iio_channel *devm_iio_channel_get_all(struct device *dev)
  416. {
  417. struct iio_channel **ptr, *channels;
  418. ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
  419. if (!ptr)
  420. return ERR_PTR(-ENOMEM);
  421. channels = iio_channel_get_all(dev);
  422. if (IS_ERR(channels)) {
  423. devres_free(ptr);
  424. return channels;
  425. }
  426. *ptr = channels;
  427. devres_add(dev, ptr);
  428. return channels;
  429. }
  430. EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
  431. void devm_iio_channel_release_all(struct device *dev,
  432. struct iio_channel *channels)
  433. {
  434. WARN_ON(devres_release(dev, devm_iio_channel_free_all,
  435. devm_iio_channel_match, channels));
  436. }
  437. EXPORT_SYMBOL_GPL(devm_iio_channel_release_all);
  438. static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
  439. enum iio_chan_info_enum info)
  440. {
  441. int unused;
  442. int vals[INDIO_MAX_RAW_ELEMENTS];
  443. int ret;
  444. int val_len = 2;
  445. if (val2 == NULL)
  446. val2 = &unused;
  447. if (!iio_channel_has_info(chan->channel, info))
  448. return -EINVAL;
  449. if (chan->indio_dev->info->read_raw_multi) {
  450. ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
  451. chan->channel, INDIO_MAX_RAW_ELEMENTS,
  452. vals, &val_len, info);
  453. *val = vals[0];
  454. *val2 = vals[1];
  455. } else
  456. ret = chan->indio_dev->info->read_raw(chan->indio_dev,
  457. chan->channel, val, val2, info);
  458. return ret;
  459. }
  460. int iio_read_channel_raw(struct iio_channel *chan, int *val)
  461. {
  462. int ret;
  463. mutex_lock(&chan->indio_dev->info_exist_lock);
  464. if (chan->indio_dev->info == NULL) {
  465. ret = -ENODEV;
  466. goto err_unlock;
  467. }
  468. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  469. err_unlock:
  470. mutex_unlock(&chan->indio_dev->info_exist_lock);
  471. return ret;
  472. }
  473. EXPORT_SYMBOL_GPL(iio_read_channel_raw);
  474. int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
  475. {
  476. int ret;
  477. mutex_lock(&chan->indio_dev->info_exist_lock);
  478. if (chan->indio_dev->info == NULL) {
  479. ret = -ENODEV;
  480. goto err_unlock;
  481. }
  482. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
  483. err_unlock:
  484. mutex_unlock(&chan->indio_dev->info_exist_lock);
  485. return ret;
  486. }
  487. EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
  488. static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
  489. int raw, int *processed, unsigned int scale)
  490. {
  491. int scale_type, scale_val, scale_val2, offset;
  492. s64 raw64 = raw;
  493. int ret;
  494. ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
  495. if (ret >= 0)
  496. raw64 += offset;
  497. scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
  498. IIO_CHAN_INFO_SCALE);
  499. if (scale_type < 0) {
  500. /*
  501. * Just pass raw values as processed if no scaling is
  502. * available.
  503. */
  504. *processed = raw;
  505. return 0;
  506. }
  507. switch (scale_type) {
  508. case IIO_VAL_INT:
  509. *processed = raw64 * scale_val;
  510. break;
  511. case IIO_VAL_INT_PLUS_MICRO:
  512. if (scale_val2 < 0)
  513. *processed = -raw64 * scale_val;
  514. else
  515. *processed = raw64 * scale_val;
  516. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  517. 1000000LL);
  518. break;
  519. case IIO_VAL_INT_PLUS_NANO:
  520. if (scale_val2 < 0)
  521. *processed = -raw64 * scale_val;
  522. else
  523. *processed = raw64 * scale_val;
  524. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  525. 1000000000LL);
  526. break;
  527. case IIO_VAL_FRACTIONAL:
  528. *processed = div_s64(raw64 * (s64)scale_val * scale,
  529. scale_val2);
  530. break;
  531. case IIO_VAL_FRACTIONAL_LOG2:
  532. *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
  533. break;
  534. default:
  535. return -EINVAL;
  536. }
  537. return 0;
  538. }
  539. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  540. int *processed, unsigned int scale)
  541. {
  542. int ret;
  543. mutex_lock(&chan->indio_dev->info_exist_lock);
  544. if (chan->indio_dev->info == NULL) {
  545. ret = -ENODEV;
  546. goto err_unlock;
  547. }
  548. ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
  549. scale);
  550. err_unlock:
  551. mutex_unlock(&chan->indio_dev->info_exist_lock);
  552. return ret;
  553. }
  554. EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
  555. int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
  556. enum iio_chan_info_enum attribute)
  557. {
  558. int ret;
  559. mutex_lock(&chan->indio_dev->info_exist_lock);
  560. if (chan->indio_dev->info == NULL) {
  561. ret = -ENODEV;
  562. goto err_unlock;
  563. }
  564. ret = iio_channel_read(chan, val, val2, attribute);
  565. err_unlock:
  566. mutex_unlock(&chan->indio_dev->info_exist_lock);
  567. return ret;
  568. }
  569. EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
  570. int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
  571. {
  572. return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
  573. }
  574. EXPORT_SYMBOL_GPL(iio_read_channel_offset);
  575. int iio_read_channel_processed(struct iio_channel *chan, int *val)
  576. {
  577. int ret;
  578. mutex_lock(&chan->indio_dev->info_exist_lock);
  579. if (chan->indio_dev->info == NULL) {
  580. ret = -ENODEV;
  581. goto err_unlock;
  582. }
  583. if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
  584. ret = iio_channel_read(chan, val, NULL,
  585. IIO_CHAN_INFO_PROCESSED);
  586. } else {
  587. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  588. if (ret < 0)
  589. goto err_unlock;
  590. ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
  591. }
  592. err_unlock:
  593. mutex_unlock(&chan->indio_dev->info_exist_lock);
  594. return ret;
  595. }
  596. EXPORT_SYMBOL_GPL(iio_read_channel_processed);
  597. int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
  598. {
  599. return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
  600. }
  601. EXPORT_SYMBOL_GPL(iio_read_channel_scale);
  602. static int iio_channel_read_avail(struct iio_channel *chan,
  603. const int **vals, int *type, int *length,
  604. enum iio_chan_info_enum info)
  605. {
  606. if (!iio_channel_has_available(chan->channel, info))
  607. return -EINVAL;
  608. return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
  609. vals, type, length, info);
  610. }
  611. int iio_read_avail_channel_raw(struct iio_channel *chan,
  612. const int **vals, int *length)
  613. {
  614. int ret;
  615. int type;
  616. mutex_lock(&chan->indio_dev->info_exist_lock);
  617. if (!chan->indio_dev->info) {
  618. ret = -ENODEV;
  619. goto err_unlock;
  620. }
  621. ret = iio_channel_read_avail(chan,
  622. vals, &type, length, IIO_CHAN_INFO_RAW);
  623. err_unlock:
  624. mutex_unlock(&chan->indio_dev->info_exist_lock);
  625. if (ret >= 0 && type != IIO_VAL_INT)
  626. /* raw values are assumed to be IIO_VAL_INT */
  627. ret = -EINVAL;
  628. return ret;
  629. }
  630. EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
  631. static int iio_channel_read_max(struct iio_channel *chan,
  632. int *val, int *val2, int *type,
  633. enum iio_chan_info_enum info)
  634. {
  635. int unused;
  636. const int *vals;
  637. int length;
  638. int ret;
  639. if (!val2)
  640. val2 = &unused;
  641. ret = iio_channel_read_avail(chan, &vals, type, &length, info);
  642. switch (ret) {
  643. case IIO_AVAIL_RANGE:
  644. switch (*type) {
  645. case IIO_VAL_INT:
  646. *val = vals[2];
  647. break;
  648. default:
  649. *val = vals[4];
  650. *val2 = vals[5];
  651. }
  652. return 0;
  653. case IIO_AVAIL_LIST:
  654. if (length <= 0)
  655. return -EINVAL;
  656. switch (*type) {
  657. case IIO_VAL_INT:
  658. *val = vals[--length];
  659. while (length) {
  660. if (vals[--length] > *val)
  661. *val = vals[length];
  662. }
  663. break;
  664. default:
  665. /* FIXME: learn about max for other iio values */
  666. return -EINVAL;
  667. }
  668. return 0;
  669. default:
  670. return ret;
  671. }
  672. }
  673. int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
  674. {
  675. int ret;
  676. int type;
  677. mutex_lock(&chan->indio_dev->info_exist_lock);
  678. if (!chan->indio_dev->info) {
  679. ret = -ENODEV;
  680. goto err_unlock;
  681. }
  682. ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
  683. err_unlock:
  684. mutex_unlock(&chan->indio_dev->info_exist_lock);
  685. return ret;
  686. }
  687. EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
  688. int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
  689. {
  690. int ret = 0;
  691. /* Need to verify underlying driver has not gone away */
  692. mutex_lock(&chan->indio_dev->info_exist_lock);
  693. if (chan->indio_dev->info == NULL) {
  694. ret = -ENODEV;
  695. goto err_unlock;
  696. }
  697. *type = chan->channel->type;
  698. err_unlock:
  699. mutex_unlock(&chan->indio_dev->info_exist_lock);
  700. return ret;
  701. }
  702. EXPORT_SYMBOL_GPL(iio_get_channel_type);
  703. static int iio_channel_write(struct iio_channel *chan, int val, int val2,
  704. enum iio_chan_info_enum info)
  705. {
  706. return chan->indio_dev->info->write_raw(chan->indio_dev,
  707. chan->channel, val, val2, info);
  708. }
  709. int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
  710. enum iio_chan_info_enum attribute)
  711. {
  712. int ret;
  713. mutex_lock(&chan->indio_dev->info_exist_lock);
  714. if (chan->indio_dev->info == NULL) {
  715. ret = -ENODEV;
  716. goto err_unlock;
  717. }
  718. ret = iio_channel_write(chan, val, val2, attribute);
  719. err_unlock:
  720. mutex_unlock(&chan->indio_dev->info_exist_lock);
  721. return ret;
  722. }
  723. EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
  724. int iio_write_channel_raw(struct iio_channel *chan, int val)
  725. {
  726. return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
  727. }
  728. EXPORT_SYMBOL_GPL(iio_write_channel_raw);
  729. unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
  730. {
  731. const struct iio_chan_spec_ext_info *ext_info;
  732. unsigned int i = 0;
  733. if (!chan->channel->ext_info)
  734. return i;
  735. for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
  736. ++i;
  737. return i;
  738. }
  739. EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
  740. static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
  741. const struct iio_channel *chan,
  742. const char *attr)
  743. {
  744. const struct iio_chan_spec_ext_info *ext_info;
  745. if (!chan->channel->ext_info)
  746. return NULL;
  747. for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
  748. if (!strcmp(attr, ext_info->name))
  749. return ext_info;
  750. }
  751. return NULL;
  752. }
  753. ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
  754. const char *attr, char *buf)
  755. {
  756. const struct iio_chan_spec_ext_info *ext_info;
  757. ext_info = iio_lookup_ext_info(chan, attr);
  758. if (!ext_info)
  759. return -EINVAL;
  760. return ext_info->read(chan->indio_dev, ext_info->private,
  761. chan->channel, buf);
  762. }
  763. EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
  764. ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
  765. const char *buf, size_t len)
  766. {
  767. const struct iio_chan_spec_ext_info *ext_info;
  768. ext_info = iio_lookup_ext_info(chan, attr);
  769. if (!ext_info)
  770. return -EINVAL;
  771. return ext_info->write(chan->indio_dev, ext_info->private,
  772. chan->channel, buf, len);
  773. }
  774. EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);