inkern.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/iio/iio.h>
  14. #include "iio_core.h"
  15. #include <linux/iio/machine.h>
  16. #include <linux/iio/driver.h>
  17. #include <linux/iio/consumer.h>
  18. struct iio_map_internal {
  19. struct iio_dev *indio_dev;
  20. struct iio_map *map;
  21. struct list_head l;
  22. };
  23. static LIST_HEAD(iio_map_list);
  24. static DEFINE_MUTEX(iio_map_list_lock);
  25. int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
  26. {
  27. int i = 0, ret = 0;
  28. struct iio_map_internal *mapi;
  29. if (maps == NULL)
  30. return 0;
  31. mutex_lock(&iio_map_list_lock);
  32. while (maps[i].consumer_dev_name != NULL) {
  33. mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
  34. if (mapi == NULL) {
  35. ret = -ENOMEM;
  36. goto error_ret;
  37. }
  38. mapi->map = &maps[i];
  39. mapi->indio_dev = indio_dev;
  40. list_add(&mapi->l, &iio_map_list);
  41. i++;
  42. }
  43. error_ret:
  44. mutex_unlock(&iio_map_list_lock);
  45. return ret;
  46. }
  47. EXPORT_SYMBOL_GPL(iio_map_array_register);
  48. /*
  49. * Remove all map entries associated with the given iio device
  50. */
  51. int iio_map_array_unregister(struct iio_dev *indio_dev)
  52. {
  53. int ret = -ENODEV;
  54. struct iio_map_internal *mapi;
  55. struct list_head *pos, *tmp;
  56. mutex_lock(&iio_map_list_lock);
  57. list_for_each_safe(pos, tmp, &iio_map_list) {
  58. mapi = list_entry(pos, struct iio_map_internal, l);
  59. if (indio_dev == mapi->indio_dev) {
  60. list_del(&mapi->l);
  61. kfree(mapi);
  62. ret = 0;
  63. }
  64. }
  65. mutex_unlock(&iio_map_list_lock);
  66. return ret;
  67. }
  68. EXPORT_SYMBOL_GPL(iio_map_array_unregister);
  69. static const struct iio_chan_spec
  70. *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
  71. {
  72. int i;
  73. const struct iio_chan_spec *chan = NULL;
  74. for (i = 0; i < indio_dev->num_channels; i++)
  75. if (indio_dev->channels[i].datasheet_name &&
  76. strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
  77. chan = &indio_dev->channels[i];
  78. break;
  79. }
  80. return chan;
  81. }
  82. struct iio_channel *iio_channel_get(const char *name, const char *channel_name)
  83. {
  84. struct iio_map_internal *c_i = NULL, *c = NULL;
  85. struct iio_channel *channel;
  86. int err;
  87. if (name == NULL && channel_name == NULL)
  88. return ERR_PTR(-ENODEV);
  89. /* first find matching entry the channel map */
  90. mutex_lock(&iio_map_list_lock);
  91. list_for_each_entry(c_i, &iio_map_list, l) {
  92. if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
  93. (channel_name &&
  94. strcmp(channel_name, c_i->map->consumer_channel) != 0))
  95. continue;
  96. c = c_i;
  97. iio_device_get(c->indio_dev);
  98. break;
  99. }
  100. mutex_unlock(&iio_map_list_lock);
  101. if (c == NULL)
  102. return ERR_PTR(-ENODEV);
  103. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  104. if (channel == NULL) {
  105. err = -ENOMEM;
  106. goto error_no_mem;
  107. }
  108. channel->indio_dev = c->indio_dev;
  109. if (c->map->adc_channel_label) {
  110. channel->channel =
  111. iio_chan_spec_from_name(channel->indio_dev,
  112. c->map->adc_channel_label);
  113. if (channel->channel == NULL) {
  114. err = -EINVAL;
  115. goto error_no_chan;
  116. }
  117. }
  118. return channel;
  119. error_no_chan:
  120. kfree(channel);
  121. error_no_mem:
  122. iio_device_put(c->indio_dev);
  123. return ERR_PTR(err);
  124. }
  125. EXPORT_SYMBOL_GPL(iio_channel_get);
  126. void iio_channel_release(struct iio_channel *channel)
  127. {
  128. iio_device_put(channel->indio_dev);
  129. kfree(channel);
  130. }
  131. EXPORT_SYMBOL_GPL(iio_channel_release);
  132. struct iio_channel *iio_channel_get_all(struct device *dev)
  133. {
  134. const char *name;
  135. struct iio_channel *chans;
  136. struct iio_map_internal *c = NULL;
  137. int nummaps = 0;
  138. int mapind = 0;
  139. int i, ret;
  140. if (dev == NULL)
  141. return ERR_PTR(-EINVAL);
  142. name = dev_name(dev);
  143. mutex_lock(&iio_map_list_lock);
  144. /* first count the matching maps */
  145. list_for_each_entry(c, &iio_map_list, l)
  146. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  147. continue;
  148. else
  149. nummaps++;
  150. if (nummaps == 0) {
  151. ret = -ENODEV;
  152. goto error_ret;
  153. }
  154. /* NULL terminated array to save passing size */
  155. chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
  156. if (chans == NULL) {
  157. ret = -ENOMEM;
  158. goto error_ret;
  159. }
  160. /* for each map fill in the chans element */
  161. list_for_each_entry(c, &iio_map_list, l) {
  162. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  163. continue;
  164. chans[mapind].indio_dev = c->indio_dev;
  165. chans[mapind].data = c->map->consumer_data;
  166. chans[mapind].channel =
  167. iio_chan_spec_from_name(chans[mapind].indio_dev,
  168. c->map->adc_channel_label);
  169. if (chans[mapind].channel == NULL) {
  170. ret = -EINVAL;
  171. goto error_free_chans;
  172. }
  173. iio_device_get(chans[mapind].indio_dev);
  174. mapind++;
  175. }
  176. if (mapind == 0) {
  177. ret = -ENODEV;
  178. goto error_free_chans;
  179. }
  180. mutex_unlock(&iio_map_list_lock);
  181. return chans;
  182. error_free_chans:
  183. for (i = 0; i < nummaps; i++)
  184. iio_device_put(chans[i].indio_dev);
  185. kfree(chans);
  186. error_ret:
  187. mutex_unlock(&iio_map_list_lock);
  188. return ERR_PTR(ret);
  189. }
  190. EXPORT_SYMBOL_GPL(iio_channel_get_all);
  191. void iio_channel_release_all(struct iio_channel *channels)
  192. {
  193. struct iio_channel *chan = &channels[0];
  194. while (chan->indio_dev) {
  195. iio_device_put(chan->indio_dev);
  196. chan++;
  197. }
  198. kfree(channels);
  199. }
  200. EXPORT_SYMBOL_GPL(iio_channel_release_all);
  201. static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
  202. enum iio_chan_info_enum info)
  203. {
  204. int unused;
  205. if (val2 == NULL)
  206. val2 = &unused;
  207. return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
  208. val, val2, info);
  209. }
  210. int iio_read_channel_raw(struct iio_channel *chan, int *val)
  211. {
  212. int ret;
  213. mutex_lock(&chan->indio_dev->info_exist_lock);
  214. if (chan->indio_dev->info == NULL) {
  215. ret = -ENODEV;
  216. goto err_unlock;
  217. }
  218. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  219. err_unlock:
  220. mutex_unlock(&chan->indio_dev->info_exist_lock);
  221. return ret;
  222. }
  223. EXPORT_SYMBOL_GPL(iio_read_channel_raw);
  224. static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
  225. int raw, int *processed, unsigned int scale)
  226. {
  227. int scale_type, scale_val, scale_val2, offset;
  228. s64 raw64 = raw;
  229. int ret;
  230. ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_SCALE);
  231. if (ret == 0)
  232. raw64 += offset;
  233. scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
  234. IIO_CHAN_INFO_SCALE);
  235. if (scale_type < 0)
  236. return scale_type;
  237. switch (scale_type) {
  238. case IIO_VAL_INT:
  239. *processed = raw64 * scale_val;
  240. break;
  241. case IIO_VAL_INT_PLUS_MICRO:
  242. if (scale_val2 < 0)
  243. *processed = -raw64 * scale_val;
  244. else
  245. *processed = raw64 * scale_val;
  246. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  247. 1000000LL);
  248. break;
  249. case IIO_VAL_INT_PLUS_NANO:
  250. if (scale_val2 < 0)
  251. *processed = -raw64 * scale_val;
  252. else
  253. *processed = raw64 * scale_val;
  254. *processed += div_s64(raw64 * (s64)scale_val2 * scale,
  255. 1000000000LL);
  256. break;
  257. case IIO_VAL_FRACTIONAL:
  258. *processed = div_s64(raw64 * (s64)scale_val * scale,
  259. scale_val2);
  260. break;
  261. case IIO_VAL_FRACTIONAL_LOG2:
  262. *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. return 0;
  268. }
  269. int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
  270. int *processed, unsigned int scale)
  271. {
  272. int ret;
  273. mutex_lock(&chan->indio_dev->info_exist_lock);
  274. if (chan->indio_dev->info == NULL) {
  275. ret = -ENODEV;
  276. goto err_unlock;
  277. }
  278. ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
  279. scale);
  280. err_unlock:
  281. mutex_unlock(&chan->indio_dev->info_exist_lock);
  282. return ret;
  283. }
  284. EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
  285. int iio_read_channel_processed(struct iio_channel *chan, int *val)
  286. {
  287. int ret;
  288. mutex_lock(&chan->indio_dev->info_exist_lock);
  289. if (chan->indio_dev->info == NULL) {
  290. ret = -ENODEV;
  291. goto err_unlock;
  292. }
  293. if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
  294. ret = iio_channel_read(chan, val, NULL,
  295. IIO_CHAN_INFO_PROCESSED);
  296. } else {
  297. ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
  298. if (ret < 0)
  299. goto err_unlock;
  300. ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
  301. }
  302. err_unlock:
  303. mutex_unlock(&chan->indio_dev->info_exist_lock);
  304. return ret;
  305. }
  306. EXPORT_SYMBOL_GPL(iio_read_channel_processed);
  307. int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
  308. {
  309. int ret;
  310. mutex_lock(&chan->indio_dev->info_exist_lock);
  311. if (chan->indio_dev->info == NULL) {
  312. ret = -ENODEV;
  313. goto err_unlock;
  314. }
  315. ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
  316. err_unlock:
  317. mutex_unlock(&chan->indio_dev->info_exist_lock);
  318. return ret;
  319. }
  320. EXPORT_SYMBOL_GPL(iio_read_channel_scale);
  321. int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
  322. {
  323. int ret = 0;
  324. /* Need to verify underlying driver has not gone away */
  325. mutex_lock(&chan->indio_dev->info_exist_lock);
  326. if (chan->indio_dev->info == NULL) {
  327. ret = -ENODEV;
  328. goto err_unlock;
  329. }
  330. *type = chan->channel->type;
  331. err_unlock:
  332. mutex_unlock(&chan->indio_dev->info_exist_lock);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL_GPL(iio_get_channel_type);