gpiolib-devprop.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include <linux/property.h>
  12. #include <linux/slab.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/gpio/driver.h>
  15. #include "gpiolib.h"
  16. /**
  17. * devprop_gpiochip_set_names - Set GPIO line names using device properties
  18. * @chip: GPIO chip whose lines should be named, if possible
  19. * @fwnode: Property Node containing the gpio-line-names property
  20. *
  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. */
  26. void devprop_gpiochip_set_names(struct gpio_chip *chip,
  27. const struct fwnode_handle *fwnode)
  28. {
  29. struct gpio_device *gdev = chip->gpiodev;
  30. const char **names;
  31. int ret, i;
  32. ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
  33. NULL, 0);
  34. if (ret < 0)
  35. return;
  36. if (ret != gdev->ngpio) {
  37. dev_warn(&gdev->dev,
  38. "names %d do not match number of GPIOs %d\n", ret,
  39. gdev->ngpio);
  40. return;
  41. }
  42. names = kcalloc(gdev->ngpio, sizeof(*names), GFP_KERNEL);
  43. if (!names)
  44. return;
  45. ret = fwnode_property_read_string_array(fwnode, "gpio-line-names",
  46. names, gdev->ngpio);
  47. if (ret < 0) {
  48. dev_warn(&gdev->dev, "failed to read GPIO line names\n");
  49. kfree(names);
  50. return;
  51. }
  52. for (i = 0; i < gdev->ngpio; i++)
  53. gdev->descs[i].name = names[i];
  54. kfree(names);
  55. }