drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / leds / leds-ariel.c
blobdd319c7e385fc1ad3e78cc2069a6b82caf43723c
1 // SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later
2 /*
3 * Dell Wyse 3020 a.k.a. "Ariel" Embedded Controller LED Driver
5 * Copyright (C) 2020 Lubomir Rintel
6 */
8 #include <linux/module.h>
9 #include <linux/leds.h>
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
13 enum ec_index {
14 EC_BLUE_LED = 0x01,
15 EC_AMBER_LED = 0x02,
16 EC_GREEN_LED = 0x03,
19 enum {
20 EC_LED_OFF = 0x00,
21 EC_LED_STILL = 0x01,
22 EC_LED_FADE = 0x02,
23 EC_LED_BLINK = 0x03,
26 struct ariel_led {
27 struct regmap *ec_ram;
28 enum ec_index ec_index;
29 struct led_classdev led_cdev;
32 #define led_cdev_to_ariel_led(c) container_of(c, struct ariel_led, led_cdev)
34 static enum led_brightness ariel_led_get(struct led_classdev *led_cdev)
36 struct ariel_led *led = led_cdev_to_ariel_led(led_cdev);
37 unsigned int led_status = 0;
39 if (regmap_read(led->ec_ram, led->ec_index, &led_status))
40 return LED_OFF;
42 if (led_status == EC_LED_STILL)
43 return LED_FULL;
44 else
45 return LED_OFF;
48 static void ariel_led_set(struct led_classdev *led_cdev,
49 enum led_brightness brightness)
51 struct ariel_led *led = led_cdev_to_ariel_led(led_cdev);
53 if (brightness == LED_OFF)
54 regmap_write(led->ec_ram, led->ec_index, EC_LED_OFF);
55 else
56 regmap_write(led->ec_ram, led->ec_index, EC_LED_STILL);
59 static int ariel_blink_set(struct led_classdev *led_cdev,
60 unsigned long *delay_on, unsigned long *delay_off)
62 struct ariel_led *led = led_cdev_to_ariel_led(led_cdev);
64 if (*delay_on == 0 && *delay_off == 0)
65 return -EINVAL;
67 if (*delay_on == 0) {
68 regmap_write(led->ec_ram, led->ec_index, EC_LED_OFF);
69 } else if (*delay_off == 0) {
70 regmap_write(led->ec_ram, led->ec_index, EC_LED_STILL);
71 } else {
72 *delay_on = 500;
73 *delay_off = 500;
74 regmap_write(led->ec_ram, led->ec_index, EC_LED_BLINK);
77 return 0;
80 #define NLEDS 3
82 static int ariel_led_probe(struct platform_device *pdev)
84 struct device *dev = &pdev->dev;
85 struct ariel_led *leds;
86 struct regmap *ec_ram;
87 int ret;
88 int i;
90 ec_ram = dev_get_regmap(dev->parent, "ec_ram");
91 if (!ec_ram)
92 return -ENODEV;
94 leds = devm_kcalloc(dev, NLEDS, sizeof(*leds), GFP_KERNEL);
95 if (!leds)
96 return -ENOMEM;
98 leds[0].ec_index = EC_BLUE_LED;
99 leds[0].led_cdev.name = "blue:power";
100 leds[0].led_cdev.default_trigger = "default-on";
102 leds[1].ec_index = EC_AMBER_LED;
103 leds[1].led_cdev.name = "amber:status";
105 leds[2].ec_index = EC_GREEN_LED;
106 leds[2].led_cdev.name = "green:status";
107 leds[2].led_cdev.default_trigger = "default-on";
109 for (i = 0; i < NLEDS; i++) {
110 leds[i].ec_ram = ec_ram;
111 leds[i].led_cdev.brightness_get = ariel_led_get;
112 leds[i].led_cdev.brightness_set = ariel_led_set;
113 leds[i].led_cdev.blink_set = ariel_blink_set;
115 ret = devm_led_classdev_register(dev, &leds[i].led_cdev);
116 if (ret)
117 return ret;
120 return 0;
123 static struct platform_driver ariel_led_driver = {
124 .probe = ariel_led_probe,
125 .driver = {
126 .name = "dell-wyse-ariel-led",
129 module_platform_driver(ariel_led_driver);
131 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
132 MODULE_DESCRIPTION("Dell Wyse 3020 Status LEDs Driver");
133 MODULE_LICENSE("Dual BSD/GPL");