1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Copyright (C) 2018 BayLibre SAS 4 // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com> 5 // 6 // LED driver for MAXIM 77650/77651 charger/power-supply. 7 8 #include <linux/i2c.h> 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> 14 15 #define MAX77650_LED_NUM_LEDS 3 16 17 #define MAX77650_LED_A_BASE 0x40 18 #define MAX77650_LED_B_BASE 0x43 19 20 #define MAX77650_LED_BR_MASK GENMASK(4, 0) 21 #define MAX77650_LED_EN_MASK GENMASK(7, 6) 22 23 #define MAX77650_LED_MAX_BRIGHTNESS MAX77650_LED_BR_MASK 24 25 /* Enable EN_LED_MSTR. */ 26 #define MAX77650_LED_TOP_DEFAULT BIT(0) 27 28 #define MAX77650_LED_ENABLE GENMASK(7, 6) 29 #define MAX77650_LED_DISABLE 0x00 30 31 #define MAX77650_LED_A_DEFAULT MAX77650_LED_DISABLE 32 /* 100% on duty */ 33 #define MAX77650_LED_B_DEFAULT GENMASK(3, 0) 34 35 struct max77650_led { 36 struct led_classdev cdev; 37 struct regmap *map; 38 unsigned int regA; 39 unsigned int regB; 40 }; 41 42 static struct max77650_led *max77650_to_led(struct led_classdev *cdev) 43 { 44 return container_of(cdev, struct max77650_led, cdev); 45 } 46 47 static int max77650_led_brightness_set(struct led_classdev *cdev, 48 enum led_brightness brightness) 49 { 50 struct max77650_led *led = max77650_to_led(cdev); 51 int val, mask; 52 53 mask = MAX77650_LED_BR_MASK | MAX77650_LED_EN_MASK; 54 55 if (brightness == LED_OFF) 56 val = MAX77650_LED_DISABLE; 57 else 58 val = MAX77650_LED_ENABLE | brightness; 59 60 return regmap_update_bits(led->map, led->regA, mask, val); 61 } 62 63 static int max77650_led_probe(struct platform_device *pdev) 64 { 65 struct device_node *of_node, *child; 66 struct max77650_led *leds, *led; 67 struct device *dev; 68 struct regmap *map; 69 const char *label; 70 int rv, num_leds; 71 u32 reg; 72 73 dev = &pdev->dev; 74 of_node = dev->of_node; 75 76 if (!of_node) 77 return -ENODEV; 78 79 leds = devm_kcalloc(dev, sizeof(*leds), 80 MAX77650_LED_NUM_LEDS, GFP_KERNEL); 81 if (!leds) 82 return -ENOMEM; 83 84 map = dev_get_regmap(dev->parent, NULL); 85 if (!map) 86 return -ENODEV; 87 88 num_leds = of_get_child_count(of_node); 89 if (!num_leds || num_leds > MAX77650_LED_NUM_LEDS) 90 return -ENODEV; 91 92 for_each_child_of_node(of_node, child) { 93 rv = of_property_read_u32(child, "reg", ®); 94 if (rv || reg >= MAX77650_LED_NUM_LEDS) { 95 rv = -EINVAL; 96 goto err_node_put; 97 } 98 99 led = &leds[reg]; 100 led->map = map; 101 led->regA = MAX77650_LED_A_BASE + reg; 102 led->regB = MAX77650_LED_B_BASE + reg; 103 led->cdev.brightness_set_blocking = max77650_led_brightness_set; 104 led->cdev.max_brightness = MAX77650_LED_MAX_BRIGHTNESS; 105 106 label = of_get_property(child, "label", NULL); 107 if (!label) { 108 led->cdev.name = "max77650::"; 109 } else { 110 led->cdev.name = devm_kasprintf(dev, GFP_KERNEL, 111 "max77650:%s", label); 112 if (!led->cdev.name) { 113 rv = -ENOMEM; 114 goto err_node_put; 115 } 116 } 117 118 of_property_read_string(child, "linux,default-trigger", 119 &led->cdev.default_trigger); 120 121 rv = devm_led_classdev_register(dev, &led->cdev); 122 if (rv) 123 goto err_node_put; 124 125 rv = regmap_write(map, led->regA, MAX77650_LED_A_DEFAULT); 126 if (rv) 127 goto err_node_put; 128 129 rv = regmap_write(map, led->regB, MAX77650_LED_B_DEFAULT); 130 if (rv) 131 goto err_node_put; 132 } 133 134 return regmap_write(map, 135 MAX77650_REG_CNFG_LED_TOP, 136 MAX77650_LED_TOP_DEFAULT); 137 err_node_put: 138 of_node_put(child); 139 return rv; 140 } 141 142 static struct platform_driver max77650_led_driver = { 143 .driver = { 144 .name = "max77650-led", 145 }, 146 .probe = max77650_led_probe, 147 }; 148 module_platform_driver(max77650_led_driver); 149 150 MODULE_DESCRIPTION("MAXIM 77650/77651 LED driver"); 151 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>"); 152 MODULE_LICENSE("GPL v2"); 153 MODULE_ALIAS("platform:max77650-led"); 154