blob: 80553022d661fcbd09e1a0048472deb5486b70ef [file] [log] [blame]
Linus Walleijb5417012010-07-20 22:32:00 +01001/*
2 * Driver for the 8 user LEDs found on the RealViews and Versatiles
3 * Based on DaVinci's DM365 board code
4 *
5 * License terms: GNU General Public License (GPL) version 2
6 * Author: Linus Walleij <triad@df.lth.se>
7 */
8#include <linux/kernel.h>
9#include <linux/init.h>
Linus Walleije4ecf2b2014-02-27 14:29:22 +010010#include <linux/module.h>
Linus Walleijb5417012010-07-20 22:32:00 +010011#include <linux/io.h>
12#include <linux/slab.h>
13#include <linux/leds.h>
Linus Walleije4ecf2b2014-02-27 14:29:22 +010014#include <linux/platform_device.h>
Linus Walleijb5417012010-07-20 22:32:00 +010015
16struct versatile_led {
Linus Walleije4ecf2b2014-02-27 14:29:22 +010017 void __iomem *base;
Linus Walleijb5417012010-07-20 22:32:00 +010018 struct led_classdev cdev;
19 u8 mask;
20};
21
22/*
23 * The triggers lines up below will only be used if the
24 * LED triggers are compiled in.
25 */
26static const struct {
27 const char *name;
28 const char *trigger;
29} versatile_leds[] = {
30 { "versatile:0", "heartbeat", },
31 { "versatile:1", "mmc0", },
Bryan Wue031cd52012-03-13 18:06:36 +080032 { "versatile:2", "cpu0" },
33 { "versatile:3", "cpu1" },
34 { "versatile:4", "cpu2" },
35 { "versatile:5", "cpu3" },
Linus Walleijb5417012010-07-20 22:32:00 +010036 { "versatile:6", },
37 { "versatile:7", },
38};
39
40static void versatile_led_set(struct led_classdev *cdev,
41 enum led_brightness b)
42{
43 struct versatile_led *led = container_of(cdev,
44 struct versatile_led, cdev);
Linus Walleije4ecf2b2014-02-27 14:29:22 +010045 u32 reg = readl(led->base);
Linus Walleijb5417012010-07-20 22:32:00 +010046
47 if (b != LED_OFF)
48 reg |= led->mask;
49 else
50 reg &= ~led->mask;
Linus Walleije4ecf2b2014-02-27 14:29:22 +010051 writel(reg, led->base);
Linus Walleijb5417012010-07-20 22:32:00 +010052}
53
54static enum led_brightness versatile_led_get(struct led_classdev *cdev)
55{
56 struct versatile_led *led = container_of(cdev,
57 struct versatile_led, cdev);
Linus Walleije4ecf2b2014-02-27 14:29:22 +010058 u32 reg = readl(led->base);
Linus Walleijb5417012010-07-20 22:32:00 +010059
60 return (reg & led->mask) ? LED_FULL : LED_OFF;
61}
62
Linus Walleije4ecf2b2014-02-27 14:29:22 +010063static int versatile_leds_probe(struct platform_device *dev)
Linus Walleijb5417012010-07-20 22:32:00 +010064{
65 int i;
Linus Walleije4ecf2b2014-02-27 14:29:22 +010066 struct resource *res;
67 void __iomem *base;
68
69 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
70 base = devm_ioremap_resource(&dev->dev, res);
71 if (IS_ERR(base))
72 return PTR_ERR(base);
Linus Walleijb5417012010-07-20 22:32:00 +010073
Russell Kingd1cb3ec2013-11-26 19:44:18 +000074 /* All off */
Linus Walleije4ecf2b2014-02-27 14:29:22 +010075 writel(0, base);
Linus Walleijb5417012010-07-20 22:32:00 +010076 for (i = 0; i < ARRAY_SIZE(versatile_leds); i++) {
77 struct versatile_led *led;
78
79 led = kzalloc(sizeof(*led), GFP_KERNEL);
80 if (!led)
81 break;
82
Linus Walleije4ecf2b2014-02-27 14:29:22 +010083 led->base = base;
Linus Walleijb5417012010-07-20 22:32:00 +010084 led->cdev.name = versatile_leds[i].name;
85 led->cdev.brightness_set = versatile_led_set;
86 led->cdev.brightness_get = versatile_led_get;
87 led->cdev.default_trigger = versatile_leds[i].trigger;
88 led->mask = BIT(i);
89
90 if (led_classdev_register(NULL, &led->cdev) < 0) {
91 kfree(led);
92 break;
93 }
94 }
95
96 return 0;
97}
98
Linus Walleije4ecf2b2014-02-27 14:29:22 +010099static struct platform_driver versatile_leds_driver = {
100 .driver = {
101 .name = "versatile-leds",
102 },
103 .probe = versatile_leds_probe,
104};
105
106module_platform_driver(versatile_leds_driver);
107
108MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
109MODULE_DESCRIPTION("ARM Versatile LED driver");
110MODULE_LICENSE("GPL v2");