Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2011 Pengutronix
0004  * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
0005  */
0006 #include <linux/err.h>
0007 #include <linux/leds.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/slab.h>
0010 
0011 /**
0012  * gpio_led_register_device - register a gpio-led device
0013  * @pdata: the platform data used for the new device
0014  * @id: platform ID
0015  *
0016  * Makes a copy of pdata and pdata->leds and registers a new leds-gpio device
0017  * with the result. This allows to have pdata and pdata-leds in .init.rodata
0018  * and so saves some bytes compared to a static struct platform_device with
0019  * static platform data.
0020  *
0021  * Returns the registered device or an error pointer.
0022  */
0023 struct platform_device *__init gpio_led_register_device(
0024         int id, const struct gpio_led_platform_data *pdata)
0025 {
0026     struct platform_device *ret;
0027     struct gpio_led_platform_data _pdata = *pdata;
0028 
0029     if (!pdata->num_leds)
0030         return ERR_PTR(-EINVAL);
0031 
0032     _pdata.leds = kmemdup(pdata->leds,
0033             pdata->num_leds * sizeof(*pdata->leds), GFP_KERNEL);
0034     if (!_pdata.leds)
0035         return ERR_PTR(-ENOMEM);
0036 
0037     ret = platform_device_register_resndata(NULL, "leds-gpio", id,
0038             NULL, 0, &_pdata, sizeof(_pdata));
0039     if (IS_ERR(ret))
0040         kfree(_pdata.leds);
0041 
0042     return ret;
0043 }