aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpiolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r--drivers/gpio/gpiolib.c241
1 files changed, 212 insertions, 29 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 58659dbe702a..487afe6f22fc 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -47,8 +47,6 @@
47 */ 47 */
48DEFINE_SPINLOCK(gpio_lock); 48DEFINE_SPINLOCK(gpio_lock);
49 49
50static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
51
52#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) 50#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
53 51
54static DEFINE_MUTEX(gpio_lookup_lock); 52static DEFINE_MUTEX(gpio_lookup_lock);
@@ -65,10 +63,24 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
65 */ 63 */
66struct gpio_desc *gpio_to_desc(unsigned gpio) 64struct gpio_desc *gpio_to_desc(unsigned gpio)
67{ 65{
68 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) 66 struct gpio_chip *chip;
69 return NULL; 67 unsigned long flags;
70 else 68
71 return &gpio_desc[gpio]; 69 spin_lock_irqsave(&gpio_lock, flags);
70
71 list_for_each_entry(chip, &gpio_chips, list) {
72 if (chip->base <= gpio && chip->base + chip->ngpio > gpio) {
73 spin_unlock_irqrestore(&gpio_lock, flags);
74 return &chip->desc[gpio - chip->base];
75 }
76 }
77
78 spin_unlock_irqrestore(&gpio_lock, flags);
79
80 if (!gpio_is_valid(gpio))
81 WARN(1, "invalid GPIO %d\n", gpio);
82
83 return NULL;
72} 84}
73EXPORT_SYMBOL_GPL(gpio_to_desc); 85EXPORT_SYMBOL_GPL(gpio_to_desc);
74 86
@@ -91,7 +103,7 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
91 */ 103 */
92int desc_to_gpio(const struct gpio_desc *desc) 104int desc_to_gpio(const struct gpio_desc *desc)
93{ 105{
94 return desc - &gpio_desc[0]; 106 return desc->chip->base + (desc - &desc->chip->desc[0]);
95} 107}
96EXPORT_SYMBOL_GPL(desc_to_gpio); 108EXPORT_SYMBOL_GPL(desc_to_gpio);
97 109
@@ -138,7 +150,7 @@ static int gpiochip_find_base(int ngpio)
138 * 150 *
139 * This function may sleep if gpiod_cansleep() is true. 151 * This function may sleep if gpiod_cansleep() is true.
140 */ 152 */
141int gpiod_get_direction(const struct gpio_desc *desc) 153int gpiod_get_direction(struct gpio_desc *desc)
142{ 154{
143 struct gpio_chip *chip; 155 struct gpio_chip *chip;
144 unsigned offset; 156 unsigned offset;
@@ -154,13 +166,11 @@ int gpiod_get_direction(const struct gpio_desc *desc)
154 if (status > 0) { 166 if (status > 0) {
155 /* GPIOF_DIR_IN, or other positive */ 167 /* GPIOF_DIR_IN, or other positive */
156 status = 1; 168 status = 1;
157 /* FLAG_IS_OUT is just a cache of the result of get_direction(), 169 clear_bit(FLAG_IS_OUT, &desc->flags);
158 * so it does not affect constness per se */
159 clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags);
160 } 170 }
161 if (status == 0) { 171 if (status == 0) {
162 /* GPIOF_DIR_OUT */ 172 /* GPIOF_DIR_OUT */
163 set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); 173 set_bit(FLAG_IS_OUT, &desc->flags);
164 } 174 }
165 return status; 175 return status;
166} 176}
@@ -206,7 +216,7 @@ static int gpiochip_add_to_list(struct gpio_chip *chip)
206/** 216/**
207 * gpiochip_add() - register a gpio_chip 217 * gpiochip_add() - register a gpio_chip
208 * @chip: the chip to register, with chip->base initialized 218 * @chip: the chip to register, with chip->base initialized
209 * Context: potentially before irqs or kmalloc will work 219 * Context: potentially before irqs will work
210 * 220 *
211 * Returns a negative errno if the chip can't be registered, such as 221 * Returns a negative errno if the chip can't be registered, such as
212 * because the chip->base is invalid or already associated with a 222 * because the chip->base is invalid or already associated with a
@@ -226,12 +236,11 @@ int gpiochip_add(struct gpio_chip *chip)
226 int status = 0; 236 int status = 0;
227 unsigned id; 237 unsigned id;
228 int base = chip->base; 238 int base = chip->base;
239 struct gpio_desc *descs;
229 240
230 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) 241 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
231 && base >= 0) { 242 if (!descs)
232 status = -EINVAL; 243 return -ENOMEM;
233 goto fail;
234 }
235 244
236 spin_lock_irqsave(&gpio_lock, flags); 245 spin_lock_irqsave(&gpio_lock, flags);
237 246
@@ -247,10 +256,8 @@ int gpiochip_add(struct gpio_chip *chip)
247 status = gpiochip_add_to_list(chip); 256 status = gpiochip_add_to_list(chip);
248 257
249 if (status == 0) { 258 if (status == 0) {
250 chip->desc = &gpio_desc[chip->base];
251
252 for (id = 0; id < chip->ngpio; id++) { 259 for (id = 0; id < chip->ngpio; id++) {
253 struct gpio_desc *desc = &chip->desc[id]; 260 struct gpio_desc *desc = &descs[id];
254 desc->chip = chip; 261 desc->chip = chip;
255 262
256 /* REVISIT: most hardware initializes GPIOs as 263 /* REVISIT: most hardware initializes GPIOs as
@@ -266,6 +273,8 @@ int gpiochip_add(struct gpio_chip *chip)
266 } 273 }
267 } 274 }
268 275
276 chip->desc = descs;
277
269 spin_unlock_irqrestore(&gpio_lock, flags); 278 spin_unlock_irqrestore(&gpio_lock, flags);
270 279
271#ifdef CONFIG_PINCTRL 280#ifdef CONFIG_PINCTRL
@@ -291,6 +300,9 @@ int gpiochip_add(struct gpio_chip *chip)
291unlock: 300unlock:
292 spin_unlock_irqrestore(&gpio_lock, flags); 301 spin_unlock_irqrestore(&gpio_lock, flags);
293fail: 302fail:
303 kfree(descs);
304 chip->desc = NULL;
305
294 /* failures here can mean systems won't boot... */ 306 /* failures here can mean systems won't boot... */
295 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, 307 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
296 chip->base, chip->base + chip->ngpio - 1, 308 chip->base, chip->base + chip->ngpio - 1,
@@ -331,6 +343,9 @@ void gpiochip_remove(struct gpio_chip *chip)
331 list_del(&chip->list); 343 list_del(&chip->list);
332 spin_unlock_irqrestore(&gpio_lock, flags); 344 spin_unlock_irqrestore(&gpio_lock, flags);
333 gpiochip_unexport(chip); 345 gpiochip_unexport(chip);
346
347 kfree(chip->desc);
348 chip->desc = NULL;
334} 349}
335EXPORT_SYMBOL_GPL(gpiochip_remove); 350EXPORT_SYMBOL_GPL(gpiochip_remove);
336 351
@@ -495,7 +510,7 @@ static int gpiochip_irq_reqres(struct irq_data *d)
495{ 510{
496 struct gpio_chip *chip = irq_data_get_irq_chip_data(d); 511 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
497 512
498 if (gpio_lock_as_irq(chip, d->hwirq)) { 513 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
499 chip_err(chip, 514 chip_err(chip,
500 "unable to lock HW IRQ %lu for IRQ\n", 515 "unable to lock HW IRQ %lu for IRQ\n",
501 d->hwirq); 516 d->hwirq);
@@ -508,7 +523,7 @@ static void gpiochip_irq_relres(struct irq_data *d)
508{ 523{
509 struct gpio_chip *chip = irq_data_get_irq_chip_data(d); 524 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
510 525
511 gpio_unlock_as_irq(chip, d->hwirq); 526 gpiochip_unlock_as_irq(chip, d->hwirq);
512} 527}
513 528
514static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) 529static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
@@ -1254,6 +1269,88 @@ static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
1254 chip->set(chip, gpio_chip_hwgpio(desc), value); 1269 chip->set(chip, gpio_chip_hwgpio(desc), value);
1255} 1270}
1256 1271
1272/*
1273 * set multiple outputs on the same chip;
1274 * use the chip's set_multiple function if available;
1275 * otherwise set the outputs sequentially;
1276 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1277 * defines which outputs are to be changed
1278 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1279 * defines the values the outputs specified by mask are to be set to
1280 */
1281static void gpio_chip_set_multiple(struct gpio_chip *chip,
1282 unsigned long *mask, unsigned long *bits)
1283{
1284 if (chip->set_multiple) {
1285 chip->set_multiple(chip, mask, bits);
1286 } else {
1287 int i;
1288 for (i = 0; i < chip->ngpio; i++) {
1289 if (mask[BIT_WORD(i)] == 0) {
1290 /* no more set bits in this mask word;
1291 * skip ahead to the next word */
1292 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1293 continue;
1294 }
1295 /* set outputs if the corresponding mask bit is set */
1296 if (__test_and_clear_bit(i, mask)) {
1297 chip->set(chip, i, test_bit(i, bits));
1298 }
1299 }
1300 }
1301}
1302
1303static void gpiod_set_array_priv(bool raw, bool can_sleep,
1304 unsigned int array_size,
1305 struct gpio_desc **desc_array,
1306 int *value_array)
1307{
1308 int i = 0;
1309
1310 while (i < array_size) {
1311 struct gpio_chip *chip = desc_array[i]->chip;
1312 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1313 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1314 int count = 0;
1315
1316 if (!can_sleep) {
1317 WARN_ON(chip->can_sleep);
1318 }
1319 memset(mask, 0, sizeof(mask));
1320 do {
1321 struct gpio_desc *desc = desc_array[i];
1322 int hwgpio = gpio_chip_hwgpio(desc);
1323 int value = value_array[i];
1324
1325 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1326 value = !value;
1327 trace_gpio_value(desc_to_gpio(desc), 0, value);
1328 /*
1329 * collect all normal outputs belonging to the same chip
1330 * open drain and open source outputs are set individually
1331 */
1332 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1333 _gpio_set_open_drain_value(desc,value);
1334 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1335 _gpio_set_open_source_value(desc, value);
1336 } else {
1337 __set_bit(hwgpio, mask);
1338 if (value) {
1339 __set_bit(hwgpio, bits);
1340 } else {
1341 __clear_bit(hwgpio, bits);
1342 }
1343 count++;
1344 }
1345 i++;
1346 } while ((i < array_size) && (desc_array[i]->chip == chip));
1347 /* push collected bits to outputs */
1348 if (count != 0) {
1349 gpio_chip_set_multiple(chip, mask, bits);
1350 }
1351 }
1352}
1353
1257/** 1354/**
1258 * gpiod_set_raw_value() - assign a gpio's raw value 1355 * gpiod_set_raw_value() - assign a gpio's raw value
1259 * @desc: gpio whose value will be assigned 1356 * @desc: gpio whose value will be assigned
@@ -1299,6 +1396,48 @@ void gpiod_set_value(struct gpio_desc *desc, int value)
1299EXPORT_SYMBOL_GPL(gpiod_set_value); 1396EXPORT_SYMBOL_GPL(gpiod_set_value);
1300 1397
1301/** 1398/**
1399 * gpiod_set_raw_array() - assign values to an array of GPIOs
1400 * @array_size: number of elements in the descriptor / value arrays
1401 * @desc_array: array of GPIO descriptors whose values will be assigned
1402 * @value_array: array of values to assign
1403 *
1404 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1405 * without regard for their ACTIVE_LOW status.
1406 *
1407 * This function should be called from contexts where we cannot sleep, and will
1408 * complain if the GPIO chip functions potentially sleep.
1409 */
1410void gpiod_set_raw_array(unsigned int array_size,
1411 struct gpio_desc **desc_array, int *value_array)
1412{
1413 if (!desc_array)
1414 return;
1415 gpiod_set_array_priv(true, false, array_size, desc_array, value_array);
1416}
1417EXPORT_SYMBOL_GPL(gpiod_set_raw_array);
1418
1419/**
1420 * gpiod_set_array() - assign values to an array of GPIOs
1421 * @array_size: number of elements in the descriptor / value arrays
1422 * @desc_array: array of GPIO descriptors whose values will be assigned
1423 * @value_array: array of values to assign
1424 *
1425 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1426 * into account.
1427 *
1428 * This function should be called from contexts where we cannot sleep, and will
1429 * complain if the GPIO chip functions potentially sleep.
1430 */
1431void gpiod_set_array(unsigned int array_size,
1432 struct gpio_desc **desc_array, int *value_array)
1433{
1434 if (!desc_array)
1435 return;
1436 gpiod_set_array_priv(false, false, array_size, desc_array, value_array);
1437}
1438EXPORT_SYMBOL_GPL(gpiod_set_array);
1439
1440/**
1302 * gpiod_cansleep() - report whether gpio value access may sleep 1441 * gpiod_cansleep() - report whether gpio value access may sleep
1303 * @desc: gpio to check 1442 * @desc: gpio to check
1304 * 1443 *
@@ -1332,14 +1471,14 @@ int gpiod_to_irq(const struct gpio_desc *desc)
1332EXPORT_SYMBOL_GPL(gpiod_to_irq); 1471EXPORT_SYMBOL_GPL(gpiod_to_irq);
1333 1472
1334/** 1473/**
1335 * gpio_lock_as_irq() - lock a GPIO to be used as IRQ 1474 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
1336 * @chip: the chip the GPIO to lock belongs to 1475 * @chip: the chip the GPIO to lock belongs to
1337 * @offset: the offset of the GPIO to lock as IRQ 1476 * @offset: the offset of the GPIO to lock as IRQ
1338 * 1477 *
1339 * This is used directly by GPIO drivers that want to lock down 1478 * This is used directly by GPIO drivers that want to lock down
1340 * a certain GPIO line to be used for IRQs. 1479 * a certain GPIO line to be used for IRQs.
1341 */ 1480 */
1342int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) 1481int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1343{ 1482{
1344 if (offset >= chip->ngpio) 1483 if (offset >= chip->ngpio)
1345 return -EINVAL; 1484 return -EINVAL;
@@ -1354,24 +1493,24 @@ int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
1354 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); 1493 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1355 return 0; 1494 return 0;
1356} 1495}
1357EXPORT_SYMBOL_GPL(gpio_lock_as_irq); 1496EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
1358 1497
1359/** 1498/**
1360 * gpio_unlock_as_irq() - unlock a GPIO used as IRQ 1499 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
1361 * @chip: the chip the GPIO to lock belongs to 1500 * @chip: the chip the GPIO to lock belongs to
1362 * @offset: the offset of the GPIO to lock as IRQ 1501 * @offset: the offset of the GPIO to lock as IRQ
1363 * 1502 *
1364 * This is used directly by GPIO drivers that want to indicate 1503 * This is used directly by GPIO drivers that want to indicate
1365 * that a certain GPIO is no longer used exclusively for IRQ. 1504 * that a certain GPIO is no longer used exclusively for IRQ.
1366 */ 1505 */
1367void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) 1506void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
1368{ 1507{
1369 if (offset >= chip->ngpio) 1508 if (offset >= chip->ngpio)
1370 return; 1509 return;
1371 1510
1372 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); 1511 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
1373} 1512}
1374EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); 1513EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
1375 1514
1376/** 1515/**
1377 * gpiod_get_raw_value_cansleep() - return a gpio's raw value 1516 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
@@ -1458,6 +1597,50 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1458EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); 1597EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
1459 1598
1460/** 1599/**
1600 * gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs
1601 * @array_size: number of elements in the descriptor / value arrays
1602 * @desc_array: array of GPIO descriptors whose values will be assigned
1603 * @value_array: array of values to assign
1604 *
1605 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1606 * without regard for their ACTIVE_LOW status.
1607 *
1608 * This function is to be called from contexts that can sleep.
1609 */
1610void gpiod_set_raw_array_cansleep(unsigned int array_size,
1611 struct gpio_desc **desc_array,
1612 int *value_array)
1613{
1614 might_sleep_if(extra_checks);
1615 if (!desc_array)
1616 return;
1617 gpiod_set_array_priv(true, true, array_size, desc_array, value_array);
1618}
1619EXPORT_SYMBOL_GPL(gpiod_set_raw_array_cansleep);
1620
1621/**
1622 * gpiod_set_array_cansleep() - assign values to an array of GPIOs
1623 * @array_size: number of elements in the descriptor / value arrays
1624 * @desc_array: array of GPIO descriptors whose values will be assigned
1625 * @value_array: array of values to assign
1626 *
1627 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1628 * into account.
1629 *
1630 * This function is to be called from contexts that can sleep.
1631 */
1632void gpiod_set_array_cansleep(unsigned int array_size,
1633 struct gpio_desc **desc_array,
1634 int *value_array)
1635{
1636 might_sleep_if(extra_checks);
1637 if (!desc_array)
1638 return;
1639 gpiod_set_array_priv(false, true, array_size, desc_array, value_array);
1640}
1641EXPORT_SYMBOL_GPL(gpiod_set_array_cansleep);
1642
1643/**
1461 * gpiod_add_lookup_table() - register GPIO device consumers 1644 * gpiod_add_lookup_table() - register GPIO device consumers
1462 * @table: table of consumers to register 1645 * @table: table of consumers to register
1463 */ 1646 */