Commit 1a78958dc212f3698fdc543857af80155cb30f7f

Authored by Linus Walleij
1 parent 77b67063bb

pinctrl: reserve pins when states are activated

This switches the way that pins are reserved for multiplexing:

We used to do this when the map was parsed, at the creation of
the settings inside the pinctrl handle, in pinmux_map_to_setting().

However this does not work for us, because we want to use the
same set of pins with different devices at different times: the
current code assumes that the pin groups in a pinmux state will
only be used with one single device, albeit different groups can
be active at different times. For example if a single I2C driver
block is used to drive two different busses located on two
pin groups A and B, then the pins for all possible states of a
function are reserved when fetching the pinctrl handle: the
I2C bus can choose either set A or set B by a mux state at
runtime, but all pins in both group A and B (the superset) are
effectively reserved for that I2C function and mapped to the
device. Another device can never get in and use the pins in
group A, even if the device/function is using group B at the
moment.

Instead: let use reserve the pins when the state is activated
and drop them when the state is disabled, i.e. when we move to
another state. This way different devices/functions can use the
same pins at different times.

We know that this is an odd way of doing things, but we really
need to switch e.g. an SD-card slot to become a tracing output
sink at runtime: we plug in a special "tracing card" then mux
the pins that used to be an SD slot around to the tracing
unit and push out tracing data there instead of SD-card
traffic.

As a side effect pinmux_free_setting() is unused but the stubs
are kept for future additions of code.

Cc: Patrice Chotard <patrice.chotard@st.com>
Cc: Loic Pallardy <loic.pallardy@st.com>
Acked-by: Stephen Warren <swarren@nvidia.com>
Tested-by: Jean Nicolas Graux <jean-nicolas.graux@stericsson.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

Showing 4 changed files with 29 additions and 46 deletions Side-by-side Diff

Documentation/pinctrl.txt
... ... @@ -1193,5 +1193,7 @@
1193 1193 ...
1194 1194 }
1195 1195  
1196   -The above has to be done from process context.
  1196 +The above has to be done from process context. The reservation of the pins
  1197 +will be done when the state is activated, so in effect one specific pin
  1198 +can be used by different functions at different times on a running system.
drivers/pinctrl/core.c
... ... @@ -563,6 +563,8 @@
563 563 return -EPROBE_DEFER;
564 564 }
565 565  
  566 + setting->dev_name = map->dev_name;
  567 +
566 568 switch (map->type) {
567 569 case PIN_MAP_TYPE_MUX_GROUP:
568 570 ret = pinmux_map_to_setting(map, setting);
drivers/pinctrl/core.h
... ... @@ -105,12 +105,14 @@
105 105 * @type: the type of setting
106 106 * @pctldev: pin control device handling to be programmed. Not used for
107 107 * PIN_MAP_TYPE_DUMMY_STATE.
  108 + * @dev_name: the name of the device using this state
108 109 * @data: Data specific to the setting type
109 110 */
110 111 struct pinctrl_setting {
111 112 struct list_head node;
112 113 enum pinctrl_map_type type;
113 114 struct pinctrl_dev *pctldev;
  115 + const char *dev_name;
114 116 union {
115 117 struct pinctrl_setting_mux mux;
116 118 struct pinctrl_setting_configs configs;
drivers/pinctrl/pinmux.c
... ... @@ -314,14 +314,11 @@
314 314 {
315 315 struct pinctrl_dev *pctldev = setting->pctldev;
316 316 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
317   - const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
318 317 char const * const *groups;
319 318 unsigned num_groups;
320 319 int ret;
321 320 const char *group;
322 321 int i;
323   - const unsigned *pins;
324   - unsigned num_pins;
325 322  
326 323 if (!pmxops) {
327 324 dev_err(pctldev->dev, "does not support mux function\n");
328 325  
... ... @@ -376,53 +373,12 @@
376 373 }
377 374 setting->data.mux.group = ret;
378 375  
379   - ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins,
380   - &num_pins);
381   - if (ret) {
382   - dev_err(pctldev->dev,
383   - "could not get pins for device %s group selector %d\n",
384   - pinctrl_dev_get_name(pctldev), setting->data.mux.group);
385   - return -ENODEV;
386   - }
387   -
388   - /* Try to allocate all pins in this group, one by one */
389   - for (i = 0; i < num_pins; i++) {
390   - ret = pin_request(pctldev, pins[i], map->dev_name, NULL);
391   - if (ret) {
392   - dev_err(pctldev->dev,
393   - "could not request pin %d on device %s\n",
394   - pins[i], pinctrl_dev_get_name(pctldev));
395   - /* On error release all taken pins */
396   - i--; /* this pin just failed */
397   - for (; i >= 0; i--)
398   - pin_free(pctldev, pins[i], NULL);
399   - return -ENODEV;
400   - }
401   - }
402   -
403 376 return 0;
404 377 }
405 378  
406 379 void pinmux_free_setting(struct pinctrl_setting const *setting)
407 380 {
408   - struct pinctrl_dev *pctldev = setting->pctldev;
409   - const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
410   - const unsigned *pins;
411   - unsigned num_pins;
412   - int ret;
413   - int i;
414   -
415   - ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
416   - &pins, &num_pins);
417   - if (ret) {
418   - dev_err(pctldev->dev,
419   - "could not get pins for device %s group selector %d\n",
420   - pinctrl_dev_get_name(pctldev), setting->data.mux.group);
421   - return;
422   - }
423   -
424   - for (i = 0; i < num_pins; i++)
425   - pin_free(pctldev, pins[i], NULL);
  381 + /* This function is currently unused */
426 382 }
427 383  
428 384 int pinmux_enable_setting(struct pinctrl_setting const *setting)
429 385  
... ... @@ -446,7 +402,23 @@
446 402 num_pins = 0;
447 403 }
448 404  
  405 + /* Try to allocate all pins in this group, one by one */
449 406 for (i = 0; i < num_pins; i++) {
  407 + ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
  408 + if (ret) {
  409 + dev_err(pctldev->dev,
  410 + "could not request pin %d on device %s\n",
  411 + pins[i], pinctrl_dev_get_name(pctldev));
  412 + /* On error release all taken pins */
  413 + i--; /* this pin just failed */
  414 + for (; i >= 0; i--)
  415 + pin_free(pctldev, pins[i], NULL);
  416 + return -ENODEV;
  417 + }
  418 + }
  419 +
  420 + /* Now that we have acquired the pins, encode the mux setting */
  421 + for (i = 0; i < num_pins; i++) {
450 422 desc = pin_desc_get(pctldev, pins[i]);
451 423 if (desc == NULL) {
452 424 dev_warn(pctldev->dev,
... ... @@ -482,6 +454,7 @@
482 454 num_pins = 0;
483 455 }
484 456  
  457 + /* Flag the descs that no setting is active */
485 458 for (i = 0; i < num_pins; i++) {
486 459 desc = pin_desc_get(pctldev, pins[i]);
487 460 if (desc == NULL) {
... ... @@ -492,6 +465,10 @@
492 465 }
493 466 desc->mux_setting = NULL;
494 467 }
  468 +
  469 + /* And release the pins */
  470 + for (i = 0; i < num_pins; i++)
  471 + pin_free(pctldev, pins[i], NULL);
495 472  
496 473 if (ops->disable)
497 474 ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group);