inkern.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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;
  56. struct list_head *pos, *tmp;
  57. mutex_lock(&iio_map_list_lock);
  58. list_for_each_safe(pos, tmp, &iio_map_list) {
  59. mapi = list_entry(pos, struct iio_map_internal, l);
  60. if (indio_dev == mapi->indio_dev) {
  61. list_del(&mapi->l);
  62. kfree(mapi);
  63. ret = 0;
  64. }
  65. }
  66. mutex_unlock(&iio_map_list_lock);
  67. return ret;
  68. }
  69. EXPORT_SYMBOL_GPL(iio_map_array_unregister);
  70. static const struct iio_chan_spec
  71. *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
  72. {
  73. int i;
  74. const struct iio_chan_spec *chan = NULL;
  75. for (i = 0; i < indio_dev->num_channels; i++)
  76. if (indio_dev->channels[i].datasheet_name &&
  77. strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
  78. chan = &indio_dev->channels[i];
  79. break;
  80. }
  81. return chan;
  82. }
  83. #ifdef CONFIG_OF
  84. static int iio_dev_node_match(struct device *dev, void *data)
  85. {
  86. return dev->of_node == data && dev->type == &iio_device_type;
  87. }
  88. /**
  89. * __of_iio_simple_xlate - translate iiospec to the IIO channel index
  90. * @indio_dev: pointer to the iio_dev structure
  91. * @iiospec: IIO specifier as found in the device tree
  92. *
  93. * This is simple translation function, suitable for the most 1:1 mapped
  94. * channels in IIO chips. This function performs only one sanity check:
  95. * whether IIO index is less than num_channels (that is specified in the
  96. * iio_dev).
  97. */
  98. static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
  99. const struct of_phandle_args *iiospec)
  100. {
  101. if (!iiospec->args_count)
  102. return 0;
  103. if (iiospec->args[0] >= indio_dev->num_channels) {
  104. dev_err(&indio_dev->dev, "invalid channel index %u\n",
  105. iiospec->args[0]);
  106. return -EINVAL;
  107. }
  108. return iiospec->args[0];
  109. }
  110. static int __of_iio_channel_get(struct iio_channel *channel,
  111. struct device_node *np, int index)
  112. {
  113. struct device *idev;
  114. struct iio_dev *indio_dev;
  115. int err;
  116. struct of_phandle_args iiospec;
  117. err = of_parse_phandle_with_args(np, "io-channels",
  118. "#io-channel-cells",
  119. index, &iiospec);
  120. if (err)
  121. return err;
  122. idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
  123. iio_dev_node_match);
  124. of_node_put(iiospec.np);
  125. if (idev == NULL)
  126. return -EPROBE_DEFER;
  127. indio_dev = dev_to_iio_dev(idev);
  128. channel->indio_dev = indio_dev;
  129. if (indio_dev->info->of_xlate)
  130. index = indio_dev->info->of_xlate(indio_dev, &iiospec);
  131. else
  132. index = __of_iio_simple_xlate(indio_dev, &iiospec);
  133. if (index < 0)
  134. goto err_put;
  135. channel->channel = &indio_dev->channels[index];
  136. return 0;
  137. err_put:
  138. iio_device_put(indio_dev);
  139. return index;
  140. }
  141. static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
  142. {
  143. struct iio_channel *channel;
  144. int err;
  145. if (index < 0)
  146. return ERR_PTR(-EINVAL);
  147. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  148. if (channel == NULL)
  149. return ERR_PTR(-ENOMEM);
  150. err = __of_iio_channel_get(channel, np, index);
  151. if (err)
  152. goto err_free_channel;
  153. return channel;
  154. err_free_channel:
  155. kfree(channel);
  156. return ERR_PTR(err);
  157. }
  158. static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
  159. const char *name)
  160. {
  161. struct iio_channel *chan = NULL;
  162. /* Walk up the tree of devices looking for a matching iio channel */
  163. while (np) {
  164. int index = 0;
  165. /*
  166. * For named iio channels, first look up the name in the
  167. * "io-channel-names" property. If it cannot be found, the
  168. * index will be an error code, and of_iio_channel_get()
  169. * will fail.
  170. */
  171. if (name)
  172. index = of_property_match_string(np, "io-channel-names",
  173. name);
  174. chan = of_iio_channel_get(np, index);
  175. if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
  176. break;
  177. else if (name && index >= 0) {
  178. pr_err("ERROR: could not get IIO channel %s:%s(%i)\n",
  179. np->full_name, name ? name : "", index);
  180. return NULL;
  181. }
  182. /*
  183. * No matching IIO channel found on this node.
  184. * If the parent node has a "io-channel-ranges" property,
  185. * then we can try one of its channels.
  186. */
  187. np = np->parent;
  188. if (np && !of_get_property(np, "io-channel-ranges", NULL))
  189. return NULL;
  190. }
  191. return chan;
  192. }
  193. static struct iio_channel *of_iio_channel_get_all(struct device *dev)
  194. {
  195. struct iio_channel *chans;
  196. int i, mapind, nummaps = 0;
  197. int ret;
  198. do {
  199. ret = of_parse_phandle_with_args(dev->of_node,
  200. "io-channels",
  201. "#io-channel-cells",
  202. nummaps, NULL);
  203. if (ret < 0)
  204. break;
  205. } while (++nummaps);
  206. if (nummaps == 0) /* no error, return NULL to search map table */
  207. return NULL;
  208. /* NULL terminated array to save passing size */
  209. chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
  210. if (chans == NULL)
  211. return ERR_PTR(-ENOMEM);
  212. /* Search for OF matches */
  213. for (mapind = 0; mapind < nummaps; mapind++) {
  214. ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
  215. mapind);
  216. if (ret)
  217. goto error_free_chans;
  218. }
  219. return chans;
  220. error_free_chans:
  221. for (i = 0; i < mapind; i++)
  222. iio_device_put(chans[i].indio_dev);
  223. kfree(chans);
  224. return ERR_PTR(ret);
  225. }
  226. #else /* CONFIG_OF */
  227. static inline struct iio_channel *
  228. of_iio_channel_get_by_name(struct device_node *np, const char *name)
  229. {
  230. return NULL;
  231. }
  232. static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
  233. {
  234. return NULL;
  235. }
  236. #endif /* CONFIG_OF */
  237. static struct iio_channel *iio_channel_get_sys(const char *name,
  238. const char *channel_name)
  239. {
  240. struct iio_map_internal *c_i = NULL, *c = NULL;
  241. struct iio_channel *channel;
  242. int err;
  243. if (name == NULL && channel_name == NULL)
  244. return ERR_PTR(-ENODEV);
  245. /* first find matching entry the channel map */
  246. mutex_lock(&iio_map_list_lock);
  247. list_for_each_entry(c_i, &iio_map_list, l) {
  248. if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
  249. (channel_name &&
  250. strcmp(channel_name, c_i->map->consumer_channel) != 0))
  251. continue;
  252. c = c_i;
  253. iio_device_get(c->indio_dev);
  254. break;
  255. }
  256. mutex_unlock(&iio_map_list_lock);
  257. if (c == NULL)
  258. return ERR_PTR(-ENODEV);
  259. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  260. if (channel == NULL) {
  261. err = -ENOMEM;
  262. goto error_no_mem;
  263. }
  264. channel->indio_dev = c->indio_dev;
  265. if (c->map->adc_channel_label) {
  266. channel->channel =
  267. iio_chan_spec_from_name(channel->indio_dev,
  268. c->map->adc_channel_label);
  269. if (channel->channel == NULL) {
  270. err = -EINVAL;
  271. goto error_no_chan;
  272. }
  273. }
  274. return channel;
  275. error_no_chan:
  276. kfree(channel);
  277. error_no_mem:
  278. iio_device_put(c->indio_dev);
  279. return ERR_PTR(err);
  280. }
  281. struct iio_channel *iio_channel_get(struct device *dev,
  282. const char *channel_name)
  283. {
  284. const char *name = dev ? dev_name(dev) : NULL;
  285. struct iio_channel *channel;
  286. if (dev) {
  287. channel = of_iio_channel_get_by_name(dev->of_node,
  288. channel_name);
  289. if (channel != NULL)
  290. return channel;
  291. }
  292. return iio_channel_get_sys(name, channel_name);
  293. }
  294. EXPORT_SYMBOL_GPL(iio_channel_get);
  295. void iio_channel_release(struct iio_channel *channel)
  296. {
  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);