|
| 1 | +/* |
| 2 | + * brcmvirt GPIO driver |
| 3 | + * |
| 4 | + * Copyright (C) 2012,2013 Dom Cobley <[email protected]> |
| 5 | + * Based on gpio-clps711x.c by Alexander Shiyan <[email protected]> |
| 6 | + * |
| 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. |
| 11 | + */ |
| 12 | + |
| 13 | +#include <linux/err.h> |
| 14 | +#include <linux/gpio/driver.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> |
| 19 | + |
| 20 | +#define MODULE_NAME "brcmvirt-gpio" |
| 21 | +#define NUM_GPIO 2 |
| 22 | + |
| 23 | +struct brcmvirt_gpio { |
| 24 | + struct gpio_chip gc; |
| 25 | + u32 __iomem *ts_base; |
| 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]; |
| 29 | + dma_addr_t bus_addr; |
| 30 | +}; |
| 31 | + |
| 32 | +static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off) |
| 33 | +{ |
| 34 | + struct brcmvirt_gpio *gpio; |
| 35 | + gpio = container_of(gc, struct brcmvirt_gpio, gc); |
| 36 | + return -EINVAL; |
| 37 | +} |
| 38 | + |
| 39 | +static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val) |
| 40 | +{ |
| 41 | + struct brcmvirt_gpio *gpio; |
| 42 | + gpio = container_of(gc, struct brcmvirt_gpio, gc); |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off) |
| 47 | +{ |
| 48 | + struct brcmvirt_gpio *gpio; |
| 49 | + unsigned v; |
| 50 | + gpio = container_of(gc, struct brcmvirt_gpio, gc); |
| 51 | + v = readl(gpio->ts_base + off); |
| 52 | + return (s16)((v >> 16) - v) > 0; |
| 53 | +} |
| 54 | + |
| 55 | +static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val) |
| 56 | +{ |
| 57 | + struct brcmvirt_gpio *gpio; |
| 58 | + u16 enables, disables; |
| 59 | + s16 diff; |
| 60 | + bool lit; |
| 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); |
| 65 | + lit = diff > 0; |
| 66 | + if ((val && lit) || (!val && !lit)) |
| 67 | + return; |
| 68 | + if (val) |
| 69 | + enables++; |
| 70 | + else |
| 71 | + disables++; |
| 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); |
| 76 | +} |
| 77 | + |
| 78 | +static int brcmvirt_gpio_probe(struct platform_device *pdev) |
| 79 | +{ |
| 80 | + int err = 0; |
| 81 | + struct device *dev = &pdev->dev; |
| 82 | + struct device_node *np = dev_of_node(dev); |
| 83 | + struct device_node *fw_node; |
| 84 | + struct rpi_firmware *fw; |
| 85 | + struct brcmvirt_gpio *ucb; |
| 86 | + u32 gpiovirtbuf; |
| 87 | + |
| 88 | + fw_node = of_get_parent(np); |
| 89 | + if (!fw_node) { |
| 90 | + dev_err(dev, "Missing firmware node\n"); |
| 91 | + return -ENOENT; |
| 92 | + } |
| 93 | + |
| 94 | + fw = devm_rpi_firmware_get(&pdev->dev, fw_node); |
| 95 | + of_node_put(fw_node); |
| 96 | + if (!fw) |
| 97 | + return -EPROBE_DEFER; |
| 98 | + |
| 99 | + ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL); |
| 100 | + if (!ucb) { |
| 101 | + err = -EINVAL; |
| 102 | + goto out; |
| 103 | + } |
| 104 | + |
| 105 | + ucb->ts_base = dma_alloc_coherent(dev, PAGE_SIZE, &ucb->bus_addr, GFP_KERNEL); |
| 106 | + if (!ucb->ts_base) { |
| 107 | + pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n", |
| 108 | + __func__, PAGE_SIZE); |
| 109 | + err = -ENOMEM; |
| 110 | + goto out; |
| 111 | + } |
| 112 | + |
| 113 | + gpiovirtbuf = (u32)ucb->bus_addr; |
| 114 | + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF, |
| 115 | + &gpiovirtbuf, sizeof(gpiovirtbuf)); |
| 116 | + |
| 117 | + if (err || gpiovirtbuf != 0) { |
| 118 | + dev_warn(dev, "Failed to set gpiovirtbuf, trying to get err:%x\n", err); |
| 119 | + dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr); |
| 120 | + ucb->ts_base = 0; |
| 121 | + ucb->bus_addr = 0; |
| 122 | + } |
| 123 | + |
| 124 | + if (!ucb->ts_base) { |
| 125 | + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF, |
| 126 | + &gpiovirtbuf, sizeof(gpiovirtbuf)); |
| 127 | + |
| 128 | + if (err) { |
| 129 | + dev_err(dev, "Failed to get gpiovirtbuf\n"); |
| 130 | + goto out; |
| 131 | + } |
| 132 | + |
| 133 | + if (!gpiovirtbuf) { |
| 134 | + dev_err(dev, "No virtgpio buffer\n"); |
| 135 | + err = -ENOENT; |
| 136 | + goto out; |
| 137 | + } |
| 138 | + |
| 139 | + // mmap the physical memory |
| 140 | + gpiovirtbuf &= ~0xc0000000; |
| 141 | + ucb->ts_base = ioremap(gpiovirtbuf, 4096); |
| 142 | + if (ucb->ts_base == NULL) { |
| 143 | + dev_err(dev, "Failed to map physical address\n"); |
| 144 | + err = -ENOENT; |
| 145 | + goto out; |
| 146 | + } |
| 147 | + ucb->bus_addr = 0; |
| 148 | + } |
| 149 | + ucb->gc.parent = dev; |
| 150 | + ucb->gc.label = MODULE_NAME; |
| 151 | + ucb->gc.owner = THIS_MODULE; |
| 152 | + ucb->gc.base = -1; |
| 153 | + ucb->gc.ngpio = NUM_GPIO; |
| 154 | + |
| 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; |
| 160 | + |
| 161 | + err = gpiochip_add_data(&ucb->gc, NULL); |
| 162 | + if (err) |
| 163 | + goto out; |
| 164 | + |
| 165 | + platform_set_drvdata(pdev, ucb); |
| 166 | + |
| 167 | + return 0; |
| 168 | +out: |
| 169 | + if (ucb->bus_addr) { |
| 170 | + dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr); |
| 171 | + ucb->bus_addr = 0; |
| 172 | + ucb->ts_base = NULL; |
| 173 | + } else if (ucb->ts_base) { |
| 174 | + iounmap(ucb->ts_base); |
| 175 | + ucb->ts_base = NULL; |
| 176 | + } |
| 177 | + return err; |
| 178 | +} |
| 179 | + |
| 180 | +static void brcmvirt_gpio_remove(struct platform_device *pdev) |
| 181 | +{ |
| 182 | + struct device *dev = &pdev->dev; |
| 183 | + struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev); |
| 184 | + |
| 185 | + gpiochip_remove(&ucb->gc); |
| 186 | + if (ucb->bus_addr) |
| 187 | + dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr); |
| 188 | + else if (ucb->ts_base) |
| 189 | + iounmap(ucb->ts_base); |
| 190 | +} |
| 191 | + |
| 192 | +static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = { |
| 193 | + { .compatible = "brcm,bcm2835-virtgpio" }, |
| 194 | + { } |
| 195 | +}; |
| 196 | +MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids); |
| 197 | + |
| 198 | +static struct platform_driver brcmvirt_gpio_driver = { |
| 199 | + .driver = { |
| 200 | + .name = MODULE_NAME, |
| 201 | + .owner = THIS_MODULE, |
| 202 | + .of_match_table = of_match_ptr(brcmvirt_gpio_ids), |
| 203 | + }, |
| 204 | + .probe = brcmvirt_gpio_probe, |
| 205 | + .remove = brcmvirt_gpio_remove, |
| 206 | +}; |
| 207 | +module_platform_driver(brcmvirt_gpio_driver); |
| 208 | + |
| 209 | +MODULE_LICENSE("GPL"); |
| 210 | +MODULE_AUTHOR( "Dom Cobley <[email protected]>"); |
| 211 | +MODULE_DESCRIPTION("brcmvirt GPIO driver"); |
| 212 | +MODULE_ALIAS("platform:brcmvirt-gpio"); |
0 commit comments