2 * IIO multiplexer driver
4 * Copyright (C) 2017 Axentia Technologies AB
6 * Author: Peter Rosin <peda@axentia.se>
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.
13 #include <linux/err.h>
14 #include <linux/iio/consumer.h>
15 #include <linux/iio/iio.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/mux/consumer.h>
20 #include <linux/platform_device.h>
22 struct mux_ext_info_cache
{
28 struct mux_ext_info_cache
*ext_info_cache
;
33 struct mux_control
*control
;
34 struct iio_channel
*parent
;
35 struct iio_dev
*indio_dev
;
36 struct iio_chan_spec
*chan
;
37 struct iio_chan_spec_ext_info
*ext_info
;
38 struct mux_child
*child
;
41 static int iio_mux_select(struct mux
*mux
, int idx
)
43 struct mux_child
*child
= &mux
->child
[idx
];
44 struct iio_chan_spec
const *chan
= &mux
->chan
[idx
];
48 ret
= mux_control_select(mux
->control
, chan
->channel
);
50 mux
->cached_state
= -1;
54 if (mux
->cached_state
== chan
->channel
)
58 for (i
= 0; chan
->ext_info
[i
].name
; ++i
) {
59 const char *attr
= chan
->ext_info
[i
].name
;
60 struct mux_ext_info_cache
*cache
;
62 cache
= &child
->ext_info_cache
[i
];
67 ret
= iio_write_channel_ext_info(mux
->parent
, attr
,
72 mux_control_deselect(mux
->control
);
73 mux
->cached_state
= -1;
78 mux
->cached_state
= chan
->channel
;
83 static void iio_mux_deselect(struct mux
*mux
)
85 mux_control_deselect(mux
->control
);
88 static int mux_read_raw(struct iio_dev
*indio_dev
,
89 struct iio_chan_spec
const *chan
,
90 int *val
, int *val2
, long mask
)
92 struct mux
*mux
= iio_priv(indio_dev
);
93 int idx
= chan
- mux
->chan
;
96 ret
= iio_mux_select(mux
, idx
);
101 case IIO_CHAN_INFO_RAW
:
102 ret
= iio_read_channel_raw(mux
->parent
, val
);
105 case IIO_CHAN_INFO_SCALE
:
106 ret
= iio_read_channel_scale(mux
->parent
, val
, val2
);
113 iio_mux_deselect(mux
);
118 static int mux_read_avail(struct iio_dev
*indio_dev
,
119 struct iio_chan_spec
const *chan
,
120 const int **vals
, int *type
, int *length
,
123 struct mux
*mux
= iio_priv(indio_dev
);
124 int idx
= chan
- mux
->chan
;
127 ret
= iio_mux_select(mux
, idx
);
132 case IIO_CHAN_INFO_RAW
:
134 ret
= iio_read_avail_channel_raw(mux
->parent
, vals
, length
);
141 iio_mux_deselect(mux
);
146 static int mux_write_raw(struct iio_dev
*indio_dev
,
147 struct iio_chan_spec
const *chan
,
148 int val
, int val2
, long mask
)
150 struct mux
*mux
= iio_priv(indio_dev
);
151 int idx
= chan
- mux
->chan
;
154 ret
= iio_mux_select(mux
, idx
);
159 case IIO_CHAN_INFO_RAW
:
160 ret
= iio_write_channel_raw(mux
->parent
, val
);
167 iio_mux_deselect(mux
);
172 static const struct iio_info mux_info
= {
173 .read_raw
= mux_read_raw
,
174 .read_avail
= mux_read_avail
,
175 .write_raw
= mux_write_raw
,
178 static ssize_t
mux_read_ext_info(struct iio_dev
*indio_dev
, uintptr_t private,
179 struct iio_chan_spec
const *chan
, char *buf
)
181 struct mux
*mux
= iio_priv(indio_dev
);
182 int idx
= chan
- mux
->chan
;
185 ret
= iio_mux_select(mux
, idx
);
189 ret
= iio_read_channel_ext_info(mux
->parent
,
190 mux
->ext_info
[private].name
,
193 iio_mux_deselect(mux
);
198 static ssize_t
mux_write_ext_info(struct iio_dev
*indio_dev
, uintptr_t private,
199 struct iio_chan_spec
const *chan
,
200 const char *buf
, size_t len
)
202 struct device
*dev
= indio_dev
->dev
.parent
;
203 struct mux
*mux
= iio_priv(indio_dev
);
204 int idx
= chan
- mux
->chan
;
208 if (len
>= PAGE_SIZE
)
211 ret
= iio_mux_select(mux
, idx
);
215 new = devm_kmemdup(dev
, buf
, len
+ 1, GFP_KERNEL
);
217 iio_mux_deselect(mux
);
223 ret
= iio_write_channel_ext_info(mux
->parent
,
224 mux
->ext_info
[private].name
,
227 iio_mux_deselect(mux
);
228 devm_kfree(dev
, new);
232 devm_kfree(dev
, mux
->child
[idx
].ext_info_cache
[private].data
);
233 mux
->child
[idx
].ext_info_cache
[private].data
= new;
234 mux
->child
[idx
].ext_info_cache
[private].size
= len
;
236 iio_mux_deselect(mux
);
241 static int mux_configure_channel(struct device
*dev
, struct mux
*mux
,
242 u32 state
, const char *label
, int idx
)
244 struct mux_child
*child
= &mux
->child
[idx
];
245 struct iio_chan_spec
*chan
= &mux
->chan
[idx
];
246 struct iio_chan_spec
const *pchan
= mux
->parent
->channel
;
253 chan
->output
= pchan
->output
;
254 chan
->datasheet_name
= label
;
255 chan
->ext_info
= mux
->ext_info
;
257 ret
= iio_get_channel_type(mux
->parent
, &chan
->type
);
259 dev_err(dev
, "failed to get parent channel type\n");
263 if (iio_channel_has_info(pchan
, IIO_CHAN_INFO_RAW
))
264 chan
->info_mask_separate
|= BIT(IIO_CHAN_INFO_RAW
);
265 if (iio_channel_has_info(pchan
, IIO_CHAN_INFO_SCALE
))
266 chan
->info_mask_separate
|= BIT(IIO_CHAN_INFO_SCALE
);
268 if (iio_channel_has_available(pchan
, IIO_CHAN_INFO_RAW
))
269 chan
->info_mask_separate_available
|= BIT(IIO_CHAN_INFO_RAW
);
271 if (state
>= mux_control_states(mux
->control
)) {
272 dev_err(dev
, "too many channels\n");
276 chan
->channel
= state
;
278 num_ext_info
= iio_get_channel_ext_info_count(mux
->parent
);
280 page
= devm_kzalloc(dev
, PAGE_SIZE
, GFP_KERNEL
);
284 child
->ext_info_cache
= devm_kzalloc(dev
,
285 sizeof(*child
->ext_info_cache
) *
286 num_ext_info
, GFP_KERNEL
);
287 if (!child
->ext_info_cache
)
290 for (i
= 0; i
< num_ext_info
; ++i
) {
291 child
->ext_info_cache
[i
].size
= -1;
293 if (!pchan
->ext_info
[i
].write
)
295 if (!pchan
->ext_info
[i
].read
)
298 ret
= iio_read_channel_ext_info(mux
->parent
,
299 mux
->ext_info
[i
].name
,
302 dev_err(dev
, "failed to get ext_info '%s'\n",
303 pchan
->ext_info
[i
].name
);
306 if (ret
>= PAGE_SIZE
) {
307 dev_err(dev
, "too large ext_info '%s'\n",
308 pchan
->ext_info
[i
].name
);
312 child
->ext_info_cache
[i
].data
= devm_kmemdup(dev
, page
, ret
+ 1,
314 if (!child
->ext_info_cache
[i
].data
)
317 child
->ext_info_cache
[i
].data
[ret
] = 0;
318 child
->ext_info_cache
[i
].size
= ret
;
322 devm_kfree(dev
, page
);
328 * Same as of_property_for_each_string(), but also keeps track of the
329 * index of each string.
331 #define of_property_for_each_string_index(np, propname, prop, s, i) \
332 for (prop = of_find_property(np, propname, NULL), \
333 s = of_prop_next_string(prop, NULL), \
336 s = of_prop_next_string(prop, s), \
339 static int mux_probe(struct platform_device
*pdev
)
341 struct device
*dev
= &pdev
->dev
;
342 struct device_node
*np
= pdev
->dev
.of_node
;
343 struct iio_dev
*indio_dev
;
344 struct iio_channel
*parent
;
346 struct property
*prop
;
358 parent
= devm_iio_channel_get(dev
, "parent");
359 if (IS_ERR(parent
)) {
360 if (PTR_ERR(parent
) != -EPROBE_DEFER
)
361 dev_err(dev
, "failed to get parent channel\n");
362 return PTR_ERR(parent
);
365 sizeof_ext_info
= iio_get_channel_ext_info_count(parent
);
366 if (sizeof_ext_info
) {
367 sizeof_ext_info
+= 1; /* one extra entry for the sentinel */
368 sizeof_ext_info
*= sizeof(*mux
->ext_info
);
372 of_property_for_each_string(np
, "channels", prop
, label
) {
377 dev_err(dev
, "not even a single child\n");
381 sizeof_priv
= sizeof(*mux
);
382 sizeof_priv
+= sizeof(*mux
->child
) * children
;
383 sizeof_priv
+= sizeof(*mux
->chan
) * children
;
384 sizeof_priv
+= sizeof_ext_info
;
386 indio_dev
= devm_iio_device_alloc(dev
, sizeof_priv
);
390 mux
= iio_priv(indio_dev
);
391 mux
->child
= (struct mux_child
*)(mux
+ 1);
392 mux
->chan
= (struct iio_chan_spec
*)(mux
->child
+ children
);
394 platform_set_drvdata(pdev
, indio_dev
);
396 mux
->parent
= parent
;
397 mux
->cached_state
= -1;
399 indio_dev
->name
= dev_name(dev
);
400 indio_dev
->dev
.parent
= dev
;
401 indio_dev
->info
= &mux_info
;
402 indio_dev
->modes
= INDIO_DIRECT_MODE
;
403 indio_dev
->channels
= mux
->chan
;
404 indio_dev
->num_channels
= children
;
405 if (sizeof_ext_info
) {
406 mux
->ext_info
= devm_kmemdup(dev
,
407 parent
->channel
->ext_info
,
408 sizeof_ext_info
, GFP_KERNEL
);
412 for (i
= 0; mux
->ext_info
[i
].name
; ++i
) {
413 if (parent
->channel
->ext_info
[i
].read
)
414 mux
->ext_info
[i
].read
= mux_read_ext_info
;
415 if (parent
->channel
->ext_info
[i
].write
)
416 mux
->ext_info
[i
].write
= mux_write_ext_info
;
417 mux
->ext_info
[i
].private = i
;
421 mux
->control
= devm_mux_control_get(dev
, NULL
);
422 if (IS_ERR(mux
->control
)) {
423 if (PTR_ERR(mux
->control
) != -EPROBE_DEFER
)
424 dev_err(dev
, "failed to get control-mux\n");
425 return PTR_ERR(mux
->control
);
429 of_property_for_each_string_index(np
, "channels", prop
, label
, state
) {
433 ret
= mux_configure_channel(dev
, mux
, state
, label
, i
++);
438 ret
= devm_iio_device_register(dev
, indio_dev
);
440 dev_err(dev
, "failed to register iio device\n");
447 static const struct of_device_id mux_match
[] = {
448 { .compatible
= "io-channel-mux" },
451 MODULE_DEVICE_TABLE(of
, mux_match
);
453 static struct platform_driver mux_driver
= {
457 .of_match_table
= mux_match
,
460 module_platform_driver(mux_driver
);
462 MODULE_DESCRIPTION("IIO multiplexer driver");
463 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
464 MODULE_LICENSE("GPL v2");