1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <fdtdec.h>
9 #include <log.h>
10 #include <malloc.h>
11 #include <acpi/acpi_device.h>
12 #include <asm/gpio.h>
13 #include <dm/acpi.h>
14 #include <dm/device-internal.h>
15 #include <dm/device_compat.h>
16 #include <dm/lists.h>
17 #include <dm/of.h>
18 #include <dm/pinctrl.h>
19 #include <dt-bindings/gpio/gpio.h>
20 #include <dt-bindings/gpio/sandbox-gpio.h>
21 
22 struct gpio_state {
23 	const char *label;	/* label given by requester */
24 	ulong flags;		/* flags (GPIOD_...) */
25 };
26 
27 /* Access routines for GPIO info */
get_gpio_state(struct udevice * dev,uint offset)28 static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
29 {
30 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
31 	struct gpio_state *state = dev_get_priv(dev);
32 
33 	if (offset >= uc_priv->gpio_count) {
34 		printf("sandbox_gpio: error: invalid gpio %u\n", offset);
35 		return NULL;
36 	}
37 
38 	return &state[offset];
39 }
40 
41 /* Access routines for GPIO flags */
get_gpio_flags(struct udevice * dev,unsigned int offset)42 static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
43 {
44 	struct gpio_state *state = get_gpio_state(dev, offset);
45 
46 	if (!state)
47 		return NULL;
48 
49 	return &state->flags;
50 
51 }
52 
get_gpio_flag(struct udevice * dev,unsigned int offset,ulong flag)53 static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
54 {
55 	return (*get_gpio_flags(dev, offset) & flag) != 0;
56 }
57 
set_gpio_flag(struct udevice * dev,unsigned int offset,ulong flag,int value)58 static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
59 			 int value)
60 {
61 	struct gpio_state *state = get_gpio_state(dev, offset);
62 
63 	if (value)
64 		state->flags |= flag;
65 	else
66 		state->flags &= ~flag;
67 
68 	return 0;
69 }
70 
71 /*
72  * Back-channel sandbox-internal-only access to GPIO state
73  */
74 
sandbox_gpio_get_value(struct udevice * dev,unsigned offset)75 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
76 {
77 	struct gpio_state *state = get_gpio_state(dev, offset);
78 	bool val;
79 
80 	if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
81 		debug("sandbox_gpio: get_value on output gpio %u\n", offset);
82 
83 	if (state->flags & GPIOD_EXT_DRIVEN) {
84 		val = state->flags & GPIOD_EXT_HIGH;
85 	} else {
86 		if (state->flags & GPIOD_EXT_PULL_UP)
87 			val = true;
88 		else if (state->flags & GPIOD_EXT_PULL_DOWN)
89 			val = false;
90 		else
91 			val = state->flags & GPIOD_PULL_UP;
92 	}
93 
94 	return val;
95 }
96 
sandbox_gpio_set_value(struct udevice * dev,unsigned offset,int value)97 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
98 {
99 	set_gpio_flag(dev, offset, GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
100 
101 	return 0;
102 }
103 
sandbox_gpio_get_direction(struct udevice * dev,unsigned offset)104 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
105 {
106 	return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
107 }
108 
sandbox_gpio_set_direction(struct udevice * dev,unsigned offset,int output)109 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
110 {
111 	set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
112 	set_gpio_flag(dev, offset, GPIOD_IS_IN, !output);
113 
114 	return 0;
115 }
116 
sandbox_gpio_get_flags(struct udevice * dev,uint offset)117 ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
118 {
119 	ulong flags = *get_gpio_flags(dev, offset);
120 
121 	return flags & ~GPIOD_SANDBOX_MASK;
122 }
123 
sandbox_gpio_set_flags(struct udevice * dev,uint offset,ulong flags)124 int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
125 {
126 	struct gpio_state *state = get_gpio_state(dev, offset);
127 
128 	state->flags = flags;
129 
130 	return 0;
131 }
132 
133 /*
134  * These functions implement the public interface within U-Boot
135  */
136 
137 /* set GPIO port 'offset' as an input */
sb_gpio_direction_input(struct udevice * dev,unsigned offset)138 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
139 {
140 	debug("%s: offset:%u\n", __func__, offset);
141 
142 	return sandbox_gpio_set_direction(dev, offset, 0);
143 }
144 
145 /* set GPIO port 'offset' as an output, with polarity 'value' */
sb_gpio_direction_output(struct udevice * dev,unsigned offset,int value)146 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
147 				    int value)
148 {
149 	int ret;
150 
151 	debug("%s: offset:%u, value = %d\n", __func__, offset, value);
152 
153 	ret = sandbox_gpio_set_direction(dev, offset, 1);
154 	if (ret)
155 		return ret;
156 	ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
157 			    GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
158 	if (ret)
159 		return ret;
160 
161 	return 0;
162 }
163 
164 /* read GPIO IN value of port 'offset' */
sb_gpio_get_value(struct udevice * dev,unsigned offset)165 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
166 {
167 	debug("%s: offset:%u\n", __func__, offset);
168 
169 	return sandbox_gpio_get_value(dev, offset);
170 }
171 
172 /* write GPIO OUT value to port 'offset' */
sb_gpio_set_value(struct udevice * dev,unsigned offset,int value)173 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
174 {
175 	int ret;
176 
177 	debug("%s: offset:%u, value = %d\n", __func__, offset, value);
178 
179 	if (!sandbox_gpio_get_direction(dev, offset)) {
180 		printf("sandbox_gpio: error: set_value on input gpio %u\n",
181 		       offset);
182 		return -1;
183 	}
184 
185 	ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
186 			    GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
187 	if (ret)
188 		return ret;
189 
190 	return 0;
191 }
192 
sb_gpio_get_function(struct udevice * dev,unsigned offset)193 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
194 {
195 	if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
196 		return GPIOF_OUTPUT;
197 	if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
198 		return GPIOF_INPUT;
199 
200 	return GPIOF_INPUT; /*GPIO is not configurated */
201 }
202 
sb_gpio_xlate(struct udevice * dev,struct gpio_desc * desc,struct ofnode_phandle_args * args)203 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
204 			 struct ofnode_phandle_args *args)
205 {
206 	desc->offset = args->args[0];
207 	if (args->args_count < 2)
208 		return 0;
209 	/* treat generic binding with gpio uclass */
210 	gpio_xlate_offs_flags(dev, desc, args);
211 
212 	/* sandbox test specific, not defined in gpio.h */
213 	if (args->args[1] & GPIO_IN)
214 		desc->flags |= GPIOD_IS_IN;
215 
216 	if (args->args[1] & GPIO_OUT)
217 		desc->flags |= GPIOD_IS_OUT;
218 
219 	if (args->args[1] & GPIO_OUT_ACTIVE)
220 		desc->flags |= GPIOD_IS_OUT_ACTIVE;
221 
222 	return 0;
223 }
224 
sb_gpio_set_flags(struct udevice * dev,unsigned int offset,ulong flags)225 static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
226 			     ulong flags)
227 {
228 	debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
229 	struct gpio_state *state = get_gpio_state(dev, offset);
230 
231 	if (flags & GPIOD_IS_OUT) {
232 		flags |= GPIOD_EXT_DRIVEN;
233 		if (flags & GPIOD_IS_OUT_ACTIVE)
234 			flags |= GPIOD_EXT_HIGH;
235 		else
236 			flags &= ~GPIOD_EXT_HIGH;
237 	} else {
238 		flags |= state->flags & GPIOD_SANDBOX_MASK;
239 	}
240 	state->flags = flags;
241 
242 	return 0;
243 }
244 
sb_gpio_get_flags(struct udevice * dev,uint offset,ulong * flagsp)245 static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
246 {
247 	debug("%s: offset:%u\n", __func__, offset);
248 	*flagsp = *get_gpio_flags(dev, offset) & ~GPIOD_SANDBOX_MASK;
249 
250 	return 0;
251 }
252 
253 #if CONFIG_IS_ENABLED(ACPIGEN)
sb_gpio_get_acpi(const struct gpio_desc * desc,struct acpi_gpio * gpio)254 static int sb_gpio_get_acpi(const struct gpio_desc *desc,
255 			    struct acpi_gpio *gpio)
256 {
257 	int ret;
258 
259 	/* Note that gpio_get_acpi() zeroes *gpio before calling here */
260 	gpio->pin_count = 1;
261 	gpio->pins[0] = desc->offset;
262 	ret = acpi_device_scope(desc->dev, gpio->resource,
263 				sizeof(gpio->resource));
264 	if (ret)
265 		return log_ret(ret);
266 
267 	/* All of these values are just used for testing */
268 	if (desc->flags & GPIOD_ACTIVE_LOW) {
269 		gpio->pin0_addr = 0x80012 + desc->offset;
270 		gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
271 		gpio->pull = ACPI_GPIO_PULL_DOWN;
272 		gpio->interrupt_debounce_timeout = 4321;
273 
274 		/* We use the GpioInt part */
275 		gpio->irq.pin = desc->offset;
276 		gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
277 		gpio->irq.shared = ACPI_IRQ_SHARED;
278 		gpio->irq.wake = ACPI_IRQ_WAKE;
279 
280 		/* The GpioIo part is only used for testing */
281 		gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
282 	} else {
283 		gpio->pin0_addr = 0xc00dc + desc->offset;
284 		gpio->type = ACPI_GPIO_TYPE_IO;
285 		gpio->pull = ACPI_GPIO_PULL_UP;
286 		gpio->interrupt_debounce_timeout = 0;
287 
288 		/* The GpioInt part is not used */
289 
290 		/* We use the GpioIo part */
291 		gpio->output_drive_strength = 1234;
292 		gpio->io_shared = true;
293 		gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
294 		gpio->polarity = 0;
295 	}
296 
297 	return 0;
298 }
299 
sb_gpio_get_name(const struct udevice * dev,char * out_name)300 static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
301 {
302 	return acpi_copy_name(out_name, "GPIO");
303 }
304 
305 struct acpi_ops gpio_sandbox_acpi_ops = {
306 	.get_name	= sb_gpio_get_name,
307 };
308 #endif /* ACPIGEN */
309 
310 static const struct dm_gpio_ops gpio_sandbox_ops = {
311 	.direction_input	= sb_gpio_direction_input,
312 	.direction_output	= sb_gpio_direction_output,
313 	.get_value		= sb_gpio_get_value,
314 	.set_value		= sb_gpio_set_value,
315 	.get_function		= sb_gpio_get_function,
316 	.xlate			= sb_gpio_xlate,
317 	.set_flags		= sb_gpio_set_flags,
318 	.get_flags		= sb_gpio_get_flags,
319 #if CONFIG_IS_ENABLED(ACPIGEN)
320 	.get_acpi		= sb_gpio_get_acpi,
321 #endif
322 };
323 
sandbox_gpio_of_to_plat(struct udevice * dev)324 static int sandbox_gpio_of_to_plat(struct udevice *dev)
325 {
326 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
327 
328 	uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
329 						   0);
330 	uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
331 
332 	return 0;
333 }
334 
gpio_sandbox_probe(struct udevice * dev)335 static int gpio_sandbox_probe(struct udevice *dev)
336 {
337 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
338 
339 	if (!dev_has_ofnode(dev))
340 		/* Tell the uclass how many GPIOs we have */
341 		uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
342 
343 	dev_set_priv(dev,
344 		     calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
345 
346 	return 0;
347 }
348 
gpio_sandbox_remove(struct udevice * dev)349 static int gpio_sandbox_remove(struct udevice *dev)
350 {
351 	free(dev_get_priv(dev));
352 
353 	return 0;
354 }
355 
356 static const struct udevice_id sandbox_gpio_ids[] = {
357 	{ .compatible = "sandbox,gpio" },
358 	{ }
359 };
360 
361 U_BOOT_DRIVER(sandbox_gpio) = {
362 	.name	= "sandbox_gpio",
363 	.id	= UCLASS_GPIO,
364 	.of_match = sandbox_gpio_ids,
365 	.of_to_plat = sandbox_gpio_of_to_plat,
366 	.probe	= gpio_sandbox_probe,
367 	.remove	= gpio_sandbox_remove,
368 	.ops	= &gpio_sandbox_ops,
369 	ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
370 };
371 
372 DM_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
373 
374 /* pincontrol: used only to check GPIO pin configuration (pinmux command) */
375 
376 struct sb_pinctrl_priv {
377 	int pinctrl_ngpios;
378 	struct list_head gpio_dev;
379 };
380 
381 struct sb_gpio_bank {
382 	struct udevice *gpio_dev;
383 	struct list_head list;
384 };
385 
sb_populate_gpio_dev_list(struct udevice * dev)386 static int sb_populate_gpio_dev_list(struct udevice *dev)
387 {
388 	struct sb_pinctrl_priv *priv = dev_get_priv(dev);
389 	struct udevice *gpio_dev;
390 	struct udevice *child;
391 	struct sb_gpio_bank *gpio_bank;
392 	int ret;
393 
394 	/*
395 	 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
396 	 * a list with all gpio device reference which belongs to the
397 	 * current pin-controller. This list is used to find pin_name and
398 	 * pin muxing
399 	 */
400 	list_for_each_entry(child, &dev->child_head, sibling_node) {
401 		ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
402 						&gpio_dev);
403 		if (ret < 0)
404 			continue;
405 
406 		gpio_bank = malloc(sizeof(*gpio_bank));
407 		if (!gpio_bank) {
408 			dev_err(dev, "Not enough memory\n");
409 			return -ENOMEM;
410 		}
411 
412 		gpio_bank->gpio_dev = gpio_dev;
413 		list_add_tail(&gpio_bank->list, &priv->gpio_dev);
414 	}
415 
416 	return 0;
417 }
418 
sb_pinctrl_get_pins_count(struct udevice * dev)419 static int sb_pinctrl_get_pins_count(struct udevice *dev)
420 {
421 	struct sb_pinctrl_priv *priv = dev_get_priv(dev);
422 	struct gpio_dev_priv *uc_priv;
423 	struct sb_gpio_bank *gpio_bank;
424 
425 	/*
426 	 * if get_pins_count has already been executed once on this
427 	 * pin-controller, no need to run it again
428 	 */
429 	if (priv->pinctrl_ngpios)
430 		return priv->pinctrl_ngpios;
431 
432 	if (list_empty(&priv->gpio_dev))
433 		sb_populate_gpio_dev_list(dev);
434 	/*
435 	 * walk through all banks to retrieve the pin-controller
436 	 * pins number
437 	 */
438 	list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
439 		uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
440 
441 		priv->pinctrl_ngpios += uc_priv->gpio_count;
442 	}
443 
444 	return priv->pinctrl_ngpios;
445 }
446 
sb_pinctrl_get_gpio_dev(struct udevice * dev,unsigned int selector,unsigned int * idx)447 static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
448 					       unsigned int selector,
449 					       unsigned int *idx)
450 {
451 	struct sb_pinctrl_priv *priv = dev_get_priv(dev);
452 	struct sb_gpio_bank *gpio_bank;
453 	struct gpio_dev_priv *uc_priv;
454 	int pin_count = 0;
455 
456 	if (list_empty(&priv->gpio_dev))
457 		sb_populate_gpio_dev_list(dev);
458 
459 	/* look up for the bank which owns the requested pin */
460 	list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
461 		uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
462 
463 		if (selector < (pin_count + uc_priv->gpio_count)) {
464 			/*
465 			 * we found the bank, convert pin selector to
466 			 * gpio bank index
467 			 */
468 			*idx = selector - pin_count;
469 
470 			return gpio_bank->gpio_dev;
471 		}
472 		pin_count += uc_priv->gpio_count;
473 	}
474 
475 	return NULL;
476 }
477 
sb_pinctrl_get_pin_name(struct udevice * dev,unsigned int selector)478 static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
479 					   unsigned int selector)
480 {
481 	struct gpio_dev_priv *uc_priv;
482 	struct udevice *gpio_dev;
483 	unsigned int gpio_idx;
484 	static char pin_name[PINNAME_SIZE];
485 
486 	/* look up for the bank which owns the requested pin */
487 	gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
488 	if (!gpio_dev) {
489 		snprintf(pin_name, PINNAME_SIZE, "Error");
490 	} else {
491 		uc_priv = dev_get_uclass_priv(gpio_dev);
492 
493 		snprintf(pin_name, PINNAME_SIZE, "%s%d",
494 			 uc_priv->bank_name,
495 			 gpio_idx);
496 	}
497 
498 	return pin_name;
499 }
500 
get_flags_string(ulong flags)501 static char *get_flags_string(ulong flags)
502 {
503 	if (flags & GPIOD_OPEN_DRAIN)
504 		return "drive-open-drain";
505 	if (flags & GPIOD_OPEN_SOURCE)
506 		return "drive-open-source";
507 	if (flags & GPIOD_PULL_UP)
508 		return "bias-pull-up";
509 	if (flags & GPIOD_PULL_DOWN)
510 		return "bias-pull-down";
511 	return ".";
512 }
513 
sb_pinctrl_get_pin_muxing(struct udevice * dev,unsigned int selector,char * buf,int size)514 static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
515 				     unsigned int selector,
516 				     char *buf, int size)
517 {
518 	struct udevice *gpio_dev;
519 	unsigned int gpio_idx;
520 	ulong flags;
521 	int function;
522 
523 	/* look up for the bank which owns the requested pin */
524 	gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
525 	if (!gpio_dev) {
526 		snprintf(buf, size, "Error");
527 	} else {
528 		function = sb_gpio_get_function(gpio_dev, gpio_idx);
529 		flags = *get_gpio_flags(gpio_dev, gpio_idx);
530 
531 		snprintf(buf, size, "gpio %s %s",
532 			 function == GPIOF_OUTPUT ? "output" : "input",
533 			 get_flags_string(flags));
534 	}
535 
536 	return 0;
537 }
538 
539 #if CONFIG_IS_ENABLED(ACPIGEN)
sb_pinctrl_get_name(const struct udevice * dev,char * out_name)540 static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
541 {
542 	return acpi_copy_name(out_name, "PINC");
543 }
544 #endif
545 
sandbox_pinctrl_probe(struct udevice * dev)546 static int sandbox_pinctrl_probe(struct udevice *dev)
547 {
548 	struct sb_pinctrl_priv *priv = dev_get_priv(dev);
549 
550 	INIT_LIST_HEAD(&priv->gpio_dev);
551 
552 	return 0;
553 }
554 
555 static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
556 	.get_pin_name		= sb_pinctrl_get_pin_name,
557 	.get_pins_count		= sb_pinctrl_get_pins_count,
558 	.get_pin_muxing		= sb_pinctrl_get_pin_muxing,
559 };
560 
561 #if CONFIG_IS_ENABLED(ACPIGEN)
562 struct acpi_ops pinctrl_sandbox_acpi_ops = {
563 	.get_name	= sb_pinctrl_get_name,
564 };
565 #endif
566 
567 static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
568 	{ .compatible = "sandbox,pinctrl-gpio" },
569 	{ /* sentinel */ }
570 };
571 
572 U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
573 	.name = "sandbox_pinctrl_gpio",
574 	.id = UCLASS_PINCTRL,
575 	.of_match = sandbox_pinctrl_gpio_match,
576 	.ops = &sandbox_pinctrl_gpio_ops,
577 	.bind = dm_scan_fdt_dev,
578 	.probe = sandbox_pinctrl_probe,
579 	.priv_auto	= sizeof(struct sb_pinctrl_priv),
580 	ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)
581 };
582