1 // SPDX-License-Identifier: GPL-2.0
3 * GPIO-controlled multiplexer driver
5 * Copyright (C) 2017 Axentia Technologies AB
7 * Author: Peter Rosin <peda@axentia.se>
10 #include <linux/bitmap.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/mux/driver.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
20 struct gpio_descs
*gpios
;
23 static int mux_gpio_set(struct mux_control
*mux
, int state
)
25 struct mux_gpio
*mux_gpio
= mux_chip_priv(mux
->chip
);
26 DECLARE_BITMAP(values
, BITS_PER_TYPE(state
));
29 bitmap_from_arr32(values
, &value
, BITS_PER_TYPE(value
));
31 gpiod_set_array_value_cansleep(mux_gpio
->gpios
->ndescs
,
32 mux_gpio
->gpios
->desc
,
33 mux_gpio
->gpios
->info
, values
);
38 static const struct mux_control_ops mux_gpio_ops
= {
42 static const struct of_device_id mux_gpio_dt_ids
[] = {
43 { .compatible
= "gpio-mux", },
46 MODULE_DEVICE_TABLE(of
, mux_gpio_dt_ids
);
48 static int mux_gpio_probe(struct platform_device
*pdev
)
50 struct device
*dev
= &pdev
->dev
;
51 struct mux_chip
*mux_chip
;
52 struct mux_gpio
*mux_gpio
;
57 pins
= gpiod_count(dev
, "mux");
61 mux_chip
= devm_mux_chip_alloc(dev
, 1, sizeof(*mux_gpio
));
63 return PTR_ERR(mux_chip
);
65 mux_gpio
= mux_chip_priv(mux_chip
);
66 mux_chip
->ops
= &mux_gpio_ops
;
68 mux_gpio
->gpios
= devm_gpiod_get_array(dev
, "mux", GPIOD_OUT_LOW
);
69 if (IS_ERR(mux_gpio
->gpios
))
70 return dev_err_probe(dev
, PTR_ERR(mux_gpio
->gpios
),
71 "failed to get gpios\n");
72 WARN_ON(pins
!= mux_gpio
->gpios
->ndescs
);
73 mux_chip
->mux
->states
= BIT(pins
);
75 ret
= device_property_read_u32(dev
, "idle-state", (u32
*)&idle_state
);
76 if (ret
>= 0 && idle_state
!= MUX_IDLE_AS_IS
) {
77 if (idle_state
< 0 || idle_state
>= mux_chip
->mux
->states
) {
78 dev_err(dev
, "invalid idle-state %u\n", idle_state
);
82 mux_chip
->mux
->idle_state
= idle_state
;
85 ret
= devm_mux_chip_register(dev
, mux_chip
);
89 dev_info(dev
, "%u-way mux-controller registered\n",
90 mux_chip
->mux
->states
);
95 static struct platform_driver mux_gpio_driver
= {
98 .of_match_table
= mux_gpio_dt_ids
,
100 .probe
= mux_gpio_probe
,
102 module_platform_driver(mux_gpio_driver
);
104 MODULE_DESCRIPTION("GPIO-controlled multiplexer driver");
105 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
106 MODULE_LICENSE("GPL v2");