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_bulk_data regulators
[2];
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_bulk_disable(ARRAY_SIZE(chip
->regulators
),
112 dev_err(&chip
->client
->dev
,
113 "Failed to disable regulators: %d\n", ret
);
117 chip
->enabled
= false;
120 static int aw2013_chip_enable(struct aw2013
*chip
)
127 ret
= regulator_bulk_enable(ARRAY_SIZE(chip
->regulators
),
130 dev_err(&chip
->client
->dev
,
131 "Failed to enable regulators: %d\n", ret
);
134 chip
->enabled
= true;
136 ret
= aw2013_chip_init(chip
);
138 aw2013_chip_disable(chip
);
143 static bool aw2013_chip_in_use(struct aw2013
*chip
)
147 for (i
= 0; i
< chip
->num_leds
; i
++)
148 if (chip
->leds
[i
].cdev
.brightness
)
154 static int aw2013_brightness_set(struct led_classdev
*cdev
,
155 enum led_brightness brightness
)
157 struct aw2013_led
*led
= container_of(cdev
, struct aw2013_led
, cdev
);
160 mutex_lock(&led
->chip
->mutex
);
162 if (aw2013_chip_in_use(led
->chip
)) {
163 ret
= aw2013_chip_enable(led
->chip
);
170 ret
= regmap_write(led
->chip
->regmap
, AW2013_REG_PWM(num
), brightness
);
175 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCTR
,
176 AW2013_LCTR_LE(num
), 0xFF);
178 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCTR
,
179 AW2013_LCTR_LE(num
), 0);
182 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCFG(num
),
188 if (!aw2013_chip_in_use(led
->chip
))
189 aw2013_chip_disable(led
->chip
);
192 mutex_unlock(&led
->chip
->mutex
);
197 static int aw2013_blink_set(struct led_classdev
*cdev
,
198 unsigned long *delay_on
, unsigned long *delay_off
)
200 struct aw2013_led
*led
= container_of(cdev
, struct aw2013_led
, cdev
);
201 int ret
, num
= led
->num
;
202 unsigned long off
= 0, on
= 0;
204 /* If no blink specified, default to 1 Hz. */
205 if (!*delay_off
&& !*delay_on
) {
210 if (!led
->cdev
.brightness
) {
211 led
->cdev
.brightness
= LED_FULL
;
212 ret
= aw2013_brightness_set(&led
->cdev
, led
->cdev
.brightness
);
217 /* Never on - just set to off */
219 led
->cdev
.brightness
= LED_OFF
;
220 return aw2013_brightness_set(&led
->cdev
, LED_OFF
);
223 mutex_lock(&led
->chip
->mutex
);
225 /* Never off - brightness is already set, disable blinking */
227 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCFG(num
),
232 /* Convert into values the HW will understand. */
233 off
= min(5, ilog2((*delay_off
- 1) / AW2013_TIME_STEP
) + 1);
234 on
= min(7, ilog2((*delay_on
- 1) / AW2013_TIME_STEP
) + 1);
236 *delay_off
= BIT(off
) * AW2013_TIME_STEP
;
237 *delay_on
= BIT(on
) * AW2013_TIME_STEP
;
240 ret
= regmap_write(led
->chip
->regmap
,
241 AW2013_LEDT0(num
), AW2013_LEDT0_T2(on
));
244 ret
= regmap_write(led
->chip
->regmap
,
245 AW2013_LEDT1(num
), AW2013_LEDT1_T4(off
));
249 /* Finally, enable the LED */
250 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCFG(num
),
251 AW2013_LCFG_MD
, 0xFF);
255 ret
= regmap_update_bits(led
->chip
->regmap
, AW2013_LCTR
,
256 AW2013_LCTR_LE(num
), 0xFF);
259 mutex_unlock(&led
->chip
->mutex
);
264 static int aw2013_probe_dt(struct aw2013
*chip
)
266 struct device_node
*np
= dev_of_node(&chip
->client
->dev
);
267 int count
, ret
= 0, i
= 0;
268 struct aw2013_led
*led
;
270 count
= of_get_available_child_count(np
);
271 if (!count
|| count
> AW2013_MAX_LEDS
)
274 regmap_write(chip
->regmap
, AW2013_RSTR
, AW2013_RSTR_RESET
);
276 for_each_available_child_of_node_scoped(np
, child
) {
277 struct led_init_data init_data
= {};
281 ret
= of_property_read_u32(child
, "reg", &source
);
282 if (ret
!= 0 || source
>= AW2013_MAX_LEDS
) {
283 dev_err(&chip
->client
->dev
,
284 "Couldn't read LED address: %d\n", ret
);
289 led
= &chip
->leds
[i
];
292 init_data
.fwnode
= of_fwnode_handle(child
);
294 if (!of_property_read_u32(child
, "led-max-microamp", &imax
)) {
295 led
->imax
= min_t(u32
, imax
/ 5000, 3);
297 led
->imax
= 1; // 5mA
298 dev_info(&chip
->client
->dev
,
299 "DT property led-max-microamp is missing\n");
302 led
->cdev
.brightness_set_blocking
= aw2013_brightness_set
;
303 led
->cdev
.blink_set
= aw2013_blink_set
;
305 ret
= devm_led_classdev_register_ext(&chip
->client
->dev
,
306 &led
->cdev
, &init_data
);
321 static void aw2013_chip_disable_action(void *data
)
323 aw2013_chip_disable(data
);
326 static const struct regmap_config aw2013_regmap_config
= {
329 .max_register
= AW2013_REG_MAX
,
332 static int aw2013_probe(struct i2c_client
*client
)
338 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
342 ret
= devm_mutex_init(&client
->dev
, &chip
->mutex
);
346 mutex_lock(&chip
->mutex
);
348 chip
->client
= client
;
349 i2c_set_clientdata(client
, chip
);
351 chip
->regmap
= devm_regmap_init_i2c(client
, &aw2013_regmap_config
);
352 if (IS_ERR(chip
->regmap
)) {
353 ret
= PTR_ERR(chip
->regmap
);
354 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
359 chip
->regulators
[0].supply
= "vcc";
360 chip
->regulators
[1].supply
= "vio";
361 ret
= devm_regulator_bulk_get(&client
->dev
,
362 ARRAY_SIZE(chip
->regulators
),
365 if (ret
!= -EPROBE_DEFER
)
366 dev_err(&client
->dev
,
367 "Failed to request regulators: %d\n", ret
);
371 ret
= regulator_bulk_enable(ARRAY_SIZE(chip
->regulators
),
374 dev_err(&client
->dev
,
375 "Failed to enable regulators: %d\n", ret
);
379 ret
= regmap_read(chip
->regmap
, AW2013_RSTR
, &chipid
);
381 dev_err(&client
->dev
, "Failed to read chip ID: %d\n",
386 if (chipid
!= AW2013_RSTR_CHIP_ID
) {
387 dev_err(&client
->dev
, "Chip reported wrong ID: %x\n",
393 ret
= devm_add_action(&client
->dev
, aw2013_chip_disable_action
, chip
);
397 ret
= aw2013_probe_dt(chip
);
401 ret
= regulator_bulk_disable(ARRAY_SIZE(chip
->regulators
),
404 dev_err(&client
->dev
,
405 "Failed to disable regulators: %d\n", ret
);
409 mutex_unlock(&chip
->mutex
);
414 regulator_bulk_disable(ARRAY_SIZE(chip
->regulators
),
418 mutex_unlock(&chip
->mutex
);
422 static const struct of_device_id aw2013_match_table
[] = {
423 { .compatible
= "awinic,aw2013", },
427 MODULE_DEVICE_TABLE(of
, aw2013_match_table
);
429 static struct i2c_driver aw2013_driver
= {
431 .name
= "leds-aw2013",
432 .of_match_table
= aw2013_match_table
,
434 .probe
= aw2013_probe
,
437 module_i2c_driver(aw2013_driver
);
439 MODULE_AUTHOR("Nikita Travkin <nikitos.tr@gmail.com>");
440 MODULE_DESCRIPTION("AW2013 LED driver");
441 MODULE_LICENSE("GPL v2");