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