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