0159-bcm2835-virtgpio-Virtual-GPIO-driver.patch 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. From 3ffde07f9d0ee50522cb017639c12a83cfd6409b Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Tue, 23 Feb 2016 19:56:04 +0000
  4. Subject: [PATCH] bcm2835-virtgpio: Virtual GPIO driver
  5. Add a virtual GPIO driver that uses the firmware mailbox interface to
  6. request that the VPU toggles LEDs.
  7. ---
  8. arch/arm/configs/bcm2709_defconfig | 1 +
  9. drivers/gpio/Kconfig | 6 +
  10. drivers/gpio/Makefile | 1 +
  11. drivers/gpio/gpio-bcm-virt.c | 180 +++++++++++++++++++++++++++++
  12. include/soc/bcm2835/raspberrypi-firmware.h | 1 +
  13. 5 files changed, 189 insertions(+)
  14. create mode 100644 drivers/gpio/gpio-bcm-virt.c
  15. --- a/arch/arm/configs/bcm2709_defconfig
  16. +++ b/arch/arm/configs/bcm2709_defconfig
  17. @@ -607,6 +607,7 @@ CONFIG_PPS=m
  18. CONFIG_PPS_CLIENT_LDISC=m
  19. CONFIG_PPS_CLIENT_GPIO=m
  20. CONFIG_GPIO_SYSFS=y
  21. +CONFIG_GPIO_BCM_VIRT=y
  22. CONFIG_GPIO_ARIZONA=m
  23. CONFIG_GPIO_STMPE=y
  24. CONFIG_W1=m
  25. --- a/drivers/gpio/Kconfig
  26. +++ b/drivers/gpio/Kconfig
  27. @@ -133,6 +133,12 @@ config GPIO_BCM_KONA
  28. help
  29. Turn on GPIO support for Broadcom "Kona" chips.
  30. +config GPIO_BCM_VIRT
  31. + bool "Broadcom Virt GPIO"
  32. + depends on OF_GPIO && RASPBERRYPI_FIRMWARE && (ARCH_BCM2835 || ARCH_BCM2708 || ARCH_BCM2709 || COMPILE_TEST)
  33. + help
  34. + Turn on virtual GPIO support for Broadcom BCM283X chips.
  35. +
  36. config GPIO_BRCMSTB
  37. tristate "BRCMSTB GPIO support"
  38. default y if ARCH_BRCMSTB
  39. --- a/drivers/gpio/Makefile
  40. +++ b/drivers/gpio/Makefile
  41. @@ -24,6 +24,7 @@ obj-$(CONFIG_GPIO_AMDPT) += gpio-amdpt.o
  42. obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizona.o
  43. obj-$(CONFIG_ATH79) += gpio-ath79.o
  44. obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
  45. +obj-$(CONFIG_GPIO_BCM_VIRT) += gpio-bcm-virt.o
  46. obj-$(CONFIG_GPIO_BRCMSTB) += gpio-brcmstb.o
  47. obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o
  48. obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o
  49. --- /dev/null
  50. +++ b/drivers/gpio/gpio-bcm-virt.c
  51. @@ -0,0 +1,180 @@
  52. +/*
  53. + * brcmvirt GPIO driver
  54. + *
  55. + * Copyright (C) 2012,2013 Dom Cobley <popcornmix@gmail.com>
  56. + * Based on gpio-clps711x.c by Alexander Shiyan <shc_work@mail.ru>
  57. + *
  58. + * This program is free software; you can redistribute it and/or modify
  59. + * it under the terms of the GNU General Public License as published by
  60. + * the Free Software Foundation; either version 2 of the License, or
  61. + * (at your option) any later version.
  62. + */
  63. +
  64. +#include <linux/err.h>
  65. +#include <linux/gpio.h>
  66. +#include <linux/module.h>
  67. +#include <linux/basic_mmio_gpio.h>
  68. +#include <linux/platform_device.h>
  69. +#include <soc/bcm2835/raspberrypi-firmware.h>
  70. +
  71. +#define MODULE_NAME "brcmvirt-gpio"
  72. +#define NUM_GPIO 2
  73. +
  74. +struct brcmvirt_gpio {
  75. + struct gpio_chip gc;
  76. + u32 __iomem *ts_base;
  77. + /* two packed 16-bit counts of enabled and disables
  78. + Allows host to detect a brief enable that was missed */
  79. + u32 enables_disables[NUM_GPIO];
  80. +};
  81. +
  82. +static int brcmvirt_gpio_dir_in(struct gpio_chip *gc, unsigned off)
  83. +{
  84. + struct brcmvirt_gpio *gpio;
  85. + gpio = container_of(gc, struct brcmvirt_gpio, gc);
  86. + return -EINVAL;
  87. +}
  88. +
  89. +static int brcmvirt_gpio_dir_out(struct gpio_chip *gc, unsigned off, int val)
  90. +{
  91. + struct brcmvirt_gpio *gpio;
  92. + gpio = container_of(gc, struct brcmvirt_gpio, gc);
  93. + return 0;
  94. +}
  95. +
  96. +static int brcmvirt_gpio_get(struct gpio_chip *gc, unsigned off)
  97. +{
  98. + struct brcmvirt_gpio *gpio;
  99. + unsigned v;
  100. + gpio = container_of(gc, struct brcmvirt_gpio, gc);
  101. + v = readl(gpio->ts_base + off);
  102. + return (v >> off) & 1;
  103. +}
  104. +
  105. +static void brcmvirt_gpio_set(struct gpio_chip *gc, unsigned off, int val)
  106. +{
  107. + struct brcmvirt_gpio *gpio;
  108. + u16 enables, disables;
  109. + s16 diff;
  110. + bool lit;
  111. + gpio = container_of(gc, struct brcmvirt_gpio, gc);
  112. + enables = gpio->enables_disables[off] >> 16;
  113. + disables = gpio->enables_disables[off] >> 0;
  114. + diff = (s16)(enables - disables);
  115. + lit = diff > 0;
  116. + if ((val && lit) || (!val && !lit))
  117. + return;
  118. + if (val)
  119. + enables++;
  120. + else
  121. + disables++;
  122. + diff = (s16)(enables - disables);
  123. + BUG_ON(diff != 0 && diff != 1);
  124. + gpio->enables_disables[off] = (enables << 16) | (disables << 0);
  125. + writel(gpio->enables_disables[off], gpio->ts_base + off);
  126. +}
  127. +
  128. +static int brcmvirt_gpio_probe(struct platform_device *pdev)
  129. +{
  130. + struct device *dev = &pdev->dev;
  131. + struct device_node *np = dev->of_node;
  132. + struct device_node *fw_node;
  133. + struct rpi_firmware *fw;
  134. + struct brcmvirt_gpio *ucb;
  135. + u32 gpiovirtbuf;
  136. + int err = 0;
  137. +
  138. + fw_node = of_parse_phandle(np, "firmware", 0);
  139. + if (!fw_node) {
  140. + dev_err(dev, "Missing firmware node\n");
  141. + return -ENOENT;
  142. + }
  143. +
  144. + fw = rpi_firmware_get(fw_node);
  145. + if (!fw)
  146. + return -EPROBE_DEFER;
  147. +
  148. + err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
  149. + &gpiovirtbuf, sizeof(gpiovirtbuf));
  150. +
  151. + if (err) {
  152. + dev_err(dev, "Failed to get gpiovirtbuf\n");
  153. + goto err;
  154. + }
  155. +
  156. + if (!gpiovirtbuf) {
  157. + dev_err(dev, "No virtgpio buffer\n");
  158. + err = -ENOENT;
  159. + goto err;
  160. + }
  161. +
  162. + ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
  163. + if (!ucb) {
  164. + err = -EINVAL;
  165. + goto err;
  166. + }
  167. +
  168. + // mmap the physical memory
  169. + gpiovirtbuf &= ~0xc0000000;
  170. + ucb->ts_base = ioremap(gpiovirtbuf, 4096);
  171. + if (ucb->ts_base == NULL) {
  172. + dev_err(dev, "Failed to map physical address\n");
  173. + err = -ENOENT;
  174. + goto err;
  175. + }
  176. +
  177. + ucb->gc.label = MODULE_NAME;
  178. + ucb->gc.owner = THIS_MODULE;
  179. + ucb->gc.dev = dev;
  180. + ucb->gc.of_node = np;
  181. + ucb->gc.base = 100;
  182. + ucb->gc.ngpio = NUM_GPIO;
  183. +
  184. + ucb->gc.direction_input = brcmvirt_gpio_dir_in;
  185. + ucb->gc.direction_output = brcmvirt_gpio_dir_out;
  186. + ucb->gc.get = brcmvirt_gpio_get;
  187. + ucb->gc.set = brcmvirt_gpio_set;
  188. + ucb->gc.can_sleep = true;
  189. +
  190. + err = gpiochip_add(&ucb->gc);
  191. + if (err)
  192. + goto err;
  193. +
  194. + platform_set_drvdata(pdev, ucb);
  195. +
  196. +err:
  197. + return err;
  198. +
  199. +}
  200. +
  201. +static int brcmvirt_gpio_remove(struct platform_device *pdev)
  202. +{
  203. + int err = 0;
  204. + struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
  205. +
  206. + gpiochip_remove(&ucb->gc);
  207. + iounmap(ucb->ts_base);
  208. + return err;
  209. +}
  210. +
  211. +static const struct of_device_id __maybe_unused brcmvirt_gpio_ids[] = {
  212. + { .compatible = "brcm,bcm2835-virtgpio" },
  213. + { }
  214. +};
  215. +MODULE_DEVICE_TABLE(of, brcmvirt_gpio_ids);
  216. +
  217. +static struct platform_driver brcmvirt_gpio_driver = {
  218. + .driver = {
  219. + .name = MODULE_NAME,
  220. + .owner = THIS_MODULE,
  221. + .of_match_table = of_match_ptr(brcmvirt_gpio_ids),
  222. + },
  223. + .probe = brcmvirt_gpio_probe,
  224. + .remove = brcmvirt_gpio_remove,
  225. +};
  226. +module_platform_driver(brcmvirt_gpio_driver);
  227. +
  228. +MODULE_LICENSE("GPL");
  229. +MODULE_AUTHOR("Dom Cobley <popcornmix@gmail.com>");
  230. +MODULE_DESCRIPTION("brcmvirt GPIO driver");
  231. +MODULE_ALIAS("platform:brcmvirt-gpio");
  232. --- a/include/soc/bcm2835/raspberrypi-firmware.h
  233. +++ b/include/soc/bcm2835/raspberrypi-firmware.h
  234. @@ -93,6 +93,7 @@ enum rpi_firmware_property_tag {
  235. RPI_FIRMWARE_FRAMEBUFFER_GET_OVERSCAN = 0x0004000a,
  236. RPI_FIRMWARE_FRAMEBUFFER_GET_PALETTE = 0x0004000b,
  237. RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF = 0x0004000f,
  238. + RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF = 0x00040010,
  239. RPI_FIRMWARE_FRAMEBUFFER_RELEASE = 0x00048001,
  240. RPI_FIRMWARE_FRAMEBUFFER_TEST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
  241. RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,