1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AXP20x pinctrl and GPIO driver
5 * Copyright (C) 2016 Maxime Ripard <maxime.ripard@free-electrons.com>
6 * Copyright (C) 2017 Quentin Schulz <quentin.schulz@free-electrons.com>
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/pinctrl/pinconf-generic.h>
20 #include <linux/pinctrl/pinctrl.h>
21 #include <linux/pinctrl/pinmux.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
26 #define AXP20X_GPIO_FUNCTIONS 0x7
27 #define AXP20X_GPIO_FUNCTION_OUT_LOW 0
28 #define AXP20X_GPIO_FUNCTION_OUT_HIGH 1
29 #define AXP20X_GPIO_FUNCTION_INPUT 2
31 #define AXP20X_FUNC_GPIO_OUT 0
32 #define AXP20X_FUNC_GPIO_IN 1
33 #define AXP20X_FUNC_LDO 2
34 #define AXP20X_FUNC_ADC 3
35 #define AXP20X_FUNCS_NB 4
37 #define AXP20X_MUX_GPIO_OUT 0
38 #define AXP20X_MUX_GPIO_IN BIT(1)
39 #define AXP20X_MUX_ADC BIT(2)
41 #define AXP813_MUX_ADC (BIT(2) | BIT(0))
43 struct axp20x_pctrl_desc {
44 const struct pinctrl_pin_desc *pins;
46 /* Stores the pins supporting LDO function. Bit offset is pin number. */
48 /* Stores the pins supporting ADC function. Bit offset is pin number. */
50 u8 gpio_status_offset;
54 struct axp20x_pinctrl_function {
62 struct gpio_chip chip;
63 struct regmap *regmap;
64 struct pinctrl_dev *pctl_dev;
66 const struct axp20x_pctrl_desc *desc;
67 struct axp20x_pinctrl_function funcs[AXP20X_FUNCS_NB];
70 static const struct pinctrl_pin_desc axp209_pins[] = {
71 PINCTRL_PIN(0, "GPIO0"),
72 PINCTRL_PIN(1, "GPIO1"),
73 PINCTRL_PIN(2, "GPIO2"),
76 static const struct pinctrl_pin_desc axp22x_pins[] = {
77 PINCTRL_PIN(0, "GPIO0"),
78 PINCTRL_PIN(1, "GPIO1"),
81 static const struct axp20x_pctrl_desc axp20x_data = {
83 .npins = ARRAY_SIZE(axp209_pins),
84 .ldo_mask = BIT(0) | BIT(1),
85 .adc_mask = BIT(0) | BIT(1),
86 .gpio_status_offset = 4,
87 .adc_mux = AXP20X_MUX_ADC,
90 static const struct axp20x_pctrl_desc axp22x_data = {
92 .npins = ARRAY_SIZE(axp22x_pins),
93 .ldo_mask = BIT(0) | BIT(1),
94 .gpio_status_offset = 0,
97 static const struct axp20x_pctrl_desc axp813_data = {
99 .npins = ARRAY_SIZE(axp22x_pins),
100 .ldo_mask = BIT(0) | BIT(1),
102 .gpio_status_offset = 0,
103 .adc_mux = AXP813_MUX_ADC,
106 static int axp20x_gpio_get_reg(unsigned int offset)
110 return AXP20X_GPIO0_CTRL;
112 return AXP20X_GPIO1_CTRL;
114 return AXP20X_GPIO2_CTRL;
120 static int axp20x_gpio_input(struct gpio_chip *chip, unsigned int offset)
122 return pinctrl_gpio_direction_input(chip->base + offset);
125 static int axp20x_gpio_get(struct gpio_chip *chip, unsigned int offset)
127 struct axp20x_pctl *pctl = gpiochip_get_data(chip);
131 ret = regmap_read(pctl->regmap, AXP20X_GPIO20_SS, &val);
135 return !!(val & BIT(offset + pctl->desc->gpio_status_offset));
138 static int axp20x_gpio_get_direction(struct gpio_chip *chip,
141 struct axp20x_pctl *pctl = gpiochip_get_data(chip);
145 reg = axp20x_gpio_get_reg(offset);
149 ret = regmap_read(pctl->regmap, reg, &val);
154 * This shouldn't really happen if the pin is in use already,
155 * or if it's not in use yet, it doesn't matter since we're
156 * going to change the value soon anyway. Default to output.
158 if ((val & AXP20X_GPIO_FUNCTIONS) > 2)
159 return GPIO_LINE_DIRECTION_OUT;
162 * The GPIO directions are the three lowest values.
163 * 2 is input, 0 and 1 are output
166 return GPIO_LINE_DIRECTION_IN;
168 return GPIO_LINE_DIRECTION_OUT;
171 static int axp20x_gpio_output(struct gpio_chip *chip, unsigned int offset,
174 chip->set(chip, offset, value);
179 static void axp20x_gpio_set(struct gpio_chip *chip, unsigned int offset,
182 struct axp20x_pctl *pctl = gpiochip_get_data(chip);
185 reg = axp20x_gpio_get_reg(offset);
189 regmap_update_bits(pctl->regmap, reg,
190 AXP20X_GPIO_FUNCTIONS,
191 value ? AXP20X_GPIO_FUNCTION_OUT_HIGH :
192 AXP20X_GPIO_FUNCTION_OUT_LOW);
195 static int axp20x_pmx_set(struct pinctrl_dev *pctldev, unsigned int offset,
198 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
201 reg = axp20x_gpio_get_reg(offset);
205 return regmap_update_bits(pctl->regmap, reg, AXP20X_GPIO_FUNCTIONS,
209 static int axp20x_pmx_func_cnt(struct pinctrl_dev *pctldev)
211 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
213 return ARRAY_SIZE(pctl->funcs);
216 static const char *axp20x_pmx_func_name(struct pinctrl_dev *pctldev,
217 unsigned int selector)
219 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
221 return pctl->funcs[selector].name;
224 static int axp20x_pmx_func_groups(struct pinctrl_dev *pctldev,
225 unsigned int selector,
226 const char * const **groups,
227 unsigned int *num_groups)
229 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
231 *groups = pctl->funcs[selector].groups;
232 *num_groups = pctl->funcs[selector].ngroups;
237 static int axp20x_pmx_set_mux(struct pinctrl_dev *pctldev,
238 unsigned int function, unsigned int group)
240 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
243 /* Every pin supports GPIO_OUT and GPIO_IN functions */
244 if (function <= AXP20X_FUNC_GPIO_IN)
245 return axp20x_pmx_set(pctldev, group,
246 pctl->funcs[function].muxval);
248 if (function == AXP20X_FUNC_LDO)
249 mask = pctl->desc->ldo_mask;
251 mask = pctl->desc->adc_mask;
253 if (!(BIT(group) & mask))
257 * We let the regulator framework handle the LDO muxing as muxing bits
258 * are basically also regulators on/off bits. It's better not to enforce
259 * any state of the regulator when selecting LDO mux so that we don't
260 * interfere with the regulator driver.
262 if (function == AXP20X_FUNC_LDO)
265 return axp20x_pmx_set(pctldev, group, pctl->funcs[function].muxval);
268 static int axp20x_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
269 struct pinctrl_gpio_range *range,
270 unsigned int offset, bool input)
272 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
275 return axp20x_pmx_set(pctldev, offset,
276 pctl->funcs[AXP20X_FUNC_GPIO_IN].muxval);
278 return axp20x_pmx_set(pctldev, offset,
279 pctl->funcs[AXP20X_FUNC_GPIO_OUT].muxval);
282 static const struct pinmux_ops axp20x_pmx_ops = {
283 .get_functions_count = axp20x_pmx_func_cnt,
284 .get_function_name = axp20x_pmx_func_name,
285 .get_function_groups = axp20x_pmx_func_groups,
286 .set_mux = axp20x_pmx_set_mux,
287 .gpio_set_direction = axp20x_pmx_gpio_set_direction,
291 static int axp20x_groups_cnt(struct pinctrl_dev *pctldev)
293 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
295 return pctl->desc->npins;
298 static int axp20x_group_pins(struct pinctrl_dev *pctldev, unsigned int selector,
299 const unsigned int **pins, unsigned int *num_pins)
301 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
303 *pins = (unsigned int *)&pctl->desc->pins[selector];
309 static const char *axp20x_group_name(struct pinctrl_dev *pctldev,
310 unsigned int selector)
312 struct axp20x_pctl *pctl = pinctrl_dev_get_drvdata(pctldev);
314 return pctl->desc->pins[selector].name;
317 static const struct pinctrl_ops axp20x_pctrl_ops = {
318 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
319 .dt_free_map = pinconf_generic_dt_free_map,
320 .get_groups_count = axp20x_groups_cnt,
321 .get_group_name = axp20x_group_name,
322 .get_group_pins = axp20x_group_pins,
325 static int axp20x_funcs_groups_from_mask(struct device *dev, unsigned int mask,
326 unsigned int mask_len,
327 struct axp20x_pinctrl_function *func,
328 const struct pinctrl_pin_desc *pins)
330 unsigned long int mask_cpy = mask;
332 unsigned int ngroups = hweight8(mask);
335 func->ngroups = ngroups;
336 if (func->ngroups > 0) {
337 func->groups = devm_kcalloc(dev,
338 ngroups, sizeof(const char *),
342 group = func->groups;
343 for_each_set_bit(bit, &mask_cpy, mask_len) {
344 *group = pins[bit].name;
352 static int axp20x_build_funcs_groups(struct platform_device *pdev)
354 struct axp20x_pctl *pctl = platform_get_drvdata(pdev);
355 int i, ret, pin, npins = pctl->desc->npins;
357 pctl->funcs[AXP20X_FUNC_GPIO_OUT].name = "gpio_out";
358 pctl->funcs[AXP20X_FUNC_GPIO_OUT].muxval = AXP20X_MUX_GPIO_OUT;
359 pctl->funcs[AXP20X_FUNC_GPIO_IN].name = "gpio_in";
360 pctl->funcs[AXP20X_FUNC_GPIO_IN].muxval = AXP20X_MUX_GPIO_IN;
361 pctl->funcs[AXP20X_FUNC_LDO].name = "ldo";
363 * Muxval for LDO is useless as we won't use it.
364 * See comment in axp20x_pmx_set_mux.
366 pctl->funcs[AXP20X_FUNC_ADC].name = "adc";
367 pctl->funcs[AXP20X_FUNC_ADC].muxval = pctl->desc->adc_mux;
369 /* Every pin supports GPIO_OUT and GPIO_IN functions */
370 for (i = 0; i <= AXP20X_FUNC_GPIO_IN; i++) {
371 pctl->funcs[i].ngroups = npins;
372 pctl->funcs[i].groups = devm_kcalloc(&pdev->dev,
373 npins, sizeof(char *),
375 if (!pctl->funcs[i].groups)
377 for (pin = 0; pin < npins; pin++)
378 pctl->funcs[i].groups[pin] = pctl->desc->pins[pin].name;
381 ret = axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->ldo_mask,
382 npins, &pctl->funcs[AXP20X_FUNC_LDO],
387 ret = axp20x_funcs_groups_from_mask(&pdev->dev, pctl->desc->adc_mask,
388 npins, &pctl->funcs[AXP20X_FUNC_ADC],
396 static const struct of_device_id axp20x_pctl_match[] = {
397 { .compatible = "x-powers,axp209-gpio", .data = &axp20x_data, },
398 { .compatible = "x-powers,axp221-gpio", .data = &axp22x_data, },
399 { .compatible = "x-powers,axp813-gpio", .data = &axp813_data, },
402 MODULE_DEVICE_TABLE(of, axp20x_pctl_match);
404 static int axp20x_pctl_probe(struct platform_device *pdev)
406 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
407 struct axp20x_pctl *pctl;
408 struct device *dev = &pdev->dev;
409 struct pinctrl_desc *pctrl_desc;
412 if (!of_device_is_available(pdev->dev.of_node))
416 dev_err(&pdev->dev, "Parent drvdata not set\n");
420 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
424 pctl->chip.base = -1;
425 pctl->chip.can_sleep = true;
426 pctl->chip.request = gpiochip_generic_request;
427 pctl->chip.free = gpiochip_generic_free;
428 pctl->chip.parent = &pdev->dev;
429 pctl->chip.label = dev_name(&pdev->dev);
430 pctl->chip.owner = THIS_MODULE;
431 pctl->chip.get = axp20x_gpio_get;
432 pctl->chip.get_direction = axp20x_gpio_get_direction;
433 pctl->chip.set = axp20x_gpio_set;
434 pctl->chip.direction_input = axp20x_gpio_input;
435 pctl->chip.direction_output = axp20x_gpio_output;
437 pctl->desc = of_device_get_match_data(dev);
439 pctl->chip.ngpio = pctl->desc->npins;
441 pctl->regmap = axp20x->regmap;
442 pctl->dev = &pdev->dev;
444 platform_set_drvdata(pdev, pctl);
446 ret = axp20x_build_funcs_groups(pdev);
448 dev_err(&pdev->dev, "failed to build groups\n");
452 pctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctrl_desc), GFP_KERNEL);
456 pctrl_desc->name = dev_name(&pdev->dev);
457 pctrl_desc->owner = THIS_MODULE;
458 pctrl_desc->pins = pctl->desc->pins;
459 pctrl_desc->npins = pctl->desc->npins;
460 pctrl_desc->pctlops = &axp20x_pctrl_ops;
461 pctrl_desc->pmxops = &axp20x_pmx_ops;
463 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
464 if (IS_ERR(pctl->pctl_dev)) {
465 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
466 return PTR_ERR(pctl->pctl_dev);
469 ret = devm_gpiochip_add_data(&pdev->dev, &pctl->chip, pctl);
471 dev_err(&pdev->dev, "Failed to register GPIO chip\n");
475 ret = gpiochip_add_pin_range(&pctl->chip, dev_name(&pdev->dev),
476 pctl->desc->pins->number,
477 pctl->desc->pins->number,
480 dev_err(&pdev->dev, "failed to add pin range\n");
484 dev_info(&pdev->dev, "AXP209 pinctrl and GPIO driver loaded\n");
489 static struct platform_driver axp20x_pctl_driver = {
490 .probe = axp20x_pctl_probe,
492 .name = "axp20x-gpio",
493 .of_match_table = axp20x_pctl_match,
497 module_platform_driver(axp20x_pctl_driver);
499 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
500 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
501 MODULE_DESCRIPTION("AXP20x PMIC pinctrl and GPIO driver");
502 MODULE_LICENSE("GPL");