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
7 Pi3 and Compute Module 3 have a GPIO expander that the
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.
12 Pwr_led node added to device-tree for Pi3.
14 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
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
26 --- a/arch/arm/boot/dts/bcm2710-rpi-3-b.dts
27 +++ b/arch/arm/boot/dts/bcm2710-rpi-3-b.dts
29 firmware = <&firmware>;
34 + compatible = "brcm,bcm2835-expgpio";
37 + firmware = <&firmware>;
44 linux,default-trigger = "mmc0";
45 gpios = <&virtgpio 0 0>;
50 + linux,default-trigger = "input";
51 + gpios = <&expgpio 7 GPIO_ACTIVE_LOW>;
56 + hpd-gpios = <&expgpio 4 GPIO_ACTIVE_LOW>;
61 act_led_activelow = <&act_led>,"gpios:8";
62 act_led_trigger = <&act_led>,"linux,default-trigger";
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";
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
74 firmware = <&firmware>;
79 + compatible = "brcm,bcm2835-expgpio";
82 + firmware = <&firmware>;
92 - hpd-gpios = <&gpio 46 GPIO_ACTIVE_LOW>;
93 + hpd-gpios = <&expgpio 0 GPIO_ACTIVE_LOW>;
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
103 +CONFIG_GPIO_BCM_EXP=y
104 CONFIG_GPIO_BCM_VIRT=y
105 CONFIG_GPIO_ARIZONA=m
107 --- a/drivers/gpio/Kconfig
108 +++ b/drivers/gpio/Kconfig
109 @@ -128,6 +128,13 @@ config GPIO_AXP209
111 Say yes to enable GPIO support for the AXP209 PMIC
114 + bool "Broadcom Exp GPIO"
115 + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || COMPILE_TEST)
117 + Turn on GPIO support for Broadcom chips using the firmware mailbox
118 + to communicate with VideoCore on BCM283x chips.
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
134 +++ b/drivers/gpio/gpio-bcm-exp.c
137 + * Broadcom expander GPIO driver
139 + * Uses the firmware mailbox service to communicate with the
140 + * GPIO expander on the VPU.
142 + * Copyright (C) 2017 Raspberry Pi Trading Ltd.
144 + * Author: Dave Stevenson <dave.stevenson@raspberrypi.org>
145 + * Based on gpio-bcm-virt.c by Dom Cobley <popcornmix@gmail.com>
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.
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>
160 +#define MODULE_NAME "brcmexp-gpio"
163 +struct brcmexp_gpio {
164 + struct gpio_chip gc;
165 + struct device *dev;
166 + struct rpi_firmware *fw;
169 +struct gpio_set_config {
170 + u32 gpio, direction, polarity, term_en, term_pull_up, state;
173 +struct gpio_get_config {
174 + u32 gpio, direction, polarity, term_en, term_pull_up;
177 +struct gpio_get_set_state {
181 +static int brcmexp_gpio_get_polarity(struct gpio_chip *gc, unsigned int off)
183 + struct brcmexp_gpio *gpio;
184 + struct gpio_get_config get;
187 + gpio = container_of(gc, struct brcmexp_gpio, gc);
189 + get.gpio = off + gpio->gc.base; /* GPIO to update */
191 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
192 + &get, sizeof(get));
195 + "Failed to get GPIO %u config (%d)\n", off, ret);
198 + return get.polarity;
201 +static int brcmexp_gpio_dir_in(struct gpio_chip *gc, unsigned int off)
203 + struct brcmexp_gpio *gpio;
204 + struct gpio_set_config set_in;
207 + gpio = container_of(gc, struct brcmexp_gpio, gc);
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 */
217 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
218 + &set_in, sizeof(set_in));
221 + "Failed to set GPIO %u to input (%d)\n",
228 +static int brcmexp_gpio_dir_out(struct gpio_chip *gc, unsigned int off, int val)
230 + struct brcmexp_gpio *gpio;
231 + struct gpio_set_config set_out;
234 + gpio = container_of(gc, struct brcmexp_gpio, gc);
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 */
244 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
245 + &set_out, sizeof(set_out));
248 + "Failed to set GPIO %u to output (%d)\n", off, ret);
254 +static int brcmexp_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
256 + struct brcmexp_gpio *gpio;
257 + struct gpio_get_config get;
260 + gpio = container_of(gc, struct brcmexp_gpio, gc);
262 + get.gpio = off + gpio->gc.base; /* GPIO to update */
264 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
265 + &get, sizeof(get));
268 + "Failed to get GPIO %u config (%d)\n", off, ret);
271 + return get.direction ? GPIOF_DIR_OUT : GPIOF_DIR_IN;
274 +static int brcmexp_gpio_get(struct gpio_chip *gc, unsigned int off)
276 + struct brcmexp_gpio *gpio;
277 + struct gpio_get_set_state get;
280 + gpio = container_of(gc, struct brcmexp_gpio, gc);
282 + get.gpio = off + gpio->gc.base; /* GPIO to update */
283 + get.state = 0; /* storage for returned value */
285 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_STATE,
286 + &get, sizeof(get));
289 + "Failed to get GPIO %u state (%d)\n", off, ret);
292 + return !!get.state;
295 +static void brcmexp_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
297 + struct brcmexp_gpio *gpio;
298 + struct gpio_get_set_state set;
301 + gpio = container_of(gc, struct brcmexp_gpio, gc);
303 + off += gpio->gc.base;
305 + set.gpio = off + gpio->gc.base; /* GPIO to update */
306 + set.state = val; /* Output state */
308 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_STATE,
309 + &set, sizeof(set));
312 + "Failed to set GPIO %u state (%d)\n", off, ret);
315 +static int brcmexp_gpio_probe(struct platform_device *pdev)
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;
324 + fw_node = of_parse_phandle(np, "firmware", 0);
326 + dev_err(dev, "Missing firmware node\n");
330 + fw = rpi_firmware_get(fw_node);
332 + return -EPROBE_DEFER;
334 + ucb = devm_kzalloc(dev, sizeof(*ucb), GFP_KERNEL);
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;
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;
353 + err = gpiochip_add(&ucb->gc);
357 + platform_set_drvdata(pdev, ucb);
362 +static int brcmexp_gpio_remove(struct platform_device *pdev)
364 + struct brcmexp_gpio *ucb = platform_get_drvdata(pdev);
366 + gpiochip_remove(&ucb->gc);
371 +static const struct of_device_id __maybe_unused brcmexp_gpio_ids[] = {
372 + { .compatible = "brcm,bcm2835-expgpio" },
375 +MODULE_DEVICE_TABLE(of, brcmexp_gpio_ids);
377 +static struct platform_driver brcmexp_gpio_driver = {
379 + .name = MODULE_NAME,
380 + .owner = THIS_MODULE,
381 + .of_match_table = of_match_ptr(brcmexp_gpio_ids),
383 + .probe = brcmexp_gpio_probe,
384 + .remove = brcmexp_gpio_remove,
386 +module_platform_driver(brcmexp_gpio_driver);
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,
405 RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE = 0x00040001,