blob: 53781b2539867b26fb589d527b9524ad0450e5b4 [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Mika Westerberg9427ecb2016-10-21 17:21:31 +03002/*
3 * Device property helpers for GPIO chips.
4 *
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
Mika Westerberg9427ecb2016-10-21 17:21:31 +03007 */
8
9#include <linux/property.h>
10#include <linux/slab.h>
11#include <linux/gpio/consumer.h>
12#include <linux/gpio/driver.h>
Linus Walleij182e80e2019-04-05 09:30:25 +070013#include <linux/export.h>
Mika Westerberg9427ecb2016-10-21 17:21:31 +030014
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 Leroy82270332017-12-15 15:02:33 +010020 * @fwnode: Property Node containing the gpio-line-names property
Mika Westerberg9427ecb2016-10-21 17:21:31 +030021 *
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 Leroy82270332017-12-15 15:02:33 +010027void devprop_gpiochip_set_names(struct gpio_chip *chip,
28 const struct fwnode_handle *fwnode)
Mika Westerberg9427ecb2016-10-21 17:21:31 +030029{
30 struct gpio_device *gdev = chip->gpiodev;
31 const char **names;
32 int ret, i;
Christophe Blaess8dc19692018-09-28 15:38:43 +020033 int count;
Mika Westerberg9427ecb2016-10-21 17:21:31 +030034
Christophe Blaess8dc19692018-09-28 15:38:43 +020035 count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
36 NULL, 0);
37 if (count < 0)
Mika Westerberg9427ecb2016-10-21 17:21:31 +030038 return;
39
Christophe Blaess8dc19692018-09-28 15:38:43 +020040 if (count > gdev->ngpio)
41 count = gdev->ngpio;
Mika Westerberg9427ecb2016-10-21 17:21:31 +030042
Christophe Blaess8dc19692018-09-28 15:38:43 +020043 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
Mika Westerberg9427ecb2016-10-21 17:21:31 +030044 if (!names)
45 return;
46
Christophe Leroy82270332017-12-15 15:02:33 +010047 ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
Christophe Blaess8dc19692018-09-28 15:38:43 +020048 names, count);
Mika Westerberg9427ecb2016-10-21 17:21:31 +030049 if (ret < 0) {
Christophe Leroy82270332017-12-15 15:02:33 +010050 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
Mika Westerberg9427ecb2016-10-21 17:21:31 +030051 kfree(names);
52 return;
53 }
54
Christophe Blaess8dc19692018-09-28 15:38:43 +020055 for (i = 0; i < count; i++)
Mika Westerberg9427ecb2016-10-21 17:21:31 +030056 gdev->descs[i].name = names[i];
57
58 kfree(names);
59}
Linus Walleij182e80e2019-04-05 09:30:25 +070060EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names);