4 * Copyright (C) 2012,2013 Dom Cobley <popcornmix@gmail.com>
5 * Based on gpio-clps711x.c by Alexander Shiyan <shc_work@mail.ru>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/err.h>
14 #include <linux/gpio.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/dma-mapping.h>
18 #include <soc/bcm2835/raspberrypi-firmware.h>
20 #define MODULE_NAME "brcmvirt-gpio"
23 struct brcmvirt_gpio {
26 /* two packed 16-bit counts of enabled and disables
27 Allows host to detect a brief enable that was missed */
28 u32 enables_disables[NUM_GPIO];
32 static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off)
34 struct brcmvirt_gpio *gpio;
35 gpio = container_of(gc, struct brcmvirt_gpio, gc);
39 static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val)
41 struct brcmvirt_gpio *gpio;
42 gpio = container_of(gc, struct brcmvirt_gpio, gc);
46 static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off)
48 struct brcmvirt_gpio *gpio;
50 gpio = container_of(gc, struct brcmvirt_gpio, gc);
51 v = readl(gpio->ts_base + off);
52 return (s16)((v >> 16) - v) > 0;
55 static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val)
57 struct brcmvirt_gpio *gpio;
58 u16 enables, disables;
61 gpio = container_of(gc, struct brcmvirt_gpio, gc);
62 enables = gpio->enables_disables[off] >> 16;
63 disables = gpio->enables_disables[off] >> 0;
64 diff = (s16)(enables - disables);
66 if ((val && lit) || (!val && !lit))
72 diff = (s16)(enables - disables);
73 BUG_ON(diff != 0 && diff != 1);
74 gpio->enables_disables[off] = (enables << 16) | (disables << 0);
75 writel(gpio->enables_disables[off], gpio->ts_base + off);
78 static int brcmvirt_gpio_probe(struct platform_device *pdev)
81 struct device *dev = &pdev->dev;
82 struct device_node *np = dev->of_node;
83 struct device_node *fw_node;
84 struct rpi_firmware *fw;
85 struct brcmvirt_gpio *ucb;
88 fw_node = of_parse_phandle(np, "firmware", 0);
90 dev_err(dev, "Missing firmware node\n");
94 fw = rpi_firmware_get(fw_node);
98 ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
104 ucb->ts_base = dma_alloc_coherent(dev, PAGE_SIZE, &ucb->bus_addr, GFP_KERNEL);
106 pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n",
107 __func__, PAGE_SIZE);
112 gpiovirtbuf = (u32)ucb->bus_addr;
113 err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF,
114 &gpiovirtbuf, sizeof(gpiovirtbuf));
116 if (err || gpiovirtbuf != 0) {
117 dev_warn(dev, "Failed to set gpiovirtbuf, trying to get err:%x\n", err);
118 dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
124 err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
125 &gpiovirtbuf, sizeof(gpiovirtbuf));
128 dev_err(dev, "Failed to get gpiovirtbuf\n");
133 dev_err(dev, "No virtgpio buffer\n");
138 // mmap the physical memory
139 gpiovirtbuf &= ~0xc0000000;
140 ucb->ts_base = ioremap(gpiovirtbuf, 4096);
141 if (ucb->ts_base == NULL) {
142 dev_err(dev, "Failed to map physical address\n");
148 ucb->gc.label = MODULE_NAME;
149 ucb->gc.owner = THIS_MODULE;
151 ucb->gc.of_node = np;
153 ucb->gc.ngpio = NUM_GPIO;
155 ucb->gc.direction_input = brcmvirt_gpio_dir_in;
156 ucb->gc.direction_output = brcmvirt_gpio_dir_out;
157 ucb->gc.get = brcmvirt_gpio_get;
158 ucb->gc.set = brcmvirt_gpio_set;
159 ucb->gc.can_sleep = true;
161 err = gpiochip_add(&ucb->gc);
165 platform_set_drvdata(pdev, ucb);
170 dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
173 } else if (ucb->ts_base) {
174 iounmap(ucb->ts_base);
180 static int brcmvirt_gpio_remove(struct platform_device *pdev)
182 struct device *dev = &pdev->dev;
184 struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
186 gpiochip_remove(&ucb->gc);
188 dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
189 else if (ucb->ts_base)
190 iounmap(ucb->ts_base);
194 static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
195 { .compatible = "brcm,bcm2835-virtgpio" },
198 MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
200 static struct platform_driver brcmvirt_gpio_driver = {
203 .owner = THIS_MODULE,
204 .of_match_table = of_match_ptr(brcmvirt_gpio_ids),
206 .probe = brcmvirt_gpio_probe,
207 .remove = brcmvirt_gpio_remove,
209 module_platform_driver(brcmvirt_gpio_driver);
211 MODULE_LICENSE("GPL");
212 MODULE_AUTHOR("Dom Cobley <popcornmix@gmail.com>");
213 MODULE_DESCRIPTION("brcmvirt GPIO driver");
214 MODULE_ALIAS("platform:brcmvirt-gpio");