8ebe8f180b3a069f6442fbc531035a5c3f7aa71b
[openwrt/staging/lynxis.git] /
1 From a0ba692072d89075d0a75c7ad9df31f2c1ee9a1c Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Mon, 27 Dec 2021 15:59:05 +0100
4 Subject: [PATCH] leds: bcm63138: add support for BCM63138 controller
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 It's a new controller first introduced in BCM63138 SoC. Later it was
10 also used in BCM4908, some BCM68xx and some BCM63xxx SoCs.
11
12 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
13 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
14 Signed-off-by: Pavel Machek <pavel@ucw.cz>
15 ---
16 drivers/leds/blink/Kconfig | 12 ++
17 drivers/leds/blink/Makefile | 1 +
18 drivers/leds/blink/leds-bcm63138.c | 308 +++++++++++++++++++++++++++++
19 3 files changed, 321 insertions(+)
20 create mode 100644 drivers/leds/blink/leds-bcm63138.c
21
22 --- /dev/null
23 +++ b/drivers/leds/blink/Kconfig
24 @@ -0,0 +1,11 @@
25 +config LEDS_BCM63138
26 + tristate "LED Support for Broadcom BCM63138 SoC"
27 + depends on LEDS_CLASS
28 + depends on ARCH_BCM4908 || ARCH_BCM_5301X || BCM63XX || COMPILE_TEST
29 + depends on HAS_IOMEM
30 + depends on OF
31 + default ARCH_BCM4908
32 + help
33 + This option enables support for LED controller that is part of
34 + BCM63138 SoC. The same hardware block is known to be also used
35 + in BCM4908, BCM6848, BCM6858, BCM63148, BCM63381 and BCM68360.
36 --- /dev/null
37 +++ b/drivers/leds/blink/Makefile
38 @@ -0,0 +1,2 @@
39 +# SPDX-License-Identifier: GPL-2.0
40 +obj-$(CONFIG_LEDS_BCM63138) += leds-bcm63138.o
41 --- /dev/null
42 +++ b/drivers/leds/blink/leds-bcm63138.c
43 @@ -0,0 +1,308 @@
44 +// SPDX-License-Identifier: GPL-2.0-only
45 +/*
46 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
47 + */
48 +#include <linux/delay.h>
49 +#include <linux/io.h>
50 +#include <linux/leds.h>
51 +#include <linux/module.h>
52 +#include <linux/of.h>
53 +#include <linux/pinctrl/consumer.h>
54 +#include <linux/platform_device.h>
55 +#include <linux/spinlock.h>
56 +
57 +#define BCM63138_MAX_LEDS 32
58 +#define BCM63138_MAX_BRIGHTNESS 9
59 +
60 +#define BCM63138_LED_BITS 4 /* how many bits control a single LED */
61 +#define BCM63138_LED_MASK ((1 << BCM63138_LED_BITS) - 1) /* 0xf */
62 +#define BCM63138_LEDS_PER_REG (32 / BCM63138_LED_BITS) /* 8 */
63 +
64 +#define BCM63138_GLB_CTRL 0x00
65 +#define BCM63138_GLB_CTRL_SERIAL_LED_DATA_PPOL 0x00000002
66 +#define BCM63138_GLB_CTRL_SERIAL_LED_EN_POL 0x00000008
67 +#define BCM63138_MASK 0x04
68 +#define BCM63138_HW_LED_EN 0x08
69 +#define BCM63138_SERIAL_LED_SHIFT_SEL 0x0c
70 +#define BCM63138_FLASH_RATE_CTRL1 0x10
71 +#define BCM63138_FLASH_RATE_CTRL2 0x14
72 +#define BCM63138_FLASH_RATE_CTRL3 0x18
73 +#define BCM63138_FLASH_RATE_CTRL4 0x1c
74 +#define BCM63138_BRIGHT_CTRL1 0x20
75 +#define BCM63138_BRIGHT_CTRL2 0x24
76 +#define BCM63138_BRIGHT_CTRL3 0x28
77 +#define BCM63138_BRIGHT_CTRL4 0x2c
78 +#define BCM63138_POWER_LED_CFG 0x30
79 +#define BCM63138_HW_POLARITY 0xb4
80 +#define BCM63138_SW_DATA 0xb8
81 +#define BCM63138_SW_POLARITY 0xbc
82 +#define BCM63138_PARALLEL_LED_POLARITY 0xc0
83 +#define BCM63138_SERIAL_LED_POLARITY 0xc4
84 +#define BCM63138_HW_LED_STATUS 0xc8
85 +#define BCM63138_FLASH_CTRL_STATUS 0xcc
86 +#define BCM63138_FLASH_BRT_CTRL 0xd0
87 +#define BCM63138_FLASH_P_LED_OUT_STATUS 0xd4
88 +#define BCM63138_FLASH_S_LED_OUT_STATUS 0xd8
89 +
90 +struct bcm63138_leds {
91 + struct device *dev;
92 + void __iomem *base;
93 + spinlock_t lock;
94 +};
95 +
96 +struct bcm63138_led {
97 + struct bcm63138_leds *leds;
98 + struct led_classdev cdev;
99 + u32 pin;
100 + bool active_low;
101 +};
102 +
103 +/*
104 + * I/O access
105 + */
106 +
107 +static void bcm63138_leds_write(struct bcm63138_leds *leds, unsigned int reg,
108 + u32 data)
109 +{
110 + writel(data, leds->base + reg);
111 +}
112 +
113 +static unsigned long bcm63138_leds_read(struct bcm63138_leds *leds,
114 + unsigned int reg)
115 +{
116 + return readl(leds->base + reg);
117 +}
118 +
119 +static void bcm63138_leds_update_bits(struct bcm63138_leds *leds,
120 + unsigned int reg, u32 mask, u32 val)
121 +{
122 + WARN_ON(val & ~mask);
123 +
124 + bcm63138_leds_write(leds, reg, (bcm63138_leds_read(leds, reg) & ~mask) | (val & mask));
125 +}
126 +
127 +/*
128 + * Helpers
129 + */
130 +
131 +static void bcm63138_leds_set_flash_rate(struct bcm63138_leds *leds,
132 + struct bcm63138_led *led,
133 + u8 value)
134 +{
135 + int reg_offset = (led->pin >> fls((BCM63138_LEDS_PER_REG - 1))) * 4;
136 + int shift = (led->pin & (BCM63138_LEDS_PER_REG - 1)) * BCM63138_LED_BITS;
137 +
138 + bcm63138_leds_update_bits(leds, BCM63138_FLASH_RATE_CTRL1 + reg_offset,
139 + BCM63138_LED_MASK << shift, value << shift);
140 +}
141 +
142 +static void bcm63138_leds_set_bright(struct bcm63138_leds *leds,
143 + struct bcm63138_led *led,
144 + u8 value)
145 +{
146 + int reg_offset = (led->pin >> fls((BCM63138_LEDS_PER_REG - 1))) * 4;
147 + int shift = (led->pin & (BCM63138_LEDS_PER_REG - 1)) * BCM63138_LED_BITS;
148 +
149 + bcm63138_leds_update_bits(leds, BCM63138_BRIGHT_CTRL1 + reg_offset,
150 + BCM63138_LED_MASK << shift, value << shift);
151 +}
152 +
153 +static void bcm63138_leds_enable_led(struct bcm63138_leds *leds,
154 + struct bcm63138_led *led,
155 + enum led_brightness value)
156 +{
157 + u32 bit = BIT(led->pin);
158 +
159 + bcm63138_leds_update_bits(leds, BCM63138_SW_DATA, bit,
160 + value == LED_OFF ? 0 : bit);
161 +}
162 +
163 +/*
164 + * API callbacks
165 + */
166 +
167 +static void bcm63138_leds_brightness_set(struct led_classdev *led_cdev,
168 + enum led_brightness value)
169 +{
170 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
171 + struct bcm63138_leds *leds = led->leds;
172 + unsigned long flags;
173 +
174 + spin_lock_irqsave(&leds->lock, flags);
175 +
176 + bcm63138_leds_enable_led(leds, led, value);
177 + if (!value)
178 + bcm63138_leds_set_flash_rate(leds, led, 0);
179 + else
180 + bcm63138_leds_set_bright(leds, led, value);
181 +
182 + spin_unlock_irqrestore(&leds->lock, flags);
183 +}
184 +
185 +static int bcm63138_leds_blink_set(struct led_classdev *led_cdev,
186 + unsigned long *delay_on,
187 + unsigned long *delay_off)
188 +{
189 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
190 + struct bcm63138_leds *leds = led->leds;
191 + unsigned long flags;
192 + u8 value;
193 +
194 + if (!*delay_on && !*delay_off) {
195 + *delay_on = 640;
196 + *delay_off = 640;
197 + }
198 +
199 + if (*delay_on != *delay_off) {
200 + dev_dbg(led_cdev->dev, "Blinking at unequal delays is not supported\n");
201 + return -EINVAL;
202 + }
203 +
204 + switch (*delay_on) {
205 + case 1152 ... 1408: /* 1280 ms ± 10% */
206 + value = 0x7;
207 + break;
208 + case 576 ... 704: /* 640 ms ± 10% */
209 + value = 0x6;
210 + break;
211 + case 288 ... 352: /* 320 ms ± 10% */
212 + value = 0x5;
213 + break;
214 + case 126 ... 154: /* 140 ms ± 10% */
215 + value = 0x4;
216 + break;
217 + case 59 ... 72: /* 65 ms ± 10% */
218 + value = 0x3;
219 + break;
220 + default:
221 + dev_dbg(led_cdev->dev, "Blinking delay value %lu is unsupported\n",
222 + *delay_on);
223 + return -EINVAL;
224 + }
225 +
226 + spin_lock_irqsave(&leds->lock, flags);
227 +
228 + bcm63138_leds_enable_led(leds, led, BCM63138_MAX_BRIGHTNESS);
229 + bcm63138_leds_set_flash_rate(leds, led, value);
230 +
231 + spin_unlock_irqrestore(&leds->lock, flags);
232 +
233 + return 0;
234 +}
235 +
236 +/*
237 + * LED driver
238 + */
239 +
240 +static void bcm63138_leds_create_led(struct bcm63138_leds *leds,
241 + struct device_node *np)
242 +{
243 + struct led_init_data init_data = {
244 + .fwnode = of_fwnode_handle(np),
245 + };
246 + struct device *dev = leds->dev;
247 + struct bcm63138_led *led;
248 + struct pinctrl *pinctrl;
249 + u32 bit;
250 + int err;
251 +
252 + led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
253 + if (!led) {
254 + dev_err(dev, "Failed to alloc LED\n");
255 + return;
256 + }
257 +
258 + led->leds = leds;
259 +
260 + if (of_property_read_u32(np, "reg", &led->pin)) {
261 + dev_err(dev, "Missing \"reg\" property in %pOF\n", np);
262 + goto err_free;
263 + }
264 +
265 + if (led->pin >= BCM63138_MAX_LEDS) {
266 + dev_err(dev, "Invalid \"reg\" value %d\n", led->pin);
267 + goto err_free;
268 + }
269 +
270 + led->active_low = of_property_read_bool(np, "active-low");
271 +
272 + led->cdev.max_brightness = BCM63138_MAX_BRIGHTNESS;
273 + led->cdev.brightness_set = bcm63138_leds_brightness_set;
274 + led->cdev.blink_set = bcm63138_leds_blink_set;
275 +
276 + err = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
277 + if (err) {
278 + dev_err(dev, "Failed to register LED %pOF: %d\n", np, err);
279 + goto err_free;
280 + }
281 +
282 + pinctrl = devm_pinctrl_get_select_default(led->cdev.dev);
283 + if (IS_ERR(pinctrl) && PTR_ERR(pinctrl) != -ENODEV) {
284 + dev_warn(led->cdev.dev, "Failed to select %pOF pinctrl: %ld\n",
285 + np, PTR_ERR(pinctrl));
286 + }
287 +
288 + bit = BIT(led->pin);
289 + bcm63138_leds_update_bits(leds, BCM63138_PARALLEL_LED_POLARITY, bit,
290 + led->active_low ? 0 : bit);
291 + bcm63138_leds_update_bits(leds, BCM63138_HW_LED_EN, bit, 0);
292 + bcm63138_leds_set_flash_rate(leds, led, 0);
293 + bcm63138_leds_enable_led(leds, led, led->cdev.brightness);
294 +
295 + return;
296 +
297 +err_free:
298 + devm_kfree(dev, led);
299 +}
300 +
301 +static int bcm63138_leds_probe(struct platform_device *pdev)
302 +{
303 + struct device_node *np = dev_of_node(&pdev->dev);
304 + struct device *dev = &pdev->dev;
305 + struct bcm63138_leds *leds;
306 + struct device_node *child;
307 +
308 + leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
309 + if (!leds)
310 + return -ENOMEM;
311 +
312 + leds->dev = dev;
313 +
314 + leds->base = devm_platform_ioremap_resource(pdev, 0);
315 + if (IS_ERR(leds->base))
316 + return PTR_ERR(leds->base);
317 +
318 + spin_lock_init(&leds->lock);
319 +
320 + bcm63138_leds_write(leds, BCM63138_GLB_CTRL,
321 + BCM63138_GLB_CTRL_SERIAL_LED_DATA_PPOL |
322 + BCM63138_GLB_CTRL_SERIAL_LED_EN_POL);
323 + bcm63138_leds_write(leds, BCM63138_HW_LED_EN, 0);
324 + bcm63138_leds_write(leds, BCM63138_SERIAL_LED_POLARITY, 0);
325 + bcm63138_leds_write(leds, BCM63138_PARALLEL_LED_POLARITY, 0);
326 +
327 + for_each_available_child_of_node(np, child) {
328 + bcm63138_leds_create_led(leds, child);
329 + }
330 +
331 + return 0;
332 +}
333 +
334 +static const struct of_device_id bcm63138_leds_of_match_table[] = {
335 + { .compatible = "brcm,bcm63138-leds", },
336 + { },
337 +};
338 +
339 +static struct platform_driver bcm63138_leds_driver = {
340 + .probe = bcm63138_leds_probe,
341 + .driver = {
342 + .name = "leds-bcm63xxx",
343 + .of_match_table = bcm63138_leds_of_match_table,
344 + },
345 +};
346 +
347 +module_platform_driver(bcm63138_leds_driver);
348 +
349 +MODULE_AUTHOR("Rafał Miłecki");
350 +MODULE_LICENSE("GPL");
351 +MODULE_DEVICE_TABLE(of, bcm63138_leds_of_match_table);
352 --- a/drivers/leds/Kconfig
353 +++ b/drivers/leds/Kconfig
354 @@ -929,6 +929,8 @@ config LEDS_ACER_A500
355 This option enables support for the Power Button LED of
356 Acer Iconia Tab A500.
357
358 +source "drivers/leds/blink/Kconfig"
359 +
360 comment "LED Triggers"
361 source "drivers/leds/trigger/Kconfig"
362
363 --- a/drivers/leds/Makefile
364 +++ b/drivers/leds/Makefile
365 @@ -105,3 +105,6 @@ obj-$(CONFIG_LEDS_USER) += uleds.o
366
367 # LED Triggers
368 obj-$(CONFIG_LEDS_TRIGGERS) += trigger/
369 +
370 +# LED Blink
371 +obj-y += blink/