blob: dd517098ab9529df69ed4d5c7ca6af5b299ee813 [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>
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 Leroy82270332017-12-15 15:02:33 +010019 * @fwnode: Property Node containing the gpio-line-names property
Mika Westerberg9427ecb2016-10-21 17:21:31 +030020 *
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 Leroy82270332017-12-15 15:02:33 +010026void devprop_gpiochip_set_names(struct gpio_chip *chip,
27 const struct fwnode_handle *fwnode)
Mika Westerberg9427ecb2016-10-21 17:21:31 +030028{
29 struct gpio_device *gdev = chip->gpiodev;
30 const char **names;
31 int ret, i;
Christophe Blaess8dc19692018-09-28 15:38:43 +020032 int count;
Mika Westerberg9427ecb2016-10-21 17:21:31 +030033
Christophe Blaess8dc19692018-09-28 15:38:43 +020034 count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
35 NULL, 0);
36 if (count < 0)
Mika Westerberg9427ecb2016-10-21 17:21:31 +030037 return;
38
Christophe Blaess8dc19692018-09-28 15:38:43 +020039 if (count > gdev->ngpio)
40 count = gdev->ngpio;
Mika Westerberg9427ecb2016-10-21 17:21:31 +030041
Christophe Blaess8dc19692018-09-28 15:38:43 +020042 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
Mika Westerberg9427ecb2016-10-21 17:21:31 +030043 if (!names)
44 return;
45
Christophe Leroy82270332017-12-15 15:02:33 +010046 ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
Christophe Blaess8dc19692018-09-28 15:38:43 +020047 names, count);
Mika Westerberg9427ecb2016-10-21 17:21:31 +030048 if (ret < 0) {
Christophe Leroy82270332017-12-15 15:02:33 +010049 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
Mika Westerberg9427ecb2016-10-21 17:21:31 +030050 kfree(names);
51 return;
52 }
53
Christophe Blaess8dc19692018-09-28 15:38:43 +020054 for (i = 0; i < count; i++)
Mika Westerberg9427ecb2016-10-21 17:21:31 +030055 gdev->descs[i].name = names[i];
56
57 kfree(names);
58}