Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Device property helpers for GPIO chips. |
| 3 | * |
| 4 | * Copyright (C) 2016, Intel Corporation |
| 5 | * Author: Mika Westerberg <mika.westerberg@linux.intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/property.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/gpio/consumer.h> |
| 15 | #include <linux/gpio/driver.h> |
| 16 | |
| 17 | #include "gpiolib.h" |
| 18 | |
| 19 | /** |
| 20 | * devprop_gpiochip_set_names - Set GPIO line names using device properties |
| 21 | * @chip: GPIO chip whose lines should be named, if possible |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 22 | * @fwnode: Property Node containing the gpio-line-names property |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 23 | * |
| 24 | * Looks for device property "gpio-line-names" and if it exists assigns |
| 25 | * GPIO line names for the chip. The memory allocated for the assigned |
| 26 | * names belong to the underlying firmware node and should not be released |
| 27 | * by the caller. |
| 28 | */ |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 29 | void devprop_gpiochip_set_names(struct gpio_chip *chip, |
| 30 | const struct fwnode_handle *fwnode) |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 31 | { |
| 32 | struct gpio_device *gdev = chip->gpiodev; |
| 33 | const char **names; |
| 34 | int ret, i; |
| 35 | |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 36 | ret = fwnode_property_read_string_array(fwnode, "gpio-line-names", |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 37 | NULL, 0); |
| 38 | if (ret < 0) |
| 39 | return; |
| 40 | |
| 41 | if (ret != gdev->ngpio) { |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 42 | dev_warn(&gdev->dev, |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 43 | "names %d do not match number of GPIOs %d\n", ret, |
| 44 | gdev->ngpio); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | names = kcalloc(gdev->ngpio, sizeof(*names), GFP_KERNEL); |
| 49 | if (!names) |
| 50 | return; |
| 51 | |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 52 | ret = fwnode_property_read_string_array(fwnode, "gpio-line-names", |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 53 | names, gdev->ngpio); |
| 54 | if (ret < 0) { |
Christophe Leroy | 8227033 | 2017-12-15 15:02:33 +0100 | [diff] [blame] | 55 | dev_warn(&gdev->dev, "failed to read GPIO line names\n"); |
Mika Westerberg | 9427ecb | 2016-10-21 17:21:31 +0300 | [diff] [blame] | 56 | kfree(names); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | for (i = 0; i < gdev->ngpio; i++) |
| 61 | gdev->descs[i].name = names[i]; |
| 62 | |
| 63 | kfree(names); |
| 64 | } |