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