1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2018 BayLibre SAS
4 // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
6 // LED driver for MAXIM 77650/77651 charger/power-supply.
9 #include <linux/leds.h>
10 #include <linux/mfd/max77650.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
15 #define MAX77650_LED_NUM_LEDS 3
17 #define MAX77650_LED_A_BASE 0x40
18 #define MAX77650_LED_B_BASE 0x43
20 #define MAX77650_LED_BR_MASK GENMASK(4, 0)
21 #define MAX77650_LED_EN_MASK GENMASK(7, 6)
23 #define MAX77650_LED_MAX_BRIGHTNESS MAX77650_LED_BR_MASK
25 /* Enable EN_LED_MSTR. */
26 #define MAX77650_LED_TOP_DEFAULT BIT(0)
28 #define MAX77650_LED_ENABLE GENMASK(7, 6)
29 #define MAX77650_LED_DISABLE 0x00
31 #define MAX77650_LED_A_DEFAULT MAX77650_LED_DISABLE
33 #define MAX77650_LED_B_DEFAULT GENMASK(3, 0)
36 struct led_classdev cdev
;
42 static struct max77650_led
*max77650_to_led(struct led_classdev
*cdev
)
44 return container_of(cdev
, struct max77650_led
, cdev
);
47 static int max77650_led_brightness_set(struct led_classdev
*cdev
,
48 enum led_brightness brightness
)
50 struct max77650_led
*led
= max77650_to_led(cdev
);
53 mask
= MAX77650_LED_BR_MASK
| MAX77650_LED_EN_MASK
;
55 if (brightness
== LED_OFF
)
56 val
= MAX77650_LED_DISABLE
;
58 val
= MAX77650_LED_ENABLE
| brightness
;
60 return regmap_update_bits(led
->map
, led
->regA
, mask
, val
);
63 static int max77650_led_probe(struct platform_device
*pdev
)
65 struct fwnode_handle
*child
;
66 struct max77650_led
*leds
, *led
;
75 leds
= devm_kcalloc(dev
, sizeof(*leds
),
76 MAX77650_LED_NUM_LEDS
, GFP_KERNEL
);
80 map
= dev_get_regmap(dev
->parent
, NULL
);
84 num_leds
= device_get_child_node_count(dev
);
85 if (!num_leds
|| num_leds
> MAX77650_LED_NUM_LEDS
)
88 device_for_each_child_node(dev
, child
) {
89 rv
= fwnode_property_read_u32(child
, "reg", ®
);
90 if (rv
|| reg
>= MAX77650_LED_NUM_LEDS
) {
97 led
->regA
= MAX77650_LED_A_BASE
+ reg
;
98 led
->regB
= MAX77650_LED_B_BASE
+ reg
;
99 led
->cdev
.brightness_set_blocking
= max77650_led_brightness_set
;
100 led
->cdev
.max_brightness
= MAX77650_LED_MAX_BRIGHTNESS
;
102 rv
= fwnode_property_read_string(child
, "label", &label
);
104 led
->cdev
.name
= "max77650::";
106 led
->cdev
.name
= devm_kasprintf(dev
, GFP_KERNEL
,
107 "max77650:%s", label
);
108 if (!led
->cdev
.name
) {
114 fwnode_property_read_string(child
, "linux,default-trigger",
115 &led
->cdev
.default_trigger
);
117 rv
= devm_led_classdev_register(dev
, &led
->cdev
);
121 rv
= regmap_write(map
, led
->regA
, MAX77650_LED_A_DEFAULT
);
125 rv
= regmap_write(map
, led
->regB
, MAX77650_LED_B_DEFAULT
);
130 return regmap_write(map
,
131 MAX77650_REG_CNFG_LED_TOP
,
132 MAX77650_LED_TOP_DEFAULT
);
134 fwnode_handle_put(child
);
138 static struct platform_driver max77650_led_driver
= {
140 .name
= "max77650-led",
142 .probe
= max77650_led_probe
,
144 module_platform_driver(max77650_led_driver
);
146 MODULE_DESCRIPTION("MAXIM 77650/77651 LED driver");
147 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
148 MODULE_LICENSE("GPL v2");
149 MODULE_ALIAS("platform:max77650-led");