1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Philippe Reynes <philippe.reynes@softathome.com>
6 * drivers/led/led_bcm6328.c
7 * drivers/led/led_bcm6358.c
17 #include <linux/bitops.h>
21 #define LEDS_MAX_BRIGHTNESS 7
23 /* LED Mode register */
24 #define LED_MODE_REG 0x0
25 #define LED_MODE_OFF 0
27 #define LED_MODE_MASK 1
29 /* LED Controller Global settings register */
30 #define LED_CTRL_REG 0x00
31 #define LED_CTRL_MASK 0x1f
32 #define LED_CTRL_LED_TEST_MODE BIT(0)
33 #define LED_CTRL_SERIAL_LED_DATA_PPOL BIT(1)
34 #define LED_CTRL_SERIAL_LED_CLK_POL BIT(2)
35 #define LED_CTRL_SERIAL_LED_EN_POL BIT(3)
36 #define LED_CTRL_SERIAL_LED_MSB_FIRST BIT(4)
38 /* LED Controller IP LED source select register */
39 #define LED_HW_LED_EN_REG 0x08
40 /* LED Flash control register0 */
41 #define LED_FLASH_RATE_CONTROL_REG0 0x10
42 /* LED Brightness control register0 */
43 #define LED_BRIGHTNESS_CONTROL_REG0 0x20
44 /* Soft LED input register */
45 #define LED_SW_LED_IP_REG 0xb8
46 /* Parallel LED Output Polarity Register */
47 #define LED_PLED_OP_PPOL_REG 0xc0
49 struct bcm6858_led_priv {
54 #ifdef CONFIG_LED_BLINK
56 * The value for flash rate are:
58 * 1 : rate is 25 Hz => 40 ms (period)
59 * 2 : rate is 12.5 Hz => 80 ms (period)
60 * 3 : rate is 6.25 Hz => 160 ms (period)
61 * 4 : rate is 3.125 Hz => 320 ms (period)
62 * 5 : rate is 1.5625 Hz => 640 ms (period)
63 * 6 : rate is 0.7815 Hz => 1280 ms (period)
64 * 7 : rate is 0.390625 Hz => 2560 ms (period)
66 static const int bcm6858_flash_rate[8] = {
67 0, 40, 80, 160, 320, 640, 1280, 2560
70 static u32 bcm6858_flash_rate_value(int period_ms)
72 unsigned long value = 7;
75 for (i = 0; i < ARRAY_SIZE(bcm6858_flash_rate); i++) {
76 if (period_ms <= bcm6858_flash_rate[i]) {
85 static int bcm6858_led_set_period(struct udevice *dev, int period_ms)
87 struct bcm6858_led_priv *priv = dev_get_priv(dev);
88 u32 offset, shift, mask, value;
90 offset = (priv->pin / 8) * 4;
91 shift = (priv->pin % 8) * 4;
93 value = bcm6858_flash_rate_value(period_ms) << shift;
95 clrbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, mask);
96 setbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, value);
102 static int led_set_brightness(struct udevice *dev, unsigned int brightness)
104 struct bcm6858_led_priv *priv = dev_get_priv(dev);
105 u32 offset, shift, mask, value;
107 offset = (priv->pin / 8) * 4;
108 shift = (priv->pin % 8) * 4;
111 /* 8 levels of brightness achieved through PWM */
112 value = (brightness > LEDS_MAX_BRIGHTNESS ?
113 LEDS_MAX_BRIGHTNESS : brightness) << shift;
115 debug("%s: %s brightness set to %u\n", __func__, dev->name, value >> shift);
117 clrbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, mask);
118 setbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, value);
123 static enum led_state_t bcm6858_led_get_state(struct udevice *dev)
125 struct bcm6858_led_priv *priv = dev_get_priv(dev);
126 enum led_state_t state = LEDST_OFF;
129 sw_led_ip = readl(priv->regs + LED_SW_LED_IP_REG);
130 if (sw_led_ip & (1 << priv->pin))
136 static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
138 struct bcm6858_led_priv *priv = dev_get_priv(dev);
140 debug("%s: Set led %s to %d\n", __func__, dev->name, state);
144 clrbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
145 #ifdef CONFIG_LED_BLINK
146 bcm6858_led_set_period(dev, 0);
150 setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
151 #ifdef CONFIG_LED_BLINK
152 bcm6858_led_set_period(dev, 0);
156 if (bcm6858_led_get_state(dev) == LEDST_OFF)
157 return bcm6858_led_set_state(dev, LEDST_ON);
159 return bcm6858_led_set_state(dev, LEDST_OFF);
161 #ifdef CONFIG_LED_BLINK
163 setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
173 static const struct led_ops bcm6858_led_ops = {
174 .get_state = bcm6858_led_get_state,
175 .set_state = bcm6858_led_set_state,
176 #ifdef CONFIG_LED_BLINK
177 .set_period = bcm6858_led_set_period,
181 static int bcm6858_led_probe(struct udevice *dev)
183 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
185 /* Top-level LED node */
186 if (!uc_plat->label) {
190 regs = dev_remap_addr(dev);
194 if (dev_read_bool(dev, "brcm,serial-led-msb-first"))
195 set_bits |= LED_CTRL_SERIAL_LED_MSB_FIRST;
196 if (dev_read_bool(dev, "brcm,serial-led-en-pol"))
197 set_bits |= LED_CTRL_SERIAL_LED_EN_POL;
198 if (dev_read_bool(dev, "brcm,serial-led-clk-pol"))
199 set_bits |= LED_CTRL_SERIAL_LED_CLK_POL;
200 if (dev_read_bool(dev, "brcm,serial-led-data-ppol"))
201 set_bits |= LED_CTRL_SERIAL_LED_DATA_PPOL;
202 if (dev_read_bool(dev, "brcm,led-test-mode"))
203 set_bits |= LED_CTRL_LED_TEST_MODE;
205 clrsetbits_32(regs + LED_CTRL_REG, ~0, set_bits);
207 struct bcm6858_led_priv *priv = dev_get_priv(dev);
209 unsigned int pin, brightness;
211 regs = dev_remap_addr(dev_get_parent(dev));
215 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
222 /* this led is managed by software */
223 clrbits_32(regs + LED_HW_LED_EN_REG, 1 << pin);
225 /* configure the polarity */
226 if (dev_read_bool(dev, "active-low"))
227 clrbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
229 setbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
231 brightness = dev_read_u32_default(dev, "default-brightness",
232 LEDS_MAX_BRIGHTNESS);
233 led_set_brightness(dev, brightness);
239 static int bcm6858_led_bind(struct udevice *parent)
243 dev_for_each_subnode(node, parent) {
247 ret = device_bind_driver_to_node(parent, "bcm6858-led",
248 ofnode_get_name(node),
257 static const struct udevice_id bcm6858_led_ids[] = {
258 { .compatible = "brcm,bcm6858-leds" },
262 U_BOOT_DRIVER(bcm6858_led) = {
263 .name = "bcm6858-led",
265 .of_match = bcm6858_led_ids,
266 .bind = bcm6858_led_bind,
267 .probe = bcm6858_led_probe,
268 .priv_auto = sizeof(struct bcm6858_led_priv),
269 .ops = &bcm6858_led_ops,