Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * gpiolib support for Wolfson WM831x PMICs
0004  *
0005  * Copyright 2009 Wolfson Microelectronics PLC.
0006  *
0007  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0008  *
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/slab.h>
0013 #include <linux/module.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/mfd/core.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/seq_file.h>
0018 
0019 #include <linux/mfd/wm831x/core.h>
0020 #include <linux/mfd/wm831x/pdata.h>
0021 #include <linux/mfd/wm831x/gpio.h>
0022 #include <linux/mfd/wm831x/irq.h>
0023 
0024 struct wm831x_gpio {
0025     struct wm831x *wm831x;
0026     struct gpio_chip gpio_chip;
0027 };
0028 
0029 static int wm831x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
0030 {
0031     struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
0032     struct wm831x *wm831x = wm831x_gpio->wm831x;
0033     int val = WM831X_GPN_DIR;
0034 
0035     if (wm831x->has_gpio_ena)
0036         val |= WM831X_GPN_TRI;
0037 
0038     return wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
0039                    WM831X_GPN_DIR | WM831X_GPN_TRI |
0040                    WM831X_GPN_FN_MASK, val);
0041 }
0042 
0043 static int wm831x_gpio_get(struct gpio_chip *chip, unsigned offset)
0044 {
0045     struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
0046     struct wm831x *wm831x = wm831x_gpio->wm831x;
0047     int ret;
0048 
0049     ret = wm831x_reg_read(wm831x, WM831X_GPIO_LEVEL);
0050     if (ret < 0)
0051         return ret;
0052 
0053     if (ret & 1 << offset)
0054         return 1;
0055     else
0056         return 0;
0057 }
0058 
0059 static void wm831x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
0060 {
0061     struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
0062     struct wm831x *wm831x = wm831x_gpio->wm831x;
0063 
0064     wm831x_set_bits(wm831x, WM831X_GPIO_LEVEL, 1 << offset,
0065             value << offset);
0066 }
0067 
0068 static int wm831x_gpio_direction_out(struct gpio_chip *chip,
0069                      unsigned offset, int value)
0070 {
0071     struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
0072     struct wm831x *wm831x = wm831x_gpio->wm831x;
0073     int val = 0;
0074     int ret;
0075 
0076     if (wm831x->has_gpio_ena)
0077         val |= WM831X_GPN_TRI;
0078 
0079     ret = wm831x_set_bits(wm831x, WM831X_GPIO1_CONTROL + offset,
0080                   WM831X_GPN_DIR | WM831X_GPN_TRI |
0081                   WM831X_GPN_FN_MASK, val);
0082     if (ret < 0)
0083         return ret;
0084 
0085     /* Can only set GPIO state once it's in output mode */
0086     wm831x_gpio_set(chip, offset, value);
0087 
0088     return 0;
0089 }
0090 
0091 static int wm831x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
0092 {
0093     struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
0094     struct wm831x *wm831x = wm831x_gpio->wm831x;
0095 
0096     return irq_create_mapping(wm831x->irq_domain,
0097                   WM831X_IRQ_GPIO_1 + offset);
0098 }
0099 
0100 static int wm831x_gpio_set_debounce(struct wm831x *wm831x, unsigned offset,
0101                     unsigned debounce)
0102 {
0103     int reg = WM831X_GPIO1_CONTROL + offset;
0104     int ret, fn;
0105 
0106     ret = wm831x_reg_read(wm831x, reg);
0107     if (ret < 0)
0108         return ret;
0109 
0110     switch (ret & WM831X_GPN_FN_MASK) {
0111     case 0:
0112     case 1:
0113         break;
0114     default:
0115         /* Not in GPIO mode */
0116         return -EBUSY;
0117     }
0118 
0119     if (debounce >= 32 && debounce <= 64)
0120         fn = 0;
0121     else if (debounce >= 4000 && debounce <= 8000)
0122         fn = 1;
0123     else
0124         return -EINVAL;
0125 
0126     return wm831x_set_bits(wm831x, reg, WM831X_GPN_FN_MASK, fn);
0127 }
0128 
0129 static int wm831x_set_config(struct gpio_chip *chip, unsigned int offset,
0130                  unsigned long config)
0131 {
0132     struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
0133     struct wm831x *wm831x = wm831x_gpio->wm831x;
0134     int reg = WM831X_GPIO1_CONTROL + offset;
0135 
0136     switch (pinconf_to_config_param(config)) {
0137     case PIN_CONFIG_DRIVE_OPEN_DRAIN:
0138         return wm831x_set_bits(wm831x, reg,
0139                        WM831X_GPN_OD_MASK, WM831X_GPN_OD);
0140     case PIN_CONFIG_DRIVE_PUSH_PULL:
0141         return wm831x_set_bits(wm831x, reg,
0142                        WM831X_GPN_OD_MASK, 0);
0143     case PIN_CONFIG_INPUT_DEBOUNCE:
0144         return wm831x_gpio_set_debounce(wm831x, offset,
0145             pinconf_to_config_argument(config));
0146     default:
0147         break;
0148     }
0149 
0150     return -ENOTSUPP;
0151 }
0152 
0153 #ifdef CONFIG_DEBUG_FS
0154 static void wm831x_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
0155 {
0156     struct wm831x_gpio *wm831x_gpio = gpiochip_get_data(chip);
0157     struct wm831x *wm831x = wm831x_gpio->wm831x;
0158     int i, tristated;
0159 
0160     for (i = 0; i < chip->ngpio; i++) {
0161         int gpio = i + chip->base;
0162         int reg;
0163         const char *label, *pull, *powerdomain;
0164 
0165         /* We report the GPIO even if it's not requested since
0166          * we're also reporting things like alternate
0167          * functions which apply even when the GPIO is not in
0168          * use as a GPIO.
0169          */
0170         label = gpiochip_is_requested(chip, i);
0171         if (!label)
0172             label = "Unrequested";
0173 
0174         seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label);
0175 
0176         reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i);
0177         if (reg < 0) {
0178             dev_err(wm831x->dev,
0179                 "GPIO control %d read failed: %d\n",
0180                 gpio, reg);
0181             seq_putc(s, '\n');
0182             continue;
0183         }
0184 
0185         switch (reg & WM831X_GPN_PULL_MASK) {
0186         case WM831X_GPIO_PULL_NONE:
0187             pull = "nopull";
0188             break;
0189         case WM831X_GPIO_PULL_DOWN:
0190             pull = "pulldown";
0191             break;
0192         case WM831X_GPIO_PULL_UP:
0193             pull = "pullup";
0194             break;
0195         default:
0196             pull = "INVALID PULL";
0197             break;
0198         }
0199 
0200         switch (i + 1) {
0201         case 1 ... 3:
0202         case 7 ... 9:
0203             if (reg & WM831X_GPN_PWR_DOM)
0204                 powerdomain = "VPMIC";
0205             else
0206                 powerdomain = "DBVDD";
0207             break;
0208 
0209         case 4 ... 6:
0210         case 10 ... 12:
0211             if (reg & WM831X_GPN_PWR_DOM)
0212                 powerdomain = "SYSVDD";
0213             else
0214                 powerdomain = "DBVDD";
0215             break;
0216 
0217         case 13 ... 16:
0218             powerdomain = "TPVDD";
0219             break;
0220 
0221         default:
0222             BUG();
0223             break;
0224         }
0225 
0226         tristated = reg & WM831X_GPN_TRI;
0227         if (wm831x->has_gpio_ena)
0228             tristated = !tristated;
0229 
0230         seq_printf(s, " %s %s %s %s%s\n"
0231                "                                  %s%s (0x%4x)\n",
0232                reg & WM831X_GPN_DIR ? "in" : "out",
0233                wm831x_gpio_get(chip, i) ? "high" : "low",
0234                pull,
0235                powerdomain,
0236                reg & WM831X_GPN_POL ? "" : " inverted",
0237                reg & WM831X_GPN_OD ? "open-drain" : "push-pull",
0238                tristated ? " tristated" : "",
0239                reg);
0240     }
0241 }
0242 #else
0243 #define wm831x_gpio_dbg_show NULL
0244 #endif
0245 
0246 static const struct gpio_chip template_chip = {
0247     .label          = "wm831x",
0248     .owner          = THIS_MODULE,
0249     .direction_input    = wm831x_gpio_direction_in,
0250     .get            = wm831x_gpio_get,
0251     .direction_output   = wm831x_gpio_direction_out,
0252     .set            = wm831x_gpio_set,
0253     .to_irq         = wm831x_gpio_to_irq,
0254     .set_config     = wm831x_set_config,
0255     .dbg_show       = wm831x_gpio_dbg_show,
0256     .can_sleep      = true,
0257 };
0258 
0259 static int wm831x_gpio_probe(struct platform_device *pdev)
0260 {
0261     struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
0262     struct wm831x_pdata *pdata = &wm831x->pdata;
0263     struct wm831x_gpio *wm831x_gpio;
0264 
0265     device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
0266 
0267     wm831x_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm831x_gpio),
0268                    GFP_KERNEL);
0269     if (wm831x_gpio == NULL)
0270         return -ENOMEM;
0271 
0272     wm831x_gpio->wm831x = wm831x;
0273     wm831x_gpio->gpio_chip = template_chip;
0274     wm831x_gpio->gpio_chip.ngpio = wm831x->num_gpio;
0275     wm831x_gpio->gpio_chip.parent = &pdev->dev;
0276     if (pdata && pdata->gpio_base)
0277         wm831x_gpio->gpio_chip.base = pdata->gpio_base;
0278     else
0279         wm831x_gpio->gpio_chip.base = -1;
0280 
0281     return devm_gpiochip_add_data(&pdev->dev, &wm831x_gpio->gpio_chip, wm831x_gpio);
0282 }
0283 
0284 static struct platform_driver wm831x_gpio_driver = {
0285     .driver.name    = "wm831x-gpio",
0286     .probe      = wm831x_gpio_probe,
0287 };
0288 
0289 static int __init wm831x_gpio_init(void)
0290 {
0291     return platform_driver_register(&wm831x_gpio_driver);
0292 }
0293 subsys_initcall(wm831x_gpio_init);
0294 
0295 static void __exit wm831x_gpio_exit(void)
0296 {
0297     platform_driver_unregister(&wm831x_gpio_driver);
0298 }
0299 module_exit(wm831x_gpio_exit);
0300 
0301 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
0302 MODULE_DESCRIPTION("GPIO interface for WM831x PMICs");
0303 MODULE_LICENSE("GPL");
0304 MODULE_ALIAS("platform:wm831x-gpio");