3d09d1dc3f5619902aa9c9e0a0f7213c0d36d096
[openwrt/staging/jogo.git] /
1 From 2f8f62fbd42a55825da437faba4b6164f2e0bee9 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.org>
3 Date: Mon, 20 Feb 2017 17:01:21 +0000
4 Subject: [PATCH] bcm2835-gpio-exp: Driver for GPIO expander via mailbox
5 service
6
7 Pi3 and Compute Module 3 have a GPIO expander that the
8 VPU communicates with.
9 There is a mailbox service that now allows control of this
10 expander, so add a kernel driver that can make use of it.
11
12 Pwr_led node added to device-tree for Pi3.
13
14 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
15 ---
16 arch/arm/boot/dts/bcm2710-rpi-3-b.dts | 22 +++
17 arch/arm/boot/dts/bcm2710-rpi-cm3.dts | 10 +-
18 arch/arm/configs/bcm2709_defconfig | 1 +
19 drivers/gpio/Kconfig | 7 +
20 drivers/gpio/Makefile | 1 +
21 drivers/gpio/gpio-bcm-exp.c | 256 +++++++++++++++++++++++++++++
22 include/soc/bcm2835/raspberrypi-firmware.h | 4 +
23 7 files changed, 300 insertions(+), 1 deletion(-)
24 create mode 100644 drivers/gpio/gpio-bcm-exp.c
25
26 --- a/arch/arm/boot/dts/bcm2710-rpi-3-b.dts
27 +++ b/arch/arm/boot/dts/bcm2710-rpi-3-b.dts
28 @@ -96,6 +96,14 @@
29 firmware = <&firmware>;
30 status = "okay";
31 };
32 +
33 + expgpio: expgpio {
34 + compatible = "brcm,bcm2835-expgpio";
35 + gpio-controller;
36 + #gpio-cells = <2>;
37 + firmware = <&firmware>;
38 + status = "okay";
39 + };
40 };
41
42 &fb {
43 @@ -163,6 +171,16 @@
44 linux,default-trigger = "mmc0";
45 gpios = <&virtgpio 0 0>;
46 };
47 +
48 + pwr_led: pwr {
49 + label = "led1";
50 + linux,default-trigger = "input";
51 + gpios = <&expgpio 7 GPIO_ACTIVE_LOW>;
52 + };
53 +};
54 +
55 +&hdmi {
56 + hpd-gpios = <&expgpio 4 GPIO_ACTIVE_LOW>;
57 };
58
59 &audio {
60 @@ -193,6 +211,10 @@
61 act_led_activelow = <&act_led>,"gpios:8";
62 act_led_trigger = <&act_led>,"linux,default-trigger";
63
64 + pwr_led_gpio = <&pwr_led>,"gpios:4";
65 + pwr_led_activelow = <&pwr_led>,"gpios:8";
66 + pwr_led_trigger = <&pwr_led>,"linux,default-trigger";
67 +
68 audio = <&audio>,"status";
69 watchdog = <&watchdog>,"status";
70 random = <&random>,"status";
71 --- a/arch/arm/boot/dts/bcm2710-rpi-cm3.dts
72 +++ b/arch/arm/boot/dts/bcm2710-rpi-cm3.dts
73 @@ -65,6 +65,14 @@
74 firmware = <&firmware>;
75 status = "okay";
76 };
77 +
78 + expgpio: expgpio {
79 + compatible = "brcm,bcm2835-expgpio";
80 + gpio-controller;
81 + #gpio-cells = <2>;
82 + firmware = <&firmware>;
83 + status = "okay";
84 + };
85 };
86
87 &fb {
88 @@ -123,7 +131,7 @@
89 };
90
91 &hdmi {
92 - hpd-gpios = <&gpio 46 GPIO_ACTIVE_LOW>;
93 + hpd-gpios = <&expgpio 0 GPIO_ACTIVE_LOW>;
94 };
95
96 &audio {
97 --- a/arch/arm/configs/bcm2709_defconfig
98 +++ b/arch/arm/configs/bcm2709_defconfig
99 @@ -625,6 +625,7 @@ CONFIG_PPS=m
100 CONFIG_PPS_CLIENT_LDISC=m
101 CONFIG_PPS_CLIENT_GPIO=m
102 CONFIG_GPIO_SYSFS=y
103 +CONFIG_GPIO_BCM_EXP=y
104 CONFIG_GPIO_BCM_VIRT=y
105 CONFIG_GPIO_ARIZONA=m
106 CONFIG_GPIO_STMPE=y
107 --- a/drivers/gpio/Kconfig
108 +++ b/drivers/gpio/Kconfig
109 @@ -128,6 +128,13 @@ config GPIO_AXP209
110 help
111 Say yes to enable GPIO support for the AXP209 PMIC
112
113 +config GPIO_BCM_EXP
114 + bool "Broadcom Exp GPIO"
115 + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || COMPILE_TEST)
116 + help
117 + Turn on GPIO support for Broadcom chips using the firmware mailbox
118 + to communicate with VideoCore on BCM283x chips.
119 +
120 config GPIO_BCM_KONA
121 bool "Broadcom Kona GPIO"
122 depends on OF_GPIO && (ARCH_BCM_MOBILE || COMPILE_TEST)
123 --- a/drivers/gpio/Makefile
124 +++ b/drivers/gpio/Makefile
125 @@ -30,6 +30,7 @@ obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizo
126 obj-$(CONFIG_GPIO_ATH79) += gpio-ath79.o
127 obj-$(CONFIG_GPIO_ASPEED) += gpio-aspeed.o
128 obj-$(CONFIG_GPIO_AXP209) += gpio-axp209.o
129 +obj-$(CONFIG_GPIO_BCM_EXP) += gpio-bcm-exp.o
130 obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
131 obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o
132 obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o
133 --- /dev/null
134 +++ b/drivers/gpio/gpio-bcm-exp.c
135 @@ -0,0 +1,256 @@
136 +/*
137 + * Broadcom expander GPIO driver
138 + *
139 + * Uses the firmware mailbox service to communicate with the
140 + * GPIO expander on the VPU.
141 + *
142 + * Copyright (C) 2017 Raspberry Pi Trading Ltd.
143 + *
144 + * Author: Dave Stevenson <dave.stevenson@raspberrypi.org>
145 + * Based on gpio-bcm-virt.c by Dom Cobley <popcornmix@gmail.com>
146 + *
147 + * This program is free software; you can redistribute it and/or modify
148 + * it under the terms of the GNU General Public License as published by
149 + * the Free Software Foundation; either version 2 of the License, or
150 + * (at your option) any later version.
151 + */
152 +
153 +#include <linux/err.h>
154 +#include <linux/gpio.h>
155 +#include <linux/module.h>
156 +#include <linux/platform_device.h>
157 +#include <linux/dma-mapping.h>
158 +#include <soc/bcm2835/raspberrypi-firmware.h>
159 +
160 +#define MODULE_NAME "brcmexp-gpio"
161 +#define NUM_GPIO 8
162 +
163 +struct brcmexp_gpio {
164 + struct gpio_chip gc;
165 + struct device *dev;
166 + struct rpi_firmware *fw;
167 +};
168 +
169 +struct gpio_set_config {
170 + u32 gpio, direction, polarity, term_en, term_pull_up, state;
171 +};
172 +
173 +struct gpio_get_config {
174 + u32 gpio, direction, polarity, term_en, term_pull_up;
175 +};
176 +
177 +struct gpio_get_set_state {
178 + u32 gpio, state;
179 +};
180 +
181 +static int brcmexp_gpio_get_polarity(struct gpio_chip *gc, unsigned int off)
182 +{
183 + struct brcmexp_gpio *gpio;
184 + struct gpio_get_config get;
185 + int ret;
186 +
187 + gpio = container_of(gc, struct brcmexp_gpio, gc);
188 +
189 + get.gpio = off + gpio->gc.base; /* GPIO to update */
190 +
191 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
192 + &get, sizeof(get));
193 + if (ret) {
194 + dev_err(gpio->dev,
195 + "Failed to get GPIO %u config (%d)\n", off, ret);
196 + return ret;
197 + }
198 + return get.polarity;
199 +}
200 +
201 +static int brcmexp_gpio_dir_in(struct gpio_chip *gc, unsigned int off)
202 +{
203 + struct brcmexp_gpio *gpio;
204 + struct gpio_set_config set_in;
205 + int ret;
206 +
207 + gpio = container_of(gc, struct brcmexp_gpio, gc);
208 +
209 + set_in.gpio = off + gpio->gc.base; /* GPIO to update */
210 + set_in.direction = 0; /* Input */
211 + set_in.polarity = brcmexp_gpio_get_polarity(gc, off);
212 + /* Retain existing setting */
213 + set_in.term_en = 0; /* termination disabled */
214 + set_in.term_pull_up = 0; /* n/a as termination disabled */
215 + set_in.state = 0; /* n/a as configured as an input */
216 +
217 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
218 + &set_in, sizeof(set_in));
219 + if (ret) {
220 + dev_err(gpio->dev,
221 + "Failed to set GPIO %u to input (%d)\n",
222 + off, ret);
223 + return ret;
224 + }
225 + return 0;
226 +}
227 +
228 +static int brcmexp_gpio_dir_out(struct gpio_chip *gc, unsigned int off, int val)
229 +{
230 + struct brcmexp_gpio *gpio;
231 + struct gpio_set_config set_out;
232 + int ret;
233 +
234 + gpio = container_of(gc, struct brcmexp_gpio, gc);
235 +
236 + set_out.gpio = off + gpio->gc.base; /* GPIO to update */
237 + set_out.direction = 1; /* Output */
238 + set_out.polarity = brcmexp_gpio_get_polarity(gc, off);
239 + /* Retain existing setting */
240 + set_out.term_en = 0; /* n/a as an output */
241 + set_out.term_pull_up = 0; /* n/a as termination disabled */
242 + set_out.state = val; /* Output state */
243 +
244 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
245 + &set_out, sizeof(set_out));
246 + if (ret) {
247 + dev_err(gpio->dev,
248 + "Failed to set GPIO %u to output (%d)\n", off, ret);
249 + return ret;
250 + }
251 + return 0;
252 +}
253 +
254 +static int brcmexp_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
255 +{
256 + struct brcmexp_gpio *gpio;
257 + struct gpio_get_config get;
258 + int ret;
259 +
260 + gpio = container_of(gc, struct brcmexp_gpio, gc);
261 +
262 + get.gpio = off + gpio->gc.base; /* GPIO to update */
263 +
264 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
265 + &get, sizeof(get));
266 + if (ret) {
267 + dev_err(gpio->dev,
268 + "Failed to get GPIO %u config (%d)\n", off, ret);
269 + return ret;
270 + }
271 + return get.direction ? GPIOF_DIR_OUT : GPIOF_DIR_IN;
272 +}
273 +
274 +static int brcmexp_gpio_get(struct gpio_chip *gc, unsigned int off)
275 +{
276 + struct brcmexp_gpio *gpio;
277 + struct gpio_get_set_state get;
278 + int ret;
279 +
280 + gpio = container_of(gc, struct brcmexp_gpio, gc);
281 +
282 + get.gpio = off + gpio->gc.base; /* GPIO to update */
283 + get.state = 0; /* storage for returned value */
284 +
285 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_STATE,
286 + &get, sizeof(get));
287 + if (ret) {
288 + dev_err(gpio->dev,
289 + "Failed to get GPIO %u state (%d)\n", off, ret);
290 + return ret;
291 + }
292 + return !!get.state;
293 +}
294 +
295 +static void brcmexp_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
296 +{
297 + struct brcmexp_gpio *gpio;
298 + struct gpio_get_set_state set;
299 + int ret;
300 +
301 + gpio = container_of(gc, struct brcmexp_gpio, gc);
302 +
303 + off += gpio->gc.base;
304 +
305 + set.gpio = off + gpio->gc.base; /* GPIO to update */
306 + set.state = val; /* Output state */
307 +
308 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_STATE,
309 + &set, sizeof(set));
310 + if (ret)
311 + dev_err(gpio->dev,
312 + "Failed to set GPIO %u state (%d)\n", off, ret);
313 +}
314 +
315 +static int brcmexp_gpio_probe(struct platform_device *pdev)
316 +{
317 + int err = 0;
318 + struct device *dev = &pdev->dev;
319 + struct device_node *np = dev->of_node;
320 + struct device_node *fw_node;
321 + struct rpi_firmware *fw;
322 + struct brcmexp_gpio *ucb;
323 +
324 + fw_node = of_parse_phandle(np, "firmware", 0);
325 + if (!fw_node) {
326 + dev_err(dev, "Missing firmware node\n");
327 + return -ENOENT;
328 + }
329 +
330 + fw = rpi_firmware_get(fw_node);
331 + if (!fw)
332 + return -EPROBE_DEFER;
333 +
334 + ucb = devm_kzalloc(dev, sizeof(*ucb), GFP_KERNEL);
335 + if (!ucb)
336 + return -EINVAL;
337 +
338 + ucb->fw = fw;
339 + ucb->dev = dev;
340 + ucb->gc.label = MODULE_NAME;
341 + ucb->gc.owner = THIS_MODULE;
342 + ucb->gc.of_node = np;
343 + ucb->gc.base = 128;
344 + ucb->gc.ngpio = NUM_GPIO;
345 +
346 + ucb->gc.direction_input = brcmexp_gpio_dir_in;
347 + ucb->gc.direction_output = brcmexp_gpio_dir_out;
348 + ucb->gc.get_direction = brcmexp_gpio_get_direction;
349 + ucb->gc.get = brcmexp_gpio_get;
350 + ucb->gc.set = brcmexp_gpio_set;
351 + ucb->gc.can_sleep = true;
352 +
353 + err = gpiochip_add(&ucb->gc);
354 + if (err)
355 + return err;
356 +
357 + platform_set_drvdata(pdev, ucb);
358 +
359 + return 0;
360 +}
361 +
362 +static int brcmexp_gpio_remove(struct platform_device *pdev)
363 +{
364 + struct brcmexp_gpio *ucb = platform_get_drvdata(pdev);
365 +
366 + gpiochip_remove(&ucb->gc);
367 +
368 + return 0;
369 +}
370 +
371 +static const struct of_device_id __maybe_unused brcmexp_gpio_ids[] = {
372 + { .compatible = "brcm,bcm2835-expgpio" },
373 + { }
374 +};
375 +MODULE_DEVICE_TABLE(of, brcmexp_gpio_ids);
376 +
377 +static struct platform_driver brcmexp_gpio_driver = {
378 + .driver = {
379 + .name = MODULE_NAME,
380 + .owner = THIS_MODULE,
381 + .of_match_table = of_match_ptr(brcmexp_gpio_ids),
382 + },
383 + .probe = brcmexp_gpio_probe,
384 + .remove = brcmexp_gpio_remove,
385 +};
386 +module_platform_driver(brcmexp_gpio_driver);
387 +
388 +MODULE_LICENSE("GPL");
389 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.org>");
390 +MODULE_DESCRIPTION("brcm-exp GPIO driver");
391 +MODULE_ALIAS("platform:brcmexp-gpio");
392 --- a/include/soc/bcm2835/raspberrypi-firmware.h
393 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
394 @@ -83,7 +83,11 @@ enum rpi_firmware_property_tag {
395 RPI_FIRMWARE_SET_TURBO = 0x00038009,
396 RPI_FIRMWARE_SET_CUSTOMER_OTP = 0x00038021,
397 RPI_FIRMWARE_SET_DOMAIN_STATE = 0x00038030,
398 + RPI_FIRMWARE_GET_GPIO_STATE = 0x00030041,
399 + RPI_FIRMWARE_SET_GPIO_STATE = 0x00038041,
400 RPI_FIRMWARE_SET_SDHOST_CLOCK = 0x00038042,
401 + RPI_FIRMWARE_GET_GPIO_CONFIG = 0x00030043,
402 + RPI_FIRMWARE_SET_GPIO_CONFIG = 0x00038043,
403
404 /* Dispmanx TAGS */
405 RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE = 0x00040001,