summaryrefslogtreecommitdiff
path: root/recipes-kernel/linux/linux-3.12.27/linux-3.12-gpio-expose-pullups.patch
blob: 4e1d482e2dd42baad963411fba67b9bdebd0831e (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
Index: linux-3.12.13/drivers/gpio/gpiolib.c
===================================================================
--- linux-3.12.13.orig/drivers/gpio/gpiolib.c	2014-02-22 15:32:50.000000000 -0600
+++ linux-3.12.13/drivers/gpio/gpiolib.c	2014-04-18 09:25:31.977377592 -0500
@@ -87,7 +87,9 @@
 static int gpiod_request(struct gpio_desc *desc, const char *label);
 static void gpiod_free(struct gpio_desc *desc);
 static int gpiod_direction_input(struct gpio_desc *desc);
+static int gpiod_direction_input_pullup(struct gpio_desc *desc, bool pullup);
 static int gpiod_direction_output(struct gpio_desc *desc, int value);
+static int gpiod_direction_output_pullup(struct gpio_desc *desc, int value, bool pullup);
 static int gpiod_get_direction(const struct gpio_desc *desc);
 static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
 static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
@@ -1523,11 +1525,19 @@
 	if (flags & GPIOF_OPEN_SOURCE)
 		set_bit(FLAG_OPEN_SOURCE, &desc->flags);
 
-	if (flags & GPIOF_DIR_IN)
-		err = gpiod_direction_input(desc);
-	else
-		err = gpiod_direction_output(desc,
-				(flags & GPIOF_INIT_HIGH) ? 1 : 0);
+	if (flags & GPIOF_DIR_IN) {
+		if (flags & GPIOF_PULLUP)
+			err = gpiod_direction_input_pullup(desc, (flags & GPIOF_PULLUP) ? true : false);
+		else
+			err = gpiod_direction_input(desc);
+	} else {
+		if (flags & GPIOF_PULLUP)
+			err = gpiod_direction_output_pullup(desc,
+					(flags & GPIOF_INIT_HIGH) ? 1 : 0, (flags & GPIOF_PULLUP) ? true : false);
+		else
+			err = gpiod_direction_output(desc,
+					(flags & GPIOF_INIT_HIGH) ? 1 : 0);
+	}
 
 	if (err)
 		goto free_gpio;
@@ -1613,6 +1623,71 @@
 }
 EXPORT_SYMBOL_GPL(gpiochip_is_requested);
 
