2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/pinctrl/pinctrl.h>
18 #include <linux/pinctrl/pinmux.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinconf-generic.h>
21 #include <linux/slab.h>
22 #include <linux/regmap.h>
23 #include <linux/gpio.h>
24 #include <linux/interrupt.h>
25 #include <linux/of_device.h>
26 #include <linux/of_irq.h>
28 #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
31 #include "../pinctrl-utils.h"
34 #define PM8XXX_GPIO_MODE_ENABLED BIT(0)
35 #define PM8XXX_GPIO_MODE_INPUT 0
36 #define PM8XXX_GPIO_MODE_OUTPUT 2
39 #define PM8XXX_GPIO_PUSH_PULL 0
40 #define PM8XXX_GPIO_OPEN_DRAIN 1
43 #define PM8XXX_GPIO_BIAS_PU_30 0
44 #define PM8XXX_GPIO_BIAS_PU_1P5 1
45 #define PM8XXX_GPIO_BIAS_PU_31P5 2
46 #define PM8XXX_GPIO_BIAS_PU_1P5_30 3
47 #define PM8XXX_GPIO_BIAS_PD 4
48 #define PM8XXX_GPIO_BIAS_NP 5
51 #define SSBI_REG_ADDR_GPIO_BASE 0x150
52 #define SSBI_REG_ADDR_GPIO(n) (SSBI_REG_ADDR_GPIO_BASE + n)
54 #define PM8XXX_BANK_WRITE BIT(7)
56 #define PM8XXX_MAX_GPIOS 44
58 /* custom pinconf parameters */
59 #define PM8XXX_QCOM_DRIVE_STRENGH (PIN_CONFIG_END + 1)
60 #define PM8XXX_QCOM_PULL_UP_STRENGTH (PIN_CONFIG_END + 2)
63 * struct pm8xxx_pin_data - dynamic configuration for a pin
64 * @reg: address of the control register
65 * @irq: IRQ from the PMIC interrupt controller
66 * @power_source: logical selected voltage source, mapping in static data
67 * is used translate to register values
68 * @mode: operating mode for the pin (input/output)
69 * @open_drain: output buffer configured as open-drain (vs push-pull)
70 * @output_value: configured output value
71 * @bias: register view of configured bias
72 * @pull_up_strength: placeholder for selected pull up strength
73 * only used to configure bias when pull up is selected
74 * @output_strength: selector of output-strength
75 * @disable: pin disabled / configured as tristate
76 * @function: pinmux selector
77 * @inverted: pin logic is inverted
79 struct pm8xxx_pin_data {
96 struct regmap *regmap;
97 struct pinctrl_dev *pctrl;
98 struct gpio_chip chip;
100 struct pinctrl_desc desc;
104 static const struct pinconf_generic_params pm8xxx_gpio_bindings[] = {
105 {"qcom,drive-strength", PM8XXX_QCOM_DRIVE_STRENGH, 0},
106 {"qcom,pull-up-strength", PM8XXX_QCOM_PULL_UP_STRENGTH, 0},
109 #ifdef CONFIG_DEBUG_FS
110 static const struct pin_config_item pm8xxx_conf_items[ARRAY_SIZE(pm8xxx_gpio_bindings)] = {
111 PCONFDUMP(PM8XXX_QCOM_DRIVE_STRENGH, "drive-strength", NULL, true),
112 PCONFDUMP(PM8XXX_QCOM_PULL_UP_STRENGTH, "pull up strength", NULL, true),
116 static const char * const pm8xxx_groups[PM8XXX_MAX_GPIOS] = {
117 "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7", "gpio8",
118 "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", "gpio15",
119 "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", "gpio22",
120 "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", "gpio29",
121 "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35", "gpio36",
122 "gpio37", "gpio38", "gpio39", "gpio40", "gpio41", "gpio42", "gpio43",
126 static const char * const pm8xxx_gpio_functions[] = {
127 PMIC_GPIO_FUNC_NORMAL, PMIC_GPIO_FUNC_PAIRED,
128 PMIC_GPIO_FUNC_FUNC1, PMIC_GPIO_FUNC_FUNC2,
129 PMIC_GPIO_FUNC_DTEST1, PMIC_GPIO_FUNC_DTEST2,
130 PMIC_GPIO_FUNC_DTEST3, PMIC_GPIO_FUNC_DTEST4,
133 static int pm8xxx_read_bank(struct pm8xxx_gpio *pctrl,
134 struct pm8xxx_pin_data *pin, int bank)
136 unsigned int val = bank << 4;
139 ret = regmap_write(pctrl->regmap, pin->reg, val);
141 dev_err(pctrl->dev, "failed to select bank %d\n", bank);
145 ret = regmap_read(pctrl->regmap, pin->reg, &val);
147 dev_err(pctrl->dev, "failed to read register %d\n", bank);
154 static int pm8xxx_write_bank(struct pm8xxx_gpio *pctrl,
155 struct pm8xxx_pin_data *pin,
161 val |= PM8XXX_BANK_WRITE;
164 ret = regmap_write(pctrl->regmap, pin->reg, val);
166 dev_err(pctrl->dev, "failed to write register\n");
171 static int pm8xxx_get_groups_count(struct pinctrl_dev *pctldev)
173 struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
178 static const char *pm8xxx_get_group_name(struct pinctrl_dev *pctldev,
181 return pm8xxx_groups[group];
185 static int pm8xxx_get_group_pins(struct pinctrl_dev *pctldev,
187 const unsigned **pins,
190 struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
192 *pins = &pctrl->desc.pins[group].number;
198 static const struct pinctrl_ops pm8xxx_pinctrl_ops = {
199 .get_groups_count = pm8xxx_get_groups_count,
200 .get_group_name = pm8xxx_get_group_name,
201 .get_group_pins = pm8xxx_get_group_pins,
202 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
203 .dt_free_map = pinctrl_utils_free_map,
206 static int pm8xxx_get_functions_count(struct pinctrl_dev *pctldev)
208 return ARRAY_SIZE(pm8xxx_gpio_functions);
211 static const char *pm8xxx_get_function_name(struct pinctrl_dev *pctldev,
214 return pm8xxx_gpio_functions[function];
217 static int pm8xxx_get_function_groups(struct pinctrl_dev *pctldev,
219 const char * const **groups,
220 unsigned * const num_groups)
222 struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
224 *groups = pm8xxx_groups;
225 *num_groups = pctrl->npins;
229 static int pm8xxx_pinmux_set_mux(struct pinctrl_dev *pctldev,
233 struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
234 struct pm8xxx_pin_data *pin = pctrl->desc.pins[group].drv_data;
237 pin->function = function;
238 val = pin->function << 1;
240 pm8xxx_write_bank(pctrl, pin, 4, val);
245 static const struct pinmux_ops pm8xxx_pinmux_ops = {
246 .get_functions_count = pm8xxx_get_functions_count,
247 .get_function_name = pm8xxx_get_function_name,
248 .get_function_groups = pm8xxx_get_function_groups,
249 .set_mux = pm8xxx_pinmux_set_mux,
252 static int pm8xxx_pin_config_get(struct pinctrl_dev *pctldev,
254 unsigned long *config)
256 struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
257 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
258 unsigned param = pinconf_to_config_param(*config);
262 case PIN_CONFIG_BIAS_DISABLE:
263 arg = pin->bias == PM8XXX_GPIO_BIAS_NP;
265 case PIN_CONFIG_BIAS_PULL_DOWN:
266 arg = pin->bias == PM8XXX_GPIO_BIAS_PD;
268 case PIN_CONFIG_BIAS_PULL_UP:
269 arg = pin->bias <= PM8XXX_GPIO_BIAS_PU_1P5_30;
271 case PM8XXX_QCOM_PULL_UP_STRENGTH:
272 arg = pin->pull_up_strength;
274 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
277 case PIN_CONFIG_INPUT_ENABLE:
278 arg = pin->mode == PM8XXX_GPIO_MODE_INPUT;
280 case PIN_CONFIG_OUTPUT:
281 if (pin->mode & PM8XXX_GPIO_MODE_OUTPUT)
282 arg = pin->output_value;
286 case PIN_CONFIG_POWER_SOURCE:
287 arg = pin->power_source;
289 case PM8XXX_QCOM_DRIVE_STRENGH:
290 arg = pin->output_strength;
292 case PIN_CONFIG_DRIVE_PUSH_PULL:
293 arg = !pin->open_drain;
295 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
296 arg = pin->open_drain;
302 *config = pinconf_to_config_packed(param, arg);
307 static int pm8xxx_pin_config_set(struct pinctrl_dev *pctldev,
309 unsigned long *configs,
310 unsigned num_configs)
312 struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
313 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
320 for (i = 0; i < num_configs; i++) {
321 param = pinconf_to_config_param(configs[i]);
322 arg = pinconf_to_config_argument(configs[i]);
325 case PIN_CONFIG_BIAS_DISABLE:
326 pin->bias = PM8XXX_GPIO_BIAS_NP;
331 case PIN_CONFIG_BIAS_PULL_DOWN:
332 pin->bias = PM8XXX_GPIO_BIAS_PD;
337 case PM8XXX_QCOM_PULL_UP_STRENGTH:
338 if (arg > PM8XXX_GPIO_BIAS_PU_1P5_30) {
339 dev_err(pctrl->dev, "invalid pull-up strength\n");
342 pin->pull_up_strength = arg;
344 case PIN_CONFIG_BIAS_PULL_UP:
345 pin->bias = pin->pull_up_strength;
350 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
354 case PIN_CONFIG_INPUT_ENABLE:
355 pin->mode = PM8XXX_GPIO_MODE_INPUT;
356 banks |= BIT(0) | BIT(1);
358 case PIN_CONFIG_OUTPUT:
359 pin->mode = PM8XXX_GPIO_MODE_OUTPUT;
360 pin->output_value = !!arg;
361 banks |= BIT(0) | BIT(1);
363 case PIN_CONFIG_POWER_SOURCE:
364 pin->power_source = arg;
367 case PM8XXX_QCOM_DRIVE_STRENGH:
368 if (arg > PMIC_GPIO_STRENGTH_LOW) {
369 dev_err(pctrl->dev, "invalid drive strength\n");
372 pin->output_strength = arg;
375 case PIN_CONFIG_DRIVE_PUSH_PULL:
379 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
385 "unsupported config parameter: %x\n",
391 if (banks & BIT(0)) {
392 val = pin->power_source << 1;
393 val |= PM8XXX_GPIO_MODE_ENABLED;
394 pm8xxx_write_bank(pctrl, pin, 0, val);
397 if (banks & BIT(1)) {
398 val = pin->mode << 2;
399 val |= pin->open_drain << 1;
400 val |= pin->output_value;
401 pm8xxx_write_bank(pctrl, pin, 1, val);
404 if (banks & BIT(2)) {
405 val = pin->bias << 1;
406 pm8xxx_write_bank(pctrl, pin, 2, val);
409 if (banks & BIT(3)) {
410 val = pin->output_strength << 2;
412 pm8xxx_write_bank(pctrl, pin, 3, val);
415 if (banks & BIT(4)) {
416 val = pin->function << 1;
417 pm8xxx_write_bank(pctrl, pin, 4, val);
420 if (banks & BIT(5)) {
424 pm8xxx_write_bank(pctrl, pin, 5, val);
430 static const struct pinconf_ops pm8xxx_pinconf_ops = {
432 .pin_config_group_get = pm8xxx_pin_config_get,
433 .pin_config_group_set = pm8xxx_pin_config_set,
436 static struct pinctrl_desc pm8xxx_pinctrl_desc = {
437 .name = "pm8xxx_gpio",
438 .pctlops = &pm8xxx_pinctrl_ops,
439 .pmxops = &pm8xxx_pinmux_ops,
440 .confops = &pm8xxx_pinconf_ops,
441 .owner = THIS_MODULE,
444 static int pm8xxx_gpio_direction_input(struct gpio_chip *chip,
447 struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
448 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
451 pin->mode = PM8XXX_GPIO_MODE_INPUT;
452 val = pin->mode << 2;
454 pm8xxx_write_bank(pctrl, pin, 1, val);
459 static int pm8xxx_gpio_direction_output(struct gpio_chip *chip,
463 struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
464 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
467 pin->mode = PM8XXX_GPIO_MODE_OUTPUT;
468 pin->output_value = !!value;
470 val = pin->mode << 2;
471 val |= pin->open_drain << 1;
472 val |= pin->output_value;
474 pm8xxx_write_bank(pctrl, pin, 1, val);
479 static int pm8xxx_gpio_get(struct gpio_chip *chip, unsigned offset)
481 struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
482 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
486 if (pin->mode == PM8XXX_GPIO_MODE_OUTPUT) {
487 ret = pin->output_value;
489 ret = irq_get_irqchip_state(pin->irq, IRQCHIP_STATE_LINE_LEVEL, &state);
497 static void pm8xxx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
499 struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
500 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
503 pin->output_value = !!value;
505 val = pin->mode << 2;
506 val |= pin->open_drain << 1;
507 val |= pin->output_value;
509 pm8xxx_write_bank(pctrl, pin, 1, val);
512 static int pm8xxx_gpio_of_xlate(struct gpio_chip *chip,
513 const struct of_phandle_args *gpio_desc,
516 if (chip->of_gpio_n_cells < 2)
520 *flags = gpio_desc->args[1];
522 return gpio_desc->args[0] - 1;
526 static int pm8xxx_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
528 struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
529 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
534 #ifdef CONFIG_DEBUG_FS
535 #include <linux/seq_file.h>
537 static void pm8xxx_gpio_dbg_show_one(struct seq_file *s,
538 struct pinctrl_dev *pctldev,
539 struct gpio_chip *chip,
543 struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
544 struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
546 static const char * const modes[] = {
547 "in", "both", "out", "off"
549 static const char * const biases[] = {
550 "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
551 "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
553 static const char * const buffer_types[] = {
554 "push-pull", "open-drain"
556 static const char * const strengths[] = {
557 "no", "high", "medium", "low"
560 seq_printf(s, " gpio%-2d:", offset + 1);
564 seq_printf(s, " %-4s", modes[pin->mode]);
565 seq_printf(s, " %-7s", pm8xxx_gpio_functions[pin->function]);
566 seq_printf(s, " VIN%d", pin->power_source);
567 seq_printf(s, " %-27s", biases[pin->bias]);
568 seq_printf(s, " %-10s", buffer_types[pin->open_drain]);
569 seq_printf(s, " %-4s", pin->output_value ? "high" : "low");
570 seq_printf(s, " %-7s", strengths[pin->output_strength]);
572 seq_puts(s, " inverted");
576 static void pm8xxx_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
578 unsigned gpio = chip->base;
581 for (i = 0; i < chip->ngpio; i++, gpio++) {
582 pm8xxx_gpio_dbg_show_one(s, NULL, chip, i, gpio);
588 #define pm8xxx_gpio_dbg_show NULL
591 static struct gpio_chip pm8xxx_gpio_template = {
592 .direction_input = pm8xxx_gpio_direction_input,
593 .direction_output = pm8xxx_gpio_direction_output,
594 .get = pm8xxx_gpio_get,
595 .set = pm8xxx_gpio_set,
596 .of_xlate = pm8xxx_gpio_of_xlate,
597 .to_irq = pm8xxx_gpio_to_irq,
598 .dbg_show = pm8xxx_gpio_dbg_show,
599 .owner = THIS_MODULE,
602 static int pm8xxx_pin_populate(struct pm8xxx_gpio *pctrl,
603 struct pm8xxx_pin_data *pin)
607 val = pm8xxx_read_bank(pctrl, pin, 0);
611 pin->power_source = (val >> 1) & 0x7;
613 val = pm8xxx_read_bank(pctrl, pin, 1);
617 pin->mode = (val >> 2) & 0x3;
618 pin->open_drain = !!(val & BIT(1));
619 pin->output_value = val & BIT(0);
621 val = pm8xxx_read_bank(pctrl, pin, 2);
625 pin->bias = (val >> 1) & 0x7;
626 if (pin->bias <= PM8XXX_GPIO_BIAS_PU_1P5_30)
627 pin->pull_up_strength = pin->bias;
629 pin->pull_up_strength = PM8XXX_GPIO_BIAS_PU_30;
631 val = pm8xxx_read_bank(pctrl, pin, 3);
635 pin->output_strength = (val >> 2) & 0x3;
636 pin->disable = val & BIT(0);
638 val = pm8xxx_read_bank(pctrl, pin, 4);
642 pin->function = (val >> 1) & 0x7;
644 val = pm8xxx_read_bank(pctrl, pin, 5);
648 pin->inverted = !(val & BIT(3));
653 static const struct of_device_id pm8xxx_gpio_of_match[] = {
654 { .compatible = "qcom,pm8018-gpio" },
655 { .compatible = "qcom,pm8038-gpio" },
656 { .compatible = "qcom,pm8058-gpio" },
657 { .compatible = "qcom,pm8917-gpio" },
658 { .compatible = "qcom,pm8921-gpio" },
659 { .compatible = "qcom,ssbi-gpio" },
662 MODULE_DEVICE_TABLE(of, pm8xxx_gpio_of_match);
664 static int pm8xxx_gpio_probe(struct platform_device *pdev)
666 struct pm8xxx_pin_data *pin_data;
667 struct pinctrl_pin_desc *pins;
668 struct pm8xxx_gpio *pctrl;
672 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
676 pctrl->dev = &pdev->dev;
677 npins = platform_irq_count(pdev);
682 pctrl->npins = npins;
684 pctrl->regmap = dev_get_regmap(pdev->dev.parent, NULL);
685 if (!pctrl->regmap) {
686 dev_err(&pdev->dev, "parent regmap unavailable\n");
690 pctrl->desc = pm8xxx_pinctrl_desc;
691 pctrl->desc.npins = pctrl->npins;
693 pins = devm_kcalloc(&pdev->dev,
695 sizeof(struct pinctrl_pin_desc),
700 pin_data = devm_kcalloc(&pdev->dev,
702 sizeof(struct pm8xxx_pin_data),
707 for (i = 0; i < pctrl->desc.npins; i++) {
708 pin_data[i].reg = SSBI_REG_ADDR_GPIO(i);
709 pin_data[i].irq = platform_get_irq(pdev, i);
710 if (pin_data[i].irq < 0) {
712 "missing interrupts for pin %d\n", i);
713 return pin_data[i].irq;
716 ret = pm8xxx_pin_populate(pctrl, &pin_data[i]);
721 pins[i].name = pm8xxx_groups[i];
722 pins[i].drv_data = &pin_data[i];
724 pctrl->desc.pins = pins;
726 pctrl->desc.num_custom_params = ARRAY_SIZE(pm8xxx_gpio_bindings);
727 pctrl->desc.custom_params = pm8xxx_gpio_bindings;
728 #ifdef CONFIG_DEBUG_FS
729 pctrl->desc.custom_conf_items = pm8xxx_conf_items;
732 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
733 if (IS_ERR(pctrl->pctrl)) {
734 dev_err(&pdev->dev, "couldn't register pm8xxx gpio driver\n");
735 return PTR_ERR(pctrl->pctrl);
738 pctrl->chip = pm8xxx_gpio_template;
739 pctrl->chip.base = -1;
740 pctrl->chip.parent = &pdev->dev;
741 pctrl->chip.of_node = pdev->dev.of_node;
742 pctrl->chip.of_gpio_n_cells = 2;
743 pctrl->chip.label = dev_name(pctrl->dev);
744 pctrl->chip.ngpio = pctrl->npins;
745 ret = gpiochip_add_data(&pctrl->chip, pctrl);
747 dev_err(&pdev->dev, "failed register gpiochip\n");
751 ret = gpiochip_add_pin_range(&pctrl->chip,
752 dev_name(pctrl->dev),
753 0, 0, pctrl->chip.ngpio);
755 dev_err(pctrl->dev, "failed to add pin range\n");
756 goto unregister_gpiochip;
759 platform_set_drvdata(pdev, pctrl);
761 dev_dbg(&pdev->dev, "Qualcomm pm8xxx gpio driver probed\n");
766 gpiochip_remove(&pctrl->chip);
771 static int pm8xxx_gpio_remove(struct platform_device *pdev)
773 struct pm8xxx_gpio *pctrl = platform_get_drvdata(pdev);
775 gpiochip_remove(&pctrl->chip);
780 static struct platform_driver pm8xxx_gpio_driver = {
782 .name = "qcom-ssbi-gpio",
783 .of_match_table = pm8xxx_gpio_of_match,
785 .probe = pm8xxx_gpio_probe,
786 .remove = pm8xxx_gpio_remove,
789 module_platform_driver(pm8xxx_gpio_driver);
791 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
792 MODULE_DESCRIPTION("Qualcomm PM8xxx GPIO driver");
793 MODULE_LICENSE("GPL v2");