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