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 const struct of_device_id
*match
;
162 struct cpcap_led
*led
;
165 match
= of_match_device(of_match_ptr(cpcap_led_of_match
), &pdev
->dev
);
166 if (!match
|| !match
->data
)
169 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
172 platform_set_drvdata(pdev
, led
);
173 led
->info
= match
->data
;
174 led
->dev
= &pdev
->dev
;
176 if (led
->info
->reg
== 0x0000) {
177 dev_err(led
->dev
, "Unsupported LED");
181 led
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
185 led
->vdd
= devm_regulator_get(&pdev
->dev
, "vdd");
186 if (IS_ERR(led
->vdd
)) {
187 err
= PTR_ERR(led
->vdd
);
188 dev_err(led
->dev
, "Couldn't get regulator: %d", err
);
192 err
= device_property_read_string(&pdev
->dev
, "label", &led
->led
.name
);
194 dev_err(led
->dev
, "Couldn't read LED label: %d", err
);
198 if (led
->info
->init_mask
) {
199 err
= regmap_update_bits(led
->regmap
, led
->info
->reg
,
200 led
->info
->init_mask
, led
->info
->init_val
);
202 dev_err(led
->dev
, "regmap failed: %d", err
);
207 mutex_init(&led
->update_lock
);
209 led
->led
.max_brightness
= led
->info
->limit
;
210 led
->led
.brightness_set_blocking
= cpcap_led_set
;
211 err
= devm_led_classdev_register(&pdev
->dev
, &led
->led
);
213 dev_err(led
->dev
, "Couldn't register LED: %d", err
);
220 static struct platform_driver cpcap_led_driver
= {
221 .probe
= cpcap_led_probe
,
224 .of_match_table
= cpcap_led_of_match
,
227 module_platform_driver(cpcap_led_driver
);
229 MODULE_DESCRIPTION("CPCAP LED driver");
230 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
231 MODULE_LICENSE("GPL");