of/platform: Initialise default DMA masks
[linux/fpc-iii.git] / drivers / platform / chrome / cros_ec_lightbar.c
blob68193bb53383687ebcb75e35df3c017181dabec2
1 /*
2 * cros_ec_lightbar - expose the Chromebook Pixel lightbar to userspace
4 * Copyright (C) 2014 Google, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #define pr_fmt(fmt) "cros_ec_lightbar: " fmt
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/fs.h>
26 #include <linux/kobject.h>
27 #include <linux/mfd/cros_ec.h>
28 #include <linux/mfd/cros_ec_commands.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/sched.h>
32 #include <linux/types.h>
33 #include <linux/uaccess.h>
34 #include <linux/slab.h>
36 /* Rate-limit the lightbar interface to prevent DoS. */
37 static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
40 * Whether or not we have given userspace control of the lightbar.
41 * If this is true, we won't do anything during suspend/resume.
43 static bool userspace_control;
44 static struct cros_ec_dev *ec_with_lightbar;
46 static ssize_t interval_msec_show(struct device *dev,
47 struct device_attribute *attr, char *buf)
49 unsigned long msec = lb_interval_jiffies * 1000 / HZ;
51 return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
54 static ssize_t interval_msec_store(struct device *dev,
55 struct device_attribute *attr,
56 const char *buf, size_t count)
58 unsigned long msec;
60 if (kstrtoul(buf, 0, &msec))
61 return -EINVAL;
63 lb_interval_jiffies = msec * HZ / 1000;
65 return count;
68 static DEFINE_MUTEX(lb_mutex);
69 /* Return 0 if able to throttle correctly, error otherwise */
70 static int lb_throttle(void)
72 static unsigned long last_access;
73 unsigned long now, next_timeslot;
74 long delay;
75 int ret = 0;
77 mutex_lock(&lb_mutex);
79 now = jiffies;
80 next_timeslot = last_access + lb_interval_jiffies;
82 if (time_before(now, next_timeslot)) {
83 delay = (long)(next_timeslot) - (long)now;
84 set_current_state(TASK_INTERRUPTIBLE);
85 if (schedule_timeout(delay) > 0) {
86 /* interrupted - just abort */
87 ret = -EINTR;
88 goto out;
90 now = jiffies;
93 last_access = now;
94 out:
95 mutex_unlock(&lb_mutex);
97 return ret;
100 static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
102 struct cros_ec_command *msg;
103 int len;
105 len = max(sizeof(struct ec_params_lightbar),
106 sizeof(struct ec_response_lightbar));
108 msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
109 if (!msg)
110 return NULL;
112 msg->version = 0;
113 msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
114 msg->outsize = sizeof(struct ec_params_lightbar);
115 msg->insize = sizeof(struct ec_response_lightbar);
117 return msg;
120 static int get_lightbar_version(struct cros_ec_dev *ec,
121 uint32_t *ver_ptr, uint32_t *flg_ptr)
123 struct ec_params_lightbar *param;
124 struct ec_response_lightbar *resp;
125 struct cros_ec_command *msg;
126 int ret;
128 msg = alloc_lightbar_cmd_msg(ec);
129 if (!msg)
130 return 0;
132 param = (struct ec_params_lightbar *)msg->data;
133 param->cmd = LIGHTBAR_CMD_VERSION;
134 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
135 if (ret < 0) {
136 ret = 0;
137 goto exit;
140 switch (msg->result) {
141 case EC_RES_INVALID_PARAM:
142 /* Pixel had no version command. */
143 if (ver_ptr)
144 *ver_ptr = 0;
145 if (flg_ptr)
146 *flg_ptr = 0;
147 ret = 1;
148 goto exit;
150 case EC_RES_SUCCESS:
151 resp = (struct ec_response_lightbar *)msg->data;
153 /* Future devices w/lightbars should implement this command */
154 if (ver_ptr)
155 *ver_ptr = resp->version.num;
156 if (flg_ptr)
157 *flg_ptr = resp->version.flags;
158 ret = 1;
159 goto exit;
162 /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
163 ret = 0;
164 exit:
165 kfree(msg);
166 return ret;
169 static ssize_t version_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 uint32_t version = 0, flags = 0;
173 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
174 int ret;
176 ret = lb_throttle();
177 if (ret)
178 return ret;
180 /* This should always succeed, because we check during init. */
181 if (!get_lightbar_version(ec, &version, &flags))
182 return -EIO;
184 return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
187 static ssize_t brightness_store(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
191 struct ec_params_lightbar *param;
192 struct cros_ec_command *msg;
193 int ret;
194 unsigned int val;
195 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
197 if (kstrtouint(buf, 0, &val))
198 return -EINVAL;
200 msg = alloc_lightbar_cmd_msg(ec);
201 if (!msg)
202 return -ENOMEM;
204 param = (struct ec_params_lightbar *)msg->data;
205 param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
206 param->set_brightness.num = val;
207 ret = lb_throttle();
208 if (ret)
209 goto exit;
211 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
212 if (ret < 0)
213 goto exit;
215 if (msg->result != EC_RES_SUCCESS) {
216 ret = -EINVAL;
217 goto exit;
220 ret = count;
221 exit:
222 kfree(msg);
223 return ret;
228 * We expect numbers, and we'll keep reading until we find them, skipping over
229 * any whitespace (sysfs guarantees that the input is null-terminated). Every
230 * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
231 * parsing error, if we don't parse any numbers, or if we have numbers left
232 * over.
234 static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct ec_params_lightbar *param;
238 struct cros_ec_command *msg;
239 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
240 unsigned int val[4];
241 int ret, i = 0, j = 0, ok = 0;
243 msg = alloc_lightbar_cmd_msg(ec);
244 if (!msg)
245 return -ENOMEM;
247 do {
248 /* Skip any whitespace */
249 while (*buf && isspace(*buf))
250 buf++;
252 if (!*buf)
253 break;
255 ret = sscanf(buf, "%i", &val[i++]);
256 if (ret == 0)
257 goto exit;
259 if (i == 4) {
260 param = (struct ec_params_lightbar *)msg->data;
261 param->cmd = LIGHTBAR_CMD_SET_RGB;
262 param->set_rgb.led = val[0];
263 param->set_rgb.red = val[1];
264 param->set_rgb.green = val[2];
265 param->set_rgb.blue = val[3];
267 * Throttle only the first of every four transactions,
268 * so that the user can update all four LEDs at once.
270 if ((j++ % 4) == 0) {
271 ret = lb_throttle();
272 if (ret)
273 goto exit;
276 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
277 if (ret < 0)
278 goto exit;
280 if (msg->result != EC_RES_SUCCESS)
281 goto exit;
283 i = 0;
284 ok = 1;
287 /* Skip over the number we just read */
288 while (*buf && !isspace(*buf))
289 buf++;
291 } while (*buf);
293 exit:
294 kfree(msg);
295 return (ok && i == 0) ? count : -EINVAL;
298 static char const *seqname[] = {
299 "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
300 "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
301 "TAP", "PROGRAM",
304 static ssize_t sequence_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct ec_params_lightbar *param;
308 struct ec_response_lightbar *resp;
309 struct cros_ec_command *msg;
310 int ret;
311 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
313 msg = alloc_lightbar_cmd_msg(ec);
314 if (!msg)
315 return -ENOMEM;
317 param = (struct ec_params_lightbar *)msg->data;
318 param->cmd = LIGHTBAR_CMD_GET_SEQ;
319 ret = lb_throttle();
320 if (ret)
321 goto exit;
323 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
324 if (ret < 0)
325 goto exit;
327 if (msg->result != EC_RES_SUCCESS) {
328 ret = scnprintf(buf, PAGE_SIZE,
329 "ERROR: EC returned %d\n", msg->result);
330 goto exit;
333 resp = (struct ec_response_lightbar *)msg->data;
334 if (resp->get_seq.num >= ARRAY_SIZE(seqname))
335 ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
336 else
337 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
338 seqname[resp->get_seq.num]);
340 exit:
341 kfree(msg);
342 return ret;
345 static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
347 struct ec_params_lightbar *param;
348 struct cros_ec_command *msg;
349 int ret;
351 msg = alloc_lightbar_cmd_msg(ec);
352 if (!msg)
353 return -ENOMEM;
355 param = (struct ec_params_lightbar *)msg->data;
356 param->cmd = cmd;
358 ret = lb_throttle();
359 if (ret)
360 goto error;
362 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
363 if (ret < 0)
364 goto error;
365 if (msg->result != EC_RES_SUCCESS) {
366 ret = -EINVAL;
367 goto error;
369 ret = 0;
370 error:
371 kfree(msg);
373 return ret;
376 int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
378 struct ec_params_lightbar *param;
379 struct cros_ec_command *msg;
380 int ret;
382 if (ec != ec_with_lightbar)
383 return 0;
385 msg = alloc_lightbar_cmd_msg(ec);
386 if (!msg)
387 return -ENOMEM;
389 param = (struct ec_params_lightbar *)msg->data;
391 param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
392 param->manual_suspend_ctrl.enable = enable;
394 ret = lb_throttle();
395 if (ret)
396 goto error;
398 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
399 if (ret < 0)
400 goto error;
401 if (msg->result != EC_RES_SUCCESS) {
402 ret = -EINVAL;
403 goto error;
405 ret = 0;
406 error:
407 kfree(msg);
409 return ret;
411 EXPORT_SYMBOL(lb_manual_suspend_ctrl);
413 int lb_suspend(struct cros_ec_dev *ec)
415 if (userspace_control || ec != ec_with_lightbar)
416 return 0;
418 return lb_send_empty_cmd(ec, LIGHTBAR_CMD_SUSPEND);
420 EXPORT_SYMBOL(lb_suspend);
422 int lb_resume(struct cros_ec_dev *ec)
424 if (userspace_control || ec != ec_with_lightbar)
425 return 0;
427 return lb_send_empty_cmd(ec, LIGHTBAR_CMD_RESUME);
429 EXPORT_SYMBOL(lb_resume);
431 static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
434 struct ec_params_lightbar *param;
435 struct cros_ec_command *msg;
436 unsigned int num;
437 int ret, len;
438 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
440 for (len = 0; len < count; len++)
441 if (!isalnum(buf[len]))
442 break;
444 for (num = 0; num < ARRAY_SIZE(seqname); num++)
445 if (!strncasecmp(seqname[num], buf, len))
446 break;
448 if (num >= ARRAY_SIZE(seqname)) {
449 ret = kstrtouint(buf, 0, &num);
450 if (ret)
451 return ret;
454 msg = alloc_lightbar_cmd_msg(ec);
455 if (!msg)
456 return -ENOMEM;
458 param = (struct ec_params_lightbar *)msg->data;
459 param->cmd = LIGHTBAR_CMD_SEQ;
460 param->seq.num = num;
461 ret = lb_throttle();
462 if (ret)
463 goto exit;
465 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
466 if (ret < 0)
467 goto exit;
469 if (msg->result != EC_RES_SUCCESS) {
470 ret = -EINVAL;
471 goto exit;
474 ret = count;
475 exit:
476 kfree(msg);
477 return ret;
480 static ssize_t program_store(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
483 int extra_bytes, max_size, ret;
484 struct ec_params_lightbar *param;
485 struct cros_ec_command *msg;
486 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
489 * We might need to reject the program for size reasons. The EC
490 * enforces a maximum program size, but we also don't want to try
491 * and send a program that is too big for the protocol. In order
492 * to ensure the latter, we also need to ensure we have extra bytes
493 * to represent the rest of the packet.
495 extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
496 max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
497 if (count > max_size) {
498 dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
499 (unsigned int)count, max_size);
501 return -EINVAL;
504 msg = alloc_lightbar_cmd_msg(ec);
505 if (!msg)
506 return -ENOMEM;
508 ret = lb_throttle();
509 if (ret)
510 goto exit;
512 dev_info(dev, "Copying %zu byte program to EC", count);
514 param = (struct ec_params_lightbar *)msg->data;
515 param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
517 param->set_program.size = count;
518 memcpy(param->set_program.data, buf, count);
521 * We need to set the message size manually or else it will use
522 * EC_LB_PROG_LEN. This might be too long, and the program
523 * is unlikely to use all of the space.
525 msg->outsize = count + extra_bytes;
527 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
528 if (ret < 0)
529 goto exit;
530 if (msg->result != EC_RES_SUCCESS) {
531 ret = -EINVAL;
532 goto exit;
535 ret = count;
536 exit:
537 kfree(msg);
539 return ret;
542 static ssize_t userspace_control_show(struct device *dev,
543 struct device_attribute *attr,
544 char *buf)
546 return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
549 static ssize_t userspace_control_store(struct device *dev,
550 struct device_attribute *attr,
551 const char *buf,
552 size_t count)
554 bool enable;
555 int ret;
557 ret = strtobool(buf, &enable);
558 if (ret < 0)
559 return ret;
561 userspace_control = enable;
563 return count;
566 /* Module initialization */
568 static DEVICE_ATTR_RW(interval_msec);
569 static DEVICE_ATTR_RO(version);
570 static DEVICE_ATTR_WO(brightness);
571 static DEVICE_ATTR_WO(led_rgb);
572 static DEVICE_ATTR_RW(sequence);
573 static DEVICE_ATTR_WO(program);
574 static DEVICE_ATTR_RW(userspace_control);
576 static struct attribute *__lb_cmds_attrs[] = {
577 &dev_attr_interval_msec.attr,
578 &dev_attr_version.attr,
579 &dev_attr_brightness.attr,
580 &dev_attr_led_rgb.attr,
581 &dev_attr_sequence.attr,
582 &dev_attr_program.attr,
583 &dev_attr_userspace_control.attr,
584 NULL,
587 bool ec_has_lightbar(struct cros_ec_dev *ec)
589 return !!get_lightbar_version(ec, NULL, NULL);
592 static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj,
593 struct attribute *a, int n)
595 struct device *dev = container_of(kobj, struct device, kobj);
596 struct cros_ec_dev *ec = to_cros_ec_dev(dev);
597 struct platform_device *pdev = to_platform_device(ec->dev);
598 struct cros_ec_platform *pdata = pdev->dev.platform_data;
599 int is_cros_ec;
601 is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME);
603 if (is_cros_ec != 0)
604 return 0;
606 /* Only instantiate this stuff if the EC has a lightbar */
607 if (ec_has_lightbar(ec)) {
608 ec_with_lightbar = ec;
609 return a->mode;
611 return 0;
614 struct attribute_group cros_ec_lightbar_attr_group = {
615 .name = "lightbar",
616 .attrs = __lb_cmds_attrs,
617 .is_visible = cros_ec_lightbar_attrs_are_visible,
619 EXPORT_SYMBOL(cros_ec_lightbar_attr_group);