gpiolib-devprop.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
  30. NULL, 0);
  31. if (ret < 0)
  32. return;
  33. if (ret != gdev->ngpio) {
  34. dev_warn(&gdev->dev,
  35. "names %d do not match number of GPIOs %d\n", ret,
  36. gdev->ngpio);
  37. return;
  38. }
  39. names = kcalloc(gdev->ngpio, sizeof(*names), GFP_KERNEL);
  40. if (!names)
  41. return;
  42. ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
  43. names, gdev->ngpio);
  44. if (ret < 0) {
  45. dev_warn(&gdev->dev, "failed to read GPIO line names\n");
  46. kfree(names);
  47. return;
  48. }
  49. for (i = 0; i < gdev->ngpio; i++)
  50. gdev->descs[i].name = names[i];
  51. kfree(names);
  52. }