inkern.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. struct iio_channel *iio_channel_get_all(struct device *dev)
  302. {
  303. const char *name;
  304. struct iio_channel *chans;
  305. struct iio_map_internal *c = NULL;
  306. int nummaps = 0;
  307. int mapind = 0;
  308. int i, ret;
  309. if (dev == NULL)
  310. return ERR_PTR(-EINVAL);
  311. chans = of_iio_channel_get_all(dev);
  312. if (chans)
  313. return chans;
  314. name = dev_name(dev);
  315. mutex_lock(&iio_map_list_lock);
  316. /* first count the matching maps */
  317. list_for_each_entry(c, &iio_map_list, l)
  318. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  319. continue;
  320. else
  321. nummaps++;
  322. if (nummaps == 0) {
  323. ret = -ENODEV;
  324. goto error_ret;
  325. }
  326. /* NULL terminated array to save passing size */
  327. chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
  328. if (chans == NULL) {
  329. ret = -ENOMEM;
  330. goto error_ret;
  331. }
  332. /* for each map fill in the chans element */
  333. list_for_each_entry(c, &iio_map_list, l) {
  334. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  335. continue;
  336. chans[mapind].indio_dev = c->indio_dev;
  337. chans[mapind].data = c->map->consumer_data;
  338. chans[mapind].channel =
  339. iio_chan_spec_from_name(chans[mapind].indio_dev,
  340. c->map->adc_channel_label);
  341. if (chans[mapind].channel == NULL) {
  342. ret = -EINVAL;
  343. goto error_free_chans;
  344. }
  345. iio_device_get(chans[mapind].indio_dev);
  346. mapind++;
  347. }
  348. if (mapind == 0) {
  349. ret = -ENODEV;
  350. goto error_free_chans;
  351. }
  352. mutex_unlock(&iio_map_list_lock);
  353. return chans;
  354. error_free_chans:
  355. for (i = 0; i < nummaps; i++)
  356. iio_device_put(chans[i].indio_dev);
  357. kfree(chans);
  358. error_ret:
  359. mutex_unlock(&iio_map_list_lock);
  360. return ERR_PTR(ret);
  361. }
  362. EXPORT_SYMBOL_GPL(iio_channel_get_all);
  363. void iio_channel_release_all(struct iio_channel *channels)
  364. {
  365. struct iio_channel *chan = &channels[0];
  366. while (chan->indio_dev) {
  367. iio_device_put(chan->indio_dev);
  368. chan++;
  369. }
  370. kfree(channels);
  371. }
  372. EXPORT_SYMBOL_GPL(iio_channel_release_all);
  373. static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
  374. enum iio_chan_info_enum info)
  375. {
  376. int unused;
  377. int vals[INDIO_MAX_RAW_ELEMENTS];
  378. int ret;
  379. int val_len = 2;
  380. if (val2 == NULL)
  381. val2 = &unused;
  382. if(!iio_channel_has_info(chan->channel, info))
  383. return -EINVAL;
  384. if (chan->indio_dev->info->read_raw_multi) {
  385. ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
  386. chan->channel, INDIO_MAX_RAW_ELEMENTS,
  387. vals, &val_len, info);
  388. *val = vals[0];
  389. *val2 = vals[1];
  390. } else
  391. ret = chan->indio_dev->info->read_raw(chan->indio_dev,
  392. chan->channel, val, val2, info);
  393. return ret;
  394. }
  395. int iio_read_channel_raw(struct iio_channel *chan, int *val)
  396. {
  397. int ret;
  398. mutex_lock(&chan->indio_dev->info_exist_lock);
  399. if (chan->indio_dev->info == NULL) {
  400. ret = -ENODEV;
  401. goto err_unlock;
  402. }
  403. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  404. err_unlock:
  405. mutex_unlock(&chan->indio_dev->info_exist_lock);
  406. return ret;
  407. }
  408. EXPORT_SYMBOL_GPL(iio_read_channel_raw);
  409. int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
  410. {
  411. int ret;
  412. mutex_lock(&chan->indio_dev->info_exist_lock);
  413. if (chan->indio_dev->info == NULL) {
  414. ret = -ENODEV;
  415. goto err_unlock;
  416. }
  417. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
  418. err_unlock:
  419. mutex_unlock(&chan->indio_dev->info_exist_lock);
  420. return ret;
  421. }
  422. EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
  423. static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
  424. int raw, int *processed, unsigned int scale)
  425. {
  426. int scale_type, scale_val, scale_val2, offset;
  427. s64 raw64 = raw;
  428. int ret;
  429. ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
  430. if (ret >= 0)
  431. raw64 += offset;
  432. scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
  433. IIO_CHAN_INFO_SCALE);
  434. if (scale_type < 0)
  435. return scale_type;
  436. switch (scale_type) {
  437. case IIO_VAL_INT:
  438. *processed = raw64 * scale_val;
  439. break;
  440. case IIO_VAL_INT_PLUS_MICRO:
  441. if (scale_val2 < 0)
  442. *processed = -raw64 * scale_val;
  443. else
  444. *processed = raw64 * scale_val;
  445. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  446. 1000000LL);
  447. break;
  448. case IIO_VAL_INT_PLUS_NANO:
  449. if (scale_val2 < 0)
  450. *processed = -raw64 * scale_val;
  451. else
  452. *processed = raw64 * scale_val;
  453. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  454. 1000000000LL);
  455. break;
  456. case IIO_VAL_FRACTIONAL:
  457. *processed = div_s64(raw64 * (s64)scale_val * scale,
  458. scale_val2);
  459. break;
  460. case IIO_VAL_FRACTIONAL_LOG2:
  461. *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
  462. break;
  463. default:
  464. return -EINVAL;
  465. }
  466. return 0;
  467. }
  468. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  469. int *processed, unsigned int scale)
  470. {
  471. int ret;
  472. mutex_lock(&chan->indio_dev->info_exist_lock);
  473. if (chan->indio_dev->info == NULL) {
  474. ret = -ENODEV;
  475. goto err_unlock;
  476. }
  477. ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
  478. scale);
  479. err_unlock:
  480. mutex_unlock(&chan->indio_dev->info_exist_lock);
  481. return ret;
  482. }
  483. EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
  484. int iio_read_channel_processed(struct iio_channel *chan, int *val)
  485. {
  486. int ret;
  487. mutex_lock(&chan->indio_dev->info_exist_lock);
  488. if (chan->indio_dev->info == NULL) {
  489. ret = -ENODEV;
  490. goto err_unlock;
  491. }
  492. if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
  493. ret = iio_channel_read(chan, val, NULL,
  494. IIO_CHAN_INFO_PROCESSED);
  495. } else {
  496. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  497. if (ret < 0)
  498. goto err_unlock;
  499. ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
  500. }
  501. err_unlock:
  502. mutex_unlock(&chan->indio_dev->info_exist_lock);
  503. return ret;
  504. }
  505. EXPORT_SYMBOL_GPL(iio_read_channel_processed);
  506. int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
  507. {
  508. int ret;
  509. mutex_lock(&chan->indio_dev->info_exist_lock);
  510. if (chan->indio_dev->info == NULL) {
  511. ret = -ENODEV;
  512. goto err_unlock;
  513. }
  514. ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
  515. err_unlock:
  516. mutex_unlock(&chan->indio_dev->info_exist_lock);
  517. return ret;
  518. }
  519. EXPORT_SYMBOL_GPL(iio_read_channel_scale);
  520. int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
  521. {
  522. int ret = 0;
  523. /* Need to verify underlying driver has not gone away */
  524. mutex_lock(&chan->indio_dev->info_exist_lock);
  525. if (chan->indio_dev->info == NULL) {
  526. ret = -ENODEV;
  527. goto err_unlock;
  528. }
  529. *type = chan->channel->type;
  530. err_unlock:
  531. mutex_unlock(&chan->indio_dev->info_exist_lock);
  532. return ret;
  533. }
  534. EXPORT_SYMBOL_GPL(iio_get_channel_type);
  535. static int iio_channel_write(struct iio_channel *chan, int val, int val2,
  536. enum iio_chan_info_enum info)
  537. {
  538. return chan->indio_dev->info->write_raw(chan->indio_dev,
  539. chan->channel, val, val2, info);
  540. }
  541. int iio_write_channel_raw(struct iio_channel *chan, int val)
  542. {
  543. int ret;
  544. mutex_lock(&chan->indio_dev->info_exist_lock);
  545. if (chan->indio_dev->info == NULL) {
  546. ret = -ENODEV;
  547. goto err_unlock;
  548. }
  549. ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
  550. err_unlock:
  551. mutex_unlock(&chan->indio_dev->info_exist_lock);
  552. return ret;
  553. }
  554. EXPORT_SYMBOL_GPL(iio_write_channel_raw);