2 * Synopsys HSDK SDP Generic PLL clock driver
4 * Copyright (C) 2017 Synopsys
5 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <asm-generic/gpio.h>
17 #include <linux/printk.h>
19 #define HSDK_CREG_MAX_GPIO 8
21 #define GPIO_ACTIVATE 0x2
22 #define GPIO_DEACTIVATE 0x3
23 #define GPIO_PIN_MASK 0x3
24 #define BIT_PER_GPIO 2
26 struct hsdk_creg_gpio {
30 static int hsdk_creg_gpio_set_value(struct udevice *dev, unsigned oft, int val)
32 struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
33 uint32_t reg = readl(hcg->regs);
34 uint32_t cmd = val ? GPIO_DEACTIVATE : GPIO_ACTIVATE;
36 reg &= ~(GPIO_PIN_MASK << (oft * BIT_PER_GPIO));
37 reg |= (cmd << (oft * BIT_PER_GPIO));
39 writel(reg, hcg->regs);
44 static int hsdk_creg_gpio_direction_output(struct udevice *dev, unsigned oft,
47 hsdk_creg_gpio_set_value(dev, oft, val);
52 static int hsdk_creg_gpio_direction_input(struct udevice *dev, unsigned oft)
54 pr_err("hsdk-creg-gpio can't be used as input!\n");
59 static int hsdk_creg_gpio_get_value(struct udevice *dev, unsigned int oft)
61 struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
62 uint32_t val = readl(hcg->regs);
64 val = (val >> (oft * BIT_PER_GPIO)) & GPIO_PIN_MASK;
65 return (val == GPIO_DEACTIVATE) ? 1 : 0;
68 static const struct dm_gpio_ops hsdk_creg_gpio_ops = {
69 .direction_output = hsdk_creg_gpio_direction_output,
70 .direction_input = hsdk_creg_gpio_direction_input,
71 .set_value = hsdk_creg_gpio_set_value,
72 .get_value = hsdk_creg_gpio_get_value,
75 static int hsdk_creg_gpio_probe(struct udevice *dev)
77 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
78 struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
80 hcg->regs = (uint32_t *)devfdt_get_addr_ptr(dev);
82 uc_priv->gpio_count = dev_read_u32_default(dev, "gpio-count", 1);
83 if (uc_priv->gpio_count > HSDK_CREG_MAX_GPIO)
84 uc_priv->gpio_count = HSDK_CREG_MAX_GPIO;
86 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
87 if (!uc_priv->bank_name)
88 uc_priv->bank_name = dev_read_name(dev);
90 pr_debug("%s GPIO [0x%p] controller with %d gpios probed\n",
91 uc_priv->bank_name, hcg->regs, uc_priv->gpio_count);
96 static const struct udevice_id hsdk_creg_gpio_ids[] = {
97 { .compatible = "snps,hsdk-creg-gpio" },
101 U_BOOT_DRIVER(gpio_hsdk_creg) = {
102 .name = "gpio_hsdk_creg",
104 .ops = &hsdk_creg_gpio_ops,
105 .probe = hsdk_creg_gpio_probe,
106 .of_match = hsdk_creg_gpio_ids,
107 .platdata_auto_alloc_size = sizeof(struct hsdk_creg_gpio),