1 // SPDX-License-Identifier: GPL-2.0+
2 // Driver for Awinic AW2013 3-channel LED driver
5 #include <linux/leds.h>
6 #include <linux/module.h>
7 #include <linux/regulator/consumer.h>
8 #include <linux/mutex.h>
10 #include <linux/regmap.h>
12 #define AW2013_MAX_LEDS 3
14 /* Reset and ID register */
15 #define AW2013_RSTR 0x00
16 #define AW2013_RSTR_RESET 0x55
17 #define AW2013_RSTR_CHIP_ID 0x33
19 /* Global control register */
20 #define AW2013_GCR 0x01
21 #define AW2013_GCR_ENABLE BIT(0)
23 /* LED channel enable register */
24 #define AW2013_LCTR 0x30
25 #define AW2013_LCTR_LE(x) BIT((x))
27 /* LED channel control registers */
28 #define AW2013_LCFG(x) (0x31 + (x))
29 #define AW2013_LCFG_IMAX_MASK (BIT(0) | BIT(1)) // Should be 0-3
30 #define AW2013_LCFG_MD BIT(4)
31 #define AW2013_LCFG_FI BIT(5)
32 #define AW2013_LCFG_FO BIT(6)
34 /* LED channel PWM registers */
35 #define AW2013_REG_PWM(x) (0x34 + (x))
37 /* LED channel timing registers */
38 #define AW2013_LEDT0(x) (0x37 + (x) * 3)
39 #define AW2013_LEDT0_T1(x) ((x) << 4) // Should be 0-7
40 #define AW2013_LEDT0_T2(x) (x) // Should be 0-5
42 #define AW2013_LEDT1(x) (0x38 + (x) * 3)
43 #define AW2013_LEDT1_T3(x) ((x) << 4) // Should be 0-7
44 #define AW2013_LEDT1_T4(x) (x) // Should be 0-7
46 #define AW2013_LEDT2(x) (0x39 + (x) * 3)
47 #define AW2013_LEDT2_T0(x) ((x) << 4) // Should be 0-8
48 #define AW2013_LEDT2_REPEAT(x) (x) // Should be 0-15
50 #define AW2013_REG_MAX 0x77
52 #define AW2013_TIME_STEP 130 /* ms */
58 struct led_classdev cdev
;
64 struct mutex mutex
; /* held when writing to registers */
65 struct regulator
*vcc_regulator
;
66 struct i2c_client
*client
;
67 struct aw2013_led leds
[AW2013_MAX_LEDS
];
68 struct regmap
*regmap
;
73 static int aw2013_chip_init(struct aw2013
*chip
)
77 ret
= regmap_write(chip
->regmap
, AW2013_GCR
, AW2013_GCR_ENABLE
);
79 dev_err(&chip
->client
->dev
, "Failed to enable the chip: %d\n",
84 for (i
= 0; i
< chip
->num_leds
; i
++) {
85 ret
= regmap_update_bits(chip
->regmap
,
86 AW2013_LCFG(chip
->leds
[i
].num
),
87 AW2013_LCFG_IMAX_MASK
,
90 dev_err(&chip
->client
->dev
,
91 "Failed to set maximum current for led %d: %d\n",
92 chip
->leds
[i
].num
, ret
);
100 static void aw2013_chip_disable(struct aw2013
*chip
)
107 regmap_write(chip
->regmap
, AW2013_GCR
, 0);
109 ret
= regulator_disable(chip
->vcc_regulator
);
111 dev_err(&chip
->client
->dev
,
112 "Failed to disable regulator: %d\n", ret
);
116 chip
->enabled
= false;
119 static int aw2013_chip_enable(struct aw2013
*chip
)
126 ret
= regulator_enable(chip
->vcc_regulator
);
128 dev_err(&chip
->client
->dev
,
129 "Failed to enable regulator: %d\n", ret
);
132 chip
->enabled
= true;
134 ret
= aw2013_chip_init(chip
);
136 aw2013_chip_disable(chip
);
141 static bool aw2013_chip_in_use(struct aw2013
*chip
)
145 for (i
= 0; i
< chip
->num_leds
; i
++)
146 if (chip
->leds
[i
].cdev
.brightness
)
152 static int aw2013_brightness_set(struct led_classdev
*cdev
,
153 enum led_brightness brightness
)
155 struct aw2013_led
*led
= container_of(cdev
, struct aw2013_led
, cdev
);
158 mutex_lock(&led
->chip
->mutex
);
160 if (aw2013_chip_in_use(led
->chip
)) {
161 ret
= aw2013_chip_enable(led
->chip
);
168 ret
= regmap_write(led
->chip
->regmap
, AW2013_REG_PWM(num
), brightness
);
173 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCTR
,
174 AW2013_LCTR_LE(num
), 0xFF);
176 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCTR
,
177 AW2013_LCTR_LE(num
), 0);
180 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCFG(num
),
186 if (!aw2013_chip_in_use(led
->chip
))
187 aw2013_chip_disable(led
->chip
);
190 mutex_unlock(&led
->chip
->mutex
);
195 static int aw2013_blink_set(struct led_classdev
*cdev
,
196 unsigned long *delay_on
, unsigned long *delay_off
)
198 struct aw2013_led
*led
= container_of(cdev
, struct aw2013_led
, cdev
);
199 int ret
, num
= led
->num
;
200 unsigned long off
= 0, on
= 0;
202 /* If no blink specified, default to 1 Hz. */
203 if (!*delay_off
&& !*delay_on
) {
208 if (!led
->cdev
.brightness
) {
209 led
->cdev
.brightness
= LED_FULL
;
210 ret
= aw2013_brightness_set(&led
->cdev
, led
->cdev
.brightness
);
215 /* Never on - just set to off */
217 led
->cdev
.brightness
= LED_OFF
;
218 return aw2013_brightness_set(&led
->cdev
, LED_OFF
);
221 mutex_lock(&led
->chip
->mutex
);
223 /* Never off - brightness is already set, disable blinking */
225 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCFG(num
),
230 /* Convert into values the HW will understand. */
231 off
= min(5, ilog2((*delay_off
- 1) / AW2013_TIME_STEP
) + 1);
232 on
= min(7, ilog2((*delay_on
- 1) / AW2013_TIME_STEP
) + 1);
234 *delay_off
= BIT(off
) * AW2013_TIME_STEP
;
235 *delay_on
= BIT(on
) * AW2013_TIME_STEP
;
238 ret
= regmap_write(led
->chip
->regmap
,
239 AW2013_LEDT0(num
), AW2013_LEDT0_T2(on
));
242 ret
= regmap_write(led
->chip
->regmap
,
243 AW2013_LEDT1(num
), AW2013_LEDT1_T4(off
));
247 /* Finally, enable the LED */
248 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCFG(num
),
249 AW2013_LCFG_MD
, 0xFF);
253 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCTR
,
254 AW2013_LCTR_LE(num
), 0xFF);
257 mutex_unlock(&led
->chip
->mutex
);
262 static int aw2013_probe_dt(struct aw2013
*chip
)
264 struct device_node
*np
= dev_of_node(&chip
->client
->dev
), *child
;
265 int count
, ret
= 0, i
= 0;
266 struct aw2013_led
*led
;
268 count
= of_get_available_child_count(np
);
269 if (!count
|| count
> AW2013_MAX_LEDS
)
272 regmap_write(chip
->regmap
, AW2013_RSTR
, AW2013_RSTR_RESET
);
274 for_each_available_child_of_node(np
, child
) {
275 struct led_init_data init_data
= {};
279 ret
= of_property_read_u32(child
, "reg", &source
);
280 if (ret
!= 0 || source
>= AW2013_MAX_LEDS
) {
281 dev_err(&chip
->client
->dev
,
282 "Couldn't read LED address: %d\n", ret
);
287 led
= &chip
->leds
[i
];
290 init_data
.fwnode
= of_fwnode_handle(child
);
292 if (!of_property_read_u32(child
, "led-max-microamp", &imax
)) {
293 led
->imax
= min_t(u32
, imax
/ 5000, 3);
295 led
->imax
= 1; // 5mA
296 dev_info(&chip
->client
->dev
,
297 "DT property led-max-microamp is missing\n");
300 led
->cdev
.brightness_set_blocking
= aw2013_brightness_set
;
301 led
->cdev
.blink_set
= aw2013_blink_set
;
303 ret
= devm_led_classdev_register_ext(&chip
->client
->dev
,
304 &led
->cdev
, &init_data
);
321 static const struct regmap_config aw2013_regmap_config
= {
324 .max_register
= AW2013_REG_MAX
,
327 static int aw2013_probe(struct i2c_client
*client
)
333 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
337 mutex_init(&chip
->mutex
);
338 mutex_lock(&chip
->mutex
);
340 chip
->client
= client
;
341 i2c_set_clientdata(client
, chip
);
343 chip
->regmap
= devm_regmap_init_i2c(client
, &aw2013_regmap_config
);
344 if (IS_ERR(chip
->regmap
)) {
345 ret
= PTR_ERR(chip
->regmap
);
346 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
351 chip
->vcc_regulator
= devm_regulator_get(&client
->dev
, "vcc");
352 ret
= PTR_ERR_OR_ZERO(chip
->vcc_regulator
);
354 if (ret
!= -EPROBE_DEFER
)
355 dev_err(&client
->dev
,
356 "Failed to request regulator: %d\n", ret
);
360 ret
= regulator_enable(chip
->vcc_regulator
);
362 dev_err(&client
->dev
,
363 "Failed to enable regulator: %d\n", ret
);
367 ret
= regmap_read(chip
->regmap
, AW2013_RSTR
, &chipid
);
369 dev_err(&client
->dev
, "Failed to read chip ID: %d\n",
374 if (chipid
!= AW2013_RSTR_CHIP_ID
) {
375 dev_err(&client
->dev
, "Chip reported wrong ID: %x\n",
381 ret
= aw2013_probe_dt(chip
);
385 ret
= regulator_disable(chip
->vcc_regulator
);
387 dev_err(&client
->dev
,
388 "Failed to disable regulator: %d\n", ret
);
392 mutex_unlock(&chip
->mutex
);
397 regulator_disable(chip
->vcc_regulator
);
400 mutex_destroy(&chip
->mutex
);
404 static int aw2013_remove(struct i2c_client
*client
)
406 struct aw2013
*chip
= i2c_get_clientdata(client
);
408 aw2013_chip_disable(chip
);
410 mutex_destroy(&chip
->mutex
);
415 static const struct of_device_id aw2013_match_table
[] = {
416 { .compatible
= "awinic,aw2013", },
420 MODULE_DEVICE_TABLE(of
, aw2013_match_table
);
422 static struct i2c_driver aw2013_driver
= {
424 .name
= "leds-aw2013",
425 .of_match_table
= of_match_ptr(aw2013_match_table
),
427 .probe_new
= aw2013_probe
,
428 .remove
= aw2013_remove
,
431 module_i2c_driver(aw2013_driver
);
433 MODULE_AUTHOR("Nikita Travkin <nikitos.tr@gmail.com>");
434 MODULE_DESCRIPTION("AW2013 LED driver");
435 MODULE_LICENSE("GPL v2");