inkern.c 9.1 KB

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