Skip to content

Commit feeab87

Browse files
I-n-o-kLee Jones
authored andcommitted
backlight: qcom-wled: Add support for short circuit handling
Handle the short circuit interrupt and check if the short circuit interrupt is valid. Re-enable the module to check if it goes away. Disable the module altogether if the short circuit event persists. Signed-off-by: Kiran Gunda <[email protected]> Reviewed-by: Bjorn Andersson <[email protected]> Reviewed-by: Daniel Thompson <[email protected]> Signed-off-by: Lee Jones <[email protected]>
1 parent 03b2b5e commit feeab87

File tree

1 file changed

+140
-4
lines changed

1 file changed

+140
-4
lines changed

drivers/video/backlight/qcom-wled.c

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
/* Copyright (c) 2015, Sony Mobile Communications, AB.
33
*/
44

5+
#include <linux/delay.h>
6+
#include <linux/interrupt.h>
7+
#include <linux/ktime.h>
58
#include <linux/kernel.h>
69
#include <linux/backlight.h>
710
#include <linux/module.h>
@@ -56,6 +59,16 @@
5659
#define WLED3_SINK_REG_STR_CABC(n) (0x66 + (n * 0x10))
5760
#define WLED3_SINK_REG_STR_CABC_MASK BIT(7)
5861

62+
/* WLED4 specific control registers */
63+
#define WLED4_CTRL_REG_SHORT_PROTECT 0x5e
64+
#define WLED4_CTRL_REG_SHORT_EN_MASK BIT(7)
65+
66+
#define WLED4_CTRL_REG_SEC_ACCESS 0xd0
67+
#define WLED4_CTRL_REG_SEC_UNLOCK 0xa5
68+
69+
#define WLED4_CTRL_REG_TEST1 0xe2
70+
#define WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2 0x09
71+
5972
/* WLED4 specific sink registers */
6073
#define WLED4_SINK_REG_CURR_SINK 0x46
6174
#define WLED4_SINK_REG_CURR_SINK_MASK GENMASK(7, 4)
@@ -105,17 +118,24 @@ struct wled_config {
105118
bool cs_out_en;
106119
bool ext_gen;
107120
bool cabc;
121+
bool external_pfet;
108122
};
109123

110124
struct wled {
111125
const char *name;
112126
struct device *dev;
113127
struct regmap *regmap;
128+
struct mutex lock; /* Lock to avoid race from thread irq handler */
129+
ktime_t last_short_event;
114130
u16 ctrl_addr;
115131
u16 sink_addr;
116132
u16 max_string_count;
117133
u32 brightness;
118134
u32 max_brightness;
135+
u32 short_count;
136+
bool disabled_by_short;
137+
bool has_short_detect;
138+
int short_irq;
119139

120140
struct wled_config cfg;
121141
int (*wled_set_brightness)(struct wled *wled, u16 brightness);
@@ -166,6 +186,9 @@ static int wled_module_enable(struct wled *wled, int val)
166186
{
167187
int rc;
168188

189+
if (wled->disabled_by_short)
190+
return -ENXIO;
191+
169192
rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
170193
WLED3_CTRL_REG_MOD_EN,
171194
WLED3_CTRL_REG_MOD_EN_MASK,
@@ -202,34 +225,81 @@ static int wled_update_status(struct backlight_device *bl)
202225
bl->props.state & BL_CORE_FBBLANK)
203226
brightness = 0;
204227

228+
mutex_lock(&wled->lock);
205229
if (brightness) {
206230
rc = wled->wled_set_brightness(wled, brightness);
207231
if (rc < 0) {
208232
dev_err(wled->dev, "wled failed to set brightness rc:%d\n",
209233
rc);
210-
return rc;
234+
goto unlock_mutex;
211235
}
212236

213237
rc = wled_sync_toggle(wled);
214238
if (rc < 0) {
215239
dev_err(wled->dev, "wled sync failed rc:%d\n", rc);
216-
return rc;
240+
goto unlock_mutex;
217241
}
218242
}
219243

220244
if (!!brightness != !!wled->brightness) {
221245
rc = wled_module_enable(wled, !!brightness);
222246
if (rc < 0) {
223247
dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
224-
return rc;
248+
goto unlock_mutex;
225249
}
226250
}
227251

228252
wled->brightness = brightness;
229253

254+
unlock_mutex:
255+
mutex_unlock(&wled->lock);
256+
230257
return rc;
231258
}
232259

