root/drivers/gpio/gpio-madera.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. madera_gpio_get_direction
  2. madera_gpio_direction_in
  3. madera_gpio_get
  4. madera_gpio_direction_out
  5. madera_gpio_set
  6. madera_gpio_probe

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * GPIO support for Cirrus Logic Madera codecs
   4  *
   5  * Copyright (C) 2015-2018 Cirrus Logic
   6  */
   7 
   8 #include <linux/gpio/driver.h>
   9 #include <linux/kernel.h>
  10 #include <linux/module.h>
  11 #include <linux/platform_device.h>
  12 
  13 #include <linux/mfd/madera/core.h>
  14 #include <linux/mfd/madera/pdata.h>
  15 #include <linux/mfd/madera/registers.h>
  16 
  17 struct madera_gpio {
  18         struct madera *madera;
  19         /* storage space for the gpio_chip we're using */
  20         struct gpio_chip gpio_chip;
  21 };
  22 
  23 static int madera_gpio_get_direction(struct gpio_chip *chip,
  24                                      unsigned int offset)
  25 {
  26         struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
  27         struct madera *madera = madera_gpio->madera;
  28         unsigned int reg_offset = 2 * offset;
  29         unsigned int val;
  30         int ret;
  31 
  32         ret = regmap_read(madera->regmap, MADERA_GPIO1_CTRL_2 + reg_offset,
  33                           &val);
  34         if (ret < 0)
  35                 return ret;
  36 
  37         return !!(val & MADERA_GP1_DIR_MASK);
  38 }
  39 
  40 static int madera_gpio_direction_in(struct gpio_chip *chip, unsigned int offset)
  41 {
  42         struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
  43         struct madera *madera = madera_gpio->madera;
  44         unsigned int reg_offset = 2 * offset;
  45 
  46         return regmap_update_bits(madera->regmap,
  47                                   MADERA_GPIO1_CTRL_2 + reg_offset,
  48                                   MADERA_GP1_DIR_MASK, MADERA_GP1_DIR);
  49 }
  50 
  51 static int madera_gpio_get(struct gpio_chip *chip, unsigned int offset)
  52 {
  53         struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
  54         struct madera *madera = madera_gpio->madera;
  55         unsigned int reg_offset = 2 * offset;
  56         unsigned int val;
  57         int ret;
  58 
  59         ret = regmap_read(madera->regmap, MADERA_GPIO1_CTRL_1 + reg_offset,
  60                           &val);
  61         if (ret < 0)
  62                 return ret;
  63 
  64         return !!(val & MADERA_GP1_LVL_MASK);
  65 }
  66 
  67 static int madera_gpio_direction_out(struct gpio_chip *chip,
  68                                      unsigned int offset, int value)
  69 {
  70         struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
  71         struct madera *madera = madera_gpio->madera;
  72         unsigned int reg_offset = 2 * offset;
  73         unsigned int reg_val = value ? MADERA_GP1_LVL : 0;
  74         int ret;
  75 
  76         ret = regmap_update_bits(madera->regmap,
  77                                  MADERA_GPIO1_CTRL_2 + reg_offset,
  78                                  MADERA_GP1_DIR_MASK, 0);
  79         if (ret < 0)
  80                 return ret;
  81 
  82         return regmap_update_bits(madera->regmap,
  83                                   MADERA_GPIO1_CTRL_1 + reg_offset,
  84                                   MADERA_GP1_LVL_MASK, reg_val);
  85 }
  86 
  87 static void madera_gpio_set(struct gpio_chip *chip, unsigned int offset,
  88                             int value)
  89 {
  90         struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
  91         struct madera *madera = madera_gpio->madera;
  92         unsigned int reg_offset = 2 * offset;
  93         unsigned int reg_val = value ? MADERA_GP1_LVL : 0;
  94         int ret;
  95 
  96         ret = regmap_update_bits(madera->regmap,
  97                                  MADERA_GPIO1_CTRL_1 + reg_offset,
  98                                  MADERA_GP1_LVL_MASK, reg_val);
  99 
 100         /* set() doesn't return an error so log a warning */
 101         if (ret)
 102                 dev_warn(madera->dev, "Failed to write to 0x%x (%d)\n",
 103                          MADERA_GPIO1_CTRL_1 + reg_offset, ret);
 104 }
 105 
 106 static const struct gpio_chip madera_gpio_chip = {
 107         .label                  = "madera",
 108         .owner                  = THIS_MODULE,
 109         .request                = gpiochip_generic_request,
 110         .free                   = gpiochip_generic_free,
 111         .get_direction          = madera_gpio_get_direction,
 112         .direction_input        = madera_gpio_direction_in,
 113         .get                    = madera_gpio_get,
 114         .direction_output       = madera_gpio_direction_out,
 115         .set                    = madera_gpio_set,
 116         .set_config             = gpiochip_generic_config,
 117         .can_sleep              = true,
 118 };
 119 
 120 static int madera_gpio_probe(struct platform_device *pdev)
 121 {
 122         struct madera *madera = dev_get_drvdata(pdev->dev.parent);
 123         struct madera_pdata *pdata = &madera->pdata;
 124         struct madera_gpio *madera_gpio;
 125         int ret;
 126 
 127         madera_gpio = devm_kzalloc(&pdev->dev, sizeof(*madera_gpio),
 128                                    GFP_KERNEL);
 129         if (!madera_gpio)
 130                 return -ENOMEM;
 131 
 132         madera_gpio->madera = madera;
 133 
 134         /* Construct suitable gpio_chip from the template in madera_gpio_chip */
 135         madera_gpio->gpio_chip = madera_gpio_chip;
 136         madera_gpio->gpio_chip.parent = pdev->dev.parent;
 137 
 138         switch (madera->type) {
 139         case CS47L15:
 140                 madera_gpio->gpio_chip.ngpio = CS47L15_NUM_GPIOS;
 141                 break;
 142         case CS47L35:
 143                 madera_gpio->gpio_chip.ngpio = CS47L35_NUM_GPIOS;
 144                 break;
 145         case CS47L85:
 146         case WM1840:
 147                 madera_gpio->gpio_chip.ngpio = CS47L85_NUM_GPIOS;
 148                 break;
 149         case CS47L90:
 150         case CS47L91:
 151                 madera_gpio->gpio_chip.ngpio = CS47L90_NUM_GPIOS;
 152                 break;
 153         case CS42L92:
 154         case CS47L92:
 155         case CS47L93:
 156                 madera_gpio->gpio_chip.ngpio = CS47L92_NUM_GPIOS;
 157                 break;
 158         default:
 159                 dev_err(&pdev->dev, "Unknown chip variant %d\n", madera->type);
 160                 return -EINVAL;
 161         }
 162 
 163         /* We want to be usable on systems that don't use devicetree or acpi */
 164         if (pdata->gpio_base)
 165                 madera_gpio->gpio_chip.base = pdata->gpio_base;
 166         else
 167                 madera_gpio->gpio_chip.base = -1;
 168 
 169         ret = devm_gpiochip_add_data(&pdev->dev,
 170                                      &madera_gpio->gpio_chip,
 171                                      madera_gpio);
 172         if (ret < 0) {
 173                 dev_dbg(&pdev->dev, "Could not register gpiochip, %d\n", ret);
 174                 return ret;
 175         }
 176 
 177         /*
 178          * This is part of a composite MFD device which can only be used with
 179          * the corresponding pinctrl driver. On all supported silicon the GPIO
 180          * to pinctrl mapping is fixed in the silicon, so we register it
 181          * explicitly instead of requiring a redundant gpio-ranges in the
 182          * devicetree.
 183          * In any case we also want to work on systems that don't use devicetree
 184          * or acpi.
 185          */
 186         ret = gpiochip_add_pin_range(&madera_gpio->gpio_chip, "madera-pinctrl",
 187                                      0, 0, madera_gpio->gpio_chip.ngpio);
 188         if (ret) {
 189                 dev_dbg(&pdev->dev, "Failed to add pin range (%d)\n", ret);
 190                 return ret;
 191         }
 192 
 193         return 0;
 194 }
 195 
 196 static struct platform_driver madera_gpio_driver = {
 197         .driver = {
 198                 .name   = "madera-gpio",
 199         },
 200         .probe          = madera_gpio_probe,
 201 };
 202 
 203 module_platform_driver(madera_gpio_driver);
 204 
 205 MODULE_SOFTDEP("pre: pinctrl-madera");
 206 MODULE_DESCRIPTION("GPIO interface for Madera codecs");
 207 MODULE_AUTHOR("Nariman Poushin <nariman@opensource.cirrus.com>");
 208 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
 209 MODULE_LICENSE("GPL v2");
 210 MODULE_ALIAS("platform:madera-gpio");

/* [<][>][^][v][top][bottom][index][help] */