leds-gpio-register.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Pengutronix
  4. * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
  5. */
  6. #include <linux/err.h>
  7. #include <linux/leds.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/slab.h>
  10. /**
  11. * gpio_led_register_device - register a gpio-led device
  12. * @pdata: the platform data used for the new device
  13. *
  14. * Makes a copy of pdata and pdata->leds and registers a new leds-gpio device
  15. * with the result. This allows to have pdata and pdata-leds in .init.rodata
  16. * and so saves some bytes compared to a static struct platform_device with
  17. * static platform data.
  18. *
  19. * Returns the registered device or an error pointer.
  20. */
  21. struct platform_device *__init gpio_led_register_device(
  22. int id, const struct gpio_led_platform_data *pdata)
  23. {
  24. struct platform_device *ret;
  25. struct gpio_led_platform_data _pdata = *pdata;
  26. if (!pdata->num_leds)
  27. return ERR_PTR(-EINVAL);
  28. _pdata.leds = kmemdup(pdata->leds,
  29. pdata->num_leds * sizeof(*pdata->leds), GFP_KERNEL);
  30. if (!_pdata.leds)
  31. return ERR_PTR(-ENOMEM);
  32. ret = platform_device_register_resndata(NULL, "leds-gpio", id,
  33. NULL, 0, &_pdata, sizeof(_pdata));
  34. if (IS_ERR(ret))
  35. kfree(_pdata.leds);
  36. return ret;
  37. }