1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
5 * Copyright (C) 2014 Google, Inc.
9 #include <linux/mfd/core.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
18 #include "cros_ec_dev.h"
20 #define DRV_NAME "cros-ec-dev"
22 /* Device variables */
23 #define CROS_MAX_DEV 128
26 static struct class cros_class = {
31 /* Basic communication */
32 static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
34 struct ec_response_get_version *resp;
35 static const char * const current_image_name[] = {
36 "unknown", "read-only", "read-write", "invalid",
38 struct cros_ec_command *msg;
41 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
46 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
47 msg->insize = sizeof(*resp);
50 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
54 if (msg->result != EC_RES_SUCCESS) {
56 "%s\nUnknown EC version: EC returned %d\n",
57 CROS_EC_DEV_VERSION, msg->result);
62 resp = (struct ec_response_get_version *)msg->data;
63 if (resp->current_image >= ARRAY_SIZE(current_image_name))
64 resp->current_image = 3; /* invalid */
66 snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
67 resp->version_string_ro, resp->version_string_rw,
68 current_image_name[resp->current_image]);
76 static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
78 struct cros_ec_command *msg;
81 if (ec->features[0] == -1U && ec->features[1] == -1U) {
82 /* features bitmap not read yet */
84 msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
89 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
90 msg->insize = sizeof(ec->features);
93 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
94 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
95 dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
97 memset(ec->features, 0, sizeof(ec->features));
99 memcpy(ec->features, msg->data, sizeof(ec->features));
102 dev_dbg(ec->dev, "EC features %08x %08x\n",
103 ec->features[0], ec->features[1]);
108 return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
111 /* Device file ops */
112 static int ec_device_open(struct inode *inode, struct file *filp)
114 struct cros_ec_dev *ec = container_of(inode->i_cdev,
115 struct cros_ec_dev, cdev);
116 filp->private_data = ec;
117 nonseekable_open(inode, filp);
121 static int ec_device_release(struct inode *inode, struct file *filp)
126 static ssize_t ec_device_read(struct file *filp, char __user *buffer,
127 size_t length, loff_t *offset)
129 struct cros_ec_dev *ec = filp->private_data;
130 char msg[sizeof(struct ec_response_get_version) +
131 sizeof(CROS_EC_DEV_VERSION)];
138 ret = ec_get_version(ec, msg, sizeof(msg));
142 count = min(length, strlen(msg));
144 if (copy_to_user(buffer, msg, count))
152 static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
155 struct cros_ec_command u_cmd;
156 struct cros_ec_command *s_cmd;
158 if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
161 if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
162 (u_cmd.insize > EC_MAX_MSG_BYTES))
165 s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
170 if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
175 if (u_cmd.outsize != s_cmd->outsize ||
176 u_cmd.insize != s_cmd->insize) {
181 s_cmd->command += ec->cmd_offset;
182 ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
183 /* Only copy data to userland if data was received. */
187 if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
194 static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
196 struct cros_ec_device *ec_dev = ec->ec_dev;
197 struct cros_ec_readmem s_mem = { };
200 /* Not every platform supports direct reads */
201 if (!ec_dev->cmd_readmem)
204 if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
207 num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
212 if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
218 static long ec_device_ioctl(struct file *filp, unsigned int cmd,
221 struct cros_ec_dev *ec = filp->private_data;
223 if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
227 case CROS_EC_DEV_IOCXCMD:
228 return ec_device_ioctl_xcmd(ec, (void __user *)arg);
229 case CROS_EC_DEV_IOCRDMEM:
230 return ec_device_ioctl_readmem(ec, (void __user *)arg);
236 /* Module initialization */
237 static const struct file_operations fops = {
238 .open = ec_device_open,
239 .release = ec_device_release,
240 .read = ec_device_read,
241 .unlocked_ioctl = ec_device_ioctl,
243 .compat_ioctl = ec_device_ioctl,
247 static void cros_ec_class_release(struct device *dev)
249 kfree(to_cros_ec_dev(dev));
252 static void cros_ec_sensors_register(struct cros_ec_dev *ec)
255 * Issue a command to get the number of sensor reported.
256 * Build an array of sensors driver and register them all.
258 int ret, i, id, sensor_num;
259 struct mfd_cell *sensor_cells;
260 struct cros_ec_sensor_platform *sensor_platforms;
261 int sensor_type[MOTIONSENSE_TYPE_MAX];
262 struct ec_params_motion_sense *params;
263 struct ec_response_motion_sense *resp;
264 struct cros_ec_command *msg;
266 msg = kzalloc(sizeof(struct cros_ec_command) +
267 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
272 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
273 msg->outsize = sizeof(*params);
274 msg->insize = sizeof(*resp);
276 params = (struct ec_params_motion_sense *)msg->data;
277 params->cmd = MOTIONSENSE_CMD_DUMP;
279 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
280 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
281 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
286 resp = (struct ec_response_motion_sense *)msg->data;
287 sensor_num = resp->dump.sensor_count;
289 * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
291 sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
293 if (sensor_cells == NULL)
296 sensor_platforms = kcalloc(sensor_num,
297 sizeof(struct cros_ec_sensor_platform),
299 if (sensor_platforms == NULL)
300 goto error_platforms;
302 memset(sensor_type, 0, sizeof(sensor_type));
304 for (i = 0; i < sensor_num; i++) {
305 params->cmd = MOTIONSENSE_CMD_INFO;
306 params->info.sensor_num = i;
307 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
308 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
309 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
310 i, ret, msg->result);
313 switch (resp->info.type) {
314 case MOTIONSENSE_TYPE_ACCEL:
315 sensor_cells[id].name = "cros-ec-accel";
317 case MOTIONSENSE_TYPE_BARO:
318 sensor_cells[id].name = "cros-ec-baro";
320 case MOTIONSENSE_TYPE_GYRO:
321 sensor_cells[id].name = "cros-ec-gyro";
323 case MOTIONSENSE_TYPE_MAG:
324 sensor_cells[id].name = "cros-ec-mag";
326 case MOTIONSENSE_TYPE_PROX:
327 sensor_cells[id].name = "cros-ec-prox";
329 case MOTIONSENSE_TYPE_LIGHT:
330 sensor_cells[id].name = "cros-ec-light";
332 case MOTIONSENSE_TYPE_ACTIVITY:
333 sensor_cells[id].name = "cros-ec-activity";
336 dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
339 sensor_platforms[id].sensor_num = i;
340 sensor_cells[id].id = sensor_type[resp->info.type];
341 sensor_cells[id].platform_data = &sensor_platforms[id];
342 sensor_cells[id].pdata_size =
343 sizeof(struct cros_ec_sensor_platform);
345 sensor_type[resp->info.type]++;
349 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
350 ec->has_kb_wake_angle = true;
352 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
353 sensor_cells[id].name = "cros-ec-ring";
356 if (cros_ec_check_features(ec,
357 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
358 sensor_cells[id].name = "cros-ec-lid-angle";
362 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
365 dev_err(ec->dev, "failed to add EC sensors\n");
367 kfree(sensor_platforms);
374 static struct cros_ec_sensor_platform sensor_platforms[] = {
379 static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
381 .name = "cros-ec-accel-legacy",
382 .platform_data = &sensor_platforms[0],
383 .pdata_size = sizeof(struct cros_ec_sensor_platform),
386 .name = "cros-ec-accel-legacy",
387 .platform_data = &sensor_platforms[1],
388 .pdata_size = sizeof(struct cros_ec_sensor_platform),
392 static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
394 struct cros_ec_device *ec_dev = ec->ec_dev;
399 * ECs that need legacy support are the main EC, directly connected to
402 if (ec->cmd_offset != 0)
406 * Check if EC supports direct memory reads and if EC has
409 if (ec_dev->cmd_readmem) {
410 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
413 dev_warn(ec->dev, "EC direct read error.\n");
417 /* Check if EC has accelerometers. */
418 if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
419 dev_info(ec->dev, "EC does not have accelerometers.\n");
425 * The device may still support accelerometers:
426 * it would be an older ARM based device that do not suppor the
427 * EC_CMD_GET_FEATURES command.
429 * Register 2 accelerometers, we will fail in the IIO driver if there
432 ret = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
433 cros_ec_accel_legacy_cells,
434 ARRAY_SIZE(cros_ec_accel_legacy_cells),
437 dev_err(ec_dev->dev, "failed to add EC sensors\n");
440 static const struct mfd_cell cros_ec_cec_cells[] = {
441 { .name = "cros-ec-cec" }
444 static const struct mfd_cell cros_ec_rtc_cells[] = {
445 { .name = "cros-ec-rtc" }
448 static const struct mfd_cell cros_usbpd_charger_cells[] = {
449 { .name = "cros-usbpd-charger" },
450 { .name = "cros-usbpd-logger" },
453 static const struct mfd_cell cros_ec_platform_cells[] = {
454 { .name = "cros-ec-debugfs" },
455 { .name = "cros-ec-lightbar" },
456 { .name = "cros-ec-sysfs" },
459 static const struct mfd_cell cros_ec_vbc_cells[] = {
460 { .name = "cros-ec-vbc" }
463 static int ec_device_probe(struct platform_device *pdev)
465 int retval = -ENOMEM;
466 struct device_node *node;
467 struct device *dev = &pdev->dev;
468 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
469 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
474 dev_set_drvdata(dev, ec);
475 ec->ec_dev = dev_get_drvdata(dev->parent);
477 ec->cmd_offset = ec_platform->cmd_offset;
478 ec->features[0] = -1U; /* Not cached yet */
479 ec->features[1] = -1U; /* Not cached yet */
480 device_initialize(&ec->class_dev);
481 cdev_init(&ec->cdev, &fops);
483 /* Check whether this is actually a Fingerprint MCU rather than an EC */
484 if (cros_ec_check_features(ec, EC_FEATURE_FINGERPRINT)) {
485 dev_info(dev, "CrOS Fingerprint MCU detected.\n");
487 * Help userspace differentiating ECs from FP MCU,
488 * regardless of the probing order.
490 ec_platform->ec_name = CROS_EC_DEV_FP_NAME;
494 * Check whether this is actually an Integrated Sensor Hub (ISH)
497 if (cros_ec_check_features(ec, EC_FEATURE_ISH)) {
498 dev_info(dev, "CrOS ISH MCU detected.\n");
500 * Help userspace differentiating ECs from ISH MCU,
501 * regardless of the probing order.
503 ec_platform->ec_name = CROS_EC_DEV_ISH_NAME;
506 /* Check whether this is actually a Touchpad MCU rather than an EC */
507 if (cros_ec_check_features(ec, EC_FEATURE_TOUCHPAD)) {
508 dev_info(dev, "CrOS Touchpad MCU detected.\n");
510 * Help userspace differentiating ECs from TP MCU,
511 * regardless of the probing order.
513 ec_platform->ec_name = CROS_EC_DEV_TP_NAME;
516 /* Check whether this is actually a SCP rather than an EC. */
517 if (cros_ec_check_features(ec, EC_FEATURE_SCP)) {
518 dev_info(dev, "CrOS SCP MCU detected.\n");
520 * Help userspace differentiating ECs from SCP,
521 * regardless of the probing order.
523 ec_platform->ec_name = CROS_EC_DEV_SCP_NAME;
527 * Add the class device
528 * Link to the character device for creating the /dev entry
531 ec->class_dev.devt = MKDEV(ec_major, pdev->id);
532 ec->class_dev.class = &cros_class;
533 ec->class_dev.parent = dev;
534 ec->class_dev.release = cros_ec_class_release;
536 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
538 dev_err(dev, "dev_set_name failed => %d\n", retval);
542 /* check whether this EC is a sensor hub. */
543 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
544 cros_ec_sensors_register(ec);
546 /* Workaroud for older EC firmware */
547 cros_ec_accel_legacy_register(ec);
549 /* Check whether this EC instance has CEC host command support */
550 if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
551 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
553 ARRAY_SIZE(cros_ec_cec_cells),
557 "failed to add cros-ec-cec device: %d\n",
561 /* Check whether this EC instance has RTC host command support */
562 if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
563 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
565 ARRAY_SIZE(cros_ec_rtc_cells),
569 "failed to add cros-ec-rtc device: %d\n",
573 /* Check whether this EC instance has the PD charge manager */
574 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
575 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
576 cros_usbpd_charger_cells,
577 ARRAY_SIZE(cros_usbpd_charger_cells),
581 "failed to add cros-usbpd-charger device: %d\n",
585 /* We can now add the sysfs class, we know which parameter to show */
586 retval = cdev_device_add(&ec->cdev, &ec->class_dev);
588 dev_err(dev, "cdev_device_add failed => %d\n", retval);
592 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
593 cros_ec_platform_cells,
594 ARRAY_SIZE(cros_ec_platform_cells),
598 "failed to add cros-ec platform devices: %d\n",
601 /* Check whether this EC instance has a VBC NVRAM */
602 node = ec->ec_dev->dev->of_node;
603 if (of_property_read_bool(node, "google,has-vbc-nvram")) {
604 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
606 ARRAY_SIZE(cros_ec_vbc_cells),
609 dev_warn(ec->dev, "failed to add VBC devices: %d\n",
616 put_device(&ec->class_dev);
620 static int ec_device_remove(struct platform_device *pdev)
622 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
624 mfd_remove_devices(ec->dev);
626 device_unregister(&ec->class_dev);
630 static const struct platform_device_id cros_ec_id[] = {
634 MODULE_DEVICE_TABLE(platform, cros_ec_id);
636 static struct platform_driver cros_ec_dev_driver = {
640 .id_table = cros_ec_id,
641 .probe = ec_device_probe,
642 .remove = ec_device_remove,
645 static int __init cros_ec_dev_init(void)
650 ret = class_register(&cros_class);
652 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
656 /* Get a range of minor numbers (starting with 0) to work with */
657 ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
659 pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
660 goto failed_chrdevreg;
662 ec_major = MAJOR(dev);
664 /* Register the driver */
665 ret = platform_driver_register(&cros_ec_dev_driver);
667 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
673 unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
675 class_unregister(&cros_class);
679 static void __exit cros_ec_dev_exit(void)
681 platform_driver_unregister(&cros_ec_dev_driver);
682 unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
683 class_unregister(&cros_class);
686 module_init(cros_ec_dev_init);
687 module_exit(cros_ec_dev_exit);
689 MODULE_ALIAS("platform:" DRV_NAME);
691 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
692 MODULE_VERSION("1.0");
693 MODULE_LICENSE("GPL");