+static int gpiod_direction_input_pullup(struct gpio_desc *desc, bool pullup)
+{
+	unsigned long		flags;
+	struct gpio_chip	*chip;
+	int			status = -EINVAL;
+	int			offset;
+
+	if (!desc || !desc->chip) {
+		pr_warn("%s: invalid GPIO\n", __func__);
+		return -EINVAL;
+	}
+
+	chip = desc->chip;
+	if (!chip->get || !chip->direction_input_pullup) {
+		pr_warn("%s: missing get() or direction_input_pullup() operations\n",
+			__func__);
+		return -EIO;
+	}
+
+	spin_lock_irqsave(&gpio_lock, flags);
+
+	status = gpio_ensure_requested(desc);
+	if (status < 0)
+		goto fail;
+
+	/* now we know the gpio is valid and chip won't vanish */
+
+	spin_unlock_irqrestore(&gpio_lock, flags);
+
+	might_sleep_if(chip->can_sleep);
+
+	offset = gpio_chip_hwgpio(desc);
+	if (status) {
+		status = chip->request(chip, offset);
+		if (status < 0) {
+			pr_debug("GPIO-%d: chip request fail, %d\n",
+				desc_to_gpio(desc), status);
+			/* and it's not available to anyone else ...
+			 * gpio_request() is the fully clean solution.
+			 */
+			goto lose;
+		}
+	}
+
+	status = chip->direction_input_pullup(chip, offset, pullup);
+	if (status == 0)
+		clear_bit(FLAG_IS_OUT, &desc->flags);
+
+	trace_gpio_direction(desc_to_gpio(desc), 1, status);
+lose:
+	return status;
+fail:
+	spin_unlock_irqrestore(&gpio_lock, flags);
+	if (status)
+		pr_debug("%s: gpio-%d status %d\n", __func__,
+			 desc_to_gpio(desc), status);
+	return status;
+}
+
+int gpio_direction_input_pullup(unsigned gpio, bool pullup)
+{
+	return gpiod_direction_input_pullup(gpio_to_desc(gpio), pullup);
+}
+EXPORT_SYMBOL_GPL(gpio_direction_input_pullup);
+
 
 /* Drivers MUST set GPIO direction before making get/set calls.  In
  * some cases this is done in early boot, before IRQs are enabled.
@@ -1688,6 +1763,79 @@
 }
 EXPORT_SYMBOL_GPL(gpio_direction_input);
 
+static int gpiod_direction_output_pullup(struct gpio_desc *desc, int value, bool pullup)
+{
+	unsigned long		flags;
+	struct gpio_chip	*chip;
+	int			status = -EINVAL;
+	int offset;
+
+	if (!desc || !desc->chip) {
+		pr_warn("%s: invalid GPIO\n", __func__);
+		return -EINVAL;
+	}
+
+	/* Open drain pin should not be driven to 1 */
+	if (value && test_bit(FLAG_OPEN_DRAIN,  &desc->flags))
+		return gpiod_direction_input(desc);
+
+	/* Open source pin should not be driven to 0 */
+	if (!value && test_bit(FLAG_OPEN_SOURCE,  &desc->flags))
+		return gpiod_direction_input(desc);
+
+	chip = desc->chip;
+	if (!chip->set || !chip->direction_output_pullup) {
+		pr_warn("%s: missing set() or direction_output_pullup() operations\n",
+			__func__);
+		return -EIO;
+	}
+
+	spin_lock_irqsave(&gpio_lock, flags);
+
+	status = gpio_ensure_requested(desc);
+	if (status < 0)
+		goto fail;
+
+	/* now we know the gpio is valid and chip won't vanish */
+
+	spin_unlock_irqrestore(&gpio_lock, flags);
+
+	might_sleep_if(chip->can_sleep);
+
+	offset = gpio_chip_hwgpio(desc);
+	if (status) {
+		status = chip->request(chip, offset);
+		if (status < 0) {
+			pr_debug("GPIO-%d: chip request fail, %d\n",
+				desc_to_gpio(desc), status);
+			/* and it's not available to anyone else ...
+			 * gpio_request() is the fully clean solution.
+			 */
+			goto lose;
+		}
+	}
+
+	status = chip->direction_output_pullup(chip, offset, value, pullup);
+	if (status == 0)
+		set_bit(FLAG_IS_OUT, &desc->flags);
+	trace_gpio_value(desc_to_gpio(desc), 0, value);
+	trace_gpio_direction(desc_to_gpio(desc), 0, status);
+lose:
+	return status;
+fail:
+	spin_unlock_irqrestore(&gpio_lock, flags);
+	if (status)
+		pr_debug("%s: gpio-%d status %d\n", __func__,
+			 desc_to_gpio(desc), status);
+	return status;
+}
+
+int gpio_direction_output_pullup(unsigned gpio, int value, bool pullup)
+{
+	return gpiod_direction_output_pullup(gpio_to_desc(gpio), value, pullup);
+}
+EXPORT_SYMBOL_GPL(gpio_direction_output_pullup);
+
 static int gpiod_direction_output(struct gpio_desc *desc, int value)
 {
 	unsigned long		flags;
Index: linux-3.12.13/drivers/pinctrl/pinctrl-at91.c
===================================================================
--- linux-3.12.13.orig/drivers/pinctrl/pinctrl-at91.c	2014-02-22 15:32:50.000000000 -0600
+++ linux-3.12.13/drivers/pinctrl/pinctrl-at91.c	2014-04-18 09:25:31.985377595 -0500
@@ -1112,6 +1112,17 @@
 	return 0;
 }
 
+static int at91_gpio_direction_input_pullup(struct gpio_chip *chip, unsigned offset, bool pullup)
+{
+	struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
+	void __iomem *pio = at91_gpio->regbase;
+	unsigned mask = 1 << offset;
+
+	writel_relaxed(mask, pio + PIO_ODR);
+	at91_mux_set_pullup(pio, mask, pullup);
+	return 0;
+}
+
 static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
 {
 	struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
@@ -1146,6 +1157,20 @@
 	return 0;
 }
 
