1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2022 Philippe Reynes <philippe.reynes@softathome.com>
6 * drivers/led/led_bcm6858.c
16 #include <linux/bitops.h>
21 /* LED Mode register */
22 #define LED_MODE_REG 0x0
23 #define LED_MODE_OFF 0
25 #define LED_MODE_MASK 1
27 /* LED Controller Global settings register */
28 #define CLED_CTRL_REG 0x00
29 #define CLED_CTRL_SERIAL_LED_DATA_PPOL BIT(1)
30 #define CLED_CTRL_SERIAL_LED_CLK_POL BIT(2)
31 #define CLED_CTRL_SERIAL_LED_EN_POL BIT(3)
32 #define CLED_CTRL_SERIAL_LED_MSB_FIRST BIT(4)
33 #define CLED_CTRL_MASK 0x1E
34 /* LED Controller IP LED source select register */
35 #define CLED_HW_LED_EN_REG 0x04
36 /* Hardware LED Polarity register */
37 #define CLED_HW_LED_IP_PPOL_REG 0x0c
38 /* Soft LED Set Register */
39 #define CLED_SW_LED_IP_SET_REG 0x10
40 /* Parallel LED Output Polarity Register */
41 #define CLED_PLED_OP_PPOL_REG 0x18
42 /* LED Channel activate register */
43 #define CLED_LED_CH_ACTIVATE_REG 0x1c
44 /* LED 0 Config 0 reg */
45 #define CLED_LED_0_CONFIG_0 0x20
46 /* Soft LED Clear Register */
47 #define CLED_SW_LED_IP_CLEAR_REG 0x444
48 /* Soft LED Status Register */
49 #define CLED_SW_LED_IP_STATUS_REG 0x448
51 /* Size of all registers used for the config of one LED */
52 #define CLED_CONFIG_SIZE (4 * sizeof(u32))
54 #define CLED_CONFIG0_MODE 0
55 #define CLED_CONFIG0_MODE_MASK (BIT(0) | BIT(1))
56 #define CLED_CONFIG0_MODE_STEADY 0
57 #define CLED_CONFIG0_MODE_FADING 1
58 #define CLED_CONFIG0_MODE_PULSATING 2
60 #define CLED_CONFIG0_FLASH_CTRL_SHIFT 3
61 #define CLED_CONFIG0_FLASH_CTRL_MASK (BIT(3) | BIT(4) | BIT(5))
63 struct bcm6753_led_priv {
69 * The value for flash rate are:
71 * 1 : rate is 25 Hz => 40 ms (period)
72 * 2 : rate is 12.5 Hz => 80 ms (period)
73 * 3 : rate is 6.25 Hz => 160 ms (period)
74 * 4 : rate is 3.125 Hz => 320 ms (period)
75 * 5 : rate is 1.5625 Hz => 640 ms (period)
76 * 6 : rate is 0.7815 Hz => 1280 ms (period)
77 * 7 : rate is 0.390625 Hz => 2560 ms (period)
79 static const int bcm6753_flash_rate[8] = {
80 0, 40, 80, 160, 320, 640, 1280, 2560
83 static u32 bcm6753_flash_rate_value(int period_ms)
85 unsigned long value = 7;
88 for (i = 0; i < ARRAY_SIZE(bcm6753_flash_rate); i++) {
89 if (period_ms <= bcm6753_flash_rate[i]) {
98 static int bcm6753_led_set_period(struct udevice *dev, int period_ms)
100 struct bcm6753_led_priv *priv = dev_get_priv(dev);
101 u32 offset, shift, value;
103 offset = CLED_LED_0_CONFIG_0 + (CLED_CONFIG_SIZE * priv->pin);
104 value = bcm6753_flash_rate_value(period_ms);
105 shift = CLED_CONFIG0_FLASH_CTRL_SHIFT;
107 /* set mode steady */
108 clrbits_32(priv->regs + offset, CLED_CONFIG0_MODE_MASK);
109 setbits_32(priv->regs + offset, CLED_CONFIG0_MODE_STEADY);
112 clrbits_32(priv->regs + offset, CLED_CONFIG0_FLASH_CTRL_MASK);
113 setbits_32(priv->regs + offset, value << shift);
116 setbits_32(priv->regs + CLED_LED_CH_ACTIVATE_REG, 1 << priv->pin);
121 static enum led_state_t bcm6753_led_get_state(struct udevice *dev)
123 struct bcm6753_led_priv *priv = dev_get_priv(dev);
124 enum led_state_t state = LEDST_OFF;
125 u32 sw_led_ip_status;
127 sw_led_ip_status = readl(priv->regs + CLED_SW_LED_IP_STATUS_REG);
128 if (sw_led_ip_status & (1 << priv->pin))
134 static int bcm6753_led_set_state(struct udevice *dev, enum led_state_t state)
136 struct bcm6753_led_priv *priv = dev_get_priv(dev);
140 setbits_32(priv->regs + CLED_SW_LED_IP_CLEAR_REG, (1 << priv->pin));
141 if (IS_ENABLED(CONFIG_LED_BLINK))
142 bcm6753_led_set_period(dev, 0);
145 setbits_32(priv->regs + CLED_SW_LED_IP_SET_REG, (1 << priv->pin));
146 if (IS_ENABLED(CONFIG_LED_BLINK))
147 bcm6753_led_set_period(dev, 0);
150 if (bcm6753_led_get_state(dev) == LEDST_OFF)
151 return bcm6753_led_set_state(dev, LEDST_ON);
153 return bcm6753_led_set_state(dev, LEDST_OFF);
155 #ifdef CONFIG_LED_BLINK
157 setbits_32(priv->regs + CLED_SW_LED_IP_SET_REG, (1 << priv->pin));
167 static const struct led_ops bcm6753_led_ops = {
168 .get_state = bcm6753_led_get_state,
169 .set_state = bcm6753_led_set_state,
170 #ifdef CONFIG_LED_BLINK
171 .set_period = bcm6753_led_set_period,
175 static int bcm6753_led_probe(struct udevice *dev)
177 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
179 /* Top-level LED node */
180 if (!uc_plat->label) {
184 regs = dev_remap_addr(dev);
188 if (dev_read_bool(dev, "brcm,serial-led-msb-first"))
189 set_bits |= CLED_CTRL_SERIAL_LED_MSB_FIRST;
190 if (dev_read_bool(dev, "brcm,serial-led-en-pol"))
191 set_bits |= CLED_CTRL_SERIAL_LED_EN_POL;
192 if (dev_read_bool(dev, "brcm,serial-led-clk-pol"))
193 set_bits |= CLED_CTRL_SERIAL_LED_CLK_POL;
194 if (dev_read_bool(dev, "brcm,serial-led-data-ppol"))
195 set_bits |= CLED_CTRL_SERIAL_LED_DATA_PPOL;
197 clrsetbits_32(regs + CLED_CTRL_REG, CLED_CTRL_MASK, set_bits);
199 struct bcm6753_led_priv *priv = dev_get_priv(dev);
203 regs = dev_remap_addr(dev_get_parent(dev));
207 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
214 /* this led is managed by software */
215 clrbits_32(regs + CLED_HW_LED_EN_REG, 1 << pin);
217 /* configure the polarity */
218 if (dev_read_bool(dev, "active-low"))
219 clrbits_32(regs + CLED_PLED_OP_PPOL_REG, 1 << pin);
221 setbits_32(regs + CLED_PLED_OP_PPOL_REG, 1 << pin);
227 static int bcm6753_led_bind(struct udevice *parent)
231 dev_for_each_subnode(node, parent) {
235 ret = device_bind_driver_to_node(parent, "bcm6753-led",
236 ofnode_get_name(node),
245 static const struct udevice_id bcm6753_led_ids[] = {
246 { .compatible = "brcm,bcm6753-leds" },
250 U_BOOT_DRIVER(bcm6753_led) = {
251 .name = "bcm6753-led",
253 .of_match = bcm6753_led_ids,
254 .bind = bcm6753_led_bind,
255 .probe = bcm6753_led_probe,
256 .priv_auto = sizeof(struct bcm6753_led_priv),
257 .ops = &bcm6753_led_ops,