1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Generic Syscon LEDs Driver 4 * 5 * Copyright (c) 2014, Linaro Limited 6 * Author: Linus Walleij <linus.walleij@linaro.org> 7 */ 8 #include <linux/io.h> 9 #include <linux/init.h> 10 #include <linux/of_device.h> 11 #include <linux/of_address.h> 12 #include <linux/platform_device.h> 13 #include <linux/stat.h> 14 #include <linux/slab.h> 15 #include <linux/mfd/syscon.h> 16 #include <linux/regmap.h> 17 #include <linux/leds.h> 18 19 /** 20 * struct syscon_led - state container for syscon based LEDs 21 * @cdev: LED class device for this LED 22 * @map: regmap to access the syscon device backing this LED 23 * @offset: the offset into the syscon regmap for the LED register 24 * @mask: the bit in the register corresponding to the LED 25 * @state: current state of the LED 26 */ 27 struct syscon_led { 28 struct led_classdev cdev; 29 struct regmap *map; 30 u32 offset; 31 u32 mask; 32 bool state; 33 }; 34 35 static void syscon_led_set(struct led_classdev *led_cdev, 36 enum led_brightness value) 37 { 38 struct syscon_led *sled = 39 container_of(led_cdev, struct syscon_led, cdev); 40 u32 val; 41 int ret; 42 43 if (value == LED_OFF) { 44 val = 0; 45 sled->state = false; 46 } else { 47 val = sled->mask; 48 sled->state = true; 49 } 50 51 ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val); 52 if (ret < 0) 53 dev_err(sled->cdev.dev, "error updating LED status\n"); 54 } 55 56 static int syscon_led_probe(struct platform_device *pdev) 57 { 58 struct led_init_data init_data = {}; 59 struct device *dev = &pdev->dev; 60 struct device_node *np = dev_of_node(dev); 61 struct device *parent; 62 struct regmap *map; 63 struct syscon_led *sled; 64 enum led_default_state state; 65 u32 value; 66 int ret; 67 68 parent = dev->parent; 69 if (!parent) { 70 dev_err(dev, "no parent for syscon LED\n"); 71 return -ENODEV; 72 } 73 map = syscon_node_to_regmap(dev_of_node(parent)); 74 if (IS_ERR(map)) { 75 dev_err(dev, "no regmap for syscon LED parent\n"); 76 return PTR_ERR(map); 77 } 78 79 sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL); 80 if (!sled) 81 return -ENOMEM; 82 83 sled->map = map; 84 85 if (of_property_read_u32(np, "offset", &sled->offset)) 86 return -EINVAL; 87 if (of_property_read_u32(np, "mask", &sled->mask)) 88 return -EINVAL; 89 90 init_data.fwnode = of_fwnode_handle(np); 91 92 state = led_init_default_state_get(init_data.fwnode); 93 switch (state) { 94 case LEDS_DEFSTATE_ON: 95 ret = regmap_update_bits(map, sled->offset, sled->mask, sled->mask); 96 if (ret < 0) 97 return ret; 98 sled->state = true; 99 break; 100 case LEDS_DEFSTATE_KEEP: 101 ret = regmap_read(map, sled->offset, &value); 102 if (ret < 0) 103 return ret; 104 sled->state = !!(value & sled->mask); 105 break; 106 default: 107 ret = regmap_update_bits(map, sled->offset, sled->mask, 0); 108 if (ret < 0) 109 return ret; 110 sled->state = false; 111 } 112 sled->cdev.brightness_set = syscon_led_set; 113 114 ret = devm_led_classdev_register_ext(dev, &sled->cdev, &init_data); 115 if (ret < 0) 116 return ret; 117 118 platform_set_drvdata(pdev, sled); 119 dev_info(dev, "registered LED %s\n", sled->cdev.name); 120 121 return 0; 122 } 123 124 static const struct of_device_id of_syscon_leds_match[] = { 125 { .compatible = "register-bit-led", }, 126 {}, 127 }; 128 129 static struct platform_driver syscon_led_driver = { 130 .probe = syscon_led_probe, 131 .driver = { 132 .name = "leds-syscon", 133 .of_match_table = of_syscon_leds_match, 134 .suppress_bind_attrs = true, 135 }, 136 }; 137 builtin_platform_driver(syscon_led_driver); 138