xref: /OK3568_Linux_fs/kernel/drivers/mux/gpio.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * GPIO-controlled multiplexer driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2017 Axentia Technologies AB
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Peter Rosin <peda@axentia.se>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/err.h>
11*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/mux/driver.h>
14*4882a593Smuzhiyun #include <linux/of_platform.h>
15*4882a593Smuzhiyun #include <linux/platform_device.h>
16*4882a593Smuzhiyun #include <linux/property.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct mux_gpio {
19*4882a593Smuzhiyun 	struct gpio_descs *gpios;
20*4882a593Smuzhiyun };
21*4882a593Smuzhiyun 
mux_gpio_set(struct mux_control * mux,int state)22*4882a593Smuzhiyun static int mux_gpio_set(struct mux_control *mux, int state)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	struct mux_gpio *mux_gpio = mux_chip_priv(mux->chip);
25*4882a593Smuzhiyun 	DECLARE_BITMAP(values, BITS_PER_TYPE(state));
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	values[0] = state;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	gpiod_set_array_value_cansleep(mux_gpio->gpios->ndescs,
30*4882a593Smuzhiyun 				       mux_gpio->gpios->desc,
31*4882a593Smuzhiyun 				       mux_gpio->gpios->info, values);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	return 0;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static const struct mux_control_ops mux_gpio_ops = {
37*4882a593Smuzhiyun 	.set = mux_gpio_set,
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static const struct of_device_id mux_gpio_dt_ids[] = {
41*4882a593Smuzhiyun 	{ .compatible = "gpio-mux", },
42*4882a593Smuzhiyun 	{ /* sentinel */ }
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mux_gpio_dt_ids);
45*4882a593Smuzhiyun 
mux_gpio_probe(struct platform_device * pdev)46*4882a593Smuzhiyun static int mux_gpio_probe(struct platform_device *pdev)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
49*4882a593Smuzhiyun 	struct mux_chip *mux_chip;
50*4882a593Smuzhiyun 	struct mux_gpio *mux_gpio;
51*4882a593Smuzhiyun 	int pins;
52*4882a593Smuzhiyun 	s32 idle_state;
53*4882a593Smuzhiyun 	int ret;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	pins = gpiod_count(dev, "mux");
56*4882a593Smuzhiyun 	if (pins < 0)
57*4882a593Smuzhiyun 		return pins;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	mux_chip = devm_mux_chip_alloc(dev, 1, sizeof(*mux_gpio));
60*4882a593Smuzhiyun 	if (IS_ERR(mux_chip))
61*4882a593Smuzhiyun 		return PTR_ERR(mux_chip);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	mux_gpio = mux_chip_priv(mux_chip);
64*4882a593Smuzhiyun 	mux_chip->ops = &mux_gpio_ops;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	mux_gpio->gpios = devm_gpiod_get_array(dev, "mux", GPIOD_OUT_LOW);
67*4882a593Smuzhiyun 	if (IS_ERR(mux_gpio->gpios)) {
68*4882a593Smuzhiyun 		ret = PTR_ERR(mux_gpio->gpios);
69*4882a593Smuzhiyun 		if (ret != -EPROBE_DEFER)
70*4882a593Smuzhiyun 			dev_err(dev, "failed to get gpios\n");
71*4882a593Smuzhiyun 		return ret;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 	WARN_ON(pins != mux_gpio->gpios->ndescs);
74*4882a593Smuzhiyun 	mux_chip->mux->states = 1 << pins;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state);
77*4882a593Smuzhiyun 	if (ret >= 0 && idle_state != MUX_IDLE_AS_IS) {
78*4882a593Smuzhiyun 		if (idle_state < 0 || idle_state >= mux_chip->mux->states) {
79*4882a593Smuzhiyun 			dev_err(dev, "invalid idle-state %u\n", idle_state);
80*4882a593Smuzhiyun 			return -EINVAL;
81*4882a593Smuzhiyun 		}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 		mux_chip->mux->idle_state = idle_state;
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	ret = devm_mux_chip_register(dev, mux_chip);
87*4882a593Smuzhiyun 	if (ret < 0)
88*4882a593Smuzhiyun 		return ret;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	dev_info(dev, "%u-way mux-controller registered\n",
91*4882a593Smuzhiyun 		 mux_chip->mux->states);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return 0;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun static struct platform_driver mux_gpio_driver = {
97*4882a593Smuzhiyun 	.driver = {
98*4882a593Smuzhiyun 		.name = "gpio-mux",
99*4882a593Smuzhiyun 		.of_match_table	= of_match_ptr(mux_gpio_dt_ids),
100*4882a593Smuzhiyun 	},
101*4882a593Smuzhiyun 	.probe = mux_gpio_probe,
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun module_platform_driver(mux_gpio_driver);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun MODULE_DESCRIPTION("GPIO-controlled multiplexer driver");
106*4882a593Smuzhiyun MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
107*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
108