inkern.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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(&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 %s:%s(%i)\n",
  177. np->full_name, 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 = kzalloc(sizeof(*chans)*(nummaps + 1), 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. return scale_type;
  501. switch (scale_type) {
  502. case IIO_VAL_INT:
  503. *processed = raw64 * scale_val;
  504. break;
  505. case IIO_VAL_INT_PLUS_MICRO:
  506. if (scale_val2 < 0)
  507. *processed = -raw64 * scale_val;
  508. else
  509. *processed = raw64 * scale_val;
  510. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  511. 1000000LL);
  512. break;
  513. case IIO_VAL_INT_PLUS_NANO:
  514. if (scale_val2 < 0)
  515. *processed = -raw64 * scale_val;
  516. else
  517. *processed = raw64 * scale_val;
  518. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  519. 1000000000LL);
  520. break;
  521. case IIO_VAL_FRACTIONAL:
  522. *processed = div_s64(raw64 * (s64)scale_val * scale,
  523. scale_val2);
  524. break;
  525. case IIO_VAL_FRACTIONAL_LOG2:
  526. *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
  527. break;
  528. default:
  529. return -EINVAL;
  530. }
  531. return 0;
  532. }
  533. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  534. int *processed, unsigned int scale)
  535. {
  536. int ret;
  537. mutex_lock(&chan->indio_dev->info_exist_lock);
  538. if (chan->indio_dev->info == NULL) {
  539. ret = -ENODEV;
  540. goto err_unlock;
  541. }
  542. ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
  543. scale);
  544. err_unlock:
  545. mutex_unlock(&chan->indio_dev->info_exist_lock);
  546. return ret;
  547. }
  548. EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
  549. int iio_read_channel_processed(struct iio_channel *chan, int *val)
  550. {
  551. int ret;
  552. mutex_lock(&chan->indio_dev->info_exist_lock);
  553. if (chan->indio_dev->info == NULL) {
  554. ret = -ENODEV;
  555. goto err_unlock;
  556. }
  557. if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
  558. ret = iio_channel_read(chan, val, NULL,
  559. IIO_CHAN_INFO_PROCESSED);
  560. } else {
  561. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  562. if (ret < 0)
  563. goto err_unlock;
  564. ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
  565. }
  566. err_unlock:
  567. mutex_unlock(&chan->indio_dev->info_exist_lock);
  568. return ret;
  569. }
  570. EXPORT_SYMBOL_GPL(iio_read_channel_processed);
  571. int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
  572. {
  573. int ret;
  574. mutex_lock(&chan->indio_dev->info_exist_lock);
  575. if (chan->indio_dev->info == NULL) {
  576. ret = -ENODEV;
  577. goto err_unlock;
  578. }
  579. ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
  580. err_unlock:
  581. mutex_unlock(&chan->indio_dev->info_exist_lock);
  582. return ret;
  583. }
  584. EXPORT_SYMBOL_GPL(iio_read_channel_scale);
  585. int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
  586. {
  587. int ret = 0;
  588. /* Need to verify underlying driver has not gone away */
  589. mutex_lock(&chan->indio_dev->info_exist_lock);
  590. if (chan->indio_dev->info == NULL) {
  591. ret = -ENODEV;
  592. goto err_unlock;
  593. }
  594. *type = chan->channel->type;
  595. err_unlock:
  596. mutex_unlock(&chan->indio_dev->info_exist_lock);
  597. return ret;
  598. }
  599. EXPORT_SYMBOL_GPL(iio_get_channel_type);
  600. static int iio_channel_write(struct iio_channel *chan, int val, int val2,
  601. enum iio_chan_info_enum info)
  602. {
  603. return chan->indio_dev->info->write_raw(chan->indio_dev,
  604. chan->channel, val, val2, info);
  605. }
  606. int iio_write_channel_raw(struct iio_channel *chan, int val)
  607. {
  608. int ret;
  609. mutex_lock(&chan->indio_dev->info_exist_lock);
  610. if (chan->indio_dev->info == NULL) {
  611. ret = -ENODEV;
  612. goto err_unlock;
  613. }
  614. ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
  615. err_unlock:
  616. mutex_unlock(&chan->indio_dev->info_exist_lock);
  617. return ret;
  618. }
  619. EXPORT_SYMBOL_GPL(iio_write_channel_raw);