Lines Matching +full:io +full:- +full:channel +full:- +full:mux
1 // SPDX-License-Identifier: GPL-2.0
16 #include <linux/mux/consumer.h>
29 struct mux { struct
39 static int iio_mux_select(struct mux *mux, int idx) in iio_mux_select() argument
41 struct mux_child *child = &mux->child[idx]; in iio_mux_select()
42 struct iio_chan_spec const *chan = &mux->chan[idx]; in iio_mux_select()
46 ret = mux_control_select_delay(mux->control, chan->channel, in iio_mux_select()
47 mux->delay_us); in iio_mux_select()
49 mux->cached_state = -1; in iio_mux_select()
53 if (mux->cached_state == chan->channel) in iio_mux_select()
56 if (chan->ext_info) { in iio_mux_select()
57 for (i = 0; chan->ext_info[i].name; ++i) { in iio_mux_select()
58 const char *attr = chan->ext_info[i].name; in iio_mux_select()
61 cache = &child->ext_info_cache[i]; in iio_mux_select()
63 if (cache->size < 0) in iio_mux_select()
66 ret = iio_write_channel_ext_info(mux->parent, attr, in iio_mux_select()
67 cache->data, in iio_mux_select()
68 cache->size); in iio_mux_select()
71 mux_control_deselect(mux->control); in iio_mux_select()
72 mux->cached_state = -1; in iio_mux_select()
77 mux->cached_state = chan->channel; in iio_mux_select()
82 static void iio_mux_deselect(struct mux *mux) in iio_mux_deselect() argument
84 mux_control_deselect(mux->control); in iio_mux_deselect()
91 struct mux *mux = iio_priv(indio_dev); in mux_read_raw() local
92 int idx = chan - mux->chan; in mux_read_raw()
95 ret = iio_mux_select(mux, idx); in mux_read_raw()
101 ret = iio_read_channel_raw(mux->parent, val); in mux_read_raw()
105 ret = iio_read_channel_scale(mux->parent, val, val2); in mux_read_raw()
109 ret = -EINVAL; in mux_read_raw()
112 iio_mux_deselect(mux); in mux_read_raw()
122 struct mux *mux = iio_priv(indio_dev); in mux_read_avail() local
123 int idx = chan - mux->chan; in mux_read_avail()
126 ret = iio_mux_select(mux, idx); in mux_read_avail()
133 ret = iio_read_avail_channel_raw(mux->parent, vals, length); in mux_read_avail()
137 ret = -EINVAL; in mux_read_avail()
140 iio_mux_deselect(mux); in mux_read_avail()
149 struct mux *mux = iio_priv(indio_dev); in mux_write_raw() local
150 int idx = chan - mux->chan; in mux_write_raw()
153 ret = iio_mux_select(mux, idx); in mux_write_raw()
159 ret = iio_write_channel_raw(mux->parent, val); in mux_write_raw()
163 ret = -EINVAL; in mux_write_raw()
166 iio_mux_deselect(mux); in mux_write_raw()
180 struct mux *mux = iio_priv(indio_dev); in mux_read_ext_info() local
181 int idx = chan - mux->chan; in mux_read_ext_info()
184 ret = iio_mux_select(mux, idx); in mux_read_ext_info()
188 ret = iio_read_channel_ext_info(mux->parent, in mux_read_ext_info()
189 mux->ext_info[private].name, in mux_read_ext_info()
192 iio_mux_deselect(mux); in mux_read_ext_info()
201 struct device *dev = indio_dev->dev.parent; in mux_write_ext_info()
202 struct mux *mux = iio_priv(indio_dev); in mux_write_ext_info() local
203 int idx = chan - mux->chan; in mux_write_ext_info()
208 return -EINVAL; in mux_write_ext_info()
210 ret = iio_mux_select(mux, idx); in mux_write_ext_info()
216 iio_mux_deselect(mux); in mux_write_ext_info()
217 return -ENOMEM; in mux_write_ext_info()
222 ret = iio_write_channel_ext_info(mux->parent, in mux_write_ext_info()
223 mux->ext_info[private].name, in mux_write_ext_info()
226 iio_mux_deselect(mux); in mux_write_ext_info()
231 devm_kfree(dev, mux->child[idx].ext_info_cache[private].data); in mux_write_ext_info()
232 mux->child[idx].ext_info_cache[private].data = new; in mux_write_ext_info()
233 mux->child[idx].ext_info_cache[private].size = len; in mux_write_ext_info()
235 iio_mux_deselect(mux); in mux_write_ext_info()
240 static int mux_configure_channel(struct device *dev, struct mux *mux, in mux_configure_channel() argument
243 struct mux_child *child = &mux->child[idx]; in mux_configure_channel()
244 struct iio_chan_spec *chan = &mux->chan[idx]; in mux_configure_channel()
245 struct iio_chan_spec const *pchan = mux->parent->channel; in mux_configure_channel()
251 chan->indexed = 1; in mux_configure_channel()
252 chan->output = pchan->output; in mux_configure_channel()
253 chan->datasheet_name = label; in mux_configure_channel()
254 chan->ext_info = mux->ext_info; in mux_configure_channel()
256 ret = iio_get_channel_type(mux->parent, &chan->type); in mux_configure_channel()
258 dev_err(dev, "failed to get parent channel type\n"); in mux_configure_channel()
263 chan->info_mask_separate |= BIT(IIO_CHAN_INFO_RAW); in mux_configure_channel()
265 chan->info_mask_separate |= BIT(IIO_CHAN_INFO_SCALE); in mux_configure_channel()
268 chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW); in mux_configure_channel()
270 if (state >= mux_control_states(mux->control)) { in mux_configure_channel()
272 return -EINVAL; in mux_configure_channel()
275 chan->channel = state; in mux_configure_channel()
277 num_ext_info = iio_get_channel_ext_info_count(mux->parent); in mux_configure_channel()
281 return -ENOMEM; in mux_configure_channel()
283 child->ext_info_cache = devm_kcalloc(dev, in mux_configure_channel()
285 sizeof(*child->ext_info_cache), in mux_configure_channel()
287 if (!child->ext_info_cache) in mux_configure_channel()
288 return -ENOMEM; in mux_configure_channel()
291 child->ext_info_cache[i].size = -1; in mux_configure_channel()
293 if (!pchan->ext_info[i].write) in mux_configure_channel()
295 if (!pchan->ext_info[i].read) in mux_configure_channel()
298 ret = iio_read_channel_ext_info(mux->parent, in mux_configure_channel()
299 mux->ext_info[i].name, in mux_configure_channel()
303 pchan->ext_info[i].name); in mux_configure_channel()
308 pchan->ext_info[i].name); in mux_configure_channel()
309 return -EINVAL; in mux_configure_channel()
312 child->ext_info_cache[i].data = devm_kmemdup(dev, page, ret + 1, in mux_configure_channel()
314 if (!child->ext_info_cache[i].data) in mux_configure_channel()
315 return -ENOMEM; in mux_configure_channel()
317 child->ext_info_cache[i].data[ret] = 0; in mux_configure_channel()
318 child->ext_info_cache[i].size = ret; in mux_configure_channel()
329 struct device *dev = &pdev->dev; in mux_probe()
332 struct mux *mux; in mux_probe() local
345 "failed to get parent channel\n"); in mux_probe()
350 sizeof_ext_info *= sizeof(*mux->ext_info); in mux_probe()
359 return -ENOMEM; in mux_probe()
372 return -EINVAL; in mux_probe()
375 sizeof_priv = sizeof(*mux); in mux_probe()
376 sizeof_priv += sizeof(*mux->child) * children; in mux_probe()
377 sizeof_priv += sizeof(*mux->chan) * children; in mux_probe()
382 return -ENOMEM; in mux_probe()
384 mux = iio_priv(indio_dev); in mux_probe()
385 mux->child = (struct mux_child *)(mux + 1); in mux_probe()
386 mux->chan = (struct iio_chan_spec *)(mux->child + children); in mux_probe()
390 mux->parent = parent; in mux_probe()
391 mux->cached_state = -1; in mux_probe()
393 mux->delay_us = 0; in mux_probe()
394 device_property_read_u32(dev, "settle-time-us", &mux->delay_us); in mux_probe()
396 indio_dev->name = dev_name(dev); in mux_probe()
397 indio_dev->info = &mux_info; in mux_probe()
398 indio_dev->modes = INDIO_DIRECT_MODE; in mux_probe()
399 indio_dev->channels = mux->chan; in mux_probe()
400 indio_dev->num_channels = children; in mux_probe()
402 mux->ext_info = devm_kmemdup(dev, in mux_probe()
403 parent->channel->ext_info, in mux_probe()
405 if (!mux->ext_info) in mux_probe()
406 return -ENOMEM; in mux_probe()
408 for (i = 0; mux->ext_info[i].name; ++i) { in mux_probe()
409 if (parent->channel->ext_info[i].read) in mux_probe()
410 mux->ext_info[i].read = mux_read_ext_info; in mux_probe()
411 if (parent->channel->ext_info[i].write) in mux_probe()
412 mux->ext_info[i].write = mux_write_ext_info; in mux_probe()
413 mux->ext_info[i].private = i; in mux_probe()
417 mux->control = devm_mux_control_get(dev, NULL); in mux_probe()
418 if (IS_ERR(mux->control)) in mux_probe()
419 return dev_err_probe(dev, PTR_ERR(mux->control), in mux_probe()
420 "failed to get control-mux\n"); in mux_probe()
427 ret = mux_configure_channel(dev, mux, state, labels[state], i++); in mux_probe()
442 { .compatible = "io-channel-mux" },
450 .name = "iio-mux",