Skip to content

Commit 92de6bc

Browse files
author
Linus Walleij
committed
Merge branch 'ib-pca953x-config' into devel
2 parents 5340f23 + 15add06 commit 92de6bc

8 files changed

Lines changed: 127 additions & 18 deletions

File tree

‎Documentation/devicetree/bindings/gpio/gpio.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ Optional standard bitfield specifiers for the last cell:
6767
https://en.wikipedia.org/wiki/Open_collector
6868
- Bit 3: 0 means the output should be maintained during sleep/low-power mode
6969
1 means the output state can be lost during sleep/low-power mode
70+
- Bit 4: 0 means no pull-up resistor should be enabled
71+
1 means a pull-up resistor should be enabled
72+
This setting only applies to hardware with a simple on/off
73+
control for pull-up configuration. If the hardware has more
74+
elaborate pull-up configuration, it should be represented
75+
using a pin control binding.
76+
- Bit 5: 0 means no pull-down resistor should be enabled
77+
1 means a pull-down resistor should be enabled
78+
This setting only applies to hardware with a simple on/off
79+
control for pull-down configuration. If the hardware has more
80+
elaborate pull-down configuration, it should be represented
81+
using a pin control binding.
7082

7183
1.1) GPIO specifier best practices
7284
----------------------------------

‎drivers/gpio/gpio-pca953x.c‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ static int pca953x_bank_shift(struct pca953x_chip *chip)
179179
#define PCA957x_BANK_OUTPUT BIT(5)
180180

181181
#define PCAL9xxx_BANK_IN_LATCH BIT(8 + 2)
182+
#define PCAL9xxx_BANK_PULL_EN BIT(8 + 3)
183+
#define PCAL9xxx_BANK_PULL_SEL BIT(8 + 4)
182184
#define PCAL9xxx_BANK_IRQ_MASK BIT(8 + 5)
183185
#define PCAL9xxx_BANK_IRQ_STAT BIT(8 + 6)
184186

@@ -200,6 +202,8 @@ static int pca953x_bank_shift(struct pca953x_chip *chip)
200202
* - Extended set, above 0x40, often chip specific.
201203
* - PCAL6524/PCAL9555A with custom PCAL IRQ handling:
202204
* Input latch register 0x40 + 2 * bank_size RW
205+
* Pull-up/pull-down enable reg 0x40 + 3 * bank_size RW
206+
* Pull-up/pull-down select reg 0x40 + 4 * bank_size RW
203207
* Interrupt mask register 0x40 + 5 * bank_size RW
204208
* Interrupt status register 0x40 + 6 * bank_size R
205209
*
@@ -248,7 +252,8 @@ static bool pca953x_readable_register(struct device *dev, unsigned int reg)
248252
}
249253

250254
if (chip->driver_data & PCA_PCAL) {
251-
bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_IRQ_MASK |
255+
bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
256+
PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK |
252257
PCAL9xxx_BANK_IRQ_STAT;
253258
}
254259

@@ -269,7 +274,8 @@ static bool pca953x_writeable_register(struct device *dev, unsigned int reg)
269274
}
270275

271276
if (chip->driver_data & PCA_PCAL)
272-
bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_IRQ_MASK;
277+
bank |= PCAL9xxx_BANK_IN_LATCH | PCAL9xxx_BANK_PULL_EN |
278+
PCAL9xxx_BANK_PULL_SEL | PCAL9xxx_BANK_IRQ_MASK;
273279

274280
return pca953x_check_register(chip, reg, bank);
275281
}
@@ -474,6 +480,61 @@ static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
474480
mutex_unlock(&chip->i2c_lock);
475481
}
476482

