blob: 6acfe036d5222adfe23e36519eee76435bfa160e [file] [log] [blame]
Simon Glass4ab61742013-02-25 14:08:37 -08001/*
2 * ChromeOS EC multi-function device
3 *
4 * Copyright (C) 2012 Google, Inc
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * The ChromeOS EC multi function device is used to mux all the requests
16 * to the EC device for its multiple features: keyboard controller,
17 * battery charging and regulator control, firmware update.
18 */
19
Todd Brochbb03ffb2015-05-20 11:31:28 +020020#include <linux/of_platform.h>
Simon Glass4ab61742013-02-25 14:08:37 -080021#include <linux/interrupt.h>
22#include <linux/slab.h>
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +010023#include <linux/module.h>
Simon Glass4ab61742013-02-25 14:08:37 -080024#include <linux/mfd/core.h>
25#include <linux/mfd/cros_ec.h>
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +010026#include <linux/suspend.h>
Vic Yang6f1d9122016-08-10 19:05:24 +020027#include <asm/unaligned.h>
Andrew Brestickera6551a72014-09-18 17:18:56 +020028
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020029#define CROS_EC_DEV_EC_INDEX 0
30#define CROS_EC_DEV_PD_INDEX 1
31
Lee Jonescf649e02015-06-15 21:39:39 +080032static struct cros_ec_platform ec_p = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020033 .ec_name = CROS_EC_DEV_NAME,
34 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
35};
36
Lee Jonescf649e02015-06-15 21:39:39 +080037static struct cros_ec_platform pd_p = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020038 .ec_name = CROS_EC_DEV_PD_NAME,
39 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
40};
41
Lee Jonescf649e02015-06-15 21:39:39 +080042static const struct mfd_cell ec_cell = {
Thierry Escandeea01a312017-11-20 17:15:25 +010043 .name = "cros-ec-dev",
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020044 .platform_data = &ec_p,
45 .pdata_size = sizeof(ec_p),
46};
47
Lee Jonescf649e02015-06-15 21:39:39 +080048static const struct mfd_cell ec_pd_cell = {
Thierry Escandeea01a312017-11-20 17:15:25 +010049 .name = "cros-ec-dev",
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020050 .platform_data = &pd_p,
51 .pdata_size = sizeof(pd_p),
Simon Glass4ab61742013-02-25 14:08:37 -080052};
53
Vic Yang6f1d9122016-08-10 19:05:24 +020054static irqreturn_t ec_irq_thread(int irq, void *data)
55{
56 struct cros_ec_device *ec_dev = data;
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +010057 bool wake_event = true;
Vic Yang6f1d9122016-08-10 19:05:24 +020058 int ret;
59
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +010060 ret = cros_ec_get_next_event(ec_dev, &wake_event);
61
62 /*
63 * Signal only if wake host events or any interrupt if
64 * cros_ec_get_next_event() returned an error (default value for
65 * wake_event is true)
66 */
67 if (wake_event && device_may_wakeup(ec_dev->dev))
Vic Yang6f1d9122016-08-10 19:05:24 +020068 pm_wakeup_event(ec_dev->dev, 0);
69
Vic Yang6f1d9122016-08-10 19:05:24 +020070 if (ret > 0)
71 blocking_notifier_call_chain(&ec_dev->event_notifier,
72 0, ec_dev);
73 return IRQ_HANDLED;
74}
75
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +010076static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
77{
78 struct {
79 struct cros_ec_command msg;
80 struct ec_params_host_sleep_event req;
81 } __packed buf;
82
83 memset(&buf, 0, sizeof(buf));
84
85 buf.req.sleep_event = sleep_event;
86
87 buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
88 buf.msg.version = 0;
89 buf.msg.outsize = sizeof(buf.req);
90
91 return cros_ec_cmd_xfer(ec_dev, &buf.msg);
92}
93
Simon Glass4ab61742013-02-25 14:08:37 -080094int cros_ec_register(struct cros_ec_device *ec_dev)
95{
96 struct device *dev = ec_dev->dev;
97 int err = 0;
98
Vic Yang6f1d9122016-08-10 19:05:24 +020099 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
100
Stephen Barber2c7589a2015-06-09 13:04:45 +0200101 ec_dev->max_request = sizeof(struct ec_params_hello);
102 ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
103 ec_dev->max_passthru = 0;
104
105 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
106 if (!ec_dev->din)
107 return -ENOMEM;
108
109 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
110 if (!ec_dev->dout)
111 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800112
Andrew Bresticker63427532014-09-18 17:18:57 +0200113 mutex_init(&ec_dev->lock);
114
Vincent Palatin0dbbf252018-04-18 12:23:58 +0200115 err = cros_ec_query_all(ec_dev);
116 if (err) {
117 dev_err(dev, "Cannot identify the EC: error %d\n", err);
118 return err;
119 }
Stephen Barber2c7589a2015-06-09 13:04:45 +0200120
Vic Yang6f1d9122016-08-10 19:05:24 +0200121 if (ec_dev->irq) {
Vincent Palatineb3f2f22018-04-18 12:23:59 +0200122 err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
123 ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
124 "chromeos-ec", ec_dev);
Vic Yang6f1d9122016-08-10 19:05:24 +0200125 if (err) {
126 dev_err(dev, "Failed to request IRQ %d: %d",
127 ec_dev->irq, err);
128 return err;
129 }
130 }
131
Enric Balletbo i Serra4bc59c2f2018-12-12 18:33:56 +0100132 err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell,
133 1, NULL, ec_dev->irq, NULL);
Simon Glass4ab61742013-02-25 14:08:37 -0800134 if (err) {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200135 dev_err(dev,
136 "Failed to register Embedded Controller subdevice %d\n",
137 err);
Vincent Palatineb3f2f22018-04-18 12:23:59 +0200138 return err;
Simon Glass4ab61742013-02-25 14:08:37 -0800139 }
140
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200141 if (ec_dev->max_passthru) {
142 /*
143 * Register a PD device as well on top of this device.
144 * We make the following assumptions:
145 * - behind an EC, we have a pd
146 * - only one device added.
147 * - the EC is responsive at init time (it is not true for a
148 * sensor hub.
149 */
Enric Balletbo i Serra4bc59c2f2018-12-12 18:33:56 +0100150 err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200151 &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
152 if (err) {
153 dev_err(dev,
154 "Failed to register Power Delivery subdevice %d\n",
155 err);
Vincent Palatineb3f2f22018-04-18 12:23:59 +0200156 return err;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200157 }
158 }
159
Todd Brochbb03ffb2015-05-20 11:31:28 +0200160 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
Benjamin Gaignard2eb131e2017-05-29 17:45:55 +0200161 err = devm_of_platform_populate(dev);
Todd Brochbb03ffb2015-05-20 11:31:28 +0200162 if (err) {
163 mfd_remove_devices(dev);
164 dev_err(dev, "Failed to register sub-devices\n");
Vincent Palatineb3f2f22018-04-18 12:23:59 +0200165 return err;
Todd Brochbb03ffb2015-05-20 11:31:28 +0200166 }
167 }
168
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100169 /*
170 * Clear sleep event - this will fail harmlessly on platforms that
171 * don't implement the sleep event host command.
172 */
173 err = cros_ec_sleep_event(ec_dev, 0);
174 if (err < 0)
175 dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
176 err);
177
Bill Richardson533cec82014-06-18 11:14:03 -0700178 dev_info(dev, "Chrome EC device registered\n");
Simon Glass4ab61742013-02-25 14:08:37 -0800179
180 return 0;
Simon Glass4ab61742013-02-25 14:08:37 -0800181}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100182EXPORT_SYMBOL(cros_ec_register);
Simon Glass4ab61742013-02-25 14:08:37 -0800183
Simon Glass4ab61742013-02-25 14:08:37 -0800184#ifdef CONFIG_PM_SLEEP
185int cros_ec_suspend(struct cros_ec_device *ec_dev)
186{
187 struct device *dev = ec_dev->dev;
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100188 int ret;
189 u8 sleep_event;
190
Wenkai Du99fb0f22018-02-22 13:30:52 -0800191 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
192 HOST_SLEEP_EVENT_S3_SUSPEND :
193 HOST_SLEEP_EVENT_S0IX_SUSPEND;
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100194
195 ret = cros_ec_sleep_event(ec_dev, sleep_event);
196 if (ret < 0)
197 dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
198 ret);
Simon Glass4ab61742013-02-25 14:08:37 -0800199
200 if (device_may_wakeup(dev))
201 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
202
203 disable_irq(ec_dev->irq);
204 ec_dev->was_wake_device = ec_dev->wake_enabled;
Joseph Loa9eb1862016-12-16 18:57:36 +0100205 ec_dev->suspended = true;
Simon Glass4ab61742013-02-25 14:08:37 -0800206
207 return 0;
208}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100209EXPORT_SYMBOL(cros_ec_suspend);
Simon Glass4ab61742013-02-25 14:08:37 -0800210
Ravi Chandra Sadineni38ba34a2018-05-30 12:22:04 -0700211static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
Vic Yang6f1d9122016-08-10 19:05:24 +0200212{
RaviChandra Sadineni61cc15d2018-08-20 08:34:19 -0700213 while (ec_dev->mkbp_event_supported &&
214 cros_ec_get_next_event(ec_dev, NULL) > 0)
Vic Yang6f1d9122016-08-10 19:05:24 +0200215 blocking_notifier_call_chain(&ec_dev->event_notifier,
216 1, ec_dev);
217}
218
Simon Glass4ab61742013-02-25 14:08:37 -0800219int cros_ec_resume(struct cros_ec_device *ec_dev)
220{
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100221 int ret;
222 u8 sleep_event;
223
Joseph Loa9eb1862016-12-16 18:57:36 +0100224 ec_dev->suspended = false;
Simon Glass4ab61742013-02-25 14:08:37 -0800225 enable_irq(ec_dev->irq);
226
Shawn Nematbakhsh9c576bd2017-01-13 16:04:32 +0100227 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
228 HOST_SLEEP_EVENT_S3_RESUME :
229 HOST_SLEEP_EVENT_S0IX_RESUME;
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100230
231 ret = cros_ec_sleep_event(ec_dev, sleep_event);
232 if (ret < 0)
233 dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
234 ret);
235
Simon Glass4ab61742013-02-25 14:08:37 -0800236 if (ec_dev->wake_enabled) {
237 disable_irq_wake(ec_dev->irq);
238 ec_dev->wake_enabled = 0;
239 }
Ravi Chandra Sadineni38ba34a2018-05-30 12:22:04 -0700240 /*
241 * Let the mfd devices know about events that occur during
242 * suspend. This way the clients know what to do with them.
243 */
244 cros_ec_report_events_during_suspend(ec_dev);
245
Simon Glass4ab61742013-02-25 14:08:37 -0800246
247 return 0;
248}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100249EXPORT_SYMBOL(cros_ec_resume);
250
Simon Glass4ab61742013-02-25 14:08:37 -0800251#endif
Bill Richardsona865a582014-04-17 12:32:17 -0700252
253MODULE_LICENSE("GPL");
254MODULE_DESCRIPTION("ChromeOS EC core driver");