summaryrefslogtreecommitdiff
path: root/target/linux/brcm2708/patches-3.18/0068-lirc-rpi-Add-device-tree-support-and-a-suitable-over.patch
blob: 5f6c784cb60315313b273d22ecbb14eb0b2e7c6c (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
From f3c1830096661e270f11f2a33ffb7274f50c90a6 Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.org>
Date: Thu, 18 Dec 2014 16:48:32 +0000
Subject: [PATCH 068/114] lirc-rpi: Add device tree support, and a suitable
 overlay

The overlay supports DT parameters that match the old module
parameters, except that gpio_in_pull should be set using the
strings "up", "down" or "off".

lirc-rpi: Also support pinctrl-bcm2835 in non-DT mode
---
 arch/arm/boot/dts/lirc-rpi-overlay.dts |  57 ++++++++++++
 drivers/staging/media/lirc/lirc_rpi.c  | 154 +++++++++++++++++++++++++++------
 2 files changed, 183 insertions(+), 28 deletions(-)
 create mode 100644 arch/arm/boot/dts/lirc-rpi-overlay.dts

diff --git a/arch/arm/boot/dts/lirc-rpi-overlay.dts b/arch/arm/boot/dts/lirc-rpi-overlay.dts
new file mode 100644
index 0000000..7d5d82b
--- /dev/null
+++ b/arch/arm/boot/dts/lirc-rpi-overlay.dts
@@ -0,0 +1,57 @@
+// Definitions for lirc-rpi module
+/dts-v1/;
+/plugin/;
+
+/ {
+	compatible = "brcm,bcm2708";
+
+	fragment@0 {
+		target-path = "/";
+		__overlay__ {
+			lirc_rpi: lirc_rpi {
+				compatible = "rpi,lirc-rpi";
+				pinctrl-names = "default";
+				pinctrl-0 = <&lirc_pins>;
+				status = "okay";
+
+				// Override autodetection of IR receiver circuit
+				// (0 = active high, 1 = active low, -1 = no override )
+				rpi,sense = <0xffffffff>;
+
+				// Software carrier
+				// (0 = off, 1 = on)
+				rpi,softcarrier = <1>;
+
+				// Invert output
+				// (0 = off, 1 = on)
+				rpi,invert = <0>;
+
+				// Enable debugging messages
+				// (0 = off, 1 = on)
+				rpi,debug = <0>;
+			};
+		};
+	};
+
+	fragment@1 {
+		target = <&gpio>;
+		__overlay__ {
+			lirc_pins: lirc_pins {
+				brcm,pins = <17 18>;
+				brcm,function = <1 0>; // out in
+				brcm,pull = <0 1>; // off down
+			};
+		};
+	};
+
+	__overrides__ {
+		gpio_out_pin =  <&lirc_pins>,"brcm,pins:0";
+		gpio_in_pin =   <&lirc_pins>,"brcm,pins:4";
+		gpio_in_pull =  <&lirc_pins>,"brcm,pull:4";
+
+		sense =         <&lirc_rpi>,"rpi,sense:0";
+		softcarrier =   <&lirc_rpi>,"rpi,softcarrier:0";
+		invert =        <&lirc_rpi>,"rpi,invert:0";
+		debug =         <&lirc_rpi>,"rpi,debug:0";
+	};
+};
diff --git a/drivers/staging/media/lirc/lirc_rpi.c b/drivers/staging/media/lirc/lirc_rpi.c
index c688364..cd66ca2 100644
--- a/drivers/staging/media/lirc/lirc_rpi.c
+++ b/drivers/staging/media/lirc/lirc_rpi.c
@@ -40,6 +40,7 @@
 #include <media/lirc_dev.h>
 #include <mach/gpio.h>
 #include <linux/gpio.h>
+#include <linux/of_platform.h>
 
 #include <linux/platform_data/bcm2708.h>
 
@@ -295,32 +296,117 @@ static int is_right_chip(struct gpio_chip *chip, void *data)
 	return 0;
 }
 
