LCOV - code coverage report
Current view: top level - drivers/gpio - gpio-bcm-virt.c (source / functions) Hit Total Coverage
Test: Real Lines: 1 85 1.2 %
Date: 2020-10-17 15:46:16 Functions: 0 8 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       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                 :          0 : 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                 :          0 :         return -EINVAL;
      37                 :            : }
      38                 :            : 
      39                 :          0 : 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                 :          0 :         return 0;
      44                 :            : }
      45                 :            : 
      46                 :          0 : 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                 :          0 :         v = readl(gpio->ts_base + off);
      52                 :          0 :         return (v >> off) & 1;
      53                 :            : }
      54                 :            : 
      55                 :          0 : 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                 :          0 :         enables  = gpio->enables_disables[off] >> 16;
      63                 :          0 :         disables = gpio->enables_disables[off] >>  0;
      64                 :          0 :         diff = (s16)(enables - disables);
      65                 :            :         lit = diff > 0;
      66                 :          0 :         if ((val && lit) || (!val && !lit))
      67                 :          0 :                 return;
      68                 :          0 :         if (val)
      69                 :          0 :                 enables++;
      70                 :            :         else
      71                 :          0 :                 disables++;
      72                 :          0 :         diff = (s16)(enables - disables);
      73                 :          0 :         BUG_ON(diff != 0 && diff != 1);
      74                 :          0 :         gpio->enables_disables[off] = (enables << 16) | (disables << 0);
      75                 :          0 :         writel(gpio->enables_disables[off], gpio->ts_base + off);
      76                 :            : }
      77                 :            : 
      78                 :          0 : static int brcmvirt_gpio_probe(struct platform_device *pdev)
      79                 :            : {
      80                 :            :         int err = 0;
      81                 :          0 :         struct device *dev = &pdev->dev;
      82                 :          0 :         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                 :          0 :         fw_node = of_parse_phandle(np, "firmware", 0);
      89                 :          0 :         if (!fw_node) {
      90                 :          0 :                 dev_err(dev, "Missing firmware node\n");
      91                 :          0 :                 return -ENOENT;
      92                 :            :         }
      93                 :            : 
      94                 :          0 :         fw = rpi_firmware_get(fw_node);
      95                 :          0 :         if (!fw)
      96                 :            :                 return -EPROBE_DEFER;
      97                 :            : 
      98                 :            :         ucb = devm_kzalloc(dev, sizeof *ucb, GFP_KERNEL);
      99                 :          0 :         if (!ucb) {
     100                 :            :                 err = -EINVAL;
     101                 :            :                 goto out;
     102                 :            :         }
     103                 :            : 
     104                 :          0 :         ucb->ts_base = dma_alloc_coherent(dev, PAGE_SIZE, &ucb->bus_addr, GFP_KERNEL);
     105                 :          0 :         if (!ucb->ts_base) {
     106                 :          0 :                 pr_err("[%s]: failed to dma_alloc_coherent(%ld)\n",
     107                 :            :                                 __func__, PAGE_SIZE);
     108                 :            :                 err = -ENOMEM;
     109                 :          0 :                 goto out;
     110                 :            :         }
     111                 :            : 
     112                 :          0 :         gpiovirtbuf = (u32)ucb->bus_addr;
     113                 :          0 :         err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF,
     114                 :            :                                     &gpiovirtbuf, sizeof(gpiovirtbuf));
     115                 :            : 
     116                 :          0 :         if (err || gpiovirtbuf != 0) {
     117                 :          0 :                 dev_warn(dev, "Failed to set gpiovirtbuf, trying to get err:%x\n", err);
     118                 :          0 :                 dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
     119                 :          0 :                 ucb->ts_base = 0;
     120                 :          0 :                 ucb->bus_addr = 0;
     121                 :            :         }
     122                 :            : 
     123                 :          0 :         if (!ucb->ts_base) {
     124                 :          0 :                 err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF,
     125                 :            :                                             &gpiovirtbuf, sizeof(gpiovirtbuf));
     126                 :            : 
     127                 :          0 :                 if (err) {
     128                 :          0 :                         dev_err(dev, "Failed to get gpiovirtbuf\n");
     129                 :          0 :                         goto out;
     130                 :            :                 }
     131                 :            : 
     132                 :          0 :                 if (!gpiovirtbuf) {
     133                 :          0 :                         dev_err(dev, "No virtgpio buffer\n");
     134                 :            :                         err = -ENOENT;
     135                 :          0 :                         goto out;
     136                 :            :                 }
     137                 :            : 
     138                 :            :                 // mmap the physical memory
     139                 :          0 :                 gpiovirtbuf &= ~0xc0000000;
     140                 :          0 :                 ucb->ts_base = ioremap(gpiovirtbuf, 4096);
     141                 :          0 :                 if (ucb->ts_base == NULL) {
     142                 :          0 :                         dev_err(dev, "Failed to map physical address\n");
     143                 :            :                         err = -ENOENT;
     144                 :          0 :                         goto out;
     145                 :            :                 }
     146                 :          0 :                 ucb->bus_addr = 0;
     147                 :            :         }
     148                 :          0 :         ucb->gc.label = MODULE_NAME;
     149                 :          0 :         ucb->gc.owner = THIS_MODULE;
     150                 :            :         //ucb->gc.dev = dev;
     151                 :          0 :         ucb->gc.of_node = np;
     152                 :          0 :         ucb->gc.base = 100;
     153                 :          0 :         ucb->gc.ngpio = NUM_GPIO;
     154                 :            : 
     155                 :          0 :         ucb->gc.direction_input = brcmvirt_gpio_dir_in;
     156                 :          0 :         ucb->gc.direction_output = brcmvirt_gpio_dir_out;
     157                 :          0 :         ucb->gc.get = brcmvirt_gpio_get;
     158                 :          0 :         ucb->gc.set = brcmvirt_gpio_set;
     159                 :          0 :         ucb->gc.can_sleep = true;
     160                 :            : 
     161                 :          0 :         err = gpiochip_add(&ucb->gc);
     162                 :          0 :         if (err)
     163                 :            :                 goto out;
     164                 :            : 
     165                 :            :         platform_set_drvdata(pdev, ucb);
     166                 :            : 
     167                 :          0 :         return 0;
     168                 :            : out:
     169                 :          0 :         if (ucb->bus_addr) {
     170                 :          0 :                 dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
     171                 :          0 :                 ucb->bus_addr = 0;
     172                 :          0 :                 ucb->ts_base = NULL;
     173                 :          0 :         } else if (ucb->ts_base) {
     174                 :          0 :                 iounmap(ucb->ts_base);
     175                 :          0 :                 ucb->ts_base = NULL;
     176                 :            :         }
     177                 :          0 :         return err;
     178                 :            : }
     179                 :            : 
     180                 :          0 : static int brcmvirt_gpio_remove(struct platform_device *pdev)
     181                 :            : {
     182                 :          0 :         struct device *dev = &pdev->dev;
     183                 :            :         int err = 0;
     184                 :            :         struct brcmvirt_gpio *ucb = platform_get_drvdata(pdev);
     185                 :            : 
     186                 :          0 :         gpiochip_remove(&ucb->gc);
     187                 :          0 :         if (ucb->bus_addr)
     188                 :          0 :                 dma_free_coherent(dev, PAGE_SIZE, ucb->ts_base, ucb->bus_addr);
     189                 :          0 :         else if (ucb->ts_base)
     190                 :          0 :                 iounmap(ucb->ts_base);
     191                 :          0 :         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                 :          3 : 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");
    

Generated by: LCOV version 1.14