blob: 75717ba68ae0b7a491995aaacef8c7b778c71aea [file] [log] [blame]
Uwe Kleine-König44406732011-05-24 17:13:29 -07001/*
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>
Xiubo Li4cc72342014-09-28 01:57:14 -070010#include <linux/leds.h>
Uwe Kleine-König44406732011-05-24 17:13:29 -070011#include <linux/platform_device.h>
12#include <linux/slab.h>
Uwe Kleine-König44406732011-05-24 17:13:29 -070013
14/**
15 * gpio_led_register_device - register a gpio-led device
16 * @pdata: the platform data used for the new device
17 *
18 * Makes a copy of pdata and pdata->leds and registers a new leds-gpio device
19 * with the result. This allows to have pdata and pdata-leds in .init.rodata
20 * and so saves some bytes compared to a static struct platform_device with
21 * static platform data.
22 *
23 * Returns the registered device or an error pointer.
24 */
25struct platform_device *__init gpio_led_register_device(
26 int id, const struct gpio_led_platform_data *pdata)
27{
28 struct platform_device *ret;
29 struct gpio_led_platform_data _pdata = *pdata;
30
Xiubo Lia823e762014-09-28 01:57:15 -070031 if (!pdata->num_leds)
32 return ERR_PTR(-EINVAL);
33
Uwe Kleine-König44406732011-05-24 17:13:29 -070034 _pdata.leds = kmemdup(pdata->leds,
35 pdata->num_leds * sizeof(*pdata->leds), GFP_KERNEL);
36 if (!_pdata.leds)
37 return ERR_PTR(-ENOMEM);
38
39 ret = platform_device_register_resndata(NULL, "leds-gpio", id,
40 NULL, 0, &_pdata, sizeof(_pdata));
41 if (IS_ERR(ret))
42 kfree(_pdata.leds);
43
44 return ret;
45}