Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / chrome / cros_ec_lightbar.c
blobde8dfb12e48633f17b85ece80689bcdceed970bb
1 // SPDX-License-Identifier: GPL-2.0+
2 // Expose the Chromebook Pixel lightbar to userspace
3 //
4 // Copyright (C) 2014 Google, Inc.
6 #include <linux/ctype.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/fs.h>
10 #include <linux/kobject.h>
11 #include <linux/module.h>
12 #include <linux/platform_data/cros_ec_commands.h>
13 #include <linux/platform_data/cros_ec_proto.h>
14 #include <linux/platform_device.h>
15 #include <linux/sched.h>
16 #include <linux/types.h>
17 #include <linux/uaccess.h>
18 #include <linux/slab.h>
20 #define DRV_NAME "cros-ec-lightbar"
22 /* Rate-limit the lightbar interface to prevent DoS. */
23 static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
26 * Whether or not we have given userspace control of the lightbar.
27 * If this is true, we won't do anything during suspend/resume.
29 static bool userspace_control;
31 static ssize_t interval_msec_show(struct device *dev,
32 struct device_attribute *attr, char *buf)
34 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
36 return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
39 static ssize_t interval_msec_store(struct device *dev,
40 struct device_attribute *attr,
41 const char *buf, size_t count)
43 unsigned long msec;
45 if (kstrtoul(buf, 0, &msec))
46 return -EINVAL;
48 lb_interval_jiffies = msec * HZ / 1000;
50 return count;
53 static DEFINE_MUTEX(lb_mutex);
54 /* Return 0 if able to throttle correctly, error otherwise */
55 static int lb_throttle(void)
57 static unsigned long last_access;
58 unsigned long now, next_timeslot;
59 long delay;
60 int ret = 0;
62 mutex_lock(&lb_mutex);
64 now = jiffies;
65 next_timeslot = last_access + lb_interval_jiffies;
67 if (time_before(now, next_timeslot)) {
68 delay = (long)(next_timeslot) - (long)now;
69 set_current_state(TASK_INTERRUPTIBLE);
70 if (schedule_timeout(delay) > 0) {
71 /* interrupted - just abort */
72 ret = -EINTR;
73 goto out;
75 now = jiffies;
78 last_access = now;
79 out:
80 mutex_unlock(&lb_mutex);
82 return ret;
85 static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
87 struct cros_ec_command *msg;
88 int len;
90 len = max(sizeof(struct ec_params_lightbar),
91 sizeof(struct ec_response_lightbar));
93 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
94 if (!msg)
95 return NULL;
97 msg->version = 0;
98 msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
99 msg->outsize = sizeof(struct ec_params_lightbar);
100 msg->insize = sizeof(struct ec_response_lightbar);
102 return msg;
105 static int get_lightbar_version(struct cros_ec_dev *ec,
106 uint32_t *ver_ptr, uint32_t *flg_ptr)
108 struct ec_params_lightbar *param;
109 struct ec_response_lightbar *resp;
110 struct cros_ec_command *msg;
111 int ret;
113 msg = alloc_lightbar_cmd_msg(ec);
114 if (!msg)
115 return 0;
117 param = (struct ec_params_lightbar *)msg->data;
118 param->cmd = LIGHTBAR_CMD_VERSION;
119 msg->outsize = sizeof(param->cmd);
120 msg->result = sizeof(resp->version);
121 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
122 if (ret < 0 && ret != -EINVAL) {
123 ret = 0;
124 goto exit;
127 switch (msg->result) {
128 case EC_RES_INVALID_PARAM:
129 /* Pixel had no version command. */
130 if (ver_ptr)
131 *ver_ptr = 0;
132 if (flg_ptr)
133 *flg_ptr = 0;
134 ret = 1;
135 goto exit;
137 case EC_RES_SUCCESS:
138 resp = (struct ec_response_lightbar *)msg->data;
140 /* Future devices w/lightbars should implement this command */
141 if (ver_ptr)
142 *ver_ptr = resp->version.num;
143 if (flg_ptr)
144 *flg_ptr = resp->version.flags;
145 ret = 1;
146 goto exit;
149 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
150 ret = 0;
151 exit:
152 kfree(msg);
153 return ret;
156 static ssize_t version_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 uint32_t version = 0, flags = 0;
160 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
161 int ret;
163 ret = lb_throttle();
164 if (ret)
165 return ret;
167 /* This should always succeed, because we check during init. */
168 if (!get_lightbar_version(ec, &version, &flags))
169 return -EIO;
171 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
174 static ssize_t brightness_store(struct device *dev,
175 struct device_attribute *attr,
176 const char *buf, size_t count)
178 struct ec_params_lightbar *param;
179 struct cros_ec_command *msg;
180 int ret;
181 unsigned int val;
182 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
184 if (kstrtouint(buf, 0, &val))
185 return -EINVAL;
187 msg = alloc_lightbar_cmd_msg(ec);
188 if (!msg)
189 return -ENOMEM;
191 param = (struct ec_params_lightbar *)msg->data;
192 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
193 param->set_brightness.num = val;
194 ret = lb_throttle();
195 if (ret)
196 goto exit;
198 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
199 if (ret < 0)
200 goto exit;
202 ret = count;
203 exit:
204 kfree(msg);
205 return ret;
210 * We expect numbers, and we'll keep reading until we find them, skipping over
211 * any whitespace (sysfs guarantees that the input is null-terminated). Every
212 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
213 * parsing error, if we don't parse any numbers, or if we have numbers left
214 * over.
216 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
217 const char *buf, size_t count)
219 struct ec_params_lightbar *param;
220 struct cros_ec_command *msg;
221 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
222 unsigned int val[4];
223 int ret, i = 0, j = 0, ok = 0;
225 msg = alloc_lightbar_cmd_msg(ec);
226 if (!msg)
227 return -ENOMEM;
229 do {
230 /* Skip any whitespace */
231 while (*buf && isspace(*buf))
232 buf++;
234 if (!*buf)
235 break;
237 ret = sscanf(buf, "%i", &val[i++]);
238 if (ret == 0)
239 goto exit;
241 if (i == 4) {
242 param = (struct ec_params_lightbar *)msg->data;
243 param->cmd = LIGHTBAR_CMD_SET_RGB;
244 param->set_rgb.led = val[0];
245 param->set_rgb.red = val[1];
246 param->set_rgb.green = val[2];
247 param->set_rgb.blue = val[3];
249 * Throttle only the first of every four transactions,
250 * so that the user can update all four LEDs at once.
252 if ((j++ % 4) == 0) {
253 ret = lb_throttle();
254 if (ret)
255 goto exit;
258 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
259 if (ret < 0)
260 goto exit;
262 i = 0;
263 ok = 1;
266 /* Skip over the number we just read */
267 while (*buf && !isspace(*buf))
268 buf++;
270 } while (*buf);
272 exit:
273 kfree(msg);
274 return (ok && i == 0) ? count : -EINVAL;
277 static char const *seqname[] = {
278 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
279 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
280 "TAP", "PROGRAM",
283 static ssize_t sequence_show(struct device *dev,
284 struct device_attribute *attr, char *buf)
286 struct ec_params_lightbar *param;
287 struct ec_response_lightbar *resp;
288 struct cros_ec_command *msg;
289 int ret;
290 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
292 msg = alloc_lightbar_cmd_msg(ec);
293 if (!msg)
294 return -ENOMEM;
296 param = (struct ec_params_lightbar *)msg->data;
297 param->cmd = LIGHTBAR_CMD_GET_SEQ;
298 ret = lb_throttle();
299 if (ret)
300 goto exit;
302 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
303 if (ret < 0) {
304 ret = scnprintf(buf, PAGE_SIZE, "XFER / EC ERROR %d / %d\n",
305 ret, msg->result);
306 goto exit;
309 resp = (struct ec_response_lightbar *)msg->data;
310 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
311 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
312 else
313 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
314 seqname[resp->get_seq.num]);
316 exit:
317 kfree(msg);
318 return ret;
321 static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
323 struct ec_params_lightbar *param;
324 struct cros_ec_command *msg;
325 int ret;
327 msg = alloc_lightbar_cmd_msg(ec);
328 if (!msg)
329 return -ENOMEM;
331 param = (struct ec_params_lightbar *)msg->data;
332 param->cmd = cmd;
334 ret = lb_throttle();
335 if (ret)
336 goto error;
338 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
339 if (ret < 0)
340 goto error;
342 ret = 0;
343 error:
344 kfree(msg);
346 return ret;
349 static int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
351 struct ec_params_lightbar *param;
352 struct cros_ec_command *msg;
353 int ret;
355 msg = alloc_lightbar_cmd_msg(ec);
356 if (!msg)
357 return -ENOMEM;
359 param = (struct ec_params_lightbar *)msg->data;
361 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
362 param->manual_suspend_ctrl.enable = enable;
364 ret = lb_throttle();
365 if (ret)
366 goto error;
368 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
369 if (ret < 0)
370 goto error;
372 ret = 0;
373 error:
374 kfree(msg);
376 return ret;
379 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t count)
382 struct ec_params_lightbar *param;
383 struct cros_ec_command *msg;
384 unsigned int num;
385 int ret, len;
386 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
388 for (len = 0; len < count; len++)
389 if (!isalnum(buf[len]))
390 break;
392 for (num = 0; num < ARRAY_SIZE(seqname); num++)
393 if (!strncasecmp(seqname[num], buf, len))
394 break;
396 if (num >= ARRAY_SIZE(seqname)) {
397 ret = kstrtouint(buf, 0, &num);
398 if (ret)
399 return ret;
402 msg = alloc_lightbar_cmd_msg(ec);
403 if (!msg)
404 return -ENOMEM;
406 param = (struct ec_params_lightbar *)msg->data;
407 param->cmd = LIGHTBAR_CMD_SEQ;
408 param->seq.num = num;
409 ret = lb_throttle();
410 if (ret)
411 goto exit;
413 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
414 if (ret < 0)
415 goto exit;
417 ret = count;
418 exit:
419 kfree(msg);
420 return ret;
423 static ssize_t program_store(struct device *dev, struct device_attribute *attr,
424 const char *buf, size_t count)
426 int extra_bytes, max_size, ret;
427 struct ec_params_lightbar *param;
428 struct cros_ec_command *msg;
429 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
432 * We might need to reject the program for size reasons. The EC
433 * enforces a maximum program size, but we also don't want to try
434 * and send a program that is too big for the protocol. In order
435 * to ensure the latter, we also need to ensure we have extra bytes
436 * to represent the rest of the packet.
438 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
439 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
440 if (count > max_size) {
441 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
442 (unsigned int)count, max_size);
444 return -EINVAL;
447 msg = alloc_lightbar_cmd_msg(ec);
448 if (!msg)
449 return -ENOMEM;
451 ret = lb_throttle();
452 if (ret)
453 goto exit;
455 dev_info(dev, "Copying %zu byte program to EC", count);
457 param = (struct ec_params_lightbar *)msg->data;
458 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
460 param->set_program.size = count;
461 memcpy(param->set_program.data, buf, count);
464 * We need to set the message size manually or else it will use
465 * EC_LB_PROG_LEN. This might be too long, and the program
466 * is unlikely to use all of the space.
468 msg->outsize = count + extra_bytes;
470 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
471 if (ret < 0)
472 goto exit;
474 ret = count;
475 exit:
476 kfree(msg);
478 return ret;
481 static ssize_t userspace_control_show(struct device *dev,
482 struct device_attribute *attr,
483 char *buf)
485 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
488 static ssize_t userspace_control_store(struct device *dev,
489 struct device_attribute *attr,
490 const char *buf,
491 size_t count)
493 bool enable;
494 int ret;
496 ret = strtobool(buf, &enable);
497 if (ret < 0)
498 return ret;
500 userspace_control = enable;
502 return count;
505 /* Module initialization */
507 static DEVICE_ATTR_RW(interval_msec);
508 static DEVICE_ATTR_RO(version);
509 static DEVICE_ATTR_WO(brightness);
510 static DEVICE_ATTR_WO(led_rgb);
511 static DEVICE_ATTR_RW(sequence);
512 static DEVICE_ATTR_WO(program);
513 static DEVICE_ATTR_RW(userspace_control);
515 static struct attribute *__lb_cmds_attrs[] = {
516 &dev_attr_interval_msec.attr,
517 &dev_attr_version.attr,
518 &dev_attr_brightness.attr,
519 &dev_attr_led_rgb.attr,
520 &dev_attr_sequence.attr,
521 &dev_attr_program.attr,
522 &dev_attr_userspace_control.attr,
523 NULL,
526 static struct attribute_group cros_ec_lightbar_attr_group = {
527 .name = "lightbar",
528 .attrs = __lb_cmds_attrs,
531 static int cros_ec_lightbar_probe(struct platform_device *pd)
533 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
534 struct cros_ec_platform *pdata = dev_get_platdata(ec_dev->dev);
535 struct device *dev = &pd->dev;
536 int ret;
539 * Only instantiate the lightbar if the EC name is 'cros_ec'. Other EC
540 * devices like 'cros_pd' doesn't have a lightbar.
542 if (strcmp(pdata->ec_name, CROS_EC_DEV_NAME) != 0)
543 return -ENODEV;
546 * Ask then for the lightbar version, if it's 0 then the 'cros_ec'
547 * doesn't have a lightbar.
549 if (!get_lightbar_version(ec_dev, NULL, NULL))
550 return -ENODEV;
552 /* Take control of the lightbar from the EC. */
553 lb_manual_suspend_ctrl(ec_dev, 1);
555 ret = sysfs_create_group(&ec_dev->class_dev.kobj,
556 &cros_ec_lightbar_attr_group);
557 if (ret < 0)
558 dev_err(dev, "failed to create %s attributes. err=%d\n",
559 cros_ec_lightbar_attr_group.name, ret);
561 return ret;
564 static int cros_ec_lightbar_remove(struct platform_device *pd)
566 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
568 sysfs_remove_group(&ec_dev->class_dev.kobj,
569 &cros_ec_lightbar_attr_group);
571 /* Let the EC take over the lightbar again. */
572 lb_manual_suspend_ctrl(ec_dev, 0);
574 return 0;
577 static int __maybe_unused cros_ec_lightbar_resume(struct device *dev)
579 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
581 if (userspace_control)
582 return 0;
584 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_RESUME);
587 static int __maybe_unused cros_ec_lightbar_suspend(struct device *dev)
589 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
591 if (userspace_control)
592 return 0;
594 return lb_send_empty_cmd(ec_dev, LIGHTBAR_CMD_SUSPEND);
597 static SIMPLE_DEV_PM_OPS(cros_ec_lightbar_pm_ops,
598 cros_ec_lightbar_suspend, cros_ec_lightbar_resume);
600 static struct platform_driver cros_ec_lightbar_driver = {
601 .driver = {
602 .name = DRV_NAME,
603 .pm = &cros_ec_lightbar_pm_ops,
605 .probe = cros_ec_lightbar_probe,
606 .remove = cros_ec_lightbar_remove,
609 module_platform_driver(cros_ec_lightbar_driver);
611 MODULE_LICENSE("GPL");
612 MODULE_DESCRIPTION("Expose the Chromebook Pixel's lightbar to userspace");
613 MODULE_ALIAS("platform:" DRV_NAME);