1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ChromeOS Embedded Controller
5 * Copyright (C) 2014 Google, Inc.
9 #include <linux/kconfig.h>
10 #include <linux/mfd/core.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
14 #include <linux/platform_device.h>
15 #include <linux/platform_data/cros_ec_chardev.h>
16 #include <linux/platform_data/cros_ec_commands.h>
17 #include <linux/platform_data/cros_ec_proto.h>
18 #include <linux/slab.h>
20 #define DRV_NAME "cros-ec-dev"
22 static struct class cros_class = {
27 * struct 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 * struct 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_gpio_cells[] = {
78 { .name = "cros-ec-gpio", },
81 static const struct mfd_cell cros_ec_rtc_cells[] = {
82 { .name = "cros-ec-rtc", },
85 static const struct mfd_cell cros_ec_sensorhub_cells[] = {
86 { .name = "cros-ec-sensorhub", },
89 static const struct mfd_cell cros_usbpd_charger_cells[] = {
90 { .name = "cros-charge-control", },
91 { .name = "cros-usbpd-charger", },
92 { .name = "cros-usbpd-logger", },
95 static const struct mfd_cell cros_usbpd_notify_cells[] = {
96 { .name = "cros-usbpd-notify", },
99 static const struct mfd_cell cros_ec_wdt_cells[] = {
100 { .name = "cros-ec-wdt", }
103 static const struct mfd_cell cros_ec_led_cells[] = {
104 { .name = "cros-ec-led", },
107 static const struct mfd_cell cros_ec_keyboard_leds_cells[] = {
108 { .name = "cros-keyboard-leds", },
111 static const struct mfd_cell cros_ec_ucsi_cells[] = {
112 { .name = "cros_ec_ucsi", },
115 static const struct cros_feature_to_cells cros_subdevices[] = {
117 .id = EC_FEATURE_CEC,
118 .mfd_cells = cros_ec_cec_cells,
119 .num_cells = ARRAY_SIZE(cros_ec_cec_cells),
122 .id = EC_FEATURE_GPIO,
123 .mfd_cells = cros_ec_gpio_cells,
124 .num_cells = ARRAY_SIZE(cros_ec_gpio_cells),
127 .id = EC_FEATURE_RTC,
128 .mfd_cells = cros_ec_rtc_cells,
129 .num_cells = ARRAY_SIZE(cros_ec_rtc_cells),
132 .id = EC_FEATURE_UCSI_PPM,
133 .mfd_cells = cros_ec_ucsi_cells,
134 .num_cells = ARRAY_SIZE(cros_ec_ucsi_cells),
137 .id = EC_FEATURE_HANG_DETECT,
138 .mfd_cells = cros_ec_wdt_cells,
139 .num_cells = ARRAY_SIZE(cros_ec_wdt_cells),
142 .id = EC_FEATURE_LED,
143 .mfd_cells = cros_ec_led_cells,
144 .num_cells = ARRAY_SIZE(cros_ec_led_cells),
147 .id = EC_FEATURE_PWM_KEYB,
148 .mfd_cells = cros_ec_keyboard_leds_cells,
149 .num_cells = ARRAY_SIZE(cros_ec_keyboard_leds_cells),
153 static const struct mfd_cell cros_ec_platform_cells[] = {
154 { .name = "cros-ec-chardev", },
155 { .name = "cros-ec-debugfs", },
156 { .name = "cros-ec-hwmon", },
157 { .name = "cros-ec-sysfs", },
160 static const struct mfd_cell cros_ec_pchg_cells[] = {
161 { .name = "cros-ec-pchg", },
164 static const struct mfd_cell cros_ec_lightbar_cells[] = {
165 { .name = "cros-ec-lightbar", }
168 static const struct mfd_cell cros_ec_vbc_cells[] = {
169 { .name = "cros-ec-vbc", }
172 static void cros_ec_class_release(struct device *dev)
174 kfree(to_cros_ec_dev(dev));
177 static int ec_device_probe(struct platform_device *pdev)
179 int retval = -ENOMEM;
180 struct device_node *node;
181 struct device *dev = &pdev->dev;
182 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
183 struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
184 struct ec_response_pchg_count pchg_count;
190 dev_set_drvdata(dev, ec);
191 ec->ec_dev = dev_get_drvdata(dev->parent);
193 ec->cmd_offset = ec_platform->cmd_offset;
194 ec->features.flags[0] = -1U; /* Not cached yet */
195 ec->features.flags[1] = -1U; /* Not cached yet */
196 device_initialize(&ec->class_dev);
198 for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) {
200 * Check whether this is actually a dedicated MCU rather
201 * than an standard EC.
203 if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) {
204 dev_info(dev, "CrOS %s MCU detected\n",
205 cros_mcu_devices[i].desc);
207 * Help userspace differentiating ECs from other MCU,
208 * regardless of the probing order.
210 ec_platform->ec_name = cros_mcu_devices[i].name;
216 * Add the class device
218 ec->class_dev.class = &cros_class;
219 ec->class_dev.parent = dev;
220 ec->class_dev.release = cros_ec_class_release;
222 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
224 dev_err(dev, "dev_set_name failed => %d\n", retval);
228 retval = device_add(&ec->class_dev);
232 /* check whether this EC is a sensor hub. */
233 if (cros_ec_get_sensor_count(ec) > 0) {
234 retval = mfd_add_hotplug_devices(ec->dev,
235 cros_ec_sensorhub_cells,
236 ARRAY_SIZE(cros_ec_sensorhub_cells));
238 dev_err(ec->dev, "failed to add %s subdevice: %d\n",
239 cros_ec_sensorhub_cells->name, retval);
243 * The following subdevices can be detected by sending the
244 * EC_FEATURE_GET_CMD Embedded Controller device.
246 for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) {
247 if (cros_ec_check_features(ec, cros_subdevices[i].id)) {
248 retval = mfd_add_hotplug_devices(ec->dev,
249 cros_subdevices[i].mfd_cells,
250 cros_subdevices[i].num_cells);
253 "failed to add %s subdevice: %d\n",
254 cros_subdevices[i].mfd_cells->name,
260 * UCSI provides power supply information so we don't need to separately
261 * load the cros_usbpd_charger driver.
263 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD) &&
264 !cros_ec_check_features(ec, EC_FEATURE_UCSI_PPM)) {
265 retval = mfd_add_hotplug_devices(ec->dev,
266 cros_usbpd_charger_cells,
267 ARRAY_SIZE(cros_usbpd_charger_cells));
270 dev_warn(ec->dev, "failed to add usbpd-charger: %d\n",
275 * Lightbar is a special case. Newer devices support autodetection,
276 * but older ones do not.
278 if (cros_ec_check_features(ec, EC_FEATURE_LIGHTBAR) ||
279 dmi_match(DMI_PRODUCT_NAME, "Link")) {
280 retval = mfd_add_hotplug_devices(ec->dev,
281 cros_ec_lightbar_cells,
282 ARRAY_SIZE(cros_ec_lightbar_cells));
284 dev_warn(ec->dev, "failed to add lightbar: %d\n",
289 * The PD notifier driver cell is separate since it only needs to be
290 * explicitly added on platforms that don't have the PD notifier ACPI
291 * device entry defined.
293 if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) {
294 if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
295 retval = mfd_add_hotplug_devices(ec->dev,
296 cros_usbpd_notify_cells,
297 ARRAY_SIZE(cros_usbpd_notify_cells));
300 "failed to add PD notify devices: %d\n",
306 * The PCHG device cannot be detected by sending EC_FEATURE_GET_CMD, but
307 * it can be detected by querying the number of peripheral chargers.
309 retval = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_PCHG_COUNT, NULL, 0,
310 &pchg_count, sizeof(pchg_count));
311 if (retval >= 0 && pchg_count.port_count) {
312 retval = mfd_add_hotplug_devices(ec->dev,
314 ARRAY_SIZE(cros_ec_pchg_cells));
316 dev_warn(ec->dev, "failed to add pchg: %d\n",
321 * The following subdevices cannot be detected by sending the
322 * EC_FEATURE_GET_CMD to the Embedded Controller device.
324 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells,
325 ARRAY_SIZE(cros_ec_platform_cells));
328 "failed to add cros-ec platform devices: %d\n",
331 /* Check whether this EC instance has a VBC NVRAM */
332 node = ec->ec_dev->dev->of_node;
333 if (of_property_read_bool(node, "google,has-vbc-nvram")) {
334 retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells,
335 ARRAY_SIZE(cros_ec_vbc_cells));
337 dev_warn(ec->dev, "failed to add VBC devices: %d\n",
344 put_device(&ec->class_dev);
348 static void ec_device_remove(struct platform_device *pdev)
350 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
352 mfd_remove_devices(ec->dev);
353 device_unregister(&ec->class_dev);
356 static const struct platform_device_id cros_ec_id[] = {
360 MODULE_DEVICE_TABLE(platform, cros_ec_id);
362 static struct platform_driver cros_ec_dev_driver = {
366 .id_table = cros_ec_id,
367 .probe = ec_device_probe,
368 .remove = ec_device_remove,
371 static int __init cros_ec_dev_init(void)
375 ret = class_register(&cros_class);
377 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
381 ret = platform_driver_register(&cros_ec_dev_driver);
383 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
384 class_unregister(&cros_class);
389 static void __exit cros_ec_dev_exit(void)
391 platform_driver_unregister(&cros_ec_dev_driver);
392 class_unregister(&cros_class);
395 module_init(cros_ec_dev_init);
396 module_exit(cros_ec_dev_exit);
399 MODULE_DESCRIPTION("ChromeOS Embedded Controller");
400 MODULE_VERSION("1.0");
401 MODULE_LICENSE("GPL");