summaryrefslogtreecommitdiff
path: root/drivers/gpio/hi6220_gpio.c
blob: 97a041789171651633320b2956c3b577dfa5ccc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015 Linaro
 * Peter Griffin <peter.griffin@linaro.org>
 */

#include <common.h>
#include <dm.h>
#include <asm/gpio.h>
#include <asm/io.h>
#include <errno.h>

static int hi6220_gpio_direction_input(struct udevice *dev, unsigned int gpio)
{
	struct gpio_bank *bank = dev_get_priv(dev);
	u8 data;

	data = readb(bank->base + HI6220_GPIO_DIR);
	data &= ~(1 << gpio);
	writeb(data, bank->base + HI6220_GPIO_DIR);

	return 0;
}

static int hi6220_gpio_set_value(struct udevice *dev, unsigned gpio,
				  int value)
{
	struct gpio_bank *bank = dev_get_priv(dev);

	writeb(!!value << gpio, bank->base + (BIT(gpio + 2)));
	return 0;
}

static int hi6220_gpio_direction_output(struct udevice *dev, unsigned gpio,
					int value)
{
	struct gpio_bank *bank = dev_get_priv(dev);
	u8 data;

	data = readb(bank->base + HI6220_GPIO_DIR);
	data |= 1 << gpio;
	writeb(data, bank->base + HI6220_GPIO_DIR);

	hi6220_gpio_set_value(dev, gpio, value);

	return 0;
}

static int hi6220_gpio_get_value(struct udevice *dev, unsigned gpio)
{
	struct gpio_bank *bank = dev_get_priv(dev);

	return !!readb(bank->base + (BIT(gpio + 2)));
}



static const struct dm_gpio_ops gpio_hi6220_ops = {
	.direction_input	= hi6220_gpio_direction_input,
	.direction_output	= hi6220_gpio_direction_output,
	.get_value		= hi6220_gpio_get_value,
	.set_value		= hi6220_gpio_set_value,
};

static int hi6220_gpio_probe(struct udevice *dev)
{
	struct gpio_bank *bank = dev_get_priv(dev);
	struct hikey_gpio_platdata *plat = dev_get_platdata(dev);
	struct gpio_dev_priv *uc_priv = dev->uclass_priv;
	char name[18], *str;

	sprintf(name, "GPIO%d_", plat->bank_index);

	str = strdup(name);
	if (!str)
		return -ENOMEM;

	uc_priv->bank_name = str;
	uc_priv->gpio_count = HI6220_GPIO_PER_BANK;

	bank->base = (u8 *)plat->base;

	return 0;
}

U_BOOT_DRIVER(gpio_hi6220) = {
	.name	= "gpio_hi6220",
	.id	= UCLASS_GPIO,
	.ops	= &gpio_hi6220_ops,
	.probe	= hi6220_gpio_probe,
	.priv_auto_alloc_size = sizeof(struct gpio_bank),
};