diff options
author | Quentin Schulz <quentin.schulz@free-electrons.com> | 2017-12-05 09:46:40 -0500 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2017-12-07 04:05:30 -0500 |
commit | 449317a8b4c4ed47c2a51f864c4697ae57195b96 (patch) | |
tree | 1035da121fb30021659546d8015f724ac93f4ceb /drivers/pinctrl/pinctrl-axp209.c | |
parent | 3cac991e33b1395430c09ae49039702eb6748c7d (diff) |
pinctrl: move gpio-axp209 to pinctrl
To prepare the driver for the upcoming pinctrl features, move the GPIO
driver AXP209 from GPIO to pinctrl subsystem.
Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/pinctrl/pinctrl-axp209.c')
-rw-r--r-- | drivers/pinctrl/pinctrl-axp209.c | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/drivers/pinctrl/pinctrl-axp209.c b/drivers/pinctrl/pinctrl-axp209.c new file mode 100644 index 000000000000..6ee7dc1418fa --- /dev/null +++ b/drivers/pinctrl/pinctrl-axp209.c | |||
@@ -0,0 +1,189 @@ | |||
1 | /* | ||
2 | * AXP20x GPIO driver | ||
3 | * | ||
4 | * Copyright (C) 2016 Maxime Ripard <maxime.ripard@free-electrons.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms of the GNU General Public License as published by the | ||
8 | * Free Software Foundation; either version 2 of the License, or (at your | ||
9 | * option) any later version. | ||
10 | */ | ||
11 | |||
12 | #include <linux/bitops.h> | ||
13 | #include <linux/device.h> | ||
14 | #include <linux/gpio/driver.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/interrupt.h> | ||
17 | #include <linux/kernel.h> | ||
18 | #include <linux/mfd/axp20x.h> | ||
19 | #include <linux/module.h> | ||
20 | #include <linux/of.h> | ||
21 | #include <linux/platform_device.h> | ||
22 | #include <linux/regmap.h> | ||
23 | #include <linux/slab.h> | ||
24 | |||
25 | #define AXP20X_GPIO_FUNCTIONS 0x7 | ||
26 | #define AXP20X_GPIO_FUNCTION_OUT_LOW 0 | ||
27 | #define AXP20X_GPIO_FUNCTION_OUT_HIGH 1 | ||
28 | #define AXP20X_GPIO_FUNCTION_INPUT 2 | ||
29 | |||
30 | struct axp20x_gpio { | ||
31 | struct gpio_chip chip; | ||
32 | struct regmap *regmap; | ||
33 | }; | ||
34 | |||
35 | static int axp20x_gpio_get_reg(unsigned int offset) | ||
36 | { | ||
37 | switch (offset) { | ||
38 | case 0: | ||
39 | return AXP20X_GPIO0_CTRL; | ||
40 | case 1: | ||
41 | return AXP20X_GPIO1_CTRL; | ||
42 | case 2: | ||
43 | return AXP20X_GPIO2_CTRL; | ||
44 | } | ||
45 | |||
46 | return -EINVAL; | ||
47 | } | ||
48 | |||
49 | static int axp20x_gpio_input(struct gpio_chip *chip, unsigned int offset) | ||
50 | { | ||
51 | struct axp20x_gpio *gpio = gpiochip_get_data(chip); | ||
52 | int reg; | ||
53 | |||
54 | reg = axp20x_gpio_get_reg(offset); | ||
55 | if (reg < 0) | ||
56 | return reg; | ||
57 | |||
58 | return regmap_update_bits(gpio->regmap, reg, | ||
59 | AXP20X_GPIO_FUNCTIONS, | ||
60 | AXP20X_GPIO_FUNCTION_INPUT); | ||
61 | } | ||
62 | |||
63 | static int axp20x_gpio_get(struct gpio_chip *chip, unsigned int offset) | ||
64 | { | ||
65 | struct axp20x_gpio *gpio = gpiochip_get_data(chip); | ||
66 | unsigned int val; | ||
67 | int ret; | ||
68 | |||
69 | ret = regmap_read(gpio->regmap, AXP20X_GPIO20_SS, &val); | ||
70 | if (ret) | ||
71 | return ret; | ||
72 | |||
73 | return !!(val & BIT(offset + 4)); | ||
74 | } | ||
75 | |||
76 | static int axp20x_gpio_get_direction(struct gpio_chip *chip, | ||
77 | unsigned int offset) | ||
78 | { | ||
79 | struct axp20x_gpio *gpio = gpiochip_get_data(chip); | ||
80 | unsigned int val; | ||
81 | int reg, ret; | ||
82 | |||
83 | reg = axp20x_gpio_get_reg(offset); | ||
84 | if (reg < 0) | ||
85 | return reg; | ||
86 | |||
87 | ret = regmap_read(gpio->regmap, reg, &val); | ||
88 | if (ret) | ||
89 | return ret; | ||
90 | |||
91 | /* | ||
92 | * This shouldn't really happen if the pin is in use already, | ||
93 | * or if it's not in use yet, it doesn't matter since we're | ||
94 | * going to change the value soon anyway. Default to output. | ||
95 | */ | ||
96 | if ((val & AXP20X_GPIO_FUNCTIONS) > 2) | ||
97 | return 0; | ||
98 | |||
99 | /* | ||
100 | * The GPIO directions are the three lowest values. | ||
101 | * 2 is input, 0 and 1 are output | ||
102 | */ | ||
103 | return val & 2; | ||
104 | } | ||
105 | |||
106 | static int axp20x_gpio_output(struct gpio_chip *chip, unsigned int offset, | ||
107 | int value) | ||
108 | { | ||
109 | struct axp20x_gpio *gpio = gpiochip_get_data(chip); | ||
110 | int reg; | ||
111 | |||
112 | reg = axp20x_gpio_get_reg(offset); | ||
113 | if (reg < 0) | ||
114 | return reg; | ||
115 | |||
116 | return regmap_update_bits(gpio->regmap, reg, | ||
117 | AXP20X_GPIO_FUNCTIONS, | ||
118 | value ? AXP20X_GPIO_FUNCTION_OUT_HIGH | ||
119 | : AXP20X_GPIO_FUNCTION_OUT_LOW); | ||
120 | } | ||
121 | |||
122 | static void axp20x_gpio_set(struct gpio_chip *chip, unsigned int offset, | ||
123 | int value) | ||
124 | { | ||
125 | axp20x_gpio_output(chip, offset, value); | ||
126 | } | ||
127 | |||
128 | static int axp20x_gpio_probe(struct platform_device *pdev) | ||
129 | { | ||
130 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); | ||
131 | struct axp20x_gpio *gpio; | ||
132 | int ret; | ||
133 | |||
134 | if (!of_device_is_available(pdev->dev.of_node)) | ||
135 | return -ENODEV; | ||
136 | |||
137 | if (!axp20x) { | ||
138 | dev_err(&pdev->dev, "Parent drvdata not set\n"); | ||
139 | return -EINVAL; | ||
140 | } | ||
141 | |||
142 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); | ||
143 | if (!gpio) | ||
144 | return -ENOMEM; | ||
145 | |||
146 | gpio->chip.base = -1; | ||
147 | gpio->chip.can_sleep = true; | ||
148 | gpio->chip.parent = &pdev->dev; | ||
149 | gpio->chip.label = dev_name(&pdev->dev); | ||
150 | gpio->chip.owner = THIS_MODULE; | ||
151 | gpio->chip.get = axp20x_gpio_get; | ||
152 | gpio->chip.get_direction = axp20x_gpio_get_direction; | ||
153 | gpio->chip.set = axp20x_gpio_set; | ||
154 | gpio->chip.direction_input = axp20x_gpio_input; | ||
155 | gpio->chip.direction_output = axp20x_gpio_output; | ||
156 | gpio->chip.ngpio = 3; | ||
157 | |||
158 | gpio->regmap = axp20x->regmap; | ||
159 | |||
160 | ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); | ||
161 | if (ret) { | ||
162 | dev_err(&pdev->dev, "Failed to register GPIO chip\n"); | ||
163 | return ret; | ||
164 | } | ||
165 | |||
166 | dev_info(&pdev->dev, "AXP209 GPIO driver loaded\n"); | ||
167 | |||
168 | return 0; | ||
169 | } | ||
170 | |||
171 | static const struct of_device_id axp20x_gpio_match[] = { | ||
172 | { .compatible = "x-powers,axp209-gpio" }, | ||
173 | { } | ||
174 | }; | ||
175 | MODULE_DEVICE_TABLE(of, axp20x_gpio_match); | ||
176 | |||
177 | static struct platform_driver axp20x_gpio_driver = { | ||
178 | .probe = axp20x_gpio_probe, | ||
179 | .driver = { | ||
180 | .name = "axp20x-gpio", | ||
181 | .of_match_table = axp20x_gpio_match, | ||
182 | }, | ||
183 | }; | ||
184 | |||
185 | module_platform_driver(axp20x_gpio_driver); | ||
186 | |||
187 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); | ||
188 | MODULE_DESCRIPTION("AXP20x PMIC GPIO driver"); | ||
189 | MODULE_LICENSE("GPL"); | ||