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