Skip to content

Commit e10f72b

Browse files
amboarLinus Walleij
authored andcommitted
gpio: gpiolib: Generalise state persistence beyond sleep
General support for state persistence is added to gpiolib with the introduction of a new pinconf parameter to propagate the request to hardware. The existing persistence support for sleep is adapted to include hardware support if the GPIO driver provides it. Persistence continues to be enabled by default; in-kernel consumers can opt out, but userspace (currently) does not have a choice. The *_SLEEP_MAY_LOSE_VALUE and *_SLEEP_MAINTAIN_VALUE symbols are renamed, dropping the SLEEP prefix to reflect that the concept is no longer sleep-specific. I feel that renaming to just *_MAY_LOSE_VALUE could initially be misinterpreted, so I've further changed the symbols to *_TRANSITORY and *_PERSISTENT to address this. The sysfs interface is modified only to keep consistency with the chardev interface in enforcing persistence for userspace exports. Signed-off-by: Andrew Jeffery <andrew@aj.id.au> Reviewed-by: Charles Keepax <ckeepax@opensource.cirrus.com> Acked-by: Rob Herring <robh@kernel.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent e2843cb commit e10f72b

9 files changed

Lines changed: 87 additions & 18 deletions

File tree

‎drivers/gpio/gpiolib-of.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
153153
*flags |= GPIO_OPEN_SOURCE;
154154
}
155155

156-
if (of_flags & OF_GPIO_SLEEP_MAY_LOSE_VALUE)
157-
*flags |= GPIO_SLEEP_MAY_LOSE_VALUE;
156+
if (of_flags & OF_GPIO_TRANSITORY)
157+
*flags |= GPIO_TRANSITORY;
158158

159159
return desc;
160160
}
@@ -214,6 +214,8 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
214214

215215
if (xlate_flags & OF_GPIO_ACTIVE_LOW)
216216
*lflags |= GPIO_ACTIVE_LOW;
217+
if (xlate_flags & OF_GPIO_TRANSITORY)
218+
*lflags |= GPIO_TRANSITORY;
217219

218220
if (of_property_read_bool(np, "input"))
219221
*dflags |= GPIOD_IN;

‎drivers/gpio/gpiolib-sysfs.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,15 @@ static ssize_t export_store(struct class *class,
474474
status = -ENODEV;
475475
goto done;
476476
}
477-
status = gpiod_export(desc, true);
478-
if (status < 0)
479-
gpiod_free(desc);
480-
else
481-
set_bit(FLAG_SYSFS, &desc->flags);
477+
478+
status = gpiod_set_transitory(desc, false);
479+
if (!status) {
480+
status = gpiod_export(desc, true);
481+
if (status < 0)
482+
gpiod_free(desc);
483+
else
484+
set_bit(FLAG_SYSFS, &desc->flags);
485+
}
482486

483487
done:
484488
if (status)

‎drivers/gpio/gpiolib.c‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
514514
if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
515515
set_bit(FLAG_OPEN_SOURCE, &desc->flags);
516516

517+
ret = gpiod_set_transitory(desc, false);
518+
if (ret < 0)
519+
goto out_free_descs;
520+
517521
/*
518522
* Lines have to be requested explicitly for input
519523
* or output, else the line will be treated "as is".
@@ -2529,6 +2533,49 @@ int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
25292533
}
25302534
EXPORT_SYMBOL_GPL(gpiod_set_debounce);
25312535

2536+
/**
2537+
* gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2538+
* @desc: descriptor of the GPIO for which to configure persistence
2539+
* @transitory: True to lose state on suspend or reset, false for persistence
2540+
*
2541+
* Returns:
2542+
* 0 on success, otherwise a negative error code.
2543+
*/
2544+
int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2545+
{
2546+
struct gpio_chip *chip;
2547+
unsigned long packed;
2548+
int gpio;
2549+
int rc;
2550+
2551+
/*
2552+
* Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2553+
* persistence state.
2554+
*/
2555+
if (transitory)
2556+
set_bit(FLAG_TRANSITORY, &desc->flags);
2557+
else
2558+
clear_bit(FLAG_TRANSITORY, &desc->flags);
2559+
2560+
/* If the driver supports it, set the persistence state now */
2561+
chip = desc->gdev->chip;
2562+
if (!chip->set_config)
2563+
return 0;
2564+
2565+
packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2566+
!transitory);
2567+
gpio = gpio_chip_hwgpio(desc);
2568+
rc = chip->set_config(chip, gpio, packed);
2569+
if (rc == -ENOTSUPP) {
2570+
dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2571+
gpio);
2572+
return 0;
2573+
}
2574+
2575+
return rc;
2576+
}
2577+
EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2578+
25322579
/**
25332580
* gpiod_is_active_low - test whether a GPIO is active-low or not
25342581
* @desc: the gpio descriptor to test
@@ -3116,8 +3163,7 @@ bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
31163163
if (offset >= chip->ngpio)
31173164
return false;
31183165

3119-
return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
3120-
&chip->gpiodev->descs[offset].flags);
3166+
return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
31213167
}
31223168
EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
31233169

@@ -3554,8 +3600,10 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
35543600

35553601
if (lflags & GPIO_OPEN_SOURCE)
35563602
set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3557-
if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3558-
set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
3603+
3604+
status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3605+
if (status < 0)
3606+
return status;
35593607

35603608
/* No particular flag request, return here... */
35613609
if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
@@ -3669,6 +3717,7 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
36693717
bool active_low = false;
36703718
bool single_ended = false;
36713719
bool open_drain = false;
3720+
bool transitory = false;
36723721
int ret;
36733722

