xref: /linux/drivers/reset/reset-gpio.c (revision 37a93dd5c49b5fda807fd204edf2547c3493319c)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/auxiliary_bus.h>
4 #include <linux/gpio/consumer.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/module.h>
7 #include <linux/of.h>
8 #include <linux/reset-controller.h>
9 
10 struct reset_gpio_priv {
11 	struct reset_controller_dev rc;
12 	struct gpio_desc *reset;
13 };
14 
15 static inline struct reset_gpio_priv
16 *rc_to_reset_gpio(struct reset_controller_dev *rc)
17 {
18 	return container_of(rc, struct reset_gpio_priv, rc);
19 }
20 
21 static int reset_gpio_assert(struct reset_controller_dev *rc, unsigned long id)
22 {
23 	struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
24 
25 	return gpiod_set_value_cansleep(priv->reset, 1);
26 }
27 
28 static int reset_gpio_deassert(struct reset_controller_dev *rc,
29 			       unsigned long id)
30 {
31 	struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
32 
33 	return gpiod_set_value_cansleep(priv->reset, 0);
34 }
35 
36 static int reset_gpio_status(struct reset_controller_dev *rc, unsigned long id)
37 {
38 	struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
39 
40 	return gpiod_get_value_cansleep(priv->reset);
41 }
42 
43 static const struct reset_control_ops reset_gpio_ops = {
44 	.assert = reset_gpio_assert,
45 	.deassert = reset_gpio_deassert,
46 	.status = reset_gpio_status,
47 };
48 
49 static int reset_gpio_of_xlate(struct reset_controller_dev *rcdev,
50 			       const struct of_phandle_args *reset_spec)
51 {
52 	return reset_spec->args[0];
53 }
54 
55 static void reset_gpio_of_node_put(void *data)
56 {
57 	of_node_put(data);
58 }
59 
60 static int reset_gpio_probe(struct auxiliary_device *adev,
61 			    const struct auxiliary_device_id *id)
62 {
63 	struct device *dev = &adev->dev;
64 	struct of_phandle_args *platdata = dev_get_platdata(dev);
65 	struct reset_gpio_priv *priv;
66 	int ret;
67 
68 	if (!platdata)
69 		return -EINVAL;
70 
71 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
72 	if (!priv)
73 		return -ENOMEM;
74 
75 	auxiliary_set_drvdata(adev, &priv->rc);
76 
77 	priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
78 	if (IS_ERR(priv->reset))
79 		return dev_err_probe(dev, PTR_ERR(priv->reset),
80 				     "Could not get reset gpios\n");
81 
82 	priv->rc.ops = &reset_gpio_ops;
83 	priv->rc.owner = THIS_MODULE;
84 	priv->rc.dev = dev;
85 	priv->rc.of_args = platdata;
86 	ret = devm_add_action_or_reset(dev, reset_gpio_of_node_put,
87 				       priv->rc.of_node);
88 	if (ret)
89 		return ret;
90 
91 	/* Cells to match GPIO specifier, but it's not really used */
92 	priv->rc.of_reset_n_cells = 2;
93 	priv->rc.of_xlate = reset_gpio_of_xlate;
94 	priv->rc.nr_resets = 1;
95 
96 	return devm_reset_controller_register(dev, &priv->rc);
97 }
98 
99 static const struct auxiliary_device_id reset_gpio_ids[] = {
100 	{ .name = "reset.gpio" },
101 	{}
102 };
103 MODULE_DEVICE_TABLE(auxiliary, reset_gpio_ids);
104 
105 static struct auxiliary_driver reset_gpio_driver = {
106 	.probe		= reset_gpio_probe,
107 	.id_table	= reset_gpio_ids,
108 	.driver	= {
109 		.name = "reset-gpio",
110 		.suppress_bind_attrs = true,
111 	},
112 };
113 module_auxiliary_driver(reset_gpio_driver);
114 
115 MODULE_AUTHOR("Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>");
116 MODULE_DESCRIPTION("Generic GPIO reset driver");
117 MODULE_LICENSE("GPL");
118