Skip to content

Commit 25553ff

Browse files
ldewanganglikely
authored andcommitted
gpio: gpiolib: Support for open source/emitter gpios
Adding support for the open source gpio on which client can specify the open source property through GPIO flag GPIOF_OPEN_SOURCE at the time of gpio request. The open source pins are normally pulled low and it cannot be driven to output with value of 0 and so when client request for setting the pin to LOW, the gpio will be set to input direction to make pin in tristate and hence PULL-DOWN on pins will make the state to LOW. The open source pin can be driven to HIGH by setting output with value of 1. 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 aca5ce1 commit 25553ff

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

drivers/gpio/gpiolib.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct gpio_desc {
5959
#define FLAG_TRIG_RISE 6 /* trigger on rising edge */
6060
#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */
6161
#define FLAG_OPEN_DRAIN 8 /* Gpio is open drain type */
62+
#define FLAG_OPEN_SOURCE 9 /* Gpio is open source type */
6263

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

@@ -1266,6 +1267,7 @@ void gpio_free(unsigned gpio)
12661267
clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
12671268
clear_bit(FLAG_REQUESTED, &desc->flags);
12681269
clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1270+
clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
12691271
} else
12701272
WARN_ON(extra_checks);
12711273

@@ -1290,6 +1292,9 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
12901292
if (flags & GPIOF_OPEN_DRAIN)
12911293
set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags);
12921294

1295+
if (flags & GPIOF_OPEN_SOURCE)
1296+
set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags);
1297+
12931298
if (flags & GPIOF_DIR_IN)
12941299
err = gpio_direction_input(gpio);
12951300
else
@@ -1443,6 +1448,10 @@ int gpio_direction_output(unsigned gpio, int value)
14431448
if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
14441449
return gpio_direction_input(gpio);
14451450

1451+
/* Open source pin should not be driven to 0 */
1452+
if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1453+
return gpio_direction_input(gpio);
1454+
14461455
spin_lock_irqsave(&gpio_lock, flags);
14471456

14481457
if (!gpio_is_valid(gpio))
@@ -1604,6 +1613,32 @@ static void _gpio_set_open_drain_value(unsigned gpio,
16041613
__func__, gpio, err);
16051614
}
16061615

1616+
/*
1617+
* _gpio_set_open_source() - Set the open source gpio's value.
1618+
* @gpio: Gpio whose state need to be set.
1619+
* @chip: Gpio chip.
1620+
* @value: Non-zero for setting it HIGH otherise it will set to LOW.
1621+
*/
1622+
static void _gpio_set_open_source_value(unsigned gpio,
1623+
struct gpio_chip *chip, int value)
1624+
{
1625+
int err = 0;
1626+
if (value) {
1627+
err = chip->direction_output(chip, gpio - chip->base, 1);
1628+
if (!err)
1629+
set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1630+
} else {
1631+
err = chip->direction_input(chip, gpio - chip->base);
1632+
if (!err)
1633+
clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags);
1634+
}
1635+
trace_gpio_direction(gpio, !value, err);
1636+
if (err < 0)
1637+
pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1638+
__func__, gpio, err);
1639+
}
1640+
1641+
16071642
/**
16081643
* __gpio_set_value() - assign a gpio's value
16091644
* @gpio: gpio whose value will be assigned
@@ -1622,6 +1657,8 @@ void __gpio_set_value(unsigned gpio, int value)
16221657
trace_gpio_value(gpio, 0, value);
16231658
if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
16241659
_gpio_set_open_drain_value(gpio, chip, value);
1660+
else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1661+
_gpio_set_open_source_value(gpio, chip, value);
16251662
else
16261663
chip->set(chip, gpio - chip->base, value);
16271664
}
@@ -1692,6 +1729,8 @@ void gpio_set_value_cansleep(unsigned gpio, int value)
16921729
trace_gpio_value(gpio, 0, value);
16931730
if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags))
16941731
_gpio_set_open_drain_value(gpio, chip, value);
1732+
else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags))
1733+
_gpio_set_open_source_value(gpio, chip, value);
16951734
else
16961735
chip->set(chip, gpio - chip->base, value);
16971736
}

include/linux/gpio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
/* Gpio pin is open drain */
1818
#define GPIOF_OPEN_DRAIN (1 << 2)
1919

20+
/* Gpio pin is open source */
21+
#define GPIOF_OPEN_SOURCE (1 << 3)
22+
2023
/**
2124
* struct gpio - a structure describing a GPIO with configuration
2225
* @gpio: the GPIO number

0 commit comments

Comments
 (0)