aboutsummaryrefslogtreecommitdiff
path: root/driver/platform.c
blob: b177474f69a0ad3cb6d7b312b55bb7e728267c53 (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
#include "platform.h"

#include <linux/types.h>
#include <linux/of.h>
#include <linux/of_address.h>

// TODO: shouldn't these be used??
// #include <linux/pinctrl/pinctrl.h>
// #include <linux/gpio/driver.h>

#include <dt-bindings/gpio/gpio.h>

// NOTE: i believe any nonzero return value for the probe function is
// recognized as an error because of
// <https://github.com/torvalds/linux/blob/a5131c3fdf2608f1c15f3809e201cf540eb28489/drivers/base/platform.c#L1004>
int lork_probe(struct platform_device* dev) {
	// read gpios field from device tree
	uint32_t dt_gpios[3];
	of_property_read_u32_array(dev->dev.of_node, "gpios", dt_gpios, 3);

	struct device_node* gpio1 = of_find_node_by_phandle(dt_gpios[0]);
	uint32_t pin = dt_gpios[1];
	uint32_t gpio_mode = dt_gpios[2];
	printk("lork: configure pin %u w/ mode 0x%02x\n", pin, gpio_mode);

	// find the multiplexer that controls pin
	size_t len = of_property_count_u32_elems(gpio1, "gpio-ranges");
	struct device_node* am33xx_pinmux = NULL;
	for (size_t i = 0; i < len; i += 4) {
		uint32_t phandle, pin_start, pin_offset, pin_size;
		of_property_read_u32_index(gpio1, "gpio-ranges", i+0, &phandle);
		of_property_read_u32_index(gpio1, "gpio-ranges", i+1, &pin_start);
		of_property_read_u32_index(gpio1, "gpio-ranges", i+2, &pin_offset);
		of_property_read_u32_index(gpio1, "gpio-ranges", i+3, &pin_size);

		// get phandle for multiplexer corresponding to configured pin number
		if (pin_start > pin || pin >= pin_start + pin_size) continue;

		am33xx_pinmux = of_find_node_by_phandle(phandle);
		printk("0x%02x, %u -> %u + %u\n", phandle, pin_start, pin_offset, pin_size);
		break;
	}
	if (am33xx_pinmux == NULL) return EINVAL;

	printk("%s(%s)\n", __PRETTY_FUNCTION__, dev->name);
	return 0;
}

int lork_remove(struct platform_device* dev) {
	printk("%s(%s)\n", __PRETTY_FUNCTION__, dev->name);
	return 0;
}

unsigned int val = 0;
ssize_t drv_attr_int_show(struct device_driver* drv, char* buf) {
	return sprintf(buf, "%u\n", val);
}
ssize_t drv_attr_int_store(struct device_driver* drv, const char* buf, size_t count) {
	sscanf(buf, "%u", &val);
	return count;
}

ssize_t dev_attr_int_show(struct device* dev, struct device_attribute* _attr, char* buf) {
	return sprintf(buf, "%u\n", val);
}
ssize_t dev_attr_int_store(struct device* dev, struct device_attribute* _attr, const char* buf, size_t count) {
	sscanf(buf, "%u", &val);
	return count;
}

char* str = NULL;
size_t str_len = 0;
ssize_t dev_attr_str_show(struct device* dev, struct device_attribute* _attr, char* buf) {
	if (str == NULL) return 0;
	strncpy(buf, str, str_len);
	return str_len;
}
ssize_t dev_attr_str_store(struct device* dev, struct device_attribute* _attr, const char* buf, size_t count) {
	if (str != NULL) kfree(str);
	str = kmalloc(count, 0);
	str_len = count;
	strncpy(str, buf, str_len);
	return count;
}