gpiolib-devprop.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <linux/property.h>
  9. #include <linux/slab.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/gpio/driver.h>
  12. #include "gpiolib.h"
  13. /**
  14. * devprop_gpiochip_set_names - Set GPIO line names using device properties
  15. * @chip: GPIO chip whose lines should be named, if possible
  16. * @fwnode: Property Node containing the gpio-line-names property
  17. *
  18. * Looks for device property "gpio-line-names" and if it exists assigns
  19. * GPIO line names for the chip. The memory allocated for the assigned
  20. * names belong to the underlying firmware node and should not be released
  21. * by the caller.
  22. */
  23. void devprop_gpiochip_set_names(struct gpio_chip *chip,
  24. const struct fwnode_handle *fwnode)
  25. {
  26. struct gpio_device *gdev = chip->gpiodev;
  27. const char **names;
  28. int ret, i;
  29. int count;
  30. count = fwnode_property_read_string_array(fwnode, "gpio-line-names",
  31. NULL, 0);
  32. if (count < 0)
  33. return;
  34. if (count > gdev->ngpio)
  35. count = gdev->ngpio;
  36. names = kcalloc(count, sizeof(*names), GFP_KERNEL);
  37. if (!names)
  38. return;
  39. ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
  40. names, count);
  41. if (ret < 0) {
  42. dev_warn(&gdev->dev, "failed to read GPIO line names\n");
  43. kfree(names);
  44. return;
  45. }
  46. for (i = 0; i < count; i++)
  47. gdev->descs[i].name = names[i];
  48. kfree(names);
  49. }