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_dt_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 inline struct as3722_pctrl_info *to_as_pci(struct gpio_chip *chip)
441 return container_of(chip, struct as3722_pctrl_info, gpio_chip);
444 static int as3722_gpio_get(struct gpio_chip *chip, unsigned offset)
446 struct as3722_pctrl_info *as_pci = to_as_pci(chip);
447 struct as3722 *as3722 = as_pci->as3722;
455 ret = as3722_read(as3722, AS3722_GPIOn_CONTROL_REG(offset), &control);
458 "GPIO_CONTROL%d_REG read failed: %d\n", offset, ret);
462 invert_enable = !!(control & AS3722_GPIO_INV);
463 mode = control & AS3722_GPIO_MODE_MASK;
465 case AS3722_GPIO_MODE_INPUT:
466 case AS3722_GPIO_MODE_INPUT_PULL_UP:
467 case AS3722_GPIO_MODE_INPUT_PULL_DOWN:
468 case AS3722_GPIO_MODE_IO_OPEN_DRAIN:
469 case AS3722_GPIO_MODE_IO_OPEN_DRAIN_PULL_UP:
470 reg = AS3722_GPIO_SIGNAL_IN_REG;
472 case AS3722_GPIO_MODE_OUTPUT_VDDH:
473 case AS3722_GPIO_MODE_OUTPUT_VDDL:
474 reg = AS3722_GPIO_SIGNAL_OUT_REG;
480 ret = as3722_read(as3722, reg, &val);
483 "GPIO_SIGNAL_IN_REG read failed: %d\n", ret);
487 val = !!(val & AS3722_GPIOn_SIGNAL(offset));
488 return (invert_enable) ? !val : val;
491 static void as3722_gpio_set(struct gpio_chip *chip, unsigned offset,
494 struct as3722_pctrl_info *as_pci = to_as_pci(chip);
495 struct as3722 *as3722 = as_pci->as3722;
500 ret = as3722_read(as3722, AS3722_GPIOn_CONTROL_REG(offset), &val);
503 "GPIO_CONTROL%d_REG read failed: %d\n", offset, ret);
506 en_invert = !!(val & AS3722_GPIO_INV);
509 val = (en_invert) ? 0 : AS3722_GPIOn_SIGNAL(offset);
511 val = (en_invert) ? AS3722_GPIOn_SIGNAL(offset) : 0;
513 ret = as3722_update_bits(as3722, AS3722_GPIO_SIGNAL_OUT_REG,
514 AS3722_GPIOn_SIGNAL(offset), val);
517 "GPIO_SIGNAL_OUT_REG update failed: %d\n", ret);
520 static int as3722_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
522 return pinctrl_gpio_direction_input(chip->base + offset);
525 static int as3722_gpio_direction_output(struct gpio_chip *chip,
526 unsigned offset, int value)
528 as3722_gpio_set(chip, offset, value);
529 return pinctrl_gpio_direction_output(chip->base + offset);
532 static int as3722_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
534 struct as3722_pctrl_info *as_pci = to_as_pci(chip);
536 return as3722_irq_get_virq(as_pci->as3722, offset);
539 static int as3722_gpio_request(struct gpio_chip *chip, unsigned offset)
541 return pinctrl_request_gpio(chip->base + offset);
544 static void as3722_gpio_free(struct gpio_chip *chip, unsigned offset)
546 pinctrl_free_gpio(chip->base + offset);
549 static const struct gpio_chip as3722_gpio_chip = {
550 .label = "as3722-gpio",
551 .owner = THIS_MODULE,
552 .request = as3722_gpio_request,
553 .free = as3722_gpio_free,
554 .get = as3722_gpio_get,
555 .set = as3722_gpio_set,
556 .direction_input = as3722_gpio_direction_input,
557 .direction_output = as3722_gpio_direction_output,
558 .to_irq = as3722_gpio_to_irq,
560 .ngpio = AS3722_PIN_NUM,
564 static int as3722_pinctrl_probe(struct platform_device *pdev)
566 struct as3722_pctrl_info *as_pci;
569 as_pci = devm_kzalloc(&pdev->dev, sizeof(*as_pci), GFP_KERNEL);
573 as_pci->dev = &pdev->dev;
574 as_pci->dev->of_node = pdev->dev.parent->of_node;
575 as_pci->as3722 = dev_get_drvdata(pdev->dev.parent);
576 platform_set_drvdata(pdev, as_pci);
578 as_pci->pins = as3722_pins_desc;
579 as_pci->num_pins = ARRAY_SIZE(as3722_pins_desc);
580 as_pci->functions = as3722_pin_function;
581 as_pci->num_functions = ARRAY_SIZE(as3722_pin_function);
582 as_pci->pin_groups = as3722_pingroups;
583 as_pci->num_pin_groups = ARRAY_SIZE(as3722_pingroups);
584 as3722_pinctrl_desc.name = dev_name(&pdev->dev);
585 as3722_pinctrl_desc.pins = as3722_pins_desc;
586 as3722_pinctrl_desc.npins = ARRAY_SIZE(as3722_pins_desc);
587 as_pci->pctl = pinctrl_register(&as3722_pinctrl_desc,
590 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
594 as_pci->gpio_chip = as3722_gpio_chip;
595 as_pci->gpio_chip.dev = &pdev->dev;
596 as_pci->gpio_chip.of_node = pdev->dev.parent->of_node;
597 ret = gpiochip_add(&as_pci->gpio_chip);
599 dev_err(&pdev->dev, "Couldn't register gpiochip, %d\n", ret);
603 ret = gpiochip_add_pin_range(&as_pci->gpio_chip, dev_name(&pdev->dev),
604 0, 0, AS3722_PIN_NUM);
606 dev_err(&pdev->dev, "Couldn't add pin range, %d\n", ret);
613 gpiochip_remove(&as_pci->gpio_chip);
615 pinctrl_unregister(as_pci->pctl);
619 static int as3722_pinctrl_remove(struct platform_device *pdev)
621 struct as3722_pctrl_info *as_pci = platform_get_drvdata(pdev);
623 gpiochip_remove(&as_pci->gpio_chip);
624 pinctrl_unregister(as_pci->pctl);
628 static const struct of_device_id as3722_pinctrl_of_match[] = {
629 { .compatible = "ams,as3722-pinctrl", },
632 MODULE_DEVICE_TABLE(of, as3722_pinctrl_of_match);
634 static struct platform_driver as3722_pinctrl_driver = {
636 .name = "as3722-pinctrl",
637 .of_match_table = as3722_pinctrl_of_match,
639 .probe = as3722_pinctrl_probe,
640 .remove = as3722_pinctrl_remove,
642 module_platform_driver(as3722_pinctrl_driver);
644 MODULE_ALIAS("platform:as3722-pinctrl");
645 MODULE_DESCRIPTION("AS3722 pin control and GPIO driver");
646 MODULE_AUTHOR("Laxman Dewangan<ldewangan@nvidia.com>");
647 MODULE_LICENSE("GPL v2");