mediatek: add patches for 5.15 and kernel config for mt7622
[openwrt/openwrt.git] / target / linux / mediatek / files / 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 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 };
43
44 static int ubnt_ledbar_perform_transaction(struct ubnt_ledbar *ledbar,
45 char *transaction)
46 {
47 int ret;
48 int i;
49
50 for (i = 0; i < UBNT_LEDBAR_TRANSACTION_LENGTH; i++)
51 i2c_smbus_write_byte(ledbar->client, transaction[i]);
52
53 return i2c_smbus_read_byte(ledbar->client);
54 }
55
56 static int ubnt_ledbar_apply_state(struct ubnt_ledbar *ledbar)
57 {
58 char setup_msg[UBNT_LEDBAR_TRANSACTION_LENGTH] = {0x40, 0x10, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x11};
60 char led_msg[UBNT_LEDBAR_TRANSACTION_LENGTH] = {0x40, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x01, 0x00};
62 char i2c_response;
63 int ret = 0;
64
65 mutex_lock(&ledbar->lock);
66
67 led_msg[UBNT_LEDBAR_TRANSACTION_BLUE_IDX] = ledbar->led_blue.brightness;
68 led_msg[UBNT_LEDBAR_TRANSACTION_GREEN_IDX] = ledbar->led_green.brightness;
69 led_msg[UBNT_LEDBAR_TRANSACTION_RED_IDX] = ledbar->led_red.brightness;
70
71 gpiod_set_raw_value(ledbar->enable_gpio, 1);
72
73 msleep(10);
74
75 i2c_response = ubnt_ledbar_perform_transaction(ledbar, setup_msg);
76 if (i2c_response != UBNT_LEDBAR_TRANSACTION_SUCCESS) {
77 dev_err(&ledbar->client->dev, "Error initializing LED transaction: %02x\n", ret);
78 ret = -EINVAL;
79 goto out_gpio;
80 }
81
82 i2c_response = ubnt_ledbar_perform_transaction(ledbar, led_msg);
83 if (i2c_response != UBNT_LEDBAR_TRANSACTION_SUCCESS) {
84 dev_err(&ledbar->client->dev, "Failed LED transaction: %02x\n", ret);
85 ret = -EINVAL;
86 goto out_gpio;
87 }
88
89 msleep(10);
90 out_gpio:
91 gpiod_set_raw_value(ledbar->enable_gpio, 0);
92
93 mutex_unlock(&ledbar->lock);
94
95 return ret;
96 }
97
98 #define UBNT_LEDBAR_CONTROL_RGBS(name) \
99 static int ubnt_ledbar_set_##name##_brightness(struct led_classdev *led_cdev,\
100 enum led_brightness value) \
101 { \
102 struct ubnt_ledbar *ledbar = \
103 container_of(led_cdev, struct ubnt_ledbar, led_##name); \
104 int ret; \
105 led_cdev->brightness = value; \
106 ret = ubnt_ledbar_apply_state(ledbar); \
107 return ret; \
108 }
109
110 UBNT_LEDBAR_CONTROL_RGBS(red);
111 UBNT_LEDBAR_CONTROL_RGBS(green);
112 UBNT_LEDBAR_CONTROL_RGBS(blue);
113
114
115 static int ubnt_ledbar_init_led(struct device_node *np, struct ubnt_ledbar *ledbar,
116 struct led_classdev *led_cdev)
117 {
118 struct led_init_data init_data = {};
119 int ret;
120
121 if (!np)
122 return 0;
123
124 init_data.fwnode = of_fwnode_handle(np);
125
126 led_cdev->max_brightness = UBNT_LEDBAR_MAX_BRIGHTNESS;
127
128 ret = devm_led_classdev_register_ext(&ledbar->client->dev, led_cdev,
129 &init_data);
130 if (ret)
131 dev_err(&ledbar->client->dev, "led register err: %d\n", ret);
132
133 return ret;
134 }
135
136
137 static int ubnt_ledbar_probe(struct i2c_client *client,
138 const struct i2c_device_id *id)
139 {
140 struct device_node *np = client->dev.of_node;
141 struct ubnt_ledbar *ledbar;
142 int ret;
143
144 ledbar = devm_kzalloc(&client->dev, sizeof(*ledbar), GFP_KERNEL);
145 if (!ledbar)
146 return -ENOMEM;
147
148 ledbar->enable_gpio = devm_gpiod_get(&client->dev, "enable", GPIOD_OUT_LOW);
149
150 if (IS_ERR(ledbar->enable_gpio)) {
151 ret = PTR_ERR(ledbar->enable_gpio);
152 dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
153 return ret;
154 }
155
156 gpiod_direction_output(ledbar->enable_gpio, 0);
157
158 ledbar->client = client;
159
160 mutex_init(&ledbar->lock);
161
162 i2c_set_clientdata(client, ledbar);
163
164 ledbar->led_red.brightness_set_blocking = ubnt_ledbar_set_red_brightness;
165 ubnt_ledbar_init_led(of_get_child_by_name(np, "red"), ledbar, &ledbar->led_red);
166
167 ledbar->led_green.brightness_set_blocking = ubnt_ledbar_set_green_brightness;
168 ubnt_ledbar_init_led(of_get_child_by_name(np, "green"), ledbar, &ledbar->led_green);
169
170 ledbar->led_blue.brightness_set_blocking = ubnt_ledbar_set_blue_brightness;
171 ubnt_ledbar_init_led(of_get_child_by_name(np, "blue"), ledbar, &ledbar->led_blue);
172
173 return ubnt_ledbar_apply_state(ledbar);
174 }
175
176 static int ubnt_ledbar_remove(struct i2c_client *client)
177 {
178 struct ubnt_ledbar *ledbar = i2c_get_clientdata(client);
179
180 mutex_destroy(&ledbar->lock);
181
182 return 0;
183 }
184
185 static const struct i2c_device_id ubnt_ledbar_id[] = {
186 { "ubnt-ledbar", 0 },
187 { }
188 };
189 MODULE_DEVICE_TABLE(i2c, ubnt_ledbar_id);
190
191 static const struct of_device_id of_ubnt_ledbar_match[] = {
192 { .compatible = "ubnt,ledbar", },
193 {},
194 };
195 MODULE_DEVICE_TABLE(of, of_ubnt_ledbar_match);
196
197 static struct i2c_driver ubnt_ledbar_driver = {
198 .driver = {
199 .name = "ubnt-ledbar",
200 .of_match_table = of_ubnt_ledbar_match,
201 },
202 .probe = ubnt_ledbar_probe,
203 .remove = ubnt_ledbar_remove,
204 .id_table = ubnt_ledbar_id,
205 };
206 module_i2c_driver(ubnt_ledbar_driver);
207
208 MODULE_DESCRIPTION("Ubiquiti LEDBAR driver");
209 MODULE_AUTHOR("David Bauer <mail@david-bauer.net>");
210 MODULE_LICENSE("GPL v2");