blob: 25aa3994d0e4a252edabe6706e977fd1ca266fb0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +02004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <led.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +020011#include <asm/io.h>
12#include <dm/lists.h>
Simon Glassc05ed002020-05-10 11:40:11 -060013#include <linux/delay.h>
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +020014
15#define LEDS_MAX 32
16#define LEDS_WAIT 100
17
18/* LED Mode register */
19#define LED_MODE_REG 0x0
20#define LED_MODE_OFF 0
21#define LED_MODE_ON 1
22#define LED_MODE_MASK 1
23
24/* LED Control register */
25#define LED_CTRL_REG 0x4
26#define LED_CTRL_CLK_MASK 0x3
27#define LED_CTRL_CLK_1 0
28#define LED_CTRL_CLK_2 1
29#define LED_CTRL_CLK_4 2
30#define LED_CTRL_CLK_8 3
31#define LED_CTRL_POL_SHIFT 2
32#define LED_CTRL_POL_MASK (1 << LED_CTRL_POL_SHIFT)
33#define LED_CTRL_BUSY_SHIFT 3
34#define LED_CTRL_BUSY_MASK (1 << LED_CTRL_BUSY_SHIFT)
35
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +020036struct bcm6358_led_priv {
37 void __iomem *regs;
38 uint8_t pin;
39 bool active_low;
40};
41
42static void bcm6358_led_busy(void __iomem *regs)
43{
44 while (readl_be(regs + LED_CTRL_REG) & LED_CTRL_BUSY_MASK)
45 udelay(LEDS_WAIT);
46}
47
48static unsigned long bcm6358_led_get_mode(struct bcm6358_led_priv *priv)
49{
50 bcm6358_led_busy(priv->regs);
51
52 return (readl_be(priv->regs + LED_MODE_REG) >> priv->pin) &
53 LED_MODE_MASK;
54}
55
56static int bcm6358_led_set_mode(struct bcm6358_led_priv *priv, uint8_t mode)
57{
58 bcm6358_led_busy(priv->regs);
59
60 clrsetbits_be32(priv->regs + LED_MODE_REG,
61 (LED_MODE_MASK << priv->pin),
62 (mode << priv->pin));
63
64 return 0;
65}
66
67static enum led_state_t bcm6358_led_get_state(struct udevice *dev)
68{
69 struct bcm6358_led_priv *priv = dev_get_priv(dev);
70 enum led_state_t state = LEDST_OFF;
71
72 switch (bcm6358_led_get_mode(priv)) {
73 case LED_MODE_OFF:
74 state = (priv->active_low ? LEDST_ON : LEDST_OFF);
75 break;
76 case LED_MODE_ON:
77 state = (priv->active_low ? LEDST_OFF : LEDST_ON);
78 break;
79 }
80
81 return state;
82}
83
84static int bcm6358_led_set_state(struct udevice *dev, enum led_state_t state)
85{
86 struct bcm6358_led_priv *priv = dev_get_priv(dev);
87 unsigned long mode;
88
89 switch (state) {
90 case LEDST_OFF:
91 mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF);
92 break;
93 case LEDST_ON:
94 mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON);
95 break;
96 case LEDST_TOGGLE:
97 if (bcm6358_led_get_state(dev) == LEDST_OFF)
98 return bcm6358_led_set_state(dev, LEDST_ON);
99 else
100 return bcm6358_led_set_state(dev, LEDST_OFF);
101 break;
102 default:
103 return -ENOSYS;
104 }
105
106 return bcm6358_led_set_mode(priv, mode);
107}
108
109static const struct led_ops bcm6358_led_ops = {
110 .get_state = bcm6358_led_get_state,
111 .set_state = bcm6358_led_set_state,
112};
113
114static int bcm6358_led_probe(struct udevice *dev)
115{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700116 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200117
118 /* Top-level LED node */
119 if (!uc_plat->label) {
120 void __iomem *regs;
121 unsigned int clk_div;
122 u32 set_bits = 0;
123
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100124 regs = dev_remap_addr(dev);
125 if (!regs)
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200126 return -EINVAL;
127
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100128 if (dev_read_bool(dev, "brcm,clk-dat-low"))
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200129 set_bits |= LED_CTRL_POL_MASK;
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100130 clk_div = dev_read_u32_default(dev, "brcm,clk-div",
131 LED_CTRL_CLK_1);
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200132 switch (clk_div) {
133 case 8:
134 set_bits |= LED_CTRL_CLK_8;
135 break;
136 case 4:
137 set_bits |= LED_CTRL_CLK_4;
138 break;
139 case 2:
140 set_bits |= LED_CTRL_CLK_2;
141 break;
142 default:
143 set_bits |= LED_CTRL_CLK_1;
144 break;
145 }
146
147 bcm6358_led_busy(regs);
148 clrsetbits_be32(regs + LED_CTRL_REG,
149 LED_CTRL_POL_MASK | LED_CTRL_CLK_MASK,
150 set_bits);
151 } else {
152 struct bcm6358_led_priv *priv = dev_get_priv(dev);
153 unsigned int pin;
154
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100155 priv->regs = dev_remap_addr(dev);
156 if (!priv->regs)
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200157 return -EINVAL;
158
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100159 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200160 if (pin >= LEDS_MAX)
161 return -EINVAL;
162
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200163 priv->pin = pin;
164
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100165 if (dev_read_bool(dev, "active-low"))
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200166 priv->active_low = true;
167 }
168
169 return 0;
170}
171
172static int bcm6358_led_bind(struct udevice *parent)
173{
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100174 ofnode node;
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200175
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100176 dev_for_each_subnode(node, parent) {
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200177 struct udevice *dev;
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200178 int ret;
179
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200180 ret = device_bind_driver_to_node(parent, "bcm6358-led",
Álvaro Fernández Rojasa15c16f2018-03-22 19:39:33 +0100181 ofnode_get_name(node),
182 node, &dev);
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200183 if (ret)
184 return ret;
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200185 }
186
187 return 0;
188}
189
190static const struct udevice_id bcm6358_led_ids[] = {
191 { .compatible = "brcm,bcm6358-leds" },
192 { /* sentinel */ }
193};
194
195U_BOOT_DRIVER(bcm6358_led) = {
196 .name = "bcm6358-led",
197 .id = UCLASS_LED,
198 .of_match = bcm6358_led_ids,
199 .bind = bcm6358_led_bind,
200 .probe = bcm6358_led_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700201 .priv_auto = sizeof(struct bcm6358_led_priv),
Álvaro Fernández Rojas632889c2017-05-07 20:11:30 +0200202 .ops = &bcm6358_led_ops,
203};