aboutsummaryrefslogtreecommitdiff
path: root/1/main.c
blob: d8ea7fc1360ce773908af8da21debbce568393c0 (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
#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/io.h>

#include "fopdrv.h"
#include "config.h"

int one = 0;
module_param(one, int, 0400);
MODULE_PARM_DESC(one, "first parameter, only printed on init");

int not_named_two = 0;
module_param_named(two, not_named_two, int, 0400);
MODULE_PARM_DESC(not_named_two, "second parameter, only printed on init");

struct cdev *cdev;
struct device *dev;
struct class *class;
dev_t node;
struct file_operations fops = {
	.read = fop_read,
	.write = fop_write,
	.open = fop_open,
	.release = fop_release,
};

static int mod_init(void) {
	int err;

	err = alloc_chrdev_region(&node, 0, 1, DRV_NAME);
	if (err < 0) {
		printk(KERN_ERR "alloc_chrdev_region error %d\n", err);
		goto return_err;
	}

	class = class_create(DRV_NAME);
	if (IS_ERR_OR_NULL(class)) {
		err = PTR_ERR(class);
		printk(KERN_ERR "class_create error %d\n", err);
		goto free_class;
	}

	cdev = cdev_alloc();
	if (!cdev) {
		err = ENOMEM;
		goto free_cdev;
	}

	cdev->ops = &fops;
	cdev_init(cdev, &fops);
	err = cdev_add(cdev, node, 1);
	if (err < 0) {
		printk(KERN_ERR "cdev_add error %d\n", err);
		goto free_cdev;
	}

	dev = device_create(class, NULL, node, NULL, DRV_NAME);
	if (IS_ERR_OR_NULL(dev)) {
		err = PTR_ERR(dev);
		printk(KERN_ERR "device_create error %d\n", err);
		goto free_device;
	}

	printk("%s() -> 0 (%d:%d)\n", __PRETTY_FUNCTION__, MAJOR(node), MINOR(node));
	printk("one = %d, two = %d\n", one, not_named_two);

	// configure gpio mux
	uint32_t* conf_gpmc_a3 = ioremap(TI_AM335X_CM_GPMC_A3_ADDR, sizeof(ti_am335x_word_t));
	if (conf_gpmc_a3 == NULL)
		return -EINVAL;
	barrier();
	uint32_t val = ioread32(conf_gpmc_a3); rmb();
	val &= ~0x7;
	val |= 0x7;
	iowrite32(val, conf_gpmc_a3); wmb();
	iounmap(conf_gpmc_a3);

	// configure output pin
	ti_am335x_word_t* gpio1 = ioremap(TI_AM335X_GPIO1_ADDR, GPIO_REG_SIZE);
	barrier();
	ti_am335x_word_t oe_reg = ioread32(gpio1 + GPIO_OE); rmb();
	oe_reg &= ~(1<<PIN); // 0=output
	iowrite32(oe_reg, gpio1 + GPIO_OE); wmb();
	iounmap(gpio1);

	return 0;

free_device:
	device_destroy(class, node);
free_cdev:
	cdev_del(cdev);
free_class:
	class_destroy(class);
return_err:
	printk("%s() -> %d\n", __PRETTY_FUNCTION__, err);
	return err;
}

static void mod_exit(void) {
	device_destroy(class, node);
	cdev_del(cdev);
	class_destroy(class);
	printk("%s()\n", __PRETTY_FUNCTION__);
}

module_init(mod_init);
module_exit(mod_exit);
MODULE_LICENSE("Dual MIT/GPL");