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 = {
27 * cros_feature_to_name - CrOS feature id to name/short description.
28 * @id: The feature identifier.
29 * @name: Device name associated with the feature id.
30 * @desc: Short name that will be displayed.
32 struct cros_feature_to_name {
39 * cros_feature_to_cells - CrOS feature id to mfd cells association.
40 * @id: The feature identifier.
41 * @mfd_cells: Pointer to the array of mfd cells that needs to be added.
42 * @num_cells: Number of mfd cells into the array.
44 struct cros_feature_to_cells {
46 const struct mfd_cell *mfd_cells;
47 unsigned int num_cells;
50 static const struct cros_feature_to_name cros_mcu_devices[] = {
52 .id = EC_FEATURE_FINGERPRINT,
53 .name = CROS_EC_DEV_FP_NAME,
54 .desc = "Fingerprint",
58 .name = CROS_EC_DEV_ISH_NAME,
59 .desc = "Integrated Sensor Hub",
63 .name = CROS_EC_DEV_SCP_NAME,
64 .desc = "System Control Processor",
67 .id = EC_FEATURE_TOUCHPAD,
68 .name = CROS_EC_DEV_TP_NAME,
73 static const struct mfd_cell cros_ec_cec_cells[] = {
74 { .name = "cros-ec-cec", },
77 static const struct mfd_cell cros_ec_rtc_cells[] = {
78 { .name = "cros-ec-rtc", },
81 static const struct mfd_cell cros_ec_sensorhub_cells[] = {
82 { .name = "cros-ec-sensorhub", },
85 static const struct mfd_cell cros_usbpd_charger_cells[] = {
86 { .name = "cros-usbpd-charger", },
87 { .name = "cros-usbpd-logger", },
90 static const struct cros_feature_to_cells cros_subdevices[] = {
93 .mfd_cells = cros_ec_cec_cells,
94 .num_cells = ARRAY_SIZE(cros_ec_cec_cells),
98 .mfd_cells = cros_ec_rtc_cells,
99 .num_cells = ARRAY_SIZE(cros_ec_rtc_cells),
102 .id = EC_FEATURE_USB_PD,
103 .mfd_cells = cros_usbpd_charger_cells,
104 .num_cells = ARRAY_SIZE(cros_usbpd_charger_cells),
108 static const struct mfd_cell cros_ec_platform_cells[] = {
109 { .name = "cros-ec-chardev", },
110 { .name = "cros-ec-debugfs", },
111 { .name = "cros-ec-lightbar", },
112 { .name = "cros-ec-sysfs", },
115 static const struct mfd_cell cros_ec_vbc_cells[] = {
116 { .name = "cros-ec-vbc", }
119 static void cros_ec_class_release(struct device *dev)
121 kfree(to_cros_ec_dev(dev));
124 static int ec_device_probe(struct platform_device *pdev)
126 int retval = -ENOMEM;
127 struct device_node *node;
128 struct device *dev = &pdev->dev;
129 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
130 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
136 dev_set_drvdata(dev, ec);
137 ec->ec_dev = dev_get_drvdata(dev->parent);
139 ec->cmd_offset = ec_platform->cmd_offset;
140 ec->features[0] = -1U; /* Not cached yet */
141 ec->features[1] = -1U; /* Not cached yet */
142 device_initialize(&ec->class_dev);
144 for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) {
146 * Check whether this is actually a dedicated MCU rather
147 * than an standard EC.
149 if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) {
150 dev_info(dev, "CrOS %s MCU detected\n",
151 cros_mcu_devices[i].desc);
153 * Help userspace differentiating ECs from other MCU,
154 * regardless of the probing order.
156 ec_platform->ec_name = cros_mcu_devices[i].name;
162 * Add the class device
164 ec->class_dev.class = &cros_class;
165 ec->class_dev.parent = dev;
166 ec->class_dev.release = cros_ec_class_release;
168 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
170 dev_err(dev, "dev_set_name failed => %d\n", retval);
174 retval = device_add(&ec->class_dev);
178 /* check whether this EC is a sensor hub. */
179 if (cros_ec_get_sensor_count(ec) > 0) {
180 retval = mfd_add_hotplug_devices(ec->dev,
181 cros_ec_sensorhub_cells,
182 ARRAY_SIZE(cros_ec_sensorhub_cells));
184 dev_err(ec->dev, "failed to add %s subdevice: %d\n",
185 cros_ec_sensorhub_cells->name, retval);
189 * The following subdevices can be detected by sending the
190 * EC_FEATURE_GET_CMD Embedded Controller device.
192 for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {
193 if (cros_ec_check_features(ec, cros_subdevices[i].id)) {
194 retval = mfd_add_hotplug_devices(ec->dev,
195 cros_subdevices[i].mfd_cells,
196 cros_subdevices[i].num_cells);
199 "failed to add %s subdevice: %d\n",
200 cros_subdevices[i].mfd_cells->name,
206 * The following subdevices cannot be detected by sending the
207 * EC_FEATURE_GET_CMD to the Embedded Controller device.
209 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells,
210 ARRAY_SIZE(cros_ec_platform_cells));
213 "failed to add cros-ec platform devices: %d\n",
216 /* Check whether this EC instance has a VBC NVRAM */
217 node = ec->ec_dev->dev->of_node;
218 if (of_property_read_bool(node, "google,has-vbc-nvram")) {
219 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells,
220 ARRAY_SIZE(cros_ec_vbc_cells));
222 dev_warn(ec->dev, "failed to add VBC devices: %d\n",
229 put_device(&ec->class_dev);
233 static int ec_device_remove(struct platform_device *pdev)
235 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
237 mfd_remove_devices(ec->dev);
238 device_unregister(&ec->class_dev);
242 static const struct platform_device_id cros_ec_id[] = {
246 MODULE_DEVICE_TABLE(platform, cros_ec_id);
248 static struct platform_driver cros_ec_dev_driver = {
252 .id_table = cros_ec_id,
253 .probe = ec_device_probe,
254 .remove = ec_device_remove,
257 static int __init cros_ec_dev_init(void)
261 ret = class_register(&cros_class);
263 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
267 /* Register the driver */
268 ret = platform_driver_register(&cros_ec_dev_driver);
270 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
276 class_unregister(&cros_class);
280 static void __exit cros_ec_dev_exit(void)
282 platform_driver_unregister(&cros_ec_dev_driver);
283 class_unregister(&cros_class);
286 module_init(cros_ec_dev_init);
287 module_exit(cros_ec_dev_exit);
289 MODULE_ALIAS("platform:" DRV_NAME);
290 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
291 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
292 MODULE_VERSION("1.0");
293 MODULE_LICENSE("GPL");