260+
#define WLED_SHORT_DLY_MS 20
261+
#define WLED_SHORT_CNT_MAX 5
262+
#define WLED_SHORT_RESET_CNT_DLY_US USEC_PER_SEC
263+
264+
static irqreturn_t wled_short_irq_handler(int irq, void *_wled)
265+
{
266+
struct wled *wled = _wled;
267+
int rc;
268+
s64 elapsed_time;
269+
270+
wled->short_count++;
271+
mutex_lock(&wled->lock);
272+
rc = wled_module_enable(wled, false);
273+
if (rc < 0) {
274+
dev_err(wled->dev, "wled disable failed rc:%d\n", rc);
275+
goto unlock_mutex;
276+
}
277+
278+
elapsed_time = ktime_us_delta(ktime_get(),
279+
wled->last_short_event);
280+
if (elapsed_time > WLED_SHORT_RESET_CNT_DLY_US)
281+
wled->short_count = 1;
282+
283+
if (wled->short_count > WLED_SHORT_CNT_MAX) {
284+
dev_err(wled->dev, "Short trigged %d times, disabling WLED forever!\n",
285+
wled->short_count);
286+
wled->disabled_by_short = true;
287+
goto unlock_mutex;
288+
}
289+
290+
wled->last_short_event = ktime_get();
291+
292+
msleep(WLED_SHORT_DLY_MS);
293+
rc = wled_module_enable(wled, true);
294+
if (rc < 0)
295+
dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
296+
297+
unlock_mutex:
298+
mutex_unlock(&wled->lock);
299+
300+
return IRQ_HANDLED;
301+
}
302+
233303
static int wled3_setup(struct wled *wled)
234304
{
235305
u16 addr;
@@ -318,7 +388,7 @@ static int wled4_setup(struct wled *wled)
318388
int rc, temp, i, j;
319389
u16 addr;
320390
u8 sink_en = 0;
321-
u32 sink_cfg = 0;
391+
u32 sink_cfg;
322392

323393
rc = regmap_update_bits(wled->regmap,
324394
wled->ctrl_addr + WLED3_CTRL_REG_OVP,
@@ -340,6 +410,21 @@ static int wled4_setup(struct wled *wled)
340410
if (rc < 0)
341411
return rc;
342412

413+
if (wled->cfg.external_pfet) {
414+
/* Unlock the secure register access */
415+
rc = regmap_write(wled->regmap, wled->ctrl_addr +
416+
WLED4_CTRL_REG_SEC_ACCESS,
417+
WLED4_CTRL_REG_SEC_UNLOCK);
418+
if (rc < 0)
419+
return rc;
420+
421+
rc = regmap_write(wled->regmap,
422+
wled->ctrl_addr + WLED4_CTRL_REG_TEST1,
423+
WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2);
424+
if (rc < 0)
425+
return rc;
426+
}
427+
343428
rc = regmap_read(wled->regmap, wled->sink_addr +
344429
WLED4_SINK_REG_CURR_SINK, &sink_cfg);
345430
if (rc < 0)
@@ -425,6 +510,7 @@ static const struct wled_config wled4_config_defaults = {
425510
.num_strings = 4,
426511
.switch_freq = 11,
427512
.cabc = false,
513+
.external_pfet = false,
428514
};
429515

430516
static const u32 wled3_boost_i_limit_values[] = {
@@ -590,6 +676,7 @@ static int wled_configure(struct wled *wled, int version)
590676
{ "qcom,cs-out", &cfg->cs_out_en, },
591677
{ "qcom,ext-gen", &cfg->ext_gen, },
592678
{ "qcom,cabc", &cfg->cabc, },
679+
{ "qcom,external-pfet", &cfg->external_pfet, },
593680
};
594681

595682
prop_addr = of_get_address(dev->of_node, 0, NULL, NULL);
@@ -678,6 +765,38 @@ static int wled_configure(struct wled *wled, int version)
678765
return 0;
679766
}
680767

768+
static int wled_configure_short_irq(struct wled *wled,
769+
struct platform_device *pdev)
770+
{
771+
int rc;
772+
773+
if (!wled->has_short_detect)
774+
return 0;
775+
776+
rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
777+
WLED4_CTRL_REG_SHORT_PROTECT,
778+
WLED4_CTRL_REG_SHORT_EN_MASK,
779+
WLED4_CTRL_REG_SHORT_EN_MASK);
780+
if (rc < 0)
781+
return rc;
782+
783+
wled->short_irq = platform_get_irq_byname(pdev, "short");
784+
if (wled->short_irq < 0) {
785+
dev_dbg(&pdev->dev, "short irq is not used\n");
786+
return 0;
787+
}
788+
789+
rc = devm_request_threaded_irq(wled->dev, wled->short_irq,
790+
NULL, wled_short_irq_handler,
791+
IRQF_ONESHOT,
792+
"wled_short_irq", wled);
793+
if (rc < 0)
794+
dev_err(wled->dev, "Unable to request short_irq (err:%d)\n",
795+
rc);
796+
797+
return rc;
798+
}
799+
681800
static const struct backlight_ops wled_ops = {
682801
.update_status = wled_update_status,
683802
};
@@ -711,6 +830,7 @@ static int wled_probe(struct platform_device *pdev)
711830
return -ENODEV;
712831
}
713832

