Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
0004  *  Andrew F. Davis <afd@ti.com>
0005  *
0006  * Based on the TPS65912 driver
0007  */
0008 
0009 #include <linux/gpio/driver.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 
0013 #include <linux/mfd/tps65086.h>
0014 
0015 struct tps65086_gpio {
0016     struct gpio_chip chip;
0017     struct tps65086 *tps;
0018 };
0019 
0020 static int tps65086_gpio_get_direction(struct gpio_chip *chip,
0021                        unsigned offset)
0022 {
0023     /* This device is output only */
0024     return GPIO_LINE_DIRECTION_OUT;
0025 }
0026 
0027 static int tps65086_gpio_direction_input(struct gpio_chip *chip,
0028                      unsigned offset)
0029 {
0030     /* This device is output only */
0031     return -EINVAL;
0032 }
0033 
0034 static int tps65086_gpio_direction_output(struct gpio_chip *chip,
0035                       unsigned offset, int value)
0036 {
0037     struct tps65086_gpio *gpio = gpiochip_get_data(chip);
0038 
0039     /* Set the initial value */
0040     regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
0041                BIT(4 + offset), value ? BIT(4 + offset) : 0);
0042 
0043     return 0;
0044 }
0045 
0046 static int tps65086_gpio_get(struct gpio_chip *chip, unsigned offset)
0047 {
0048     struct tps65086_gpio *gpio = gpiochip_get_data(chip);
0049     int ret, val;
0050 
0051     ret = regmap_read(gpio->tps->regmap, TPS65086_GPOCTRL, &val);
0052     if (ret < 0)
0053         return ret;
0054 
0055     return val & BIT(4 + offset);
0056 }
0057 
0058 static void tps65086_gpio_set(struct gpio_chip *chip, unsigned offset,
0059                   int value)
0060 {
0061     struct tps65086_gpio *gpio = gpiochip_get_data(chip);
0062 
0063     regmap_update_bits(gpio->tps->regmap, TPS65086_GPOCTRL,
0064                BIT(4 + offset), value ? BIT(4 + offset) : 0);
0065 }
0066 
0067 static const struct gpio_chip template_chip = {
0068     .label          = "tps65086-gpio",
0069     .owner          = THIS_MODULE,
0070     .get_direction      = tps65086_gpio_get_direction,
0071     .direction_input    = tps65086_gpio_direction_input,
0072     .direction_output   = tps65086_gpio_direction_output,
0073     .get            = tps65086_gpio_get,
0074     .set            = tps65086_gpio_set,
0075     .base           = -1,
0076     .ngpio          = 4,
0077     .can_sleep      = true,
0078 };
0079 
0080 static int tps65086_gpio_probe(struct platform_device *pdev)
0081 {
0082     struct tps65086_gpio *gpio;
0083     int ret;
0084 
0085     gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
0086     if (!gpio)
0087         return -ENOMEM;
0088 
0089     platform_set_drvdata(pdev, gpio);
0090 
0091     gpio->tps = dev_get_drvdata(pdev->dev.parent);
0092     gpio->chip = template_chip;
0093     gpio->chip.parent = gpio->tps->dev;
0094 
0095     ret = gpiochip_add_data(&gpio->chip, gpio);
0096     if (ret < 0) {
0097         dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
0098         return ret;
0099     }
0100 
0101     return 0;
0102 }
0103 
0104 static int tps65086_gpio_remove(struct platform_device *pdev)
0105 {
0106     struct tps65086_gpio *gpio = platform_get_drvdata(pdev);
0107 
0108     gpiochip_remove(&gpio->chip);
0109 
0110     return 0;
0111 }
0112 
0113 static const struct platform_device_id tps65086_gpio_id_table[] = {
0114     { "tps65086-gpio", },
0115     { /* sentinel */ }
0116 };
0117 MODULE_DEVICE_TABLE(platform, tps65086_gpio_id_table);
0118 
0119 static struct platform_driver tps65086_gpio_driver = {
0120     .driver = {
0121         .name = "tps65086-gpio",
0122     },
0123     .probe = tps65086_gpio_probe,
0124     .remove = tps65086_gpio_remove,
0125     .id_table = tps65086_gpio_id_table,
0126 };
0127 module_platform_driver(tps65086_gpio_driver);
0128 
0129 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
0130 MODULE_DESCRIPTION("TPS65086 GPIO driver");
0131 MODULE_LICENSE("GPL v2");