2 * GPIO-controlled 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/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/mux/driver.h>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
22 struct gpio_descs
*gpios
;
26 static int mux_gpio_set(struct mux_control
*mux
, int state
)
28 struct mux_gpio
*mux_gpio
= mux_chip_priv(mux
->chip
);
31 for (i
= 0; i
< mux_gpio
->gpios
->ndescs
; i
++)
32 mux_gpio
->val
[i
] = (state
>> i
) & 1;
34 gpiod_set_array_value_cansleep(mux_gpio
->gpios
->ndescs
,
35 mux_gpio
->gpios
->desc
,
41 static const struct mux_control_ops mux_gpio_ops
= {
45 static const struct of_device_id mux_gpio_dt_ids
[] = {
46 { .compatible
= "gpio-mux", },
49 MODULE_DEVICE_TABLE(of
, mux_gpio_dt_ids
);
51 static int mux_gpio_probe(struct platform_device
*pdev
)
53 struct device
*dev
= &pdev
->dev
;
54 struct mux_chip
*mux_chip
;
55 struct mux_gpio
*mux_gpio
;
60 pins
= gpiod_count(dev
, "mux");
64 mux_chip
= devm_mux_chip_alloc(dev
, 1, sizeof(*mux_gpio
) +
65 pins
* sizeof(*mux_gpio
->val
));
67 return PTR_ERR(mux_chip
);
69 mux_gpio
= mux_chip_priv(mux_chip
);
70 mux_gpio
->val
= (int *)(mux_gpio
+ 1);
71 mux_chip
->ops
= &mux_gpio_ops
;
73 mux_gpio
->gpios
= devm_gpiod_get_array(dev
, "mux", GPIOD_OUT_LOW
);
74 if (IS_ERR(mux_gpio
->gpios
)) {
75 ret
= PTR_ERR(mux_gpio
->gpios
);
76 if (ret
!= -EPROBE_DEFER
)
77 dev_err(dev
, "failed to get gpios\n");
80 WARN_ON(pins
!= mux_gpio
->gpios
->ndescs
);
81 mux_chip
->mux
->states
= 1 << pins
;
83 ret
= device_property_read_u32(dev
, "idle-state", (u32
*)&idle_state
);
84 if (ret
>= 0 && idle_state
!= MUX_IDLE_AS_IS
) {
85 if (idle_state
< 0 || idle_state
>= mux_chip
->mux
->states
) {
86 dev_err(dev
, "invalid idle-state %u\n", idle_state
);
90 mux_chip
->mux
->idle_state
= idle_state
;
93 ret
= devm_mux_chip_register(dev
, mux_chip
);
97 dev_info(dev
, "%u-way mux-controller registered\n",
98 mux_chip
->mux
->states
);
103 static struct platform_driver mux_gpio_driver
= {
106 .of_match_table
= of_match_ptr(mux_gpio_dt_ids
),
108 .probe
= mux_gpio_probe
,
110 module_platform_driver(mux_gpio_driver
);
112 MODULE_DESCRIPTION("GPIO-controlled multiplexer driver");
113 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
114 MODULE_LICENSE("GPL v2");