^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Driver core interface to the pinctrl subsystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 ST-Ericsson SA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Written on behalf of Linaro for ST-Ericsson
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on bits of regulator core, gpio core and clk core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: Linus Walleij <linus.walleij@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pinctrl/devinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/pinctrl/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * pinctrl_bind_pins() - called by the device core before probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @dev: the device that is just about to probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int pinctrl_bind_pins(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (dev->of_node_reused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!dev->pins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) dev->pins->p = devm_pinctrl_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (IS_ERR(dev->pins->p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) dev_dbg(dev, "no pinctrl handle\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ret = PTR_ERR(dev->pins->p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) goto cleanup_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) PINCTRL_STATE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (IS_ERR(dev->pins->default_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) dev_dbg(dev, "no default pinctrl state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) goto cleanup_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) PINCTRL_STATE_INIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (IS_ERR(dev->pins->init_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Not supplying this state is perfectly legal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dev_dbg(dev, "no init pinctrl state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ret = pinctrl_select_state(dev->pins->p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) dev->pins->default_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) dev_dbg(dev, "failed to activate initial pinctrl state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) goto cleanup_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * If power management is enabled, we also look for the optional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * sleep and idle pin states, with semantics as defined in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * <linux/pinctrl/pinctrl-state.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) PINCTRL_STATE_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (IS_ERR(dev->pins->sleep_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* Not supplying this state is perfectly legal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dev_dbg(dev, "no sleep pinctrl state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) PINCTRL_STATE_IDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (IS_ERR(dev->pins->idle_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Not supplying this state is perfectly legal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) dev_dbg(dev, "no idle pinctrl state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * If no pinctrl handle or default state was found for this device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * let's explicitly free the pin container in the device, there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * no point in keeping it around.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) cleanup_get:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) devm_pinctrl_put(dev->pins->p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cleanup_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) devm_kfree(dev, dev->pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) dev->pins = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Return deferrals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (ret == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* Return serious errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ret == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* We ignore errors like -ENOENT meaning no pinctrl state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }