1 // SPDX-License-Identifier: GPL-2.0
3 * IIO multiplexer driver
5 * Copyright (C) 2017 Axentia Technologies AB
7 * Author: Peter Rosin <peda@axentia.se>
10 #include <linux/err.h>
11 #include <linux/iio/consumer.h>
12 #include <linux/iio/iio.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/mux/consumer.h>
17 #include <linux/platform_device.h>
19 struct mux_ext_info_cache
{
25 struct mux_ext_info_cache
*ext_info_cache
;
30 struct mux_control
*control
;
31 struct iio_channel
*parent
;
32 struct iio_dev
*indio_dev
;
33 struct iio_chan_spec
*chan
;
34 struct iio_chan_spec_ext_info
*ext_info
;
35 struct mux_child
*child
;
38 static int iio_mux_select(struct mux
*mux
, int idx
)
40 struct mux_child
*child
= &mux
->child
[idx
];
41 struct iio_chan_spec
const *chan
= &mux
->chan
[idx
];
45 ret
= mux_control_select(mux
->control
, chan
->channel
);
47 mux
->cached_state
= -1;
51 if (mux
->cached_state
== chan
->channel
)
55 for (i
= 0; chan
->ext_info
[i
].name
; ++i
) {
56 const char *attr
= chan
->ext_info
[i
].name
;
57 struct mux_ext_info_cache
*cache
;
59 cache
= &child
->ext_info_cache
[i
];
64 ret
= iio_write_channel_ext_info(mux
->parent
, attr
,
69 mux_control_deselect(mux
->control
);
70 mux
->cached_state
= -1;
75 mux
->cached_state
= chan
->channel
;
80 static void iio_mux_deselect(struct mux
*mux
)
82 mux_control_deselect(mux
->control
);
85 static int mux_read_raw(struct iio_dev
*indio_dev
,
86 struct iio_chan_spec
const *chan
,
87 int *val
, int *val2
, long mask
)
89 struct mux
*mux
= iio_priv(indio_dev
);
90 int idx
= chan
- mux
->chan
;
93 ret
= iio_mux_select(mux
, idx
);
98 case IIO_CHAN_INFO_RAW
:
99 ret
= iio_read_channel_raw(mux
->parent
, val
);
102 case IIO_CHAN_INFO_SCALE
:
103 ret
= iio_read_channel_scale(mux
->parent
, val
, val2
);
110 iio_mux_deselect(mux
);
115 static int mux_read_avail(struct iio_dev
*indio_dev
,
116 struct iio_chan_spec
const *chan
,
117 const int **vals
, int *type
, int *length
,
120 struct mux
*mux
= iio_priv(indio_dev
);
121 int idx
= chan
- mux
->chan
;
124 ret
= iio_mux_select(mux
, idx
);
129 case IIO_CHAN_INFO_RAW
:
131 ret
= iio_read_avail_channel_raw(mux
->parent
, vals
, length
);
138 iio_mux_deselect(mux
);
143 static int mux_write_raw(struct iio_dev
*indio_dev
,
144 struct iio_chan_spec
const *chan
,
145 int val
, int val2
, long mask
)
147 struct mux
*mux
= iio_priv(indio_dev
);
148 int idx
= chan
- mux
->chan
;
151 ret
= iio_mux_select(mux
, idx
);
156 case IIO_CHAN_INFO_RAW
:
157 ret
= iio_write_channel_raw(mux
->parent
, val
);
164 iio_mux_deselect(mux
);
169 static const struct iio_info mux_info
= {
170 .read_raw
= mux_read_raw
,
171 .read_avail
= mux_read_avail
,
172 .write_raw
= mux_write_raw
,
175 static ssize_t
mux_read_ext_info(struct iio_dev
*indio_dev
, uintptr_t private,
176 struct iio_chan_spec
const *chan
, char *buf
)
178 struct mux
*mux
= iio_priv(indio_dev
);
179 int idx
= chan
- mux
->chan
;
182 ret
= iio_mux_select(mux
, idx
);
186 ret
= iio_read_channel_ext_info(mux
->parent
,
187 mux
->ext_info
[private].name
,
190 iio_mux_deselect(mux
);
195 static ssize_t
mux_write_ext_info(struct iio_dev
*indio_dev
, uintptr_t private,
196 struct iio_chan_spec
const *chan
,
197 const char *buf
, size_t len
)
199 struct device
*dev
= indio_dev
->dev
.parent
;
200 struct mux
*mux
= iio_priv(indio_dev
);
201 int idx
= chan
- mux
->chan
;
205 if (len
>= PAGE_SIZE
)
208 ret
= iio_mux_select(mux
, idx
);
212 new = devm_kmemdup(dev
, buf
, len
+ 1, GFP_KERNEL
);
214 iio_mux_deselect(mux
);
220 ret
= iio_write_channel_ext_info(mux
->parent
,
221 mux
->ext_info
[private].name
,
224 iio_mux_deselect(mux
);
225 devm_kfree(dev
, new);
229 devm_kfree(dev
, mux
->child
[idx
].ext_info_cache
[private].data
);
230 mux
->child
[idx
].ext_info_cache
[private].data
= new;
231 mux
->child
[idx
].ext_info_cache
[private].size
= len
;
233 iio_mux_deselect(mux
);
238 static int mux_configure_channel(struct device
*dev
, struct mux
*mux
,
239 u32 state
, const char *label
, int idx
)
241 struct mux_child
*child
= &mux
->child
[idx
];
242 struct iio_chan_spec
*chan
= &mux
->chan
[idx
];
243 struct iio_chan_spec
const *pchan
= mux
->parent
->channel
;
250 chan
->output
= pchan
->output
;
251 chan
->datasheet_name
= label
;
252 chan
->ext_info
= mux
->ext_info
;
254 ret
= iio_get_channel_type(mux
->parent
, &chan
->type
);
256 dev_err(dev
, "failed to get parent channel type\n");
260 if (iio_channel_has_info(pchan
, IIO_CHAN_INFO_RAW
))
261 chan
->info_mask_separate
|= BIT(IIO_CHAN_INFO_RAW
);
262 if (iio_channel_has_info(pchan
, IIO_CHAN_INFO_SCALE
))
263 chan
->info_mask_separate
|= BIT(IIO_CHAN_INFO_SCALE
);
265 if (iio_channel_has_available(pchan
, IIO_CHAN_INFO_RAW
))
266 chan
->info_mask_separate_available
|= BIT(IIO_CHAN_INFO_RAW
);
268 if (state
>= mux_control_states(mux
->control
)) {
269 dev_err(dev
, "too many channels\n");
273 chan
->channel
= state
;
275 num_ext_info
= iio_get_channel_ext_info_count(mux
->parent
);
277 page
= devm_kzalloc(dev
, PAGE_SIZE
, GFP_KERNEL
);
281 child
->ext_info_cache
= devm_kcalloc(dev
,
283 sizeof(*child
->ext_info_cache
),
285 if (!child
->ext_info_cache
)
288 for (i
= 0; i
< num_ext_info
; ++i
) {
289 child
->ext_info_cache
[i
].size
= -1;
291 if (!pchan
->ext_info
[i
].write
)
293 if (!pchan
->ext_info
[i
].read
)
296 ret
= iio_read_channel_ext_info(mux
->parent
,
297 mux
->ext_info
[i
].name
,
300 dev_err(dev
, "failed to get ext_info '%s'\n",
301 pchan
->ext_info
[i
].name
);
304 if (ret
>= PAGE_SIZE
) {
305 dev_err(dev
, "too large ext_info '%s'\n",
306 pchan
->ext_info
[i
].name
);
310 child
->ext_info_cache
[i
].data
= devm_kmemdup(dev
, page
, ret
+ 1,
312 if (!child
->ext_info_cache
[i
].data
)
315 child
->ext_info_cache
[i
].data
[ret
] = 0;
316 child
->ext_info_cache
[i
].size
= ret
;
320 devm_kfree(dev
, page
);
326 * Same as of_property_for_each_string(), but also keeps track of the
327 * index of each string.
329 #define of_property_for_each_string_index(np, propname, prop, s, i) \
330 for (prop = of_find_property(np, propname, NULL), \
331 s = of_prop_next_string(prop, NULL), \
334 s = of_prop_next_string(prop, s), \
337 static int mux_probe(struct platform_device
*pdev
)
339 struct device
*dev
= &pdev
->dev
;
340 struct device_node
*np
= pdev
->dev
.of_node
;
341 struct iio_dev
*indio_dev
;
342 struct iio_channel
*parent
;
344 struct property
*prop
;
356 parent
= devm_iio_channel_get(dev
, "parent");
358 return dev_err_probe(dev
, PTR_ERR(parent
),
359 "failed to get parent channel\n");
361 sizeof_ext_info
= iio_get_channel_ext_info_count(parent
);
362 if (sizeof_ext_info
) {
363 sizeof_ext_info
+= 1; /* one extra entry for the sentinel */
364 sizeof_ext_info
*= sizeof(*mux
->ext_info
);
368 of_property_for_each_string(np
, "channels", prop
, label
) {
373 dev_err(dev
, "not even a single child\n");
377 sizeof_priv
= sizeof(*mux
);
378 sizeof_priv
+= sizeof(*mux
->child
) * children
;
379 sizeof_priv
+= sizeof(*mux
->chan
) * children
;
380 sizeof_priv
+= sizeof_ext_info
;
382 indio_dev
= devm_iio_device_alloc(dev
, sizeof_priv
);
386 mux
= iio_priv(indio_dev
);
387 mux
->child
= (struct mux_child
*)(mux
+ 1);
388 mux
->chan
= (struct iio_chan_spec
*)(mux
->child
+ children
);
390 platform_set_drvdata(pdev
, indio_dev
);
392 mux
->parent
= parent
;
393 mux
->cached_state
= -1;
395 indio_dev
->name
= dev_name(dev
);
396 indio_dev
->info
= &mux_info
;
397 indio_dev
->modes
= INDIO_DIRECT_MODE
;
398 indio_dev
->channels
= mux
->chan
;
399 indio_dev
->num_channels
= children
;
400 if (sizeof_ext_info
) {
401 mux
->ext_info
= devm_kmemdup(dev
,
402 parent
->channel
->ext_info
,
403 sizeof_ext_info
, GFP_KERNEL
);
407 for (i
= 0; mux
->ext_info
[i
].name
; ++i
) {
408 if (parent
->channel
->ext_info
[i
].read
)
409 mux
->ext_info
[i
].read
= mux_read_ext_info
;
410 if (parent
->channel
->ext_info
[i
].write
)
411 mux
->ext_info
[i
].write
= mux_write_ext_info
;
412 mux
->ext_info
[i
].private = i
;
416 mux
->control
= devm_mux_control_get(dev
, NULL
);
417 if (IS_ERR(mux
->control
)) {
418 if (PTR_ERR(mux
->control
) != -EPROBE_DEFER
)
419 dev_err(dev
, "failed to get control-mux\n");
420 return PTR_ERR(mux
->control
);
424 of_property_for_each_string_index(np
, "channels", prop
, label
, state
) {
428 ret
= mux_configure_channel(dev
, mux
, state
, label
, i
++);
433 ret
= devm_iio_device_register(dev
, indio_dev
);
435 dev_err(dev
, "failed to register iio device\n");
442 static const struct of_device_id mux_match
[] = {
443 { .compatible
= "io-channel-mux" },
446 MODULE_DEVICE_TABLE(of
, mux_match
);
448 static struct platform_driver mux_driver
= {
452 .of_match_table
= mux_match
,
455 module_platform_driver(mux_driver
);
457 MODULE_DESCRIPTION("IIO multiplexer driver");
458 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
459 MODULE_LICENSE("GPL v2");