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/platform_device.h>
9 #include <linux/regmap.h>
11 #define PM8058_LED_TYPE_COMMON 0x00
12 #define PM8058_LED_TYPE_KEYPAD 0x01
13 #define PM8058_LED_TYPE_FLASH 0x02
15 #define PM8058_LED_TYPE_COMMON_MASK 0xf8
16 #define PM8058_LED_TYPE_KEYPAD_MASK 0xf0
17 #define PM8058_LED_TYPE_COMMON_SHIFT 3
18 #define PM8058_LED_TYPE_KEYPAD_SHIFT 4
24 struct led_classdev cdev
;
27 static void pm8058_led_set(struct led_classdev
*cled
,
28 enum led_brightness value
)
30 struct pm8058_led
*led
;
32 unsigned int mask
= 0;
35 led
= container_of(cled
, struct pm8058_led
, cdev
);
36 switch (led
->ledtype
) {
37 case PM8058_LED_TYPE_COMMON
:
38 mask
= PM8058_LED_TYPE_COMMON_MASK
;
39 val
= value
<< PM8058_LED_TYPE_COMMON_SHIFT
;
41 case PM8058_LED_TYPE_KEYPAD
:
42 case PM8058_LED_TYPE_FLASH
:
43 mask
= PM8058_LED_TYPE_KEYPAD_MASK
;
44 val
= value
<< PM8058_LED_TYPE_KEYPAD_SHIFT
;
50 ret
= regmap_update_bits(led
->map
, led
->reg
, mask
, val
);
52 pr_err("Failed to set LED brightness\n");
55 static enum led_brightness
pm8058_led_get(struct led_classdev
*cled
)
57 struct pm8058_led
*led
;
61 led
= container_of(cled
, struct pm8058_led
, cdev
);
63 ret
= regmap_read(led
->map
, led
->reg
, &val
);
65 pr_err("Failed to get LED brightness\n");
69 switch (led
->ledtype
) {
70 case PM8058_LED_TYPE_COMMON
:
71 val
&= PM8058_LED_TYPE_COMMON_MASK
;
72 val
>>= PM8058_LED_TYPE_COMMON_SHIFT
;
74 case PM8058_LED_TYPE_KEYPAD
:
75 case PM8058_LED_TYPE_FLASH
:
76 val
&= PM8058_LED_TYPE_KEYPAD_MASK
;
77 val
>>= PM8058_LED_TYPE_KEYPAD_SHIFT
;
87 static int pm8058_led_probe(struct platform_device
*pdev
)
89 struct led_init_data init_data
= {};
90 struct device
*dev
= &pdev
->dev
;
91 struct pm8058_led
*led
;
92 struct device_node
*np
;
95 enum led_brightness maxbright
;
96 enum led_default_state state
;
98 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
102 led
->ledtype
= (u32
)(unsigned long)of_device_get_match_data(dev
);
104 map
= dev_get_regmap(dev
->parent
, NULL
);
106 dev_err(dev
, "Parent regmap unavailable.\n");
111 np
= dev_of_node(dev
);
113 ret
= of_property_read_u32(np
, "reg", &led
->reg
);
115 dev_err(dev
, "no register offset specified\n");
119 led
->cdev
.brightness_set
= pm8058_led_set
;
120 led
->cdev
.brightness_get
= pm8058_led_get
;
121 if (led
->ledtype
== PM8058_LED_TYPE_COMMON
)
122 maxbright
= 31; /* 5 bits */
124 maxbright
= 15; /* 4 bits */
125 led
->cdev
.max_brightness
= maxbright
;
127 init_data
.fwnode
= of_fwnode_handle(np
);
129 state
= led_init_default_state_get(init_data
.fwnode
);
131 case LEDS_DEFSTATE_ON
:
132 led
->cdev
.brightness
= maxbright
;
133 pm8058_led_set(&led
->cdev
, maxbright
);
135 case LEDS_DEFSTATE_KEEP
:
136 led
->cdev
.brightness
= pm8058_led_get(&led
->cdev
);
139 led
->cdev
.brightness
= LED_OFF
;
140 pm8058_led_set(&led
->cdev
, LED_OFF
);
143 if (led
->ledtype
== PM8058_LED_TYPE_KEYPAD
||
144 led
->ledtype
== PM8058_LED_TYPE_FLASH
)
145 led
->cdev
.flags
= LED_CORE_SUSPENDRESUME
;
147 ret
= devm_led_classdev_register_ext(dev
, &led
->cdev
, &init_data
);
149 dev_err(dev
, "Failed to register LED for %pOF\n", np
);
154 static const struct of_device_id pm8058_leds_id_table
[] = {
156 .compatible
= "qcom,pm8058-led",
157 .data
= (void *)PM8058_LED_TYPE_COMMON
160 .compatible
= "qcom,pm8058-keypad-led",
161 .data
= (void *)PM8058_LED_TYPE_KEYPAD
164 .compatible
= "qcom,pm8058-flash-led",
165 .data
= (void *)PM8058_LED_TYPE_FLASH
169 MODULE_DEVICE_TABLE(of
, pm8058_leds_id_table
);
171 static struct platform_driver pm8058_led_driver
= {
172 .probe
= pm8058_led_probe
,
174 .name
= "pm8058-leds",
175 .of_match_table
= pm8058_leds_id_table
,
178 module_platform_driver(pm8058_led_driver
);
180 MODULE_DESCRIPTION("PM8058 LEDs driver");
181 MODULE_LICENSE("GPL v2");
182 MODULE_ALIAS("platform:pm8058-leds");