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
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
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.
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>
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
23 +++ b/drivers/leds/blink/Kconfig
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
31 + default ARCH_BCM4908
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.
37 +++ b/drivers/leds/blink/Makefile
39 +# SPDX-License-Identifier: GPL-2.0
40 +obj-$(CONFIG_LEDS_BCM63138) += leds-bcm63138.o
42 +++ b/drivers/leds/blink/leds-bcm63138.c
44 +// SPDX-License-Identifier: GPL-2.0-only
46 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
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>
57 +#define BCM63138_MAX_LEDS 32
58 +#define BCM63138_MAX_BRIGHTNESS 9
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 */
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
90 +struct bcm63138_leds {
96 +struct bcm63138_led {
97 + struct bcm63138_leds *leds;
98 + struct led_classdev cdev;
107 +static void bcm63138_leds_write(struct bcm63138_leds *leds, unsigned int reg,
110 + writel(data, leds->base + reg);
113 +static unsigned long bcm63138_leds_read(struct bcm63138_leds *leds,
116 + return readl(leds->base + reg);
119 +static void bcm63138_leds_update_bits(struct bcm63138_leds *leds,
120 + unsigned int reg, u32 mask, u32 val)
122 + WARN_ON(val & ~mask);
124 + bcm63138_leds_write(leds, reg, (bcm63138_leds_read(leds, reg) & ~mask) | (val & mask));
131 +static void bcm63138_leds_set_flash_rate(struct bcm63138_leds *leds,
132 + struct bcm63138_led *led,
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;
138 + bcm63138_leds_update_bits(leds, BCM63138_FLASH_RATE_CTRL1 + reg_offset,
139 + BCM63138_LED_MASK << shift, value << shift);
142 +static void bcm63138_leds_set_bright(struct bcm63138_leds *leds,
143 + struct bcm63138_led *led,
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;
149 + bcm63138_leds_update_bits(leds, BCM63138_BRIGHT_CTRL1 + reg_offset,
150 + BCM63138_LED_MASK << shift, value << shift);
153 +static void bcm63138_leds_enable_led(struct bcm63138_leds *leds,
154 + struct bcm63138_led *led,
155 + enum led_brightness value)
157 + u32 bit = BIT(led->pin);
159 + bcm63138_leds_update_bits(leds, BCM63138_SW_DATA, bit,
160 + value == LED_OFF ? 0 : bit);
167 +static void bcm63138_leds_brightness_set(struct led_classdev *led_cdev,
168 + enum led_brightness value)
170 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
171 + struct bcm63138_leds *leds = led->leds;
172 + unsigned long flags;
174 + spin_lock_irqsave(&leds->lock, flags);
176 + bcm63138_leds_enable_led(leds, led, value);
178 + bcm63138_leds_set_flash_rate(leds, led, 0);
180 + bcm63138_leds_set_bright(leds, led, value);
182 + spin_unlock_irqrestore(&leds->lock, flags);
185 +static int bcm63138_leds_blink_set(struct led_classdev *led_cdev,
186 + unsigned long *delay_on,
187 + unsigned long *delay_off)
189 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
190 + struct bcm63138_leds *leds = led->leds;
191 + unsigned long flags;
194 + if (!*delay_on && !*delay_off) {
199 + if (*delay_on != *delay_off) {
200 + dev_dbg(led_cdev->dev, "Blinking at unequal delays is not supported\n");
204 + switch (*delay_on) {
205 + case 1152 ... 1408: /* 1280 ms ± 10% */
208 + case 576 ... 704: /* 640 ms ± 10% */
211 + case 288 ... 352: /* 320 ms ± 10% */
214 + case 126 ... 154: /* 140 ms ± 10% */
217 + case 59 ... 72: /* 65 ms ± 10% */
221 + dev_dbg(led_cdev->dev, "Blinking delay value %lu is unsupported\n",
226 + spin_lock_irqsave(&leds->lock, flags);
228 + bcm63138_leds_enable_led(leds, led, BCM63138_MAX_BRIGHTNESS);
229 + bcm63138_leds_set_flash_rate(leds, led, value);
231 + spin_unlock_irqrestore(&leds->lock, flags);
240 +static void bcm63138_leds_create_led(struct bcm63138_leds *leds,
241 + struct device_node *np)
243 + struct led_init_data init_data = {
244 + .fwnode = of_fwnode_handle(np),
246 + struct device *dev = leds->dev;
247 + struct bcm63138_led *led;
248 + struct pinctrl *pinctrl;
252 + led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
254 + dev_err(dev, "Failed to alloc LED\n");
260 + if (of_property_read_u32(np, "reg", &led->pin)) {
261 + dev_err(dev, "Missing \"reg\" property in %pOF\n", np);
265 + if (led->pin >= BCM63138_MAX_LEDS) {
266 + dev_err(dev, "Invalid \"reg\" value %d\n", led->pin);
270 + led->active_low = of_property_read_bool(np, "active-low");
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;
276 + err = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
278 + dev_err(dev, "Failed to register LED %pOF: %d\n", np, err);
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));
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);
298 + devm_kfree(dev, led);
301 +static int bcm63138_leds_probe(struct platform_device *pdev)
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;
308 + leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
314 + leds->base = devm_platform_ioremap_resource(pdev, 0);
315 + if (IS_ERR(leds->base))
316 + return PTR_ERR(leds->base);
318 + spin_lock_init(&leds->lock);
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);
327 + for_each_available_child_of_node(np, child) {
328 + bcm63138_leds_create_led(leds, child);
334 +static const struct of_device_id bcm63138_leds_of_match_table[] = {
335 + { .compatible = "brcm,bcm63138-leds", },
339 +static struct platform_driver bcm63138_leds_driver = {
340 + .probe = bcm63138_leds_probe,
342 + .name = "leds-bcm63xxx",
343 + .of_match_table = bcm63138_leds_of_match_table,
347 +module_platform_driver(bcm63138_leds_driver);
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.
358 +source "drivers/leds/blink/Kconfig"
360 comment "LED Triggers"
361 source "drivers/leds/trigger/Kconfig"
363 --- a/drivers/leds/Makefile
364 +++ b/drivers/leds/Makefile
365 @@ -105,3 +105,6 @@ obj-$(CONFIG_LEDS_USER) += uleds.o
368 obj-$(CONFIG_LEDS_TRIGGERS) += trigger/