+static int at91_gpio_direction_output_pullup(struct gpio_chip *chip, unsigned offset,
+				int val, bool pullup)
+{
+	struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
+	void __iomem *pio = at91_gpio->regbase;
+	unsigned mask = 1 << offset;
+
+	writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
+	writel_relaxed(mask, pio + PIO_OER);
+	at91_mux_set_pullup(pio, mask, pullup);
+
+	return 0;
+}
+
 static int at91_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
 {
 	struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
@@ -1513,8 +1538,10 @@
 	.request		= at91_gpio_request,
 	.free			= at91_gpio_free,
 	.direction_input	= at91_gpio_direction_input,
+	.direction_input_pullup = at91_gpio_direction_input_pullup,
 	.get			= at91_gpio_get,
 	.direction_output	= at91_gpio_direction_output,
+	.direction_output_pullup	= at91_gpio_direction_output_pullup,
 	.set			= at91_gpio_set,
 	.to_irq			= at91_gpio_to_irq,
 	.dbg_show		= at91_gpio_dbg_show,
Index: linux-3.12.13/include/linux/gpio.h
===================================================================
--- linux-3.12.13.orig/include/linux/gpio.h	2014-02-22 15:32:50.000000000 -0600
+++ linux-3.12.13/include/linux/gpio.h	2014-04-18 09:25:31.957377594 -0500
@@ -12,7 +12,10 @@
 #define GPIOF_INIT_LOW	(0 << 1)
 #define GPIOF_INIT_HIGH	(1 << 1)
 
+#define GPIOF_PULLUP		(2 << 0)
+
 #define GPIOF_IN		(GPIOF_DIR_IN)
+#define GPIOF_IN_PULLUP			(GPIOF_DIR_IN | GPIOF_PULLUP)
 #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
 #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
 
Index: linux-3.12.13/include/asm-generic/gpio.h
===================================================================
--- linux-3.12.13.orig/include/asm-generic/gpio.h	2014-02-22 15:32:50.000000000 -0600
+++ linux-3.12.13/include/asm-generic/gpio.h	2014-04-18 09:26:42.669377408 -0500
@@ -62,9 +62,11 @@
  * @get_direction: returns direction for signal "offset", 0=out, 1=in,
  *	(same as GPIOF_DIR_XXX), or negative error
  * @direction_input: configures signal "offset" as input, or returns error
+ * @direction_input_pullup: configures signal "offset" as input with pullup enabled, or returns error
  * @get: returns value for signal "offset"; for output signals this
  *	returns either the value actually sensed, or zero
  * @direction_output: configures signal "offset" as output, or returns error
+ * @direction_output_pullup: configures signal "offset" as output with pullup enabled, or returns error
  * @set_debounce: optional hook for setting debounce time for specified gpio in
  *      interrupt triggered gpio chips
  * @set: assigns output value for signal "offset"
@@ -111,10 +113,14 @@
 						unsigned offset);
 	int			(*direction_input)(struct gpio_chip *chip,
 						unsigned offset);
+	int			(*direction_input_pullup)(struct gpio_chip *chip,
+						unsigned offset, bool pullup);
 	int			(*get)(struct gpio_chip *chip,
 						unsigned offset);
 	int			(*direction_output)(struct gpio_chip *chip,
 						unsigned offset, int value);
+	int			(*direction_output_pullup)(struct gpio_chip *chip,
+						unsigned offset, int value, bool pullup);
 	int			(*set_debounce)(struct gpio_chip *chip,
 						unsigned offset, unsigned debounce);
 
@@ -173,7 +179,9 @@
 extern void gpio_free(unsigned gpio);
 
 extern int gpio_direction_input(unsigned gpio);
+extern int gpio_direction_input_pullup(unsigned gpio, bool pullup);
 extern int gpio_direction_output(unsigned gpio, int value);
+extern int gpio_direction_output_pullup(unsigned gpio, int value, bool pullup);
 
 extern int gpio_set_debounce(unsigned gpio, unsigned debounce);