376584574fcd179ca26d0694d3be59a4da760511
[openwrt/staging/neocturne.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 --- a/drivers/leds/blink/Kconfig
23 +++ b/drivers/leds/blink/Kconfig
24 @@ -1,3 +1,15 @@
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 +
37 config LEDS_LGM
38 tristate "LED support for LGM SoC series"
39 depends on X86 || COMPILE_TEST
40 --- a/drivers/leds/blink/Makefile
41 +++ b/drivers/leds/blink/Makefile
42 @@ -1,2 +1,3 @@
43 # SPDX-License-Identifier: GPL-2.0
44 +obj-$(CONFIG_LEDS_BCM63138) += leds-bcm63138.o
45 obj-$(CONFIG_LEDS_LGM) += leds-lgm-sso.o
46 --- /dev/null
47 +++ b/drivers/leds/blink/leds-bcm63138.c
48 @@ -0,0 +1,308 @@
49 +// SPDX-License-Identifier: GPL-2.0-only
50 +/*
51 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
52 + */
53 +#include <linux/delay.h>
54 +#include <linux/io.h>
55 +#include <linux/leds.h>
56 +#include <linux/module.h>
57 +#include <linux/of.h>
58 +#include <linux/pinctrl/consumer.h>
59 +#include <linux/platform_device.h>
60 +#include <linux/spinlock.h>
61 +
62 +#define BCM63138_MAX_LEDS 32
63 +#define BCM63138_MAX_BRIGHTNESS 9
64 +
65 +#define BCM63138_LED_BITS 4 /* how many bits control a single LED */
66 +#define BCM63138_LED_MASK ((1 << BCM63138_LED_BITS) - 1) /* 0xf */
67 +#define BCM63138_LEDS_PER_REG (32 / BCM63138_LED_BITS) /* 8 */
68 +
69 +#define BCM63138_GLB_CTRL 0x00
70 +#define BCM63138_GLB_CTRL_SERIAL_LED_DATA_PPOL 0x00000002
71 +#define BCM63138_GLB_CTRL_SERIAL_LED_EN_POL 0x00000008
72 +#define BCM63138_MASK 0x04
73 +#define BCM63138_HW_LED_EN 0x08
74 +#define BCM63138_SERIAL_LED_SHIFT_SEL 0x0c
75 +#define BCM63138_FLASH_RATE_CTRL1 0x10
76 +#define BCM63138_FLASH_RATE_CTRL2 0x14
77 +#define BCM63138_FLASH_RATE_CTRL3 0x18
78 +#define BCM63138_FLASH_RATE_CTRL4 0x1c
79 +#define BCM63138_BRIGHT_CTRL1 0x20
80 +#define BCM63138_BRIGHT_CTRL2 0x24
81 +#define BCM63138_BRIGHT_CTRL3 0x28
82 +#define BCM63138_BRIGHT_CTRL4 0x2c
83 +#define BCM63138_POWER_LED_CFG 0x30
84 +#define BCM63138_HW_POLARITY 0xb4
85 +#define BCM63138_SW_DATA 0xb8
86 +#define BCM63138_SW_POLARITY 0xbc
87 +#define BCM63138_PARALLEL_LED_POLARITY 0xc0
88 +#define BCM63138_SERIAL_LED_POLARITY 0xc4
89 +#define BCM63138_HW_LED_STATUS 0xc8
90 +#define BCM63138_FLASH_CTRL_STATUS 0xcc
91 +#define BCM63138_FLASH_BRT_CTRL 0xd0
92 +#define BCM63138_FLASH_P_LED_OUT_STATUS 0xd4
93 +#define BCM63138_FLASH_S_LED_OUT_STATUS 0xd8
94 +
95 +struct bcm63138_leds {
96 + struct device *dev;
97 + void __iomem *base;
98 + spinlock_t lock;
99 +};
100 +
101 +struct bcm63138_led {
102 + struct bcm63138_leds *leds;
103 + struct led_classdev cdev;
104 + u32 pin;
105 + bool active_low;
106 +};
107 +
108 +/*
109 + * I/O access
110 + */
111 +
112 +static void bcm63138_leds_write(struct bcm63138_leds *leds, unsigned int reg,
113 + u32 data)
114 +{
115 + writel(data, leds->base + reg);
116 +}
117 +
118 +static unsigned long bcm63138_leds_read(struct bcm63138_leds *leds,
119 + unsigned int reg)
120 +{
121 + return readl(leds->base + reg);
122 +}
123 +
124 +static void bcm63138_leds_update_bits(struct bcm63138_leds *leds,
125 + unsigned int reg, u32 mask, u32 val)
126 +{
127 + WARN_ON(val & ~mask);
128 +
129 + bcm63138_leds_write(leds, reg, (bcm63138_leds_read(leds, reg) & ~mask) | (val & mask));
130 +}
131 +
132 +/*
133 + * Helpers
134 + */
135 +
136 +static void bcm63138_leds_set_flash_rate(struct bcm63138_leds *leds,
137 + struct bcm63138_led *led,
138 + u8 value)
139 +{
140 + int reg_offset = (led->pin >> fls((BCM63138_LEDS_PER_REG - 1))) * 4;
141 + int shift = (led->pin & (BCM63138_LEDS_PER_REG - 1)) * BCM63138_LED_BITS;
142 +
143 + bcm63138_leds_update_bits(leds, BCM63138_FLASH_RATE_CTRL1 + reg_offset,
144 + BCM63138_LED_MASK << shift, value << shift);
145 +}
146 +
147 +static void bcm63138_leds_set_bright(struct bcm63138_leds *leds,
148 + struct bcm63138_led *led,
149 + u8 value)
150 +{
151 + int reg_offset = (led->pin >> fls((BCM63138_LEDS_PER_REG - 1))) * 4;
152 + int shift = (led->pin & (BCM63138_LEDS_PER_REG - 1)) * BCM63138_LED_BITS;
153 +
154 + bcm63138_leds_update_bits(leds, BCM63138_BRIGHT_CTRL1 + reg_offset,
155 + BCM63138_LED_MASK << shift, value << shift);
156 +}
157 +
158 +static void bcm63138_leds_enable_led(struct bcm63138_leds *leds,
159 + struct bcm63138_led *led,
160 + enum led_brightness value)
161 +{
162 + u32 bit = BIT(led->pin);
163 +
164 + bcm63138_leds_update_bits(leds, BCM63138_SW_DATA, bit,
165 + value == LED_OFF ? 0 : bit);
166 +}
167 +
168 +/*
169 + * API callbacks
170 + */
171 +
172 +static void bcm63138_leds_brightness_set(struct led_classdev *led_cdev,
173 + enum led_brightness value)
174 +{
175 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
176 + struct bcm63138_leds *leds = led->leds;
177 + unsigned long flags;
178 +
179 + spin_lock_irqsave(&leds->lock, flags);
180 +
181 + bcm63138_leds_enable_led(leds, led, value);
182 + if (!value)
183 + bcm63138_leds_set_flash_rate(leds, led, 0);
184 + else
185 + bcm63138_leds_set_bright(leds, led, value);
186 +
187 + spin_unlock_irqrestore(&leds->lock, flags);
188 +}
189 +
190 +static int bcm63138_leds_blink_set(struct led_classdev *led_cdev,
191 + unsigned long *delay_on,
192 + unsigned long *delay_off)
193 +{
194 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
195 + struct bcm63138_leds *leds = led->leds;
196 + unsigned long flags;
197 + u8 value;
198 +
199 + if (!*delay_on && !*delay_off) {
200 + *delay_on = 640;
201 + *delay_off = 640;
202 + }
203 +
204 + if (*delay_on != *delay_off) {
205 + dev_dbg(led_cdev->dev, "Blinking at unequal delays is not supported\n");
206 + return -EINVAL;
207 + }
208 +
209 + switch (*delay_on) {
210 + case 1152 ... 1408: /* 1280 ms ± 10% */
211 + value = 0x7;
212 + break;
213 + case 576 ... 704: /* 640 ms ± 10% */
214 + value = 0x6;
215 + break;
216 + case 288 ... 352: /* 320 ms ± 10% */
217 + value = 0x5;
218 + break;
219 + case 126 ... 154: /* 140 ms ± 10% */
220 + value = 0x4;
221 + break;
222 + case 59 ... 72: /* 65 ms ± 10% */
223 + value = 0x3;
224 + break;
225 + default:
226 + dev_dbg(led_cdev->dev, "Blinking delay value %lu is unsupported\n",
227 + *delay_on);
228 + return -EINVAL;
229 + }
230 +
231 + spin_lock_irqsave(&leds->lock, flags);
232 +
233 + bcm63138_leds_enable_led(leds, led, BCM63138_MAX_BRIGHTNESS);
234 + bcm63138_leds_set_flash_rate(leds, led, value);
235 +
236 + spin_unlock_irqrestore(&leds->lock, flags);
237 +
238 + return 0;
239 +}
240 +
241 +/*
242 + * LED driver
243 + */
244 +
245 +static void bcm63138_leds_create_led(struct bcm63138_leds *leds,
246 + struct device_node *np)
247 +{
248 + struct led_init_data init_data = {
249 + .fwnode = of_fwnode_handle(np),
250 + };
251 + struct device *dev = leds->dev;
252 + struct bcm63138_led *led;
253 + struct pinctrl *pinctrl;
254 + u32 bit;
255 + int err;
256 +
257 + led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
258 + if (!led) {
259 + dev_err(dev, "Failed to alloc LED\n");
260 + return;
261 + }
262 +
263 + led->leds = leds;
264 +
265 + if (of_property_read_u32(np, "reg", &led->pin)) {
266 + dev_err(dev, "Missing \"reg\" property in %pOF\n", np);
267 + goto err_free;
268 + }
269 +
270 + if (led->pin >= BCM63138_MAX_LEDS) {
271 + dev_err(dev, "Invalid \"reg\" value %d\n", led->pin);
272 + goto err_free;
273 + }
274 +
275 + led->active_low = of_property_read_bool(np, "active-low");
276 +
277 + led->cdev.max_brightness = BCM63138_MAX_BRIGHTNESS;
278 + led->cdev.brightness_set = bcm63138_leds_brightness_set;
279 + led->cdev.blink_set = bcm63138_leds_blink_set;
280 +
281 + err = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
282 + if (err) {
283 + dev_err(dev, "Failed to register LED %pOF: %d\n", np, err);
284 + goto err_free;
285 + }
286 +
287 + pinctrl = devm_pinctrl_get_select_default(led->cdev.dev);
288 + if (IS_ERR(pinctrl) && PTR_ERR(pinctrl) != -ENODEV) {
289 + dev_warn(led->cdev.dev, "Failed to select %pOF pinctrl: %ld\n",
290 + np, PTR_ERR(pinctrl));
291 + }
292 +
293 + bit = BIT(led->pin);
294 + bcm63138_leds_update_bits(leds, BCM63138_PARALLEL_LED_POLARITY, bit,
295 + led->active_low ? 0 : bit);
296 + bcm63138_leds_update_bits(leds, BCM63138_HW_LED_EN, bit, 0);
297 + bcm63138_leds_set_flash_rate(leds, led, 0);
298 + bcm63138_leds_enable_led(leds, led, led->cdev.brightness);
299 +
300 + return;
301 +
302 +err_free:
303 + devm_kfree(dev, led);
304 +}
305 +
306 +static int bcm63138_leds_probe(struct platform_device *pdev)
307 +{
308 + struct device_node *np = dev_of_node(&pdev->dev);
309 + struct device *dev = &pdev->dev;
310 + struct bcm63138_leds *leds;
311 + struct device_node *child;
312 +
313 + leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
314 + if (!leds)
315 + return -ENOMEM;
316 +
317 + leds->dev = dev;
318 +
319 + leds->base = devm_platform_ioremap_resource(pdev, 0);
320 + if (IS_ERR(leds->base))
321 + return PTR_ERR(leds->base);
322 +
323 + spin_lock_init(&leds->lock);
324 +
325 + bcm63138_leds_write(leds, BCM63138_GLB_CTRL,
326 + BCM63138_GLB_CTRL_SERIAL_LED_DATA_PPOL |
327 + BCM63138_GLB_CTRL_SERIAL_LED_EN_POL);
328 + bcm63138_leds_write(leds, BCM63138_HW_LED_EN, 0);
329 + bcm63138_leds_write(leds, BCM63138_SERIAL_LED_POLARITY, 0);
330 + bcm63138_leds_write(leds, BCM63138_PARALLEL_LED_POLARITY, 0);
331 +
332 + for_each_available_child_of_node(np, child) {
333 + bcm63138_leds_create_led(leds, child);
334 + }
335 +
336 + return 0;
337 +}
338 +
339 +static const struct of_device_id bcm63138_leds_of_match_table[] = {
340 + { .compatible = "brcm,bcm63138-leds", },
341 + { },
342 +};
343 +
344 +static struct platform_driver bcm63138_leds_driver = {
345 + .probe = bcm63138_leds_probe,
346 + .driver = {
347 + .name = "leds-bcm63xxx",
348 + .of_match_table = bcm63138_leds_of_match_table,
349 + },
350 +};
351 +
352 +module_platform_driver(bcm63138_leds_driver);
353 +
354 +MODULE_AUTHOR("Rafał Miłecki");
355 +MODULE_LICENSE("GPL");
356 +MODULE_DEVICE_TABLE(of, bcm63138_leds_of_match_table);