]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/gpio/gpio-bcm-virt.c
clk: bcm2835: Don't rate change PLLs on behalf of DSI PLL dividers.
[mirror_ubuntu-zesty-kernel.git] / drivers / gpio / gpio-bcm-virt.c
1 /*
2 * brcmvirt GPIO driver
3 *
4 * Copyright (C) 2012,2013 Dom Cobley <popcornmix@gmail.com>
5 * Based on gpio-clps711x.c by Alexander Shiyan <shc_work@mail.ru>
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.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 (v >> off) & 1;
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;
83 struct device_node *fw_node;
84 struct rpi_firmware *fw;
85 struct brcmvirt_gpio *ucb;
86 u32 gpiovirtbuf;
87
88 fw_node = of_parse_phandle(np, "firmware", 0);
89 if (!fw_node) {
90 dev_err(dev, "Missing firmware node\n");
91 return -ENOENT;
92 }
93
94 fw = rpi_firmware_get(fw_node);
95 if (!fw)
96 return -EPROBE_DEFER;
97
98 ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
99 if (!ucb) {
100 err = -EINVAL;
101 goto out;
102 }
103
104 ucb->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ucb->bus_addr, GFP_KERNEL);
105 if (!ucb->ts_base) {
106 pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n",
107 __func__, PAGE_SIZE);
108 err = -ENOMEM;
109 goto out;
110 }
111
112 gpiovirtbuf = (u32)ucb->bus_addr;
113 err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF,
114 &gpiovirtbuf, sizeof(gpiovirtbuf));
115
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);
119 ucb->ts_base = 0;
120 ucb->bus_addr = 0;
121 }
122
123 if (!ucb->ts_base) {
124 err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
125 &gpiovirtbuf, sizeof(gpiovirtbuf));
126
127 if (err) {
128 dev_err(dev, "Failed to get gpiovirtbuf\n");
129 goto out;
130 }
131
132 if (!gpiovirtbuf) {
133 dev_err(dev, "No virtgpio buffer\n");
134 err = -ENOENT;
135 goto out;
136 }
137
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");
143 err = -ENOENT;
144 goto out;
145 }
146 ucb->bus_addr = 0;
147 }
148 ucb->gc.label = MODULE_NAME;
149 ucb->gc.owner = THIS_MODULE;
150 //ucb->gc.dev = dev;
151 ucb->gc.of_node = np;
152 ucb->gc.base = 100;
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(&ucb->gc);
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 int brcmvirt_gpio_remove(struct platform_device *pdev)
181 {
182 struct device *dev = &pdev->dev;
183 int err = 0;
184 struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
185
186 gpiochip_remove(&ucb->gc);
187 if (ucb->bus_addr)
188 dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
189 else if (ucb->ts_base)
190 iounmap(ucb->ts_base);
191 return err;
192 }
193
194 static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
195 { .compatible = "brcm,bcm2835-virtgpio" },
196 { }
197 };
198 MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
199
200 static struct platform_driver brcmvirt_gpio_driver = {
201 .driver = {
202 .name = MODULE_NAME,
203 .owner = THIS_MODULE,
204 .of_match_table = of_match_ptr(brcmvirt_gpio_ids),
205 },
206 .probe = brcmvirt_gpio_probe,
207 .remove = brcmvirt_gpio_remove,
208 };
209 module_platform_driver(brcmvirt_gpio_driver);
210
211 MODULE_LICENSE("GPL");
212 MODULE_AUTHOR("Dom Cobley <popcornmix@gmail.com>");
213 MODULE_DESCRIPTION("brcmvirt GPIO driver");
214 MODULE_ALIAS("platform:brcmvirt-gpio");