1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010, 2011, 2016 The Linux Foundation. All rights reserved.
4 #include <linux/leds.h>
5 #include <linux/module.h>
7 #include <linux/of_device.h>
8 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
12 #define PM8058_LED_TYPE_COMMON 0x00
13 #define PM8058_LED_TYPE_KEYPAD 0x01
14 #define PM8058_LED_TYPE_FLASH 0x02
16 #define PM8058_LED_TYPE_COMMON_MASK 0xf8
17 #define PM8058_LED_TYPE_KEYPAD_MASK 0xf0
18 #define PM8058_LED_TYPE_COMMON_SHIFT 3
19 #define PM8058_LED_TYPE_KEYPAD_SHIFT 4
25 struct led_classdev cdev
;
28 static void pm8058_led_set(struct led_classdev
*cled
,
29 enum led_brightness value
)
31 struct pm8058_led
*led
;
33 unsigned int mask
= 0;
36 led
= container_of(cled
, struct pm8058_led
, cdev
);
37 switch (led
->ledtype
) {
38 case PM8058_LED_TYPE_COMMON
:
39 mask
= PM8058_LED_TYPE_COMMON_MASK
;
40 val
= value
<< PM8058_LED_TYPE_COMMON_SHIFT
;
42 case PM8058_LED_TYPE_KEYPAD
:
43 case PM8058_LED_TYPE_FLASH
:
44 mask
= PM8058_LED_TYPE_KEYPAD_MASK
;
45 val
= value
<< PM8058_LED_TYPE_KEYPAD_SHIFT
;
51 ret
= regmap_update_bits(led
->map
, led
->reg
, mask
, val
);
53 pr_err("Failed to set LED brightness\n");
56 static enum led_brightness
pm8058_led_get(struct led_classdev
*cled
)
58 struct pm8058_led
*led
;
62 led
= container_of(cled
, struct pm8058_led
, cdev
);
64 ret
= regmap_read(led
->map
, led
->reg
, &val
);
66 pr_err("Failed to get LED brightness\n");
70 switch (led
->ledtype
) {
71 case PM8058_LED_TYPE_COMMON
:
72 val
&= PM8058_LED_TYPE_COMMON_MASK
;
73 val
>>= PM8058_LED_TYPE_COMMON_SHIFT
;
75 case PM8058_LED_TYPE_KEYPAD
:
76 case PM8058_LED_TYPE_FLASH
:
77 val
&= PM8058_LED_TYPE_KEYPAD_MASK
;
78 val
>>= PM8058_LED_TYPE_KEYPAD_SHIFT
;
88 static int pm8058_led_probe(struct platform_device
*pdev
)
90 struct pm8058_led
*led
;
91 struct device_node
*np
= pdev
->dev
.of_node
;
95 enum led_brightness maxbright
;
97 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
101 led
->ledtype
= (u32
)(unsigned long)of_device_get_match_data(&pdev
->dev
);
103 map
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
105 dev_err(&pdev
->dev
, "Parent regmap unavailable.\n");
110 ret
= of_property_read_u32(np
, "reg", &led
->reg
);
112 dev_err(&pdev
->dev
, "no register offset specified\n");
116 /* Use label else node name */
117 led
->cdev
.name
= of_get_property(np
, "label", NULL
) ? : np
->name
;
118 led
->cdev
.default_trigger
=
119 of_get_property(np
, "linux,default-trigger", NULL
);
120 led
->cdev
.brightness_set
= pm8058_led_set
;
121 led
->cdev
.brightness_get
= pm8058_led_get
;
122 if (led
->ledtype
== PM8058_LED_TYPE_COMMON
)
123 maxbright
= 31; /* 5 bits */
125 maxbright
= 15; /* 4 bits */
126 led
->cdev
.max_brightness
= maxbright
;
128 state
= of_get_property(np
, "default-state", NULL
);
130 if (!strcmp(state
, "keep")) {
131 led
->cdev
.brightness
= pm8058_led_get(&led
->cdev
);
132 } else if (!strcmp(state
, "on")) {
133 led
->cdev
.brightness
= maxbright
;
134 pm8058_led_set(&led
->cdev
, maxbright
);
136 led
->cdev
.brightness
= LED_OFF
;
137 pm8058_led_set(&led
->cdev
, LED_OFF
);
141 if (led
->ledtype
== PM8058_LED_TYPE_KEYPAD
||
142 led
->ledtype
== PM8058_LED_TYPE_FLASH
)
143 led
->cdev
.flags
= LED_CORE_SUSPENDRESUME
;
145 ret
= devm_led_classdev_register(&pdev
->dev
, &led
->cdev
);
147 dev_err(&pdev
->dev
, "unable to register led \"%s\"\n",
155 static const struct of_device_id pm8058_leds_id_table
[] = {
157 .compatible
= "qcom,pm8058-led",
158 .data
= (void *)PM8058_LED_TYPE_COMMON
161 .compatible
= "qcom,pm8058-keypad-led",
162 .data
= (void *)PM8058_LED_TYPE_KEYPAD
165 .compatible
= "qcom,pm8058-flash-led",
166 .data
= (void *)PM8058_LED_TYPE_FLASH
170 MODULE_DEVICE_TABLE(of
, pm8058_leds_id_table
);
172 static struct platform_driver pm8058_led_driver
= {
173 .probe
= pm8058_led_probe
,
175 .name
= "pm8058-leds",
176 .of_match_table
= pm8058_leds_id_table
,
179 module_platform_driver(pm8058_led_driver
);
181 MODULE_DESCRIPTION("PM8058 LEDs driver");
182 MODULE_LICENSE("GPL v2");
183 MODULE_ALIAS("platform:pm8058-leds");