2 * Generic Syscon LEDs Driver
4 * Copyright (c) 2014, Linaro Limited
5 * Author: Linus Walleij <linus.walleij@linaro.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/of_address.h>
26 #include <linux/platform_device.h>
27 #include <linux/stat.h>
28 #include <linux/slab.h>
29 #include <linux/mfd/syscon.h>
30 #include <linux/regmap.h>
31 #include <linux/leds.h>
34 * struct syscon_led - state container for syscon based LEDs
35 * @cdev: LED class device for this LED
36 * @map: regmap to access the syscon device backing this LED
37 * @offset: the offset into the syscon regmap for the LED register
38 * @mask: the bit in the register corresponding to the LED
39 * @state: current state of the LED
42 struct led_classdev cdev
;
49 static void syscon_led_set(struct led_classdev
*led_cdev
,
50 enum led_brightness value
)
52 struct syscon_led
*sled
=
53 container_of(led_cdev
, struct syscon_led
, cdev
);
57 if (value
== LED_OFF
) {
65 ret
= regmap_update_bits(sled
->map
, sled
->offset
, sled
->mask
, val
);
67 dev_err(sled
->cdev
.dev
, "error updating LED status\n");
70 static int syscon_led_probe(struct platform_device
*pdev
)
72 struct device
*dev
= &pdev
->dev
;
73 struct device_node
*np
= dev
->of_node
;
74 struct device
*parent
;
76 struct syscon_led
*sled
;
82 dev_err(dev
, "no parent for syscon LED\n");
85 map
= syscon_node_to_regmap(parent
->of_node
);
87 dev_err(dev
, "no regmap for syscon LED parent\n");
91 sled
= devm_kzalloc(dev
, sizeof(*sled
), GFP_KERNEL
);
97 if (of_property_read_u32(np
, "offset", &sled
->offset
))
99 if (of_property_read_u32(np
, "mask", &sled
->mask
))
102 of_get_property(np
, "label", NULL
) ? : np
->name
;
103 sled
->cdev
.default_trigger
=
104 of_get_property(np
, "linux,default-trigger", NULL
);
106 state
= of_get_property(np
, "default-state", NULL
);
108 if (!strcmp(state
, "keep")) {
111 ret
= regmap_read(map
, sled
->offset
, &val
);
114 sled
->state
= !!(val
& sled
->mask
);
115 } else if (!strcmp(state
, "on")) {
117 ret
= regmap_update_bits(map
, sled
->offset
,
124 ret
= regmap_update_bits(map
, sled
->offset
,
130 sled
->cdev
.brightness_set
= syscon_led_set
;
132 ret
= led_classdev_register(dev
, &sled
->cdev
);
136 platform_set_drvdata(pdev
, sled
);
137 dev_info(dev
, "registered LED %s\n", sled
->cdev
.name
);
142 static int syscon_led_remove(struct platform_device
*pdev
)
144 struct syscon_led
*sled
= platform_get_drvdata(pdev
);
146 led_classdev_unregister(&sled
->cdev
);
148 regmap_update_bits(sled
->map
, sled
->offset
, sled
->mask
, 0);
152 static const struct of_device_id of_syscon_leds_match
[] = {
153 { .compatible
= "register-bit-led", },
157 MODULE_DEVICE_TABLE(of
, of_syscon_leds_match
);
159 static struct platform_driver syscon_led_driver
= {
160 .probe
= syscon_led_probe
,
161 .remove
= syscon_led_remove
,
163 .name
= "leds-syscon",
164 .of_match_table
= of_syscon_leds_match
,
167 module_platform_driver(syscon_led_driver
);