1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Device driver for leds in MAX5970 and MAX5978 IC 4 * 5 * Copyright (c) 2022 9elements GmbH 6 * 7 * Author: Patrick Rudolph <patrick.rudolph@9elements.com> 8 */ 9 10 #include <linux/bits.h> 11 #include <linux/container_of.h> 12 #include <linux/device.h> 13 #include <linux/leds.h> 14 #include <linux/mfd/max5970.h> 15 #include <linux/module.h> 16 #include <linux/platform_device.h> 17 #include <linux/property.h> 18 #include <linux/regmap.h> 19 20 #define ldev_to_maxled(c) container_of(c, struct max5970_led, cdev) 21 22 struct max5970_led { 23 struct device *dev; 24 struct regmap *regmap; 25 struct led_classdev cdev; 26 unsigned int index; 27 }; 28 29 static int max5970_led_set_brightness(struct led_classdev *cdev, 30 enum led_brightness brightness) 31 { 32 struct max5970_led *ddata = ldev_to_maxled(cdev); 33 int ret, val; 34 35 /* Set/clear corresponding bit for given led index */ 36 val = !brightness ? BIT(ddata->index) : 0; 37 38 ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val); 39 if (ret < 0) 40 dev_err(cdev->dev, "failed to set brightness %d", ret); 41 42 return ret; 43 } 44 45 static int max5970_led_probe(struct platform_device *pdev) 46 { 47 struct fwnode_handle *child; 48 struct device *dev = &pdev->dev; 49 struct regmap *regmap; 50 struct max5970_led *ddata; 51 int ret = -ENODEV; 52 53 regmap = dev_get_regmap(dev->parent, NULL); 54 if (!regmap) 55 return -ENODEV; 56 57 struct fwnode_handle *led_node __free(fwnode_handle) = 58 device_get_named_child_node(dev->parent, "leds"); 59 if (!led_node) 60 return -ENODEV; 61 62 fwnode_for_each_child_node(led_node, child) { 63 u32 reg; 64 65 if (fwnode_property_read_u32(child, "reg", ®)) 66 continue; 67 68 if (reg >= MAX5970_NUM_LEDS) { 69 dev_err_probe(dev, -EINVAL, "invalid LED (%u >= %d)\n", reg, MAX5970_NUM_LEDS); 70 continue; 71 } 72 73 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); 74 if (!ddata) { 75 fwnode_handle_put(child); 76 return -ENOMEM; 77 } 78 79 ddata->index = reg; 80 ddata->regmap = regmap; 81 ddata->dev = dev; 82 83 if (fwnode_property_read_string(child, "label", &ddata->cdev.name)) 84 ddata->cdev.name = fwnode_get_name(child); 85 86 ddata->cdev.max_brightness = 1; 87 ddata->cdev.brightness_set_blocking = max5970_led_set_brightness; 88 ddata->cdev.default_trigger = "none"; 89 90 ret = devm_led_classdev_register(dev, &ddata->cdev); 91 if (ret < 0) { 92 fwnode_handle_put(child); 93 return dev_err_probe(dev, ret, "Failed to initialize LED %u\n", reg); 94 } 95 } 96 97 return ret; 98 } 99 100 static struct platform_driver max5970_led_driver = { 101 .driver = { 102 .name = "max5970-led", 103 }, 104 .probe = max5970_led_probe, 105 }; 106 module_platform_driver(max5970_led_driver); 107 108 MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>"); 109 MODULE_AUTHOR("Naresh Solanki <Naresh.Solanki@9elements.com>"); 110 MODULE_DESCRIPTION("MAX5970_hot-swap controller LED driver"); 111 MODULE_LICENSE("GPL"); 112