blob: 2c7229380f081a998ceb838c95bc14b70056462f [file] [log] [blame]
Linus Walleijae6b4d82011-10-19 18:14:33 +02001/*
2 * Core driver for the 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#define pr_fmt(fmt) "pinconfig core: " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.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>
Laurent Meunierf07512e2013-04-18 10:48:07 +020020#include <linux/uaccess.h>
Linus Walleijae6b4d82011-10-19 18:14:33 +020021#include <linux/pinctrl/machine.h>
22#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinconf.h>
24#include "core.h"
25#include "pinconf.h"
26
Stephen Warren2b694252012-02-19 23:45:46 -070027int pinconf_check_ops(struct pinctrl_dev *pctldev)
28{
29 const struct pinconf_ops *ops = pctldev->desc->confops;
30
Stephen Warren2b694252012-02-19 23:45:46 -070031 /* We have to be able to config the pins in SOME way */
John Crispinad6e1102012-04-26 16:47:11 +020032 if (!ops->pin_config_set && !ops->pin_config_group_set) {
33 dev_err(pctldev->dev,
34 "pinconf has to be able to set a pins config\n");
Stephen Warren2b694252012-02-19 23:45:46 -070035 return -EINVAL;
John Crispinad6e1102012-04-26 16:47:11 +020036 }
Stephen Warren2b694252012-02-19 23:45:46 -070037 return 0;
38}
39
Masahiro Yamada3f713b72017-08-04 11:22:31 +090040int pinconf_validate_map(const struct pinctrl_map *map, int i)
Stephen Warren1e2082b2012-03-02 13:05:48 -070041{
42 if (!map->data.configs.group_or_pin) {
43 pr_err("failed to register map %s (%d): no group/pin given\n",
44 map->name, i);
45 return -EINVAL;
46 }
47
Dong Aishengc95df2d2012-05-14 19:06:36 +080048 if (!map->data.configs.num_configs ||
Stephen Warren1e2082b2012-03-02 13:05:48 -070049 !map->data.configs.configs) {
Dong Aishengc95df2d2012-05-14 19:06:36 +080050 pr_err("failed to register map %s (%d): no configs given\n",
Stephen Warren1e2082b2012-03-02 13:05:48 -070051 map->name, i);
52 return -EINVAL;
53 }
54
55 return 0;
56}
57
Linus Walleij394349f2011-11-24 18:27:15 +010058int pin_config_get_for_pin(struct pinctrl_dev *pctldev, unsigned pin,
Linus Walleijae6b4d82011-10-19 18:14:33 +020059 unsigned long *config)
60{
61 const struct pinconf_ops *ops = pctldev->desc->confops;
62
63 if (!ops || !ops->pin_config_get) {
Masahiro Yamadaca67f102015-07-30 17:27:44 +090064 dev_dbg(pctldev->dev,
65 "cannot get pin configuration, .pin_config_get missing in driver\n");
Alexandre Bellonic4206192013-12-09 11:38:29 +010066 return -ENOTSUPP;
Linus Walleijae6b4d82011-10-19 18:14:33 +020067 }
68
69 return ops->pin_config_get(pctldev, pin, config);
70}
71
Stephen Warren43699de2011-12-15 16:57:17 -070072int pin_config_group_get(const char *dev_name, const char *pin_group,
Linus Walleijae6b4d82011-10-19 18:14:33 +020073 unsigned long *config)
74{
Stephen Warren43699de2011-12-15 16:57:17 -070075 struct pinctrl_dev *pctldev;
76 const struct pinconf_ops *ops;
Stephen Warren57b676f2012-03-02 13:05:44 -070077 int selector, ret;
78
Linus Walleij9dfac4f2012-02-01 18:02:47 +010079 pctldev = get_pinctrl_dev_from_devname(dev_name);
Stephen Warren57b676f2012-03-02 13:05:44 -070080 if (!pctldev) {
81 ret = -EINVAL;
Patrice Chotard42fed7b2013-04-11 11:01:27 +020082 return ret;
Stephen Warren57b676f2012-03-02 13:05:44 -070083 }
Patrice Chotard42fed7b2013-04-11 11:01:27 +020084
85 mutex_lock(&pctldev->mutex);
86
Stephen Warren43699de2011-12-15 16:57:17 -070087 ops = pctldev->desc->confops;
88
Linus Walleijae6b4d82011-10-19 18:14:33 +020089 if (!ops || !ops->pin_config_group_get) {
Markus Elfringe4d03052017-05-02 10:22:47 +020090 dev_dbg(pctldev->dev,
91 "cannot get configuration for pin group, missing group config get function in driver\n");
Alexandre Bellonic4206192013-12-09 11:38:29 +010092 ret = -ENOTSUPP;
Stephen Warren57b676f2012-03-02 13:05:44 -070093 goto unlock;
Linus Walleijae6b4d82011-10-19 18:14:33 +020094 }
95
96 selector = pinctrl_get_group_selector(pctldev, pin_group);
Stephen Warren57b676f2012-03-02 13:05:44 -070097 if (selector < 0) {
98 ret = selector;
99 goto unlock;
100 }
Linus Walleijae6b4d82011-10-19 18:14:33 +0200101
Stephen Warren57b676f2012-03-02 13:05:44 -0700102 ret = ops->pin_config_group_get(pctldev, selector, config);
103
104unlock:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200105 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700106 return ret;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200107}
Linus Walleijae6b4d82011-10-19 18:14:33 +0200108
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900109int pinconf_map_to_setting(const struct pinctrl_map *map,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700110 struct pinctrl_setting *setting)
111{
112 struct pinctrl_dev *pctldev = setting->pctldev;
Linus Walleij70b36372012-03-12 21:38:29 +0100113 int pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700114
115 switch (setting->type) {
116 case PIN_MAP_TYPE_CONFIGS_PIN:
Linus Walleij70b36372012-03-12 21:38:29 +0100117 pin = pin_get_from_name(pctldev,
118 map->data.configs.group_or_pin);
119 if (pin < 0) {
120 dev_err(pctldev->dev, "could not map pin config for \"%s\"",
121 map->data.configs.group_or_pin);
122 return pin;
123 }
124 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700125 break;
126 case PIN_MAP_TYPE_CONFIGS_GROUP:
Linus Walleij70b36372012-03-12 21:38:29 +0100127 pin = pinctrl_get_group_selector(pctldev,
128 map->data.configs.group_or_pin);
129 if (pin < 0) {
130 dev_err(pctldev->dev, "could not map group config for \"%s\"",
131 map->data.configs.group_or_pin);
132 return pin;
133 }
134 setting->data.configs.group_or_pin = pin;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700135 break;
136 default:
137 return -EINVAL;
138 }
139
140 setting->data.configs.num_configs = map->data.configs.num_configs;
141 setting->data.configs.configs = map->data.configs.configs;
142
143 return 0;
144}
145
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900146void pinconf_free_setting(const struct pinctrl_setting *setting)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700147{
148}
149
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900150int pinconf_apply_setting(const struct pinctrl_setting *setting)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700151{
152 struct pinctrl_dev *pctldev = setting->pctldev;
153 const struct pinconf_ops *ops = pctldev->desc->confops;
Sherman Yin03b054e2013-08-27 11:32:12 -0700154 int ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700155
156 if (!ops) {
157 dev_err(pctldev->dev, "missing confops\n");
158 return -EINVAL;
159 }
160
161 switch (setting->type) {
162 case PIN_MAP_TYPE_CONFIGS_PIN:
163 if (!ops->pin_config_set) {
164 dev_err(pctldev->dev, "missing pin_config_set op\n");
165 return -EINVAL;
166 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700167 ret = ops->pin_config_set(pctldev,
168 setting->data.configs.group_or_pin,
169 setting->data.configs.configs,
170 setting->data.configs.num_configs);
171 if (ret < 0) {
172 dev_err(pctldev->dev,
173 "pin_config_set op failed for pin %d\n",
174 setting->data.configs.group_or_pin);
175 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700176 }
177 break;
178 case PIN_MAP_TYPE_CONFIGS_GROUP:
179 if (!ops->pin_config_group_set) {
180 dev_err(pctldev->dev,
181 "missing pin_config_group_set op\n");
182 return -EINVAL;
183 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700184 ret = ops->pin_config_group_set(pctldev,
185 setting->data.configs.group_or_pin,
186 setting->data.configs.configs,
187 setting->data.configs.num_configs);
188 if (ret < 0) {
189 dev_err(pctldev->dev,
190 "pin_config_group_set op failed for group %d\n",
191 setting->data.configs.group_or_pin);
192 return ret;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700193 }
194 break;
195 default:
196 return -EINVAL;
197 }
198
199 return 0;
200}
201
Mika Westerberg15381bc2017-01-23 15:34:33 +0300202int pinconf_set_config(struct pinctrl_dev *pctldev, unsigned pin,
203 unsigned long *configs, size_t nconfigs)
204{
205 const struct pinconf_ops *ops;
206
207 ops = pctldev->desc->confops;
Masahiro Yamada17a51242017-08-04 11:59:32 +0900208 if (!ops || !ops->pin_config_set)
Mika Westerberg15381bc2017-01-23 15:34:33 +0300209 return -ENOTSUPP;
210
211 return ops->pin_config_set(pctldev, pin, configs, nconfigs);
212}
213
Linus Walleijae6b4d82011-10-19 18:14:33 +0200214#ifdef CONFIG_DEBUG_FS
215
kbuild test robot6de52c12015-07-17 21:37:09 +0800216static void pinconf_show_config(struct seq_file *s, struct pinctrl_dev *pctldev,
Jon Hunterd96310a2015-07-14 11:17:59 +0100217 unsigned long *configs, unsigned num_configs)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700218{
Stephen Warren6cb41582012-04-13 10:49:06 -0600219 const struct pinconf_ops *confops;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700220 int i;
221
Stephen Warren6cb41582012-04-13 10:49:06 -0600222 if (pctldev)
223 confops = pctldev->desc->confops;
224 else
225 confops = NULL;
226
Jon Hunterd96310a2015-07-14 11:17:59 +0100227 for (i = 0; i < num_configs; i++) {
228 seq_puts(s, "config ");
229 if (confops && confops->pin_config_config_dbg_show)
230 confops->pin_config_config_dbg_show(pctldev, s,
231 configs[i]);
232 else
233 seq_printf(s, "%08lx", configs[i]);
Markus Elfring47352a62017-05-01 22:24:29 +0200234 seq_putc(s, '\n');
Jon Hunterd96310a2015-07-14 11:17:59 +0100235 }
236}
237
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900238void pinconf_show_map(struct seq_file *s, const struct pinctrl_map *map)
Jon Hunterd96310a2015-07-14 11:17:59 +0100239{
240 struct pinctrl_dev *pctldev;
241
242 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
243
Stephen Warren1e2082b2012-03-02 13:05:48 -0700244 switch (map->type) {
245 case PIN_MAP_TYPE_CONFIGS_PIN:
Markus Elfringde2eae22017-05-02 09:52:50 +0200246 seq_puts(s, "pin ");
Stephen Warren1e2082b2012-03-02 13:05:48 -0700247 break;
248 case PIN_MAP_TYPE_CONFIGS_GROUP:
Markus Elfringde2eae22017-05-02 09:52:50 +0200249 seq_puts(s, "group ");
Stephen Warren1e2082b2012-03-02 13:05:48 -0700250 break;
251 default:
252 break;
253 }
254
255 seq_printf(s, "%s\n", map->data.configs.group_or_pin);
256
Jon Hunterd96310a2015-07-14 11:17:59 +0100257 pinconf_show_config(s, pctldev, map->data.configs.configs,
258 map->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700259}
260
261void pinconf_show_setting(struct seq_file *s,
Masahiro Yamada3f713b72017-08-04 11:22:31 +0900262 const struct pinctrl_setting *setting)
Stephen Warren1e2082b2012-03-02 13:05:48 -0700263{
264 struct pinctrl_dev *pctldev = setting->pctldev;
265 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
266 struct pin_desc *desc;
Stephen Warren1e2082b2012-03-02 13:05:48 -0700267
268 switch (setting->type) {
269 case PIN_MAP_TYPE_CONFIGS_PIN:
270 desc = pin_desc_get(setting->pctldev,
271 setting->data.configs.group_or_pin);
Masahiro Yamadacf9d9942016-05-24 14:26:26 +0900272 seq_printf(s, "pin %s (%d)", desc->name,
Stephen Warren1e2082b2012-03-02 13:05:48 -0700273 setting->data.configs.group_or_pin);
274 break;
275 case PIN_MAP_TYPE_CONFIGS_GROUP:
276 seq_printf(s, "group %s (%d)",
277 pctlops->get_group_name(pctldev,
278 setting->data.configs.group_or_pin),
279 setting->data.configs.group_or_pin);
280 break;
281 default:
282 break;
283 }
284
285 /*
Andy Shevchenko3ec440e2017-02-28 16:59:56 +0200286 * FIXME: We should really get the pin controller to dump the config
Stephen Warren1e2082b2012-03-02 13:05:48 -0700287 * values, so they can be decoded to something meaningful.
288 */
Jon Hunterd96310a2015-07-14 11:17:59 +0100289 pinconf_show_config(s, pctldev, setting->data.configs.configs,
290 setting->data.configs.num_configs);
Stephen Warren1e2082b2012-03-02 13:05:48 -0700291}
292
Linus Walleijae6b4d82011-10-19 18:14:33 +0200293static void pinconf_dump_pin(struct pinctrl_dev *pctldev,
294 struct seq_file *s, int pin)
295{
296 const struct pinconf_ops *ops = pctldev->desc->confops;
297
Linus Walleij394349f2011-11-24 18:27:15 +0100298 /* no-op when not using generic pin config */
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800299 pinconf_generic_dump_pins(pctldev, s, NULL, pin);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200300 if (ops && ops->pin_config_dbg_show)
301 ops->pin_config_dbg_show(pctldev, s, pin);
302}
303
304static int pinconf_pins_show(struct seq_file *s, void *what)
305{
306 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900307 unsigned i, pin;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200308
309 seq_puts(s, "Pin config settings per pin\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800310 seq_puts(s, "Format: pin (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200311
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200312 mutex_lock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700313
Chanho Park706e8522012-01-03 16:47:50 +0900314 /* The pin number can be retrived from the pin controller descriptor */
Stephen Warren546edd82012-01-06 13:38:31 -0700315 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200316 struct pin_desc *desc;
317
Chanho Park706e8522012-01-03 16:47:50 +0900318 pin = pctldev->desc->pins[i].number;
Linus Walleijae6b4d82011-10-19 18:14:33 +0200319 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900320 /* Skip if we cannot search the pin */
Markus Elfring76ce37f2017-05-02 10:01:57 +0200321 if (!desc)
Linus Walleijae6b4d82011-10-19 18:14:33 +0200322 continue;
323
Masahiro Yamadaa672eb52016-05-25 15:37:27 +0900324 seq_printf(s, "pin %d (%s): ", pin, desc->name);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200325
326 pinconf_dump_pin(pctldev, s, pin);
Markus Elfring47352a62017-05-01 22:24:29 +0200327 seq_putc(s, '\n');
Linus Walleijae6b4d82011-10-19 18:14:33 +0200328 }
329
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200330 mutex_unlock(&pctldev->mutex);
Stephen Warren57b676f2012-03-02 13:05:44 -0700331
Linus Walleijae6b4d82011-10-19 18:14:33 +0200332 return 0;
333}
334
335static void pinconf_dump_group(struct pinctrl_dev *pctldev,
336 struct seq_file *s, unsigned selector,
337 const char *gname)
338{
339 const struct pinconf_ops *ops = pctldev->desc->confops;
340
Linus Walleij394349f2011-11-24 18:27:15 +0100341 /* no-op when not using generic pin config */
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800342 pinconf_generic_dump_pins(pctldev, s, gname, 0);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200343 if (ops && ops->pin_config_group_dbg_show)
344 ops->pin_config_group_dbg_show(pctldev, s, selector);
345}
346
347static int pinconf_groups_show(struct seq_file *s, void *what)
348{
349 struct pinctrl_dev *pctldev = s->private;
350 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Viresh Kumard1e90e92012-03-30 11:25:40 +0530351 unsigned ngroups = pctlops->get_groups_count(pctldev);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200352 unsigned selector = 0;
353
Linus Walleijae6b4d82011-10-19 18:14:33 +0200354 seq_puts(s, "Pin config settings per pin group\n");
Dong Aisheng2aeefe02012-04-16 22:07:24 +0800355 seq_puts(s, "Format: group (name): configs\n");
Linus Walleijae6b4d82011-10-19 18:14:33 +0200356
Viresh Kumard1e90e92012-03-30 11:25:40 +0530357 while (selector < ngroups) {
Linus Walleijae6b4d82011-10-19 18:14:33 +0200358 const char *gname = pctlops->get_group_name(pctldev, selector);
359
Masahiro Yamadaa672eb52016-05-25 15:37:27 +0900360 seq_printf(s, "%u (%s): ", selector, gname);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200361 pinconf_dump_group(pctldev, s, selector, gname);
Markus Elfring47352a62017-05-01 22:24:29 +0200362 seq_putc(s, '\n');
Linus Walleijae6b4d82011-10-19 18:14:33 +0200363 selector++;
364 }
365
Linus Walleijae6b4d82011-10-19 18:14:33 +0200366 return 0;
367}
368
Yangtao Li0819dc72018-11-30 11:36:17 -0500369DEFINE_SHOW_ATTRIBUTE(pinconf_pins);
370DEFINE_SHOW_ATTRIBUTE(pinconf_groups);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200371
Laurent Meunierf07512e2013-04-18 10:48:07 +0200372#define MAX_NAME_LEN 15
373
374struct dbg_cfg {
375 enum pinctrl_map_type map_type;
Markus Elfringe8c5d752017-05-02 10:32:19 +0200376 char dev_name[MAX_NAME_LEN + 1];
377 char state_name[MAX_NAME_LEN + 1];
378 char pin_name[MAX_NAME_LEN + 1];
Laurent Meunierf07512e2013-04-18 10:48:07 +0200379};
380
381/*
382 * Goal is to keep this structure as global in order to simply read the
383 * pinconf-config file after a write to check config is as expected
384 */
385static struct dbg_cfg pinconf_dbg_conf;
386
387/**
388 * pinconf_dbg_config_print() - display the pinctrl config from the pinctrl
389 * map, of the dev/pin/state that was last written to pinconf-config file.
390 * @s: string filled in with config description
391 * @d: not used
392 */
393static int pinconf_dbg_config_print(struct seq_file *s, void *d)
394{
395 struct pinctrl_maps *maps_node;
396 const struct pinctrl_map *map;
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100397 const struct pinctrl_map *found = NULL;
398 struct pinctrl_dev *pctldev;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200399 struct dbg_cfg *dbg = &pinconf_dbg_conf;
Laurent Meunierd99c8052015-10-30 15:15:51 +0100400 int i;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200401
Linus Walleija3862672013-05-27 15:53:32 +0200402 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200403
404 /* Parse the pinctrl map and look for the elected pin/state */
405 for_each_maps(maps_node, i, map) {
406 if (map->type != dbg->map_type)
407 continue;
408 if (strcmp(map->dev_name, dbg->dev_name))
409 continue;
410 if (strcmp(map->name, dbg->state_name))
411 continue;
412
Laurent Meunierd99c8052015-10-30 15:15:51 +0100413 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
414 /* We found the right pin */
415 found = map;
416 break;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200417 }
418 }
419
420 if (!found) {
421 seq_printf(s, "No config found for dev/state/pin, expected:\n");
422 seq_printf(s, "Searched dev:%s\n", dbg->dev_name);
423 seq_printf(s, "Searched state:%s\n", dbg->state_name);
424 seq_printf(s, "Searched pin:%s\n", dbg->pin_name);
425 seq_printf(s, "Use: modify config_pin <devname> "\
426 "<state> <pinname> <value>\n");
427 goto exit;
428 }
429
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100430 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
Jon Hunterd96310a2015-07-14 11:17:59 +0100431 seq_printf(s, "Dev %s has config of %s in state %s:\n",
432 dbg->dev_name, dbg->pin_name, dbg->state_name);
433 pinconf_show_config(s, pctldev, found->data.configs.configs,
434 found->data.configs.num_configs);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200435
436exit:
Linus Walleija3862672013-05-27 15:53:32 +0200437 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200438
439 return 0;
440}
441
442/**
443 * pinconf_dbg_config_write() - modify the pinctrl config in the pinctrl
444 * map, of a dev/pin/state entry based on user entries to pinconf-config
445 * @user_buf: contains the modification request with expected format:
Jon Hunter75629982015-07-14 11:17:58 +0100446 * modify <config> <devicename> <state> <name> <newvalue>
Laurent Meunierf07512e2013-04-18 10:48:07 +0200447 * modify is literal string, alternatives like add/delete not supported yet
Jon Hunter75629982015-07-14 11:17:58 +0100448 * <config> is the configuration to be changed. Supported configs are
449 * "config_pin" or "config_group", alternatives like config_mux are not
450 * supported yet.
451 * <devicename> <state> <name> are values that should match the pinctrl-maps
Andy Shevchenko3ec440e2017-02-28 16:59:56 +0200452 * <newvalue> reflects the new config and is driver dependent
Laurent Meunierf07512e2013-04-18 10:48:07 +0200453 */
Vincent Stehlé3b59e432013-09-16 22:50:32 +0200454static ssize_t pinconf_dbg_config_write(struct file *file,
Laurent Meunierf07512e2013-04-18 10:48:07 +0200455 const char __user *user_buf, size_t count, loff_t *ppos)
456{
457 struct pinctrl_maps *maps_node;
458 const struct pinctrl_map *map;
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100459 const struct pinctrl_map *found = NULL;
460 struct pinctrl_dev *pctldev;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200461 const struct pinconf_ops *confops = NULL;
462 struct dbg_cfg *dbg = &pinconf_dbg_conf;
463 const struct pinctrl_map_configs *configs;
Markus Elfringe8c5d752017-05-02 10:32:19 +0200464 char config[MAX_NAME_LEN + 1];
Laurent Meunierf07512e2013-04-18 10:48:07 +0200465 char buf[128];
466 char *b = &buf[0];
467 int buf_size;
468 char *token;
469 int i;
470
471 /* Get userspace string and assure termination */
Dan Carpenter81d36c42013-09-17 13:47:54 +0300472 buf_size = min(count, sizeof(buf) - 1);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200473 if (copy_from_user(buf, user_buf, buf_size))
474 return -EFAULT;
475 buf[buf_size] = 0;
476
477 /*
478 * need to parse entry and extract parameters:
479 * modify configs_pin devicename state pinname newvalue
480 */
481
482 /* Get arg: 'modify' */
483 token = strsep(&b, " ");
484 if (!token)
485 return -EINVAL;
486 if (strcmp(token, "modify"))
487 return -EINVAL;
488
Jon Hunter75629982015-07-14 11:17:58 +0100489 /*
490 * Get arg type: "config_pin" and "config_group"
491 * types are supported so far
492 */
Laurent Meunierf07512e2013-04-18 10:48:07 +0200493 token = strsep(&b, " ");
494 if (!token)
495 return -EINVAL;
Jon Hunter75629982015-07-14 11:17:58 +0100496 if (!strcmp(token, "config_pin"))
497 dbg->map_type = PIN_MAP_TYPE_CONFIGS_PIN;
498 else if (!strcmp(token, "config_group"))
499 dbg->map_type = PIN_MAP_TYPE_CONFIGS_GROUP;
500 else
Laurent Meunierf07512e2013-04-18 10:48:07 +0200501 return -EINVAL;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200502
503 /* get arg 'device_name' */
504 token = strsep(&b, " ");
Markus Elfring76ce37f2017-05-02 10:01:57 +0200505 if (!token)
Laurent Meunierf07512e2013-04-18 10:48:07 +0200506 return -EINVAL;
507 if (strlen(token) >= MAX_NAME_LEN)
508 return -EINVAL;
509 strncpy(dbg->dev_name, token, MAX_NAME_LEN);
510
511 /* get arg 'state_name' */
512 token = strsep(&b, " ");
Markus Elfring76ce37f2017-05-02 10:01:57 +0200513 if (!token)
Laurent Meunierf07512e2013-04-18 10:48:07 +0200514 return -EINVAL;
515 if (strlen(token) >= MAX_NAME_LEN)
516 return -EINVAL;
517 strncpy(dbg->state_name, token, MAX_NAME_LEN);
518
519 /* get arg 'pin_name' */
520 token = strsep(&b, " ");
Markus Elfring76ce37f2017-05-02 10:01:57 +0200521 if (!token)
Laurent Meunierf07512e2013-04-18 10:48:07 +0200522 return -EINVAL;
523 if (strlen(token) >= MAX_NAME_LEN)
524 return -EINVAL;
525 strncpy(dbg->pin_name, token, MAX_NAME_LEN);
526
527 /* get new_value of config' */
528 token = strsep(&b, " ");
Markus Elfring76ce37f2017-05-02 10:01:57 +0200529 if (!token)
Laurent Meunierf07512e2013-04-18 10:48:07 +0200530 return -EINVAL;
531 if (strlen(token) >= MAX_NAME_LEN)
532 return -EINVAL;
533 strncpy(config, token, MAX_NAME_LEN);
534
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200535 mutex_lock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200536
537 /* Parse the pinctrl map and look for the selected dev/state/pin */
538 for_each_maps(maps_node, i, map) {
539 if (strcmp(map->dev_name, dbg->dev_name))
540 continue;
541 if (map->type != dbg->map_type)
542 continue;
543 if (strcmp(map->name, dbg->state_name))
544 continue;
545
546 /* we found the right pin / state, so overwrite config */
547 if (!strcmp(map->data.configs.group_or_pin, dbg->pin_name)) {
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100548 found = map;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200549 break;
550 }
551 }
552
553 if (!found) {
Laurent Meunierf07512e2013-04-18 10:48:07 +0200554 count = -EINVAL;
Laurent Meuniercb6d3152013-04-24 13:05:50 +0200555 goto exit;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200556 }
557
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100558 pctldev = get_pinctrl_dev_from_devname(found->ctrl_dev_name);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200559 if (pctldev)
560 confops = pctldev->desc->confops;
561
562 if (confops && confops->pin_config_dbg_parse_modify) {
Russell King - ARM Linux8a9dcc32013-07-28 13:13:00 +0100563 configs = &found->data.configs;
Laurent Meunierf07512e2013-04-18 10:48:07 +0200564 for (i = 0; i < configs->num_configs; i++) {
565 confops->pin_config_dbg_parse_modify(pctldev,
566 config,
567 &configs->configs[i]);
568 }
569 }
570
571exit:
Patrice Chotard42fed7b2013-04-11 11:01:27 +0200572 mutex_unlock(&pinctrl_maps_mutex);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200573
574 return count;
575}
576
577static int pinconf_dbg_config_open(struct inode *inode, struct file *file)
578{
579 return single_open(file, pinconf_dbg_config_print, inode->i_private);
580}
581
582static const struct file_operations pinconf_dbg_pinconfig_fops = {
583 .open = pinconf_dbg_config_open,
584 .write = pinconf_dbg_config_write,
585 .read = seq_read,
586 .llseek = seq_lseek,
587 .release = single_release,
588 .owner = THIS_MODULE,
589};
590
Linus Walleijae6b4d82011-10-19 18:14:33 +0200591void pinconf_init_device_debugfs(struct dentry *devroot,
592 struct pinctrl_dev *pctldev)
593{
594 debugfs_create_file("pinconf-pins", S_IFREG | S_IRUGO,
Yangtao Li0819dc72018-11-30 11:36:17 -0500595 devroot, pctldev, &pinconf_pins_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200596 debugfs_create_file("pinconf-groups", S_IFREG | S_IRUGO,
Yangtao Li0819dc72018-11-30 11:36:17 -0500597 devroot, pctldev, &pinconf_groups_fops);
Laurent Meunierf07512e2013-04-18 10:48:07 +0200598 debugfs_create_file("pinconf-config", (S_IRUGO | S_IWUSR | S_IWGRP),
599 devroot, pctldev, &pinconf_dbg_pinconfig_fops);
Linus Walleijae6b4d82011-10-19 18:14:33 +0200600}
601
602#endif