1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AXP20x pinctrl and GPIO driver
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/axp20x.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinmux.h>
28 #define AXP20X_GPIO_FUNCTIONS 0x7
29 #define AXP20X_GPIO_FUNCTION_OUT_LOW 0
30 #define AXP20X_GPIO_FUNCTION_OUT_HIGH 1
31 #define AXP20X_GPIO_FUNCTION_INPUT 2
33 #define AXP20X_FUNC_GPIO_OUT 0
34 #define AXP20X_FUNC_GPIO_IN 1
35 #define AXP20X_FUNC_LDO 2
36 #define AXP20X_FUNC_ADC 3
37 #define AXP20X_FUNCS_NB 4
39 #define AXP20X_MUX_GPIO_OUT 0
40 #define AXP20X_MUX_GPIO_IN BIT(1)
41 #define AXP20X_MUX_ADC BIT(2)
43 #define AXP813_MUX_ADC (BIT(2) | BIT(0))
45 struct axp20x_pctrl_desc {
46 const struct pinctrl_pin_desc *pins;
48 /* Stores the pins supporting LDO function. Bit offset is pin number. */
50 /* Stores the pins supporting ADC function. Bit offset is pin number. */
52 u8 gpio_status_offset;
56 struct axp20x_pinctrl_function {
64 struct gpio_chip chip;
65 struct regmap *regmap;
66 struct pinctrl_dev *pctl_dev;
68 const struct axp20x_pctrl_desc *desc;
69 struct axp20x_pinctrl_function funcs[AXP20X_FUNCS_NB];
72 static const struct pinctrl_pin_desc axp209_pins[] = {
73 PINCTRL_PIN(0, "GPIO0"),
74 PINCTRL_PIN(1, "GPIO1"),
75 PINCTRL_PIN(2, "GPIO2"),
78 static const struct pinctrl_pin_desc axp22x_pins[] = {
79 PINCTRL_PIN(0, "GPIO0"),
80 PINCTRL_PIN(1, "GPIO1"),
83 static const struct axp20x_pctrl_desc axp20x_data = {
85 .npins = ARRAY_SIZE(axp209_pins),
86 .ldo_mask = BIT(0) | BIT(1),
87 .adc_mask = BIT(0) | BIT(1),
88 .gpio_status_offset = 4,
89 .adc_mux = AXP20X_MUX_ADC,
92 static const struct axp20x_pctrl_desc axp22x_data = {
94 .npins = ARRAY_SIZE(axp22x_pins),
95 .ldo_mask = BIT(0) | BIT(1),
96 .gpio_status_offset = 0,
99 static const struct axp20x_pctrl_desc axp813_data = {
101 .npins = ARRAY_SIZE(axp22x_pins),
102 .ldo_mask = BIT(0) | BIT(1),
104 .gpio_status_offset = 0,
105 .adc_mux = AXP813_MUX_ADC,
108 static int axp20x_gpio_get_reg(unsigned int offset)
112 return AXP20X_GPIO0_CTRL;
114 return AXP20X_GPIO1_CTRL;
116 return AXP20X_GPIO2_CTRL;
122 static int axp20x_gpio_input(struct gpio_chip *chip, unsigned int offset)
124 return pinctrl_gpio_direction_input(chip->base + offset);
127 static int axp20x_gpio_get(struct gpio_chip *chip, unsigned int offset)
129 struct axp20x_pctl *pctl = gpiochip_get_data(chip);
133 ret = regmap_read(pctl->regmap, AXP20X_GPIO20_SS, &val);
137 return !!(val & BIT(offset + pctl->desc->gpio_status_offset));
140 static int axp20x_gpio_get_direction(struct gpio_chip *chip,
143 struct axp20x_pctl *pctl = gpiochip_get_data(chip);
147 reg = axp20x_gpio_get_reg(offset);
151 ret = regmap_read(pctl->regmap, reg, &val);
156 * This shouldn't really happen if the pin is in use already,
157 * or if it's not in use yet, it doesn't matter since we're
158 * going to change the value soon anyway. Default to output.
160 if ((val & AXP20X_GPIO_FUNCTIONS) > 2)
161 return GPIO_LINE_DIRECTION_OUT;
164 * The GPIO directions are the three lowest values.
165 * 2 is input, 0 and 1 are output
168 return GPIO_LINE_DIRECTION_IN;
170 return GPIO_LINE_DIRECTION_OUT;
173 static int axp20x_gpio_output(struct gpio_chip *chip, unsigned int offset,
176 chip->set(chip, offset, value);
181 static void axp20x_gpio_set(struct gpio_chip *chip, unsigned int offset,
184 struct axp20x_pctl *pctl = gpiochip_get_data(chip);
187 reg = axp20x_gpio_get_reg(offset);
191 regmap_update_bits(pctl->regmap, reg,
192 AXP20X_GPIO_FUNCTIONS,
193 value ? AXP20X_GPIO_FUNCTION_OUT_HIGH :
194 AXP20X_GPIO_FUNCTION_OUT_LOW);
197 static int axp20x_pmx_set(struct pinctrl_dev *pctldev, unsigned int offset,
200 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
203 reg = axp20x_gpio_get_reg(offset);
207 return regmap_update_bits(pctl->regmap, reg, AXP20X_GPIO_FUNCTIONS,
211 static int axp20x_pmx_func_cnt(struct pinctrl_dev *pctldev)
213 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
215 return ARRAY_SIZE(pctl->funcs);
218 static const char *axp20x_pmx_func_name(struct pinctrl_dev *pctldev,
219 unsigned int selector)
221 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
223 return pctl->funcs[selector].name;
226 static int axp20x_pmx_func_groups(struct pinctrl_dev *pctldev,
227 unsigned int selector,
228 const char * const **groups,
229 unsigned int *num_groups)
231 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
233 *groups = pctl->funcs[selector].groups;
234 *num_groups = pctl->funcs[selector].ngroups;
239 static int axp20x_pmx_set_mux(struct pinctrl_dev *pctldev,
240 unsigned int function, unsigned int group)
242 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
245 /* Every pin supports GPIO_OUT and GPIO_IN functions */
246 if (function <= AXP20X_FUNC_GPIO_IN)
247 return axp20x_pmx_set(pctldev, group,
248 pctl->funcs[function].muxval);
250 if (function == AXP20X_FUNC_LDO)
251 mask = pctl->desc->ldo_mask;
253 mask = pctl->desc->adc_mask;
255 if (!(BIT(group) & mask))
259 * We let the regulator framework handle the LDO muxing as muxing bits
260 * are basically also regulators on/off bits. It's better not to enforce
261 * any state of the regulator when selecting LDO mux so that we don't
262 * interfere with the regulator driver.
264 if (function == AXP20X_FUNC_LDO)
267 return axp20x_pmx_set(pctldev, group, pctl->funcs[function].muxval);
270 static int axp20x_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
271 struct pinctrl_gpio_range *range,
272 unsigned int offset, bool input)
274 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
277 return axp20x_pmx_set(pctldev, offset,
278 pctl->funcs[AXP20X_FUNC_GPIO_IN].muxval);
280 return axp20x_pmx_set(pctldev, offset,
281 pctl->funcs[AXP20X_FUNC_GPIO_OUT].muxval);
284 static const struct pinmux_ops axp20x_pmx_ops = {
285 .get_functions_count = axp20x_pmx_func_cnt,
286 .get_function_name = axp20x_pmx_func_name,
287 .get_function_groups = axp20x_pmx_func_groups,
288 .set_mux = axp20x_pmx_set_mux,
289 .gpio_set_direction = axp20x_pmx_gpio_set_direction,
293 static int axp20x_groups_cnt(struct pinctrl_dev *pctldev)
295 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
297 return pctl->desc->npins;
300 static int axp20x_group_pins(struct pinctrl_dev *pctldev, unsigned int selector,
301 const unsigned int **pins, unsigned int *num_pins)
303 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
305 *pins = (unsigned int *)&pctl->desc->pins[selector];
311 static const char *axp20x_group_name(struct pinctrl_dev *pctldev,
312 unsigned int selector)
314 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
316 return pctl->desc->pins[selector].name;
319 static const struct pinctrl_ops axp20x_pctrl_ops = {
320 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
321 .dt_free_map = pinconf_generic_dt_free_map,
322 .get_groups_count = axp20x_groups_cnt,
323 .get_group_name = axp20x_group_name,
324 .get_group_pins = axp20x_group_pins,
327 static int axp20x_funcs_groups_from_mask(struct device *dev, unsigned int mask,
328 unsigned int mask_len,
329 struct axp20x_pinctrl_function *func,
330 const struct pinctrl_pin_desc *pins)
332 unsigned long int mask_cpy = mask;
334 unsigned int ngroups = hweight8(mask);
337 func->ngroups = ngroups;
338 if (func->ngroups > 0) {
339 func->groups = devm_kcalloc(dev,
340 ngroups, sizeof(const char *),
344 group = func->groups;
345 for_each_set_bit(bit, &mask_cpy, mask_len) {
346 *group = pins[bit].name;
354 static int axp20x_build_funcs_groups(struct platform_device *pdev)
356 struct axp20x_pctl *pctl = platform_get_drvdata(pdev);
357 int i, ret, pin, npins = pctl->desc->npins;
359 pctl->funcs[AXP20X_FUNC_GPIO_OUT].name = "gpio_out";
360 pctl->funcs[AXP20X_FUNC_GPIO_OUT].muxval = AXP20X_MUX_GPIO_OUT;
361 pctl->funcs[AXP20X_FUNC_GPIO_IN].name = "gpio_in";
362 pctl->funcs[AXP20X_FUNC_GPIO_IN].muxval = AXP20X_MUX_GPIO_IN;
363 pctl->funcs[AXP20X_FUNC_LDO].name = "ldo";
365 * Muxval for LDO is useless as we won't use it.
366 * See comment in axp20x_pmx_set_mux.
368 pctl->funcs[AXP20X_FUNC_ADC].name = "adc";
369 pctl->funcs[AXP20X_FUNC_ADC].muxval = pctl->desc->adc_mux;
371 /* Every pin supports GPIO_OUT and GPIO_IN functions */
372 for (i = 0; i <= AXP20X_FUNC_GPIO_IN; i++) {
373 pctl->funcs[i].ngroups = npins;
374 pctl->funcs[i].groups = devm_kcalloc(&pdev->dev,
375 npins, sizeof(char *),
377 if (!pctl->funcs[i].groups)
379 for (pin = 0; pin < npins; pin++)
380 pctl->funcs[i].groups[pin] = pctl->desc->pins[pin].name;
383 ret = axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->ldo_mask,
384 npins, &pctl->funcs[AXP20X_FUNC_LDO],
389 ret = axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->adc_mask,
390 npins, &pctl->funcs[AXP20X_FUNC_ADC],
398 static const struct of_device_id axp20x_pctl_match[] = {
399 { .compatible = "x-powers,axp209-gpio", .data = &axp20x_data, },
400 { .compatible = "x-powers,axp221-gpio", .data = &axp22x_data, },
401 { .compatible = "x-powers,axp813-gpio", .data = &axp813_data, },
404 MODULE_DEVICE_TABLE(of, axp20x_pctl_match);
406 static int axp20x_pctl_probe(struct platform_device *pdev)
408 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
409 struct axp20x_pctl *pctl;
410 struct device *dev = &pdev->dev;
411 struct pinctrl_desc *pctrl_desc;
414 if (!of_device_is_available(pdev->dev.of_node))
418 dev_err(&pdev->dev, "Parent drvdata not set\n");
422 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
426 pctl->chip.base = -1;
427 pctl->chip.can_sleep = true;
428 pctl->chip.request = gpiochip_generic_request;
429 pctl->chip.free = gpiochip_generic_free;
430 pctl->chip.parent = &pdev->dev;
431 pctl->chip.label = dev_name(&pdev->dev);
432 pctl->chip.owner = THIS_MODULE;
433 pctl->chip.get = axp20x_gpio_get;
434 pctl->chip.get_direction = axp20x_gpio_get_direction;
435 pctl->chip.set = axp20x_gpio_set;
436 pctl->chip.direction_input = axp20x_gpio_input;
437 pctl->chip.direction_output = axp20x_gpio_output;
439 pctl->desc = of_device_get_match_data(dev);
441 pctl->chip.ngpio = pctl->desc->npins;
443 pctl->regmap = axp20x->regmap;
444 pctl->dev = &pdev->dev;
446 platform_set_drvdata(pdev, pctl);
448 ret = axp20x_build_funcs_groups(pdev);
450 dev_err(&pdev->dev, "failed to build groups\n");
454 pctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctrl_desc), GFP_KERNEL);
458 pctrl_desc->name = dev_name(&pdev->dev);
459 pctrl_desc->owner = THIS_MODULE;
460 pctrl_desc->pins = pctl->desc->pins;
461 pctrl_desc->npins = pctl->desc->npins;
462 pctrl_desc->pctlops = &axp20x_pctrl_ops;
463 pctrl_desc->pmxops = &axp20x_pmx_ops;
465 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
466 if (IS_ERR(pctl->pctl_dev)) {
467 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
468 return PTR_ERR(pctl->pctl_dev);
471 ret = devm_gpiochip_add_data(&pdev->dev, &pctl->chip, pctl);
473 dev_err(&pdev->dev, "Failed to register GPIO chip\n");
477 ret = gpiochip_add_pin_range(&pctl->chip, dev_name(&pdev->dev),
478 pctl->desc->pins->number,
479 pctl->desc->pins->number,
482 dev_err(&pdev->dev, "failed to add pin range\n");
486 dev_info(&pdev->dev, "AXP209 pinctrl and GPIO driver loaded\n");
491 static struct platform_driver axp20x_pctl_driver = {
492 .probe = axp20x_pctl_probe,
494 .name = "axp20x-gpio",
495 .of_match_table = axp20x_pctl_match,
499 module_platform_driver(axp20x_pctl_driver);
503 MODULE_DESCRIPTION("AXP20x PMIC pinctrl and GPIO driver");
504 MODULE_LICENSE("GPL");