483+
static int pca953x_gpio_set_pull_up_down(struct pca953x_chip *chip,
484+
unsigned int offset,
485+
unsigned long config)
486+
{
487+
u8 pull_en_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_EN, offset,
488+
true, false);
489+
u8 pull_sel_reg = pca953x_recalc_addr(chip, PCAL953X_PULL_SEL, offset,
490+
true, false);
491+
u8 bit = BIT(offset % BANK_SZ);
492+
int ret;
493+
494+
/*
495+
* pull-up/pull-down configuration requires PCAL extended
496+
* registers
497+
*/
498+
if (!(chip->driver_data & PCA_PCAL))
499+
return -ENOTSUPP;
500+
501+
mutex_lock(&chip->i2c_lock);
502+
503+
/* Disable pull-up/pull-down */
504+
ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, 0);
505+
if (ret)
506+
goto exit;
507+
508+
/* Configure pull-up/pull-down */
509+
if (config == PIN_CONFIG_BIAS_PULL_UP)
510+
ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, bit);
511+
else if (config == PIN_CONFIG_BIAS_PULL_DOWN)
512+
ret = regmap_write_bits(chip->regmap, pull_sel_reg, bit, 0);
513+
if (ret)
514+
goto exit;
515+
516+
/* Enable pull-up/pull-down */
517+
ret = regmap_write_bits(chip->regmap, pull_en_reg, bit, bit);
518+
519+
exit:
520+
mutex_unlock(&chip->i2c_lock);
521+
return ret;
522+
}
523+
524+
static int pca953x_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
525+
unsigned long config)
526+
{
527+
struct pca953x_chip *chip = gpiochip_get_data(gc);
528+
529+
switch (config) {
530+
case PIN_CONFIG_BIAS_PULL_UP:
531+
case PIN_CONFIG_BIAS_PULL_DOWN:
532+
return pca953x_gpio_set_pull_up_down(chip, offset, config);
533+
default:
534+
return -ENOTSUPP;
535+
}
536+
}
537+
477538
static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
478539
{
479540
struct gpio_chip *gc;
@@ -486,6 +547,7 @@ static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
486547
gc->set = pca953x_gpio_set_value;
487548
gc->get_direction = pca953x_gpio_get_direction;
488549
gc->set_multiple = pca953x_gpio_set_multiple;
550+
gc->set_config = pca953x_gpio_set_config;
489551
gc->can_sleep = true;
490552

491553
gc->base = chip->gpio_start;

‎drivers/gpio/gpiolib-of.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
345345
if (of_flags & OF_GPIO_TRANSITORY)
346346
*flags |= GPIO_TRANSITORY;
347347

348+
if (of_flags & OF_GPIO_PULL_UP)
349+
*flags |= GPIO_PULL_UP;
350+
if (of_flags & OF_GPIO_PULL_DOWN)
351+
*flags |= GPIO_PULL_DOWN;
352+
348353
return desc;
349354
}
350355

‎drivers/gpio/gpiolib.c‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,6 +2555,14 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
25552555
* rely on gpio_request() having been called beforehand.
25562556
*/
25572557

2558+
static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
2559+
enum pin_config_param mode)
2560+
{
2561+
unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2562+
2563+
return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2564+
}
2565+
25582566
/**
25592567
* gpiod_direction_input - set the GPIO direction to input
25602568
* @desc: GPIO to set to input
@@ -2602,20 +2610,19 @@ int gpiod_direction_input(struct gpio_desc *desc)
26022610
if (status == 0)
26032611
clear_bit(FLAG_IS_OUT, &desc->flags);
26042612

2613+
if (test_bit(FLAG_PULL_UP, &desc->flags))
2614+
gpio_set_config(chip, gpio_chip_hwgpio(desc),
2615+
PIN_CONFIG_BIAS_PULL_UP);
2616+
else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2617+
gpio_set_config(chip, gpio_chip_hwgpio(desc),
2618+
PIN_CONFIG_BIAS_PULL_DOWN);
2619+
26052620
trace_gpio_direction(desc_to_gpio(desc), 1, status);
26062621

26072622
return status;
26082623
}
26092624
EXPORT_SYMBOL_GPL(gpiod_direction_input);
26102625

2611-
static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2612-
enum pin_config_param mode)
2613-
{
2614-
unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2615-
2616-
return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2617-
}
2618-
26192626
static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
26202627
{
26212628
struct gpio_chip *gc = desc->gdev->chip;
@@ -2712,25 +2719,25 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
27122719
gc = desc->gdev->chip;
27132720
if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
27142721
/* First see if we can enable open drain in hardware */
2715-
ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2716-
PIN_CONFIG_DRIVE_OPEN_DRAIN);
2722+
ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
2723+
PIN_CONFIG_DRIVE_OPEN_DRAIN);
27172724
if (!ret)
27182725
goto set_output_value;
27192726
/* Emulate open drain by not actively driving the line high */
27202727
if (value)
27212728
return gpiod_direction_input(desc);
27222729
}
27232730
else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2724-
ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2725-
PIN_CONFIG_DRIVE_OPEN_SOURCE);
2731+
ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
2732+
PIN_CONFIG_DRIVE_OPEN_SOURCE);
27262733
if (!ret)
27272734
goto set_output_value;
27282735
/* Emulate open source by not actively driving the line low */
27292736
if (!value)
27302737
return gpiod_direction_input(desc);
27312738
} else {
2732-
gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2733-
PIN_CONFIG_DRIVE_PUSH_PULL);
2739+
gpio_set_config(gc, gpio_chip_hwgpio(desc),
2740+
PIN_CONFIG_DRIVE_PUSH_PULL);
27342741
}
27352742

