1 // SPDX-License-Identifier: GPL-2.0+
3 * Raspberry Pi 3 expander GPIO driver
5 * Uses the firmware mailbox service to communicate with the
6 * GPIO expander on the VPU.
8 * Copyright (C) 2017 Raspberry Pi Trading Ltd.
11 #include <linux/err.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <soc/bcm2835/raspberrypi-firmware.h>
17 #define MODULE_NAME "raspberrypi-exp-gpio"
20 #define RPI_EXP_GPIO_BASE 128
22 #define RPI_EXP_GPIO_DIR_IN 0
23 #define RPI_EXP_GPIO_DIR_OUT 1
27 struct rpi_firmware *fw;
30 /* VC4 firmware mailbox interface data structures */
32 struct gpio_set_config {
41 struct gpio_get_config {
49 struct gpio_get_set_state {
54 static int rpi_exp_gpio_get_polarity(struct gpio_chip *gc, unsigned int off)
56 struct rpi_exp_gpio *gpio;
57 struct gpio_get_config get;
60 gpio = gpiochip_get_data(gc);
62 get.gpio = off + RPI_EXP_GPIO_BASE; /* GPIO to update */
64 ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
66 if (ret || get.gpio != 0) {
67 dev_err(gc->parent, "Failed to get GPIO %u config (%d %x)\n",
69 return ret ? ret : -EIO;
74 static int rpi_exp_gpio_dir_in(struct gpio_chip *gc, unsigned int off)
76 struct rpi_exp_gpio *gpio;
77 struct gpio_set_config set_in;
80 gpio = gpiochip_get_data(gc);
82 set_in.gpio = off + RPI_EXP_GPIO_BASE; /* GPIO to update */
83 set_in.direction = RPI_EXP_GPIO_DIR_IN;
84 set_in.term_en = 0; /* termination disabled */
85 set_in.term_pull_up = 0; /* n/a as termination disabled */
86 set_in.state = 0; /* n/a as configured as an input */
88 ret = rpi_exp_gpio_get_polarity(gc, off);
91 set_in.polarity = ret; /* Retain existing setting */
93 ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
94 &set_in, sizeof(set_in));
95 if (ret || set_in.gpio != 0) {
96 dev_err(gc->parent, "Failed to set GPIO %u to input (%d %x)\n",
97 off, ret, set_in.gpio);
98 return ret ? ret : -EIO;
103 static int rpi_exp_gpio_dir_out(struct gpio_chip *gc, unsigned int off, int val)
105 struct rpi_exp_gpio *gpio;
106 struct gpio_set_config set_out;
109 gpio = gpiochip_get_data(gc);
111 set_out.gpio = off + RPI_EXP_GPIO_BASE; /* GPIO to update */
112 set_out.direction = RPI_EXP_GPIO_DIR_OUT;
113 set_out.term_en = 0; /* n/a as an output */
114 set_out.term_pull_up = 0; /* n/a as termination disabled */
115 set_out.state = val; /* Output state */
117 ret = rpi_exp_gpio_get_polarity(gc, off);
120 set_out.polarity = ret; /* Retain existing setting */
122 ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_CONFIG,
123 &set_out, sizeof(set_out));
124 if (ret || set_out.gpio != 0) {
125 dev_err(gc->parent, "Failed to set GPIO %u to output (%d %x)\n",
126 off, ret, set_out.gpio);
127 return ret ? ret : -EIO;
132 static int rpi_exp_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
134 struct rpi_exp_gpio *gpio;
135 struct gpio_get_config get;
138 gpio = gpiochip_get_data(gc);
140 get.gpio = off + RPI_EXP_GPIO_BASE; /* GPIO to update */
142 ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_CONFIG,
144 if (ret || get.gpio != 0) {
146 "Failed to get GPIO %u config (%d %x)\n", off, ret,
148 return ret ? ret : -EIO;
151 return GPIO_LINE_DIRECTION_OUT;
153 return GPIO_LINE_DIRECTION_IN;
156 static int rpi_exp_gpio_get(struct gpio_chip *gc, unsigned int off)
158 struct rpi_exp_gpio *gpio;
159 struct gpio_get_set_state get;
162 gpio = gpiochip_get_data(gc);
164 get.gpio = off + RPI_EXP_GPIO_BASE; /* GPIO to update */
165 get.state = 0; /* storage for returned value */
167 ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_GET_GPIO_STATE,
169 if (ret || get.gpio != 0) {
171 "Failed to get GPIO %u state (%d %x)\n", off, ret,
173 return ret ? ret : -EIO;
178 static void rpi_exp_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
180 struct rpi_exp_gpio *gpio;
181 struct gpio_get_set_state set;
184 gpio = gpiochip_get_data(gc);
186 set.gpio = off + RPI_EXP_GPIO_BASE; /* GPIO to update */
187 set.state = val; /* Output state */
189 ret = rpi_firmware_property(gpio->fw, RPI_FIRMWARE_SET_GPIO_STATE,
191 if (ret || set.gpio != 0)
193 "Failed to set GPIO %u state (%d %x)\n", off, ret,
197 static int rpi_exp_gpio_probe(struct platform_device *pdev)
199 struct device *dev = &pdev->dev;
200 struct device_node *np = dev->of_node;
201 struct device_node *fw_node;
202 struct rpi_firmware *fw;
203 struct rpi_exp_gpio *rpi_gpio;
205 fw_node = of_get_parent(np);
207 dev_err(dev, "Missing firmware node\n");
211 fw = devm_rpi_firmware_get(&pdev->dev, fw_node);
212 of_node_put(fw_node);
214 return -EPROBE_DEFER;
216 rpi_gpio = devm_kzalloc(dev, sizeof(*rpi_gpio), GFP_KERNEL);
221 rpi_gpio->gc.parent = dev;
222 rpi_gpio->gc.label = MODULE_NAME;
223 rpi_gpio->gc.owner = THIS_MODULE;
224 rpi_gpio->gc.base = -1;
225 rpi_gpio->gc.ngpio = NUM_GPIO;
227 rpi_gpio->gc.direction_input = rpi_exp_gpio_dir_in;
228 rpi_gpio->gc.direction_output = rpi_exp_gpio_dir_out;
229 rpi_gpio->gc.get_direction = rpi_exp_gpio_get_direction;
230 rpi_gpio->gc.get = rpi_exp_gpio_get;
231 rpi_gpio->gc.set = rpi_exp_gpio_set;
232 rpi_gpio->gc.can_sleep = true;
234 return devm_gpiochip_add_data(dev, &rpi_gpio->gc, rpi_gpio);
237 static const struct of_device_id rpi_exp_gpio_ids[] = {
238 { .compatible = "raspberrypi,firmware-gpio" },
241 MODULE_DEVICE_TABLE(of, rpi_exp_gpio_ids);
243 static struct platform_driver rpi_exp_gpio_driver = {
246 .of_match_table = of_match_ptr(rpi_exp_gpio_ids),
248 .probe = rpi_exp_gpio_probe,
250 module_platform_driver(rpi_exp_gpio_driver);
252 MODULE_LICENSE("GPL");
253 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.org>");
254 MODULE_DESCRIPTION("Raspberry Pi 3 expander GPIO driver");
255 MODULE_ALIAS("platform:rpi-exp-gpio");