Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pinctrl: move subsystem mutex to pinctrl_dev struct

This mutex avoids deadlock in case of use of multiple pin
controllers. Before this modification, by using a global
mutex, deadlock appeared when, for example, a call to
pinctrl_pins_show() locked the pinctrl_mutex, called the
ops->pin_dbg_show of a particular pin controller. If this
pin controller needs I2C access to retrieve configuration
information and I2C driver is using pinctrl to drive its
pins, a call to pinctrl_select_state() try to lock again
pinctrl_mutex which leads to a deadlock.

Notice that the mutex grab from the two direction functions
was moved into pinctrl_gpio_direction().

For several cases, we can't replace pinctrl_mutex by
pctldev->mutex, because at this stage, pctldev is
not accessible :
- pinctrl_get()/pinctrl_put()
- pinctrl_register_maps()

So add respectively pinctrl_list_mutex and
pinctrl_maps_mutex in order to protect
pinctrl_list and pinctrl_maps list instead.

Reintroduce pinctrldev_list_mutex in
find_pinctrl_by_of_node(),
pinctrl_find_and_add_gpio_range()
pinctrl_request_gpio(), pinctrl_free_gpio(),
pinctrl_gpio_direction(), pinctrl_devices_show(),
pinctrl_register() and pinctrl_unregister() to
protect pinctrldev_list.

Changes v2->v3:
- Fix a missing EXPORT_SYMBOL_GPL() for pinctrl_select_state().

Changes v1->v2:
- pinctrl_select_state_locked() is removed, all lock mechanism
is located inside pinctrl_select_state(). When parsing
the state->setting list, take the per-pin-controller driver
lock. (Patrice).
- Introduce pinctrldev_list_mutex to protect pinctrldev_list
in all functions which parse or modify pictrldev_list.
(Patrice).
- move find_pinctrl_by_of_node() from pinctrl/devicetree.c to
pinctrl/core.c in order to protect pinctrldev_list.
(Patrice).
- Sink mutex:es into some functions and remove some _locked
variants down to where the lists are actually accessed to
make things simpler. (Linus)
- Drop *all* mutexes completely from pinctrl_lookup_state()
and pinctrl_select_state() - no relevant mutex was taken
and it was unclear what this was protecting against. (Linus)

Reported by : Seraphin Bonnaffe <seraphin.bonnaffe@stericsson.com>
Signed-off-by: Patrice Chotard <patrice.chotard@st.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Patrice Chotard and committed by
Linus Walleij
42fed7ba cb6d315d

