gpiolib-devprop.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. *
  20. * Looks for device property "gpio-line-names" and if it exists assigns
  21. * GPIO line names for the chip. The memory allocated for the assigned
  22. * names belong to the underlying firmware node and should not be released
  23. * by the caller.
  24. */
  25. void devprop_gpiochip_set_names(struct gpio_chip *chip)
  26. {
  27. struct gpio_device *gdev = chip->gpiodev;
  28. const char **names;
  29. int ret, i;
  30. if (!chip->parent) {
  31. dev_warn(&gdev->dev, "GPIO chip parent is NULL\n");
  32. return;
  33. }
  34. ret = device_property_read_string_array(chip->parent, "gpio-line-names",
  35. NULL, 0);
  36. if (ret < 0)
  37. return;
  38. if (ret != gdev->ngpio) {
  39. dev_warn(chip->parent,
  40. "names %d do not match number of GPIOs %d\n", ret,
  41. gdev->ngpio);
  42. return;
  43. }
  44. names = kcalloc(gdev->ngpio, sizeof(*names), GFP_KERNEL);
  45. if (!names)
  46. return;
  47. ret = device_property_read_string_array(chip->parent, "gpio-line-names",
  48. names, gdev->ngpio);
  49. if (ret < 0) {
  50. dev_warn(chip->parent, "failed to read GPIO line names\n");
  51. kfree(names);
  52. return;
  53. }
  54. for (i = 0; i < gdev->ngpio; i++)
  55. gdev->descs[i].name = names[i];
  56. kfree(names);
  57. }