summaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpio-jz4740.c
blob: 87e0716b06a9105a2cde9dd699cef75a4e48b711 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2013, 2014 Antony Pavlov <antonynpavlov@gmail.com>
 *
 * Based on Linux JZ4740 platform GPIO support:
 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <common.h>
#include <init.h>
#include <io.h>
#include <gpio.h>
#include <malloc.h>
#include <linux/err.h>

#define JZ_REG_GPIO_PIN			0x00
#define JZ_REG_GPIO_DATA		0x10
#define JZ_REG_GPIO_DATA_SET		0x14
#define JZ_REG_GPIO_DATA_CLEAR		0x18
#define JZ_REG_GPIO_DIRECTION		0x60
#define JZ_REG_GPIO_DIRECTION_SET	0x64
#define JZ_REG_GPIO_DIRECTION_CLEAR	0x68

#define GPIO_TO_BIT(gpio) BIT(gpio & 0x1f)
#define CHIP_TO_REG(chip, reg) (gpio_chip_to_jz4740_gpio_chip(chip)->base + (reg))

struct jz4740_gpio_chip {
	void __iomem *base;
	struct gpio_chip chip;
};

static inline struct jz4740_gpio_chip *gpio_chip_to_jz4740_gpio_chip(struct gpio_chip *chip)
{
	return container_of(chip, struct jz4740_gpio_chip, chip);
}

static int jz4740_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
{
	return !!(readl(CHIP_TO_REG(chip, JZ_REG_GPIO_PIN)) & BIT(gpio));
}

static void jz4740_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
{
	uint32_t __iomem *reg = CHIP_TO_REG(chip, JZ_REG_GPIO_DATA_SET);
	reg += !value;
	writel(BIT(gpio), reg);
}

static int jz4740_gpio_get_direction(struct gpio_chip *chip, unsigned gpio)
{
	if (readl(CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION)) & BIT(gpio))
		return GPIOF_DIR_OUT;

	return GPIOF_DIR_IN;
}

static int jz4740_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
{
	writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_CLEAR));

	return 0;
}

static int jz4740_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
	int value)
{
	jz4740_gpio_set_value(chip, gpio, value);
	writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_SET));

	return 0;
}

static struct gpio_ops jz4740_gpio_ops = {
	.direction_input = jz4740_gpio_direction_input,
	.direction_output = jz4740_gpio_direction_output,
	.get_direction = jz4740_gpio_get_direction,
	.get = jz4740_gpio_get_value,
	.set = jz4740_gpio_set_value,
};

static int jz4740_gpio_probe(struct device_d *dev)
{
	struct resource *iores;
	void __iomem *base;
	struct jz4740_gpio_chip *jz4740_gpio;
	int ret;

	iores = dev_request_mem_resource(dev, 0);
	if (IS_ERR(iores)) {
		dev_err(dev, "could not get memory region\n");
		return PTR_ERR(iores);
	}
	base = IOMEM(iores->start);

	jz4740_gpio = xzalloc(sizeof(*jz4740_gpio));
	jz4740_gpio->base = base;
	jz4740_gpio->chip.ops = &jz4740_gpio_ops;
	jz4740_gpio->chip.base = -1; /* dev->id * 32; */
	jz4740_gpio->chip.ngpio = 32;
	jz4740_gpio->chip.dev = dev;

	ret = gpiochip_add(&jz4740_gpio->chip);
	if (ret) {
		dev_err(dev, "couldn't add gpiochip\n");
		free(jz4740_gpio);
		return ret;
	}

	dev_dbg(dev, "probed gpiochip%d with base %d\n",
				dev->id, jz4740_gpio->chip.base);

	return 0;
}

static __maybe_unused struct of_device_id jz4740_gpio_dt_ids[] = {
	{
		.compatible = "ingenic,jz4740-gpio",
	}, {
		/* sentinel */
	},
};

static struct driver_d jz4740_gpio_driver = {
	.name = "jz4740-gpio",
	.probe = jz4740_gpio_probe,
	.of_compatible	= DRV_OF_COMPAT(jz4740_gpio_dt_ids),
};

static int jz4740_gpio_init(void)
{
	return platform_driver_register(&jz4740_gpio_driver);
}
coredevice_initcall(jz4740_gpio_init);