summaryrefslogtreecommitdiff
path: root/drivers/char/wmt_gpio.c
blob: 8441c5839f504884bc9fbd04de7a65b81e391ca8 (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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * linux/drivers/char/wmt_gpio.c
 * 
 * Copyright (c) 2008  WonderMedia Technologies, Inc.
 * 
 * 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.
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * WonderMedia Technologies, Inc.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/sysctl.h>

#include <mach/wmt_env.h>
#include <mach/wmt_iomux.h>
#include <linux/gpio.h>

#define DRVNAME "wmtgpio"

struct user_gpio {
	int gpio;
	int type;
	int value;
};

struct kern_gpio {
	struct user_gpio user;
	struct list_head list;
};

struct gpio_dev {
	int		major;
	struct class	*class;
	struct device	*dev;
	struct mutex	lock;
	struct list_head gpio_list;
};

enum {
        GPIO_DIRECTION_OUT = 0,
        GPIO_DIRECTION_IN,
};

static struct gpio_dev *gpio_device;

static int gpio_dev_open(struct inode *inode, struct file *filp)
{
	filp->private_data = gpio_device;
	return 0;
}

static int gpio_dev_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static int register_gpio(struct gpio_dev *gd, struct user_gpio *u)
{
	struct kern_gpio *k;
	struct list_head *node, *next;
	int rc;

	list_for_each_safe(node, next, &gd->gpio_list) {
		k = container_of(node, struct kern_gpio, list);
		if (k->user.gpio == u->gpio)
			goto requested;
	}

	rc = gpio_request(u->gpio, "user gpio");
	if (rc) {
		pr_err("gpio%d requested failed\n", u->gpio);
		return rc;
	}

	k = kzalloc(sizeof(*k), GFP_KERNEL);
	if (!k) {
		gpio_free(u->gpio);
		return -ENOMEM;
	}

	list_add_tail(&k->list, &gd->gpio_list);

requested:
	k->user = *u;
	if (u->type == GPIO_DIRECTION_OUT) { //output
		gpio_direction_output(k->user.gpio, k->user.value);
		printk(KERN_DEBUG "gpio%d direction output %d\n",
			k->user.gpio, k->user.value);
		return 0;
	}
	else if (u->type == GPIO_DIRECTION_IN) { //input
		rc = gpio_direction_input(k->user.gpio);
		rc = __gpio_get_value(k->user.gpio);
		return rc;
	}
	
	return -EINVAL;
}

static ssize_t gpio_dev_write(struct file *filp, const char __user *buf,
			      size_t count, loff_t *f_pos)
{
	struct gpio_dev *gd = filp->private_data;
	struct user_gpio ug;
	int rc = 0;

	if (count != sizeof(ug))
		return -EINVAL;

	if (copy_from_user(&ug, buf, count)) {
		return -EFAULT;
	}

	mutex_lock(&gd->lock);
	rc = register_gpio(gd, &ug);
	if (!rc) {
		rc = count;
	}
	mutex_unlock(&gd->lock);
	return rc;
}

static ssize_t gpio_dev_read(struct file *filp, char __user *buf,
			     size_t count, loff_t *f_pos)
{
	struct gpio_dev *gd = filp->private_data;
	struct user_gpio ug;
	int rc;
	
	if (copy_from_user(&ug, buf, sizeof(ug)))
		return -EFAULT;

	if (ug.type != GPIO_DIRECTION_IN)
		return -EINVAL;
	
	mutex_lock(&gd->lock);
	
	rc = register_gpio(gd, &ug);
	
	mutex_unlock(&gd->lock);
	
	if (rc == 0)
		copy_to_user(buf, "0:", sizeof("0:"));
	else if (rc == 1)
		copy_to_user(buf, "1:", sizeof("1:"));
	else
		copy_to_user(buf, "-1:", sizeof("-1:")); //error !!

	return count;
}

static struct file_operations gpio_fops = {
	.owner	= THIS_MODULE,
	.open	= gpio_dev_open,
	.write	= gpio_dev_write,
	.read	= gpio_dev_read,
	.release = gpio_dev_release,
};

static struct miscdevice gpio_miscdev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = DRVNAME,
	.fops = &gpio_fops,
};

