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
22 --- a/drivers/leds/blink/Kconfig
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.
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
43 # SPDX-License-Identifier: GPL-2.0
44 +obj-$(CONFIG_LEDS_BCM63138) += leds-bcm63138.o
45 obj-$(CONFIG_LEDS_LGM) += leds-lgm-sso.o
47 +++ b/drivers/leds/blink/leds-bcm63138.c
49 +// SPDX-License-Identifier: GPL-2.0-only
51 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
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>
62 +#define BCM63138_MAX_LEDS 32
63 +#define BCM63138_MAX_BRIGHTNESS 9
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 */
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
95 +struct bcm63138_leds {
101 +struct bcm63138_led {
102 + struct bcm63138_leds *leds;
103 + struct led_classdev cdev;
112 +static void bcm63138_leds_write(struct bcm63138_leds *leds, unsigned int reg,
115 + writel(data, leds->base + reg);
118 +static unsigned long bcm63138_leds_read(struct bcm63138_leds *leds,
121 + return readl(leds->base + reg);
124 +static void bcm63138_leds_update_bits(struct bcm63138_leds *leds,
125 + unsigned int reg, u32 mask, u32 val)
127 + WARN_ON(val & ~mask);
129 + bcm63138_leds_write(leds, reg, (bcm63138_leds_read(leds, reg) & ~mask) | (val & mask));
136 +static void bcm63138_leds_set_flash_rate(struct bcm63138_leds *leds,
137 + struct bcm63138_led *led,
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;
143 + bcm63138_leds_update_bits(leds, BCM63138_FLASH_RATE_CTRL1 + reg_offset,
144 + BCM63138_LED_MASK << shift, value << shift);
147 +static void bcm63138_leds_set_bright(struct bcm63138_leds *leds,
148 + struct bcm63138_led *led,
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;
154 + bcm63138_leds_update_bits(leds, BCM63138_BRIGHT_CTRL1 + reg_offset,
155 + BCM63138_LED_MASK << shift, value << shift);
158 +static void bcm63138_leds_enable_led(struct bcm63138_leds *leds,
159 + struct bcm63138_led *led,
160 + enum led_brightness value)
162 + u32 bit = BIT(led->pin);
164 + bcm63138_leds_update_bits(leds, BCM63138_SW_DATA, bit,
165 + value == LED_OFF ? 0 : bit);
172 +static void bcm63138_leds_brightness_set(struct led_classdev *led_cdev,
173 + enum led_brightness value)
175 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
176 + struct bcm63138_leds *leds = led->leds;
177 + unsigned long flags;
179 + spin_lock_irqsave(&leds->lock, flags);
181 + bcm63138_leds_enable_led(leds, led, value);
183 + bcm63138_leds_set_flash_rate(leds, led, 0);
185 + bcm63138_leds_set_bright(leds, led, value);
187 + spin_unlock_irqrestore(&leds->lock, flags);
190 +static int bcm63138_leds_blink_set(struct led_classdev *led_cdev,
191 + unsigned long *delay_on,
192 + unsigned long *delay_off)
194 + struct bcm63138_led *led = container_of(led_cdev, struct bcm63138_led, cdev);
195 + struct bcm63138_leds *leds = led->leds;
196 + unsigned long flags;
199 + if (!*delay_on && !*delay_off) {
204 + if (*delay_on != *delay_off) {
205 + dev_dbg(led_cdev->dev, "Blinking at unequal delays is not supported\n");
209 + switch (*delay_on) {
210 + case 1152 ... 1408: /* 1280 ms ± 10% */
213 + case 576 ... 704: /* 640 ms ± 10% */
216 + case 288 ... 352: /* 320 ms ± 10% */
219 + case 126 ... 154: /* 140 ms ± 10% */
222 + case 59 ... 72: /* 65 ms ± 10% */
226 + dev_dbg(led_cdev->dev, "Blinking delay value %lu is unsupported\n",
231 + spin_lock_irqsave(&leds->lock, flags);
233 + bcm63138_leds_enable_led(leds, led, BCM63138_MAX_BRIGHTNESS);
234 + bcm63138_leds_set_flash_rate(leds, led, value);
236 + spin_unlock_irqrestore(&leds->lock, flags);
245 +static void bcm63138_leds_create_led(struct bcm63138_leds *leds,
246 + struct device_node *np)
248 + struct led_init_data init_data = {
249 + .fwnode = of_fwnode_handle(np),
251 + struct device *dev = leds->dev;
252 + struct bcm63138_led *led;
253 + struct pinctrl *pinctrl;
257 + led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
259 + dev_err(dev, "Failed to alloc LED\n");
265 + if (of_property_read_u32(np, "reg", &led->pin)) {
266 + dev_err(dev, "Missing \"reg\" property in %pOF\n", np);
270 + if (led->pin >= BCM63138_MAX_LEDS) {
271 + dev_err(dev, "Invalid \"reg\" value %d\n", led->pin);
275 + led->active_low = of_property_read_bool(np, "active-low");
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;
281 + err = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
283 + dev_err(dev, "Failed to register LED %pOF: %d\n", np, err);
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));
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);
303 + devm_kfree(dev, led);
306 +static int bcm63138_leds_probe(struct platform_device *pdev)
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;
313 + leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
319 + leds->base = devm_platform_ioremap_resource(pdev, 0);
320 + if (IS_ERR(leds->base))
321 + return PTR_ERR(leds->base);
323 + spin_lock_init(&leds->lock);
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);
332 + for_each_available_child_of_node(np, child) {
333 + bcm63138_leds_create_led(leds, child);
339 +static const struct of_device_id bcm63138_leds_of_match_table[] = {
340 + { .compatible = "brcm,bcm63138-leds", },
344 +static struct platform_driver bcm63138_leds_driver = {
345 + .probe = bcm63138_leds_probe,
347 + .name = "leds-bcm63xxx",
348 + .of_match_table = bcm63138_leds_of_match_table,
352 +module_platform_driver(bcm63138_leds_driver);
354 +MODULE_AUTHOR("Rafał Miłecki");
355 +MODULE_LICENSE("GPL");
356 +MODULE_DEVICE_TABLE(of, bcm63138_leds_of_match_table);