blob: 180b856e90ac2f9fe08f1e47bb0800c6880f85ae [file] [log] [blame]
Linus Walleij394349f2011-11-24 18:27:15 +01001/*
2 * Core driver for the generic pin config portions of the pin control subsystem
3 *
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 *
7 * Author: Linus Walleij <linus.walleij@linaro.org>
8 *
9 * License terms: GNU General Public License (GPL) version 2
10 */
11
12#define pr_fmt(fmt) "generic pinconfig core: " fmt
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/debugfs.h>
19#include <linux/seq_file.h>
20#include <linux/pinctrl/pinctrl.h>
21#include <linux/pinctrl/pinconf.h>
22#include <linux/pinctrl/pinconf-generic.h>
Heiko Stübner06c04002013-06-10 21:40:29 +020023#include <linux/of.h>
Linus Walleij394349f2011-11-24 18:27:15 +010024#include "core.h"
25#include "pinconf.h"
26
27#ifdef CONFIG_DEBUG_FS
28
29struct pin_config_item {
30 const enum pin_config_param param;
31 const char * const display;
32 const char * const format;
33};
34
35#define PCONFDUMP(a, b, c) { .param = a, .display = b, .format = c }
36
37struct pin_config_item conf_items[] = {
38 PCONFDUMP(PIN_CONFIG_BIAS_DISABLE, "input bias disabled", NULL),
39 PCONFDUMP(PIN_CONFIG_BIAS_HIGH_IMPEDANCE, "input bias high impedance", NULL),
James Hoganb9ae0452013-05-24 17:21:12 +010040 PCONFDUMP(PIN_CONFIG_BIAS_BUS_HOLD, "input bias bus hold", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010041 PCONFDUMP(PIN_CONFIG_BIAS_PULL_UP, "input bias pull up", NULL),
42 PCONFDUMP(PIN_CONFIG_BIAS_PULL_DOWN, "input bias pull down", NULL),
Heiko Stübnera4e478c2013-06-06 16:44:25 +020043 PCONFDUMP(PIN_CONFIG_BIAS_PULL_PIN_DEFAULT,
44 "input bias pull to pin specific state", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010045 PCONFDUMP(PIN_CONFIG_DRIVE_PUSH_PULL, "output drive push pull", NULL),
46 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_DRAIN, "output drive open drain", NULL),
47 PCONFDUMP(PIN_CONFIG_DRIVE_OPEN_SOURCE, "output drive open source", NULL),
James Hogan2dd15212013-05-24 17:21:11 +010048 PCONFDUMP(PIN_CONFIG_DRIVE_STRENGTH, "output drive strength", "mA"),
Haojian Zhuangeda8952b2013-02-12 01:10:57 +080049 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT_ENABLE, "input schmitt enabled", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010050 PCONFDUMP(PIN_CONFIG_INPUT_SCHMITT, "input schmitt trigger", NULL),
51 PCONFDUMP(PIN_CONFIG_INPUT_DEBOUNCE, "input debounce", "time units"),
52 PCONFDUMP(PIN_CONFIG_POWER_SOURCE, "pin power source", "selector"),
Haojian Zhuang8d08d962013-01-18 15:31:15 +080053 PCONFDUMP(PIN_CONFIG_SLEW_RATE, "slew rate", NULL),
Linus Walleij394349f2011-11-24 18:27:15 +010054 PCONFDUMP(PIN_CONFIG_LOW_POWER_MODE, "pin low power", "mode"),
Linus Walleij50ec39a2013-01-04 17:57:40 +010055 PCONFDUMP(PIN_CONFIG_OUTPUT, "pin output", "level"),
Linus Walleij394349f2011-11-24 18:27:15 +010056};
57
58void pinconf_generic_dump_pin(struct pinctrl_dev *pctldev,
59 struct seq_file *s, unsigned pin)
60{
61 const struct pinconf_ops *ops = pctldev->desc->confops;
62 int i;
63
64 if (!ops->is_generic)
65 return;
66
67 for(i = 0; i < ARRAY_SIZE(conf_items); i++) {
68 unsigned long config;
69 int ret;
70
71 /* We want to check out this parameter */
72 config = pinconf_to_config_packed(conf_items[i].param, 0);
73 ret = pin_config_get_for_pin(pctldev, pin, &config);
74 /* These are legal errors */
75 if (ret == -EINVAL || ret == -ENOTSUPP)
76 continue;
77 if (ret) {
78 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
79 continue;
80 }
81 /* Space between multiple configs */
82 seq_puts(s, " ");
83 seq_puts(s, conf_items[i].display);
84 /* Print unit if available */
85 if (conf_items[i].format &&
86 pinconf_to_config_argument(config) != 0)
87 seq_printf(s, " (%u %s)",
88 pinconf_to_config_argument(config),
89 conf_items[i].format);
90 }
91}
92
93void pinconf_generic_dump_group(struct pinctrl_dev *pctldev,
94 struct seq_file *s, const char *gname)
95{
96 const struct pinconf_ops *ops = pctldev->desc->confops;
97 int i;
98
99 if (!ops->is_generic)
100 return;
101
102 for(i = 0; i < ARRAY_SIZE(conf_items); i++) {
103 unsigned long config;
104 int ret;
105
106 /* We want to check out this parameter */
107 config = pinconf_to_config_packed(conf_items[i].param, 0);
108 ret = pin_config_group_get(dev_name(pctldev->dev), gname,
109 &config);
110 /* These are legal errors */
111 if (ret == -EINVAL || ret == -ENOTSUPP)
112 continue;
113 if (ret) {
114 seq_printf(s, "ERROR READING CONFIG SETTING %d ", i);
115 continue;
116 }
117 /* Space between multiple configs */
118 seq_puts(s, " ");
119 seq_puts(s, conf_items[i].display);
120 /* Print unit if available */
121 if (conf_items[i].format && config != 0)
122 seq_printf(s, " (%u %s)",
123 pinconf_to_config_argument(config),
124 conf_items[i].format);
125 }
126}
127
128#endif
Heiko Stübner06c04002013-06-10 21:40:29 +0200129
130#ifdef CONFIG_OF
131struct pinconf_generic_dt_params {
132 const char * const property;
133 enum pin_config_param param;
134 u32 default_value;
135};
136
137static struct pinconf_generic_dt_params dt_params[] = {
138 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
139 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
140 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
Heiko Stübnerb87b8142013-06-14 17:42:49 +0200141 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
142 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
143 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
Heiko Stübner06c04002013-06-10 21:40:29 +0200144 { "drive-push-pull", PIN_CONFIG_DRIVE_PUSH_PULL, 0 },
145 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
146 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
147 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
148 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
149 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
150 { "input-schmitt", PIN_CONFIG_INPUT_SCHMITT, 0 },
151 { "input-debounce", PIN_CONFIG_INPUT_DEBOUNCE, 0 },
152 { "power-source", PIN_CONFIG_POWER_SOURCE, 0 },
153 { "slew-rate", PIN_CONFIG_SLEW_RATE, 0 },
Heiko Stübnerb87b8142013-06-14 17:42:49 +0200154 { "low-power-enable", PIN_CONFIG_LOW_POWER_MODE, 1 },
155 { "low-power-disable", PIN_CONFIG_LOW_POWER_MODE, 0 },
Heiko Stübner06c04002013-06-10 21:40:29 +0200156 { "output-low", PIN_CONFIG_OUTPUT, 0, },
157 { "output-high", PIN_CONFIG_OUTPUT, 1, },
158};
159
160/**
161 * pinconf_generic_parse_dt_config()
162 * parse the config properties into generic pinconfig values.
163 * @np: node containing the pinconfig properties
164 * @configs: array with nconfigs entries containing the generic pinconf values
165 * @nconfigs: umber of configurations
166 */
167int pinconf_generic_parse_dt_config(struct device_node *np,
168 unsigned long **configs,
169 unsigned int *nconfigs)
170{
Heiko Stübner162e8082013-06-14 17:43:55 +0200171 unsigned long *cfg;
Heiko Stübner06c04002013-06-10 21:40:29 +0200172 unsigned int ncfg = 0;
173 int ret;
174 int i;
175 u32 val;
176
177 if (!np)
178 return -EINVAL;
179
Heiko Stübner162e8082013-06-14 17:43:55 +0200180 /* allocate a temporary array big enough to hold one of each option */
181 cfg = kzalloc(sizeof(*cfg) * ARRAY_SIZE(dt_params), GFP_KERNEL);
182 if (!cfg)
183 return -ENOMEM;
184
Heiko Stübner06c04002013-06-10 21:40:29 +0200185 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
186 struct pinconf_generic_dt_params *par = &dt_params[i];
187 ret = of_property_read_u32(np, par->property, &val);
188
189 /* property not found */
190 if (ret == -EINVAL)
191 continue;
192
193 /* use default value, when no value is specified */
194 if (ret)
195 val = par->default_value;
196
197 pr_debug("found %s with value %u\n", par->property, val);
198 cfg[ncfg] = pinconf_to_config_packed(par->param, val);
199 ncfg++;
200 }
201
Heiko Stübner162e8082013-06-14 17:43:55 +0200202 ret = 0;
203
Heiko Stübner829a05d2013-06-14 17:43:21 +0200204 /* no configs found at all */
205 if (ncfg == 0) {
206 *configs = NULL;
207 *nconfigs = 0;
Heiko Stübner162e8082013-06-14 17:43:55 +0200208 goto out;
Heiko Stübner829a05d2013-06-14 17:43:21 +0200209 }
210
Heiko Stübner06c04002013-06-10 21:40:29 +0200211 /*
212 * Now limit the number of configs to the real number of
213 * found properties.
214 */
215 *configs = kzalloc(ncfg * sizeof(unsigned long), GFP_KERNEL);
Heiko Stübner162e8082013-06-14 17:43:55 +0200216 if (!*configs) {
217 ret = -ENOMEM;
218 goto out;
219 }
Heiko Stübner06c04002013-06-10 21:40:29 +0200220
Heiko Stübner162e8082013-06-14 17:43:55 +0200221 memcpy(*configs, cfg, ncfg * sizeof(unsigned long));
Heiko Stübner06c04002013-06-10 21:40:29 +0200222 *nconfigs = ncfg;
Heiko Stübner162e8082013-06-14 17:43:55 +0200223
224out:
225 kfree(cfg);
226 return ret;
Heiko Stübner06c04002013-06-10 21:40:29 +0200227}
228#endif