static int __gpio_init(void)
{
	static char env[] = "wmt.gpio.pull";
	uint8_t buf[128];
	size_t l = sizeof(buf);
	uint8_t *p = buf;
	int gpio, pull;

	if (wmt_getsyspara(env, buf, &l))
		return -EINVAL;

	for (;;) {
		int rc = sscanf(p, "%d:%d", &gpio, &pull);
		if (rc != 2)
			break;
		printk(" gpio %d, pull %d\n", gpio, pull);
		if (gpio_is_valid(gpio))
			wmt_gpio_setpull(gpio, pull ? WMT_GPIO_PULL_UP
					 	: WMT_GPIO_PULL_DOWN);

		p = strchr(p, ',');
		if (!p)
			break;
		++p;
	}

	return 0;
}

static int gpio_dev_probe(struct platform_device *pdev)
{
	struct gpio_dev *gd;
	int ret;

	gd = kzalloc(sizeof(*gd), GFP_KERNEL);
	if (!gd) {
		dev_err(&pdev->dev, "no mem");
		return -ENOMEM;
	}

	ret = misc_register(&gpio_miscdev);
	if (ret) {
		kfree(gd);
		return ret;
	}

	INIT_LIST_HEAD(&gd->gpio_list);
	mutex_init(&gd->lock);
	gpio_device = gd;

	__gpio_init();
	return 0;
}

static int gpio_dev_remove(struct platform_device *pdev)
{
	struct gpio_dev *gd = gpio_device;
	struct list_head *node, *next;
	struct kern_gpio *k;

	if (!gd)
		return 0;

	dev_info(&pdev->dev, "wmt gpio dev unregistered: %d\n", gd->major);

	device_destroy(gd->class, MKDEV(gd->major, 0));
	class_destroy(gd->class);
	unregister_chrdev(gd->major, DRVNAME);

	list_for_each_safe(node, next, &gd->gpio_list) {
		k = container_of(node, struct kern_gpio, list);
		gpio_free(k->user.gpio);
		kfree(k);
	}

	kfree(gd);
	gpio_device = NULL;
	return 0;
}

static int gpio_dev_suspend(struct platform_device *pdev, pm_message_t state)
{
	dev_info(&pdev->dev, "wmt gpio dev suspend\n");
	return 0;
}

static int gpio_dev_resume(struct platform_device *pdev)
{
	struct gpio_dev *gd = gpio_device;
	struct list_head *node, *next;
	struct kern_gpio *k;

	if (!gd)
		return 0;

	dev_info(&pdev->dev, "wmt gpio dev resume\n");

	list_for_each_safe(node, next, &gd->gpio_list) {
		k = container_of(node, struct kern_gpio, list);
		printk(KERN_DEBUG "%s: gpio%d, type:%d, value %d\n",
		       __func__, k->user.gpio, k->user.type, k->user.value);
		gpio_re_enabled(k->user.gpio);
		if (k->user.type == GPIO_DIRECTION_OUT) { //output
			gpio_direction_output(k->user.gpio, k->user.value);
		}
		else if (k->user.type == GPIO_DIRECTION_IN) { //input
			gpio_direction_input(k->user.gpio);
		}	
	}

	return 0;
}

static struct platform_driver gpio_dev_driver = {
	.driver = {
		.name = DRVNAME,
	},
	.probe		= gpio_dev_probe,
	.remove		= gpio_dev_remove,
	.suspend	= gpio_dev_suspend,
	.resume		= gpio_dev_resume,
};

static struct platform_device *_pdev;

static int gpio_dev_init(void)
{
	int ret;

	_pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0);
	if (IS_ERR(_pdev)) {
		pr_err(DRVNAME "unable to register platform device\n");
		return PTR_ERR(_pdev);
	}

	ret = platform_driver_register(&gpio_dev_driver);
	if (ret)
		platform_device_unregister(_pdev);

	return ret;
}

static void gpio_dev_exit(void)
{
	platform_driver_unregister(&gpio_dev_driver);
	platform_device_unregister(_pdev);
}

module_init(gpio_dev_init);
module_exit(gpio_dev_exit);

MODULE_AUTHOR("WonderMedia Technologies, Inc.");
MODULE_DESCRIPTION("WMT [gpio device] driver");
MODULE_LICENSE("GPL");