inkern.c 9.1 KB

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