1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* ChromeOS EC LED Driver
*
* Copyright (C) 2024 Thomas Weißschuh <linux@weissschuh.net>
*/
#include <linux/device.h>
#include <linux/leds.h>
#include <linux/led-class-multicolor.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>
static const char * const cros_ec_led_functions[] = {
[EC_LED_ID_BATTERY_LED] = LED_FUNCTION_CHARGING,
[EC_LED_ID_POWER_LED] = LED_FUNCTION_POWER,
[EC_LED_ID_ADAPTER_LED] = "adapter",
[EC_LED_ID_LEFT_LED] = "left",
[EC_LED_ID_RIGHT_LED] = "right",
[EC_LED_ID_RECOVERY_HW_REINIT_LED] = "recovery-hw-reinit",
[EC_LED_ID_SYSRQ_DEBUG_LED] = "sysrq-debug",
};
static_assert(ARRAY_SIZE(cros_ec_led_functions) == EC_LED_ID_COUNT);
static const int cros_ec_led_to_linux_id[] = {
[EC_LED_COLOR_RED] = LED_COLOR_ID_RED,
[EC_LED_COLOR_GREEN] = LED_COLOR_ID_GREEN,
[EC_LED_COLOR_BLUE] = LED_COLOR_ID_BLUE,
[EC_LED_COLOR_YELLOW] = LED_COLOR_ID_YELLOW,
[EC_LED_COLOR_WHITE] = LED_COLOR_ID_WHITE,
[EC_LED_COLOR_AMBER] = LED_COLOR_ID_AMBER,
};
static_assert(ARRAY_SIZE(cros_ec_led_to_linux_id) == EC_LED_COLOR_COUNT);
static const int cros_ec_linux_to_ec_id[] = {
[LED_COLOR_ID_RED] = EC_LED_COLOR_RED,
[LED_COLOR_ID_GREEN] = EC_LED_COLOR_GREEN,
[LED_COLOR_ID_BLUE] = EC_LED_COLOR_BLUE,
[LED_COLOR_ID_YELLOW] = EC_LED_COLOR_YELLOW,
[LED_COLOR_ID_WHITE] = EC_LED_COLOR_WHITE,
[LED_COLOR_ID_AMBER] = EC_LED_COLOR_AMBER,
};
struct cros_ec_led_priv {
struct led_classdev_mc led_mc_cdev;
struct cros_ec_device *cros_ec;
enum ec_led_id led_id;
};
static inline struct cros_ec_led_priv *cros_ec_led_cdev_to_priv(struct led_classdev *led_cdev)
{
return container_of(lcdev_to_mccdev(led_cdev), struct cros_ec_led_priv, led_mc_cdev);
}
union cros_ec_led_cmd_data {
struct ec_params_led_control req;
struct ec_response_led_control resp;
} __packed;
static int cros_ec_led_send_cmd(struct cros_ec_device *cros_ec,
union cros_ec_led_cmd_data *arg)
{
int ret;
struct {
struct cros_ec_command msg;
union cros_ec_led_cmd_data data;
} __packed buf = {
.msg = {
.version = 1,
.command = EC_CMD_LED_CONTROL,
.insize = sizeof(arg->resp),
.outsize = sizeof(arg->req),
},
.data.req = arg->req
};
ret = cros_ec_cmd_xfer_status(cros_ec, &buf.msg);
if (ret < 0)
return ret;
arg->resp = buf.data.resp;
return 0;
}
static int cros_ec_led_trigger_activate(struct led_classdev *led_cdev)
{
struct cros_ec_led_priv *priv = cros_ec_led_cdev_to_priv(led_cdev);
union cros_ec_led_cmd_data arg = {};
arg.req.led_id = priv->led_id;
arg.req.flags = EC_LED_FLAGS_AUTO;
return cros_ec_led_send_cmd(priv->cros_ec, &arg);
}
static struct led_hw_trigger_type cros_ec_led_trigger_type;
static struct led_trigger cros_ec_led_trigger = {
.name = "chromeos-auto",
.trigger_type = &cros_ec_led_trigger_type,
.activate = cros_ec_led_trigger_activate,
};
static int cros_ec_led_brightness_set_blocking(struct led_classdev *led_cdev,
enum led_brightness brightness)
{
struct cros_ec_led_priv *priv = cros_ec_led_cdev_to_priv(led_cdev);
union cros_ec_led_cmd_data arg = {};
enum ec_led_colors led_color;
struct mc_subled *subled;
size_t i;
led_mc_calc_color_components(&priv->led_mc_cdev, brightness);
arg.req.led_id = priv->led_id;
for (i = 0; i < priv->led_mc_cdev.num_colors; i++) {
subled = &priv->led_mc_cdev.subled_info[i];
led_color = cros_ec_linux_to_ec_id[subled->color_index];
arg.req.brightness[led_color] = subled->brightness;
}
return cros_ec_led_send_cmd(priv->cros_ec, &arg);
}
static int cros_ec_led_count_subleds(struct device *dev,
struct ec_response_led_control *resp,
unsigned int *max_brightness)
{
unsigned int range, common_range = 0;
int num_subleds = 0;
size_t i;
for (i = 0; i < EC_LED_COLOR_COUNT; i++) {
range = resp->brightness_range[i];
if (!range)
continue;
num_subleds++;
if (!common_range)
common_range = range;
if (common_range != range) {
/* The multicolor LED API expects a uniform max_brightness */
dev_err(dev, "Inconsistent LED brightness values\n");
return -EINVAL;
}
}
if (!num_subleds)
return -EINVAL;
*max_brightness = common_range;
return num_subleds;
}
static const char *cros_ec_led_get_color_name(struct led_classdev_mc *led_mc_cdev)
{
int color;
if (led_mc_cdev->num_colors == 1)
color = led_mc_cdev->subled_info[0].color_index;
else
color = LED_COLOR_ID_MULTI;
return led_get_color_name(color);
}
static int cros_ec_led_probe_one(struct device *dev, struct cros_ec_device *cros_ec,
enum ec_led_id id)
{
union cros_ec_led_cmd_data arg = {};
struct cros_ec_led_priv *priv;
struct led_classdev *led_cdev;
struct mc_subled *subleds;
int i, ret, num_subleds;
size_t subled;
arg.req.led_id = id;
arg.req.flags = EC_LED_FLAGS_QUERY;
ret = cros_ec_led_send_cmd(cros_ec, &arg);
if (ret == -EINVAL)
return 0; /* Unknown LED, skip */
if (ret == -EOPNOTSUPP)
return -ENODEV;
if (ret < 0)
return ret;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
num_subleds = cros_ec_led_count_subleds(dev, &arg.resp,
&priv->led_mc_cdev.led_cdev.max_brightness);
if (num_subleds < 0)
return num_subleds;
priv->cros_ec = cros_ec;
priv->led_id = id;
subleds = devm_kcalloc(dev, num_subleds, sizeof(*subleds), GFP_KERNEL);
if (!subleds)
return -ENOMEM;
subled = 0;
for (i = 0; i < EC_LED_COLOR_COUNT; i++) {
if (!arg.resp.brightness_range[i])
continue;
subleds[subled].color_index = cros_ec_led_to_linux_id[i];
if (subled == 0)
subleds[subled].intensity = 100;
subled++;
}
priv->led_mc_cdev.subled_info = subleds;
priv->led_mc_cdev.num_colors = num_subleds;
led_cdev = &priv->led_mc_cdev.led_cdev;
led_cdev->brightness_set_blocking = cros_ec_led_brightness_set_blocking;
led_cdev->trigger_type = &cros_ec_led_trigger_type;
led_cdev->default_trigger = cros_ec_led_trigger.name;
led_cdev->hw_control_trigger = cros_ec_led_trigger.name;
led_cdev->name = devm_kasprintf(dev, GFP_KERNEL, "chromeos:%s:%s",
cros_ec_led_get_color_name(&priv->led_mc_cdev),
cros_ec_led_functions[id]);
if (!led_cdev->name)
return -ENOMEM;
return devm_led_classdev_multicolor_register(dev, &priv->led_mc_cdev);
}
static int cros_ec_led_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
struct cros_ec_device *cros_ec = ec_dev->ec_dev;
int i, ret = 0;
ret = devm_led_trigger_register(dev, &cros_ec_led_trigger);
if (ret)
return ret;
for (i = 0; i < EC_LED_ID_COUNT; i++) {
ret = cros_ec_led_probe_one(dev, cros_ec, i);
if (ret)
break;
}
return ret;
}
static const struct platform_device_id cros_ec_led_id[] = {
{ "cros-ec-led", 0 },
{}
};
static struct platform_driver cros_ec_led_driver = {
.driver.name = "cros-ec-led",
.probe = cros_ec_led_probe,
.id_table = cros_ec_led_id,
};
module_platform_driver(cros_ec_led_driver);
MODULE_DEVICE_TABLE(platform, cros_ec_led_id);
MODULE_DESCRIPTION("ChromeOS EC LED Driver");
MODULE_AUTHOR("Thomas Weißschuh <linux@weissschuh.net");
MODULE_LICENSE("GPL");
|