blob: 6b3698674b9d293d421e0bd6d2e1f2b0e3480e9b [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
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010015#include <asm/io.h>
16#include <dm/lists.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010018
19#define LEDS_MAX 32
20#define LEDS_WAIT 100
Paul HENRYS891d2422022-03-17 10:29:47 +010021#define LEDS_MAX_BRIGHTNESS 7
Philippe Reynes9d5fb9c2019-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 HENRYS891d2422022-03-17 10:29:47 +010042/* LED Brightness control register0 */
43#define LED_BRIGHTNESS_CONTROL_REG0 0x20
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +010044/* Soft LED input register */
45#define LED_SW_LED_IP_REG 0xb8
Steven Lawrancee88d5a02020-10-29 18:27:34 +010046/* Parallel LED Output Polarity Register */
47#define LED_PLED_OP_PPOL_REG 0xc0
Philippe Reynes9d5fb9c2019-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 HENRYS891d2422022-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 Reynes9d5fb9c2019-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 HENRYS891d2422022-03-17 10:29:47 +0100140 debug("%s: Set led %s to %d\n", __func__, dev->name, state);
141
Philippe Reynes9d5fb9c2019-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{
Simon Glass71fa5b42020-12-03 16:55:18 -0700183 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100184
185 /* Top-level LED node */
186 if (!uc_plat->label) {
187 void __iomem *regs;
188 u32 set_bits = 0;
189
190 regs = dev_remap_addr(dev);
191 if (!regs)
192 return -EINVAL;
193
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;
204
205 clrsetbits_32(regs + LED_CTRL_REG, ~0, set_bits);
206 } else {
207 struct bcm6858_led_priv *priv = dev_get_priv(dev);
208 void __iomem *regs;
Paul HENRYS891d2422022-03-17 10:29:47 +0100209 unsigned int pin, brightness;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100210
211 regs = dev_remap_addr(dev_get_parent(dev));
212 if (!regs)
213 return -EINVAL;
214
215 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
216 if (pin >= LEDS_MAX)
217 return -EINVAL;
218
219 priv->regs = regs;
220 priv->pin = pin;
221
222 /* this led is managed by software */
223 clrbits_32(regs + LED_HW_LED_EN_REG, 1 << pin);
224
225 /* configure the polarity */
226 if (dev_read_bool(dev, "active-low"))
Steven Lawrancee88d5a02020-10-29 18:27:34 +0100227 clrbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100228 else
Steven Lawrancee88d5a02020-10-29 18:27:34 +0100229 setbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin);
Paul HENRYS891d2422022-03-17 10:29:47 +0100230
231 brightness = dev_read_u32_default(dev, "default-brightness",
232 LEDS_MAX_BRIGHTNESS);
233 led_set_brightness(dev, brightness);
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100234 }
235
236 return 0;
237}
238
239static int bcm6858_led_bind(struct udevice *parent)
240{
241 ofnode node;
242
243 dev_for_each_subnode(node, parent) {
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100244 struct udevice *dev;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100245 int ret;
246
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100247 ret = device_bind_driver_to_node(parent, "bcm6858-led",
248 ofnode_get_name(node),
249 node, &dev);
250 if (ret)
251 return ret;
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100252 }
253
254 return 0;
255}
256
257static const struct udevice_id bcm6858_led_ids[] = {
258 { .compatible = "brcm,bcm6858-leds" },
259 { /* sentinel */ }
260};
261
262U_BOOT_DRIVER(bcm6858_led) = {
263 .name = "bcm6858-led",
264 .id = UCLASS_LED,
265 .of_match = bcm6858_led_ids,
266 .bind = bcm6858_led_bind,
267 .probe = bcm6858_led_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700268 .priv_auto = sizeof(struct bcm6858_led_priv),
Philippe Reynes9d5fb9c2019-03-22 17:02:01 +0100269 .ops = &bcm6858_led_ops,
270};