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.
8 #include <linux/mfd/core.h>
9 #include <linux/mfd/cros_ec.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/platform_data/cros_ec_chardev.h>
15 #include <linux/platform_data/cros_ec_commands.h>
16 #include <linux/platform_data/cros_ec_proto.h>
17 #include <linux/slab.h>
19 #define DRV_NAME "cros-ec-dev"
21 static struct class cros_class = {
26 static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
28 struct cros_ec_command *msg;
31 if (ec->features[0] == -1U && ec->features[1] == -1U) {
32 /* features bitmap not read yet */
34 msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
39 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
40 msg->insize = sizeof(ec->features);
43 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
44 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
45 dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
47 memset(ec->features, 0, sizeof(ec->features));
49 memcpy(ec->features, msg->data, sizeof(ec->features));
52 dev_dbg(ec->dev, "EC features %08x %08x\n",
53 ec->features[0], ec->features[1]);
58 return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
61 static void cros_ec_class_release(struct device *dev)
63 kfree(to_cros_ec_dev(dev));
66 static void cros_ec_sensors_register(struct cros_ec_dev *ec)
69 * Issue a command to get the number of sensor reported.
70 * Build an array of sensors driver and register them all.
72 int ret, i, id, sensor_num;
73 struct mfd_cell *sensor_cells;
74 struct cros_ec_sensor_platform *sensor_platforms;
75 int sensor_type[MOTIONSENSE_TYPE_MAX];
76 struct ec_params_motion_sense *params;
77 struct ec_response_motion_sense *resp;
78 struct cros_ec_command *msg;
80 msg = kzalloc(sizeof(struct cros_ec_command) +
81 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
86 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
87 msg->outsize = sizeof(*params);
88 msg->insize = sizeof(*resp);
90 params = (struct ec_params_motion_sense *)msg->data;
91 params->cmd = MOTIONSENSE_CMD_DUMP;
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 sensor information: %d/%d\n",
100 resp = (struct ec_response_motion_sense *)msg->data;
101 sensor_num = resp->dump.sensor_count;
103 * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
105 sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
107 if (sensor_cells == NULL)
110 sensor_platforms = kcalloc(sensor_num,
111 sizeof(struct cros_ec_sensor_platform),
113 if (sensor_platforms == NULL)
114 goto error_platforms;
116 memset(sensor_type, 0, sizeof(sensor_type));
118 for (i = 0; i < sensor_num; i++) {
119 params->cmd = MOTIONSENSE_CMD_INFO;
120 params->info.sensor_num = i;
121 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
122 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
123 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
124 i, ret, msg->result);
127 switch (resp->info.type) {
128 case MOTIONSENSE_TYPE_ACCEL:
129 sensor_cells[id].name = "cros-ec-accel";
131 case MOTIONSENSE_TYPE_BARO:
132 sensor_cells[id].name = "cros-ec-baro";
134 case MOTIONSENSE_TYPE_GYRO:
135 sensor_cells[id].name = "cros-ec-gyro";
137 case MOTIONSENSE_TYPE_MAG:
138 sensor_cells[id].name = "cros-ec-mag";
140 case MOTIONSENSE_TYPE_PROX:
141 sensor_cells[id].name = "cros-ec-prox";
143 case MOTIONSENSE_TYPE_LIGHT:
144 sensor_cells[id].name = "cros-ec-light";
146 case MOTIONSENSE_TYPE_ACTIVITY:
147 sensor_cells[id].name = "cros-ec-activity";
150 dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
153 sensor_platforms[id].sensor_num = i;
154 sensor_cells[id].id = sensor_type[resp->info.type];
155 sensor_cells[id].platform_data = &sensor_platforms[id];
156 sensor_cells[id].pdata_size =
157 sizeof(struct cros_ec_sensor_platform);
159 sensor_type[resp->info.type]++;
163 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
164 ec->has_kb_wake_angle = true;
166 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
167 sensor_cells[id].name = "cros-ec-ring";
170 if (cros_ec_check_features(ec,
171 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
172 sensor_cells[id].name = "cros-ec-lid-angle";
176 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
179 dev_err(ec->dev, "failed to add EC sensors\n");
181 kfree(sensor_platforms);
188 static struct cros_ec_sensor_platform sensor_platforms[] = {
193 static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
195 .name = "cros-ec-accel-legacy",
196 .platform_data = &sensor_platforms[0],
197 .pdata_size = sizeof(struct cros_ec_sensor_platform),
200 .name = "cros-ec-accel-legacy",
201 .platform_data = &sensor_platforms[1],
202 .pdata_size = sizeof(struct cros_ec_sensor_platform),
206 static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
208 struct cros_ec_device *ec_dev = ec->ec_dev;
213 * ECs that need legacy support are the main EC, directly connected to
216 if (ec->cmd_offset != 0)
220 * Check if EC supports direct memory reads and if EC has
223 if (ec_dev->cmd_readmem) {
224 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
227 dev_warn(ec->dev, "EC direct read error.\n");
231 /* Check if EC has accelerometers. */
232 if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
233 dev_info(ec->dev, "EC does not have accelerometers.\n");
239 * The device may still support accelerometers:
240 * it would be an older ARM based device that do not suppor the
241 * EC_CMD_GET_FEATURES command.
243 * Register 2 accelerometers, we will fail in the IIO driver if there
246 ret = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
247 cros_ec_accel_legacy_cells,
248 ARRAY_SIZE(cros_ec_accel_legacy_cells),
251 dev_err(ec_dev->dev, "failed to add EC sensors\n");
254 static const struct mfd_cell cros_ec_cec_cells[] = {
255 { .name = "cros-ec-cec" }
258 static const struct mfd_cell cros_ec_rtc_cells[] = {
259 { .name = "cros-ec-rtc" }
262 static const struct mfd_cell cros_usbpd_charger_cells[] = {
263 { .name = "cros-usbpd-charger" },
264 { .name = "cros-usbpd-logger" },
267 static const struct mfd_cell cros_ec_platform_cells[] = {
268 { .name = "cros-ec-chardev" },
269 { .name = "cros-ec-debugfs" },
270 { .name = "cros-ec-lightbar" },
271 { .name = "cros-ec-sysfs" },
274 static const struct mfd_cell cros_ec_vbc_cells[] = {
275 { .name = "cros-ec-vbc" }
278 static int ec_device_probe(struct platform_device *pdev)
280 int retval = -ENOMEM;
281 struct device_node *node;
282 struct device *dev = &pdev->dev;
283 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
284 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
289 dev_set_drvdata(dev, ec);
290 ec->ec_dev = dev_get_drvdata(dev->parent);
292 ec->cmd_offset = ec_platform->cmd_offset;
293 ec->features[0] = -1U; /* Not cached yet */
294 ec->features[1] = -1U; /* Not cached yet */
295 device_initialize(&ec->class_dev);
297 /* Check whether this is actually a Fingerprint MCU rather than an EC */
298 if (cros_ec_check_features(ec, EC_FEATURE_FINGERPRINT)) {
299 dev_info(dev, "CrOS Fingerprint MCU detected.\n");
301 * Help userspace differentiating ECs from FP MCU,
302 * regardless of the probing order.
304 ec_platform->ec_name = CROS_EC_DEV_FP_NAME;
308 * Check whether this is actually an Integrated Sensor Hub (ISH)
311 if (cros_ec_check_features(ec, EC_FEATURE_ISH)) {
312 dev_info(dev, "CrOS ISH MCU detected.\n");
314 * Help userspace differentiating ECs from ISH MCU,
315 * regardless of the probing order.
317 ec_platform->ec_name = CROS_EC_DEV_ISH_NAME;
320 /* Check whether this is actually a Touchpad MCU rather than an EC */
321 if (cros_ec_check_features(ec, EC_FEATURE_TOUCHPAD)) {
322 dev_info(dev, "CrOS Touchpad MCU detected.\n");
324 * Help userspace differentiating ECs from TP MCU,
325 * regardless of the probing order.
327 ec_platform->ec_name = CROS_EC_DEV_TP_NAME;
330 /* Check whether this is actually a SCP rather than an EC. */
331 if (cros_ec_check_features(ec, EC_FEATURE_SCP)) {
332 dev_info(dev, "CrOS SCP MCU detected.\n");
334 * Help userspace differentiating ECs from SCP,
335 * regardless of the probing order.
337 ec_platform->ec_name = CROS_EC_DEV_SCP_NAME;
341 * Add the class device
343 ec->class_dev.class = &cros_class;
344 ec->class_dev.parent = dev;
345 ec->class_dev.release = cros_ec_class_release;
347 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
349 dev_err(dev, "dev_set_name failed => %d\n", retval);
353 retval = device_add(&ec->class_dev);
357 /* check whether this EC is a sensor hub. */
358 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
359 cros_ec_sensors_register(ec);
361 /* Workaroud for older EC firmware */
362 cros_ec_accel_legacy_register(ec);
364 /* Check whether this EC instance has CEC host command support */
365 if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
366 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
368 ARRAY_SIZE(cros_ec_cec_cells),
372 "failed to add cros-ec-cec device: %d\n",
376 /* Check whether this EC instance has RTC host command support */
377 if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
378 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
380 ARRAY_SIZE(cros_ec_rtc_cells),
384 "failed to add cros-ec-rtc device: %d\n",
388 /* Check whether this EC instance has the PD charge manager */
389 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
390 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
391 cros_usbpd_charger_cells,
392 ARRAY_SIZE(cros_usbpd_charger_cells),
396 "failed to add cros-usbpd-charger device: %d\n",
400 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
401 cros_ec_platform_cells,
402 ARRAY_SIZE(cros_ec_platform_cells),
406 "failed to add cros-ec platform devices: %d\n",
409 /* Check whether this EC instance has a VBC NVRAM */
410 node = ec->ec_dev->dev->of_node;
411 if (of_property_read_bool(node, "google,has-vbc-nvram")) {
412 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
414 ARRAY_SIZE(cros_ec_vbc_cells),
417 dev_warn(ec->dev, "failed to add VBC devices: %d\n",
424 put_device(&ec->class_dev);
428 static int ec_device_remove(struct platform_device *pdev)
430 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
432 mfd_remove_devices(ec->dev);
433 device_unregister(&ec->class_dev);
437 static const struct platform_device_id cros_ec_id[] = {
441 MODULE_DEVICE_TABLE(platform, cros_ec_id);
443 static struct platform_driver cros_ec_dev_driver = {
447 .id_table = cros_ec_id,
448 .probe = ec_device_probe,
449 .remove = ec_device_remove,
452 static int __init cros_ec_dev_init(void)
456 ret = class_register(&cros_class);
458 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
462 /* Register the driver */
463 ret = platform_driver_register(&cros_ec_dev_driver);
465 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
471 class_unregister(&cros_class);
475 static void __exit cros_ec_dev_exit(void)
477 platform_driver_unregister(&cros_ec_dev_driver);
478 class_unregister(&cros_class);
481 module_init(cros_ec_dev_init);
482 module_exit(cros_ec_dev_exit);
484 MODULE_ALIAS("platform:" DRV_NAME);
485 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
486 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
487 MODULE_VERSION("1.0");
488 MODULE_LICENSE("GPL");