mediatek: add support for reset gpio
[openwrt/staging/hauke.git] / target / linux / mediatek / files-5.10 / drivers / leds / leds-ubnt-ledbar.c
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/delay.h>
4 #include <linux/i2c.h>
5 #include <linux/init.h>
6 #include <linux/leds.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/of.h>
10 #include <linux/of_gpio.h>
11 #include <linux/gpio/consumer.h>
12
13 /**
14 * Driver for the Ubiquiti RGB LED controller (LEDBAR).
15 * This Controller is based on a Holtek HT32F52241 and connected
16 * via I2C.
17 *
18 * - The Controller needs an enable signal set to high when
19 * performing a transaction. On the U6-LR, this is located
20 * at Pin 18 (R6902)
21 *
22 * - The Pin is also printed when calling the "usetled" function
23 * contained in the ubntapp bootloader application.
24 */
25
26 #define UBNT_LEDBAR_MAX_BRIGHTNESS 0xff
27
28 #define UBNT_LEDBAR_TRANSACTION_LENGTH 8
29 #define UBNT_LEDBAR_TRANSACTION_SUCCESS (char) 0xaa
30
31 #define UBNT_LEDBAR_TRANSACTION_BLUE_IDX 2
32 #define UBNT_LEDBAR_TRANSACTION_GREEN_IDX 3
33 #define UBNT_LEDBAR_TRANSACTION_RED_IDX 4
34
35 struct ubnt_ledbar {
36 struct mutex lock;
37 struct i2c_client *client;
38 struct led_classdev led_red;
39 struct led_classdev led_green;
40 struct led_classdev led_blue;
41 struct gpio_desc *enable_gpio;
42 struct gpio_desc *reset_gpio;
43 };
44
45 static void ubnt_ledbar_perform_transaction(struct ubnt_ledbar *ledbar,
46 const char *transaction, int len,
47 char *result, int result_len)
48 {
49 int i;
50
51 for (i = 0; i < len; i++)
52 i2c_smbus_write_byte(ledbar->client, transaction[i]);
53
54 for (i = 0; i < result_len; i++)
55 result[i] = i2c_smbus_read_byte(ledbar->client);
56 }
57
58 static int ubnt_ledbar_apply_state(struct ubnt_ledbar *ledbar)
59 {
60 char setup_msg[UBNT_LEDBAR_TRANSACTION_LENGTH] = {0x40, 0x10, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x11};
62 char led_msg[UBNT_LEDBAR_TRANSACTION_LENGTH] = {0x40, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x01, 0x00};
64 char i2c_response;
65 int ret = 0;
66
67 mutex_lock(&ledbar->lock);
68
69 led_msg[UBNT_LEDBAR_TRANSACTION_BLUE_IDX] = ledbar->led_blue.brightness;
70 led_msg[UBNT_LEDBAR_TRANSACTION_GREEN_IDX] = ledbar->led_green.brightness;
71 led_msg[UBNT_LEDBAR_TRANSACTION_RED_IDX] = ledbar->led_red.brightness;
72
73 gpiod_set_value(ledbar->enable_gpio, 1);
74
75 msleep(10);
76
77 ubnt_ledbar_perform_transaction(ledbar, setup_msg, sizeof(setup_msg), &i2c_response, sizeof(i2c_response));
78 if (i2c_response != UBNT_LEDBAR_TRANSACTION_SUCCESS) {
79 dev_err(&ledbar->client->dev, "Error initializing LED transaction: %02hhx\n", i2c_response);
80 ret = -EINVAL;
81 goto out_gpio;
82 }
83
84 ubnt_ledbar_perform_transaction(ledbar, led_msg, sizeof(led_msg), &i2c_response, sizeof(i2c_response));
85 if (i2c_response != UBNT_LEDBAR_TRANSACTION_SUCCESS) {
86 dev_err(&ledbar->client->dev, "Failed LED transaction: %02hhx\n", i2c_response);
87 ret = -EINVAL;
88 goto out_gpio;
89 }
90
91 msleep(10);
92 out_gpio:
93 gpiod_set_value(ledbar->enable_gpio, 0);
94
95 mutex_unlock(&ledbar->lock);
96
97 return ret;
98 }
99
100 static void ubnt_ledbar_reset(struct ubnt_ledbar *ledbar)
101 {
102 if (!ledbar->reset_gpio)
103 return;
104
105 mutex_lock(&ledbar->lock);
106
107 gpiod_set_value(ledbar->reset_gpio, 1);
108 msleep(10);
109 gpiod_set_value(ledbar->reset_gpio, 0);
110
111 mutex_unlock(&ledbar->lock);
112 }
113
114 #define UBNT_LEDBAR_CONTROL_RGBS(name) \
115 static int ubnt_ledbar_set_##name##_brightness(struct led_classdev *led_cdev,\
116 enum led_brightness value) \
117 { \
118 struct ubnt_ledbar *ledbar = \
119 container_of(led_cdev, struct ubnt_ledbar, led_##name); \
120 int ret; \
121 led_cdev->brightness = value; \
122 ret = ubnt_ledbar_apply_state(ledbar); \
123 return ret; \
124 }
125
126 UBNT_LEDBAR_CONTROL_RGBS(red);
127 UBNT_LEDBAR_CONTROL_RGBS(green);
128 UBNT_LEDBAR_CONTROL_RGBS(blue);
129
130
131 static int ubnt_ledbar_init_led(struct device_node *np, struct ubnt_ledbar *ledbar,
132 struct led_classdev *led_cdev)
133 {
134 struct led_init_data init_data = {};
135 int ret;
136
137 if (!np)
138 return 0;
139
140 init_data.fwnode = of_fwnode_handle(np);
141
142 led_cdev->max_brightness = UBNT_LEDBAR_MAX_BRIGHTNESS;
143
144 ret = devm_led_classdev_register_ext(&ledbar->client->dev, led_cdev,
145 &init_data);
146 if (ret)
147 dev_err(&ledbar->client->dev, "led register err: %d\n", ret);
148
149 return ret;
150 }
151
152
153 static int ubnt_ledbar_probe(struct i2c_client *client,
154 const struct i2c_device_id *id)
155 {
156 struct device_node *np = client->dev.of_node;
157 struct ubnt_ledbar *ledbar;
158 int ret;
159
160 ledbar = devm_kzalloc(&client->dev, sizeof(*ledbar), GFP_KERNEL);
161 if (!ledbar)
162 return -ENOMEM;
163
164 ledbar->enable_gpio = devm_gpiod_get(&client->dev, "enable", GPIOD_OUT_LOW);
165
166 if (IS_ERR(ledbar->enable_gpio)) {
167 ret = PTR_ERR(ledbar->enable_gpio);
168 dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
169 return ret;
170 }
171
172 ledbar->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW);
173
174 if (IS_ERR(ledbar->reset_gpio)) {
175 ret = PTR_ERR(ledbar->reset_gpio);
176 dev_err(&client->dev, "Failed to get reset gpio: %d\n", ret);
177 return ret;
178 }
179
180 ledbar->client = client;
181
182 mutex_init(&ledbar->lock);
183
184 i2c_set_clientdata(client, ledbar);
185
186 // Reset and initialize the MCU
187 ubnt_ledbar_reset(ledbar);
188
189 ledbar->led_red.brightness_set_blocking = ubnt_ledbar_set_red_brightness;
190 ubnt_ledbar_init_led(of_get_child_by_name(np, "red"), ledbar, &ledbar->led_red);
191
192 ledbar->led_green.brightness_set_blocking = ubnt_ledbar_set_green_brightness;
193 ubnt_ledbar_init_led(of_get_child_by_name(np, "green"), ledbar, &ledbar->led_green);
194
195 ledbar->led_blue.brightness_set_blocking = ubnt_ledbar_set_blue_brightness;
196 ubnt_ledbar_init_led(of_get_child_by_name(np, "blue"), ledbar, &ledbar->led_blue);
197
198 return ubnt_ledbar_apply_state(ledbar);
199 }
200
201 static int ubnt_ledbar_remove(struct i2c_client *client)
202 {
203 struct ubnt_ledbar *ledbar = i2c_get_clientdata(client);
204
205 mutex_destroy(&ledbar->lock);
206
207 return 0;
208 }
209
210 static const struct i2c_device_id ubnt_ledbar_id[] = {
211 { "ubnt-ledbar", 0 },
212 { }
213 };
214 MODULE_DEVICE_TABLE(i2c, ubnt_ledbar_id);
215
216 static const struct of_device_id of_ubnt_ledbar_match[] = {
217 { .compatible = "ubnt,ledbar", },
218 {},
219 };
220 MODULE_DEVICE_TABLE(of, of_ubnt_ledbar_match);
221
222 static struct i2c_driver ubnt_ledbar_driver = {
223 .driver = {
224 .name = "ubnt-ledbar",
225 .of_match_table = of_ubnt_ledbar_match,
226 },
227 .probe = ubnt_ledbar_probe,
228 .remove = ubnt_ledbar_remove,
229 .id_table = ubnt_ledbar_id,
230 };
231 module_i2c_driver(ubnt_ledbar_driver);
232
233 MODULE_DESCRIPTION("Ubiquiti LEDBAR driver");
234 MODULE_AUTHOR("David Bauer <mail@david-bauer.net>");
235 MODULE_LICENSE("GPL v2");