blob: 11caecdc26b141769e89060f09dd8c143ca23070 [file] [log] [blame]
Álvaro Fernández Rojas9bcd3552017-05-07 20:11:30 +02001/*
2 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <led.h>
11#include <asm/io.h>
12#include <dm/lists.h>
13
14#define LEDS_MAX 32
15#define LEDS_WAIT 100
16
17/* LED Mode register */
18#define LED_MODE_REG 0x0
19#define LED_MODE_OFF 0
20#define LED_MODE_ON 1
21#define LED_MODE_MASK 1
22
23/* LED Control register */
24#define LED_CTRL_REG 0x4
25#define LED_CTRL_CLK_MASK 0x3
26#define LED_CTRL_CLK_1 0
27#define LED_CTRL_CLK_2 1
28#define LED_CTRL_CLK_4 2
29#define LED_CTRL_CLK_8 3
30#define LED_CTRL_POL_SHIFT 2
31#define LED_CTRL_POL_MASK (1 << LED_CTRL_POL_SHIFT)
32#define LED_CTRL_BUSY_SHIFT 3
33#define LED_CTRL_BUSY_MASK (1 << LED_CTRL_BUSY_SHIFT)
34
35DECLARE_GLOBAL_DATA_PTR;
36
37struct bcm6358_led_priv {
38 void __iomem *regs;
39 uint8_t pin;
40 bool active_low;
41};
42
43static void bcm6358_led_busy(void __iomem *regs)
44{
45 while (readl_be(regs + LED_CTRL_REG) & LED_CTRL_BUSY_MASK)
46 udelay(LEDS_WAIT);
47}
48
49static unsigned long bcm6358_led_get_mode(struct bcm6358_led_priv *priv)
50{
51 bcm6358_led_busy(priv->regs);
52
53 return (readl_be(priv->regs + LED_MODE_REG) >> priv->pin) &
54 LED_MODE_MASK;
55}
56
57static int bcm6358_led_set_mode(struct bcm6358_led_priv *priv, uint8_t mode)
58{
59 bcm6358_led_busy(priv->regs);
60
61 clrsetbits_be32(priv->regs + LED_MODE_REG,
62 (LED_MODE_MASK << priv->pin),
63 (mode << priv->pin));
64
65 return 0;
66}
67
68static enum led_state_t bcm6358_led_get_state(struct udevice *dev)
69{
70 struct bcm6358_led_priv *priv = dev_get_priv(dev);
71 enum led_state_t state = LEDST_OFF;
72
73 switch (bcm6358_led_get_mode(priv)) {
74 case LED_MODE_OFF:
75 state = (priv->active_low ? LEDST_ON : LEDST_OFF);
76 break;
77 case LED_MODE_ON:
78 state = (priv->active_low ? LEDST_OFF : LEDST_ON);
79 break;
80 }
81
82 return state;
83}
84
85static int bcm6358_led_set_state(struct udevice *dev, enum led_state_t state)
86{
87 struct bcm6358_led_priv *priv = dev_get_priv(dev);
88 unsigned long mode;
89
90 switch (state) {
91 case LEDST_OFF:
92 mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF);
93 break;
94 case LEDST_ON:
95 mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON);
96 break;
97 case LEDST_TOGGLE:
98 if (bcm6358_led_get_state(dev) == LEDST_OFF)
99 return bcm6358_led_set_state(dev, LEDST_ON);
100 else
101 return bcm6358_led_set_state(dev, LEDST_OFF);
102 break;
103 default:
104 return -ENOSYS;
105 }
106
107 return bcm6358_led_set_mode(priv, mode);
108}
109
110static const struct led_ops bcm6358_led_ops = {
111 .get_state = bcm6358_led_get_state,
112 .set_state = bcm6358_led_set_state,
113};
114
115static int bcm6358_led_probe(struct udevice *dev)
116{
117 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
118 fdt_addr_t addr;
119 fdt_size_t size;
120
121 /* Top-level LED node */
122 if (!uc_plat->label) {
123 void __iomem *regs;
124 unsigned int clk_div;
125 u32 set_bits = 0;
126
127 addr = dev_get_addr_size_index(dev, 0, &size);
128 if (addr == FDT_ADDR_T_NONE)
129 return -EINVAL;
130
131 regs = ioremap(addr, size);
132
133 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
134 "brcm,clk-dat-low"))
135 set_bits |= LED_CTRL_POL_MASK;
136 clk_div = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
137 "brcm,clk-div", LED_CTRL_CLK_1);
138 switch (clk_div) {
139 case 8:
140 set_bits |= LED_CTRL_CLK_8;
141 break;
142 case 4:
143 set_bits |= LED_CTRL_CLK_4;
144 break;
145 case 2:
146 set_bits |= LED_CTRL_CLK_2;
147 break;
148 default:
149 set_bits |= LED_CTRL_CLK_1;
150 break;
151 }
152
153 bcm6358_led_busy(regs);
154 clrsetbits_be32(regs + LED_CTRL_REG,
155 LED_CTRL_POL_MASK | LED_CTRL_CLK_MASK,
156 set_bits);
157 } else {
158 struct bcm6358_led_priv *priv = dev_get_priv(dev);
159 unsigned int pin;
160
161 addr = dev_get_addr_size_index(dev_get_parent(dev), 0, &size);
162 if (addr == FDT_ADDR_T_NONE)
163 return -EINVAL;
164
165 pin = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), "reg",
166 LEDS_MAX);
167 if (pin >= LEDS_MAX)
168 return -EINVAL;
169
170 priv->regs = ioremap(addr, size);
171 priv->pin = pin;
172
173 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
174 "active-low"))
175 priv->active_low = true;
176 }
177
178 return 0;
179}
180
181static int bcm6358_led_bind(struct udevice *parent)
182{
183 const void *blob = gd->fdt_blob;
184 int node;
185
186 for (node = fdt_first_subnode(blob, dev_of_offset(parent));
187 node > 0;
188 node = fdt_next_subnode(blob, node)) {
189 struct led_uc_plat *uc_plat;
190 struct udevice *dev;
191 const char *label;
192 int ret;
193
194 label = fdt_getprop(blob, node, "label", NULL);
195 if (!label) {
196 debug("%s: node %s has no label\n", __func__,
197 fdt_get_name(blob, node, NULL));
198 return -EINVAL;
199 }
200
201 ret = device_bind_driver_to_node(parent, "bcm6358-led",
202 fdt_get_name(blob, node, NULL),
203 node, &dev);
204 if (ret)
205 return ret;
206
207 uc_plat = dev_get_uclass_platdata(dev);
208 uc_plat->label = label;
209 }
210
211 return 0;
212}
213
214static const struct udevice_id bcm6358_led_ids[] = {
215 { .compatible = "brcm,bcm6358-leds" },
216 { /* sentinel */ }
217};
218
219U_BOOT_DRIVER(bcm6358_led) = {
220 .name = "bcm6358-led",
221 .id = UCLASS_LED,
222 .of_match = bcm6358_led_ids,
223 .bind = bcm6358_led_bind,
224 .probe = bcm6358_led_probe,
225 .priv_auto_alloc_size = sizeof(struct bcm6358_led_priv),
226 .ops = &bcm6358_led_ops,
227};