1 // SPDX-License-Identifier: GPL-2.0
3 * Core driver for Wilco Embedded Controller
5 * Copyright 2018 Google LLC
7 * This is the entry point for the drivers that control the Wilco EC.
8 * This driver is responsible for several tasks:
9 * - Initialize the register interface that is used by wilco_ec_mailbox()
10 * - Create a platform device which is picked up by the debugfs driver
11 * - Create a platform device which is picked up by the RTC driver
14 #include <linux/acpi.h>
15 #include <linux/device.h>
16 #include <linux/ioport.h>
17 #include <linux/module.h>
18 #include <linux/platform_data/wilco-ec.h>
19 #include <linux/platform_device.h>
21 #include "../cros_ec_lpc_mec.h"
23 #define DRV_NAME "wilco-ec"
25 static struct resource *wilco_get_resource(struct platform_device *pdev,
28 struct device *dev = &pdev->dev;
31 res = platform_get_resource(pdev, IORESOURCE_IO, index);
33 dev_dbg(dev, "Couldn't find IO resource %d\n", index);
37 return devm_request_region(dev, res->start, resource_size(res),
41 static int wilco_ec_probe(struct platform_device *pdev)
43 struct device *dev = &pdev->dev;
44 struct wilco_ec_device *ec;
47 ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
51 platform_set_drvdata(pdev, ec);
53 mutex_init(&ec->mailbox_lock);
55 ec->data_size = sizeof(struct wilco_ec_response) + EC_MAILBOX_DATA_SIZE;
56 ec->data_buffer = devm_kzalloc(dev, ec->data_size, GFP_KERNEL);
60 /* Prepare access to IO regions provided by ACPI */
61 ec->io_data = wilco_get_resource(pdev, 0); /* Host Data */
62 ec->io_command = wilco_get_resource(pdev, 1); /* Host Command */
63 ec->io_packet = wilco_get_resource(pdev, 2); /* MEC EMI */
64 if (!ec->io_data || !ec->io_command || !ec->io_packet)
67 /* Initialize cros_ec register interface for communication */
68 cros_ec_lpc_mec_init(ec->io_packet->start,
69 ec->io_packet->start + EC_MAILBOX_DATA_SIZE);
72 * Register a child device that will be found by the debugfs driver.
75 ec->debugfs_pdev = platform_device_register_data(dev,
80 /* Register a child device that will be found by the RTC driver. */
81 ec->rtc_pdev = platform_device_register_data(dev, "rtc-wilco-ec",
84 if (IS_ERR(ec->rtc_pdev)) {
85 dev_err(dev, "Failed to create RTC platform device\n");
86 ret = PTR_ERR(ec->rtc_pdev);
87 goto unregister_debugfs;
90 ret = wilco_ec_add_sysfs(ec);
92 dev_err(dev, "Failed to create sysfs entries: %d", ret);
96 /* Register child device that will be found by the telemetry driver. */
97 ec->telem_pdev = platform_device_register_data(dev, "wilco_telem",
100 if (IS_ERR(ec->telem_pdev)) {
101 dev_err(dev, "Failed to create telemetry platform device\n");
102 ret = PTR_ERR(ec->telem_pdev);
109 wilco_ec_remove_sysfs(ec);
111 platform_device_unregister(ec->rtc_pdev);
113 if (ec->debugfs_pdev)
114 platform_device_unregister(ec->debugfs_pdev);
115 cros_ec_lpc_mec_destroy();
119 static int wilco_ec_remove(struct platform_device *pdev)
121 struct wilco_ec_device *ec = platform_get_drvdata(pdev);
123 wilco_ec_remove_sysfs(ec);
124 platform_device_unregister(ec->telem_pdev);
125 platform_device_unregister(ec->rtc_pdev);
126 if (ec->debugfs_pdev)
127 platform_device_unregister(ec->debugfs_pdev);
129 /* Teardown cros_ec interface */
130 cros_ec_lpc_mec_destroy();
135 static const struct acpi_device_id wilco_ec_acpi_device_ids[] = {
139 MODULE_DEVICE_TABLE(acpi, wilco_ec_acpi_device_ids);
141 static struct platform_driver wilco_ec_driver = {
144 .acpi_match_table = wilco_ec_acpi_device_ids,
146 .probe = wilco_ec_probe,
147 .remove = wilco_ec_remove,
150 module_platform_driver(wilco_ec_driver);
154 MODULE_LICENSE("GPL v2");
155 MODULE_DESCRIPTION("ChromeOS Wilco Embedded Controller driver");
156 MODULE_ALIAS("platform:" DRV_NAME);