+177 -165
+143 -124
drivers/pinctrl/core.c
··· 40 40 41 41 static bool pinctrl_dummy_state; 42 42 43 - /* Mutex taken by all entry points */ 44 - DEFINE_MUTEX(pinctrl_mutex); 43 + /* Mutex taken to protect pinctrl_list */ 44 + DEFINE_MUTEX(pinctrl_list_mutex); 45 + 46 + /* Mutex taken to protect pinctrl_maps */ 47 + DEFINE_MUTEX(pinctrl_maps_mutex); 48 + 49 + /* Mutex taken to protect pinctrldev_list */ 50 + DEFINE_MUTEX(pinctrldev_list_mutex); 45 51 46 52 /* Global list of pin control devices (struct pinctrl_dev) */ 47 - LIST_HEAD(pinctrldev_list); 53 + static LIST_HEAD(pinctrldev_list); 48 54 49 55 /* List of pin controller handles (struct pinctrl) */ 50 56 static LIST_HEAD(pinctrl_list); ··· 117 111 return found ? pctldev : NULL; 118 112 } 119 113 114 + struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np) 115 + { 116 + struct pinctrl_dev *pctldev; 117 + 118 + mutex_lock(&pinctrldev_list_mutex); 119 + 120 + list_for_each_entry(pctldev, &pinctrldev_list, node) 121 + if (pctldev->dev->of_node == np) { 122 + mutex_unlock(&pinctrldev_list_mutex); 123 + return pctldev; 124 + } 125 + 126 + mutex_lock(&pinctrldev_list_mutex); 127 + 128 + return NULL; 129 + } 130 + 120 131 /** 121 132 * pin_get_from_name() - look up a pin number from a name 122 133 * @pctldev: the pin control device to lookup the pin on ··· 193 170 if (pin < 0) 194 171 return false; 195 172 196 - mutex_lock(&pinctrl_mutex); 173 + mutex_lock(&pctldev->mutex); 197 174 pindesc = pin_desc_get(pctldev, pin); 198 - mutex_unlock(&pinctrl_mutex); 175 + mutex_unlock(&pctldev->mutex); 199 176 200 177 return pindesc != NULL; 201 178 } ··· 292 269 { 293 270 struct pinctrl_gpio_range *range = NULL; 294 271 272 + mutex_lock(&pctldev->mutex); 295 273 /* Loop over the ranges */ 296 274 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 297 275 /* Check if we're in the valid range */ 298 276 if (gpio >= range->base && 299 277 gpio < range->base + range->npins) { 278 + mutex_unlock(&pctldev->mutex); 300 279 return range; 301 280 } 302 281 } 303 - 282 + mutex_unlock(&pctldev->mutex); 304 283 return NULL; 305 284 } 306 285 ··· 386 361 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, 387 362 struct pinctrl_gpio_range *range) 388 363 { 389 - mutex_lock(&pinctrl_mutex); 364 + mutex_lock(&pctldev->mutex); 390 365 list_add_tail(&range->node, &pctldev->gpio_ranges); 391 - mutex_unlock(&pinctrl_mutex); 366 + mutex_unlock(&pctldev->mutex); 392 367 } 393 368 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range); 394 369 ··· 406 381 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname, 407 382 struct pinctrl_gpio_range *range) 408 383 { 409 - struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname); 384 + struct pinctrl_dev *pctldev; 385 + 386 + mutex_lock(&pinctrldev_list_mutex); 387 + 388 + pctldev = get_pinctrl_dev_from_devname(devname); 410 389 411 390 /* 412 391 * If we can't find this device, let's assume that is because 413 392 * it has not probed yet, so the driver trying to register this 414 393 * range need to defer probing. 415 394 */ 416 - if (!pctldev) 395 + if (!pctldev) { 396 + mutex_unlock(&pinctrldev_list_mutex); 417 397 return ERR_PTR(-EPROBE_DEFER); 418 - 398 + } 419 399 pinctrl_add_gpio_range(pctldev, range); 400 + 401 + mutex_unlock(&pinctrldev_list_mutex); 402 + 420 403 return pctldev; 421 404 } 422 405 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range); ··· 440 407 { 441 408 struct pinctrl_gpio_range *range = NULL; 442 409 410 + mutex_lock(&pctldev->mutex); 443 411 /* Loop over the ranges */ 444 412 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 445 413 /* Check if we're in the valid range */ 446 414 if (pin >= range->pin_base && 447 415 pin < range->pin_base + range->npins) { 416 + mutex_unlock(&pctldev->mutex); 448 417 return range; 449 418 } 450 419 } 420 + mutex_unlock(&pctldev->mutex); 451 421 452 422 return NULL; 453 423 } ··· 464 428 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, 465 429 struct pinctrl_gpio_range *range) 466 430 { 467 - mutex_lock(&pinctrl_mutex); 431 + mutex_lock(&pctldev->mutex); 468 432 list_del(&range->node); 469 - mutex_unlock(&pinctrl_mutex); 433 + mutex_unlock(&pctldev->mutex); 470 434 } 471 435 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range); 472 436 ··· 517 481 int ret; 518 482 int pin; 519 483 520 - mutex_lock(&pinctrl_mutex); 484 + mutex_lock(&pinctrldev_list_mutex); 521 485 522 486 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 523 487 if (ret) { 524 488 if (pinctrl_ready_for_gpio_range(gpio)) 525 489 ret = 0; 526 - mutex_unlock(&pinctrl_mutex); 490 + mutex_unlock(&pinctrldev_list_mutex); 527 491 return ret; 528 492 } 529 493 ··· 532 496 533 497 ret = pinmux_request_gpio(pctldev, range, pin, gpio); 534 498 535 - mutex_unlock(&pinctrl_mutex); 499 + mutex_unlock(&pinctrldev_list_mutex); 536 500 return ret; 537 501 } 538 502 EXPORT_SYMBOL_GPL(pinctrl_request_gpio); ··· 552 516 int ret; 553 517 int pin; 554 518 555 - mutex_lock(&pinctrl_mutex); 519 + mutex_lock(&pinctrldev_list_mutex); 556 520 557 521 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 558 522 if (ret) { 559 - mutex_unlock(&pinctrl_mutex); 523 + mutex_unlock(&pinctrldev_list_mutex); 560 524 return; 561 525 } 526 + mutex_lock(&pctldev->mutex); 562 527 563 528 /* Convert to the pin controllers number space */ 564 529 pin = gpio - range->base + range->pin_base; 565 530 566 531 pinmux_free_gpio(pctldev, pin, range); 567 532 568 - mutex_unlock(&pinctrl_mutex); 533 + mutex_unlock(&pctldev->mutex); 534 + mutex_unlock(&pinctrldev_list_mutex); 569 535 } 570 536 EXPORT_SYMBOL_GPL(pinctrl_free_gpio); 571 537 ··· 578 540 int ret; 579 541 int pin; 580 542 543 + mutex_lock(&pinctrldev_list_mutex); 544 + 581 545 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 582 - if (ret) 546 + if (ret) { 547 + mutex_unlock(&pinctrldev_list_mutex); 583 548 return ret; 549 + } 550 + 551 + mutex_lock(&pctldev->mutex); 584 552 585 553 /* Convert to the pin controllers number space */ 586 554 pin = gpio - range->base + range->pin_base; 555 + ret = pinmux_gpio_direction(pctldev, range, pin, input); 587 556 588 - return pinmux_gpio_direction(pctldev, range, pin, input); 557 + mutex_unlock(&pctldev->mutex); 558 + mutex_unlock(&pinctrldev_list_mutex); 559 + 560 + return ret; 589 561 } 590 562 591 563 /** ··· 608 560 */ 609 561 int pinctrl_gpio_direction_input(unsigned gpio) 610 562 { 611 - int ret; 612 - mutex_lock(&pinctrl_mutex); 613 - ret = pinctrl_gpio_direction(gpio, true); 614 - mutex_unlock(&pinctrl_mutex); 615 - return ret; 563 + return pinctrl_gpio_direction(gpio, true); 616 564 } 617 565 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); 618 566 ··· 622 578 */ 623 579 int pinctrl_gpio_direction_output(unsigned gpio) 624 580 { 625 - int ret; 626 - mutex_lock(&pinctrl_mutex); 627 - ret = pinctrl_gpio_direction(gpio, false); 628 - mutex_unlock(&pinctrl_mutex); 629 - return ret; 581 + return pinctrl_gpio_direction(gpio, false); 630 582 } 631 583 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); 632 584 ··· 725 685 { 726 686 struct pinctrl *p; 727 687 688 + mutex_lock(&pinctrl_list_mutex); 728 689 list_for_each_entry(p, &pinctrl_list, node) 729 - if (p->dev == dev) 690 + if (p->dev == dev) { 691 + mutex_unlock(&pinctrl_list_mutex); 730 692 return p; 693 + } 731 694 695 + mutex_unlock(&pinctrl_list_mutex); 732 696 return NULL; 733 697 } 734 698 735 - static void pinctrl_put_locked(struct pinctrl *p, bool inlist); 699 + static void pinctrl_free(struct pinctrl *p, bool inlist); 736 700 737 701 static struct pinctrl *create_pinctrl(struct device *dev) 738 702 { ··· 769 725 770 726 devname = dev_name(dev); 771 727 728 + mutex_lock(&pinctrl_maps_mutex); 772 729 /* Iterate over the pin control maps to locate the right ones */ 773 730 for_each_maps(maps_node, i, map) { 774 731 /* Map must be for this device */ ··· 791 746 * an -EPROBE_DEFER later, as that is the worst case. 792 747 */ 793 748 if (ret == -EPROBE_DEFER) { 794 - pinctrl_put_locked(p, false); 749 + pinctrl_free(p, false); 750 + mutex_unlock(&pinctrl_maps_mutex); 795 751 return ERR_PTR(ret); 796 752 } 797 753 } 754 + mutex_unlock(&pinctrl_maps_mutex); 755 + 798 756 if (ret < 0) { 799 757 /* If some other error than deferral occured, return here */ 800 - pinctrl_put_locked(p, false); 758 + pinctrl_free(p, false); 801 759 return ERR_PTR(ret); 802 760 } 803 761 ··· 812 764 return p; 813 765 } 814 766 815 - static struct pinctrl *pinctrl_get_locked(struct device *dev) 767 + /** 768 + * pinctrl_get() - retrieves the pinctrl handle for a device 769 + * @dev: the device to obtain the handle for 770 + */ 771 + struct pinctrl *pinctrl_get(struct device *dev) 816 772 { 817 773 struct pinctrl *p; 818 774 ··· 836 784 } 837 785 838 786 return create_pinctrl(dev); 839 - } 840 - 841 - /** 842 - * pinctrl_get() - retrieves the pinctrl handle for a device 843 - * @dev: the device to obtain the handle for 844 - */ 845 - struct pinctrl *pinctrl_get(struct device *dev) 846 - { 847 - struct pinctrl *p; 848 - 849 - mutex_lock(&pinctrl_mutex); 850 - p = pinctrl_get_locked(dev); 851 - mutex_unlock(&pinctrl_mutex); 852 - 853 - return p; 854 787 } 855 788 EXPORT_SYMBOL_GPL(pinctrl_get); 856 789 ··· 857 820 } 858 821 } 859 822 860 - static void pinctrl_put_locked(struct pinctrl *p, bool inlist) 823 + static void pinctrl_free(struct pinctrl *p, bool inlist) 861 824 { 862 825 struct pinctrl_state *state, *n1; 863 826 struct pinctrl_setting *setting, *n2; 864 827 828 + mutex_lock(&pinctrl_list_mutex); 865 829 list_for_each_entry_safe(state, n1, &p->states, node) { 866 830 list_for_each_entry_safe(setting, n2, &state->settings, node) { 867 831 pinctrl_free_setting(state == p->state, setting); ··· 878 840 if (inlist) 879 841 list_del(&p->node); 880 842 kfree(p); 843 + mutex_unlock(&pinctrl_list_mutex); 881 844 } 882 845 883 846 /** ··· 889 850 { 890 851 struct pinctrl *p = container_of(kref, struct pinctrl, users); 891 852 892 - pinctrl_put_locked(p, true); 853 + pinctrl_free(p, true); 893 854 } 894 855 895 856 /** ··· 898 859 */ 899 860 void pinctrl_put(struct pinctrl *p) 900 861 { 901 - mutex_lock(&pinctrl_mutex); 902 862 kref_put(&p->users, pinctrl_release); 903 - mutex_unlock(&pinctrl_mutex); 904 863 } 905 864 EXPORT_SYMBOL_GPL(pinctrl_put); 906 865 907 - static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p, 908 - const char *name) 866 + /** 867 + * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle 868 + * @p: the pinctrl handle to retrieve the state from 869 + * @name: the state name to retrieve 870 + */ 871 + struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, 872 + const char *name) 909 873 { 910 874 struct pinctrl_state *state; 911 875 ··· 925 883 926 884 return state; 927 885 } 928 - 929 - /** 930 - * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle 931 - * @p: the pinctrl handle to retrieve the state from 932 - * @name: the state name to retrieve 933 - */ 934 - struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name) 935 - { 936 - struct pinctrl_state *s; 937 - 938 - mutex_lock(&pinctrl_mutex); 939 - s = pinctrl_lookup_state_locked(p, name); 940 - mutex_unlock(&pinctrl_mutex); 941 - 942 - return s; 943 - } 944 886 EXPORT_SYMBOL_GPL(pinctrl_lookup_state); 945 887 946 - static int pinctrl_select_state_locked(struct pinctrl *p, 947 - struct pinctrl_state *state) 888 + /** 889 + * pinctrl_select_state() - select/activate/program a pinctrl state to HW 890 + * @p: the pinctrl handle for the device that requests configuration 891 + * @state: the state handle to select/activate/program 892 + */ 893 + int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) 948 894 { 949 895 struct pinctrl_setting *setting, *setting2; 950 896 struct pinctrl_state *old_state = p->state; ··· 986 956 break; 987 957 } 988 958 989 - if (ret < 0) 959 + if (ret < 0) { 990 960 goto unapply_new_state; 961 + } 991 962 } 992 963 993 964 p->state = state; ··· 1014 983 1015 984 /* There's no infinite recursive loop here because p->state is NULL */ 1016 985 if (old_state) 1017 - pinctrl_select_state_locked(p, old_state); 1018 - 1019 - return ret; 1020 - } 1021 - 1022 - /** 1023 - * pinctrl_select() - select/activate/program a pinctrl state to HW 1024 - * @p: the pinctrl handle for the device that requests configuratio 1025 - * @state: the state handle to select/activate/program 1026 - */ 1027 - int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) 1028 - { 1029 - int ret; 1030 - 1031 - mutex_lock(&pinctrl_mutex); 1032 - ret = pinctrl_select_state_locked(p, state); 1033 - mutex_unlock(&pinctrl_mutex); 986 + pinctrl_select_state(p, old_state); 1034 987 1035 988 return ret; 1036 989 } ··· 1144 1129 } 1145 1130 1146 1131 if (!locked) 1147 - mutex_lock(&pinctrl_mutex); 1132 + mutex_lock(&pinctrl_maps_mutex); 1148 1133 list_add_tail(&maps_node->node, &pinctrl_maps); 1149 1134 if (!locked) 1150 - mutex_unlock(&pinctrl_mutex); 1135 + mutex_unlock(&pinctrl_maps_mutex); 1151 1136 1152 1137 return 0; 1153 1138 } ··· 1169 1154 { 1170 1155 struct pinctrl_maps *maps_node; 1171 1156 1157 + mutex_lock(&pinctrl_maps_mutex); 1172 1158 list_for_each_entry(maps_node, &pinctrl_maps, node) { 1173 1159 if (maps_node->maps == map) { 1174 1160 list_del(&maps_node->node); 1161 + mutex_unlock(&pinctrl_maps_mutex); 1175 1162 return; 1176 1163 } 1177 1164 } 1165 + mutex_unlock(&pinctrl_maps_mutex); 1178 1166 } 1179 1167 1180 1168 /** ··· 1214 1196 1215 1197 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); 1216 1198 1217 - mutex_lock(&pinctrl_mutex); 1199 + mutex_lock(&pctldev->mutex); 1218 1200 1219 1201 /* The pin number can be retrived from the pin controller descriptor */ 1220 1202 for (i = 0; i < pctldev->desc->npins; i++) { ··· 1236 1218 seq_puts(s, "\n"); 1237 1219 } 1238 1220 1239 - mutex_unlock(&pinctrl_mutex); 1221 + mutex_unlock(&pctldev->mutex); 1240 1222 1241 1223 return 0; 1242 1224 } ··· 1247 1229 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1248 1230 unsigned ngroups, selector = 0; 1249 1231 1232 + mutex_lock(&pctldev->mutex); 1233 + 1250 1234 ngroups = ops->get_groups_count(pctldev); 1251 - mutex_lock(&pinctrl_mutex); 1252 1235 1253 1236 seq_puts(s, "registered pin groups:\n"); 1254 1237 while (selector < ngroups) { ··· 1270 1251 for (i = 0; i < num_pins; i++) { 1271 1252 pname = pin_get_name(pctldev, pins[i]); 1272 1253 if (WARN_ON(!pname)) { 1273 - mutex_unlock(&pinctrl_mutex); 1254 + mutex_unlock(&pctldev->mutex); 1274 1255 return -EINVAL; 1275 1256 } 1276 1257 seq_printf(s, "pin %d (%s)\n", pins[i], pname); ··· 1280 1261 selector++; 1281 1262 } 1282 1263 1283 - mutex_unlock(&pinctrl_mutex); 1264 + mutex_unlock(&pctldev->mutex); 1284 1265 1285 1266 return 0; 1286 1267 } ··· 1292 1273 1293 1274 seq_puts(s, "GPIO ranges handled:\n"); 1294 1275 1295 - mutex_lock(&pinctrl_mutex); 1276 + mutex_lock(&pctldev->mutex); 1296 1277 1297 1278 /* Loop over the ranges */ 1298 1279 list_for_each_entry(range, &pctldev->gpio_ranges, node) { ··· 1303 1284 (range->pin_base + range->npins - 1)); 1304 1285 } 1305 1286 1306 - mutex_unlock(&pinctrl_mutex); 1287 + mutex_unlock(&pctldev->mutex); 1307 1288 1308 1289 return 0; 1309 1290 } ··· 1314 1295 1315 1296 seq_puts(s, "name [pinmux] [pinconf]\n"); 1316 1297 1317 - mutex_lock(&pinctrl_mutex); 1298 + mutex_lock(&pinctrldev_list_mutex); 1318 1299 1319 1300 list_for_each_entry(pctldev, &pinctrldev_list, node) { 1320 1301 seq_printf(s, "%s ", pctldev->desc->name); ··· 1329 1310 seq_puts(s, "\n"); 1330 1311 } 1331 1312 1332 - mutex_unlock(&pinctrl_mutex); 1313 + mutex_unlock(&pinctrldev_list_mutex); 1333 1314 1334 1315 return 0; 1335 1316 } ··· 1358 1339 1359 1340 seq_puts(s, "Pinctrl maps:\n"); 1360 1341 1361 - mutex_lock(&pinctrl_mutex); 1362 - 1342 + mutex_lock(&pinctrl_maps_mutex); 1363 1343 for_each_maps(maps_node, i, map) { 1364 1344 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", 1365 1345 map->dev_name, map->name, map_type(map->type), ··· 1382 1364 1383 1365 seq_printf(s, "\n"); 1384 1366 } 1385 - 1386 - mutex_unlock(&pinctrl_mutex); 1367 + mutex_unlock(&pinctrl_maps_mutex); 1387 1368 1388 1369 return 0; 1389 1370 } ··· 1395 1378 1396 1379 seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); 1397 1380 1398 - mutex_lock(&pinctrl_mutex); 1381 + mutex_lock(&pinctrl_list_mutex); 1399 1382 1400 1383 list_for_each_entry(p, &pinctrl_list, node) { 1401 1384 seq_printf(s, "device: %s current state: %s\n", ··· 1427 1410 } 1428 1411 } 1429 1412 1430 - mutex_unlock(&pinctrl_mutex); 1413 + mutex_unlock(&pinctrl_list_mutex); 1431 1414 1432 1415 return 0; 1433 1416 } ··· 1613 1596 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); 1614 1597 INIT_LIST_HEAD(&pctldev->gpio_ranges); 1615 1598 pctldev->dev = dev; 1599 + mutex_init(&pctldev->mutex); 1616 1600 1617 1601 /* check core ops for sanity */ 1618 1602 if (pinctrl_check_ops(pctldev)) { ··· 1643 1625 goto out_err; 1644 1626 } 1645 1627 1646 - mutex_lock(&pinctrl_mutex); 1647 - 1628 + mutex_lock(&pinctrldev_list_mutex); 1648 1629 list_add_tail(&pctldev->node, &pinctrldev_list); 1630 + mutex_unlock(&pinctrldev_list_mutex); 1649 1631 1650 - pctldev->p = pinctrl_get_locked(pctldev->dev); 1632 + pctldev->p = pinctrl_get(pctldev->dev); 1633 + 1651 1634 if (!IS_ERR(pctldev->p)) { 1652 1635 pctldev->hog_default = 1653 - pinctrl_lookup_state_locked(pctldev->p, 1654 - PINCTRL_STATE_DEFAULT); 1636 + pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT); 1655 1637 if (IS_ERR(pctldev->hog_default)) { 1656 1638 dev_dbg(dev, "failed to lookup the default state\n"); 1657 1639 } else { 1658 - if (pinctrl_select_state_locked(pctldev->p, 1640 + if (pinctrl_select_state(pctldev->p, 1659 1641 pctldev->hog_default)) 1660 1642 dev_err(dev, 1661 1643 "failed to select default state\n"); 1662 1644 } 1663 1645 1664 1646 pctldev->hog_sleep = 1665 - pinctrl_lookup_state_locked(pctldev->p, 1647 + pinctrl_lookup_state(pctldev->p, 1666 1648 PINCTRL_STATE_SLEEP); 1667 1649 if (IS_ERR(pctldev->hog_sleep)) 1668 1650 dev_dbg(dev, "failed to lookup the sleep state\n"); 1669 1651 } 1670 - 1671 - mutex_unlock(&pinctrl_mutex); 1672 1652 1673 1653 pinctrl_init_device_debugfs(pctldev); 1674 1654 1675 1655 return pctldev; 1676 1656 1677 1657 out_err: 1658 + mutex_destroy(&pctldev->mutex); 1678 1659 kfree(pctldev); 1679 1660 return NULL; 1680 1661 } ··· 1691 1674 if (pctldev == NULL) 1692 1675 return; 1693 1676 1677 + mutex_lock(&pinctrldev_list_mutex); 1678 + mutex_lock(&pctldev->mutex); 1679 + 1694 1680 pinctrl_remove_device_debugfs(pctldev); 1695 1681 1696 - mutex_lock(&pinctrl_mutex); 1697 - 1698 1682 if (!IS_ERR(pctldev->p)) 1699 - pinctrl_put_locked(pctldev->p, true); 1683 + pinctrl_put(pctldev->p); 1700 1684 1701 1685 /* TODO: check that no pinmuxes are still active? */ 1702 1686 list_del(&pctldev->node); ··· 1708 1690 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) 1709 1691 list_del(&range->node); 1710 1692 1693 + mutex_unlock(&pctldev->mutex); 1694 + mutex_destroy(&pctldev->mutex); 1711 1695 kfree(pctldev); 1712 - 1713 - mutex_unlock(&pinctrl_mutex); 1696 + mutex_unlock(&pinctrldev_list_mutex); 1714 1697 } 1715 1698 EXPORT_SYMBOL_GPL(pinctrl_unregister); 1716 1699
+4 -2
drivers/pinctrl/core.h
··· 33 33 * @p: result of pinctrl_get() for this device 34 34 * @hog_default: default state for pins hogged by this device 35 35 * @hog_sleep: sleep state for pins hogged by this device 36 + * @mutex: mutex taken on each pin controller specific action 36 37 * @device_root: debugfs root for this device 37 38 */ 38 39 struct pinctrl_dev { ··· 47 46 struct pinctrl *p; 48 47 struct pinctrl_state *hog_default; 49 48 struct pinctrl_state *hog_sleep; 49 + struct mutex mutex; 50 50 #ifdef CONFIG_DEBUG_FS 51 51 struct dentry *device_root; 52 52 #endif ··· 170 168 }; 171 169 172 170 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name); 171 + struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np); 173 172 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name); 174 173 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin); 175 174 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, ··· 189 186 extern int pinctrl_force_sleep(struct pinctrl_dev *pctldev); 190 187 extern int pinctrl_force_default(struct pinctrl_dev *pctldev); 191 188 192 - extern struct mutex pinctrl_mutex; 193 - extern struct list_head pinctrldev_list; 189 + extern struct mutex pinctrl_maps_mutex; 194 190 extern struct list_head pinctrl_maps; 195 191 196 192 #define for_each_maps(_maps_node_, _i_, _map_) \
+2 -13
drivers/pinctrl/devicetree.c
··· 95 95 return pinctrl_register_map(map, num_maps, false, true); 96 96 } 97 97 98 - static struct pinctrl_dev *find_pinctrl_by_of_node(struct device_node *np) 99 - { 100 - struct pinctrl_dev *pctldev; 101 - 102 - list_for_each_entry(pctldev, &pinctrldev_list, node) 103 - if (pctldev->dev->of_node == np) 104 - return pctldev; 105 - 106 - return NULL; 107 - } 108 - 109 98 struct pinctrl_dev *of_pinctrl_get(struct device_node *np) 110 99 { 111 100 struct pinctrl_dev *pctldev; 112 101 113 - pctldev = find_pinctrl_by_of_node(np); 102 + pctldev = get_pinctrl_dev_from_of_node(np); 114 103 if (!pctldev) 115 104 return NULL; 116 105 ··· 127 138 /* OK let's just assume this will appear later then */ 128 139 return -EPROBE_DEFER; 129 140 } 130 - pctldev = find_pinctrl_by_of_node(np_pctldev); 141 + pctldev = get_pinctrl_dev_from_of_node(np_pctldev); 131 142 if (pctldev) 132 143 break; 133 144 /* Do not defer probing of hogs (circular loop) */
+24 -22
drivers/pinctrl/pinconf.c
··· 89 89 struct pinctrl_dev *pctldev; 90 90 int pin; 91 91 92 - mutex_lock(&pinctrl_mutex); 93 - 94 92 pctldev = get_pinctrl_dev_from_devname(dev_name); 95 93 if (!pctldev) { 96 94 pin = -EINVAL; 97 - goto unlock; 95 + return pin; 98 96 } 97 + 98 + mutex_lock(&pctldev->mutex); 99 99 100 100 pin = pin_get_from_name(pctldev, name); 101 101 if (pin < 0) ··· 104 104 pin = pin_config_get_for_pin(pctldev, pin, config); 105 105 106 106 unlock: 107 - mutex_unlock(&pinctrl_mutex); 107 + mutex_unlock(&pctldev->mutex); 108 108 return pin; 109 109 } 110 110 EXPORT_SYMBOL(pin_config_get); ··· 145 145 struct pinctrl_dev *pctldev; 146 146 int pin, ret; 147 147 148 - mutex_lock(&pinctrl_mutex); 149 - 150 148 pctldev = get_pinctrl_dev_from_devname(dev_name); 151 149 if (!pctldev) { 152 150 ret = -EINVAL; 153 - goto unlock; 151 + return ret; 154 152 } 153 + 154 + mutex_lock(&pctldev->mutex); 155 155 156 156 pin = pin_get_from_name(pctldev, name); 157 157 if (pin < 0) { ··· 162 162 ret = pin_config_set_for_pin(pctldev, pin, config); 163 163 164 164 unlock: 165 - mutex_unlock(&pinctrl_mutex); 165 + mutex_unlock(&pctldev->mutex); 166 166 return ret; 167 167 } 168 168 EXPORT_SYMBOL(pin_config_set); ··· 174 174 const struct pinconf_ops *ops; 175 175 int selector, ret; 176 176 177 - mutex_lock(&pinctrl_mutex); 178 - 179 177 pctldev = get_pinctrl_dev_from_devname(dev_name); 180 178 if (!pctldev) { 181 179 ret = -EINVAL; 182 - goto unlock; 180 + return ret; 183 181 } 182 + 183 + mutex_lock(&pctldev->mutex); 184 + 184 185 ops = pctldev->desc->confops; 185 186 186 187 if (!ops || !ops->pin_config_group_get) { ··· 201 200 ret = ops->pin_config_group_get(pctldev, selector, config); 202 201 203 202 unlock: 204 - mutex_unlock(&pinctrl_mutex); 203 + mutex_unlock(&pctldev->mutex); 205 204 return ret; 206 205 } 207 206 EXPORT_SYMBOL(pin_config_group_get); ··· 218 217 int ret; 219 218 int i; 220 219 221 - mutex_lock(&pinctrl_mutex); 222 - 223 220 pctldev = get_pinctrl_dev_from_devname(dev_name); 224 221 if (!pctldev) { 225 222 ret = -EINVAL; 226 - goto unlock; 223 + return ret; 227 224 } 225 + 226 + mutex_lock(&pctldev->mutex); 227 + 228 228 ops = pctldev->desc->confops; 229 229 pctlops = pctldev->desc->pctlops; 230 230 ··· 281 279 ret = 0; 282 280 283 281 unlock: 284 - mutex_unlock(&pinctrl_mutex); 282 + mutex_unlock(&pctldev->mutex); 285 283 286 284 return ret; 287 285 } ··· 489 487 seq_puts(s, "Pin config settings per pin\n"); 490 488 seq_puts(s, "Format: pin (name): configs\n"); 491 489 492 - mutex_lock(&pinctrl_mutex); 490 + mutex_lock(&pctldev->mutex); 493 491 494 492 /* The pin number can be retrived from the pin controller descriptor */ 495 493 for (i = 0; i < pctldev->desc->npins; i++) { ··· 509 507 seq_printf(s, "\n"); 510 508 } 511 509 512 - mutex_unlock(&pinctrl_mutex); 510 + mutex_unlock(&pctldev->mutex); 513 511 514 512 return 0; 515 513 } ··· 610 608 bool found = false; 611 609 unsigned long config; 612 610 613 - mutex_lock(&pinctrl_mutex); 611 + mutex_lock(&pctldev->mutex); 614 612 615 613 /* Parse the pinctrl map and look for the elected pin/state */ 616 614 for_each_maps(maps_node, i, map) { ··· 659 657 confops->pin_config_config_dbg_show(pctldev, s, config); 660 658 661 659 exit: 662 - mutex_unlock(&pinctrl_mutex); 660 + mutex_unlock(&pctldev->mutex); 663 661 664 662 return 0; 665 663 } ··· 749 747 return -EINVAL; 750 748 strncpy(config, token, MAX_NAME_LEN); 751 749 752 - mutex_lock(&pinctrl_mutex); 750 + mutex_lock(&pinctrl_maps_mutex); 753 751 754 752 /* Parse the pinctrl map and look for the selected dev/state/pin */ 755 753 for_each_maps(maps_node, i, map) { ··· 787 785 } 788 786 789 787 exit: 790 - mutex_unlock(&pinctrl_mutex); 788 + mutex_unlock(&pinctrl_maps_mutex); 791 789 792 790 return count; 793 791 }
+4 -4
drivers/pinctrl/pinmux.c
··· 506 506 if (!pmxops) 507 507 return 0; 508 508 509 - mutex_lock(&pinctrl_mutex); 509 + mutex_lock(&pctldev->mutex); 510 510 nfuncs = pmxops->get_functions_count(pctldev); 511 511 while (func_selector < nfuncs) { 512 512 const char *func = pmxops->get_function_name(pctldev, ··· 530 530 func_selector++; 531 531 } 532 532 533 - mutex_unlock(&pinctrl_mutex); 533 + mutex_unlock(&pctldev->mutex); 534 534 535 535 return 0; 536 536 } ··· 548 548 seq_puts(s, "Pinmux settings per pin\n"); 549 549 seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n"); 550 550 551 - mutex_lock(&pinctrl_mutex); 551 + mutex_lock(&pctldev->mutex); 552 552 553 553 /* The pin number can be retrived from the pin controller descriptor */ 554 554 for (i = 0; i < pctldev->desc->npins; i++) { ··· 583 583 seq_printf(s, "\n"); 584 584 } 585 585 586 - mutex_unlock(&pinctrl_mutex); 586 + mutex_unlock(&pctldev->mutex); 587 587 588 588 return 0; 589 589 }