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