xref: /linux/drivers/gpio/gpio-en7523.c (revision 18fbf5151d2c0bfe433c7428eef03cabf5fdb2fa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/types.h>
4 #include <linux/io.h>
5 #include <linux/bits.h>
6 #include <linux/gpio/driver.h>
7 #include <linux/gpio/generic.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/property.h>
11 
12 #define AIROHA_GPIO_MAX		32
13 
14 /**
15  * struct airoha_gpio_ctrl - Airoha GPIO driver data
16  * @gen_gc: Associated gpio_generic_chip instance.
17  * @data: The data register.
18  * @dir: [0] The direction register for the lower 16 pins.
19  * [1]: The direction register for the higher 16 pins.
20  * @output: The output enable register.
21  */
22 struct airoha_gpio_ctrl {
23 	struct gpio_generic_chip gen_gc;
24 	void __iomem *data;
25 	void __iomem *dir[2];
26 	void __iomem *output;
27 };
28 
29 static int airoha_dir_set(struct gpio_chip *gc, unsigned int gpio,
30 			  int val, int out)
31 {
32 	struct airoha_gpio_ctrl *ctrl = gpiochip_get_data(gc);
33 	u32 dir = ioread32(ctrl->dir[gpio / 16]);
34 	u32 output = ioread32(ctrl->output);
35 	u32 mask = BIT((gpio % 16) * 2);
36 
37 	if (out) {
38 		dir |= mask;
39 		output |= BIT(gpio);
40 	} else {
41 		dir &= ~mask;
42 		output &= ~BIT(gpio);
43 	}
44 
45 	iowrite32(dir, ctrl->dir[gpio / 16]);
46 
47 	if (out)
48 		gpio_generic_chip_set(&ctrl->gen_gc, gpio, val);
49 
50 	iowrite32(output, ctrl->output);
51 
52 	return 0;
53 }
54 
55 static int airoha_dir_out(struct gpio_chip *gc, unsigned int gpio,
56 			  int val)
57 {
58 	return airoha_dir_set(gc, gpio, val, 1);
59 }
60 
61 static int airoha_dir_in(struct gpio_chip *gc, unsigned int gpio)
62 {
63 	return airoha_dir_set(gc, gpio, 0, 0);
64 }
65 
66 static int airoha_get_dir(struct gpio_chip *gc, unsigned int gpio)
67 {
68 	struct airoha_gpio_ctrl *ctrl = gpiochip_get_data(gc);
69 	u32 dir = ioread32(ctrl->dir[gpio / 16]);
70 	u32 mask = BIT((gpio % 16) * 2);
71 
72 	return (dir & mask) ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
73 }
74 
75 static int airoha_gpio_probe(struct platform_device *pdev)
76 {
77 	struct gpio_generic_chip_config config = { };
78 	struct device *dev = &pdev->dev;
79 	struct airoha_gpio_ctrl *ctrl;
80 	int err;
81 
82 	ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
83 	if (!ctrl)
84 		return -ENOMEM;
85 
86 	ctrl->data = devm_platform_ioremap_resource(pdev, 0);
87 	if (IS_ERR(ctrl->data))
88 		return PTR_ERR(ctrl->data);
89 
90 	ctrl->dir[0] = devm_platform_ioremap_resource(pdev, 1);
91 	if (IS_ERR(ctrl->dir[0]))
92 		return PTR_ERR(ctrl->dir[0]);
93 
94 	ctrl->dir[1] = devm_platform_ioremap_resource(pdev, 2);
95 	if (IS_ERR(ctrl->dir[1]))
96 		return PTR_ERR(ctrl->dir[1]);
97 
98 	ctrl->output = devm_platform_ioremap_resource(pdev, 3);
99 	if (IS_ERR(ctrl->output))
100 		return PTR_ERR(ctrl->output);
101 
102 	config.dev = dev;
103 	config.sz = 4;
104 	config.dat = ctrl->data;
105 
106 	err = gpio_generic_chip_init(&ctrl->gen_gc, &config);
107 	if (err)
108 		return dev_err_probe(dev, err, "unable to init generic GPIO");
109 
110 	ctrl->gen_gc.gc.ngpio = AIROHA_GPIO_MAX;
111 	ctrl->gen_gc.gc.owner = THIS_MODULE;
112 	ctrl->gen_gc.gc.direction_output = airoha_dir_out;
113 	ctrl->gen_gc.gc.direction_input = airoha_dir_in;
114 	ctrl->gen_gc.gc.get_direction = airoha_get_dir;
115 
116 	return devm_gpiochip_add_data(dev, &ctrl->gen_gc.gc, ctrl);
117 }
118 
119 static const struct of_device_id airoha_gpio_of_match[] = {
120 	{ .compatible = "airoha,en7523-gpio" },
121 	{ }
122 };
123 MODULE_DEVICE_TABLE(of, airoha_gpio_of_match);
124 
125 static struct platform_driver airoha_gpio_driver = {
126 	.driver = {
127 		.name = "airoha-gpio",
128 		.of_match_table	= airoha_gpio_of_match,
129 	},
130 	.probe = airoha_gpio_probe,
131 };
132 module_platform_driver(airoha_gpio_driver);
133 
134 MODULE_DESCRIPTION("Airoha GPIO support");
135 MODULE_AUTHOR("John Crispin <john@phrozen.org>");
136 MODULE_LICENSE("GPL v2");
137