Linus Walleij | dae5f0a | 2018-09-25 09:08:48 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Device property helpers for GPIO chips. |
| 4 | * |
| 5 | * Copyright (C) 2016, Intel Corporation |
| 6 | * Author: Mika Westerberg <mika.westerberg@linux.intel.com> |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/property.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/gpio/consumer.h> |
| 12 | #include <linux/gpio/driver.h> |
Linus Walleij | 182e80e | 2019-04-05 09:30:25 +0700 | [diff] [blame] | 13 | #include <linux/export.h> |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 14 | |
| 15 | #include "gpiolib.h" |
| 16 | |
| 17 | /** |
| 18 | * devprop_gpiochip_set_names - Set GPIO line names using device properties |
| 19 | * @chip: GPIO chip whose lines should be named, if possible |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 20 | * @fwnode: Property Node containing the gpio-line-names property |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 21 | * |
| 22 | * Looks for device property "gpio-line-names" and if it exists assigns |
| 23 | * GPIO line names for the chip. The memory allocated for the assigned |
| 24 | * names belong to the underlying firmware node and should not be released |
| 25 | * by the caller. |
| 26 | */ |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 27 | void devprop_gpiochip_set_names(struct gpio_chip *chip, |
| 28 | const struct fwnode_handle *fwnode) |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 29 | { |
| 30 | struct gpio_device *gdev = chip->gpiodev; |
| 31 | const char **names; |
| 32 | int ret, i; |
Christophe Blaess | 8dc1969 | 2018-09-28 15:38:43 +0200 | [diff] [blame] | 33 | int count; |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 34 | |
Christophe Blaess | 8dc1969 | 2018-09-28 15:38:43 +0200 | [diff] [blame] | 35 | count = fwnode_property_read_string_array(fwnode, "gpio-line-names", |
| 36 | NULL, 0); |
| 37 | if (count < 0) |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 38 | return; |
| 39 | |
Christophe Blaess | 8dc1969 | 2018-09-28 15:38:43 +0200 | [diff] [blame] | 40 | if (count > gdev->ngpio) |
| 41 | count = gdev->ngpio; |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 42 | |
Christophe Blaess | 8dc1969 | 2018-09-28 15:38:43 +0200 | [diff] [blame] | 43 | names = kcalloc(count, sizeof(*names), GFP_KERNEL); |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 44 | if (!names) |
| 45 | return; |
| 46 | |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 47 | ret = fwnode_property_read_string_array(fwnode, "gpio-line-names", |
Christophe Blaess | 8dc1969 | 2018-09-28 15:38:43 +0200 | [diff] [blame] | 48 | names, count); |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 49 | if (ret < 0) { |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 50 | dev_warn(&gdev->dev, "failed to read GPIO line names\n"); |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 51 | kfree(names); |
| 52 | return; |
| 53 | } |
| 54 | |
Christophe Blaess | 8dc1969 | 2018-09-28 15:38:43 +0200 | [diff] [blame] | 55 | for (i = 0; i < count; i++) |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 56 | gdev->descs[i].name = names[i]; |
| 57 | |
| 58 | kfree(names); |
| 59 | } |
Linus Walleij | 182e80e | 2019-04-05 09:30:25 +0700 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names); |