+static inline int read_bool_property(const struct device_node *np,
+				     const char *propname,
+				     bool *out_value)
+{
+	u32 value = 0;
+	int err = of_property_read_u32(np, propname, &value);
+	if (err == 0)
+		*out_value = (value != 0);
+	return err;
+}
+
+static void read_pin_settings(struct device_node *node)
+{
+	u32 pin;
+	int index;
+
+	for (index = 0;
+	     of_property_read_u32_index(
+		     node,
+		     "brcm,pins",
+		     index,
+		     &pin) == 0;
+	     index++) {
+		u32 function;
+		int err;
+		err = of_property_read_u32_index(
+			node,
+			"brcm,function",
+			index,
+			&function);
+		if (err == 0) {
+			if (function == 1) /* Output */
+				gpio_out_pin = pin;
+			else if (function == 0) /* Input */
+				gpio_in_pin = pin;
+		}
+	}
+}
+
 static int init_port(void)
 {
 	int i, nlow, nhigh, ret;
+	struct device_node *node;
+
+	node = lirc_rpi_dev->dev.of_node;
 
 	gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
 
-	if (!gpiochip)
+	/*
+	 * Because of the lack of a setpull function, only support
+	 * pinctrl-bcm2835 if using device tree.
+	*/
+	if (!gpiochip && node)
+		gpiochip = gpiochip_find("pinctrl-bcm2835", is_right_chip);
+
+	if (!gpiochip) {
+		pr_err(LIRC_DRIVER_NAME ": gpio chip not found!\n");
 		return -ENODEV;
+	}
+
+	if (node) {
+		struct device_node *pins_node;
+
+		pins_node = of_parse_phandle(node, "pinctrl-0", 0);
+		if (!pins_node) {
+			printk(KERN_ERR LIRC_DRIVER_NAME
+			       ": pinctrl settings not found!\n");
+			ret = -EINVAL;
+			goto exit_init_port;
+		}
+
+		read_pin_settings(pins_node);
+
+		of_property_read_u32(node, "rpi,sense", &sense);
+
+		read_bool_property(node, "rpi,softcarrier", &softcarrier);
+
+		read_bool_property(node, "rpi,invert", &invert);
+
+		read_bool_property(node, "rpi,debug", &debug);
 
-	if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
-		printk(KERN_ALERT LIRC_DRIVER_NAME
-		       ": cant claim gpio pin %d\n", gpio_out_pin);
-		ret = -ENODEV;
-		goto exit_init_port;
 	}
+	else
+	{
+		if (gpio_in_pin >= BCM2708_NR_GPIOS ||
+		    gpio_out_pin >= BCM2708_NR_GPIOS) {
+			ret = -EINVAL;
+			printk(KERN_ERR LIRC_DRIVER_NAME
+			       ": invalid GPIO pin(s) specified!\n");
+			goto exit_init_port;
+		}
 
-	if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
-		printk(KERN_ALERT LIRC_DRIVER_NAME
-		       ": cant claim gpio pin %d\n", gpio_in_pin);
-		ret = -ENODEV;
-		goto exit_gpio_free_out_pin;
+		if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
+			printk(KERN_ALERT LIRC_DRIVER_NAME
+			       ": cant claim gpio pin %d\n", gpio_out_pin);
+			ret = -ENODEV;
+			goto exit_init_port;
+		}
+
+		if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
+			printk(KERN_ALERT LIRC_DRIVER_NAME
+			       ": cant claim gpio pin %d\n", gpio_in_pin);
+			ret = -ENODEV;
+			goto exit_gpio_free_out_pin;
+		}
+
+		bcm2708_gpio_setpull(gpiochip, gpio_in_pin, gpio_in_pull);
+		gpiochip->direction_input(gpiochip, gpio_in_pin);
+		gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
 	}
 
-	bcm2708_gpio_setpull(gpiochip, gpio_in_pin, gpio_in_pull);
-	gpiochip->direction_input(gpiochip, gpio_in_pin);
-	gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
 	gpiochip->set(gpiochip, gpio_out_pin, invert);
 
 	irq_num = gpiochip->to_irq(gpiochip, gpio_in_pin);
@@ -514,15 +600,23 @@ static struct lirc_driver driver = {
 	.owner		= THIS_MODULE,
 };
 
+static const struct of_device_id lirc_rpi_of_match[] = {
+	{ .compatible = "rpi,lirc-rpi", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, lirc_rpi_of_match);
+
 static struct platform_driver lirc_rpi_driver = {
 	.driver = {
 		.name   = LIRC_DRIVER_NAME,
 		.owner  = THIS_MODULE,
+		.of_match_table = of_match_ptr(lirc_rpi_of_match),
 	},
 };
 
 static int __init lirc_rpi_init(void)
 {
+	struct device_node *node;
 	int result;
 
 	/* Init read buffer. */
@@ -537,15 +631,26 @@ static int __init lirc_rpi_init(void)
 		goto exit_buffer_free;
 	}
 
-	lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
-	if (!lirc_rpi_dev) {
-		result = -ENOMEM;
-		goto exit_driver_unregister;
+	node = of_find_compatible_node(NULL, NULL,
+				       lirc_rpi_of_match[0].compatible);
+
+	if (node) {
+		/* DT-enabled */
+		lirc_rpi_dev = of_find_device_by_node(node);
+		WARN_ON(lirc_rpi_dev->dev.of_node != node);
+		of_node_put(node);
 	}
+	else {
+		lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
+		if (!lirc_rpi_dev) {
+			result = -ENOMEM;
+			goto exit_driver_unregister;
+		}
 
-	result = platform_device_add(lirc_rpi_dev);
-	if (result)
-		goto exit_device_put;
+		result = platform_device_add(lirc_rpi_dev);
+		if (result)
+			goto exit_device_put;
+	}
 
 	return 0;
 
@@ -577,13 +682,6 @@ static int __init lirc_rpi_init_module(void)
 	if (result)
 		return result;
 
-	if (gpio_in_pin >= BCM2708_NR_GPIOS || gpio_out_pin >= BCM2708_NR_GPIOS) {
-		result = -EINVAL;
-		printk(KERN_ERR LIRC_DRIVER_NAME
-		       ": invalid GPIO pin(s) specified!\n");
-		goto exit_rpi;
-	}
-
 	result = init_port();
 	if (result < 0)
 		goto exit_rpi;
-- 
1.8.3.2