833+
mutex_init(&wled->lock);
714834
rc = wled_configure(wled, version);
715835
if (rc)
716836
return rc;
@@ -725,6 +845,7 @@ static int wled_probe(struct platform_device *pdev)
725845
break;
726846

727847
case 4:
848+
wled->has_short_detect = true;
728849
rc = wled4_setup(wled);
729850
if (rc) {
730851
dev_err(&pdev->dev, "wled4_setup failed\n");
@@ -737,6 +858,10 @@ static int wled_probe(struct platform_device *pdev)
737858
break;
738859
}
739860

861+
rc = wled_configure_short_irq(wled, pdev);
862+
if (rc < 0)
863+
return rc;
864+
740865
val = WLED_DEFAULT_BRIGHTNESS;
741866
of_property_read_u32(pdev->dev.of_node, "default-brightness", &val);
742867

@@ -750,6 +875,16 @@ static int wled_probe(struct platform_device *pdev)
750875
return PTR_ERR_OR_ZERO(bl);
751876
};
752877

878+
static int wled_remove(struct platform_device *pdev)
879+
{
880+
struct wled *wled = dev_get_drvdata(&pdev->dev);
881+
882+
mutex_destroy(&wled->lock);
883+
disable_irq(wled->short_irq);
884+
885+
return 0;
886+
}
887+
753888
static const struct of_device_id wled_match_table[] = {
754889
{ .compatible = "qcom,pm8941-wled", .data = (void *)3 },
755890
{ .compatible = "qcom,pmi8998-wled", .data = (void *)4 },
@@ -760,6 +895,7 @@ MODULE_DEVICE_TABLE(of, wled_match_table);
760895

761896
static struct platform_driver wled_driver = {
762897
.probe = wled_probe,
898+
.remove = wled_remove,
763899
.driver = {
764900
.name = "qcom,wled",
765901
.of_match_table = wled_match_table,

0 commit comments

Comments
 (0)