12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2589fca16SÁlvaro Fernández Rojas /*
3589fca16SÁlvaro Fernández Rojas * Driver for BCM6358 memory-mapped LEDs, based on leds-syscon.c
4589fca16SÁlvaro Fernández Rojas *
5589fca16SÁlvaro Fernández Rojas * Copyright 2015 Álvaro Fernández Rojas <noltari@gmail.com>
6589fca16SÁlvaro Fernández Rojas */
7589fca16SÁlvaro Fernández Rojas #include <linux/delay.h>
8589fca16SÁlvaro Fernández Rojas #include <linux/io.h>
9589fca16SÁlvaro Fernández Rojas #include <linux/leds.h>
10589fca16SÁlvaro Fernández Rojas #include <linux/module.h>
11589fca16SÁlvaro Fernández Rojas #include <linux/of.h>
12589fca16SÁlvaro Fernández Rojas #include <linux/platform_device.h>
13589fca16SÁlvaro Fernández Rojas #include <linux/spinlock.h>
14589fca16SÁlvaro Fernández Rojas
15589fca16SÁlvaro Fernández Rojas #define BCM6358_REG_MODE 0x0
16589fca16SÁlvaro Fernández Rojas #define BCM6358_REG_CTRL 0x4
17589fca16SÁlvaro Fernández Rojas
18589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_CLKDIV_MASK 3
19589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_CLKDIV_1 0
20589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_CLKDIV_2 1
21589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_CLKDIV_4 2
22589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_CLKDIV_8 3
23589fca16SÁlvaro Fernández Rojas
24589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_POLARITY BIT(2)
25589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_BUSY BIT(3)
26589fca16SÁlvaro Fernández Rojas
27589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_MAX_COUNT 32
28589fca16SÁlvaro Fernández Rojas #define BCM6358_SLED_WAIT 100
29589fca16SÁlvaro Fernández Rojas
30589fca16SÁlvaro Fernández Rojas /**
31589fca16SÁlvaro Fernández Rojas * struct bcm6358_led - state container for bcm6358 based LEDs
32589fca16SÁlvaro Fernández Rojas * @cdev: LED class device for this LED
33589fca16SÁlvaro Fernández Rojas * @mem: memory resource
34589fca16SÁlvaro Fernández Rojas * @lock: memory lock
35589fca16SÁlvaro Fernández Rojas * @pin: LED pin number
36589fca16SÁlvaro Fernández Rojas * @active_low: LED is active low
37589fca16SÁlvaro Fernández Rojas */
38589fca16SÁlvaro Fernández Rojas struct bcm6358_led {
39589fca16SÁlvaro Fernández Rojas struct led_classdev cdev;
40589fca16SÁlvaro Fernández Rojas void __iomem *mem;
41589fca16SÁlvaro Fernández Rojas spinlock_t *lock;
42589fca16SÁlvaro Fernández Rojas unsigned long pin;
43589fca16SÁlvaro Fernández Rojas bool active_low;
44589fca16SÁlvaro Fernández Rojas };
45589fca16SÁlvaro Fernández Rojas
bcm6358_led_write(void __iomem * reg,unsigned long data)46589fca16SÁlvaro Fernández Rojas static void bcm6358_led_write(void __iomem *reg, unsigned long data)
47589fca16SÁlvaro Fernández Rojas {
484ba113b6SÁlvaro Fernández Rojas #ifdef CONFIG_CPU_BIG_ENDIAN
49589fca16SÁlvaro Fernández Rojas iowrite32be(data, reg);
504ba113b6SÁlvaro Fernández Rojas #else
514ba113b6SÁlvaro Fernández Rojas writel(data, reg);
524ba113b6SÁlvaro Fernández Rojas #endif
53589fca16SÁlvaro Fernández Rojas }
54589fca16SÁlvaro Fernández Rojas
bcm6358_led_read(void __iomem * reg)55589fca16SÁlvaro Fernández Rojas static unsigned long bcm6358_led_read(void __iomem *reg)
56589fca16SÁlvaro Fernández Rojas {
574ba113b6SÁlvaro Fernández Rojas #ifdef CONFIG_CPU_BIG_ENDIAN
58589fca16SÁlvaro Fernández Rojas return ioread32be(reg);
594ba113b6SÁlvaro Fernández Rojas #else
604ba113b6SÁlvaro Fernández Rojas return readl(reg);
614ba113b6SÁlvaro Fernández Rojas #endif
62589fca16SÁlvaro Fernández Rojas }
63589fca16SÁlvaro Fernández Rojas
bcm6358_led_busy(void __iomem * mem)64589fca16SÁlvaro Fernández Rojas static unsigned long bcm6358_led_busy(void __iomem *mem)
65589fca16SÁlvaro Fernández Rojas {
66589fca16SÁlvaro Fernández Rojas unsigned long val;
67589fca16SÁlvaro Fernández Rojas
68589fca16SÁlvaro Fernández Rojas while ((val = bcm6358_led_read(mem + BCM6358_REG_CTRL)) &
69589fca16SÁlvaro Fernández Rojas BCM6358_SLED_BUSY)
70589fca16SÁlvaro Fernández Rojas udelay(BCM6358_SLED_WAIT);
71589fca16SÁlvaro Fernández Rojas
72589fca16SÁlvaro Fernández Rojas return val;
73589fca16SÁlvaro Fernández Rojas }
74589fca16SÁlvaro Fernández Rojas
bcm6358_led_set(struct led_classdev * led_cdev,enum led_brightness value)756e636a0aSÁlvaro Fernández Rojas static void bcm6358_led_set(struct led_classdev *led_cdev,
766e636a0aSÁlvaro Fernández Rojas enum led_brightness value)
77589fca16SÁlvaro Fernández Rojas {
786e636a0aSÁlvaro Fernández Rojas struct bcm6358_led *led =
796e636a0aSÁlvaro Fernández Rojas container_of(led_cdev, struct bcm6358_led, cdev);
806e636a0aSÁlvaro Fernández Rojas unsigned long flags, val;
81589fca16SÁlvaro Fernández Rojas
826e636a0aSÁlvaro Fernández Rojas spin_lock_irqsave(led->lock, flags);
83589fca16SÁlvaro Fernández Rojas bcm6358_led_busy(led->mem);
84589fca16SÁlvaro Fernández Rojas val = bcm6358_led_read(led->mem + BCM6358_REG_MODE);
85589fca16SÁlvaro Fernández Rojas if ((led->active_low && value == LED_OFF) ||
86589fca16SÁlvaro Fernández Rojas (!led->active_low && value != LED_OFF))
87589fca16SÁlvaro Fernández Rojas val |= BIT(led->pin);
88589fca16SÁlvaro Fernández Rojas else
89589fca16SÁlvaro Fernández Rojas val &= ~(BIT(led->pin));
90589fca16SÁlvaro Fernández Rojas bcm6358_led_write(led->mem + BCM6358_REG_MODE, val);
91589fca16SÁlvaro Fernández Rojas spin_unlock_irqrestore(led->lock, flags);
92589fca16SÁlvaro Fernández Rojas }
93589fca16SÁlvaro Fernández Rojas
bcm6358_led(struct device * dev,struct device_node * nc,u32 reg,void __iomem * mem,spinlock_t * lock)94589fca16SÁlvaro Fernández Rojas static int bcm6358_led(struct device *dev, struct device_node *nc, u32 reg,
95589fca16SÁlvaro Fernández Rojas void __iomem *mem, spinlock_t *lock)
96589fca16SÁlvaro Fernández Rojas {
97e4e912a3SMarek Behún struct led_init_data init_data = {};
98589fca16SÁlvaro Fernández Rojas struct bcm6358_led *led;
99*e41ff817SAndy Shevchenko enum led_default_state state;
100*e41ff817SAndy Shevchenko unsigned long val;
101589fca16SÁlvaro Fernández Rojas int rc;
102589fca16SÁlvaro Fernández Rojas
103589fca16SÁlvaro Fernández Rojas led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
104589fca16SÁlvaro Fernández Rojas if (!led)
105589fca16SÁlvaro Fernández Rojas return -ENOMEM;
106589fca16SÁlvaro Fernández Rojas
107589fca16SÁlvaro Fernández Rojas led->pin = reg;
108589fca16SÁlvaro Fernández Rojas led->mem = mem;
109589fca16SÁlvaro Fernández Rojas led->lock = lock;
110589fca16SÁlvaro Fernández Rojas
111589fca16SÁlvaro Fernández Rojas if (of_property_read_bool(nc, "active-low"))
112589fca16SÁlvaro Fernández Rojas led->active_low = true;
113589fca16SÁlvaro Fernández Rojas
114*e41ff817SAndy Shevchenko init_data.fwnode = of_fwnode_handle(nc);
115*e41ff817SAndy Shevchenko
116*e41ff817SAndy Shevchenko state = led_init_default_state_get(init_data.fwnode);
117*e41ff817SAndy Shevchenko switch (state) {
118*e41ff817SAndy Shevchenko case LEDS_DEFSTATE_ON:
119589fca16SÁlvaro Fernández Rojas led->cdev.brightness = LED_FULL;
120*e41ff817SAndy Shevchenko break;
121*e41ff817SAndy Shevchenko case LEDS_DEFSTATE_KEEP:
122589fca16SÁlvaro Fernández Rojas val = bcm6358_led_read(led->mem + BCM6358_REG_MODE);
123589fca16SÁlvaro Fernández Rojas val &= BIT(led->pin);
124*e41ff817SAndy Shevchenko if ((led->active_low && !val) || (!led->active_low && val))
125589fca16SÁlvaro Fernández Rojas led->cdev.brightness = LED_FULL;
126589fca16SÁlvaro Fernández Rojas else
127589fca16SÁlvaro Fernández Rojas led->cdev.brightness = LED_OFF;
128*e41ff817SAndy Shevchenko break;
129*e41ff817SAndy Shevchenko default:
130589fca16SÁlvaro Fernández Rojas led->cdev.brightness = LED_OFF;
131589fca16SÁlvaro Fernández Rojas }
132589fca16SÁlvaro Fernández Rojas
13342273caaSÁlvaro Fernández Rojas bcm6358_led_set(&led->cdev, led->cdev.brightness);
13442273caaSÁlvaro Fernández Rojas
135589fca16SÁlvaro Fernández Rojas led->cdev.brightness_set = bcm6358_led_set;
136589fca16SÁlvaro Fernández Rojas
137e4e912a3SMarek Behún rc = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
138589fca16SÁlvaro Fernández Rojas if (rc < 0)
139589fca16SÁlvaro Fernández Rojas return rc;
140589fca16SÁlvaro Fernández Rojas
141589fca16SÁlvaro Fernández Rojas dev_dbg(dev, "registered LED %s\n", led->cdev.name);
142589fca16SÁlvaro Fernández Rojas
143589fca16SÁlvaro Fernández Rojas return 0;
144589fca16SÁlvaro Fernández Rojas }
145589fca16SÁlvaro Fernández Rojas
bcm6358_leds_probe(struct platform_device * pdev)146589fca16SÁlvaro Fernández Rojas static int bcm6358_leds_probe(struct platform_device *pdev)
147589fca16SÁlvaro Fernández Rojas {
148589fca16SÁlvaro Fernández Rojas struct device *dev = &pdev->dev;
1498853c95eSMarek Behún struct device_node *np = dev_of_node(&pdev->dev);
150589fca16SÁlvaro Fernández Rojas struct device_node *child;
151589fca16SÁlvaro Fernández Rojas void __iomem *mem;
152589fca16SÁlvaro Fernández Rojas spinlock_t *lock; /* memory lock */
153589fca16SÁlvaro Fernández Rojas unsigned long val;
154589fca16SÁlvaro Fernández Rojas u32 clk_div;
155589fca16SÁlvaro Fernández Rojas
1568b4423d6SMarkus Elfring mem = devm_platform_ioremap_resource(pdev, 0);
157589fca16SÁlvaro Fernández Rojas if (IS_ERR(mem))
158589fca16SÁlvaro Fernández Rojas return PTR_ERR(mem);
159589fca16SÁlvaro Fernández Rojas
160589fca16SÁlvaro Fernández Rojas lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL);
161589fca16SÁlvaro Fernández Rojas if (!lock)
162589fca16SÁlvaro Fernández Rojas return -ENOMEM;
163589fca16SÁlvaro Fernández Rojas
164589fca16SÁlvaro Fernández Rojas spin_lock_init(lock);
165589fca16SÁlvaro Fernández Rojas
166589fca16SÁlvaro Fernández Rojas val = bcm6358_led_busy(mem);
167589fca16SÁlvaro Fernández Rojas val &= ~(BCM6358_SLED_POLARITY | BCM6358_SLED_CLKDIV_MASK);
168589fca16SÁlvaro Fernández Rojas if (of_property_read_bool(np, "brcm,clk-dat-low"))
169589fca16SÁlvaro Fernández Rojas val |= BCM6358_SLED_POLARITY;
170589fca16SÁlvaro Fernández Rojas of_property_read_u32(np, "brcm,clk-div", &clk_div);
171589fca16SÁlvaro Fernández Rojas switch (clk_div) {
172589fca16SÁlvaro Fernández Rojas case 8:
173589fca16SÁlvaro Fernández Rojas val |= BCM6358_SLED_CLKDIV_8;
174589fca16SÁlvaro Fernández Rojas break;
175589fca16SÁlvaro Fernández Rojas case 4:
176589fca16SÁlvaro Fernández Rojas val |= BCM6358_SLED_CLKDIV_4;
177589fca16SÁlvaro Fernández Rojas break;
178589fca16SÁlvaro Fernández Rojas case 2:
179589fca16SÁlvaro Fernández Rojas val |= BCM6358_SLED_CLKDIV_2;
180589fca16SÁlvaro Fernández Rojas break;
181589fca16SÁlvaro Fernández Rojas default:
182589fca16SÁlvaro Fernández Rojas val |= BCM6358_SLED_CLKDIV_1;
183589fca16SÁlvaro Fernández Rojas break;
184589fca16SÁlvaro Fernández Rojas }
185589fca16SÁlvaro Fernández Rojas bcm6358_led_write(mem + BCM6358_REG_CTRL, val);
186589fca16SÁlvaro Fernández Rojas
187589fca16SÁlvaro Fernández Rojas for_each_available_child_of_node(np, child) {
188589fca16SÁlvaro Fernández Rojas int rc;
189589fca16SÁlvaro Fernández Rojas u32 reg;
190589fca16SÁlvaro Fernández Rojas
191589fca16SÁlvaro Fernández Rojas if (of_property_read_u32(child, "reg", ®))
192589fca16SÁlvaro Fernández Rojas continue;
193589fca16SÁlvaro Fernández Rojas
194589fca16SÁlvaro Fernández Rojas if (reg >= BCM6358_SLED_MAX_COUNT) {
195589fca16SÁlvaro Fernández Rojas dev_err(dev, "invalid LED (%u >= %d)\n", reg,
196589fca16SÁlvaro Fernández Rojas BCM6358_SLED_MAX_COUNT);
197589fca16SÁlvaro Fernández Rojas continue;
198589fca16SÁlvaro Fernández Rojas }
199589fca16SÁlvaro Fernández Rojas
200589fca16SÁlvaro Fernández Rojas rc = bcm6358_led(dev, child, reg, mem, lock);
2014b6ba5e2SJulia Lawall if (rc < 0) {
2024b6ba5e2SJulia Lawall of_node_put(child);
203589fca16SÁlvaro Fernández Rojas return rc;
204589fca16SÁlvaro Fernández Rojas }
2054b6ba5e2SJulia Lawall }
206589fca16SÁlvaro Fernández Rojas
207589fca16SÁlvaro Fernández Rojas return 0;
208589fca16SÁlvaro Fernández Rojas }
209589fca16SÁlvaro Fernández Rojas
210589fca16SÁlvaro Fernández Rojas static const struct of_device_id bcm6358_leds_of_match[] = {
211589fca16SÁlvaro Fernández Rojas { .compatible = "brcm,bcm6358-leds", },
212589fca16SÁlvaro Fernández Rojas { },
213589fca16SÁlvaro Fernández Rojas };
21401736f07SLuis de Bethencourt MODULE_DEVICE_TABLE(of, bcm6358_leds_of_match);
215589fca16SÁlvaro Fernández Rojas
216589fca16SÁlvaro Fernández Rojas static struct platform_driver bcm6358_leds_driver = {
217589fca16SÁlvaro Fernández Rojas .probe = bcm6358_leds_probe,
218589fca16SÁlvaro Fernández Rojas .driver = {
219589fca16SÁlvaro Fernández Rojas .name = "leds-bcm6358",
220589fca16SÁlvaro Fernández Rojas .of_match_table = bcm6358_leds_of_match,
221589fca16SÁlvaro Fernández Rojas },
222589fca16SÁlvaro Fernández Rojas };
223589fca16SÁlvaro Fernández Rojas
224589fca16SÁlvaro Fernández Rojas module_platform_driver(bcm6358_leds_driver);
225589fca16SÁlvaro Fernández Rojas
226589fca16SÁlvaro Fernández Rojas MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
227589fca16SÁlvaro Fernández Rojas MODULE_DESCRIPTION("LED driver for BCM6358 controllers");
228589fca16SÁlvaro Fernández Rojas MODULE_LICENSE("GPL v2");
229589fca16SÁlvaro Fernández Rojas MODULE_ALIAS("platform:leds-bcm6358");
230