blob: 397dc0d8693ddd8fae84843b3b3a25507d8251fd [file] [log] [blame]
Philippe Reynesd00c6a22019-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
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <led.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Philippe Reynesd00c6a22019-03-22 17:02:01 +010015#include <asm/io.h>
16#include <dm/lists.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Philippe Reynesd00c6a22019-03-22 17:02:01 +010018
19#define LEDS_MAX 32
20#define LEDS_WAIT 100
Paul HENRYS877de2a2022-03-17 10:29:47 +010021#define LEDS_MAX_BRIGHTNESS 7
Philippe Reynesd00c6a22019-03-22 17:02:01 +010022
23/* LED Mode register */
24#define LED_MODE_REG 0x0
25#define LED_MODE_OFF 0
26#define LED_MODE_ON 1
27#define LED_MODE_MASK 1
28
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)
37
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
Paul HENRYS877de2a2022-03-17 10:29:47 +010042/* LED Brightness control register0 */
43#define LED_BRIGHTNESS_CONTROL_REG0 0x20
Philippe Reynesd00c6a22019-03-22 17:02:01 +010044/* Soft LED input register */
45#define LED_SW_LED_IP_REG 0xb8
Steven Lawrancee0407252020-10-29 18:27:34 +010046/* Parallel LED Output Polarity Register */
47#define LED_PLED_OP_PPOL_REG 0xc0
Philippe Reynesd00c6a22019-03-22 17:02:01 +010048
49struct bcm6858_led_priv {
50 void __iomem *regs;
51 u8 pin;
52};
53
54#ifdef CONFIG_LED_BLINK
55/*
56 * The value for flash rate are:
57 * 0 : no blinking
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)
65 */
66static const int bcm6858_flash_rate[8] = {
67 0, 40, 80, 160, 320, 640, 1280, 2560
68};
69
70static u32 bcm6858_flash_rate_value(int period_ms)
71{
72 unsigned long value = 7;
73 int i;
74
75 for (i = 0; i < ARRAY_SIZE(bcm6858_flash_rate); i++) {
76 if (period_ms <= bcm6858_flash_rate[i]) {
77 value = i;
78 break;
79 }
80 }
81
82 return value;
83}
84
85static int bcm6858_led_set_period(struct udevice *dev, int period_ms)
86{
87 struct bcm6858_led_priv *priv = dev_get_priv(dev);
88 u32 offset, shift, mask, value;
89
90 offset = (priv->pin / 8) * 4;
91 shift = (priv->pin % 8) * 4;
92 mask = 0x7 << shift;
93 value = bcm6858_flash_rate_value(period_ms) << shift;
94
95 clrbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, mask);
96 setbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, value);
97
98 return 0;
99}
100#endif
101
Paul HENRYS877de2a2022-03-17 10:29:47 +0100102static int led_set_brightness(struct udevice *dev, unsigned int brightness)
103{
104 struct bcm6858_led_priv *priv = dev_get_priv(dev);
105 u32 offset, shift, mask, value;
106
107 offset = (priv->pin / 8) * 4;
108 shift = (priv->pin % 8) * 4;
109 mask = 0xf << shift;
110
111 /* 8 levels of brightness achieved through PWM */
112 value = (brightness > LEDS_MAX_BRIGHTNESS ?
113 LEDS_MAX_BRIGHTNESS : brightness) << shift;
114
115 debug("%s: %s brightness set to %u\n", __func__, dev->name, value >> shift);
116
117 clrbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, mask);
118 setbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, value);
119
120 return 0;
121}
122
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100123static enum led_state_t bcm6858_led_get_state(struct udevice *dev)
124{
125 struct bcm6858_led_priv *priv = dev_get_priv(dev);
126 enum led_state_t state = LEDST_OFF;
127 u32 sw_led_ip;
128
129 sw_led_ip = readl(priv->regs + LED_SW_LED_IP_REG);
130 if (sw_led_ip & (1 << priv->pin))
131 state = LEDST_ON;
132
133 return state;
134}
135
136static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
137{
138 struct bcm6858_led_priv *priv = dev_get_priv(dev);
139
Paul HENRYS877de2a2022-03-17 10:29:47 +0100140 debug("%s: Set led %s to %d\n", __func__, dev->name, state);
141
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100142 switch (state) {
143 case LEDST_OFF:
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);
147#endif
148 break;
149 case LEDST_ON:
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);
153#endif
154 break;
155 case LEDST_TOGGLE:
156 if (bcm6858_led_get_state(dev) == LEDST_OFF)
157 return bcm6858_led_set_state(dev, LEDST_ON);
158 else
159 return bcm6858_led_set_state(dev, LEDST_OFF);
160 break;
161#ifdef CONFIG_LED_BLINK
162 case LEDST_BLINK:
163 setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
164 break;
165#endif
166 default:
167 return -EINVAL;
168 }
169
170 return 0;
171}
172
173static 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,
178#endif
179};
180
181static int bcm6858_led_probe(struct udevice *dev)
182{
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200183 struct bcm6858_led_priv *priv = dev_get_priv(dev);
184 void __iomem *regs;
185 unsigned int pin, brightness;
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100186
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200187 regs = dev_remap_addr(dev_get_parent(dev));
188 if (!regs)
189 return -EINVAL;
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100190
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200191 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
192 if (pin >= LEDS_MAX)
193 return -EINVAL;
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100194
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200195 priv->regs = regs;
196 priv->pin = pin;
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100197
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200198 /* this led is managed by software */
199 clrbits_32(regs + LED_HW_LED_EN_REG, 1 << pin);
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100200
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200201 /* configure the polarity */
202 if (dev_read_bool(dev, "active-low"))
203 clrbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
204 else
205 setbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100206
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200207 brightness = dev_read_u32_default(dev, "default-brightness",
Paul HENRYS877de2a2022-03-17 10:29:47 +0100208 LEDS_MAX_BRIGHTNESS);
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200209 led_set_brightness(dev, brightness);
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100210
211 return 0;
212}
213
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200214U_BOOT_DRIVER(bcm6858_led) = {
215 .name = "bcm6858-led",
216 .id = UCLASS_LED,
217 .probe = bcm6858_led_probe,
218 .priv_auto = sizeof(struct bcm6858_led_priv),
219 .ops = &bcm6858_led_ops,
220};
221
222static int bcm6858_led_wrap_probe(struct udevice *dev)
223{
224 void __iomem *regs;
225 u32 set_bits = 0;
226
227 regs = dev_remap_addr(dev);
228 if (!regs)
229 return -EINVAL;
230
231 if (dev_read_bool(dev, "brcm,serial-led-msb-first"))
232 set_bits |= LED_CTRL_SERIAL_LED_MSB_FIRST;
233 if (dev_read_bool(dev, "brcm,serial-led-en-pol"))
234 set_bits |= LED_CTRL_SERIAL_LED_EN_POL;
235 if (dev_read_bool(dev, "brcm,serial-led-clk-pol"))
236 set_bits |= LED_CTRL_SERIAL_LED_CLK_POL;
237 if (dev_read_bool(dev, "brcm,serial-led-data-ppol"))
238 set_bits |= LED_CTRL_SERIAL_LED_DATA_PPOL;
239 if (dev_read_bool(dev, "brcm,led-test-mode"))
240 set_bits |= LED_CTRL_LED_TEST_MODE;
241
242 clrsetbits_32(regs + LED_CTRL_REG, ~0, set_bits);
243
244 return 0;
245}
246
247static int bcm6858_led_wrap_bind(struct udevice *parent)
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100248{
249 ofnode node;
250
251 dev_for_each_subnode(node, parent) {
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100252 struct udevice *dev;
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100253 int ret;
254
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100255 ret = device_bind_driver_to_node(parent, "bcm6858-led",
256 ofnode_get_name(node),
257 node, &dev);
258 if (ret)
259 return ret;
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100260 }
261
262 return 0;
263}
264
265static const struct udevice_id bcm6858_led_ids[] = {
266 { .compatible = "brcm,bcm6858-leds" },
267 { /* sentinel */ }
268};
269
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200270U_BOOT_DRIVER(bcm6858_led_wrap) = {
271 .name = "bcm6858_led_wrap",
272 .id = UCLASS_NOP,
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100273 .of_match = bcm6858_led_ids,
Philippe Reynes8fd6e642023-06-23 18:36:42 +0200274 .probe = bcm6858_led_wrap_probe,
275 .bind = bcm6858_led_wrap_bind,
Philippe Reynesd00c6a22019-03-22 17:02:01 +0100276};