iio-mux.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. .driver_module = THIS_MODULE,
  143. };
  144. static ssize_t mux_read_ext_info(struct iio_dev *indio_dev, uintptr_t private,
  145. struct iio_chan_spec const *chan, char *buf)
  146. {
  147. struct mux *mux = iio_priv(indio_dev);
  148. int idx = chan - mux->chan;
  149. ssize_t ret;
  150. ret = iio_mux_select(mux, idx);
  151. if (ret < 0)
  152. return ret;
  153. ret = iio_read_channel_ext_info(mux->parent,
  154. mux->ext_info[private].name,
  155. buf);
  156. iio_mux_deselect(mux);
  157. return ret;
  158. }
  159. static ssize_t mux_write_ext_info(struct iio_dev *indio_dev, uintptr_t private,
  160. struct iio_chan_spec const *chan,
  161. const char *buf, size_t len)
  162. {
  163. struct device *dev = indio_dev->dev.parent;
  164. struct mux *mux = iio_priv(indio_dev);
  165. int idx = chan - mux->chan;
  166. char *new;
  167. ssize_t ret;
  168. if (len >= PAGE_SIZE)
  169. return -EINVAL;
  170. ret = iio_mux_select(mux, idx);
  171. if (ret < 0)
  172. return ret;
  173. new = devm_kmemdup(dev, buf, len + 1, GFP_KERNEL);
  174. if (!new) {
  175. iio_mux_deselect(mux);
  176. return -ENOMEM;
  177. }
  178. new[len] = 0;
  179. ret = iio_write_channel_ext_info(mux->parent,
  180. mux->ext_info[private].name,
  181. buf, len);
  182. if (ret < 0) {
  183. iio_mux_deselect(mux);
  184. devm_kfree(dev, new);
  185. return ret;
  186. }
  187. devm_kfree(dev, mux->child[idx].ext_info_cache[private].data);
  188. mux->child[idx].ext_info_cache[private].data = new;
  189. mux->child[idx].ext_info_cache[private].size = len;
  190. iio_mux_deselect(mux);
  191. return ret;
  192. }
  193. static int mux_configure_channel(struct device *dev, struct mux *mux,
  194. u32 state, const char *label, int idx)
  195. {
  196. struct mux_child *child = &mux->child[idx];
  197. struct iio_chan_spec *chan = &mux->chan[idx];
  198. struct iio_chan_spec const *pchan = mux->parent->channel;
  199. char *page = NULL;
  200. int num_ext_info;
  201. int i;
  202. int ret;
  203. chan->indexed = 1;
  204. chan->output = pchan->output;
  205. chan->datasheet_name = label;
  206. chan->ext_info = mux->ext_info;
  207. ret = iio_get_channel_type(mux->parent, &chan->type);
  208. if (ret < 0) {
  209. dev_err(dev, "failed to get parent channel type\n");
  210. return ret;
  211. }
  212. if (iio_channel_has_info(pchan, IIO_CHAN_INFO_RAW))
  213. chan->info_mask_separate |= BIT(IIO_CHAN_INFO_RAW);
  214. if (iio_channel_has_info(pchan, IIO_CHAN_INFO_SCALE))
  215. chan->info_mask_separate |= BIT(IIO_CHAN_INFO_SCALE);
  216. if (iio_channel_has_available(pchan, IIO_CHAN_INFO_RAW))
  217. chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW);
  218. if (state >= mux_control_states(mux->control)) {
  219. dev_err(dev, "too many channels\n");
  220. return -EINVAL;
  221. }
  222. chan->channel = state;
  223. num_ext_info = iio_get_channel_ext_info_count(mux->parent);
  224. if (num_ext_info) {
  225. page = devm_kzalloc(dev, PAGE_SIZE, GFP_KERNEL);
  226. if (!page)
  227. return -ENOMEM;
  228. }
  229. child->ext_info_cache = devm_kzalloc(dev,
  230. sizeof(*child->ext_info_cache) *
  231. num_ext_info, GFP_KERNEL);
  232. for (i = 0; i < num_ext_info; ++i) {
  233. child->ext_info_cache[i].size = -1;
  234. if (!pchan->ext_info[i].write)
  235. continue;
  236. if (!pchan->ext_info[i].read)
  237. continue;
  238. ret = iio_read_channel_ext_info(mux->parent,
  239. mux->ext_info[i].name,
  240. page);
  241. if (ret < 0) {
  242. dev_err(dev, "failed to get ext_info '%s'\n",
  243. pchan->ext_info[i].name);
  244. return ret;
  245. }
  246. if (ret >= PAGE_SIZE) {
  247. dev_err(dev, "too large ext_info '%s'\n",
  248. pchan->ext_info[i].name);
  249. return -EINVAL;
  250. }
  251. child->ext_info_cache[i].data = devm_kmemdup(dev, page, ret + 1,
  252. GFP_KERNEL);
  253. child->ext_info_cache[i].data[ret] = 0;
  254. child->ext_info_cache[i].size = ret;
  255. }
  256. if (page)
  257. devm_kfree(dev, page);
  258. return 0;
  259. }
  260. /*
  261. * Same as of_property_for_each_string(), but also keeps track of the
  262. * index of each string.
  263. */
  264. #define of_property_for_each_string_index(np, propname, prop, s, i) \
  265. for (prop = of_find_property(np, propname, NULL), \
  266. s = of_prop_next_string(prop, NULL), \
  267. i = 0; \
  268. s; \
  269. s = of_prop_next_string(prop, s), \
  270. i++)
  271. static int mux_probe(struct platform_device *pdev)
  272. {
  273. struct device *dev = &pdev->dev;
  274. struct device_node *np = pdev->dev.of_node;
  275. struct iio_dev *indio_dev;
  276. struct iio_channel *parent;
  277. struct mux *mux;
  278. struct property *prop;
  279. const char *label;
  280. u32 state;
  281. int sizeof_ext_info;
  282. int children;
  283. int sizeof_priv;
  284. int i;
  285. int ret;
  286. if (!np)
  287. return -ENODEV;
  288. parent = devm_iio_channel_get(dev, "parent");
  289. if (IS_ERR(parent)) {
  290. if (PTR_ERR(parent) != -EPROBE_DEFER)
  291. dev_err(dev, "failed to get parent channel\n");
  292. return PTR_ERR(parent);
  293. }
  294. sizeof_ext_info = iio_get_channel_ext_info_count(parent);
  295. if (sizeof_ext_info) {
  296. sizeof_ext_info += 1; /* one extra entry for the sentinel */
  297. sizeof_ext_info *= sizeof(*mux->ext_info);
  298. }
  299. children = 0;
  300. of_property_for_each_string(np, "channels", prop, label) {
  301. if (*label)
  302. children++;
  303. }
  304. if (children <= 0) {
  305. dev_err(dev, "not even a single child\n");
  306. return -EINVAL;
  307. }
  308. sizeof_priv = sizeof(*mux);
  309. sizeof_priv += sizeof(*mux->child) * children;
  310. sizeof_priv += sizeof(*mux->chan) * children;
  311. sizeof_priv += sizeof_ext_info;
  312. indio_dev = devm_iio_device_alloc(dev, sizeof_priv);
  313. if (!indio_dev)
  314. return -ENOMEM;
  315. mux = iio_priv(indio_dev);
  316. mux->child = (struct mux_child *)(mux + 1);
  317. mux->chan = (struct iio_chan_spec *)(mux->child + children);
  318. platform_set_drvdata(pdev, indio_dev);
  319. mux->parent = parent;
  320. mux->cached_state = -1;
  321. indio_dev->name = dev_name(dev);
  322. indio_dev->dev.parent = dev;
  323. indio_dev->info = &mux_info;
  324. indio_dev->modes = INDIO_DIRECT_MODE;
  325. indio_dev->channels = mux->chan;
  326. indio_dev->num_channels = children;
  327. if (sizeof_ext_info) {
  328. mux->ext_info = devm_kmemdup(dev,
  329. parent->channel->ext_info,
  330. sizeof_ext_info, GFP_KERNEL);
  331. if (!mux->ext_info)
  332. return -ENOMEM;
  333. for (i = 0; mux->ext_info[i].name; ++i) {
  334. if (parent->channel->ext_info[i].read)
  335. mux->ext_info[i].read = mux_read_ext_info;
  336. if (parent->channel->ext_info[i].write)
  337. mux->ext_info[i].write = mux_write_ext_info;
  338. mux->ext_info[i].private = i;
  339. }
  340. }
  341. mux->control = devm_mux_control_get(dev, NULL);
  342. if (IS_ERR(mux->control)) {
  343. if (PTR_ERR(mux->control) != -EPROBE_DEFER)
  344. dev_err(dev, "failed to get control-mux\n");
  345. return PTR_ERR(mux->control);
  346. }
  347. i = 0;
  348. of_property_for_each_string_index(np, "channels", prop, label, state) {
  349. if (!*label)
  350. continue;
  351. ret = mux_configure_channel(dev, mux, state, label, i++);
  352. if (ret < 0)
  353. return ret;
  354. }
  355. ret = devm_iio_device_register(dev, indio_dev);
  356. if (ret) {
  357. dev_err(dev, "failed to register iio device\n");
  358. return ret;
  359. }
  360. return 0;
  361. }
  362. static const struct of_device_id mux_match[] = {
  363. { .compatible = "io-channel-mux" },
  364. { /* sentinel */ }
  365. };
  366. MODULE_DEVICE_TABLE(of, mux_match);
  367. static struct platform_driver mux_driver = {
  368. .probe = mux_probe,
  369. .driver = {
  370. .name = "iio-mux",
  371. .of_match_table = mux_match,
  372. },
  373. };
  374. module_platform_driver(mux_driver);
  375. MODULE_DESCRIPTION("IIO multiplexer driver");
  376. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  377. MODULE_LICENSE("GPL v2");