1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2017 Sebastian Reichel <sre@kernel.org>
6 #include <linux/leds.h>
7 #include <linux/mfd/motorola-cpcap.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/consumer.h>
15 #define CPCAP_LED_NO_CURRENT 0x0001
17 struct cpcap_led_info {
25 static const struct cpcap_led_info cpcap_led_red = {
26 .reg = CPCAP_REG_REDC,
31 static const struct cpcap_led_info cpcap_led_green = {
32 .reg = CPCAP_REG_GREENC,
37 static const struct cpcap_led_info cpcap_led_blue = {
38 .reg = CPCAP_REG_BLUEC,
43 /* aux display light */
44 static const struct cpcap_led_info cpcap_led_adl = {
45 .reg = CPCAP_REG_ADLC,
52 /* camera privacy led */
53 static const struct cpcap_led_info cpcap_led_cp = {
54 .reg = CPCAP_REG_CLEDC,
62 struct led_classdev led;
63 const struct cpcap_led_info *info;
65 struct regmap *regmap;
66 struct mutex update_lock;
67 struct regulator *vdd;
73 static u16 cpcap_led_val(u8 current_limit, u8 duty_cycle)
75 current_limit &= 0x1f; /* 5 bit */
76 duty_cycle &= 0x0f; /* 4 bit */
78 return current_limit << 4 | duty_cycle;
81 static int cpcap_led_set_power(struct cpcap_led *led, bool status)
85 if (status == led->powered)
89 err = regulator_enable(led->vdd);
91 err = regulator_disable(led->vdd);
94 dev_err(led->dev, "regulator failure: %d", err);
98 led->powered = status;
103 static int cpcap_led_set(struct led_classdev *ledc, enum led_brightness value)
105 struct cpcap_led *led = container_of(ledc, struct cpcap_led, led);
109 mutex_lock(&led->update_lock);
111 if (value > LED_OFF) {
112 err = cpcap_led_set_power(led, true);
117 if (value == LED_OFF) {
118 /* Avoid HW issue by turning off current before duty cycle */
119 err = regmap_update_bits(led->regmap,
120 led->info->reg, led->info->mask, CPCAP_LED_NO_CURRENT);
122 dev_err(led->dev, "regmap failed: %d", err);
126 brightness = cpcap_led_val(value, LED_OFF);
128 brightness = cpcap_led_val(value, LED_ON);
131 err = regmap_update_bits(led->regmap, led->info->reg, led->info->mask,
134 dev_err(led->dev, "regmap failed: %d", err);
138 if (value == LED_OFF) {
139 err = cpcap_led_set_power(led, false);
145 mutex_unlock(&led->update_lock);
149 static const struct of_device_id cpcap_led_of_match[] = {
150 { .compatible = "motorola,cpcap-led-red", .data = &cpcap_led_red },
151 { .compatible = "motorola,cpcap-led-green", .data = &cpcap_led_green },
152 { .compatible = "motorola,cpcap-led-blue", .data = &cpcap_led_blue },
153 { .compatible = "motorola,cpcap-led-adl", .data = &cpcap_led_adl },
154 { .compatible = "motorola,cpcap-led-cp", .data = &cpcap_led_cp },
157 MODULE_DEVICE_TABLE(of, cpcap_led_of_match);
159 static int cpcap_led_probe(struct platform_device *pdev)
161 struct cpcap_led *led;
164 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
167 platform_set_drvdata(pdev, led);
168 led->info = device_get_match_data(&pdev->dev);
169 led->dev = &pdev->dev;
171 if (led->info->reg == 0x0000) {
172 dev_err(led->dev, "Unsupported LED");
176 led->regmap = dev_get_regmap(pdev->dev.parent, NULL);
180 led->vdd = devm_regulator_get(&pdev->dev, "vdd");
181 if (IS_ERR(led->vdd)) {
182 err = PTR_ERR(led->vdd);
183 dev_err(led->dev, "Couldn't get regulator: %d", err);
187 err = device_property_read_string(&pdev->dev, "label", &led->led.name);
189 dev_err(led->dev, "Couldn't read LED label: %d", err);
193 if (led->info->init_mask) {
194 err = regmap_update_bits(led->regmap, led->info->reg,
195 led->info->init_mask, led->info->init_val);
197 dev_err(led->dev, "regmap failed: %d", err);
202 mutex_init(&led->update_lock);
204 led->led.max_brightness = led->info->limit;
205 led->led.brightness_set_blocking = cpcap_led_set;
206 err = devm_led_classdev_register(&pdev->dev, &led->led);
208 dev_err(led->dev, "Couldn't register LED: %d", err);
215 static struct platform_driver cpcap_led_driver = {
216 .probe = cpcap_led_probe,
219 .of_match_table = cpcap_led_of_match,
222 module_platform_driver(cpcap_led_driver);
224 MODULE_DESCRIPTION("CPCAP LED driver");
225 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
226 MODULE_LICENSE("GPL");