blob: 53781b2539867b26fb589d527b9524ad0450e5b4 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Device property helpers for GPIO chips.
4 *
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#include <linux/property.h>
10#include <linux/slab.h>
11#include <linux/gpio/consumer.h>
12#include <linux/gpio/driver.h>
David Brazdil0f672f62019-12-10 10:32:29 +000013#include <linux/export.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014
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
20 * @fwnode: Property Node containing the gpio-line-names property
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 */
27void devprop_gpiochip_set_names(struct gpio_chip *chip,
28 const struct fwnode_handle *fwnode)
29{
30 struct gpio_device *gdev = chip->gpiodev;
31 const char **names;
32 int ret, i;
David Brazdil0f672f62019-12-10 10:32:29 +000033 int count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034
David Brazdil0f672f62019-12-10 10:32:29 +000035 count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
36 NULL, 0);
37 if (count < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 return;
39
David Brazdil0f672f62019-12-10 10:32:29 +000040 if (count > gdev->ngpio)
41 count = gdev->ngpio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042
David Brazdil0f672f62019-12-10 10:32:29 +000043 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 if (!names)
45 return;
46
47 ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
David Brazdil0f672f62019-12-10 10:32:29 +000048 names, count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 if (ret < 0) {
50 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
51 kfree(names);
52 return;
53 }
54
David Brazdil0f672f62019-12-10 10:32:29 +000055 for (i = 0; i < count; i++)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056 gdev->descs[i].name = names[i];
57
58 kfree(names);
59}
David Brazdil0f672f62019-12-10 10:32:29 +000060EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names);