drivers/leds/rgb/leds-group-multicolor.c

Source file repositories/reference/linux-study-clean/drivers/leds/rgb/leds-group-multicolor.c

File Facts

System
Linux kernel
Corpus path
drivers/leds/rgb/leds-group-multicolor.c
Extension
.c
Size
5208 bytes
Lines
174
Domain
Driver Families
Bucket
drivers/leds
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct leds_multicolor {
	struct led_classdev_mc mc_cdev;
	struct led_classdev **monochromatics;
};

static int leds_gmc_set(struct led_classdev *cdev, enum led_brightness brightness)
{
	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
	struct leds_multicolor *priv = container_of(mc_cdev, struct leds_multicolor, mc_cdev);
	const unsigned int group_max_brightness = mc_cdev->led_cdev.max_brightness;
	int i;

	for (i = 0; i < mc_cdev->num_colors; i++) {
		struct led_classdev *mono = priv->monochromatics[i];
		const unsigned int mono_max_brightness = mono->max_brightness;
		unsigned int intensity = mc_cdev->subled_info[i].intensity;
		int mono_brightness;

		/*
		 * Scale the brightness according to relative intensity of the
		 * color AND the max brightness of the monochromatic LED.
		 */
		mono_brightness = DIV_ROUND_CLOSEST(brightness * intensity * mono_max_brightness,
						    group_max_brightness * group_max_brightness);

		led_set_brightness(mono, mono_brightness);
	}

	return 0;
}

static void restore_sysfs_write_access(void *data)
{
	struct led_classdev *led_cdev = data;

	/* Restore the write access to the LED */
	mutex_lock(&led_cdev->led_access);
	led_sysfs_enable(led_cdev);
	mutex_unlock(&led_cdev->led_access);
}

static int leds_gmc_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct led_init_data init_data = {};
	struct led_classdev *cdev;
	struct mc_subled *subled;
	struct leds_multicolor *priv;
	unsigned int max_brightness = 0;
	int i, ret, count = 0, common_flags = 0;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	for (;;) {
		struct led_classdev *led_cdev;

		led_cdev = devm_of_led_get_optional(dev, count);
		if (IS_ERR(led_cdev))
			return dev_err_probe(dev, PTR_ERR(led_cdev), "Unable to get LED #%d",
					     count);
		if (!led_cdev)
			break;

		priv->monochromatics = devm_krealloc_array(dev, priv->monochromatics,
					count + 1, sizeof(*priv->monochromatics),
					GFP_KERNEL);
		if (!priv->monochromatics)
			return -ENOMEM;

		common_flags |= led_cdev->flags;
		priv->monochromatics[count] = led_cdev;

		max_brightness = max(max_brightness, led_cdev->max_brightness);

		count++;
	}

	subled = devm_kcalloc(dev, count, sizeof(*subled), GFP_KERNEL);
	if (!subled)
		return -ENOMEM;
	priv->mc_cdev.subled_info = subled;

	for (i = 0; i < count; i++) {
		struct led_classdev *led_cdev = priv->monochromatics[i];

		subled[i].color_index = led_cdev->color;

		/* Configure the LED intensity to its maximum */

Annotation

Implementation Notes