2 * ams AS3722 pin control and GPIO driver.
4 * Copyright (c) 2013, NVIDIA Corporation.
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <linux/delay.h>
24 #include <linux/gpio.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/mfd/as3722.h>
29 #include <linux/of_device.h>
30 #include <linux/platform_device.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/pinctrl/machine.h>
33 #include <linux/pinctrl/pinctrl.h>
34 #include <linux/pinctrl/pinconf-generic.h>
35 #include <linux/pinctrl/pinconf.h>
36 #include <linux/pinctrl/pinmux.h>
38 #include <linux/slab.h>
42 #include "pinctrl-utils.h"
44 #define AS3722_PIN_GPIO0 0
45 #define AS3722_PIN_GPIO1 1
46 #define AS3722_PIN_GPIO2 2
47 #define AS3722_PIN_GPIO3 3
48 #define AS3722_PIN_GPIO4 4
49 #define AS3722_PIN_GPIO5 5
50 #define AS3722_PIN_GPIO6 6
51 #define AS3722_PIN_GPIO7 7
52 #define AS3722_PIN_NUM (AS3722_PIN_GPIO7 + 1)
54 #define AS3722_GPIO_MODE_PULL_UP BIT(PIN_CONFIG_BIAS_PULL_UP)
55 #define AS3722_GPIO_MODE_PULL_DOWN BIT(PIN_CONFIG_BIAS_PULL_DOWN)
56 #define AS3722_GPIO_MODE_HIGH_IMPED BIT(PIN_CONFIG_BIAS_HIGH_IMPEDANCE)
57 #define AS3722_GPIO_MODE_OPEN_DRAIN BIT(PIN_CONFIG_DRIVE_OPEN_DRAIN)
59 struct as3722_pin_function {
61 const char * const *groups;
66 struct as3722_gpio_pin_control {
71 struct as3722_pingroup {
73 const unsigned pins[1];
77 struct as3722_pctrl_info {
79 struct pinctrl_dev *pctl;
80 struct as3722 *as3722;
81 struct gpio_chip gpio_chip;
82 int pins_current_opt[AS3722_PIN_NUM];
83 const struct as3722_pin_function *functions;
84 unsigned num_functions;
85 const struct as3722_pingroup *pin_groups;
87 const struct pinctrl_pin_desc *pins;
89 struct as3722_gpio_pin_control gpio_control[AS3722_PIN_NUM];
92 static const struct pinctrl_pin_desc as3722_pins_desc[] = {
93 PINCTRL_PIN(AS3722_PIN_GPIO0, "gpio0"),
94 PINCTRL_PIN(AS3722_PIN_GPIO1, "gpio1"),
95 PINCTRL_PIN(AS3722_PIN_GPIO2, "gpio2"),
96 PINCTRL_PIN(AS3722_PIN_GPIO3, "gpio3"),
97 PINCTRL_PIN(AS3722_PIN_GPIO4, "gpio4"),
98 PINCTRL_PIN(AS3722_PIN_GPIO5, "gpio5"),
99 PINCTRL_PIN(AS3722_PIN_GPIO6, "gpio6"),
100 PINCTRL_PIN(AS3722_PIN_GPIO7, "gpio7"),
103 static const char * const gpio_groups[] = {
114 enum as3722_pinmux_option {
115 AS3722_PINMUX_GPIO = 0,
116 AS3722_PINMUX_INTERRUPT_OUT = 1,
117 AS3722_PINMUX_VSUB_VBAT_UNDEB_LOW_OUT = 2,
118 AS3722_PINMUX_GPIO_INTERRUPT = 3,
119 AS3722_PINMUX_PWM_INPUT = 4,
120 AS3722_PINMUX_VOLTAGE_IN_STBY = 5,
121 AS3722_PINMUX_OC_PG_SD0 = 6,
122 AS3722_PINMUX_PG_OUT = 7,
123 AS3722_PINMUX_CLK32K_OUT = 8,
124 AS3722_PINMUX_WATCHDOG_INPUT = 9,
125 AS3722_PINMUX_SOFT_RESET_IN = 11,
126 AS3722_PINMUX_PWM_OUTPUT = 12,
127 AS3722_PINMUX_VSUB_VBAT_LOW_DEB_OUT = 13,
128 AS3722_PINMUX_OC_PG_SD6 = 14,
131 #define FUNCTION_GROUP(fname, mux) \
134 .groups = gpio_groups, \
135 .ngroups = ARRAY_SIZE(gpio_groups), \
136 .mux_option = AS3722_PINMUX_##mux, \
139 static const struct as3722_pin_function as3722_pin_function[] = {
140 FUNCTION_GROUP(gpio, GPIO),
141 FUNCTION_GROUP(interrupt-out, INTERRUPT_OUT),
142 FUNCTION_GROUP(gpio-in-interrupt, GPIO_INTERRUPT),
143 FUNCTION_GROUP(vsup-vbat-low-undebounce-out, VSUB_VBAT_UNDEB_LOW_OUT),
144 FUNCTION_GROUP(vsup-vbat-low-debounce-out, VSUB_VBAT_LOW_DEB_OUT),
145 FUNCTION_GROUP(voltage-in-standby, VOLTAGE_IN_STBY),
146 FUNCTION_GROUP(oc-pg-sd0, OC_PG_SD0),
147 FUNCTION_GROUP(oc-pg-sd6, OC_PG_SD6),
148 FUNCTION_GROUP(powergood-out, PG_OUT),
149 FUNCTION_GROUP(pwm-in, PWM_INPUT),
150 FUNCTION_GROUP(pwm-out, PWM_OUTPUT),
151 FUNCTION_GROUP(clk32k-out, CLK32K_OUT),
152 FUNCTION_GROUP(watchdog-in, WATCHDOG_INPUT),
153 FUNCTION_GROUP(soft-reset-in, SOFT_RESET_IN),
156 #define AS3722_PINGROUP(pg_name, pin_id) \
159 .pins = {AS3722_PIN_##pin_id}, \
163 static const struct as3722_pingroup as3722_pingroups[] = {
164 AS3722_PINGROUP(gpio0, GPIO0),
165 AS3722_PINGROUP(gpio1, GPIO1),
166 AS3722_PINGROUP(gpio2, GPIO2),
167 AS3722_PINGROUP(gpio3, GPIO3),
168 AS3722_PINGROUP(gpio4, GPIO4),
169 AS3722_PINGROUP(gpio5, GPIO5),
170 AS3722_PINGROUP(gpio6, GPIO6),
171 AS3722_PINGROUP(gpio7, GPIO7),
174 static int as3722_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
176 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
178 return as_pci->num_pin_groups;
181 static const char *as3722_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
184 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
186 return as_pci->pin_groups[group].name;
189 static int as3722_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
190 unsigned group, const unsigned **pins, unsigned *num_pins)
192 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
194 *pins = as_pci->pin_groups[group].pins;
195 *num_pins = as_pci->pin_groups[group].npins;
199 static const struct pinctrl_ops as3722_pinctrl_ops = {
200 .get_groups_count = as3722_pinctrl_get_groups_count,
201 .get_group_name = as3722_pinctrl_get_group_name,
202 .get_group_pins = as3722_pinctrl_get_group_pins,
203 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
204 .dt_free_map = pinctrl_utils_free_map,
207 static int as3722_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
209 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
211 return as_pci->num_functions;
214 static const char *as3722_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
217 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
219 return as_pci->functions[function].name;
222 static int as3722_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
223 unsigned function, const char * const **groups,
224 unsigned * const num_groups)
226 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
228 *groups = as_pci->functions[function].groups;
229 *num_groups = as_pci->functions[function].ngroups;
233 static int as3722_pinctrl_set(struct pinctrl_dev *pctldev, unsigned function,
236 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
237 int gpio_cntr_reg = AS3722_GPIOn_CONTROL_REG(group);
238 u8 val = AS3722_GPIO_IOSF_VAL(as_pci->functions[function].mux_option);
241 dev_dbg(as_pci->dev, "%s(): GPIO %u pin to function %u and val %u\n",
242 __func__, group, function, val);
244 ret = as3722_update_bits(as_pci->as3722, gpio_cntr_reg,
245 AS3722_GPIO_IOSF_MASK, val);
247 dev_err(as_pci->dev, "GPIO%d_CTRL_REG update failed %d\n",
251 as_pci->gpio_control[group].io_function = function;
254 case AS3722_GPIO_IOSF_SD0_OUT:
255 case AS3722_GPIO_IOSF_PWR_GOOD_OUT:
256 case AS3722_GPIO_IOSF_Q32K_OUT:
257 case AS3722_GPIO_IOSF_PWM_OUT:
258 case AS3722_GPIO_IOSF_SD6_LOW_VOLT_LOW:
259 ret = as3722_update_bits(as_pci->as3722, gpio_cntr_reg,
260 AS3722_GPIO_MODE_MASK, AS3722_GPIO_MODE_OUTPUT_VDDH);
262 dev_err(as_pci->dev, "GPIO%d_CTRL update failed %d\n",
266 as_pci->gpio_control[group].mode_prop =
267 AS3722_GPIO_MODE_OUTPUT_VDDH;
275 static int as3722_pinctrl_gpio_get_mode(unsigned gpio_mode_prop, bool input)
277 if (gpio_mode_prop & AS3722_GPIO_MODE_HIGH_IMPED)
280 if (gpio_mode_prop & AS3722_GPIO_MODE_OPEN_DRAIN) {
281 if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_UP)
282 return AS3722_GPIO_MODE_IO_OPEN_DRAIN_PULL_UP;
283 return AS3722_GPIO_MODE_IO_OPEN_DRAIN;
286 if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_UP)
287 return AS3722_GPIO_MODE_INPUT_PULL_UP;
288 else if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_DOWN)
289 return AS3722_GPIO_MODE_INPUT_PULL_DOWN;
290 return AS3722_GPIO_MODE_INPUT;
292 if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_DOWN)
293 return AS3722_GPIO_MODE_OUTPUT_VDDL;
294 return AS3722_GPIO_MODE_OUTPUT_VDDH;
297 static int as3722_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
298 struct pinctrl_gpio_range *range, unsigned offset)
300 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
302 if (as_pci->gpio_control[offset].io_function)
307 static int as3722_pinctrl_gpio_set_direction(struct pinctrl_dev *pctldev,
308 struct pinctrl_gpio_range *range, unsigned offset, bool input)
310 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
311 struct as3722 *as3722 = as_pci->as3722;
314 mode = as3722_pinctrl_gpio_get_mode(
315 as_pci->gpio_control[offset].mode_prop, input);
317 dev_err(as_pci->dev, "%s direction for GPIO %d not supported\n",
318 (input) ? "Input" : "Output", offset);
322 return as3722_update_bits(as3722, AS3722_GPIOn_CONTROL_REG(offset),
323 AS3722_GPIO_MODE_MASK, mode);
326 static const struct pinmux_ops as3722_pinmux_ops = {
327 .get_functions_count = as3722_pinctrl_get_funcs_count,
328 .get_function_name = as3722_pinctrl_get_func_name,
329 .get_function_groups = as3722_pinctrl_get_func_groups,
330 .set_mux = as3722_pinctrl_set,
331 .gpio_request_enable = as3722_pinctrl_gpio_request_enable,
332 .gpio_set_direction = as3722_pinctrl_gpio_set_direction,
335 static int as3722_pinconf_get(struct pinctrl_dev *pctldev,
336 unsigned pin, unsigned long *config)
338 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
339 enum pin_config_param param = pinconf_to_config_param(*config);
344 case PIN_CONFIG_BIAS_DISABLE:
345 prop = AS3722_GPIO_MODE_PULL_UP |
346 AS3722_GPIO_MODE_PULL_DOWN;
347 if (!(as_pci->gpio_control[pin].mode_prop & prop))
352 case PIN_CONFIG_BIAS_PULL_UP:
353 prop = AS3722_GPIO_MODE_PULL_UP;
356 case PIN_CONFIG_BIAS_PULL_DOWN:
357 prop = AS3722_GPIO_MODE_PULL_DOWN;
360 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
361 prop = AS3722_GPIO_MODE_OPEN_DRAIN;
364 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
365 prop = AS3722_GPIO_MODE_HIGH_IMPED;
369 dev_err(as_pci->dev, "Properties not supported\n");
373 if (as_pci->gpio_control[pin].mode_prop & prop)
376 *config = pinconf_to_config_packed(param, (u16)arg);
380 static int as3722_pinconf_set(struct pinctrl_dev *pctldev,
381 unsigned pin, unsigned long *configs,
382 unsigned num_configs)
384 struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
385 enum pin_config_param param;
389 for (i = 0; i < num_configs; i++) {
390 param = pinconf_to_config_param(configs[i]);
391 mode_prop = as_pci->gpio_control[pin].mode_prop;
394 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
397 case PIN_CONFIG_BIAS_DISABLE:
398 mode_prop &= ~(AS3722_GPIO_MODE_PULL_UP |
399 AS3722_GPIO_MODE_PULL_DOWN);
401 case PIN_CONFIG_BIAS_PULL_UP:
402 mode_prop |= AS3722_GPIO_MODE_PULL_UP;
405 case PIN_CONFIG_BIAS_PULL_DOWN:
406 mode_prop |= AS3722_GPIO_MODE_PULL_DOWN;
409 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
410 mode_prop |= AS3722_GPIO_MODE_HIGH_IMPED;
413 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
414 mode_prop |= AS3722_GPIO_MODE_OPEN_DRAIN;
418 dev_err(as_pci->dev, "Properties not supported\n");
422 as_pci->gpio_control[pin].mode_prop = mode_prop;
427 static const struct pinconf_ops as3722_pinconf_ops = {
428 .pin_config_get = as3722_pinconf_get,
429 .pin_config_set = as3722_pinconf_set,
432 static struct pinctrl_desc as3722_pinctrl_desc = {
433 .pctlops = &as3722_pinctrl_ops,
434 .pmxops = &as3722_pinmux_ops,
435 .confops = &as3722_pinconf_ops,
436 .owner = THIS_MODULE,
439 static int as3722_gpio_get(struct gpio_chip *chip, unsigned offset)
441 struct as3722_pctrl_info *as_pci = gpiochip_get_data(chip);
442 struct as3722 *as3722 = as_pci->as3722;
450 ret = as3722_read(as3722, AS3722_GPIOn_CONTROL_REG(offset), &control);
453 "GPIO_CONTROL%d_REG read failed: %d\n", offset, ret);
457 invert_enable = !!(control & AS3722_GPIO_INV);
458 mode = control & AS3722_GPIO_MODE_MASK;
460 case AS3722_GPIO_MODE_INPUT:
461 case AS3722_GPIO_MODE_INPUT_PULL_UP:
462 case AS3722_GPIO_MODE_INPUT_PULL_DOWN:
463 case AS3722_GPIO_MODE_IO_OPEN_DRAIN:
464 case AS3722_GPIO_MODE_IO_OPEN_DRAIN_PULL_UP:
465 reg = AS3722_GPIO_SIGNAL_IN_REG;
467 case AS3722_GPIO_MODE_OUTPUT_VDDH:
468 case AS3722_GPIO_MODE_OUTPUT_VDDL:
469 reg = AS3722_GPIO_SIGNAL_OUT_REG;
475 ret = as3722_read(as3722, reg, &val);
478 "GPIO_SIGNAL_IN_REG read failed: %d\n", ret);
482 val = !!(val & AS3722_GPIOn_SIGNAL(offset));
483 return (invert_enable) ? !val : val;
486 static void as3722_gpio_set(struct gpio_chip *chip, unsigned offset,
489 struct as3722_pctrl_info *as_pci = gpiochip_get_data(chip);
490 struct as3722 *as3722 = as_pci->as3722;
495 ret = as3722_read(as3722, AS3722_GPIOn_CONTROL_REG(offset), &val);
498 "GPIO_CONTROL%d_REG read failed: %d\n", offset, ret);
501 en_invert = !!(val & AS3722_GPIO_INV);
504 val = (en_invert) ? 0 : AS3722_GPIOn_SIGNAL(offset);
506 val = (en_invert) ? AS3722_GPIOn_SIGNAL(offset) : 0;
508 ret = as3722_update_bits(as3722, AS3722_GPIO_SIGNAL_OUT_REG,
509 AS3722_GPIOn_SIGNAL(offset), val);
512 "GPIO_SIGNAL_OUT_REG update failed: %d\n", ret);
515 static int as3722_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
517 return pinctrl_gpio_direction_input(chip->base + offset);
520 static int as3722_gpio_direction_output(struct gpio_chip *chip,
521 unsigned offset, int value)
523 as3722_gpio_set(chip, offset, value);
524 return pinctrl_gpio_direction_output(chip->base + offset);
527 static int as3722_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
529 struct as3722_pctrl_info *as_pci = gpiochip_get_data(chip);
531 return as3722_irq_get_virq(as_pci->as3722, offset);
534 static const struct gpio_chip as3722_gpio_chip = {
535 .label = "as3722-gpio",
536 .owner = THIS_MODULE,
537 .request = gpiochip_generic_request,
538 .free = gpiochip_generic_free,
539 .get = as3722_gpio_get,
540 .set = as3722_gpio_set,
541 .direction_input = as3722_gpio_direction_input,
542 .direction_output = as3722_gpio_direction_output,
543 .to_irq = as3722_gpio_to_irq,
545 .ngpio = AS3722_PIN_NUM,
549 static int as3722_pinctrl_probe(struct platform_device *pdev)
551 struct as3722_pctrl_info *as_pci;
554 as_pci = devm_kzalloc(&pdev->dev, sizeof(*as_pci), GFP_KERNEL);
558 as_pci->dev = &pdev->dev;
559 as_pci->dev->of_node = pdev->dev.parent->of_node;
560 as_pci->as3722 = dev_get_drvdata(pdev->dev.parent);
561 platform_set_drvdata(pdev, as_pci);
563 as_pci->pins = as3722_pins_desc;
564 as_pci->num_pins = ARRAY_SIZE(as3722_pins_desc);
565 as_pci->functions = as3722_pin_function;
566 as_pci->num_functions = ARRAY_SIZE(as3722_pin_function);
567 as_pci->pin_groups = as3722_pingroups;
568 as_pci->num_pin_groups = ARRAY_SIZE(as3722_pingroups);
569 as3722_pinctrl_desc.name = dev_name(&pdev->dev);
570 as3722_pinctrl_desc.pins = as3722_pins_desc;
571 as3722_pinctrl_desc.npins = ARRAY_SIZE(as3722_pins_desc);
572 as_pci->pctl = devm_pinctrl_register(&pdev->dev, &as3722_pinctrl_desc,
574 if (IS_ERR(as_pci->pctl)) {
575 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
576 return PTR_ERR(as_pci->pctl);
579 as_pci->gpio_chip = as3722_gpio_chip;
580 as_pci->gpio_chip.parent = &pdev->dev;
581 as_pci->gpio_chip.of_node = pdev->dev.parent->of_node;
582 ret = gpiochip_add_data(&as_pci->gpio_chip, as_pci);
584 dev_err(&pdev->dev, "Couldn't register gpiochip, %d\n", ret);
588 ret = gpiochip_add_pin_range(&as_pci->gpio_chip, dev_name(&pdev->dev),
589 0, 0, AS3722_PIN_NUM);
591 dev_err(&pdev->dev, "Couldn't add pin range, %d\n", ret);
598 gpiochip_remove(&as_pci->gpio_chip);
602 static int as3722_pinctrl_remove(struct platform_device *pdev)
604 struct as3722_pctrl_info *as_pci = platform_get_drvdata(pdev);
606 gpiochip_remove(&as_pci->gpio_chip);
610 static const struct of_device_id as3722_pinctrl_of_match[] = {
611 { .compatible = "ams,as3722-pinctrl", },
614 MODULE_DEVICE_TABLE(of, as3722_pinctrl_of_match);
616 static struct platform_driver as3722_pinctrl_driver = {
618 .name = "as3722-pinctrl",
619 .of_match_table = as3722_pinctrl_of_match,
621 .probe = as3722_pinctrl_probe,
622 .remove = as3722_pinctrl_remove,
624 module_platform_driver(as3722_pinctrl_driver);
626 MODULE_ALIAS("platform:as3722-pinctrl");
627 MODULE_DESCRIPTION("AS3722 pin control and GPIO driver");
628 MODULE_AUTHOR("Laxman Dewangan<ldewangan@nvidia.com>");
629 MODULE_LICENSE("GPL v2");