blob: 02c075cc010b10b5925c665bababc51daf1458e9 [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleijae6b4d82011-10-19 18:14:33 +02002/*
3 * Core driver for the pin config portions of the pin control subsystem
4 *
5 * Copyright (C) 2011 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 *
8 * Author: Linus Walleij <linus.walleij@linaro.org>
Linus Walleijae6b4d82011-10-19 18:14:33 +02009 */
10#define pr_fmt(fmt) "pinconfig core: " fmt
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/slab.h>
17#include <linux/debugfs.h>
18#include <linux/seq_file.h>
19#include <linux/pinctrl/machine.h>
20#include <linux/pinctrl/pinctrl.h>
21#include <linux/pinctrl/pinconf.h>
22#include "core.h"
23#include "pinconf.h"
24
Stephen Warren2b694252012-02-19 23:45:46 -070025int pinconf_check_ops(struct pinctrl_dev *pctldev)
26{
27 const struct pinconf_ops *ops = pctldev->desc->confops;
28
Stephen Warren2b694252012-02-19 23:45:46 -070029 /* We have to be able to config the pins in SOME way */
John Crispinad6e1102012-04-26 16:47:11 +020030 if (!ops->pin_config_set && !ops->pin_config_group_set) {
31 dev_err(pctldev->dev,
32 "pinconf has to be able to set a pins config\n");
Stephen Warren2b694252012-02-19 23:45:46 -070033 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +020034 }
Stephen Warren2b694252012-02-19 23:45:46 -070035 return 0;
36}
37
Masahiro Yamada3f713b72017-08-04 11:22:31 +090038int pinconf_validate_map(const struct pinctrl_map *map, int i)
Stephen Warren1e2082b2012-03-02 13:05:48 -070039{
40 if (!map->data.configs.group_or_pin) {
41 pr_err("failed to register map %s (%d): no group/pin given\n",
42 map->name, i);
43 return -EINVAL;
44 }
45
Dong Aishengc95df2d2012-05-14 19:06:36 +080046 if (!map->data.configs.num_configs ||
Stephen Warren1e2082b2012-03-02 13:05:48 -070047 !map->data.configs.configs) {
Dong Aishengc95df2d2012-05-14 19:06:36 +080048 pr_err("failed to register map %s (%d): no configs given\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -070049 map->name, i);
50 return -EINVAL;
51 }
52
53 return 0;
54}
55
Linus Walleij394349f2011-11-24 18:27:15 +010056int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020057 unsigned long *config)
58{
59 const struct pinconf_ops *ops = pctldev->desc->confops;
60
61 if (!ops || !ops->pin_config_get) {
Masahiro Yamadaca67f102015-07-30 17:27:44 +090062 dev_dbg(pctldev->dev,
63 "cannot get pin configuration, .pin_config_get missing in driver\n");
Alexandre Bellonic4206192013-12-09 11:38:29 +010064 return -ENOTSUPP;
Linus Walleijae6b4d82011-10-19 18:14:33 +020065 }
66
67 return ops->pin_config_get(pctldev, pin, config);
68}
69
Stephen Warren43699de2011-12-15 16:57:17 -070070int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +020071 unsigned long *config)
72{
Stephen Warren43699de2011-12-15 16:57:17 -070073 struct pinctrl_dev *pctldev;
74 const struct pinconf_ops *ops;
Stephen Warren57b676f2012-03-02 13:05:44 -070075 int selector, ret;
76
Linus Walleij9dfac4f2012-02-01 18:02:47 +010077 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -070078 if (!pctldev) {
79 ret = -EINVAL;
Patrice Chotard42fed7b2013-04-11 11:01:27 +020080 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -070081 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +020082
83 mutex_lock(&pctldev->mutex);
84
Stephen Warren43699de2011-12-15 16:57:17 -070085 ops = pctldev->desc->confops;
86
Linus Walleijae6b4d82011-10-19 18:14:33 +020087 if (!ops || !ops->pin_config_group_get) {
Markus Elfringe4d03052017-05-02 10:22:47 +020088 dev_dbg(pctldev->dev,
89 "cannot get configuration for pin group, missing group config get function in driver\n");
Alexandre Bellonic4206192013-12-09 11:38:29 +010090 ret = -ENOTSUPP;
Stephen Warren57b676f2012-03-02 13:05:44 -070091 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +020092 }
93
94 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -070095 if (selector < 0) {
96 ret = selector;
97 goto unlock;
98 }
Linus Walleijae6b4d82011-10-19 18:14:33 +020099
Stephen Warren57b676f2012-03-02 13:05:44 -0700100 ret = ops->pin_config_group_get(pctldev, selector, config);
101
102unlock:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200103 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700104 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200105}
Linus Walleijae6b4d82011-10-19 18:14:33 +0200106
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900107int pinconf_map_to_setting(const struct pinctrl_map *map,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700108 struct pinctrl_setting *setting)
109{
110 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleij70b36372012-03-12 21:38:29 +0100111 int pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700112
113 switch (setting->type) {
114 case PIN_MAP_TYPE_CONFIGS_PIN:
Linus Walleij70b36372012-03-12 21:38:29 +0100115 pin = pin_get_from_name(pctldev,
116 map->data.configs.group_or_pin);
117 if (pin < 0) {
118 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
119 map->data.configs.group_or_pin);
120 return pin;
121 }
122 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700123 break;
124 case PIN_MAP_TYPE_CONFIGS_GROUP:
Linus Walleij70b36372012-03-12 21:38:29 +0100125 pin = pinctrl_get_group_selector(pctldev,
126 map->data.configs.group_or_pin);
127 if (pin < 0) {
128 dev_err(pctldev->dev, "could not map group config for \"%s\"",
129 map->data.configs.group_or_pin);
130 return pin;
131 }
132 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700133 break;
134 default:
135 return -EINVAL;
136 }
137
138 setting->data.configs.num_configs = map->data.configs.num_configs;
139 setting->data.configs.configs = map->data.configs.configs;
140
141 return 0;
142}
143
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900144void pinconf_free_setting(const struct pinctrl_setting *setting)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700145{
146}
147
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900148int pinconf_apply_setting(const struct pinctrl_setting *setting)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700149{
150 struct pinctrl_dev *pctldev = setting->pctldev;
151 const struct pinconf_ops *ops = pctldev->desc->confops;
Sherman Yin03b054e2013-08-27 11:32:12 -0700152 int ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700153
154 if (!ops) {
155 dev_err(pctldev->dev, "missing confops\n");
156 return -EINVAL;
157 }
158
159 switch (setting->type) {
160 case PIN_MAP_TYPE_CONFIGS_PIN:
161 if (!ops->pin_config_set) {
162 dev_err(pctldev->dev, "missing pin_config_set op\n");
163 return -EINVAL;
164 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700165 ret = ops->pin_config_set(pctldev,
166 setting->data.configs.group_or_pin,
167 setting->data.configs.configs,
168 setting->data.configs.num_configs);
169 if (ret < 0) {
170 dev_err(pctldev->dev,
171 "pin_config_set op failed for pin %d\n",
172 setting->data.configs.group_or_pin);
173 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700174 }
175 break;
176 case PIN_MAP_TYPE_CONFIGS_GROUP:
177 if (!ops->pin_config_group_set) {
178 dev_err(pctldev->dev,
179 "missing pin_config_group_set op\n");
180 return -EINVAL;
181 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700182 ret = ops->pin_config_group_set(pctldev,
183 setting->data.configs.group_or_pin,
184 setting->data.configs.configs,
185 setting->data.configs.num_configs);
186 if (ret < 0) {
187 dev_err(pctldev->dev,
188 "pin_config_group_set op failed for group %d\n",
189 setting->data.configs.group_or_pin);
190 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700191 }
192 break;
193 default:
194 return -EINVAL;
195 }
196
197 return 0;
198}
199
Mika Westerberg15381bc2017-01-23 15:34:33 +0300200int pinconf_set_config(struct pinctrl_dev *pctldev, unsigned pin,
201 unsigned long *configs, size_t nconfigs)
202{
203 const struct pinconf_ops *ops;
204
205 ops = pctldev->desc->confops;
Masahiro Yamada17a51242017-08-04 11:59:32 +0900206 if (!ops || !ops->pin_config_set)
Mika Westerberg15381bc2017-01-23 15:34:33 +0300207 return -ENOTSUPP;
208
209 return ops->pin_config_set(pctldev, pin, configs, nconfigs);
210}
211
Linus Walleijae6b4d82011-10-19 18:14:33 +0200212#ifdef CONFIG_DEBUG_FS
213
kbuild test robot6de52c12015-07-17 21:37:09 +0800214static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
Jon Hunterd96310a2015-07-14 11:17:59 +0100215 unsigned long *configs, unsigned num_configs)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700216{
Stephen Warren6cb41582012-04-13 10:49:06 -0600217 const struct pinconf_ops *confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700218 int i;
219
Stephen Warren6cb41582012-04-13 10:49:06 -0600220 if (pctldev)
221 confops = pctldev->desc->confops;
222 else
223 confops = NULL;
224
Jon Hunterd96310a2015-07-14 11:17:59 +0100225 for (i = 0; i < num_configs; i++) {
226 seq_puts(s, "config ");
227 if (confops && confops->pin_config_config_dbg_show)
228 confops->pin_config_config_dbg_show(pctldev, s,
229 configs[i]);
230 else
231 seq_printf(s, "%08lx", configs[i]);
Markus Elfring47352a62017-05-01 22:24:29 +0200232 seq_putc(s, '\n');
Jon Hunterd96310a2015-07-14 11:17:59 +0100233 }
234}
235
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900236void pinconf_show_map(struct seq_file *s, const struct pinctrl_map *map)
Jon Hunterd96310a2015-07-14 11:17:59 +0100237{
238 struct pinctrl_dev *pctldev;
239
240 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
241
Stephen Warren1e2082b2012-03-02 13:05:48 -0700242 switch (map->type) {
243 case PIN_MAP_TYPE_CONFIGS_PIN:
Markus Elfringde2eae22017-05-02 09:52:50 +0200244 seq_puts(s, "pin ");
Stephen Warren1e2082b2012-03-02 13:05:48 -0700245 break;
246 case PIN_MAP_TYPE_CONFIGS_GROUP:
Markus Elfringde2eae22017-05-02 09:52:50 +0200247 seq_puts(s, "group ");
Stephen Warren1e2082b2012-03-02 13:05:48 -0700248 break;
249 default:
250 break;
251 }
252
253 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
254
Jon Hunterd96310a2015-07-14 11:17:59 +0100255 pinconf_show_config(s, pctldev, map->data.configs.configs,
256 map->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700257}
258
259void pinconf_show_setting(struct seq_file *s,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900260 const struct pinctrl_setting *setting)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700261{
262 struct pinctrl_dev *pctldev = setting->pctldev;
263 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
264 struct pin_desc *desc;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700265
266 switch (setting->type) {
267 case PIN_MAP_TYPE_CONFIGS_PIN:
268 desc = pin_desc_get(setting->pctldev,
269 setting->data.configs.group_or_pin);
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900270 seq_printf(s, "pin %s (%d)", desc->name,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700271 setting->data.configs.group_or_pin);
272 break;
273 case PIN_MAP_TYPE_CONFIGS_GROUP:
274 seq_printf(s, "group %s (%d)",
275 pctlops->get_group_name(pctldev,
276 setting->data.configs.group_or_pin),
277 setting->data.configs.group_or_pin);
278 break;
279 default:
280 break;
281 }
282
283 /*
Andy Shevchenko3ec440e2017-02-28 16:59:56 +0200284 * FIXME: We should really get the pin controller to dump the config
Stephen Warren1e2082b2012-03-02 13:05:48 -0700285 * values, so they can be decoded to something meaningful.
286 */
Jon Hunterd96310a2015-07-14 11:17:59 +0100287 pinconf_show_config(s, pctldev, setting->data.configs.configs,
288 setting->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700289}
290
Linus Walleijae6b4d82011-10-19 18:14:33 +0200291static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
292 struct seq_file *s, int pin)
293{
294 const struct pinconf_ops *ops = pctldev->desc->confops;
295
Linus Walleij394349f2011-11-24 18:27:15 +0100296 /* no-op when not using generic pin config */
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800297 pinconf_generic_dump_pins(pctldev, s, NULL, pin);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200298 if (ops && ops->pin_config_dbg_show)
299 ops->pin_config_dbg_show(pctldev, s, pin);
300}
301
302static int pinconf_pins_show(struct seq_file *s, void *what)
303{
304 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900305 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200306
307 seq_puts(s, "Pin config settings per pin\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800308 seq_puts(s, "Format: pin (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200309
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200310 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700311
Chanho Park706e8522012-01-03 16:47:50 +0900312 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700313 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200314 struct pin_desc *desc;
315
Chanho Park706e8522012-01-03 16:47:50 +0900316 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200317 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900318 /* Skip if we cannot search the pin */
Markus Elfring76ce37f2017-05-02 10:01:57 +0200319 if (!desc)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200320 continue;
321
Masahiro Yamadaa672eb52016-05-25 15:37:27 +0900322 seq_printf(s, "pin %d (%s): ", pin, desc->name);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200323
324 pinconf_dump_pin(pctldev, s, pin);
Markus Elfring47352a62017-05-01 22:24:29 +0200325 seq_putc(s, '\n');
Linus Walleijae6b4d82011-10-19 18:14:33 +0200326 }
327
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200328 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700329
Linus Walleijae6b4d82011-10-19 18:14:33 +0200330 return 0;
331}
332
333static void pinconf_dump_group(struct pinctrl_dev *pctldev,
334 struct seq_file *s, unsigned selector,
335 const char *gname)
336{
337 const struct pinconf_ops *ops = pctldev->desc->confops;
338
Linus Walleij394349f2011-11-24 18:27:15 +0100339 /* no-op when not using generic pin config */
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800340 pinconf_generic_dump_pins(pctldev, s, gname, 0);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200341 if (ops && ops->pin_config_group_dbg_show)
342 ops->pin_config_group_dbg_show(pctldev, s, selector);
343}
344
345static int pinconf_groups_show(struct seq_file *s, void *what)
346{
347 struct pinctrl_dev *pctldev = s->private;
348 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530349 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200350 unsigned selector = 0;
351
Linus Walleijae6b4d82011-10-19 18:14:33 +0200352 seq_puts(s, "Pin config settings per pin group\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800353 seq_puts(s, "Format: group (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200354
Viresh Kumard1e90e92012-03-30 11:25:40 +0530355 while (selector < ngroups) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200356 const char *gname = pctlops->get_group_name(pctldev, selector);
357
Masahiro Yamadaa672eb52016-05-25 15:37:27 +0900358 seq_printf(s, "%u (%s): ", selector, gname);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200359 pinconf_dump_group(pctldev, s, selector, gname);
Markus Elfring47352a62017-05-01 22:24:29 +0200360 seq_putc(s, '\n');
Linus Walleijae6b4d82011-10-19 18:14:33 +0200361 selector++;
362 }
363
Linus Walleijae6b4d82011-10-19 18:14:33 +0200364 return 0;
365}
366
Yangtao Li0819dc72018-11-30 11:36:17 -0500367DEFINE_SHOW_ATTRIBUTE(pinconf_pins);
368DEFINE_SHOW_ATTRIBUTE(pinconf_groups);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200369
370void pinconf_init_device_debugfs(struct dentry *devroot,
371 struct pinctrl_dev *pctldev)
372{
373 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
Yangtao Li0819dc72018-11-30 11:36:17 -0500374 devroot, pctldev, &pinconf_pins_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200375 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
Yangtao Li0819dc72018-11-30 11:36:17 -0500376 devroot, pctldev, &pinconf_groups_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200377}
378
379#endif