iio-mux.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * IIO multiplexer driver
  3. *
  4. * Copyright (C) 2017 Axentia Technologies AB
  5. *
  6. * Author: Peter Rosin <peda@axentia.se>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/iio/consumer.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/mux/consumer.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. struct mux_ext_info_cache {
  21. char *data;
  22. ssize_t size;
  23. };
  24. struct mux_child {
  25. struct mux_ext_info_cache *ext_info_cache;
  26. };
  27. struct mux {
  28. int cached_state;
  29. struct mux_control *control;
  30. struct iio_channel *parent;
  31. struct iio_dev *indio_dev;
  32. struct iio_chan_spec *chan;
  33. struct iio_chan_spec_ext_info *ext_info;
  34. struct mux_child *child;
  35. };
  36. static int iio_mux_select(struct mux *mux, int idx)
  37. {
  38. struct mux_child *child = &mux->child[idx];
  39. struct iio_chan_spec const *chan = &mux->chan[idx];
  40. int ret;
  41. int i;
  42. ret = mux_control_select(mux->control, chan->channel);
  43. if (ret < 0) {
  44. mux->cached_state = -1;
  45. return ret;
  46. }
  47. if (mux->cached_state == chan->channel)
  48. return 0;
  49. if (chan->ext_info) {
  50. for (i = 0; chan->ext_info[i].name; ++i) {
  51. const char *attr = chan->ext_info[i].name;
  52. struct mux_ext_info_cache *cache;
  53. cache = &child->ext_info_cache[i];
  54. if (cache->size < 0)
  55. continue;
  56. ret = iio_write_channel_ext_info(mux->parent, attr,
  57. cache->data,
  58. cache->size);
  59. if (ret < 0) {
  60. mux_control_deselect(mux->control);
  61. mux->cached_state = -1;
  62. return ret;
  63. }
  64. }
  65. }
  66. mux->cached_state = chan->channel;
  67. return 0;
  68. }
  69. static void iio_mux_deselect(struct mux *mux)
  70. {
  71. mux_control_deselect(mux->control);
  72. }
  73. static int mux_read_raw(struct iio_dev *indio_dev,
  74. struct iio_chan_spec const *chan,
  75. int *val, int *val2, long mask)
  76. {
  77. struct mux *mux = iio_priv(indio_dev);
  78. int idx = chan - mux->chan;
  79. int ret;
  80. ret = iio_mux_select(mux, idx);
  81. if (ret < 0)
  82. return ret;
  83. switch (mask) {
  84. case IIO_CHAN_INFO_RAW:
  85. ret = iio_read_channel_raw(mux->parent, val);
  86. break;
  87. case IIO_CHAN_INFO_SCALE:
  88. ret = iio_read_channel_scale(mux->parent, val, val2);
  89. break;
  90. default:
  91. ret = -EINVAL;
  92. }
  93. iio_mux_deselect(mux);
  94. return ret;
  95. }
  96. static int mux_read_avail(struct iio_dev *indio_dev,
  97. struct iio_chan_spec const *chan,
  98. const int **vals, int *type, int *length,
  99. long mask)
  100. {
  101. struct mux *mux = iio_priv(indio_dev);
  102. int idx = chan - mux->chan;
  103. int ret;
  104. ret = iio_mux_select(mux, idx);
  105. if (ret < 0)
  106. return ret;
  107. switch (mask) {
  108. case IIO_CHAN_INFO_RAW:
  109. *type = IIO_VAL_INT;
  110. ret = iio_read_avail_channel_raw(mux->parent, vals, length);
  111. break;
  112. default:
  113. ret = -EINVAL;
  114. }
  115. iio_mux_deselect(mux);
  116. return ret;
  117. }
  118. static int mux_write_raw(struct iio_dev *indio_dev,
  119. struct iio_chan_spec const *chan,
  120. int val, int val2, long mask)
  121. {
  122. struct mux *mux = iio_priv(indio_dev);
  123. int idx = chan - mux->chan;
  124. int ret;
  125. ret = iio_mux_select(mux, idx);
  126. if (ret < 0)
  127. return ret;
  128. switch (mask) {
  129. case IIO_CHAN_INFO_RAW:
  130. ret = iio_write_channel_raw(mux->parent, val);
  131. break;
  132. default:
  133. ret = -EINVAL;
  134. }
  135. iio_mux_deselect(mux);
  136. return ret;
  137. }
  138. static const struct iio_info mux_info = {
  139. .read_raw = mux_read_raw,
  140. .read_avail = mux_read_avail,
  141. .write_raw = mux_write_raw,
  142. };
  143. static ssize_t mux_read_ext_info(struct iio_dev *indio_dev, uintptr_t private,
  144. struct iio_chan_spec const *chan, char *buf)
  145. {
  146. struct mux *mux = iio_priv(indio_dev);
  147. int idx = chan - mux->chan;
  148. ssize_t ret;
  149. ret = iio_mux_select(mux, idx);
  150. if (ret < 0)
  151. return ret;
  152. ret = iio_read_channel_ext_info(mux->parent,
  153. mux->ext_info[private].name,
  154. buf);
  155. iio_mux_deselect(mux);
  156. return ret;
  157. }
  158. static ssize_t mux_write_ext_info(struct iio_dev *indio_dev, uintptr_t private,
  159. struct iio_chan_spec const *chan,
  160. const char *buf, size_t len)
  161. {
  162. struct device *dev = indio_dev->dev.parent;
  163. struct mux *mux = iio_priv(indio_dev);
  164. int idx = chan - mux->chan;
  165. char *new;
  166. ssize_t ret;
  167. if (len >= PAGE_SIZE)
  168. return -EINVAL;
  169. ret = iio_mux_select(mux, idx);
  170. if (ret < 0)
  171. return ret;
  172. new = devm_kmemdup(dev, buf, len + 1, GFP_KERNEL);
  173. if (!new) {
  174. iio_mux_deselect(mux);
  175. return -ENOMEM;
  176. }
  177. new[len] = 0;
  178. ret = iio_write_channel_ext_info(mux->parent,
  179. mux->ext_info[private].name,
  180. buf, len);
  181. if (ret < 0) {
  182. iio_mux_deselect(mux);
  183. devm_kfree(dev, new);
  184. return ret;
  185. }
  186. devm_kfree(dev, mux->child[idx].ext_info_cache[private].data);
  187. mux->child[idx].ext_info_cache[private].data = new;
  188. mux->child[idx].ext_info_cache[private].size = len;
  189. iio_mux_deselect(mux);
  190. return ret;
  191. }
  192. static int mux_configure_channel(struct device *dev, struct mux *mux,
  193. u32 state, const char *label, int idx)
  194. {
  195. struct mux_child *child = &mux->child[idx];
  196. struct iio_chan_spec *chan = &mux->chan[idx];
  197. struct iio_chan_spec const *pchan = mux->parent->channel;
  198. char *page = NULL;
  199. int num_ext_info;
  200. int i;
  201. int ret;
  202. chan->indexed = 1;
  203. chan->output = pchan->output;
  204. chan->datasheet_name = label;
  205. chan->ext_info = mux->ext_info;
  206. ret = iio_get_channel_type(mux->parent, &chan->type);
  207. if (ret < 0) {
  208. dev_err(dev, "failed to get parent channel type\n");
  209. return ret;
  210. }
  211. if (iio_channel_has_info(pchan, IIO_CHAN_INFO_RAW))
  212. chan->info_mask_separate |= BIT(IIO_CHAN_INFO_RAW);
  213. if (iio_channel_has_info(pchan, IIO_CHAN_INFO_SCALE))
  214. chan->info_mask_separate |= BIT(IIO_CHAN_INFO_SCALE);
  215. if (iio_channel_has_available(pchan, IIO_CHAN_INFO_RAW))
  216. chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);
  217. if (state >= mux_control_states(mux->control)) {
  218. dev_err(dev, "too many channels\n");
  219. return -EINVAL;
  220. }
  221. chan->channel = state;
  222. num_ext_info = iio_get_channel_ext_info_count(mux->parent);
  223. if (num_ext_info) {
  224. page = devm_kzalloc(dev, PAGE_SIZE, GFP_KERNEL);
  225. if (!page)
  226. return -ENOMEM;
  227. }
  228. child->ext_info_cache = devm_kzalloc(dev,
  229. sizeof(*child->ext_info_cache) *
  230. num_ext_info, GFP_KERNEL);
  231. if (!child->ext_info_cache)
  232. return -ENOMEM;
  233. for (i = 0; i < num_ext_info; ++i) {
  234. child->ext_info_cache[i].size = -1;
  235. if (!pchan->ext_info[i].write)
  236. continue;
  237. if (!pchan->ext_info[i].read)
  238. continue;
  239. ret = iio_read_channel_ext_info(mux->parent,
  240. mux->ext_info[i].name,
  241. page);
  242. if (ret < 0) {
  243. dev_err(dev, "failed to get ext_info '%s'\n",
  244. pchan->ext_info[i].name);
  245. return ret;
  246. }
  247. if (ret >= PAGE_SIZE) {
  248. dev_err(dev, "too large ext_info '%s'\n",
  249. pchan->ext_info[i].name);
  250. return -EINVAL;
  251. }
  252. child->ext_info_cache[i].data = devm_kmemdup(dev, page, ret + 1,
  253. GFP_KERNEL);
  254. if (!child->ext_info_cache[i].data)
  255. return -ENOMEM;
  256. child->ext_info_cache[i].data[ret] = 0;
  257. child->ext_info_cache[i].size = ret;
  258. }
  259. if (page)
  260. devm_kfree(dev, page);
  261. return 0;
  262. }
  263. /*
  264. * Same as of_property_for_each_string(), but also keeps track of the
  265. * index of each string.
  266. */
  267. #define of_property_for_each_string_index(np, propname, prop, s, i) \
  268. for (prop = of_find_property(np, propname, NULL), \
  269. s = of_prop_next_string(prop, NULL), \
  270. i = 0; \
  271. s; \
  272. s = of_prop_next_string(prop, s), \
  273. i++)
  274. static int mux_probe(struct platform_device *pdev)
  275. {
  276. struct device *dev = &pdev->dev;
  277. struct device_node *np = pdev->dev.of_node;
  278. struct iio_dev *indio_dev;
  279. struct iio_channel *parent;
  280. struct mux *mux;
  281. struct property *prop;
  282. const char *label;
  283. u32 state;
  284. int sizeof_ext_info;
  285. int children;
  286. int sizeof_priv;
  287. int i;
  288. int ret;
  289. if (!np)
  290. return -ENODEV;
  291. parent = devm_iio_channel_get(dev, "parent");
  292. if (IS_ERR(parent)) {
  293. if (PTR_ERR(parent) != -EPROBE_DEFER)
  294. dev_err(dev, "failed to get parent channel\n");
  295. return PTR_ERR(parent);
  296. }
  297. sizeof_ext_info = iio_get_channel_ext_info_count(parent);
  298. if (sizeof_ext_info) {
  299. sizeof_ext_info += 1; /* one extra entry for the sentinel */
  300. sizeof_ext_info *= sizeof(*mux->ext_info);
  301. }
  302. children = 0;
  303. of_property_for_each_string(np, "channels", prop, label) {
  304. if (*label)
  305. children++;
  306. }
  307. if (children <= 0) {
  308. dev_err(dev, "not even a single child\n");
  309. return -EINVAL;
  310. }
  311. sizeof_priv = sizeof(*mux);
  312. sizeof_priv += sizeof(*mux->child) * children;
  313. sizeof_priv += sizeof(*mux->chan) * children;
  314. sizeof_priv += sizeof_ext_info;
  315. indio_dev = devm_iio_device_alloc(dev, sizeof_priv);
  316. if (!indio_dev)
  317. return -ENOMEM;
  318. mux = iio_priv(indio_dev);
  319. mux->child = (struct mux_child *)(mux + 1);
  320. mux->chan = (struct iio_chan_spec *)(mux->child + children);
  321. platform_set_drvdata(pdev, indio_dev);
  322. mux->parent = parent;
  323. mux->cached_state = -1;
  324. indio_dev->name = dev_name(dev);
  325. indio_dev->dev.parent = dev;
  326. indio_dev->info = &mux_info;
  327. indio_dev->modes = INDIO_DIRECT_MODE;
  328. indio_dev->channels = mux->chan;
  329. indio_dev->num_channels = children;
  330. if (sizeof_ext_info) {
  331. mux->ext_info = devm_kmemdup(dev,
  332. parent->channel->ext_info,
  333. sizeof_ext_info, GFP_KERNEL);
  334. if (!mux->ext_info)
  335. return -ENOMEM;
  336. for (i = 0; mux->ext_info[i].name; ++i) {
  337. if (parent->channel->ext_info[i].read)
  338. mux->ext_info[i].read = mux_read_ext_info;
  339. if (parent->channel->ext_info[i].write)
  340. mux->ext_info[i].write = mux_write_ext_info;
  341. mux->ext_info[i].private = i;
  342. }
  343. }
  344. mux->control = devm_mux_control_get(dev, NULL);
  345. if (IS_ERR(mux->control)) {
  346. if (PTR_ERR(mux->control) != -EPROBE_DEFER)
  347. dev_err(dev, "failed to get control-mux\n");
  348. return PTR_ERR(mux->control);
  349. }
  350. i = 0;
  351. of_property_for_each_string_index(np, "channels", prop, label, state) {
  352. if (!*label)
  353. continue;
  354. ret = mux_configure_channel(dev, mux, state, label, i++);
  355. if (ret < 0)
  356. return ret;
  357. }
  358. ret = devm_iio_device_register(dev, indio_dev);
  359. if (ret) {
  360. dev_err(dev, "failed to register iio device\n");
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. static const struct of_device_id mux_match[] = {
  366. { .compatible = "io-channel-mux" },
  367. { /* sentinel */ }
  368. };
  369. MODULE_DEVICE_TABLE(of, mux_match);
  370. static struct platform_driver mux_driver = {
  371. .probe = mux_probe,
  372. .driver = {
  373. .name = "iio-mux",
  374. .of_match_table = mux_match,
  375. },
  376. };
  377. module_platform_driver(mux_driver);
  378. MODULE_DESCRIPTION("IIO multiplexer driver");
  379. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  380. MODULE_LICENSE("GPL v2");