1a58fd261fc32afe13dd51daff6b837603136dc2
[openwrt/staging/dangole.git] /
1 From 74e1aacb52fcaf5d07b5d765dca1287e4234f34d 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 091/454] bcm2835-gpio-exp: Driver for GPIO expander via
5 mailbox 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 drivers/gpio/Kconfig | 7 +
17 drivers/gpio/Makefile | 1 +
18 drivers/gpio/gpio-bcm-exp.c | 254 ++++++++++++++++++++++++++++++++++++
19 3 files changed, 262 insertions(+)
20 create mode 100644 drivers/gpio/gpio-bcm-exp.c
21
22 --- a/drivers/gpio/Kconfig
23 +++ b/drivers/gpio/Kconfig
24 @@ -128,6 +128,13 @@ config GPIO_AXP209
25 help
26 Say yes to enable GPIO support for the AXP209 PMIC
27
28 +config GPIO_BCM_EXP
29 + bool "Broadcom Exp GPIO"
30 + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || COMPILE_TEST)
31 + help
32 + Turn on GPIO support for Broadcom chips using the firmware mailbox
33 + to communicate with VideoCore on BCM283x chips.
34 +
35 config GPIO_BCM_KONA
36 bool "Broadcom Kona GPIO"
37 depends on OF_GPIO && (ARCH_BCM_MOBILE || COMPILE_TEST)
38 --- a/drivers/gpio/Makefile
39 +++ b/drivers/gpio/Makefile
40 @@ -33,6 +33,7 @@ obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizo
41 obj-$(CONFIG_GPIO_ATH79) += gpio-ath79.o
42 obj-$(CONFIG_GPIO_ASPEED) += gpio-aspeed.o
43 obj-$(CONFIG_GPIO_AXP209) += gpio-axp209.o
44 +obj-$(CONFIG_GPIO_BCM_EXP) += gpio-bcm-exp.o
45 obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
46 obj-$(CONFIG_GPIO_BD9571MWV) += gpio-bd9571mwv.o
47 obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o
48 --- /dev/null
49 +++ b/drivers/gpio/gpio-bcm-exp.c
50 @@ -0,0 +1,254 @@
51 +/*
52 + * Broadcom expander GPIO driver
53 + *
54 + * Uses the firmware mailbox service to communicate with the
55 + * GPIO expander on the VPU.
56 + *
57 + * Copyright (C) 2017 Raspberry Pi Trading Ltd.
58 + *
59 + * Author: Dave Stevenson <dave.stevenson@raspberrypi.org>
60 + * Based on gpio-bcm-virt.c by Dom Cobley <popcornmix@gmail.com>
61 + *
62 + * This program is free software; you can redistribute it and/or modify
63 + * it under the terms of the GNU General Public License as published by
64 + * the Free Software Foundation; either version 2 of the License, or
65 + * (at your option) any later version.
66 + */
67 +
68 +#include <linux/err.h>
69 +#include <linux/gpio.h>
70 +#include <linux/module.h>
71 +#include <linux/platform_device.h>
72 +#include <linux/dma-mapping.h>
73 +#include <soc/bcm2835/raspberrypi-firmware.h>
74 +
75 +#define MODULE_NAME "brcmexp-gpio"
76 +#define NUM_GPIO 8
77 +
78 +struct brcmexp_gpio {
79 + struct gpio_chip gc;
80 + struct device *dev;
81 + struct rpi_firmware *fw;
82 +};
83 +
84 +struct gpio_set_config {
85 + u32 gpio, direction, polarity, term_en, term_pull_up, state;
86 +};
87 +
88 +struct gpio_get_config {
89 + u32 gpio, direction, polarity, term_en, term_pull_up;
90 +};
91 +
92 +struct gpio_get_set_state {
93 + u32 gpio, state;
94 +};
95 +
96 +static int brcmexp_gpio_get_polarity(struct gpio_chip *gc, unsigned int off)
97 +{
98 + struct brcmexp_gpio *gpio;
99 + struct gpio_get_config get;
100 + int ret;
101 +
102 + gpio = container_of(gc, struct brcmexp_gpio, gc);
103 +
104 + get.gpio = off + gpio->gc.base; /* GPIO to update */
105 +
106 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
107 + &get, sizeof(get));
108 + if (ret) {
109 + dev_err(gpio->dev,
110 + "Failed to get GPIO %u config (%d)\n", off, ret);
111 + return ret;
112 + }
113 + return get.polarity;
114 +}
115 +
116 +static int brcmexp_gpio_dir_in(struct gpio_chip *gc, unsigned int off)
117 +{
118 + struct brcmexp_gpio *gpio;
119 + struct gpio_set_config set_in;
120 + int ret;
121 +
122 + gpio = container_of(gc, struct brcmexp_gpio, gc);
123 +
124 + set_in.gpio = off + gpio->gc.base; /* GPIO to update */
125 + set_in.direction = 0; /* Input */
126 + set_in.polarity = brcmexp_gpio_get_polarity(gc, off);
127 + /* Retain existing setting */
128 + set_in.term_en = 0; /* termination disabled */
129 + set_in.term_pull_up = 0; /* n/a as termination disabled */
130 + set_in.state = 0; /* n/a as configured as an input */
131 +
132 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
133 + &set_in, sizeof(set_in));
134 + if (ret) {
135 + dev_err(gpio->dev,
136 + "Failed to set GPIO %u to input (%d)\n",
137 + off, ret);
138 + return ret;
139 + }
140 + return 0;
141 +}
142 +
143 +static int brcmexp_gpio_dir_out(struct gpio_chip *gc, unsigned int off, int val)
144 +{
145 + struct brcmexp_gpio *gpio;
146 + struct gpio_set_config set_out;
147 + int ret;
148 +
149 + gpio = container_of(gc, struct brcmexp_gpio, gc);
150 +
151 + set_out.gpio = off + gpio->gc.base; /* GPIO to update */
152 + set_out.direction = 1; /* Output */
153 + set_out.polarity = brcmexp_gpio_get_polarity(gc, off);
154 + /* Retain existing setting */
155 + set_out.term_en = 0; /* n/a as an output */
156 + set_out.term_pull_up = 0; /* n/a as termination disabled */
157 + set_out.state = val; /* Output state */
158 +
159 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
160 + &set_out, sizeof(set_out));
161 + if (ret) {
162 + dev_err(gpio->dev,
163 + "Failed to set GPIO %u to output (%d)\n", off, ret);
164 + return ret;
165 + }
166 + return 0;
167 +}
168 +
169 +static int brcmexp_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
170 +{
171 + struct brcmexp_gpio *gpio;
172 + struct gpio_get_config get;
173 + int ret;
174 +
175 + gpio = container_of(gc, struct brcmexp_gpio, gc);
176 +
177 + get.gpio = off + gpio->gc.base; /* GPIO to update */
178 +
179 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
180 + &get, sizeof(get));
181 + if (ret) {
182 + dev_err(gpio->dev,
183 + "Failed to get GPIO %u config (%d)\n", off, ret);
184 + return ret;
185 + }
186 + return get.direction ? GPIOF_DIR_OUT : GPIOF_DIR_IN;
187 +}
188 +
189 +static int brcmexp_gpio_get(struct gpio_chip *gc, unsigned int off)
190 +{
191 + struct brcmexp_gpio *gpio;
192 + struct gpio_get_set_state get;
193 + int ret;
194 +
195 + gpio = container_of(gc, struct brcmexp_gpio, gc);
196 +
197 + get.gpio = off + gpio->gc.base; /* GPIO to update */
198 + get.state = 0; /* storage for returned value */
199 +
200 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_STATE,
201 + &get, sizeof(get));
202 + if (ret) {
203 + dev_err(gpio->dev,
204 + "Failed to get GPIO %u state (%d)\n", off, ret);
205 + return ret;
206 + }
207 + return !!get.state;
208 +}
209 +
210 +static void brcmexp_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
211 +{
212 + struct brcmexp_gpio *gpio;
213 + struct gpio_get_set_state set;
214 + int ret;
215 +
216 + gpio = container_of(gc, struct brcmexp_gpio, gc);
217 +
218 + set.gpio = off + gpio->gc.base; /* GPIO to update */
219 + set.state = val; /* Output state */
220 +
221 + ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_STATE,
222 + &set, sizeof(set));
223 + if (ret)
224 + dev_err(gpio->dev,
225 + "Failed to set GPIO %u state (%d)\n", off, ret);
226 +}
227 +
228 +static int brcmexp_gpio_probe(struct platform_device *pdev)
229 +{
230 + int err = 0;
231 + struct device *dev = &pdev->dev;
232 + struct device_node *np = dev->of_node;
233 + struct device_node *fw_node;
234 + struct rpi_firmware *fw;
235 + struct brcmexp_gpio *ucb;
236 +
237 + fw_node = of_parse_phandle(np, "firmware", 0);
238 + if (!fw_node) {
239 + dev_err(dev, "Missing firmware node\n");
240 + return -ENOENT;
241 + }
242 +
243 + fw = rpi_firmware_get(fw_node);
244 + if (!fw)
245 + return -EPROBE_DEFER;
246 +
247 + ucb = devm_kzalloc(dev, sizeof(*ucb), GFP_KERNEL);
248 + if (!ucb)
249 + return -EINVAL;
250 +
251 + ucb->fw = fw;
252 + ucb->dev = dev;
253 + ucb->gc.label = MODULE_NAME;
254 + ucb->gc.owner = THIS_MODULE;
255 + ucb->gc.of_node = np;
256 + ucb->gc.base = 128;
257 + ucb->gc.ngpio = NUM_GPIO;
258 +
259 + ucb->gc.direction_input = brcmexp_gpio_dir_in;
260 + ucb->gc.direction_output = brcmexp_gpio_dir_out;
261 + ucb->gc.get_direction = brcmexp_gpio_get_direction;
262 + ucb->gc.get = brcmexp_gpio_get;
263 + ucb->gc.set = brcmexp_gpio_set;
264 + ucb->gc.can_sleep = true;
265 +
266 + err = gpiochip_add(&ucb->gc);
267 + if (err)
268 + return err;
269 +
270 + platform_set_drvdata(pdev, ucb);
271 +
272 + return 0;
273 +}
274 +
275 +static int brcmexp_gpio_remove(struct platform_device *pdev)
276 +{
277 + struct brcmexp_gpio *ucb = platform_get_drvdata(pdev);
278 +
279 + gpiochip_remove(&ucb->gc);
280 +
281 + return 0;
282 +}
283 +
284 +static const struct of_device_id __maybe_unused brcmexp_gpio_ids[] = {
285 + { .compatible = "brcm,bcm2835-expgpio" },
286 + { }
287 +};
288 +MODULE_DEVICE_TABLE(of, brcmexp_gpio_ids);
289 +
290 +static struct platform_driver brcmexp_gpio_driver = {
291 + .driver = {
292 + .name = MODULE_NAME,
293 + .owner = THIS_MODULE,
294 + .of_match_table = of_match_ptr(brcmexp_gpio_ids),
295 + },
296 + .probe = brcmexp_gpio_probe,
297 + .remove = brcmexp_gpio_remove,
298 +};
299 +module_platform_driver(brcmexp_gpio_driver);
300 +
301 +MODULE_LICENSE("GPL");
302 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.org>");
303 +MODULE_DESCRIPTION("brcm-exp GPIO driver");
304 +MODULE_ALIAS("platform:brcmexp-gpio");