27362743
set_output_value:
@@ -2762,7 +2769,7 @@ int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
27622769
}
27632770

27642771
config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2765-
return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
2772+
return gpio_set_config(chip, gpio_chip_hwgpio(desc), config);
27662773
}
27672774
EXPORT_SYMBOL_GPL(gpiod_set_debounce);
27682775

@@ -2799,7 +2806,7 @@ int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
27992806
packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
28002807
!transitory);
28012808
gpio = gpio_chip_hwgpio(desc);
2802-
rc = chip->set_config(chip, gpio, packed);
2809+
rc = gpio_set_config(chip, gpio, packed);
28032810
if (rc == -ENOTSUPP) {
28042811
dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
28052812
gpio);
@@ -4087,6 +4094,17 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
40874094
if (lflags & GPIO_OPEN_SOURCE)
40884095
set_bit(FLAG_OPEN_SOURCE, &desc->flags);
40894096

4097+
if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4098+
gpiod_err(desc,
4099+
"both pull-up and pull-down enabled, invalid configuration\n");
4100+
return -EINVAL;
4101+
}
4102+
4103+
if (lflags & GPIO_PULL_UP)
4104+
set_bit(FLAG_PULL_UP, &desc->flags);
4105+
else if (lflags & GPIO_PULL_DOWN)
4106+
set_bit(FLAG_PULL_DOWN, &desc->flags);
4107+
40904108
status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
40914109
if (status < 0)
40924110
return status;

‎drivers/gpio/gpiolib.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ struct gpio_desc {
219219
#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
220220
#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
221221
#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
222+
#define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
223+
#define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
222224

223225
/* Connection label */
224226
const char *label;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@
3333
#define GPIO_PERSISTENT 0
3434
#define GPIO_TRANSITORY 8
3535

36+
/* Bit 4 express pull up */
37+
#define GPIO_PULL_UP 16
38+
39+
/* Bit 5 express pull down */
40+
#define GPIO_PULL_DOWN 32
41+
3642
#endif

‎include/linux/gpio/machine.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ enum gpio_lookup_flags {
1212
GPIO_OPEN_SOURCE = (1 << 2),
1313
GPIO_PERSISTENT = (0 << 3),
1414
GPIO_TRANSITORY = (1 << 3),
15+
GPIO_PULL_UP = (1 << 4),
16+
GPIO_PULL_DOWN = (1 << 5),
1517
};
1618

1719
/**

‎include/linux/of_gpio.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ enum of_gpio_flags {
2828
OF_GPIO_SINGLE_ENDED = 0x2,
2929
OF_GPIO_OPEN_DRAIN = 0x4,
3030
OF_GPIO_TRANSITORY = 0x8,
31+
OF_GPIO_PULL_UP = 0x10,
32+
OF_GPIO_PULL_DOWN = 0x20,
3133
};
3234

3335
#ifdef CONFIG_OF_GPIO

0 commit comments

Comments
 (0)