2 * Copyright (c) 2017 Sebastian Reichel <sre@kernel.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 or
6 * later as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/leds.h>
15 #include <linux/mfd/motorola-cpcap.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
23 #define CPCAP_LED_NO_CURRENT 0x0001
25 struct cpcap_led_info
{
33 static const struct cpcap_led_info cpcap_led_red
= {
34 .reg
= CPCAP_REG_REDC
,
39 static const struct cpcap_led_info cpcap_led_green
= {
40 .reg
= CPCAP_REG_GREENC
,
45 static const struct cpcap_led_info cpcap_led_blue
= {
46 .reg
= CPCAP_REG_BLUEC
,
51 /* aux display light */
52 static const struct cpcap_led_info cpcap_led_adl
= {
53 .reg
= CPCAP_REG_ADLC
,
60 /* camera privacy led */
61 static const struct cpcap_led_info cpcap_led_cp
= {
62 .reg
= CPCAP_REG_CLEDC
,
70 struct led_classdev led
;
71 const struct cpcap_led_info
*info
;
73 struct regmap
*regmap
;
74 struct mutex update_lock
;
75 struct regulator
*vdd
;
81 static u16
cpcap_led_val(u8 current_limit
, u8 duty_cycle
)
83 current_limit
&= 0x1f; /* 5 bit */
84 duty_cycle
&= 0x0f; /* 4 bit */
86 return current_limit
<< 4 | duty_cycle
;
89 static int cpcap_led_set_power(struct cpcap_led
*led
, bool status
)
93 if (status
== led
->powered
)
97 err
= regulator_enable(led
->vdd
);
99 err
= regulator_disable(led
->vdd
);
102 dev_err(led
->dev
, "regulator failure: %d", err
);
106 led
->powered
= status
;
111 static int cpcap_led_set(struct led_classdev
*ledc
, enum led_brightness value
)
113 struct cpcap_led
*led
= container_of(ledc
, struct cpcap_led
, led
);
117 mutex_lock(&led
->update_lock
);
119 if (value
> LED_OFF
) {
120 err
= cpcap_led_set_power(led
, true);
125 if (value
== LED_OFF
) {
126 /* Avoid HW issue by turning off current before duty cycle */
127 err
= regmap_update_bits(led
->regmap
,
128 led
->info
->reg
, led
->info
->mask
, CPCAP_LED_NO_CURRENT
);
130 dev_err(led
->dev
, "regmap failed: %d", err
);
134 brightness
= cpcap_led_val(value
, LED_OFF
);
136 brightness
= cpcap_led_val(value
, LED_ON
);
139 err
= regmap_update_bits(led
->regmap
, led
->info
->reg
, led
->info
->mask
,
142 dev_err(led
->dev
, "regmap failed: %d", err
);
146 if (value
== LED_OFF
) {
147 err
= cpcap_led_set_power(led
, false);
153 mutex_unlock(&led
->update_lock
);
157 static const struct of_device_id cpcap_led_of_match
[] = {
158 { .compatible
= "motorola,cpcap-led-red", .data
= &cpcap_led_red
},
159 { .compatible
= "motorola,cpcap-led-green", .data
= &cpcap_led_green
},
160 { .compatible
= "motorola,cpcap-led-blue", .data
= &cpcap_led_blue
},
161 { .compatible
= "motorola,cpcap-led-adl", .data
= &cpcap_led_adl
},
162 { .compatible
= "motorola,cpcap-led-cp", .data
= &cpcap_led_cp
},
165 MODULE_DEVICE_TABLE(of
, cpcap_led_of_match
);
167 static int cpcap_led_probe(struct platform_device
*pdev
)
169 const struct of_device_id
*match
;
170 struct cpcap_led
*led
;
173 match
= of_match_device(of_match_ptr(cpcap_led_of_match
), &pdev
->dev
);
174 if (!match
|| !match
->data
)
177 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
180 platform_set_drvdata(pdev
, led
);
181 led
->info
= match
->data
;
182 led
->dev
= &pdev
->dev
;
184 if (led
->info
->reg
== 0x0000) {
185 dev_err(led
->dev
, "Unsupported LED");
189 led
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
193 led
->vdd
= devm_regulator_get(&pdev
->dev
, "vdd");
194 if (IS_ERR(led
->vdd
)) {
195 err
= PTR_ERR(led
->vdd
);
196 dev_err(led
->dev
, "Couldn't get regulator: %d", err
);
200 err
= device_property_read_string(&pdev
->dev
, "label", &led
->led
.name
);
202 dev_err(led
->dev
, "Couldn't read LED label: %d", err
);
206 if (led
->info
->init_mask
) {
207 err
= regmap_update_bits(led
->regmap
, led
->info
->reg
,
208 led
->info
->init_mask
, led
->info
->init_val
);
210 dev_err(led
->dev
, "regmap failed: %d", err
);
215 mutex_init(&led
->update_lock
);
217 led
->led
.max_brightness
= led
->info
->limit
;
218 led
->led
.brightness_set_blocking
= cpcap_led_set
;
219 err
= devm_led_classdev_register(&pdev
->dev
, &led
->led
);
221 dev_err(led
->dev
, "Couldn't register LED: %d", err
);
228 static struct platform_driver cpcap_led_driver
= {
229 .probe
= cpcap_led_probe
,
232 .of_match_table
= cpcap_led_of_match
,
235 module_platform_driver(cpcap_led_driver
);
237 MODULE_DESCRIPTION("CPCAP LED driver");
238 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
239 MODULE_LICENSE("GPL");