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/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/mux/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
20 struct mux_ext_info_cache
{
26 struct mux_ext_info_cache
*ext_info_cache
;
31 struct mux_control
*control
;
32 struct iio_channel
*parent
;
33 struct iio_chan_spec
*chan
;
34 struct iio_chan_spec_ext_info
*ext_info
;
35 struct mux_child
*child
;
39 static int iio_mux_select(struct mux
*mux
, int idx
)
41 struct mux_child
*child
= &mux
->child
[idx
];
42 struct iio_chan_spec
const *chan
= &mux
->chan
[idx
];
46 ret
= mux_control_select_delay(mux
->control
, chan
->channel
,
49 mux
->cached_state
= -1;
53 if (mux
->cached_state
== chan
->channel
)
57 for (i
= 0; chan
->ext_info
[i
].name
; ++i
) {
58 const char *attr
= chan
->ext_info
[i
].name
;
59 struct mux_ext_info_cache
*cache
;
61 cache
= &child
->ext_info_cache
[i
];
66 ret
= iio_write_channel_ext_info(mux
->parent
, attr
,
71 mux_control_deselect(mux
->control
);
72 mux
->cached_state
= -1;
77 mux
->cached_state
= chan
->channel
;
82 static void iio_mux_deselect(struct mux
*mux
)
84 mux_control_deselect(mux
->control
);
87 static int mux_read_raw(struct iio_dev
*indio_dev
,
88 struct iio_chan_spec
const *chan
,
89 int *val
, int *val2
, long mask
)
91 struct mux
*mux
= iio_priv(indio_dev
);
92 int idx
= chan
- mux
->chan
;
95 ret
= iio_mux_select(mux
, idx
);
100 case IIO_CHAN_INFO_RAW
:
101 ret
= iio_read_channel_raw(mux
->parent
, val
);
104 case IIO_CHAN_INFO_SCALE
:
105 ret
= iio_read_channel_scale(mux
->parent
, val
, val2
);
112 iio_mux_deselect(mux
);
117 static int mux_read_avail(struct iio_dev
*indio_dev
,
118 struct iio_chan_spec
const *chan
,
119 const int **vals
, int *type
, int *length
,
122 struct mux
*mux
= iio_priv(indio_dev
);
123 int idx
= chan
- mux
->chan
;
126 ret
= iio_mux_select(mux
, idx
);
131 case IIO_CHAN_INFO_RAW
:
133 ret
= iio_read_avail_channel_raw(mux
->parent
, vals
, length
);
140 iio_mux_deselect(mux
);
145 static int mux_write_raw(struct iio_dev
*indio_dev
,
146 struct iio_chan_spec
const *chan
,
147 int val
, int val2
, long mask
)
149 struct mux
*mux
= iio_priv(indio_dev
);
150 int idx
= chan
- mux
->chan
;
153 ret
= iio_mux_select(mux
, idx
);
158 case IIO_CHAN_INFO_RAW
:
159 ret
= iio_write_channel_raw(mux
->parent
, val
);
166 iio_mux_deselect(mux
);
171 static const struct iio_info mux_info
= {
172 .read_raw
= mux_read_raw
,
173 .read_avail
= mux_read_avail
,
174 .write_raw
= mux_write_raw
,
177 static ssize_t
mux_read_ext_info(struct iio_dev
*indio_dev
, uintptr_t private,
178 struct iio_chan_spec
const *chan
, char *buf
)
180 struct mux
*mux
= iio_priv(indio_dev
);
181 int idx
= chan
- mux
->chan
;
184 ret
= iio_mux_select(mux
, idx
);
188 ret
= iio_read_channel_ext_info(mux
->parent
,
189 mux
->ext_info
[private].name
,
192 iio_mux_deselect(mux
);
197 static ssize_t
mux_write_ext_info(struct iio_dev
*indio_dev
, uintptr_t private,
198 struct iio_chan_spec
const *chan
,
199 const char *buf
, size_t len
)
201 struct device
*dev
= indio_dev
->dev
.parent
;
202 struct mux
*mux
= iio_priv(indio_dev
);
203 int idx
= chan
- mux
->chan
;
207 if (len
>= PAGE_SIZE
)
210 ret
= iio_mux_select(mux
, idx
);
214 new = devm_kmemdup(dev
, buf
, len
+ 1, GFP_KERNEL
);
216 iio_mux_deselect(mux
);
222 ret
= iio_write_channel_ext_info(mux
->parent
,
223 mux
->ext_info
[private].name
,
226 iio_mux_deselect(mux
);
227 devm_kfree(dev
, new);
231 devm_kfree(dev
, mux
->child
[idx
].ext_info_cache
[private].data
);
232 mux
->child
[idx
].ext_info_cache
[private].data
= new;
233 mux
->child
[idx
].ext_info_cache
[private].size
= len
;
235 iio_mux_deselect(mux
);
240 static int mux_configure_channel(struct device
*dev
, struct mux
*mux
,
241 u32 state
, const char *label
, int idx
)
243 struct mux_child
*child
= &mux
->child
[idx
];
244 struct iio_chan_spec
*chan
= &mux
->chan
[idx
];
245 struct iio_chan_spec
const *pchan
= mux
->parent
->channel
;
252 chan
->output
= pchan
->output
;
253 chan
->datasheet_name
= label
;
254 chan
->ext_info
= mux
->ext_info
;
256 ret
= iio_get_channel_type(mux
->parent
, &chan
->type
);
258 dev_err(dev
, "failed to get parent channel type\n");
262 if (iio_channel_has_info(pchan
, IIO_CHAN_INFO_RAW
))
263 chan
->info_mask_separate
|= BIT(IIO_CHAN_INFO_RAW
);
264 if (iio_channel_has_info(pchan
, IIO_CHAN_INFO_SCALE
))
265 chan
->info_mask_separate
|= BIT(IIO_CHAN_INFO_SCALE
);
267 if (iio_channel_has_available(pchan
, IIO_CHAN_INFO_RAW
))
268 chan
->info_mask_separate_available
|= BIT(IIO_CHAN_INFO_RAW
);
270 if (state
>= mux_control_states(mux
->control
)) {
271 dev_err(dev
, "too many channels\n");
275 chan
->channel
= state
;
277 num_ext_info
= iio_get_channel_ext_info_count(mux
->parent
);
279 page
= devm_kzalloc(dev
, PAGE_SIZE
, GFP_KERNEL
);
283 child
->ext_info_cache
= devm_kcalloc(dev
,
285 sizeof(*child
->ext_info_cache
),
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
);
327 static int mux_probe(struct platform_device
*pdev
)
329 struct device
*dev
= &pdev
->dev
;
330 struct iio_dev
*indio_dev
;
331 struct iio_channel
*parent
;
342 parent
= devm_iio_channel_get(dev
, "parent");
344 return dev_err_probe(dev
, PTR_ERR(parent
),
345 "failed to get parent channel\n");
347 sizeof_ext_info
= iio_get_channel_ext_info_count(parent
);
348 if (sizeof_ext_info
) {
349 sizeof_ext_info
+= 1; /* one extra entry for the sentinel */
350 sizeof_ext_info
*= sizeof(*mux
->ext_info
);
353 all_children
= device_property_string_array_count(dev
, "channels");
354 if (all_children
< 0)
357 labels
= devm_kmalloc_array(dev
, all_children
, sizeof(*labels
), GFP_KERNEL
);
361 ret
= device_property_read_string_array(dev
, "channels", labels
, all_children
);
366 for (state
= 0; state
< all_children
; state
++) {
371 dev_err(dev
, "not even a single child\n");
375 sizeof_priv
= sizeof(*mux
);
376 sizeof_priv
+= sizeof(*mux
->child
) * children
;
377 sizeof_priv
+= sizeof(*mux
->chan
) * children
;
378 sizeof_priv
+= sizeof_ext_info
;
380 indio_dev
= devm_iio_device_alloc(dev
, sizeof_priv
);
384 mux
= iio_priv(indio_dev
);
385 mux
->child
= (struct mux_child
*)(mux
+ 1);
386 mux
->chan
= (struct iio_chan_spec
*)(mux
->child
+ children
);
388 platform_set_drvdata(pdev
, indio_dev
);
390 mux
->parent
= parent
;
391 mux
->cached_state
= -1;
394 device_property_read_u32(dev
, "settle-time-us", &mux
->delay_us
);
396 indio_dev
->name
= dev_name(dev
);
397 indio_dev
->info
= &mux_info
;
398 indio_dev
->modes
= INDIO_DIRECT_MODE
;
399 indio_dev
->channels
= mux
->chan
;
400 indio_dev
->num_channels
= children
;
401 if (sizeof_ext_info
) {
402 mux
->ext_info
= devm_kmemdup(dev
,
403 parent
->channel
->ext_info
,
404 sizeof_ext_info
, GFP_KERNEL
);
408 for (i
= 0; mux
->ext_info
[i
].name
; ++i
) {
409 if (parent
->channel
->ext_info
[i
].read
)
410 mux
->ext_info
[i
].read
= mux_read_ext_info
;
411 if (parent
->channel
->ext_info
[i
].write
)
412 mux
->ext_info
[i
].write
= mux_write_ext_info
;
413 mux
->ext_info
[i
].private = i
;
417 mux
->control
= devm_mux_control_get(dev
, NULL
);
418 if (IS_ERR(mux
->control
))
419 return dev_err_probe(dev
, PTR_ERR(mux
->control
),
420 "failed to get control-mux\n");
423 for (state
= 0; state
< all_children
; state
++) {
427 ret
= mux_configure_channel(dev
, mux
, state
, labels
[state
], i
++);
432 ret
= devm_iio_device_register(dev
, indio_dev
);
434 dev_err(dev
, "failed to register iio device\n");
441 static const struct of_device_id mux_match
[] = {
442 { .compatible
= "io-channel-mux" },
445 MODULE_DEVICE_TABLE(of
, mux_match
);
447 static struct platform_driver mux_driver
= {
451 .of_match_table
= mux_match
,
454 module_platform_driver(mux_driver
);
456 MODULE_DESCRIPTION("IIO multiplexer driver");
457 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
458 MODULE_LICENSE("GPL v2");