summaryrefslogtreecommitdiff
path: root/gpio-mock.c
blob: 6c568c7fd569a5479da6cda5e175cc19142abe75 (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
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
/**
 * Copyright (C) Linaro 2021. All rights reserved.
 *
 * Viresh Kumar <viresh.kumar@linaro.org>
 *
 * 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.
 */

#define pr_fmt(fmt) "GPIO Mock: " fmt

#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/printk.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/gpio/driver.h>
#include <linux/workqueue.h>

#define NGPIO	64

struct gpio_line {
	int dir;
	int val;
};

struct mock_gpio_chip {
	struct gpio_chip gc;
	struct gpio_line lines[NGPIO];
};
static struct mock_gpio_chip *mock_gc;
static volatile unsigned int irq_offset;
static struct dentry *rootdir;

static const char *gpio_line_names[NGPIO] = {
	"gpio0",
	"gpio1",
	"gpio2",
	"gpio3",
	"gpio4",
	"gpio5",
	"gpio6",
	"gpio7",
	"gpio8",
	"gpio9",
	"gpio10",
	"gpio11",
	"gpio12",
};

static void mock_gpio_irq(struct work_struct *work);
static DECLARE_WORK(work, mock_gpio_irq);

static int mock_gpio_get(struct gpio_chip *gc, unsigned offset)
{
	struct mock_gpio_chip *mock_gc = gpiochip_get_data(gc);

	pr_info("%s: %d: %u\n", __func__, __LINE__, offset);
	return mock_gc->lines[offset].val;
}

static void mock_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
{
	struct mock_gpio_chip *mock_gc = gpiochip_get_data(gc);

	mock_gc->lines[offset].val = value;
	pr_info("%s: %d: %u\n", __func__, __LINE__, offset);
}

static int mock_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
{
	struct mock_gpio_chip *mock_gc = gpiochip_get_data(gc);

	pr_info("%s: %d: %u\n", __func__, __LINE__, offset);
	return mock_gc->lines[offset].dir;
}

static int mock_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
{
	struct mock_gpio_chip *mock_gc = gpiochip_get_data(gc);

	pr_info("%s: %d: %u\n", __func__, __LINE__, offset);
	mock_gc->lines[offset].dir = 1;
	return 0;
}

static int mock_gpio_direction_output(struct gpio_chip *gc,
		unsigned offset, int value)
{
	struct mock_gpio_chip *mock_gc = gpiochip_get_data(gc);

	pr_info("%s: %d: %u\n", __func__, __LINE__, offset);
	mock_gc->lines[offset].val = value;
	mock_gc->lines[offset].dir = 0;
	return 0;
}

static void mock_gpio_irq_enable(struct irq_data *d)
{
	pr_info("%s: %d: %lu\n", __func__, __LINE__, d->hwirq);

	irq_offset = d->hwirq;
	schedule_work(&work);
}

static void mock_gpio_irq_disable(struct irq_data *d)
{
	pr_info("%s: %d: %lu\n", __func__, __LINE__, d->hwirq);
}

static void mock_gpio_irq_mask(struct irq_data *d)
{
	pr_info("%s: %d: %lu\n", __func__, __LINE__, d->hwirq);
}

static void mock_gpio_irq_unmask(struct irq_data *d)
{
	pr_info("%s: %d: %lu\n", __func__, __LINE__, d->hwirq);
}

static int mock_gpio_irq_set_type(struct irq_data *d, unsigned int type)
{
	pr_info("%s: %d: %lu\n", __func__, __LINE__, d->hwirq);

	return 0;
}

static struct irq_chip irq_chip = {
	.name			= "mock-gpio",
	.irq_enable		= mock_gpio_irq_enable,
	.irq_disable		= mock_gpio_irq_disable,
	.irq_mask		= mock_gpio_irq_mask,
	.irq_unmask		= mock_gpio_irq_unmask,
	.irq_set_type		= mock_gpio_irq_set_type,
};

static void mock_gpio_irq(struct work_struct *work)
{
	unsigned int irq = irq_find_mapping(mock_gc->gc.irq.domain, irq_offset);
	pr_info("%s: %d: %u\n", __func__, __LINE__, irq_offset);

//	local_irq_disable();
	handle_nested_irq(irq);
	pr_info("%s: %d: %u\n", __func__, __LINE__, irq_offset);

//	local_irq_enable();
}

static int mock_gpio_debugfs(void *data, u64 val)
{
	if (val >= NGPIO) {
		pr_info("Invalid gpio number %s: %d\n", __func__, __LINE__);
		return 0;
	}

	irq_offset = val;
	schedule_work(&work);

	return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(mock_gpio_fops, NULL, mock_gpio_debugfs, "%llu\n");

static int mock_gpio_probe(struct platform_device *pdev)
{
	int i, ret;

	pr_info("%s: %d\n", __func__, __LINE__);
	mock_gc = devm_kzalloc(&pdev->dev, sizeof(*mock_gc), GFP_KERNEL);
	if (!mock_gc)
		return -ENOMEM;

	mock_gc->gc.get_direction	= mock_gpio_get_direction;
	mock_gc->gc.direction_input	= mock_gpio_direction_input;
	mock_gc->gc.direction_output	= mock_gpio_direction_output;
	mock_gc->gc.get			= mock_gpio_get;
	mock_gc->gc.set			= mock_gpio_set;
	mock_gc->gc.owner		= THIS_MODULE;
	mock_gc->gc.parent		= &pdev->dev;
	mock_gc->gc.ngpio		= NGPIO;
	mock_gc->gc.base		= -1;
	mock_gc->gc.label		= dev_name(&pdev->dev);
	mock_gc->gc.names		= gpio_line_names;
	mock_gc->gc.owner		= THIS_MODULE;
	mock_gc->gc.irq.parent_handler	= NULL;
	mock_gc->gc.irq.num_parents	= 0;
	mock_gc->gc.irq.parents		= NULL;
	mock_gc->gc.irq.default_type	= IRQ_TYPE_NONE;
	mock_gc->gc.irq.handler		= handle_edge_irq;
	mock_gc->gc.irq.threaded	= true;
	mock_gc->gc.irq.chip		= &irq_chip;

	for (i = 0; i < NGPIO; i++)
		mock_gc->lines[i].dir = 1;

	ret = gpiochip_add_data(&mock_gc->gc, mock_gc);
	if (ret) {
		dev_err(&pdev->dev, "Failed to add mock-gpio controller\n");
	} else {
		platform_set_drvdata(pdev, mock_gc);
		pr_info("%s: %d\n", __func__, __LINE__);
	}

	return ret;
}

static int mock_gpio_remove(struct platform_device *pdev)
{
	struct mock_gpio_chip *mock_gc = platform_get_drvdata(pdev);

	pr_info("%s: %d\n", __func__, __LINE__);
	gpiochip_remove(&mock_gc->gc);
	return 0;
}

static struct platform_driver mock_gpio_driver = {
	.driver = {
		.name	= "mock_gpio",
	},
	.probe		= mock_gpio_probe,
	.remove		= mock_gpio_remove,
};

static struct platform_device *gpdev;

static int __init mock_gpio_init(void)
{
	int ret = -EINVAL;

	gpdev = platform_device_register_data(NULL, "mock_gpio", -1, NULL, 0);
	if (!IS_ERR(gpdev)) {
		ret = platform_driver_register(&mock_gpio_driver);
		if (ret)
			platform_device_unregister(gpdev);

	}

	rootdir = debugfs_create_dir("mockgpio", NULL);
	debugfs_create_file("gpio", S_IRUGO | S_IWUSR, rootdir, NULL,
			&mock_gpio_fops);

	pr_info("%s: %d: %d\n", __func__, __LINE__, ret);
	return ret;
}
module_init(mock_gpio_init);

static void __exit mock_gpio_exit(void)
{
	pr_info("%s: %d\n", __func__, __LINE__);
	platform_driver_unregister(&mock_gpio_driver);
	platform_device_unregister(gpdev);
	debugfs_remove_recursive(rootdir);
}
module_exit(mock_gpio_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
MODULE_DESCRIPTION("Mock GPIO");