1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2021 Mark Kettenis <kettenis@openbsd.org>
4  */
5 
6 #include <dm.h>
7 #include <dm/device-internal.h>
8 #include <dm/pinctrl.h>
9 #include <dt-bindings/pinctrl/apple.h>
10 #include <asm/io.h>
11 #include <asm-generic/gpio.h>
12 #include <linux/bitfield.h>
13 
14 struct apple_pinctrl_priv {
15 	void *base;
16 	int pin_count;
17 };
18 
19 #define REG_GPIO(x)	(4 * (x))
20 #define  REG_GPIO_DATA		BIT(0)
21 #define  REG_GPIO_MODE		GENMASK(3, 1)
22 #define  REG_GPIO_OUT		1
23 #define  REG_GPIO_PERIPH	GENMASK(6, 5)
24 #define  REG_GPIO_INPUT_ENABLE	BIT(9)
25 
apple_pinctrl_config_pin(struct apple_pinctrl_priv * priv,unsigned pin,u32 clr,u32 set)26 static void apple_pinctrl_config_pin(struct apple_pinctrl_priv *priv,
27 				     unsigned pin, u32 clr, u32 set)
28 {
29 	unsigned reg = REG_GPIO(pin);
30 	u32 old, new;
31 
32 	old = readl(priv->base + REG_GPIO(pin));
33 	new = (old & ~clr) | set;
34 	writel(new, priv->base + reg);
35 }
36 
apple_gpio_get_value(struct udevice * dev,unsigned offset)37 static int apple_gpio_get_value(struct udevice *dev, unsigned offset)
38 {
39 	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
40 
41 	return !!(readl(priv->base + REG_GPIO(offset)) & REG_GPIO_DATA);
42 }
43 
apple_gpio_set_value(struct udevice * dev,unsigned offset,int value)44 static int apple_gpio_set_value(struct udevice *dev, unsigned offset,
45 				int value)
46 {
47 	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
48 
49 	apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA,
50 				 value ? REG_GPIO_DATA : 0);
51 	return 0;
52 }
53 
apple_gpio_get_direction(struct udevice * dev,unsigned offset)54 static int apple_gpio_get_direction(struct udevice *dev, unsigned offset)
55 {
56 	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
57 	u32 reg = readl(priv->base + REG_GPIO(offset));
58 
59 	if (FIELD_GET(REG_GPIO_MODE, reg) == REG_GPIO_OUT)
60 		return GPIOF_OUTPUT;
61 	else
62 		return GPIOF_INPUT;
63 }
64 
apple_gpio_direction_input(struct udevice * dev,unsigned offset)65 static int apple_gpio_direction_input(struct udevice *dev, unsigned offset)
66 {
67 	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
68 
69 	apple_pinctrl_config_pin(priv, offset,
70 				 REG_GPIO_PERIPH | REG_GPIO_MODE,
71 				 REG_GPIO_INPUT_ENABLE);
72 	return 0;
73 }
74 
apple_gpio_direction_output(struct udevice * dev,unsigned offset,int value)75 static int apple_gpio_direction_output(struct udevice *dev, unsigned offset,
76 				       int value)
77 {
78 	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
79 	u32 set = (value ? REG_GPIO_DATA : 0);
80 
81 	apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA |
82 				 REG_GPIO_PERIPH | REG_GPIO_MODE,
83 				 set | FIELD_PREP(REG_GPIO_MODE, REG_GPIO_OUT));
84 	return 0;
85 }
86 
apple_gpio_probe(struct udevice * dev)87 static int apple_gpio_probe(struct udevice *dev)
88 {
89 	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
90 	struct gpio_dev_priv *uc_priv;
91 
92 	uc_priv = dev_get_uclass_priv(dev);
93 	uc_priv->bank_name = "gpio";
94 	uc_priv->gpio_count = priv->pin_count;
95 
96 	return 0;
97 }
98 
99 static struct dm_gpio_ops apple_gpio_ops = {
100 	.get_value = apple_gpio_get_value,
101 	.set_value = apple_gpio_set_value,
102 	.get_function = apple_gpio_get_direction,
103 	.direction_input = apple_gpio_direction_input,
104 	.direction_output = apple_gpio_direction_output,
105 };
106 
107 static struct driver apple_gpio_driver = {
108 	.name = "apple_gpio",
109 	.id = UCLASS_GPIO,
110 	.probe = apple_gpio_probe,
111 	.ops = &apple_gpio_ops,
112 };
113 
apple_pinctrl_get_pins_count(struct udevice * dev)114 static int apple_pinctrl_get_pins_count(struct udevice *dev)
115 {
116 	struct apple_pinctrl_priv *priv = dev_get_priv(dev);
117 
118 	return priv->pin_count;
119 }
120 
apple_pinctrl_get_pin_name(struct udevice * dev,unsigned selector)121 static const char *apple_pinctrl_get_pin_name(struct udevice *dev,
122 					      unsigned selector)
123 {
124 	static char pin_name[PINNAME_SIZE];
125 
126 	snprintf(pin_name, PINNAME_SIZE, "pin%d", selector);
127 	return pin_name;
128 }
129 
apple_pinctrl_get_pin_muxing(struct udevice * dev,unsigned selector,char * buf,int size)130 static int apple_pinctrl_get_pin_muxing(struct udevice *dev, unsigned selector,
131 					char *buf, int size)
132 {
133 	struct apple_pinctrl_priv *priv = dev_get_priv(dev);
134 
135 	if (readl(priv->base + REG_GPIO(selector)) & REG_GPIO_PERIPH)
136 		strncpy(buf, "periph", size);
137 	else
138 		strncpy(buf, "gpio", size);
139 	return 0;
140 }
141 
apple_pinctrl_pinmux_set(struct udevice * dev,unsigned pin_selector,unsigned func_selector)142 static int apple_pinctrl_pinmux_set(struct udevice *dev, unsigned pin_selector,
143 				    unsigned func_selector)
144 {
145 	struct apple_pinctrl_priv *priv = dev_get_priv(dev);
146 
147 	apple_pinctrl_config_pin(priv, pin_selector,
148 				 REG_GPIO_DATA | REG_GPIO_MODE,
149 				 FIELD_PREP(REG_GPIO_PERIPH, func_selector) |
150 				 REG_GPIO_INPUT_ENABLE);
151 	return 0;
152 }
153 
apple_pinctrl_pinmux_property_set(struct udevice * dev,u32 pinmux_group)154 static int apple_pinctrl_pinmux_property_set(struct udevice *dev,
155 					     u32 pinmux_group)
156 {
157 	unsigned pin_selector = APPLE_PIN(pinmux_group);
158 	unsigned func_selector = APPLE_FUNC(pinmux_group);
159 	int ret;
160 
161 	ret = apple_pinctrl_pinmux_set(dev, pin_selector, func_selector);
162 	return ret ? ret : pin_selector;
163 }
164 
apple_pinctrl_probe(struct udevice * dev)165 static int apple_pinctrl_probe(struct udevice *dev)
166 {
167 	struct apple_pinctrl_priv *priv = dev_get_priv(dev);
168 	struct ofnode_phandle_args args;
169 	struct udevice *child;
170 
171 	priv->base = dev_read_addr_ptr(dev);
172 	if (!priv->base)
173 		return -EINVAL;
174 
175 	if (!dev_read_phandle_with_args(dev, "gpio-ranges",
176 					NULL, 3, 0, &args))
177 		priv->pin_count = args.args[2];
178 
179 	device_bind(dev, &apple_gpio_driver, "apple_gpio", NULL,
180 		    dev_ofnode(dev), &child);
181 
182 	return 0;
183 }
184 
185 static struct pinctrl_ops apple_pinctrl_ops = {
186 	.set_state = pinctrl_generic_set_state,
187 	.get_pins_count = apple_pinctrl_get_pins_count,
188 	.get_pin_name = apple_pinctrl_get_pin_name,
189 	.pinmux_set = apple_pinctrl_pinmux_set,
190 	.pinmux_property_set = apple_pinctrl_pinmux_property_set,
191 	.get_pin_muxing = apple_pinctrl_get_pin_muxing,
192 };
193 
194 static const struct udevice_id apple_pinctrl_ids[] = {
195 	{ .compatible = "apple,pinctrl" },
196 	{ /* sentinel */ }
197 };
198 
199 U_BOOT_DRIVER(pinctrl_apple) = {
200 	.name = "apple_pinctrl",
201 	.id = UCLASS_PINCTRL,
202 	.of_match = apple_pinctrl_ids,
203 	.priv_auto = sizeof(struct apple_pinctrl_priv),
204 	.ops = &apple_pinctrl_ops,
205 	.probe = apple_pinctrl_probe,
206 };
207