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