pinctrl: Add Apple pinctrl driver
authorMark Kettenis <kettenis@openbsd.org>
Tue, 2 Nov 2021 17:21:57 +0000 (18:21 +0100)
committerTom Rini <trini@konsulko.com>
Wed, 17 Nov 2021 22:04:58 +0000 (17:04 -0500)
This driver supports both pin muxing and GPIO support for the
pin control logic found on Apple SoCs.

Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
MAINTAINERS
arch/arm/Kconfig
doc/device-tree-bindings/pinctrl/apple,pinctrl.yaml [new file with mode: 0644]
drivers/pinctrl/Kconfig
drivers/pinctrl/Makefile
drivers/pinctrl/pinctrl-apple.c [new file with mode: 0644]

index ae0262e..cc6b4c7 100644 (file)
@@ -115,6 +115,7 @@ F:  arch/arm/include/asm/arch-m1/
 F:     arch/arm/mach-apple/
 F:     configs/apple_m1_defconfig
 F:     drivers/iommu/apple_dart.c
+F:     drivers/pinctrl/pinctrl-apple.c
 F:     include/configs/apple.h
 
 ARM
index f7f0383..eed27af 100644 (file)
@@ -927,6 +927,7 @@ config ARCH_APPLE
        select CLK
        select CMD_USB
        select DM
+       select DM_GPIO
        select DM_KEYBOARD
        select DM_SERIAL
        select DM_USB
@@ -935,6 +936,7 @@ config ARCH_APPLE
        select LINUX_KERNEL_IMAGE_HEADER
        select OF_CONTROL
        select OF_BOARD
+       select PINCTRL
        select POSITION_INDEPENDENT
        select USB
        imply CMD_DM
diff --git a/doc/device-tree-bindings/pinctrl/apple,pinctrl.yaml b/doc/device-tree-bindings/pinctrl/apple,pinctrl.yaml
new file mode 100644 (file)
index 0000000..d50571a
--- /dev/null
@@ -0,0 +1,106 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pinctrl/apple,pinctrl.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Apple GPIO controller
+
+maintainers:
+  - Mark Kettenis <kettenis@openbsd.org>
+
+description: |
+  The Apple GPIO controller is a simple combined pin and GPIO
+  controller present on Apple ARM SoC platforms, including various
+  iPhone and iPad devices and the "Apple Silicon" Macs.
+
+properties:
+  compatible:
+    items:
+      - const: apple,t8103-pinctrl
+      - const: apple,pinctrl
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  gpio-controller: true
+
+  '#gpio-cells':
+    const: 2
+
+  gpio-ranges:
+    maxItems: 1
+
+  interrupts:
+    description: One interrupt for each of the (up to 7) interrupt
+      groups supported by the controller sorted by interrupt group
+      number in ascending order.
+    minItems: 1
+    maxItems: 7
+
+  interrupt-controller: true
+
+patternProperties:
+  '-pins$':
+    type: object
+    $ref: pinmux-node.yaml#
+
+    properties:
+      pinmux:
+        description:
+          Values are constructed from pin number and alternate function
+          configuration number using the APPLE_PINMUX() helper macro
+          defined in include/dt-bindings/pinctrl/apple.h.
+
+    required:
+      - pinmux
+
+    additionalProperties: false
+
+required:
+  - compatible
+  - reg
+  - gpio-controller
+  - '#gpio-cells'
+  - gpio-ranges
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/apple-aic.h>
+    #include <dt-bindings/pinctrl/apple.h>
+
+    soc {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      pinctrl: pinctrl@23c100000 {
+        compatible = "apple,t8103-pinctrl", "apple,pinctrl";
+        reg = <0x2 0x3c100000 0x0 0x100000>;
+        clocks = <&gpio_clk>;
+
+        gpio-controller;
+        #gpio-cells = <2>;
+        gpio-ranges = <&pinctrl 0 0 212>;
+
+        interrupt-controller;
+        interrupt-parent = <&aic>;
+        interrupts = <AIC_IRQ 16 IRQ_TYPE_LEVEL_HIGH>,
+                     <AIC_IRQ 17 IRQ_TYPE_LEVEL_HIGH>,
+                     <AIC_IRQ 18 IRQ_TYPE_LEVEL_HIGH>,
+                     <AIC_IRQ 19 IRQ_TYPE_LEVEL_HIGH>,
+                     <AIC_IRQ 20 IRQ_TYPE_LEVEL_HIGH>,
+                     <AIC_IRQ 21 IRQ_TYPE_LEVEL_HIGH>,
+                     <AIC_IRQ 22 IRQ_TYPE_LEVEL_HIGH>;
+
+        pcie_pins: pcie-pins {
+          pinmux = <APPLE_PINMUX(150, 1)>,
+                   <APPLE_PINMUX(151, 1)>,
+                   <APPLE_PINMUX(32, 1)>;
+        };
+      };
+    };
index 42f25e2..0394624 100644 (file)
@@ -145,6 +145,17 @@ config SPL_PINCONF_RECURSIVE
 
 if PINCTRL || SPL_PINCTRL
 
