leds-gpio-register.c 1.3 KB

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