summaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpio-digic.c
blob: 714e3b4a1db289e4dfbdcb2236c31a2d8c0829c7 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
 *
 * This file is part of barebox.
 * See file CREDITS for list of people who contributed to this project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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 <malloc.h>
#include <errno.h>
#include <io.h>
#include <gpio.h>
#include <init.h>

/*
 * See http://magiclantern.wikia.com/wiki/Register_Map#GPIO_Ports
 */

#define DIGIC_GPIO_IN_LVL	1
#define DIGIC_GPIO_OUT_LVL	2
#define DIGIC_GPIO_DIR	4
#define DIGIC_GPIO_TRISTATE	8

struct digic_gpio_chip {
	void __iomem		*base;
	struct gpio_chip	gc;
};

#define to_digic_gpio_chip(c) container_of(c, struct digic_gpio_chip, gc)

static inline uint32_t digic_gpio_readl(struct digic_gpio_chip *chip,
					uint32_t offset)
{
	return readl(chip->base + 4 * offset);
}

static inline void digic_gpio_writel(struct digic_gpio_chip *chip,
					uint32_t value, uint32_t offset)
{
	writel(value, chip->base + 4 * offset);
}

static int digic_gpio_get_value(struct gpio_chip *gc, unsigned offset)
{
	struct digic_gpio_chip *chip = to_digic_gpio_chip(gc);

	if (offset >= gc->ngpio)
		return -EINVAL;

	return digic_gpio_readl(chip, offset) & DIGIC_GPIO_IN_LVL;
}

static void digic_gpio_set_value(struct gpio_chip *gc, unsigned offset,
					int value)
{
	struct digic_gpio_chip *chip = to_digic_gpio_chip(gc);
	uint32_t t;

	if (offset >= gc->ngpio)
		return;

	t = digic_gpio_readl(chip, offset);
	/* Port direction (1 = OUT, 0 = IN) */
	if (value)
		t |= DIGIC_GPIO_OUT_LVL;
	else
		t &= ~(DIGIC_GPIO_OUT_LVL);
	digic_gpio_writel(chip, t, offset);
}

static int digic_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
{
	struct digic_gpio_chip *chip = to_digic_gpio_chip(gc);
	uint32_t t;

	if (offset >= gc->ngpio)
		return -EINVAL;

	t = digic_gpio_readl(chip, offset);
	/* Port direction (1 = OUT, 0 = IN) */
	t &= ~(DIGIC_GPIO_DIR);
	digic_gpio_writel(chip, t, offset);

	return 0;
}

static int digic_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
		int value)
{
	struct digic_gpio_chip *chip = to_digic_gpio_chip(gc);
	uint32_t t;

	if (offset >= gc->ngpio)
		return -EINVAL;

	t = digic_gpio_readl(chip, offset);
	/* Port direction (1 = OUT, 0 = IN) */
	t |= DIGIC_GPIO_DIR;
	digic_gpio_writel(chip, t, offset);

	digic_gpio_set_value(gc, offset, value);

	return 0;
}

static struct gpio_ops digic_gpio_ops = {
	.direction_input = digic_gpio_direction_input,
	.direction_output = digic_gpio_direction_output,
	.get = digic_gpio_get_value,
	.set = digic_gpio_set_value,
};

static int digic_gpio_probe(struct device_d *dev)
{
	struct resource *iores;
	struct digic_gpio_chip *chip;
	struct resource *res;
	resource_size_t rsize;
	int ret = -EINVAL;

	chip = xzalloc(sizeof(*chip));

	res = dev_get_resource(dev, IORESOURCE_MEM, 0);
	if (!res)
		goto err;

	rsize = resource_size(res);
	chip->gc.ngpio = rsize / sizeof(int32_t);

	iores = dev_request_mem_resource(dev, 0);
	if (IS_ERR(iores))
		return PTR_ERR(iores);
	chip->base = IOMEM(iores->start);
	chip->gc.ops = &digic_gpio_ops;
	chip->gc.base = 0;

	chip->gc.dev = dev;

	ret = gpiochip_add(&chip->gc);
	if (ret) {
		dev_err(dev, "couldn't add gpiochip, ret = %d\n", ret);
		goto err;
	}

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

	return 0;

err:
	kfree(chip);

	return ret;
}

static __maybe_unused struct of_device_id digic_gpio_dt_ids[] = {
	{
		.compatible = "canon,digic-gpio",
	}, {
		/* sentinel */
	}
};

static struct driver_d digic_gpio_driver = {
	.name = "digic-gpio",
	.probe = digic_gpio_probe,
	.of_compatible = DRV_OF_COMPAT(digic_gpio_dt_ids),
};

static int digic_gpio_init(void)
{
	return platform_driver_register(&digic_gpio_driver);
}
coredevice_initcall(digic_gpio_init);