+config PINCTRL_APPLE
+       bool "Apple pinctrl driver"
+       depends on DM && PINCTRL_GENERIC && ARCH_APPLE
+       default y
+       help
+         Support pin multiplexing on Apple SoCs.
+
+         The driver is controlled by a device tree node which contains
+         both the GPIO definitions and pin control functions for each
+         available multiplex function.
+
 config PINCTRL_AR933X
        bool "QCA/Athores ar933x pin control driver"
        depends on DM && SOC_AR933X
index 05b71f2..fd736a7 100644 (file)
@@ -3,6 +3,7 @@
 obj-y                                  += pinctrl-uclass.o
 obj-$(CONFIG_$(SPL_)PINCTRL_GENERIC)   += pinctrl-generic.o
 
+obj-$(CONFIG_PINCTRL_APPLE)            += pinctrl-apple.o
 obj-$(CONFIG_PINCTRL_AT91)             += pinctrl-at91.o
 obj-$(CONFIG_PINCTRL_AT91PIO4)         += pinctrl-at91-pio4.o
 obj-y                                  += nxp/
diff --git a/drivers/pinctrl/pinctrl-apple.c b/drivers/pinctrl/pinctrl-apple.c
new file mode 100644 (file)
index 0000000..6247635
--- /dev/null
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2021 Mark Kettenis <kettenis@openbsd.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/device-internal.h>
+#include <dm/pinctrl.h>
+#include <dt-bindings/pinctrl/apple.h>
+#include <asm/io.h>
+#include <asm-generic/gpio.h>
+#include <linux/bitfield.h>
+
+struct apple_pinctrl_priv {
+       void *base;
+       int pin_count;
+};
+
+#define REG_GPIO(x)    (4 * (x))
+#define  REG_GPIO_DATA         BIT(0)
+#define  REG_GPIO_MODE         GENMASK(3, 1)
+#define  REG_GPIO_OUT          1
+#define  REG_GPIO_PERIPH       GENMASK(6, 5)
+#define  REG_GPIO_INPUT_ENABLE BIT(9)
+
+static void apple_pinctrl_config_pin(struct apple_pinctrl_priv *priv,
+                                    unsigned pin, u32 clr, u32 set)
+{
+       unsigned reg = REG_GPIO(pin);
+       u32 old, new;
+
+       old = readl(priv->base + REG_GPIO(pin));
+       new = (old & ~clr) | set;
+       writel(new, priv->base + reg);
+}
+
+static int apple_gpio_get_value(struct udevice *dev, unsigned offset)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
+
+       return !!(readl(priv->base + REG_GPIO(offset)) & REG_GPIO_DATA);
+}
+
+static int apple_gpio_set_value(struct udevice *dev, unsigned offset,
+                               int value)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
+
+       apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA,
+                                value ? REG_GPIO_DATA : 0);
+       return 0;
+}
+
+static int apple_gpio_get_direction(struct udevice *dev, unsigned offset)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
+       u32 reg = readl(priv->base + REG_GPIO(offset));
+
+       if (FIELD_GET(REG_GPIO_MODE, reg) == REG_GPIO_OUT)
+               return GPIOF_OUTPUT;
+       else
+               return GPIOF_INPUT;
+}
+
+static int apple_gpio_direction_input(struct udevice *dev, unsigned offset)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
+
+       apple_pinctrl_config_pin(priv, offset,
+                                REG_GPIO_PERIPH | REG_GPIO_MODE,
+                                REG_GPIO_INPUT_ENABLE);
+       return 0;
+}
+
+static int apple_gpio_direction_output(struct udevice *dev, unsigned offset,
+                                      int value)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
+       u32 set = (value ? REG_GPIO_DATA : 0);
+
+       apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA |
+                                REG_GPIO_PERIPH | REG_GPIO_MODE,
+                                set | FIELD_PREP(REG_GPIO_MODE, REG_GPIO_OUT));
+       return 0;
+}
+
+static int apple_gpio_probe(struct udevice *dev)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
+       struct gpio_dev_priv *uc_priv;
+
+       uc_priv = dev_get_uclass_priv(dev);
+       uc_priv->bank_name = "gpio";
+       uc_priv->gpio_count = priv->pin_count;
+
+       return 0;
+}
+
+static struct dm_gpio_ops apple_gpio_ops = {
+       .get_value = apple_gpio_get_value,
+       .set_value = apple_gpio_set_value,
+       .get_function = apple_gpio_get_direction,
+       .direction_input = apple_gpio_direction_input,
+       .direction_output = apple_gpio_direction_output,
+};
+
+static struct driver apple_gpio_driver = {
+       .name = "apple_gpio",
+       .id = UCLASS_GPIO,
+       .probe = apple_gpio_probe,
+       .ops = &apple_gpio_ops,
+};
+
+static int apple_pinctrl_get_pins_count(struct udevice *dev)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev);
+
+       return priv->pin_count;
+}
+
+static const char *apple_pinctrl_get_pin_name(struct udevice *dev,
+                                             unsigned selector)
+{
+       static char pin_name[PINNAME_SIZE];
+
+       snprintf(pin_name, PINNAME_SIZE, "pin%d", selector);
+       return pin_name;
+}
+
+static int apple_pinctrl_get_pin_muxing(struct udevice *dev, unsigned selector,
+                                       char *buf, int size)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev);
+
+       if (readl(priv->base + REG_GPIO(selector)) & REG_GPIO_PERIPH)
+               strncpy(buf, "periph", size);
+       else
+               strncpy(buf, "gpio", size);
+       return 0;
+}
+
+static int apple_pinctrl_pinmux_set(struct udevice *dev, unsigned pin_selector,
+                                   unsigned func_selector)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev);
+
+       apple_pinctrl_config_pin(priv, pin_selector,
+                                REG_GPIO_DATA | REG_GPIO_MODE,
+                                FIELD_PREP(REG_GPIO_PERIPH, func_selector) |
+                                REG_GPIO_INPUT_ENABLE);
+       return 0;
+}
+
+static int apple_pinctrl_pinmux_property_set(struct udevice *dev,
+                                            u32 pinmux_group)
+{
+       unsigned pin_selector = APPLE_PIN(pinmux_group);
+       unsigned func_selector = APPLE_FUNC(pinmux_group);
+       int ret;
+
+       ret = apple_pinctrl_pinmux_set(dev, pin_selector, func_selector);
+       return ret ? ret : pin_selector;
+}
+
+static int apple_pinctrl_probe(struct udevice *dev)
+{
+       struct apple_pinctrl_priv *priv = dev_get_priv(dev);
+       struct ofnode_phandle_args args;
+       struct udevice *child;
+
+       priv->base = dev_read_addr_ptr(dev);
+       if (!priv->base)
+               return -EINVAL;
+
+       if (!dev_read_phandle_with_args(dev, "gpio-ranges",
+                                       NULL, 3, 0, &args))
+               priv->pin_count = args.args[2];
+
+       device_bind(dev, &apple_gpio_driver, "apple_gpio", NULL,
+                   dev_ofnode(dev), &child);
+
+       return 0;
+}
+
+static struct pinctrl_ops apple_pinctrl_ops = {
+       .set_state = pinctrl_generic_set_state,
+       .get_pins_count = apple_pinctrl_get_pins_count,
+       .get_pin_name = apple_pinctrl_get_pin_name,
+       .pinmux_set = apple_pinctrl_pinmux_set,
+       .pinmux_property_set = apple_pinctrl_pinmux_property_set,
+       .get_pin_muxing = apple_pinctrl_get_pin_muxing,
+};
+
+static const struct udevice_id apple_pinctrl_ids[] = {
+       { .compatible = "apple,pinctrl" },
+       { /* sentinel */ }
+};
+
+U_BOOT_DRIVER(pinctrl_apple) = {
+       .name = "apple_pinctrl",
+       .id = UCLASS_PINCTRL,
+       .of_match = apple_pinctrl_ids,
+       .priv_auto = sizeof(struct apple_pinctrl_priv),
+       .ops = &apple_pinctrl_ops,
+       .probe = apple_pinctrl_probe,
+};