blob: 3ad68bd78282590103a735c49430b6348276b803 [file] [log] [blame]
Linus Walleijd5a4da12018-08-29 16:49:14 +02001// SPDX-License-Identifier: GPL-2.0
Margarita Olaya668a6cc2011-06-09 14:50:19 -05002/*
Andrew F. Davisca801a22016-01-25 09:43:47 -06003 * GPIO driver for TI TPS65912x PMICs
Margarita Olaya668a6cc2011-06-09 14:50:19 -05004 *
Andrew F. Davisca801a22016-01-25 09:43:47 -06005 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
6 * Andrew F. Davis <afd@ti.com>
Margarita Olaya668a6cc2011-06-09 14:50:19 -05007 *
Andrew F. Davisca801a22016-01-25 09:43:47 -06008 * Based on the Arizona GPIO driver and the previous TPS65912 driver by
9 * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
Margarita Olaya668a6cc2011-06-09 14:50:19 -050010 */
11
Linus Walleij5d756832018-08-29 16:45:30 +020012#include <linux/gpio/driver.h>
Andrew F. Davisca801a22016-01-25 09:43:47 -060013#include <linux/module.h>
Margarita Olaya668a6cc2011-06-09 14:50:19 -050014#include <linux/platform_device.h>
Andrew F. Davisca801a22016-01-25 09:43:47 -060015
Margarita Olaya668a6cc2011-06-09 14:50:19 -050016#include <linux/mfd/tps65912.h>
17
Andrew F. Davisca801a22016-01-25 09:43:47 -060018struct tps65912_gpio {
Margarita Olaya668a6cc2011-06-09 14:50:19 -050019 struct gpio_chip gpio_chip;
Andrew F. Davisca801a22016-01-25 09:43:47 -060020 struct tps65912 *tps;
Margarita Olaya668a6cc2011-06-09 14:50:19 -050021};
22
Andrew F. Davisca801a22016-01-25 09:43:47 -060023static int tps65912_gpio_get_direction(struct gpio_chip *gc,
24 unsigned offset)
25{
26 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
27
28 int ret, val;
29
30 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
31 if (ret)
32 return ret;
33
34 if (val & GPIO_CFG_MASK)
Linus Walleij5d756832018-08-29 16:45:30 +020035 return 0;
Andrew F. Davisca801a22016-01-25 09:43:47 -060036 else
Linus Walleij5d756832018-08-29 16:45:30 +020037 return 1;
Andrew F. Davisca801a22016-01-25 09:43:47 -060038}
39
40static int tps65912_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
41{
42 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
43
44 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
45 GPIO_CFG_MASK, 0);
46}
47
48static int tps65912_gpio_direction_output(struct gpio_chip *gc,
49 unsigned offset, int value)
50{
51 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
52
53 /* Set the initial value */
54 regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
55 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
56
57 return regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
58 GPIO_CFG_MASK, GPIO_CFG_MASK);
59}
60
Margarita Olaya668a6cc2011-06-09 14:50:19 -050061static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
62{
Andrew F. Davisca801a22016-01-25 09:43:47 -060063 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
64 int ret, val;
Margarita Olaya668a6cc2011-06-09 14:50:19 -050065
Andrew F. Davisca801a22016-01-25 09:43:47 -060066 ret = regmap_read(gpio->tps->regmap, TPS65912_GPIO1 + offset, &val);
67 if (ret)
68 return ret;
Margarita Olaya668a6cc2011-06-09 14:50:19 -050069
70 if (val & GPIO_STS_MASK)
71 return 1;
72
73 return 0;
74}
75
76static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
77 int value)
78{
Andrew F. Davisca801a22016-01-25 09:43:47 -060079 struct tps65912_gpio *gpio = gpiochip_get_data(gc);
Margarita Olaya668a6cc2011-06-09 14:50:19 -050080
Andrew F. Davisca801a22016-01-25 09:43:47 -060081 regmap_update_bits(gpio->tps->regmap, TPS65912_GPIO1 + offset,
82 GPIO_SET_MASK, value ? GPIO_SET_MASK : 0);
Margarita Olaya668a6cc2011-06-09 14:50:19 -050083}
84
Julia Lawalle35b5ab2016-09-11 14:14:37 +020085static const struct gpio_chip template_chip = {
Andrew F. Davisca801a22016-01-25 09:43:47 -060086 .label = "tps65912-gpio",
Margarita Olaya668a6cc2011-06-09 14:50:19 -050087 .owner = THIS_MODULE,
Andrew F. Davisca801a22016-01-25 09:43:47 -060088 .get_direction = tps65912_gpio_get_direction,
89 .direction_input = tps65912_gpio_direction_input,
90 .direction_output = tps65912_gpio_direction_output,
Margarita Olaya668a6cc2011-06-09 14:50:19 -050091 .get = tps65912_gpio_get,
92 .set = tps65912_gpio_set,
Margarita Olaya668a6cc2011-06-09 14:50:19 -050093 .base = -1,
Andrew F. Davisca801a22016-01-25 09:43:47 -060094 .ngpio = 5,
95 .can_sleep = true,
Margarita Olaya668a6cc2011-06-09 14:50:19 -050096};
97
Bill Pemberton38363092012-11-19 13:22:34 -050098static int tps65912_gpio_probe(struct platform_device *pdev)
Margarita Olaya668a6cc2011-06-09 14:50:19 -050099{
Andrew F. Davisca801a22016-01-25 09:43:47 -0600100 struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
101 struct tps65912_gpio *gpio;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500102 int ret;
103
Andrew F. Davisca801a22016-01-25 09:43:47 -0600104 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
105 if (!gpio)
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500106 return -ENOMEM;
107
Andrew F. Davisca801a22016-01-25 09:43:47 -0600108 gpio->tps = dev_get_drvdata(pdev->dev.parent);
109 gpio->gpio_chip = template_chip;
110 gpio->gpio_chip.parent = tps->dev;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500111
Linus Walleij9d93efe2016-03-09 22:02:52 +0700112 ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gpio_chip,
113 gpio);
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500114 if (ret < 0) {
Andrew F. Davisca801a22016-01-25 09:43:47 -0600115 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
Axel Lin45e253a2012-09-01 17:44:27 +0800116 return ret;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500117 }
118
Andrew F. Davisca801a22016-01-25 09:43:47 -0600119 platform_set_drvdata(pdev, gpio);
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500120
Andrew F. Davisca801a22016-01-25 09:43:47 -0600121 return 0;
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500122}
123
Andrew F. Davisca801a22016-01-25 09:43:47 -0600124static const struct platform_device_id tps65912_gpio_id_table[] = {
125 { "tps65912-gpio", },
126 { /* sentinel */ }
127};
128MODULE_DEVICE_TABLE(platform, tps65912_gpio_id_table);
129
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500130static struct platform_driver tps65912_gpio_driver = {
131 .driver = {
132 .name = "tps65912-gpio",
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500133 },
134 .probe = tps65912_gpio_probe,
Andrew F. Davisca801a22016-01-25 09:43:47 -0600135 .id_table = tps65912_gpio_id_table,
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500136};
Andrew F. Davisca801a22016-01-25 09:43:47 -0600137module_platform_driver(tps65912_gpio_driver);
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500138
Andrew F. Davisca801a22016-01-25 09:43:47 -0600139MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
140MODULE_DESCRIPTION("TPS65912 GPIO driver");
Margarita Olaya668a6cc2011-06-09 14:50:19 -0500141MODULE_LICENSE("GPL v2");