950-0907-drivers-char-add-generic-gpiomem-driver.patch 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. From fdf9cab5eaa849e90b12e17718bc47130a91433c Mon Sep 17 00:00:00 2001
  2. From: Jonathan Bell <[email protected]>
  3. Date: Tue, 25 Apr 2023 15:52:13 +0100
  4. Subject: [PATCH] drivers: char: add generic gpiomem driver
  5. Based on bcm2835-gpiomem.
  6. We allow export of the "GPIO registers" to userspace via a chardev as
  7. this allows for finer access control (e.g. users must be group gpio, root
  8. not required).
  9. This driver allows access to either rp1-gpiomem or gpiomem, depending on
  10. which nodes are populated in devicetree.
  11. RP1 has a different look-and-feel to BCM283x SoCs as it has split ranges
  12. for IO controls and the parallel registered OE/IN/OUT access. To handle
  13. this, the driver concatenates the ranges for an IO bank and the
  14. corresponding RIO instance into a contiguous buffer.
  15. Signed-off-by: Jonathan Bell <[email protected]>
  16. ---
  17. drivers/char/Kconfig | 8 +
  18. drivers/char/Makefile | 1 +
  19. drivers/char/raspberrypi-gpiomem.c | 276 +++++++++++++++++++++++++++++
  20. 3 files changed, 285 insertions(+)
  21. create mode 100644 drivers/char/raspberrypi-gpiomem.c
  22. --- a/drivers/char/Kconfig
  23. +++ b/drivers/char/Kconfig
  24. @@ -461,4 +461,12 @@ config RANDOM_TRUST_BOOTLOADER
  25. believe its RNG facilities may be faulty. This may also be configured
  26. at boot time with "random.trust_bootloader=on/off".
  27. +config RASPBERRYPI_GPIOMEM
  28. + tristate "Rootless GPIO access via mmap() on Raspberry Pi boards"
  29. + default n
  30. + help
  31. + Provides users with root-free access to the GPIO registers
  32. + on the board. Calling mmap(/dev/gpiomem) will map the GPIO
  33. + register page to the user's pointer.
  34. +
  35. endmenu
  36. --- a/drivers/char/Makefile
  37. +++ b/drivers/char/Makefile
  38. @@ -46,3 +46,4 @@ obj-$(CONFIG_XILLYBUS_CLASS) += xillybus
  39. obj-$(CONFIG_POWERNV_OP_PANEL) += powernv-op-panel.o
  40. obj-$(CONFIG_ADI) += adi.o
  41. obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
  42. +obj-$(CONFIG_RASPBERRYPI_GPIOMEM) += raspberrypi-gpiomem.o
  43. --- /dev/null
  44. +++ b/drivers/char/raspberrypi-gpiomem.c
  45. @@ -0,0 +1,276 @@
  46. +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  47. +/**
  48. + * raspberrypi-gpiomem.c
  49. + *
  50. + * Provides MMIO access to discontiguous section of Device memory as a linear
  51. + * user mapping. Successor to bcm2835-gpiomem.c.
  52. + *
  53. + * Copyright (c) 2023, Raspberry Pi Ltd.
  54. + */
  55. +
  56. +#include <linux/kernel.h>
  57. +#include <linux/module.h>
  58. +#include <linux/of.h>
  59. +#include <linux/of_device.h>
  60. +#include <linux/platform_device.h>
  61. +#include <linux/mm.h>
  62. +#include <linux/slab.h>
  63. +#include <linux/cdev.h>
  64. +#include <linux/pagemap.h>
  65. +#include <linux/io.h>
  66. +
  67. +#define DRIVER_NAME "rpi-gpiomem"
  68. +#define DEVICE_MINOR 0
  69. +
  70. +/*
  71. + * Sensible max for a hypothetical "gpio" controller that splits pads,
  72. + * IO controls, GPIO in/out/enable, and function selection into different
  73. + * ranges. Most use only one or two.
  74. + */
  75. +#define MAX_RANGES 4
  76. +
  77. +struct io_windows {
  78. + unsigned long phys_base;
  79. + unsigned long len;
  80. +};
  81. +
  82. +struct rpi_gpiomem_priv {
  83. + dev_t devid;
  84. + struct class *class;
  85. + struct cdev rpi_gpiomem_cdev;
  86. + struct device *dev;
  87. + const char *name;
  88. + unsigned int nr_wins;
  89. + struct io_windows iowins[4];
  90. +};
  91. +
  92. +static int rpi_gpiomem_open(struct inode *inode, struct file *file)
  93. +{
  94. + int dev = iminor(inode);
  95. + int ret = 0;
  96. + struct rpi_gpiomem_priv *priv;
  97. +
  98. + if (dev != DEVICE_MINOR)
  99. + ret = -ENXIO;
  100. +
  101. + priv = container_of(inode->i_cdev, struct rpi_gpiomem_priv,
  102. + rpi_gpiomem_cdev);
  103. + if (!priv)
  104. + return -EINVAL;
  105. + file->private_data = priv;
  106. + return ret;
  107. +}
  108. +
  109. +static int rpi_gpiomem_release(struct inode *inode, struct file *file)
  110. +{
  111. + int dev = iminor(inode);
  112. + int ret = 0;
  113. +
  114. + if (dev != DEVICE_MINOR)
  115. + ret = -ENXIO;
  116. +
  117. + return ret;
  118. +}
  119. +
  120. +static const struct vm_operations_struct rpi_gpiomem_vm_ops = {
  121. +#ifdef CONFIG_HAVE_IOREMAP_PROT
  122. + .access = generic_access_phys
  123. +#endif
  124. +};
  125. +
  126. +static int rpi_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
  127. +{
  128. + int i;
  129. + struct rpi_gpiomem_priv *priv;
  130. + unsigned long base;
  131. + unsigned long len = 0;
  132. + unsigned long offset;
  133. +
  134. + priv = file->private_data;
  135. + /*
  136. + * Userspace must provide a virtual address space at least
  137. + * the size of the concatenated ranges.
  138. + */
  139. + for (i = 0; i < priv->nr_wins; i++)
  140. + len += priv->iowins[i].len;
  141. + if (len > vma->vm_end - vma->vm_start + 1)
  142. + return -EINVAL;
  143. +
  144. + vma->vm_ops = &rpi_gpiomem_vm_ops;
  145. + offset = vma->vm_start;
  146. + for (i = 0; i < priv->nr_wins; i++) {
  147. + base = priv->iowins[i].phys_base >> PAGE_SHIFT;
  148. + len = priv->iowins[i].len;
  149. + vma->vm_page_prot = phys_mem_access_prot(file, base, len,
  150. + vma->vm_page_prot);
  151. + if (remap_pfn_range(vma, offset,
  152. + base, len,
  153. + vma->vm_page_prot))
  154. + break;
  155. + offset += len;
  156. + }
  157. +
  158. + if (i < priv->nr_wins)
  159. + return -EAGAIN;
  160. +
  161. + return 0;
  162. +}
  163. +
  164. +static const struct file_operations rpi_gpiomem_fops = {
  165. + .owner = THIS_MODULE,
  166. + .open = rpi_gpiomem_open,
  167. + .release = rpi_gpiomem_release,
  168. + .mmap = rpi_gpiomem_mmap,
  169. +};
  170. +
  171. +static const struct of_device_id rpi_gpiomem_of_match[];
  172. +
  173. +static int rpi_gpiomem_probe(struct platform_device *pdev)
  174. +{
  175. + int err, i;
  176. + const struct of_device_id *id;
  177. + struct device *dev = &pdev->dev;
  178. + struct device_node *node = dev->of_node;
  179. + struct resource *ioresource;
  180. + struct rpi_gpiomem_priv *priv;
  181. +
  182. + /* Allocate buffers and instance data */
  183. +
  184. + priv = kzalloc(sizeof(struct rpi_gpiomem_priv), GFP_KERNEL);
  185. +
  186. + if (!priv) {
  187. + err = -ENOMEM;
  188. + goto failed_inst_alloc;
  189. + }
  190. + platform_set_drvdata(pdev, priv);
  191. +
  192. + priv->dev = dev;
  193. + id = of_match_device(rpi_gpiomem_of_match, dev);
  194. + if (!id)
  195. + return -EINVAL;
  196. +
  197. + /*
  198. + * Device node naming - for legacy (bcm2835) DT bindings, the driver
  199. + * created the node based on a hardcoded name - for new bindings,
  200. + * take the node name from DT.
  201. + */
  202. + if (id == &rpi_gpiomem_of_match[0]) {
  203. + priv->name = "gpiomem";
  204. + } else {
  205. + err = of_property_read_string(node, "chardev-name", &priv->name);
  206. + if (err)
  207. + return -EINVAL;
  208. + }
  209. +
  210. + /*
  211. + * Go find the register ranges associated with this instance
  212. + */
  213. + for (i = 0; i < MAX_RANGES; i++) {
  214. + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, i);
  215. + if (!ioresource && i == 0) {
  216. + dev_err(priv->dev, "failed to get IO resource - no ranges available\n");
  217. + err = -ENOENT;
  218. + goto failed_get_resource;
  219. + }
  220. + if (!ioresource)
  221. + break;
  222. +
  223. + priv->iowins[i].phys_base = ioresource->start;
  224. + priv->iowins[i].len = (ioresource->end + 1) - ioresource->start;
  225. + dev_info(&pdev->dev, "window base 0x%08lx size 0x%08lx\n",
  226. + priv->iowins[i].phys_base, priv->iowins[i].len);
  227. + priv->nr_wins++;
  228. + }
  229. +
  230. + /* Create character device entries */
  231. +
  232. + err = alloc_chrdev_region(&priv->devid,
  233. + DEVICE_MINOR, 1, priv->name);
  234. + if (err != 0) {
  235. + dev_err(priv->dev, "unable to allocate device number");
  236. + goto failed_alloc_chrdev;
  237. + }
  238. + cdev_init(&priv->rpi_gpiomem_cdev, &rpi_gpiomem_fops);
  239. + priv->rpi_gpiomem_cdev.owner = THIS_MODULE;
  240. + err = cdev_add(&priv->rpi_gpiomem_cdev, priv->devid, 1);
  241. + if (err != 0) {
  242. + dev_err(priv->dev, "unable to register device");
  243. + goto failed_cdev_add;
  244. + }
  245. +
  246. + /* Create sysfs entries */
  247. +
  248. + priv->class = class_create(THIS_MODULE, priv->name);
  249. + if (IS_ERR(priv->class)) {
  250. + err = PTR_ERR(priv->class);
  251. + goto failed_class_create;
  252. + }
  253. +
  254. + dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
  255. + if (IS_ERR(dev)) {
  256. + err = PTR_ERR(dev);
  257. + goto failed_device_create;
  258. + }
  259. +
  260. + dev_info(priv->dev, "initialised %u regions as /dev/%s\n",
  261. + priv->nr_wins, priv->name);
  262. +
  263. + return 0;
  264. +
  265. +failed_device_create:
  266. + class_destroy(priv->class);
  267. +failed_class_create:
  268. + cdev_del(&priv->rpi_gpiomem_cdev);
  269. +failed_cdev_add:
  270. + unregister_chrdev_region(priv->devid, 1);
  271. +failed_alloc_chrdev:
  272. +failed_get_resource:
  273. + kfree(priv);
  274. +failed_inst_alloc:
  275. + dev_err(&pdev->dev, "could not load rpi_gpiomem");
  276. + return err;
  277. +}
  278. +
  279. +static int rpi_gpiomem_remove(struct platform_device *pdev)
  280. +{
  281. + struct device *dev = &pdev->dev;
  282. + struct rpi_gpiomem_priv *priv = platform_get_drvdata(pdev);
  283. +
  284. + device_destroy(priv->class, priv->devid);
  285. + class_destroy(priv->class);
  286. + cdev_del(&priv->rpi_gpiomem_cdev);
  287. + unregister_chrdev_region(priv->devid, 1);
  288. + kfree(priv);
  289. +
  290. + dev_info(dev, "%s driver removed - OK", priv->name);
  291. + return 0;
  292. +}
  293. +
  294. +static const struct of_device_id rpi_gpiomem_of_match[] = {
  295. + {
  296. + .compatible = "brcm,bcm2835-gpiomem",
  297. + },
  298. + {
  299. + .compatible = "raspberrypi,gpiomem",
  300. + },
  301. + { /* sentinel */ },
  302. +};
  303. +
  304. +MODULE_DEVICE_TABLE(of, rpi_gpiomem_of_match);
  305. +
  306. +static struct platform_driver rpi_gpiomem_driver = {
  307. + .probe = rpi_gpiomem_probe,
  308. + .remove = rpi_gpiomem_remove,
  309. + .driver = {
  310. + .name = DRIVER_NAME,
  311. + .owner = THIS_MODULE,
  312. + .of_match_table = rpi_gpiomem_of_match,
  313. + },
  314. +};
  315. +
  316. +module_platform_driver(rpi_gpiomem_driver);
  317. +
  318. +MODULE_ALIAS("platform:rpi-gpiomem");
  319. +MODULE_LICENSE("Dual BSD/GPL");
  320. +MODULE_DESCRIPTION("Driver for accessing GPIOs from userspace");
  321. +MODULE_AUTHOR("Jonathan Bell <[email protected]>");