Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * gpiolib support for Wolfson WM835x 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/wm8350/core.h>
0020 #include <linux/mfd/wm8350/gpio.h>
0021 
0022 struct wm8350_gpio_data {
0023     struct wm8350 *wm8350;
0024     struct gpio_chip gpio_chip;
0025 };
0026 
0027 static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
0028 {
0029     struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
0030     struct wm8350 *wm8350 = wm8350_gpio->wm8350;
0031 
0032     return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
0033                    1 << offset);
0034 }
0035 
0036 static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
0037 {
0038     struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
0039     struct wm8350 *wm8350 = wm8350_gpio->wm8350;
0040     int ret;
0041 
0042     ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
0043     if (ret < 0)
0044         return ret;
0045 
0046     if (ret & (1 << offset))
0047         return 1;
0048     else
0049         return 0;
0050 }
0051 
0052 static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
0053 {
0054     struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
0055     struct wm8350 *wm8350 = wm8350_gpio->wm8350;
0056 
0057     if (value)
0058         wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
0059     else
0060         wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
0061 }
0062 
0063 static int wm8350_gpio_direction_out(struct gpio_chip *chip,
0064                      unsigned offset, int value)
0065 {
0066     struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
0067     struct wm8350 *wm8350 = wm8350_gpio->wm8350;
0068     int ret;
0069 
0070     ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
0071                 1 << offset);
0072     if (ret < 0)
0073         return ret;
0074 
0075     /* Don't have an atomic direction/value setup */
0076     wm8350_gpio_set(chip, offset, value);
0077 
0078     return 0;
0079 }
0080 
0081 static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
0082 {
0083     struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
0084     struct wm8350 *wm8350 = wm8350_gpio->wm8350;
0085 
0086     if (!wm8350->irq_base)
0087         return -EINVAL;
0088 
0089     return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
0090 }
0091 
0092 static const struct gpio_chip template_chip = {
0093     .label          = "wm8350",
0094     .owner          = THIS_MODULE,
0095     .direction_input    = wm8350_gpio_direction_in,
0096     .get            = wm8350_gpio_get,
0097     .direction_output   = wm8350_gpio_direction_out,
0098     .set            = wm8350_gpio_set,
0099     .to_irq         = wm8350_gpio_to_irq,
0100     .can_sleep      = true,
0101 };
0102 
0103 static int wm8350_gpio_probe(struct platform_device *pdev)
0104 {
0105     struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
0106     struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
0107     struct wm8350_gpio_data *wm8350_gpio;
0108 
0109     wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
0110                    GFP_KERNEL);
0111     if (wm8350_gpio == NULL)
0112         return -ENOMEM;
0113 
0114     wm8350_gpio->wm8350 = wm8350;
0115     wm8350_gpio->gpio_chip = template_chip;
0116     wm8350_gpio->gpio_chip.ngpio = 13;
0117     wm8350_gpio->gpio_chip.parent = &pdev->dev;
0118     if (pdata && pdata->gpio_base)
0119         wm8350_gpio->gpio_chip.base = pdata->gpio_base;
0120     else
0121         wm8350_gpio->gpio_chip.base = -1;
0122 
0123     return devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip, wm8350_gpio);
0124 }
0125 
0126 static struct platform_driver wm8350_gpio_driver = {
0127     .driver.name    = "wm8350-gpio",
0128     .probe      = wm8350_gpio_probe,
0129 };
0130 
0131 static int __init wm8350_gpio_init(void)
0132 {
0133     return platform_driver_register(&wm8350_gpio_driver);
0134 }
0135 subsys_initcall(wm8350_gpio_init);
0136 
0137 static void __exit wm8350_gpio_exit(void)
0138 {
0139     platform_driver_unregister(&wm8350_gpio_driver);
0140 }
0141 module_exit(wm8350_gpio_exit);
0142 
0143 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
0144 MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
0145 MODULE_LICENSE("GPL");
0146 MODULE_ALIAS("platform:wm8350-gpio");