0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kernel.h>
0012 #include <linux/init.h>
0013 #include <linux/errno.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/i2c.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/mfd/tps65910.h>
0018 #include <linux/of_device.h>
0019
0020 struct tps65910_gpio {
0021 struct gpio_chip gpio_chip;
0022 struct tps65910 *tps65910;
0023 };
0024
0025 static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
0026 {
0027 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
0028 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
0029 unsigned int val;
0030
0031 regmap_read(tps65910->regmap, TPS65910_GPIO0 + offset, &val);
0032
0033 if (val & GPIO_STS_MASK)
0034 return 1;
0035
0036 return 0;
0037 }
0038
0039 static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
0040 int value)
0041 {
0042 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
0043 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
0044
0045 if (value)
0046 regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
0047 GPIO_SET_MASK);
0048 else
0049 regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
0050 GPIO_SET_MASK);
0051 }
0052
0053 static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
0054 int value)
0055 {
0056 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
0057 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
0058
0059
0060 tps65910_gpio_set(gc, offset, value);
0061
0062 return regmap_set_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
0063 GPIO_CFG_MASK);
0064 }
0065
0066 static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
0067 {
0068 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
0069 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
0070
0071 return regmap_clear_bits(tps65910->regmap, TPS65910_GPIO0 + offset,
0072 GPIO_CFG_MASK);
0073 }
0074
0075 #ifdef CONFIG_OF
0076 static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
0077 struct tps65910 *tps65910, int chip_ngpio)
0078 {
0079 struct tps65910_board *tps65910_board = tps65910->of_plat_data;
0080 unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
0081 int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
0082 int ret;
0083 int idx;
0084
0085 tps65910_board->gpio_base = -1;
0086 ret = of_property_read_u32_array(tps65910->dev->of_node,
0087 "ti,en-gpio-sleep", prop_array, ngpio);
0088 if (ret < 0) {
0089 dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
0090 return tps65910_board;
0091 }
0092
0093 for (idx = 0; idx < ngpio; idx++)
0094 tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
0095
0096 return tps65910_board;
0097 }
0098 #else
0099 static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
0100 struct tps65910 *tps65910, int chip_ngpio)
0101 {
0102 return NULL;
0103 }
0104 #endif
0105
0106 static int tps65910_gpio_probe(struct platform_device *pdev)
0107 {
0108 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
0109 struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
0110 struct tps65910_gpio *tps65910_gpio;
0111 int ret;
0112 int i;
0113
0114 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
0115
0116 tps65910_gpio = devm_kzalloc(&pdev->dev,
0117 sizeof(*tps65910_gpio), GFP_KERNEL);
0118 if (!tps65910_gpio)
0119 return -ENOMEM;
0120
0121 tps65910_gpio->tps65910 = tps65910;
0122
0123 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
0124 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
0125
0126 switch (tps65910_chip_id(tps65910)) {
0127 case TPS65910:
0128 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
0129 break;
0130 case TPS65911:
0131 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
0132 break;
0133 default:
0134 return -EINVAL;
0135 }
0136 tps65910_gpio->gpio_chip.can_sleep = true;
0137 tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
0138 tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
0139 tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
0140 tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
0141 tps65910_gpio->gpio_chip.parent = &pdev->dev;
0142
0143 if (pdata && pdata->gpio_base)
0144 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
0145 else
0146 tps65910_gpio->gpio_chip.base = -1;
0147
0148 if (!pdata && tps65910->dev->of_node)
0149 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
0150 tps65910_gpio->gpio_chip.ngpio);
0151
0152 if (!pdata)
0153 goto skip_init;
0154
0155
0156 for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
0157 if (!pdata->en_gpio_sleep[i])
0158 continue;
0159
0160 ret = regmap_set_bits(tps65910->regmap,
0161 TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
0162 if (ret < 0)
0163 dev_warn(tps65910->dev,
0164 "GPIO Sleep setting failed with err %d\n", ret);
0165 }
0166
0167 skip_init:
0168 return devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
0169 tps65910_gpio);
0170 }
0171
0172 static struct platform_driver tps65910_gpio_driver = {
0173 .driver.name = "tps65910-gpio",
0174 .probe = tps65910_gpio_probe,
0175 };
0176
0177 static int __init tps65910_gpio_init(void)
0178 {
0179 return platform_driver_register(&tps65910_gpio_driver);
0180 }
0181 subsys_initcall(tps65910_gpio_init);