Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Device property helpers for GPIO chips.
4 *
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
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#include <linux/export.h>
14
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;
33 int count;
34
35 count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
36 NULL, 0);
37 if (count < 0)
38 return;
39
40 if (count > gdev->ngpio) {
41 dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
42 count, gdev->ngpio);
43 count = gdev->ngpio;
44 }
45
46 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
47 if (!names)
48 return;
49
50 ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
51 names, count);
52 if (ret < 0) {
53 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
54 kfree(names);
55 return;
56 }
57
58 for (i = 0; i < count; i++)
59 gdev->descs[i].name = names[i];
60
61 kfree(names);
62}
63EXPORT_SYMBOL_GPL(devprop_gpiochip_set_names);
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
22 *
23 * Looks for device property "gpio-line-names" and if it exists assigns
24 * GPIO line names for the chip. The memory allocated for the assigned
25 * names belong to the underlying firmware node and should not be released
26 * by the caller.
27 */
28void devprop_gpiochip_set_names(struct gpio_chip *chip)
29{
30 struct gpio_device *gdev = chip->gpiodev;
31 const char **names;
32 int ret, i;
33
34 if (!chip->parent) {
35 dev_warn(&gdev->dev, "GPIO chip parent is NULL\n");
36 return;
37 }
38
39 ret = device_property_read_string_array(chip->parent, "gpio-line-names",
40 NULL, 0);
41 if (ret < 0)
42 return;
43
44 if (ret != gdev->ngpio) {
45 dev_warn(chip->parent,
46 "names %d do not match number of GPIOs %d\n", ret,
47 gdev->ngpio);
48 return;
49 }
50
51 names = kcalloc(gdev->ngpio, sizeof(*names), GFP_KERNEL);
52 if (!names)
53 return;
54
55 ret = device_property_read_string_array(chip->parent, "gpio-line-names",
56 names, gdev->ngpio);
57 if (ret < 0) {
58 dev_warn(chip->parent, "failed to read GPIO line names\n");
59 kfree(names);
60 return;
61 }
62
63 for (i = 0; i < gdev->ngpio; i++)
64 gdev->descs[i].name = names[i];
65
66 kfree(names);
67}