Skip to content

Commit aca5ce1

Browse files
ldewanganglikely
authored andcommitted
gpio: gpiolib: Support for open drain/collector gpios
Adding support for the open drain gpio on which client can specify the open drain property through GPIO flag GPIOF_OPEN_DRAIN at the time of gpio request. The open drain pins are normally pulled high and it cannot be driven to output with value of 1 and so when client request for setting the pin to HIGH, the gpio will be set to input direction to make pin in tristate and hence PULL-UP on pins will make the state to HIGH. The open drain pin can be driven to LOW by setting output with value of 0. Signed-off-by: Laxman Dewangan <[email protected]> Reviwed-by: Mark Brown <[email protected]> Acked-by: Linus Walleij <[email protected]> Signed-off-by: Grant Likely <[email protected]>
1 parent 3d2ddfd commit aca5ce1

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

drivers/gpio/gpiolib.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct gpio_desc {
5858
#define FLAG_TRIG_FALL 5 /* trigger on falling edge */
5959
#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
6060
#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
61+
#define FLAG_OPEN_DRAIN 8 /* Gpio is open drain type */
6162

6263
#define ID_SHIFT 16 /* add new flags before this one */
6364

@@ -1264,6 +1265,7 @@ void gpio_free(unsigned gpio)
12641265
module_put(desc->chip->owner);
12651266
clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
12661267
clear_bit(FLAG_REQUESTED, &desc->flags);
1268+
clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
12671269
} else
12681270
WARN_ON(extra_checks);
12691271

@@ -1285,6 +1287,9 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
12851287
if (err)
12861288
return err;
12871289

1290+
if (flags & GPIOF_OPEN_DRAIN)
1291+
set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
1292+
12881293
if (flags & GPIOF_DIR_IN)
12891294
err = gpio_direction_input(gpio);
12901295
else
@@ -1434,6 +1439,10 @@ int gpio_direction_output(unsigned gpio, int value)
14341439
struct gpio_desc *desc = &gpio_desc[gpio];
14351440
int status = -EINVAL;
14361441

1442+
/* Open drain pin should not be driven to 1 */
1443+
if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1444+
return gpio_direction_input(gpio);
1445+
14371446
spin_lock_irqsave(&gpio_lock, flags);
14381447

14391448
if (!gpio_is_valid(gpio))
@@ -1570,6 +1579,31 @@ int __gpio_get_value(unsigned gpio)
15701579
}
15711580
EXPORT_SYMBOL_GPL(__gpio_get_value);
15721581

1582+
/*
1583+
* _gpio_set_open_drain_value() - Set the open drain gpio's value.
1584+
* @gpio: Gpio whose state need to be set.
1585+
* @chip: Gpio chip.
1586+
* @value: Non-zero for setting it HIGH otherise it will set to LOW.
1587+
*/
1588+
static void _gpio_set_open_drain_value(unsigned gpio,
1589+
struct gpio_chip *chip, int value)
1590+
{
1591+
int err = 0;
1592+
if (value) {
1593+
err = chip->direction_input(chip, gpio - chip->base);
1594+
if (!err)
1595+
clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1596+
} else {
1597+
err = chip->direction_output(chip, gpio - chip->base, 0);
1598+
if (!err)
1599+
set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1600+
}
1601+
trace_gpio_direction(gpio, value, err);
1602+
if (err < 0)
1603+
pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1604+
__func__, gpio, err);
1605+
}
1606+
15731607
/**
15741608
* __gpio_set_value() - assign a gpio's value
15751609
* @gpio: gpio whose value will be assigned
@@ -1586,7 +1620,10 @@ void __gpio_set_value(unsigned gpio, int value)
15861620
chip = gpio_to_chip(gpio);
15871621
WARN_ON(chip->can_sleep);
15881622
trace_gpio_value(gpio, 0, value);
1589-
chip->set(chip, gpio - chip->base, value);
1623+
if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1624+
_gpio_set_open_drain_value(gpio, chip, value);
1625+
else
1626+
chip->set(chip, gpio - chip->base, value);
15901627
}
15911628
EXPORT_SYMBOL_GPL(__gpio_set_value);
15921629

@@ -1653,7 +1690,10 @@ void gpio_set_value_cansleep(unsigned gpio, int value)
16531690
might_sleep_if(extra_checks);
16541691
chip = gpio_to_chip(gpio);
16551692
trace_gpio_value(gpio, 0, value);
1656-
chip->set(chip, gpio - chip->base, value);
1693+
if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
1694+
_gpio_set_open_drain_value(gpio, chip, value);
1695+
else
1696+
chip->set(chip, gpio - chip->base, value);
16571697
}
16581698
EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
16591699

include/linux/gpio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW)
1515
#define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
1616

17+
/* Gpio pin is open drain */
18+
#define GPIOF_OPEN_DRAIN (1 << 2)
19+
1720
/**
1821
* struct gpio - a structure describing a GPIO with configuration
1922
* @gpio: the GPIO number

0 commit comments

Comments
 (0)