14 #define pr_fmt(fmt) "pinctrl core: " fmt
16 #include <linux/kernel.h>
17 #include <linux/export.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
22 #include <linux/list.h>
46 static bool pinctrl_dummy_state;
60 #define for_each_maps(_maps_node_, _i_, _map_) \
61 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
62 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
63 _i_ < _maps_node_->num_maps; \
64 _i_++, _map_ = &_maps_node_->maps[_i_])
76 pinctrl_dummy_state =
true;
82 return pctldev->
desc->name;
108 if (!
strcmp(dev_name(pctldev->
dev), devname)) {
115 return found ? pctldev :
NULL;
128 for (i = 0; i < pctldev->
desc->npins; i++) {
131 pin = pctldev->
desc->pins[
i].number;
132 desc = pin_desc_get(pctldev, pin);
152 desc = pin_desc_get(pctldev, pin);
154 dev_err(pctldev->
dev,
"failed to get pin(%d) name\n",
178 pindesc = pin_desc_get(pctldev, pin);
181 return pindesc !=
NULL;
187 const struct pinctrl_pin_desc *
pins,
192 for (i = 0; i < num_pins; i++) {
197 if (pindesc !=
NULL) {
207 static int pinctrl_register_one_pin(
struct pinctrl_dev *pctldev,
208 unsigned number,
const char *
name)
212 pindesc = pin_desc_get(pctldev, number);
213 if (pindesc !=
NULL) {
214 pr_err(
"pin %d already registered on %s\n", number,
215 pctldev->
desc->name);
219 pindesc = kzalloc(
sizeof(*pindesc),
GFP_KERNEL);
220 if (pindesc ==
NULL) {
221 dev_err(pctldev->
dev,
"failed to alloc struct pin_desc\n");
241 pr_debug(
"registered pin %d (%s) on %s\n",
242 number, pindesc->
name, pctldev->
desc->name);
246 static int pinctrl_register_pins(
struct pinctrl_dev *pctldev,
247 struct pinctrl_pin_desc
const *pins,
253 for (i = 0; i < num_descs; i++) {
254 ret = pinctrl_register_one_pin(pctldev,
255 pins[i].number, pins[i].name);
271 static struct pinctrl_gpio_range *
279 if (gpio >= range->base &&
299 static int pinctrl_get_device_gpio_range(
unsigned gpio,
301 struct pinctrl_gpio_range **outrange)
307 struct pinctrl_gpio_range *
range;
309 range = pinctrl_match_gpio_range(pctldev, gpio);
329 struct pinctrl_gpio_range *range)
338 struct pinctrl_gpio_range *ranges,
343 for (i = 0; i < nranges; i++)
354 const char *pin_group)
356 const struct pinctrl_ops *pctlops = pctldev->
desc->pctlops;
357 unsigned ngroups = pctlops->get_groups_count(pctldev);
358 unsigned group_selector = 0;
360 while (group_selector < ngroups) {
361 const char *gname = pctlops->get_group_name(pctldev,
363 if (!
strcmp(gname, pin_group)) {
365 "found group selector %u for %s\n",
368 return group_selector;
374 dev_err(pctldev->
dev,
"does not have pin group %s\n",
391 struct pinctrl_gpio_range *
range;
397 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
404 pin = gpio - range->base + range->pin_base;
424 struct pinctrl_gpio_range *
range;
430 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
437 pin = gpio - range->base + range->pin_base;
445 static int pinctrl_gpio_direction(
unsigned gpio,
bool input)
448 struct pinctrl_gpio_range *
range;
452 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
457 pin = gpio - range->base + range->pin_base;
474 ret = pinctrl_gpio_direction(gpio,
true);
492 ret = pinctrl_gpio_direction(gpio,
false);
518 "failed to alloc struct pinctrl_state\n");
536 state = find_state(p, map->
name);
538 state = create_state(p, map->
name);
540 return PTR_ERR(state);
545 setting = kzalloc(
sizeof(*setting),
GFP_KERNEL);
546 if (setting == NULL) {
548 "failed to alloc struct pinctrl_setting\n");
555 if (setting->
pctldev == NULL) {
556 dev_info(p->
dev,
"unknown pinctrl device %s in map entry, deferring probe",
599 static
void pinctrl_put_locked(
struct pinctrl *p,
bool inlist);
617 dev_err(dev,
"failed to alloc struct pinctrl\n");
621 INIT_LIST_HEAD(&p->
states);
638 ret = add_setting(p, map);
640 pinctrl_put_locked(p,
false);
651 static struct pinctrl *pinctrl_get_locked(
struct device *dev)
658 p = find_pinctrl(dev);
660 return ERR_PTR(-
EBUSY);
662 return create_pinctrl(dev);
674 p = pinctrl_get_locked(dev);
681 static void pinctrl_put_locked(
struct pinctrl *p,
bool inlist)
688 switch (setting->
type) {
690 if (state == p->
state)
722 pinctrl_put_locked(p,
true);
732 state = find_state(p, name);
734 if (pinctrl_dummy_state) {
736 dev_dbg(p->
dev,
"using pinctrl dummy state (%s)\n",
738 state = create_state(p, name);
756 s = pinctrl_lookup_state_locked(p, name);
763 static int pinctrl_select_state_locked(
struct pinctrl *p,
769 if (p->
state == state)
789 if (setting2->
data.
mux.group ==
804 switch (setting->
type) {
835 ret = pinctrl_select_state_locked(p, state);
842 static void devm_pinctrl_release(
struct device *dev,
void *
res)
874 static int devm_pinctrl_match(
struct device *dev,
void *res,
void *
data)
892 devm_pinctrl_match, p));
903 pr_debug(
"add %d pinmux maps\n", num_maps);
907 if (!maps[i].dev_name) {
908 pr_err(
"failed to register map %s (%d): no device given\n",
914 pr_err(
"failed to register map %d: no map name given\n",
920 !maps[i].ctrl_dev_name) {
921 pr_err(
"failed to register map %s (%d): no pin control device given\n",
926 switch (maps[i].
type) {
941 pr_err(
"failed to register map %s (%d): invalid type given\n",
947 maps_node = kzalloc(
sizeof(*maps_node),
GFP_KERNEL);
949 pr_err(
"failed to alloc struct pinctrl_maps\n");
955 maps_node->
maps =
kmemdup(maps,
sizeof(*maps) * num_maps,
957 if (!maps_node->
maps) {
958 pr_err(
"failed to duplicate mapping table\n");
993 if (maps_node->
maps == map) {
1000 #ifdef CONFIG_DEBUG_FS
1002 static int pinctrl_pins_show(
struct seq_file *
s,
void *
what)
1005 const struct pinctrl_ops *
ops = pctldev->
desc->pctlops;
1013 for (i = 0; i < pctldev->
desc->npins; i++) {
1016 pin = pctldev->
desc->pins[
i].number;
1017 desc = pin_desc_get(pctldev, pin);
1023 desc->
name ? desc->
name :
"unnamed");
1026 if (ops->pin_dbg_show)
1027 ops->pin_dbg_show(pctldev, s, pin);
1037 static int pinctrl_groups_show(
struct seq_file *s,
void *what)
1040 const struct pinctrl_ops *ops = pctldev->
desc->pctlops;
1043 ngroups = ops->get_groups_count(pctldev);
1046 seq_puts(s,
"registered pin groups:\n");
1047 while (selector < ngroups) {
1048 const unsigned *
pins;
1050 const char *gname = ops->get_group_name(pctldev, selector);
1055 ret = ops->get_group_pins(pctldev, selector,
1062 for (i = 0; i < num_pins; i++) {
1068 seq_printf(s,
"pin %d (%s)\n", pins[i], pname);
1080 static int pinctrl_gpioranges_show(
struct seq_file *s,
void *what)
1083 struct pinctrl_gpio_range *range =
NULL;
1085 seq_puts(s,
"GPIO ranges handled:\n");
1091 seq_printf(s,
"%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1092 range->id, range->name,
1093 range->base, (range->base + range->npins - 1),
1095 (range->pin_base + range->npins - 1));
1103 static int pinctrl_devices_show(
struct seq_file *s,
void *what)
1107 seq_puts(s,
"name [pinmux] [pinconf]\n");
1113 if (pctldev->
desc->pmxops)
1117 if (pctldev->
desc->confops)
1131 static const char *
const names[] = {
1145 static int pinctrl_maps_show(
struct seq_file *s,
void *what)
1156 seq_printf(s,
"device %s\nstate %s\ntype %s (%d)\n",
1164 switch (map->
type) {
1166 pinmux_show_map(s, map);
1170 pinconf_show_map(s, map);
1184 static int pinctrl_show(
struct seq_file *s,
void *what)
1190 seq_puts(s,
"Requested pin control handlers their pinmux maps:\n");
1195 seq_printf(s,
"device: %s current state: %s\n",
1209 switch (setting->
type) {
1211 pinmux_show_setting(s, setting);
1215 pinconf_show_setting(s, setting);
1246 return single_open(file, pinctrl_devices_show, NULL);
1251 return single_open(file, pinctrl_maps_show, NULL);
1260 .
open = pinctrl_pins_open,
1267 .
open = pinctrl_groups_open,
1274 .
open = pinctrl_gpioranges_open,
1281 .
open = pinctrl_devices_open,
1288 .
open = pinctrl_maps_open,
1295 .
open = pinctrl_open,
1301 static struct dentry *debugfs_root;
1303 static void pinctrl_init_device_debugfs(
struct pinctrl_dev *pctldev)
1305 struct dentry *device_root;
1309 pctldev->device_root = device_root;
1311 if (IS_ERR(device_root) || !device_root) {
1312 pr_warn(
"failed to create debugfs directory for %s\n",
1313 dev_name(pctldev->
dev));
1317 device_root, pctldev, &pinctrl_pins_ops);
1319 device_root, pctldev, &pinctrl_groups_ops);
1321 device_root, pctldev, &pinctrl_gpioranges_ops);
1322 pinmux_init_device_debugfs(device_root, pctldev);
1323 pinconf_init_device_debugfs(device_root, pctldev);
1326 static void pinctrl_remove_device_debugfs(
struct pinctrl_dev *pctldev)
1331 static void pinctrl_init_debugfs(
void)
1334 if (IS_ERR(debugfs_root) || !debugfs_root) {
1335 pr_warn(
"failed to create debugfs directory\n");
1336 debugfs_root =
NULL;
1341 debugfs_root, NULL, &pinctrl_devices_ops);
1343 debugfs_root, NULL, &pinctrl_maps_ops);
1345 debugfs_root, NULL, &pinctrl_ops);
1350 static void pinctrl_init_device_debugfs(
struct pinctrl_dev *pctldev)
1354 static void pinctrl_init_debugfs(
void)
1358 static void pinctrl_remove_device_debugfs(
struct pinctrl_dev *pctldev)
1364 static int pinctrl_check_ops(
struct pinctrl_dev *pctldev)
1366 const struct pinctrl_ops *ops = pctldev->
desc->pctlops;
1369 !ops->get_groups_count ||
1370 !ops->get_group_name ||
1371 !ops->get_group_pins)
1374 if (ops->dt_node_to_map && !ops->dt_free_map)
1394 if (!pctldesc->name)
1397 pctldev = kzalloc(
sizeof(*pctldev),
GFP_KERNEL);
1398 if (pctldev == NULL) {
1399 dev_err(dev,
"failed to alloc struct pinctrl_dev\n");
1404 pctldev->
owner = pctldesc->owner;
1405 pctldev->
desc = pctldesc;
1412 if (pinctrl_check_ops(pctldev)) {
1413 dev_err(dev,
"pinctrl ops lacks necessary functions\n");
1418 if (pctldesc->pmxops) {
1424 if (pctldesc->confops) {
1430 dev_dbg(dev,
"try to register %d pins ...\n", pctldesc->npins);
1431 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1433 dev_err(dev,
"error during pin registration\n");
1434 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1443 pctldev->
p = pinctrl_get_locked(pctldev->
dev);
1444 if (!IS_ERR(pctldev->
p)) {
1446 pinctrl_lookup_state_locked(pctldev->
p,
1449 dev_dbg(dev,
"failed to lookup the default state\n");
1451 if (pinctrl_select_state_locked(pctldev->
p, s))
1453 "failed to select default state\n");
1459 pinctrl_init_device_debugfs(pctldev);
1477 struct pinctrl_gpio_range *
range, *
n;
1478 if (pctldev == NULL)
1481 pinctrl_remove_device_debugfs(pctldev);
1485 if (!IS_ERR(pctldev->
p))
1486 pinctrl_put_locked(pctldev->
p,
true);
1491 pinctrl_free_pindescs(pctldev, pctldev->
desc->pins,
1492 pctldev->
desc->npins);
1503 static int __init pinctrl_init(
void)
1505 pr_info(
"initialized pinctrl subsystem\n");
1506 pinctrl_init_debugfs();