1 // SPDX-License-Identifier: GPL-2.0-only
3 * Core driver for the pin config portions of the pin control subsystem
5 * Copyright (C) 2011 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 #define pr_fmt(fmt) "pinconfig core: " fmt
12 #include <linux/array_size.h>
13 #include <linux/debugfs.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/seq_file.h>
18 #include <linux/slab.h>
20 #include <linux/pinctrl/machine.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinctrl.h>
27 int pinconf_check_ops(struct pinctrl_dev
*pctldev
)
29 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
31 /* We have to be able to config the pins in SOME way */
32 if (!ops
->pin_config_set
&& !ops
->pin_config_group_set
) {
34 "pinconf has to be able to set a pins config\n");
40 int pinconf_validate_map(const struct pinctrl_map
*map
, int i
)
42 if (!map
->data
.configs
.group_or_pin
) {
43 pr_err("failed to register map %s (%d): no group/pin given\n",
48 if (!map
->data
.configs
.num_configs
||
49 !map
->data
.configs
.configs
) {
50 pr_err("failed to register map %s (%d): no configs given\n",
58 int pin_config_get_for_pin(struct pinctrl_dev
*pctldev
, unsigned int pin
,
59 unsigned long *config
)
61 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
63 if (!ops
|| !ops
->pin_config_get
) {
65 "cannot get pin configuration, .pin_config_get missing in driver\n");
69 return ops
->pin_config_get(pctldev
, pin
, config
);
72 int pin_config_group_get(const char *dev_name
, const char *pin_group
,
73 unsigned long *config
)
75 struct pinctrl_dev
*pctldev
;
76 const struct pinconf_ops
*ops
;
79 pctldev
= get_pinctrl_dev_from_devname(dev_name
);
85 mutex_lock(&pctldev
->mutex
);
87 ops
= pctldev
->desc
->confops
;
89 if (!ops
|| !ops
->pin_config_group_get
) {
91 "cannot get configuration for pin group, missing group config get function in driver\n");
96 selector
= pinctrl_get_group_selector(pctldev
, pin_group
);
102 ret
= ops
->pin_config_group_get(pctldev
, selector
, config
);
105 mutex_unlock(&pctldev
->mutex
);
109 int pinconf_map_to_setting(const struct pinctrl_map
*map
,
110 struct pinctrl_setting
*setting
)
112 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
115 switch (setting
->type
) {
116 case PIN_MAP_TYPE_CONFIGS_PIN
:
117 pin
= pin_get_from_name(pctldev
,
118 map
->data
.configs
.group_or_pin
);
120 dev_err(pctldev
->dev
, "could not map pin config for \"%s\"",
121 map
->data
.configs
.group_or_pin
);
124 setting
->data
.configs
.group_or_pin
= pin
;
126 case PIN_MAP_TYPE_CONFIGS_GROUP
:
127 pin
= pinctrl_get_group_selector(pctldev
,
128 map
->data
.configs
.group_or_pin
);
130 dev_err(pctldev
->dev
, "could not map group config for \"%s\"",
131 map
->data
.configs
.group_or_pin
);
134 setting
->data
.configs
.group_or_pin
= pin
;
140 setting
->data
.configs
.num_configs
= map
->data
.configs
.num_configs
;
141 setting
->data
.configs
.configs
= map
->data
.configs
.configs
;
146 void pinconf_free_setting(const struct pinctrl_setting
*setting
)
150 int pinconf_apply_setting(const struct pinctrl_setting
*setting
)
152 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
153 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
157 dev_err(pctldev
->dev
, "missing confops\n");
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");
167 ret
= ops
->pin_config_set(pctldev
,
168 setting
->data
.configs
.group_or_pin
,
169 setting
->data
.configs
.configs
,
170 setting
->data
.configs
.num_configs
);
172 dev_err(pctldev
->dev
,
173 "pin_config_set op failed for pin %d\n",
174 setting
->data
.configs
.group_or_pin
);
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");
184 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
);
189 dev_err(pctldev
->dev
,
190 "pin_config_group_set op failed for group %d\n",
191 setting
->data
.configs
.group_or_pin
);
202 int pinconf_set_config(struct pinctrl_dev
*pctldev
, unsigned int pin
,
203 unsigned long *configs
, size_t nconfigs
)
205 const struct pinconf_ops
*ops
;
207 ops
= pctldev
->desc
->confops
;
208 if (!ops
|| !ops
->pin_config_set
)
211 return ops
->pin_config_set(pctldev
, pin
, configs
, nconfigs
);
214 #ifdef CONFIG_DEBUG_FS
216 static void pinconf_show_config(struct seq_file
*s
, struct pinctrl_dev
*pctldev
,
217 unsigned long *configs
, unsigned int num_configs
)
219 const struct pinconf_ops
*confops
;
223 confops
= pctldev
->desc
->confops
;
227 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
,
233 seq_printf(s
, "%08lx", configs
[i
]);
238 void pinconf_show_map(struct seq_file
*s
, const struct pinctrl_map
*map
)
240 struct pinctrl_dev
*pctldev
;
242 pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
245 case PIN_MAP_TYPE_CONFIGS_PIN
:
248 case PIN_MAP_TYPE_CONFIGS_GROUP
:
249 seq_puts(s
, "group ");
255 seq_printf(s
, "%s\n", map
->data
.configs
.group_or_pin
);
257 pinconf_show_config(s
, pctldev
, map
->data
.configs
.configs
,
258 map
->data
.configs
.num_configs
);
261 void pinconf_show_setting(struct seq_file
*s
,
262 const struct pinctrl_setting
*setting
)
264 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
265 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
266 struct pin_desc
*desc
;
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
);
272 seq_printf(s
, "pin %s (%d)", desc
->name
,
273 setting
->data
.configs
.group_or_pin
);
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
);
286 * FIXME: We should really get the pin controller to dump the config
287 * values, so they can be decoded to something meaningful.
289 pinconf_show_config(s
, pctldev
, setting
->data
.configs
.configs
,
290 setting
->data
.configs
.num_configs
);
293 static void pinconf_dump_pin(struct pinctrl_dev
*pctldev
,
294 struct seq_file
*s
, int pin
)
296 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
298 /* no-op when not using generic pin config */
299 pinconf_generic_dump_pins(pctldev
, s
, NULL
, pin
);
300 if (ops
&& ops
->pin_config_dbg_show
)
301 ops
->pin_config_dbg_show(pctldev
, s
, pin
);
304 static int pinconf_pins_show(struct seq_file
*s
, void *what
)
306 struct pinctrl_dev
*pctldev
= s
->private;
309 seq_puts(s
, "Pin config settings per pin\n");
310 seq_puts(s
, "Format: pin (name): configs\n");
312 mutex_lock(&pctldev
->mutex
);
314 /* The pin number can be retrived from the pin controller descriptor */
315 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
316 struct pin_desc
*desc
;
318 pin
= pctldev
->desc
->pins
[i
].number
;
319 desc
= pin_desc_get(pctldev
, pin
);
320 /* Skip if we cannot search the pin */
324 seq_printf(s
, "pin %d (%s): ", pin
, desc
->name
);
326 pinconf_dump_pin(pctldev
, s
, pin
);
330 mutex_unlock(&pctldev
->mutex
);
335 static void pinconf_dump_group(struct pinctrl_dev
*pctldev
,
336 struct seq_file
*s
, unsigned int selector
,
339 const struct pinconf_ops
*ops
= pctldev
->desc
->confops
;
341 /* no-op when not using generic pin config */
342 pinconf_generic_dump_pins(pctldev
, s
, gname
, 0);
343 if (ops
&& ops
->pin_config_group_dbg_show
)
344 ops
->pin_config_group_dbg_show(pctldev
, s
, selector
);
347 static int pinconf_groups_show(struct seq_file
*s
, void *what
)
349 struct pinctrl_dev
*pctldev
= s
->private;
350 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
351 unsigned int ngroups
= pctlops
->get_groups_count(pctldev
);
352 unsigned int selector
= 0;
354 seq_puts(s
, "Pin config settings per pin group\n");
355 seq_puts(s
, "Format: group (name): configs\n");
357 while (selector
< ngroups
) {
358 const char *gname
= pctlops
->get_group_name(pctldev
, selector
);
360 seq_printf(s
, "%u (%s): ", selector
, gname
);
361 pinconf_dump_group(pctldev
, s
, selector
, gname
);
369 DEFINE_SHOW_ATTRIBUTE(pinconf_pins
);
370 DEFINE_SHOW_ATTRIBUTE(pinconf_groups
);
372 void pinconf_init_device_debugfs(struct dentry
*devroot
,
373 struct pinctrl_dev
*pctldev
)
375 debugfs_create_file("pinconf-pins", 0444,
376 devroot
, pctldev
, &pinconf_pins_fops
);
377 debugfs_create_file("pinconf-groups", 0444,
378 devroot
, pctldev
, &pinconf_groups_fops
);