-
Notifications
You must be signed in to change notification settings - Fork 0
/
altera_pio.c
293 lines (242 loc) · 6.82 KB
/
altera_pio.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: GPL-2.0
/** @file flx_frs_main.c
* @brief Parallel I/O to general purpose I/O
*/
/*
* Parallel I/O to general purpose I/O Linux driver
*
* Copyright (C) 2013 Flexibilis Oy
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define DRV_NAME "flx_pio"
#define DRV_VERSION "1.0.0"
/// PIO data register
#define FLX_PIO_DATA_REG 0x0
/// PIO direction register
#define FLX_PIO_DIR_REG 0x4
/// PIO interrupt mask register
#define FLX_PIO_IRQ_MASK_REG 0x8
/// PIO edge capture register
#define FLX_PIO_EDGE_CAPTURE_REG 0xc
/// PIO value set register to set individual output bits
#define FLX_PIO_SET_REG 0x10
/// PIO value set register to clear individual output bits
#define FLX_PIO_CLR_REG 0x14
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/spinlock.h>
#include <linux/of.h>
#include <linux/version.h>
#include "altera_pio.h"
/**
* PIO device context
*/
struct flx_pio_dev_priv {
struct device *dev;
struct list_head list; ///< linked list
unsigned int width; ///< Number of I/O on device
struct gpio_chip gpio_chip; ///< GPIO chip for PIO
u32 dir; ///< Current PIO direction bits
/* Serialize dir changes */
spinlock_t lock; ///< Serialize dir changes
void __iomem *ioaddr; ///< Memory mapped register I/O
};
/**
* GPIO chip interface: set GPIO direction as input
*/
static int flx_pio_gpio_direction_input(struct gpio_chip *chip,
unsigned int offset)
{
struct flx_pio_dev_priv *dp = container_of(chip,
struct flx_pio_dev_priv,
gpio_chip);
spin_lock_bh(&dp->lock);
dp->dir &= ~(1u << offset);
iowrite32(dp->dir, dp->ioaddr + FLX_PIO_DIR_REG);
spin_unlock_bh(&dp->lock);
dev_info(dp->dev, "DIR %u input 0x%08x DIR 0x%08x DATA %08x\n",
offset, dp->dir,
ioread32(dp->ioaddr + FLX_PIO_DIR_REG),
ioread32(dp->ioaddr + FLX_PIO_DATA_REG));
return 0;
}
/**
* GPIO chip interface: set GPIO direction as output
*/
static int flx_pio_gpio_direction_output(struct gpio_chip *chip,
unsigned int offset, int value)
{
struct flx_pio_dev_priv *dp = container_of(chip,
struct flx_pio_dev_priv,
gpio_chip);
spin_lock_bh(&dp->lock);
if (value)
iowrite32(1u << offset, dp->ioaddr + FLX_PIO_SET_REG);
else
iowrite32(1u << offset, dp->ioaddr + FLX_PIO_CLR_REG);
dp->dir |= 1u << offset;
iowrite32(dp->dir, dp->ioaddr + FLX_PIO_DIR_REG);
spin_unlock_bh(&dp->lock);
dev_info(dp->dev, "DIR %u output 0x%08x DIR 0x%08x DATA %08x\n",
offset, dp->dir,
ioread32(dp->ioaddr + FLX_PIO_DIR_REG),
ioread32(dp->ioaddr + FLX_PIO_DATA_REG));
return 0;
}
/**
* GPIO chip interface: Get GPIO value
*/
static int flx_pio_gpio_get(struct gpio_chip *chip, unsigned int offset)
{
struct flx_pio_dev_priv *dp = container_of(chip,
struct flx_pio_dev_priv,
gpio_chip);
u32 tmp;
tmp = ioread32(dp->ioaddr + FLX_PIO_DATA_REG);
dev_info(dp->dev, "GET %u value %u DIR 0x%08x DATA %08x\n",
offset, (tmp & (1u << offset)),
ioread32(dp->ioaddr + FLX_PIO_DIR_REG),
ioread32(dp->ioaddr + FLX_PIO_DATA_REG));
return (tmp & (1u << offset));
}
/**
* GPIO chip interface: set GPIO value
*/
static void flx_pio_gpio_set(struct gpio_chip *chip,
unsigned int offset, int value)
{
struct flx_pio_dev_priv *dp = container_of(chip,
struct flx_pio_dev_priv,
gpio_chip);
if (value)
iowrite32(1u << offset, dp->ioaddr + FLX_PIO_SET_REG);
else
iowrite32(1u << offset, dp->ioaddr + FLX_PIO_CLR_REG);
dev_info(dp->dev, "SET %u to %u DIR 0x%08x DATA %08x\n",
offset, value,
ioread32(dp->ioaddr + FLX_PIO_DIR_REG),
ioread32(dp->ioaddr + FLX_PIO_DATA_REG));
}
/**
* Turn PIO device to GPIOs.
* @param pdev Platform_device providing PIO
*/
int flx_pio_probe_one(unsigned int pio_id, struct device *dev, void *base,
struct flx_pio_dev_priv **pio_ret)
{
int ret = -ENXIO;
struct flx_pio_dev_priv *dp = NULL;
struct gpio_chip *chip = NULL;
u32 value = 32;
dev_info(dev, "Init PIO dev %i\n", pio_id);
dp = kmalloc(sizeof(*dp), GFP_KERNEL);
if (!dp) {
ret = -ENOMEM;
goto err_alloc_priv;
}
dp->dev = dev;
dp->ioaddr = NULL;
INIT_LIST_HEAD(&dp->list);
spin_lock_init(&dp->lock);
#ifdef CONFIG_OF
ret = of_property_read_u32(dev->of_node, "width", &value);
#else
value = 32;
ret = 0;
#endif
if (ret) {
dev_info(dev, "Missing width\n");
ret = -EINVAL;
goto err_width;
} else {
dp->width = value;
}
dp->ioaddr = base;
// Direction register is not correct if PIO is fixed to output.
#ifdef CONFIG_OF
ret = of_property_read_u32(dev->of_node, "direction", &value);
if (ret == 0)
dp->dir = value;
#else
ret = -ENOENT;
#endif
if (ret)
dp->dir = ioread32(dp->ioaddr + FLX_PIO_DIR_REG);
chip = &dp->gpio_chip;
*chip = (struct gpio_chip) {
.label = DRV_NAME,
// dev was renamed in Linux 4.5.
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
.dev = dev,
#else
.parent = dev,
#endif
.direction_input = &flx_pio_gpio_direction_input,
.direction_output = &flx_pio_gpio_direction_output,
.get = &flx_pio_gpio_get,
.set = &flx_pio_gpio_set,
// Allocate GPIO numbers dynamically
.base = -1,
.ngpio = dp->width,
.can_sleep = 0,
};
dev_info(dev, "Adding gpio chip\n");
ret = gpiochip_add(chip);
if (ret) {
dev_err(dev, "Failed to add gpio chip\n");
goto err_gpiochip_add;
}
dev_info(dev, "Added gpio base %i\n", chip->base);
dev_info(dev, "Added PIO device GPIOs %u .. %u\n",
chip->base, chip->base + chip->ngpio - 1);
*pio_ret = dp;
/* FIXME: workaround for PHY reset */
flx_pio_gpio_direction_output(chip, 0, 0);
flx_pio_gpio_set(chip, 0, 0);
msleep(300);
flx_pio_gpio_set(chip, 0, 1);
msleep(300);
return 0;
err_gpiochip_add:
err_width:
kfree(dp);
err_alloc_priv:
return ret;
}
/**
* Cleanup GPIO device for PIO.
* @param pdev Platform device of PIO.
*/
void flx_pio_shutdown(struct flx_pio_dev_priv *dp)
{
if (!dp)
return;
dev_info(dp->dev, "Removing GPIO chip GPIOs %u .. %u\n",
dp->gpio_chip.base,
dp->gpio_chip.base + dp->gpio_chip.ngpio - 1);
gpiochip_remove(&dp->gpio_chip);
kfree(dp);
}
void *flx_pio_get_base(struct flx_pio_dev_priv *dp)
{
if (!dp)
return NULL;
return dp->ioaddr;
}