Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Walleij | 535f09c | 2014-05-22 00:34:16 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Generic Syscon LEDs Driver |
| 4 | * |
| 5 | * Copyright (c) 2014, Linaro Limited |
| 6 | * Author: Linus Walleij <linus.walleij@linaro.org> |
Linus Walleij | 535f09c | 2014-05-22 00:34:16 +0200 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/io.h> |
Paul Gortmaker | f7d98a6 | 2015-12-13 16:45:51 -0500 | [diff] [blame] | 9 | #include <linux/init.h> |
Linus Walleij | 535f09c | 2014-05-22 00:34:16 +0200 | [diff] [blame] | 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 | |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 56 | static int syscon_led_probe(struct platform_device *pdev) |
Linus Walleij | 535f09c | 2014-05-22 00:34:16 +0200 | [diff] [blame] | 57 | { |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 58 | struct device *dev = &pdev->dev; |
| 59 | struct device_node *np = dev->of_node; |
| 60 | struct device *parent; |
| 61 | struct regmap *map; |
| 62 | struct syscon_led *sled; |
| 63 | const char *state; |
Linus Walleij | 535f09c | 2014-05-22 00:34:16 +0200 | [diff] [blame] | 64 | int ret; |
| 65 | |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 66 | parent = dev->parent; |
| 67 | if (!parent) { |
| 68 | dev_err(dev, "no parent for syscon LED\n"); |
| 69 | return -ENODEV; |
Linus Walleij | 535f09c | 2014-05-22 00:34:16 +0200 | [diff] [blame] | 70 | } |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 71 | map = syscon_node_to_regmap(parent->of_node); |
Bjorn Andersson | 991a3f6 | 2015-08-18 12:25:26 -0700 | [diff] [blame] | 72 | if (IS_ERR(map)) { |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 73 | dev_err(dev, "no regmap for syscon LED parent\n"); |
Bjorn Andersson | 991a3f6 | 2015-08-18 12:25:26 -0700 | [diff] [blame] | 74 | return PTR_ERR(map); |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL); |
| 78 | if (!sled) |
| 79 | return -ENOMEM; |
| 80 | |
| 81 | sled->map = map; |
| 82 | |
| 83 | if (of_property_read_u32(np, "offset", &sled->offset)) |
| 84 | return -EINVAL; |
| 85 | if (of_property_read_u32(np, "mask", &sled->mask)) |
| 86 | return -EINVAL; |
| 87 | sled->cdev.name = |
| 88 | of_get_property(np, "label", NULL) ? : np->name; |
| 89 | sled->cdev.default_trigger = |
| 90 | of_get_property(np, "linux,default-trigger", NULL); |
| 91 | |
| 92 | state = of_get_property(np, "default-state", NULL); |
| 93 | if (state) { |
| 94 | if (!strcmp(state, "keep")) { |
| 95 | u32 val; |
| 96 | |
| 97 | ret = regmap_read(map, sled->offset, &val); |
| 98 | if (ret < 0) |
| 99 | return ret; |
| 100 | sled->state = !!(val & sled->mask); |
| 101 | } else if (!strcmp(state, "on")) { |
| 102 | sled->state = true; |
| 103 | ret = regmap_update_bits(map, sled->offset, |
| 104 | sled->mask, |
| 105 | sled->mask); |
| 106 | if (ret < 0) |
| 107 | return ret; |
| 108 | } else { |
| 109 | sled->state = false; |
| 110 | ret = regmap_update_bits(map, sled->offset, |
| 111 | sled->mask, 0); |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | } |
| 115 | } |
| 116 | sled->cdev.brightness_set = syscon_led_set; |
| 117 | |
Alexander Dahl | 68bfcf6 | 2019-08-27 15:00:27 +0200 | [diff] [blame] | 118 | ret = devm_led_classdev_register(dev, &sled->cdev); |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | |
| 122 | platform_set_drvdata(pdev, sled); |
| 123 | dev_info(dev, "registered LED %s\n", sled->cdev.name); |
| 124 | |
Linus Walleij | 3f6e42c | 2014-10-24 03:57:19 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
Linus Walleij | 535f09c | 2014-05-22 00:34:16 +0200 | [diff] [blame] | 127 | |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 128 | static const struct of_device_id of_syscon_leds_match[] = { |
| 129 | { .compatible = "register-bit-led", }, |
| 130 | {}, |
| 131 | }; |
| 132 | |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 133 | static struct platform_driver syscon_led_driver = { |
| 134 | .probe = syscon_led_probe, |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 135 | .driver = { |
| 136 | .name = "leds-syscon", |
| 137 | .of_match_table = of_syscon_leds_match, |
Paul Gortmaker | f7d98a6 | 2015-12-13 16:45:51 -0500 | [diff] [blame] | 138 | .suppress_bind_attrs = true, |
Linus Walleij | a917d4b4 | 2015-03-03 10:08:19 +0100 | [diff] [blame] | 139 | }, |
| 140 | }; |
Paul Gortmaker | f7d98a6 | 2015-12-13 16:45:51 -0500 | [diff] [blame] | 141 | builtin_platform_driver(syscon_led_driver); |