36743723
if (!fwnode)
@@ -3683,6 +3732,7 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
36833732
active_low = flags & OF_GPIO_ACTIVE_LOW;
36843733
single_ended = flags & OF_GPIO_SINGLE_ENDED;
36853734
open_drain = flags & OF_GPIO_OPEN_DRAIN;
3735+
transitory = flags & OF_GPIO_TRANSITORY;
36863736
}
36873737
} else if (is_acpi_node(fwnode)) {
36883738
struct acpi_gpio_info info;
@@ -3711,6 +3761,9 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
37113761
lflags |= GPIO_OPEN_SOURCE;
37123762
}
37133763

3764+
if (transitory)
3765+
lflags |= GPIO_TRANSITORY;
3766+
37143767
ret = gpiod_configure_flags(desc, propname, lflags, dflags);
37153768
if (ret < 0) {
37163769
gpiod_put(desc);

‎drivers/gpio/gpiolib.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ struct gpio_desc {
209209
#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
210210
#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
211211
#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
212-
#define FLAG_SLEEP_MAY_LOSE_VALUE 12 /* GPIO may lose value in sleep */
212+
#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
213213

214214
/* Connection label */
215215
const char *label;

‎include/dt-bindings/gpio/gpio.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#define GPIO_OPEN_DRAIN (GPIO_SINGLE_ENDED | GPIO_LINE_OPEN_DRAIN)
3030
#define GPIO_OPEN_SOURCE (GPIO_SINGLE_ENDED | GPIO_LINE_OPEN_SOURCE)
3131

32-
/* Bit 3 express GPIO suspend/resume persistence */
33-
#define GPIO_SLEEP_MAINTAIN_VALUE 0
34-
#define GPIO_SLEEP_MAY_LOSE_VALUE 8
32+
/* Bit 3 express GPIO suspend/resume and reset persistence */
33+
#define GPIO_PERSISTENT 0
34+
#define GPIO_TRANSITORY 8
3535

3636
#endif

‎include/linux/gpio/consumer.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
139139
int *value_array);
140140

141141
int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
142+
int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
142143

143144
int gpiod_is_active_low(const struct gpio_desc *desc);
144145
int gpiod_cansleep(const struct gpio_desc *desc);
@@ -431,6 +432,13 @@ static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
431432
return -ENOSYS;
432433
}
433434

435+
static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
436+
{
437+
/* GPIO can never have been requested */
438+
WARN_ON(1);
439+
return -ENOSYS;
440+
}
441+
434442
static inline int gpiod_is_active_low(const struct gpio_desc *desc)
435443
{
436444
/* GPIO can never have been requested */

‎include/linux/gpio/machine.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ enum gpio_lookup_flags {
1010
GPIO_ACTIVE_LOW = (1 << 0),
1111
GPIO_OPEN_DRAIN = (1 << 1),
1212
GPIO_OPEN_SOURCE = (1 << 2),
13-
GPIO_SLEEP_MAINTAIN_VALUE = (0 << 3),
14-
GPIO_SLEEP_MAY_LOSE_VALUE = (1 << 3),
13+
GPIO_PERSISTENT = (0 << 3),
14+
GPIO_TRANSITORY = (1 << 3),
1515
};
1616

1717
/**

‎include/linux/of_gpio.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum of_gpio_flags {
3131
OF_GPIO_ACTIVE_LOW = 0x1,
3232
OF_GPIO_SINGLE_ENDED = 0x2,
3333
OF_GPIO_OPEN_DRAIN = 0x4,
34-
OF_GPIO_SLEEP_MAY_LOSE_VALUE = 0x8,
34+
OF_GPIO_TRANSITORY = 0x8,
3535
};
3636

3737
#ifdef CONFIG_OF_GPIO

‎include/linux/pinctrl/pinconf-generic.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
* or latch delay (on outputs) this parameter (in a custom format)
9595
* specifies the clock skew or latch delay. It typically controls how
9696
* many double inverters are put in front of the line.
97+
* @PIN_CONFIG_PERSIST_STATE: retain pin state across sleep or controller reset
9798
* @PIN_CONFIG_END: this is the last enumerator for pin configurations, if
9899
* you need to pass in custom configurations to the pin controller, use
99100
* PIN_CONFIG_END+1 as the base offset.
@@ -122,6 +123,7 @@ enum pin_config_param {
122123
PIN_CONFIG_SLEEP_HARDWARE_STATE,
123124
PIN_CONFIG_SLEW_RATE,
124125
PIN_CONFIG_SKEW_DELAY,
126+
PIN_CONFIG_PERSIST_STATE,
125127
PIN_CONFIG_END = 0x7F,
126128
PIN_CONFIG_MAX = 0xFF,
127129
};

0 commit comments

Comments
 (0)