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 */
33 msg = kzalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
37 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
38 msg->insize = sizeof(ec->features);
40 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
42 dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
44 memset(ec->features, 0, sizeof(ec->features));
46 memcpy(ec->features, msg->data, sizeof(ec->features));
49 dev_dbg(ec->dev, "EC features %08x %08x\n",
50 ec->features[0], ec->features[1]);
55 return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
58 static void cros_ec_class_release(struct device *dev)
60 kfree(to_cros_ec_dev(dev));
63 static void cros_ec_sensors_register(struct cros_ec_dev *ec)
66 * Issue a command to get the number of sensor reported.
67 * Build an array of sensors driver and register them all.
69 int ret, i, id, sensor_num;
70 struct mfd_cell *sensor_cells;
71 struct cros_ec_sensor_platform *sensor_platforms;
72 int sensor_type[MOTIONSENSE_TYPE_MAX];
73 struct ec_params_motion_sense *params;
74 struct ec_response_motion_sense *resp;
75 struct cros_ec_command *msg;
77 msg = kzalloc(sizeof(struct cros_ec_command) +
78 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
83 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
84 msg->outsize = sizeof(*params);
85 msg->insize = sizeof(*resp);
87 params = (struct ec_params_motion_sense *)msg->data;
88 params->cmd = MOTIONSENSE_CMD_DUMP;
90 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
92 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
97 resp = (struct ec_response_motion_sense *)msg->data;
98 sensor_num = resp->dump.sensor_count;
100 * Allocate 2 extra sensors if lid angle sensor and/or FIFO are needed.
102 sensor_cells = kcalloc(sensor_num + 2, sizeof(struct mfd_cell),
104 if (sensor_cells == NULL)
107 sensor_platforms = kcalloc(sensor_num,
108 sizeof(struct cros_ec_sensor_platform),
110 if (sensor_platforms == NULL)
111 goto error_platforms;
113 memset(sensor_type, 0, sizeof(sensor_type));
115 for (i = 0; i < sensor_num; i++) {
116 params->cmd = MOTIONSENSE_CMD_INFO;
117 params->info.sensor_num = i;
118 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
120 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
121 i, ret, msg->result);
124 switch (resp->info.type) {
125 case MOTIONSENSE_TYPE_ACCEL:
126 sensor_cells[id].name = "cros-ec-accel";
128 case MOTIONSENSE_TYPE_BARO:
129 sensor_cells[id].name = "cros-ec-baro";
131 case MOTIONSENSE_TYPE_GYRO:
132 sensor_cells[id].name = "cros-ec-gyro";
134 case MOTIONSENSE_TYPE_MAG:
135 sensor_cells[id].name = "cros-ec-mag";
137 case MOTIONSENSE_TYPE_PROX:
138 sensor_cells[id].name = "cros-ec-prox";
140 case MOTIONSENSE_TYPE_LIGHT:
141 sensor_cells[id].name = "cros-ec-light";
143 case MOTIONSENSE_TYPE_ACTIVITY:
144 sensor_cells[id].name = "cros-ec-activity";
147 dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
150 sensor_platforms[id].sensor_num = i;
151 sensor_cells[id].id = sensor_type[resp->info.type];
152 sensor_cells[id].platform_data = &sensor_platforms[id];
153 sensor_cells[id].pdata_size =
154 sizeof(struct cros_ec_sensor_platform);
156 sensor_type[resp->info.type]++;
160 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
161 ec->has_kb_wake_angle = true;
163 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
164 sensor_cells[id].name = "cros-ec-ring";
167 if (cros_ec_check_features(ec,
168 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
169 sensor_cells[id].name = "cros-ec-lid-angle";
173 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
176 dev_err(ec->dev, "failed to add EC sensors\n");
178 kfree(sensor_platforms);
185 static struct cros_ec_sensor_platform sensor_platforms[] = {
190 static const struct mfd_cell cros_ec_accel_legacy_cells[] = {
192 .name = "cros-ec-accel-legacy",
193 .platform_data = &sensor_platforms[0],
194 .pdata_size = sizeof(struct cros_ec_sensor_platform),
197 .name = "cros-ec-accel-legacy",
198 .platform_data = &sensor_platforms[1],
199 .pdata_size = sizeof(struct cros_ec_sensor_platform),
203 static void cros_ec_accel_legacy_register(struct cros_ec_dev *ec)
205 struct cros_ec_device *ec_dev = ec->ec_dev;
210 * ECs that need legacy support are the main EC, directly connected to
213 if (ec->cmd_offset != 0)
217 * Check if EC supports direct memory reads and if EC has
220 if (ec_dev->cmd_readmem) {
221 ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS, 1,
224 dev_warn(ec->dev, "EC direct read error.\n");
228 /* Check if EC has accelerometers. */
229 if (!(status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
230 dev_info(ec->dev, "EC does not have accelerometers.\n");
236 * The device may still support accelerometers:
237 * it would be an older ARM based device that do not suppor the
238 * EC_CMD_GET_FEATURES command.
240 * Register 2 accelerometers, we will fail in the IIO driver if there
243 ret = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
244 cros_ec_accel_legacy_cells,
245 ARRAY_SIZE(cros_ec_accel_legacy_cells),
248 dev_err(ec_dev->dev, "failed to add EC sensors\n");
251 static const struct mfd_cell cros_ec_cec_cells[] = {
252 { .name = "cros-ec-cec" }
255 static const struct mfd_cell cros_ec_rtc_cells[] = {
256 { .name = "cros-ec-rtc" }
259 static const struct mfd_cell cros_usbpd_charger_cells[] = {
260 { .name = "cros-usbpd-charger" },
261 { .name = "cros-usbpd-logger" },
264 static const struct mfd_cell cros_ec_platform_cells[] = {
265 { .name = "cros-ec-chardev" },
266 { .name = "cros-ec-debugfs" },
267 { .name = "cros-ec-lightbar" },
268 { .name = "cros-ec-sysfs" },
271 static const struct mfd_cell cros_ec_vbc_cells[] = {
272 { .name = "cros-ec-vbc" }
275 static int ec_device_probe(struct platform_device *pdev)
277 int retval = -ENOMEM;
278 struct device_node *node;
279 struct device *dev = &pdev->dev;
280 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
281 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
286 dev_set_drvdata(dev, ec);
287 ec->ec_dev = dev_get_drvdata(dev->parent);
289 ec->cmd_offset = ec_platform->cmd_offset;
290 ec->features[0] = -1U; /* Not cached yet */
291 ec->features[1] = -1U; /* Not cached yet */
292 device_initialize(&ec->class_dev);
294 /* Check whether this is actually a Fingerprint MCU rather than an EC */
295 if (cros_ec_check_features(ec, EC_FEATURE_FINGERPRINT)) {
296 dev_info(dev, "CrOS Fingerprint MCU detected.\n");
298 * Help userspace differentiating ECs from FP MCU,
299 * regardless of the probing order.
301 ec_platform->ec_name = CROS_EC_DEV_FP_NAME;
305 * Check whether this is actually an Integrated Sensor Hub (ISH)
308 if (cros_ec_check_features(ec, EC_FEATURE_ISH)) {
309 dev_info(dev, "CrOS ISH MCU detected.\n");
311 * Help userspace differentiating ECs from ISH MCU,
312 * regardless of the probing order.
314 ec_platform->ec_name = CROS_EC_DEV_ISH_NAME;
317 /* Check whether this is actually a Touchpad MCU rather than an EC */
318 if (cros_ec_check_features(ec, EC_FEATURE_TOUCHPAD)) {
319 dev_info(dev, "CrOS Touchpad MCU detected.\n");
321 * Help userspace differentiating ECs from TP MCU,
322 * regardless of the probing order.
324 ec_platform->ec_name = CROS_EC_DEV_TP_NAME;
327 /* Check whether this is actually a SCP rather than an EC. */
328 if (cros_ec_check_features(ec, EC_FEATURE_SCP)) {
329 dev_info(dev, "CrOS SCP MCU detected.\n");
331 * Help userspace differentiating ECs from SCP,
332 * regardless of the probing order.
334 ec_platform->ec_name = CROS_EC_DEV_SCP_NAME;
338 * Add the class device
340 ec->class_dev.class = &cros_class;
341 ec->class_dev.parent = dev;
342 ec->class_dev.release = cros_ec_class_release;
344 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
346 dev_err(dev, "dev_set_name failed => %d\n", retval);
350 retval = device_add(&ec->class_dev);
354 /* check whether this EC is a sensor hub. */
355 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
356 cros_ec_sensors_register(ec);
358 /* Workaroud for older EC firmware */
359 cros_ec_accel_legacy_register(ec);
361 /* Check whether this EC instance has CEC host command support */
362 if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
363 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
365 ARRAY_SIZE(cros_ec_cec_cells),
369 "failed to add cros-ec-cec device: %d\n",
373 /* Check whether this EC instance has RTC host command support */
374 if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
375 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
377 ARRAY_SIZE(cros_ec_rtc_cells),
381 "failed to add cros-ec-rtc device: %d\n",
385 /* Check whether this EC instance has the PD charge manager */
386 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
387 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
388 cros_usbpd_charger_cells,
389 ARRAY_SIZE(cros_usbpd_charger_cells),
393 "failed to add cros-usbpd-charger device: %d\n",
397 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
398 cros_ec_platform_cells,
399 ARRAY_SIZE(cros_ec_platform_cells),
403 "failed to add cros-ec platform devices: %d\n",
406 /* Check whether this EC instance has a VBC NVRAM */
407 node = ec->ec_dev->dev->of_node;
408 if (of_property_read_bool(node, "google,has-vbc-nvram")) {
409 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
411 ARRAY_SIZE(cros_ec_vbc_cells),
414 dev_warn(ec->dev, "failed to add VBC devices: %d\n",
421 put_device(&ec->class_dev);
425 static int ec_device_remove(struct platform_device *pdev)
427 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
429 mfd_remove_devices(ec->dev);
430 device_unregister(&ec->class_dev);
434 static const struct platform_device_id cros_ec_id[] = {
438 MODULE_DEVICE_TABLE(platform, cros_ec_id);
440 static struct platform_driver cros_ec_dev_driver = {
444 .id_table = cros_ec_id,
445 .probe = ec_device_probe,
446 .remove = ec_device_remove,
449 static int __init cros_ec_dev_init(void)
453 ret = class_register(&cros_class);
455 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
459 /* Register the driver */
460 ret = platform_driver_register(&cros_ec_dev_driver);
462 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
468 class_unregister(&cros_class);
472 static void __exit cros_ec_dev_exit(void)
474 platform_driver_unregister(&cros_ec_dev_driver);
475 class_unregister(&cros_class);
478 module_init(cros_ec_dev_init);
479 module_exit(cros_ec_dev_exit);
481 MODULE_ALIAS("platform:" DRV_NAME);
482 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
483 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
484 MODULE_VERSION("1.0");
485 MODULE_LICENSE("GPL");