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> |
| 13 | |
| 14 | #include "gpiolib.h" |
| 15 | |
| 16 | /** |
| 17 | * devprop_gpiochip_set_names - Set GPIO line names using device properties |
| 18 | * @chip: GPIO chip whose lines should be named, if possible |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 19 | * @fwnode: Property Node containing the gpio-line-names property |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 20 | * |
| 21 | * Looks for device property "gpio-line-names" and if it exists assigns |
| 22 | * GPIO line names for the chip. The memory allocated for the assigned |
| 23 | * names belong to the underlying firmware node and should not be released |
| 24 | * by the caller. |
| 25 | */ |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 26 | void devprop_gpiochip_set_names(struct gpio_chip *chip, |
| 27 | const struct fwnode_handle *fwnode) |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 28 | { |
| 29 | struct gpio_device *gdev = chip->gpiodev; |
| 30 | const char **names; |
| 31 | int ret, i; |
| 32 | |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 33 | ret = fwnode_property_read_string_array(fwnode, "gpio-line-names", |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 34 | NULL, 0); |
| 35 | if (ret < 0) |
| 36 | return; |
| 37 | |
| 38 | if (ret != gdev->ngpio) { |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 39 | dev_warn(&gdev->dev, |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 40 | "names %d do not match number of GPIOs %d\n", ret, |
| 41 | gdev->ngpio); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | names = kcalloc(gdev->ngpio, sizeof(*names), GFP_KERNEL); |
| 46 | if (!names) |
| 47 | return; |
| 48 | |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 49 | ret = fwnode_property_read_string_array(fwnode, "gpio-line-names", |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 50 | names, gdev->ngpio); |
| 51 | if (ret < 0) { |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 52 | dev_warn(&gdev->dev, "failed to read GPIO line names\n"); |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 53 | kfree(names); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | for (i = 0; i < gdev->ngpio; i++) |
| 58 | gdev->descs[i].name = names[i]; |
| 59 | |
| 60 | kfree(names); |
| 61 | } |