blob: a6efdcf640535729671ed7937932acefe7acd57d [file] [log] [blame]
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Philippe Reynes <philippe.reynes@softathome.com>
4 *
5 * based on:
6 * drivers/led/led_bcm6328.c
7 * drivers/led/led_bcm6358.c
8 */
9
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010010#include <dm.h>
11#include <errno.h>
12#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010014#include <asm/io.h>
15#include <dm/lists.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010017
18#define LEDS_MAX 32
19#define LEDS_WAIT 100
Paul HENRYS891d2422022-03-17 10:29:47 +010020#define LEDS_MAX_BRIGHTNESS 7
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010021
22/* LED Mode register */
23#define LED_MODE_REG 0x0
24#define LED_MODE_OFF 0
25#define LED_MODE_ON 1
26#define LED_MODE_MASK 1
27
28/* LED Controller Global settings register */
29#define LED_CTRL_REG 0x00
30#define LED_CTRL_MASK 0x1f
31#define LED_CTRL_LED_TEST_MODE BIT(0)
32#define LED_CTRL_SERIAL_LED_DATA_PPOL BIT(1)
33#define LED_CTRL_SERIAL_LED_CLK_POL BIT(2)
34#define LED_CTRL_SERIAL_LED_EN_POL BIT(3)
35#define LED_CTRL_SERIAL_LED_MSB_FIRST BIT(4)
36
37/* LED Controller IP LED source select register */
38#define LED_HW_LED_EN_REG 0x08
39/* LED Flash control register0 */
40#define LED_FLASH_RATE_CONTROL_REG0 0x10
Paul HENRYS891d2422022-03-17 10:29:47 +010041/* LED Brightness control register0 */
42#define LED_BRIGHTNESS_CONTROL_REG0 0x20
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010043/* Soft LED input register */
44#define LED_SW_LED_IP_REG 0xb8
Steven Lawrancee88d5a02020-10-29 18:27:34 +010045/* Parallel LED Output Polarity Register */
46#define LED_PLED_OP_PPOL_REG 0xc0
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010047
48struct bcm6858_led_priv {
49 void __iomem *regs;
50 u8 pin;
51};
52
53#ifdef CONFIG_LED_BLINK
54/*
55 * The value for flash rate are:
56 * 0 : no blinking
57 * 1 : rate is 25 Hz => 40 ms (period)
58 * 2 : rate is 12.5 Hz => 80 ms (period)
59 * 3 : rate is 6.25 Hz => 160 ms (period)
60 * 4 : rate is 3.125 Hz => 320 ms (period)
61 * 5 : rate is 1.5625 Hz => 640 ms (period)
62 * 6 : rate is 0.7815 Hz => 1280 ms (period)
63 * 7 : rate is 0.390625 Hz => 2560 ms (period)
64 */
65static const int bcm6858_flash_rate[8] = {
66 0, 40, 80, 160, 320, 640, 1280, 2560
67};
68
69static u32 bcm6858_flash_rate_value(int period_ms)
70{
71 unsigned long value = 7;
72 int i;
73
74 for (i = 0; i < ARRAY_SIZE(bcm6858_flash_rate); i++) {
75 if (period_ms <= bcm6858_flash_rate[i]) {
76 value = i;
77 break;
78 }
79 }
80
81 return value;
82}
83
84static int bcm6858_led_set_period(struct udevice *dev, int period_ms)
85{
86 struct bcm6858_led_priv *priv = dev_get_priv(dev);
87 u32 offset, shift, mask, value;
88
89 offset = (priv->pin / 8) * 4;
90 shift = (priv->pin % 8) * 4;
91 mask = 0x7 << shift;
92 value = bcm6858_flash_rate_value(period_ms) << shift;
93
94 clrbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, mask);
95 setbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, value);
96
97 return 0;
98}
99#endif
100
Paul HENRYS891d2422022-03-17 10:29:47 +0100101static int led_set_brightness(struct udevice *dev, unsigned int brightness)
102{
103 struct bcm6858_led_priv *priv = dev_get_priv(dev);
104 u32 offset, shift, mask, value;
105
106 offset = (priv->pin / 8) * 4;
107 shift = (priv->pin % 8) * 4;
108 mask = 0xf << shift;
109
110 /* 8 levels of brightness achieved through PWM */
111 value = (brightness > LEDS_MAX_BRIGHTNESS ?
112 LEDS_MAX_BRIGHTNESS : brightness) << shift;
113
114 debug("%s: %s brightness set to %u\n", __func__, dev->name, value >> shift);
115
116 clrbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, mask);
117 setbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, value);
118
119 return 0;
120}
121
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100122static enum led_state_t bcm6858_led_get_state(struct udevice *dev)
123{
124 struct bcm6858_led_priv *priv = dev_get_priv(dev);
125 enum led_state_t state = LEDST_OFF;
126 u32 sw_led_ip;
127
128 sw_led_ip = readl(priv->regs + LED_SW_LED_IP_REG);
129 if (sw_led_ip & (1 << priv->pin))
130 state = LEDST_ON;
131
132 return state;
133}
134
135static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
136{
137 struct bcm6858_led_priv *priv = dev_get_priv(dev);
138
Paul HENRYS891d2422022-03-17 10:29:47 +0100139 debug("%s: Set led %s to %d\n", __func__, dev->name, state);
140
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100141 switch (state) {
142 case LEDST_OFF:
143 clrbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
144#ifdef CONFIG_LED_BLINK
145 bcm6858_led_set_period(dev, 0);
146#endif
147 break;
148 case LEDST_ON:
149 setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
150#ifdef CONFIG_LED_BLINK
151 bcm6858_led_set_period(dev, 0);
152#endif
153 break;
154 case LEDST_TOGGLE:
155 if (bcm6858_led_get_state(dev) == LEDST_OFF)
156 return bcm6858_led_set_state(dev, LEDST_ON);
157 else
158 return bcm6858_led_set_state(dev, LEDST_OFF);
159 break;
160#ifdef CONFIG_LED_BLINK
161 case LEDST_BLINK:
162 setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
163 break;
164#endif
165 default:
166 return -EINVAL;
167 }
168
169 return 0;
170}
171
172static const struct led_ops bcm6858_led_ops = {
173 .get_state = bcm6858_led_get_state,
174 .set_state = bcm6858_led_set_state,
175#ifdef CONFIG_LED_BLINK
176 .set_period = bcm6858_led_set_period,
177#endif
178};
179
180static int bcm6858_led_probe(struct udevice *dev)
181{
Philippe Reynes914524a2023-06-23 18:36:42 +0200182 struct bcm6858_led_priv *priv = dev_get_priv(dev);
183 void __iomem *regs;
184 unsigned int pin, brightness;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100185
Philippe Reynes914524a2023-06-23 18:36:42 +0200186 regs = dev_remap_addr(dev_get_parent(dev));
187 if (!regs)
188 return -EINVAL;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100189
Philippe Reynes914524a2023-06-23 18:36:42 +0200190 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
191 if (pin >= LEDS_MAX)
192 return -EINVAL;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100193
Philippe Reynes914524a2023-06-23 18:36:42 +0200194 priv->regs = regs;
195 priv->pin = pin;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100196
Philippe Reynes914524a2023-06-23 18:36:42 +0200197 /* this led is managed by software */
198 clrbits_32(regs + LED_HW_LED_EN_REG, 1 << pin);
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100199
Philippe Reynes914524a2023-06-23 18:36:42 +0200200 /* configure the polarity */
201 if (dev_read_bool(dev, "active-low"))
202 clrbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
203 else
204 setbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100205
Philippe Reynes914524a2023-06-23 18:36:42 +0200206 brightness = dev_read_u32_default(dev, "default-brightness",
207 LEDS_MAX_BRIGHTNESS);
208 led_set_brightness(dev, brightness);
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100209
Philippe Reynes914524a2023-06-23 18:36:42 +0200210 return 0;
211}
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100212
Philippe Reynes914524a2023-06-23 18:36:42 +0200213U_BOOT_DRIVER(bcm6858_led) = {
214 .name = "bcm6858-led",
215 .id = UCLASS_LED,
216 .probe = bcm6858_led_probe,
217 .priv_auto = sizeof(struct bcm6858_led_priv),
218 .ops = &bcm6858_led_ops,
219};
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100220
Philippe Reynes914524a2023-06-23 18:36:42 +0200221static int bcm6858_led_wrap_probe(struct udevice *dev)
222{
223 void __iomem *regs;
224 u32 set_bits = 0;
Paul HENRYS891d2422022-03-17 10:29:47 +0100225
Philippe Reynes914524a2023-06-23 18:36:42 +0200226 regs = dev_remap_addr(dev);
227 if (!regs)
228 return -EINVAL;
229
230 if (dev_read_bool(dev, "brcm,serial-led-msb-first"))
231 set_bits |= LED_CTRL_SERIAL_LED_MSB_FIRST;
232 if (dev_read_bool(dev, "brcm,serial-led-en-pol"))
233 set_bits |= LED_CTRL_SERIAL_LED_EN_POL;
234 if (dev_read_bool(dev, "brcm,serial-led-clk-pol"))
235 set_bits |= LED_CTRL_SERIAL_LED_CLK_POL;
236 if (dev_read_bool(dev, "brcm,serial-led-data-ppol"))
237 set_bits |= LED_CTRL_SERIAL_LED_DATA_PPOL;
238 if (dev_read_bool(dev, "brcm,led-test-mode"))
239 set_bits |= LED_CTRL_LED_TEST_MODE;
240
241 clrsetbits_32(regs + LED_CTRL_REG, ~0, set_bits);
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100242
243 return 0;
244}
245
Philippe Reynes914524a2023-06-23 18:36:42 +0200246static int bcm6858_led_wrap_bind(struct udevice *parent)
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100247{
248 ofnode node;
249
250 dev_for_each_subnode(node, parent) {
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100251 struct udevice *dev;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100252 int ret;
253
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100254 ret = device_bind_driver_to_node(parent, "bcm6858-led",
255 ofnode_get_name(node),
256 node, &dev);
257 if (ret)
258 return ret;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100259 }
260
261 return 0;
262}
263
264static const struct udevice_id bcm6858_led_ids[] = {
265 { .compatible = "brcm,bcm6858-leds" },
266 { /* sentinel */ }
267};
268
Philippe Reynes914524a2023-06-23 18:36:42 +0200269U_BOOT_DRIVER(bcm6858_led_wrap) = {
270 .name = "bcm6858_led_wrap",
271 .id = UCLASS_NOP,
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100272 .of_match = bcm6858_led_ids,
Philippe Reynes914524a2023-06-23 18:36:42 +0200273 .probe = bcm6858_led_wrap_probe,
274 .bind = bcm6858_led_wrap_bind,
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100275};