blob: b415d8b2b40405c55fe96b35de5fc34745c7e513 [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
21
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
41/* Soft LED input register */
42#define LED_SW_LED_IP_REG 0xb8
43/* Soft LED input polarity register */
44#define LED_SW_LED_IP_PPOL_REG 0xbc
45
46struct bcm6858_led_priv {
47 void __iomem *regs;
48 u8 pin;
49};
50
51#ifdef CONFIG_LED_BLINK
52/*
53 * The value for flash rate are:
54 * 0 : no blinking
55 * 1 : rate is 25 Hz => 40 ms (period)
56 * 2 : rate is 12.5 Hz => 80 ms (period)
57 * 3 : rate is 6.25 Hz => 160 ms (period)
58 * 4 : rate is 3.125 Hz => 320 ms (period)
59 * 5 : rate is 1.5625 Hz => 640 ms (period)
60 * 6 : rate is 0.7815 Hz => 1280 ms (period)
61 * 7 : rate is 0.390625 Hz => 2560 ms (period)
62 */
63static const int bcm6858_flash_rate[8] = {
64 0, 40, 80, 160, 320, 640, 1280, 2560
65};
66
67static u32 bcm6858_flash_rate_value(int period_ms)
68{
69 unsigned long value = 7;
70 int i;
71
72 for (i = 0; i < ARRAY_SIZE(bcm6858_flash_rate); i++) {
73 if (period_ms <= bcm6858_flash_rate[i]) {
74 value = i;
75 break;
76 }
77 }
78
79 return value;
80}
81
82static int bcm6858_led_set_period(struct udevice *dev, int period_ms)
83{
84 struct bcm6858_led_priv *priv = dev_get_priv(dev);
85 u32 offset, shift, mask, value;
86
87 offset = (priv->pin / 8) * 4;
88 shift = (priv->pin % 8) * 4;
89 mask = 0x7 << shift;
90 value = bcm6858_flash_rate_value(period_ms) << shift;
91
92 clrbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, mask);
93 setbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, value);
94
95 return 0;
96}
97#endif
98
99static enum led_state_t bcm6858_led_get_state(struct udevice *dev)
100{
101 struct bcm6858_led_priv *priv = dev_get_priv(dev);
102 enum led_state_t state = LEDST_OFF;
103 u32 sw_led_ip;
104
105 sw_led_ip = readl(priv->regs + LED_SW_LED_IP_REG);
106 if (sw_led_ip & (1 << priv->pin))
107 state = LEDST_ON;
108
109 return state;
110}
111
112static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state)
113{
114 struct bcm6858_led_priv *priv = dev_get_priv(dev);
115
116 switch (state) {
117 case LEDST_OFF:
118 clrbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
119#ifdef CONFIG_LED_BLINK
120 bcm6858_led_set_period(dev, 0);
121#endif
122 break;
123 case LEDST_ON:
124 setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
125#ifdef CONFIG_LED_BLINK
126 bcm6858_led_set_period(dev, 0);
127#endif
128 break;
129 case LEDST_TOGGLE:
130 if (bcm6858_led_get_state(dev) == LEDST_OFF)
131 return bcm6858_led_set_state(dev, LEDST_ON);
132 else
133 return bcm6858_led_set_state(dev, LEDST_OFF);
134 break;
135#ifdef CONFIG_LED_BLINK
136 case LEDST_BLINK:
137 setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin));
138 break;
139#endif
140 default:
141 return -EINVAL;
142 }
143
144 return 0;
145}
146
147static const struct led_ops bcm6858_led_ops = {
148 .get_state = bcm6858_led_get_state,
149 .set_state = bcm6858_led_set_state,
150#ifdef CONFIG_LED_BLINK
151 .set_period = bcm6858_led_set_period,
152#endif
153};
154
155static int bcm6858_led_probe(struct udevice *dev)
156{
157 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
158
159 /* Top-level LED node */
160 if (!uc_plat->label) {
161 void __iomem *regs;
162 u32 set_bits = 0;
163
164 regs = dev_remap_addr(dev);
165 if (!regs)
166 return -EINVAL;
167
168 if (dev_read_bool(dev, "brcm,serial-led-msb-first"))
169 set_bits |= LED_CTRL_SERIAL_LED_MSB_FIRST;
170 if (dev_read_bool(dev, "brcm,serial-led-en-pol"))
171 set_bits |= LED_CTRL_SERIAL_LED_EN_POL;
172 if (dev_read_bool(dev, "brcm,serial-led-clk-pol"))
173 set_bits |= LED_CTRL_SERIAL_LED_CLK_POL;
174 if (dev_read_bool(dev, "brcm,serial-led-data-ppol"))
175 set_bits |= LED_CTRL_SERIAL_LED_DATA_PPOL;
176 if (dev_read_bool(dev, "brcm,led-test-mode"))
177 set_bits |= LED_CTRL_LED_TEST_MODE;
178
179 clrsetbits_32(regs + LED_CTRL_REG, ~0, set_bits);
180 } else {
181 struct bcm6858_led_priv *priv = dev_get_priv(dev);
182 void __iomem *regs;
183 unsigned int pin;
184
185 regs = dev_remap_addr(dev_get_parent(dev));
186 if (!regs)
187 return -EINVAL;
188
189 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
190 if (pin >= LEDS_MAX)
191 return -EINVAL;
192
193 priv->regs = regs;
194 priv->pin = pin;
195
196 /* this led is managed by software */
197 clrbits_32(regs + LED_HW_LED_EN_REG, 1 << pin);
198
199 /* configure the polarity */
200 if (dev_read_bool(dev, "active-low"))
201 clrbits_32(regs + LED_SW_LED_IP_PPOL_REG, 1 << pin);
202 else
203 setbits_32(regs + LED_SW_LED_IP_PPOL_REG, 1 << pin);
204 }
205
206 return 0;
207}
208
209static int bcm6858_led_bind(struct udevice *parent)
210{
211 ofnode node;
212
213 dev_for_each_subnode(node, parent) {
214 struct led_uc_plat *uc_plat;
215 struct udevice *dev;
216 const char *label;
217 int ret;
218
219 label = ofnode_read_string(node, "label");
220 if (!label) {
221 debug("%s: node %s has no label\n", __func__,
222 ofnode_get_name(node));
223 return -EINVAL;
224 }
225
226 ret = device_bind_driver_to_node(parent, "bcm6858-led",
227 ofnode_get_name(node),
228 node, &dev);
229 if (ret)
230 return ret;
231
232 uc_plat = dev_get_uclass_platdata(dev);
233 uc_plat->label = label;
234 }
235
236 return 0;
237}
238
239static const struct udevice_id bcm6858_led_ids[] = {
240 { .compatible = "brcm,bcm6858-leds" },
241 { /* sentinel */ }
242};
243
244U_BOOT_DRIVER(bcm6858_led) = {
245 .name = "bcm6858-led",
246 .id = UCLASS_LED,
247 .of_match = bcm6858_led_ids,
248 .bind = bcm6858_led_bind,
249 .probe = bcm6858_led_probe,
250 .priv_auto_alloc_size = sizeof(struct bcm6858_led_priv),
251 .ops = &bcm6858_led_ops,
252};