Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
0004  *  Keerthy <j-keerthy@ti.com>
0005  *
0006  * Based on the TPS65218 driver
0007  */
0008 
0009 #include <linux/gpio/driver.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/regmap.h>
0013 
0014 #include <linux/mfd/lp873x.h>
0015 
0016 #define BITS_PER_GPO        0x4
0017 #define LP873X_GPO_CTRL_OD  0x2
0018 
0019 struct lp873x_gpio {
0020     struct gpio_chip chip;
0021     struct lp873x *lp873;
0022 };
0023 
0024 static int lp873x_gpio_get_direction(struct gpio_chip *chip,
0025                      unsigned int offset)
0026 {
0027     /* This device is output only */
0028     return GPIO_LINE_DIRECTION_OUT;
0029 }
0030 
0031 static int lp873x_gpio_direction_input(struct gpio_chip *chip,
0032                        unsigned int offset)
0033 {
0034     /* This device is output only */
0035     return -EINVAL;
0036 }
0037 
0038 static int lp873x_gpio_direction_output(struct gpio_chip *chip,
0039                     unsigned int offset, int value)
0040 {
0041     struct lp873x_gpio *gpio = gpiochip_get_data(chip);
0042 
0043     /* Set the initial value */
0044     return regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
0045                   BIT(offset * BITS_PER_GPO),
0046                   value ? BIT(offset * BITS_PER_GPO) : 0);
0047 }
0048 
0049 static int lp873x_gpio_get(struct gpio_chip *chip, unsigned int offset)
0050 {
0051     struct lp873x_gpio *gpio = gpiochip_get_data(chip);
0052     int ret, val;
0053 
0054     ret = regmap_read(gpio->lp873->regmap, LP873X_REG_GPO_CTRL, &val);
0055     if (ret < 0)
0056         return ret;
0057 
0058     return val & BIT(offset * BITS_PER_GPO);
0059 }
0060 
0061 static void lp873x_gpio_set(struct gpio_chip *chip, unsigned int offset,
0062                 int value)
0063 {
0064     struct lp873x_gpio *gpio = gpiochip_get_data(chip);
0065 
0066     regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
0067                BIT(offset * BITS_PER_GPO),
0068                value ? BIT(offset * BITS_PER_GPO) : 0);
0069 }
0070 
0071 static int lp873x_gpio_request(struct gpio_chip *gc, unsigned int offset)
0072 {
0073     struct lp873x_gpio *gpio = gpiochip_get_data(gc);
0074     int ret;
0075 
0076     switch (offset) {
0077     case 0:
0078         /* No MUX Set up Needed for GPO */
0079         break;
0080     case 1:
0081         /* Setup the CLKIN_PIN_SEL MUX to GPO2 */
0082         ret = regmap_update_bits(gpio->lp873->regmap, LP873X_REG_CONFIG,
0083                      LP873X_CONFIG_CLKIN_PIN_SEL, 0);
0084         if (ret)
0085             return ret;
0086 
0087         break;
0088     default:
0089         return -EINVAL;
0090     }
0091 
0092     return 0;
0093 }
0094 
0095 static int lp873x_gpio_set_config(struct gpio_chip *gc, unsigned offset,
0096                   unsigned long config)
0097 {
0098     struct lp873x_gpio *gpio = gpiochip_get_data(gc);
0099 
0100     switch (pinconf_to_config_param(config)) {
0101     case PIN_CONFIG_DRIVE_OPEN_DRAIN:
0102         return regmap_update_bits(gpio->lp873->regmap,
0103                       LP873X_REG_GPO_CTRL,
0104                       BIT(offset * BITS_PER_GPO +
0105                       LP873X_GPO_CTRL_OD),
0106                       BIT(offset * BITS_PER_GPO +
0107                       LP873X_GPO_CTRL_OD));
0108 
0109     case PIN_CONFIG_DRIVE_PUSH_PULL:
0110         return regmap_update_bits(gpio->lp873->regmap,
0111                       LP873X_REG_GPO_CTRL,
0112                       BIT(offset * BITS_PER_GPO +
0113                       LP873X_GPO_CTRL_OD), 0);
0114     default:
0115         return -ENOTSUPP;
0116     }
0117 }
0118 
0119 static const struct gpio_chip template_chip = {
0120     .label          = "lp873x-gpio",
0121     .owner          = THIS_MODULE,
0122     .request        = lp873x_gpio_request,
0123     .get_direction      = lp873x_gpio_get_direction,
0124     .direction_input    = lp873x_gpio_direction_input,
0125     .direction_output   = lp873x_gpio_direction_output,
0126     .get            = lp873x_gpio_get,
0127     .set            = lp873x_gpio_set,
0128     .set_config     = lp873x_gpio_set_config,
0129     .base           = -1,
0130     .ngpio          = 2,
0131     .can_sleep      = true,
0132 };
0133 
0134 static int lp873x_gpio_probe(struct platform_device *pdev)
0135 {
0136     struct lp873x_gpio *gpio;
0137     int ret;
0138 
0139     gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
0140     if (!gpio)
0141         return -ENOMEM;
0142 
0143     platform_set_drvdata(pdev, gpio);
0144 
0145     gpio->lp873 = dev_get_drvdata(pdev->dev.parent);
0146     gpio->chip = template_chip;
0147     gpio->chip.parent = gpio->lp873->dev;
0148 
0149     ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
0150     if (ret < 0) {
0151         dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
0152         return ret;
0153     }
0154 
0155     return 0;
0156 }
0157 
0158 static const struct platform_device_id lp873x_gpio_id_table[] = {
0159     { "lp873x-gpio", },
0160     { /* sentinel */ }
0161 };
0162 MODULE_DEVICE_TABLE(platform, lp873x_gpio_id_table);
0163 
0164 static struct platform_driver lp873x_gpio_driver = {
0165     .driver = {
0166         .name = "lp873x-gpio",
0167     },
0168     .probe = lp873x_gpio_probe,
0169     .id_table = lp873x_gpio_id_table,
0170 };
0171 module_platform_driver(lp873x_gpio_driver);
0172 
0173 MODULE_AUTHOR("Keerthy <j-keerthy@ti.com>");
0174 MODULE_DESCRIPTION("LP873X GPIO driver");
0175 MODULE_LICENSE("GPL v2");