blob: dc6ce90916949d0821ca5d72f27d266a99294dab [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 = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020043 .name = "cros-ec-ctl",
44 .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 = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020049 .name = "cros-ec-ctl",
50 .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;
57 int ret;
58
59 if (device_may_wakeup(ec_dev->dev))
60 pm_wakeup_event(ec_dev->dev, 0);
61
62 ret = cros_ec_get_next_event(ec_dev);
63 if (ret > 0)
64 blocking_notifier_call_chain(&ec_dev->event_notifier,
65 0, ec_dev);
66 return IRQ_HANDLED;
67}
68
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +010069static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
70{
71 struct {
72 struct cros_ec_command msg;
73 struct ec_params_host_sleep_event req;
74 } __packed buf;
75
76 memset(&buf, 0, sizeof(buf));
77
78 buf.req.sleep_event = sleep_event;
79
80 buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
81 buf.msg.version = 0;
82 buf.msg.outsize = sizeof(buf.req);
83
84 return cros_ec_cmd_xfer(ec_dev, &buf.msg);
85}
86
Simon Glass4ab61742013-02-25 14:08:37 -080087int cros_ec_register(struct cros_ec_device *ec_dev)
88{
89 struct device *dev = ec_dev->dev;
90 int err = 0;
91
Vic Yang6f1d9122016-08-10 19:05:24 +020092 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
93
Stephen Barber2c7589a2015-06-09 13:04:45 +020094 ec_dev->max_request = sizeof(struct ec_params_hello);
95 ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
96 ec_dev->max_passthru = 0;
97
98 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
99 if (!ec_dev->din)
100 return -ENOMEM;
101
102 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
103 if (!ec_dev->dout)
104 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800105
Andrew Bresticker63427532014-09-18 17:18:57 +0200106 mutex_init(&ec_dev->lock);
107
Stephen Barber2c7589a2015-06-09 13:04:45 +0200108 cros_ec_query_all(ec_dev);
109
Vic Yang6f1d9122016-08-10 19:05:24 +0200110 if (ec_dev->irq) {
111 err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread,
112 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
113 "chromeos-ec", ec_dev);
114 if (err) {
115 dev_err(dev, "Failed to request IRQ %d: %d",
116 ec_dev->irq, err);
117 return err;
118 }
119 }
120
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200121 err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1,
Simon Glass4ab61742013-02-25 14:08:37 -0800122 NULL, ec_dev->irq, NULL);
123 if (err) {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200124 dev_err(dev,
125 "Failed to register Embedded Controller subdevice %d\n",
126 err);
Vic Yang6f1d9122016-08-10 19:05:24 +0200127 goto fail_mfd;
Simon Glass4ab61742013-02-25 14:08:37 -0800128 }
129
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200130 if (ec_dev->max_passthru) {
131 /*
132 * Register a PD device as well on top of this device.
133 * We make the following assumptions:
134 * - behind an EC, we have a pd
135 * - only one device added.
136 * - the EC is responsive at init time (it is not true for a
137 * sensor hub.
138 */
139 err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
140 &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
141 if (err) {
142 dev_err(dev,
143 "Failed to register Power Delivery subdevice %d\n",
144 err);
Vic Yang6f1d9122016-08-10 19:05:24 +0200145 goto fail_mfd;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200146 }
147 }
148
Todd Brochbb03ffb2015-05-20 11:31:28 +0200149 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
Benjamin Gaignard2eb131e2017-05-29 17:45:55 +0200150 err = devm_of_platform_populate(dev);
Todd Brochbb03ffb2015-05-20 11:31:28 +0200151 if (err) {
152 mfd_remove_devices(dev);
153 dev_err(dev, "Failed to register sub-devices\n");
Vic Yang6f1d9122016-08-10 19:05:24 +0200154 goto fail_mfd;
Todd Brochbb03ffb2015-05-20 11:31:28 +0200155 }
156 }
157
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100158 /*
159 * Clear sleep event - this will fail harmlessly on platforms that
160 * don't implement the sleep event host command.
161 */
162 err = cros_ec_sleep_event(ec_dev, 0);
163 if (err < 0)
164 dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
165 err);
166
Bill Richardson533cec82014-06-18 11:14:03 -0700167 dev_info(dev, "Chrome EC device registered\n");
Simon Glass4ab61742013-02-25 14:08:37 -0800168
Archana Patnie04653a2017-02-01 17:22:03 +0100169 cros_ec_acpi_install_gpe_handler(dev);
170
Simon Glass4ab61742013-02-25 14:08:37 -0800171 return 0;
Vic Yang6f1d9122016-08-10 19:05:24 +0200172
173fail_mfd:
174 if (ec_dev->irq)
175 free_irq(ec_dev->irq, ec_dev);
176 return err;
Simon Glass4ab61742013-02-25 14:08:37 -0800177}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100178EXPORT_SYMBOL(cros_ec_register);
Simon Glass4ab61742013-02-25 14:08:37 -0800179
180int cros_ec_remove(struct cros_ec_device *ec_dev)
181{
182 mfd_remove_devices(ec_dev->dev);
Simon Glass4ab61742013-02-25 14:08:37 -0800183
Archana Patnie04653a2017-02-01 17:22:03 +0100184 cros_ec_acpi_remove_gpe_handler();
185
Jeffy Chenf58b14e2017-06-12 16:42:46 +0800186 if (ec_dev->irq)
187 free_irq(ec_dev->irq, ec_dev);
188
Simon Glass4ab61742013-02-25 14:08:37 -0800189 return 0;
190}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100191EXPORT_SYMBOL(cros_ec_remove);
Simon Glass4ab61742013-02-25 14:08:37 -0800192
193#ifdef CONFIG_PM_SLEEP
194int cros_ec_suspend(struct cros_ec_device *ec_dev)
195{
196 struct device *dev = ec_dev->dev;
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100197 int ret;
198 u8 sleep_event;
199
Archana Patnie04653a2017-02-01 17:22:03 +0100200 if (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) {
201 sleep_event = HOST_SLEEP_EVENT_S3_SUSPEND;
202 } else {
203 sleep_event = HOST_SLEEP_EVENT_S0IX_SUSPEND;
204
205 /* Clearing the GPE status for any pending event */
206 cros_ec_acpi_clear_gpe();
207 }
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100208
209 ret = cros_ec_sleep_event(ec_dev, sleep_event);
210 if (ret < 0)
211 dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
212 ret);
Simon Glass4ab61742013-02-25 14:08:37 -0800213
214 if (device_may_wakeup(dev))
215 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
216
217 disable_irq(ec_dev->irq);
218 ec_dev->was_wake_device = ec_dev->wake_enabled;
Joseph Loa9eb1862016-12-16 18:57:36 +0100219 ec_dev->suspended = true;
Simon Glass4ab61742013-02-25 14:08:37 -0800220
221 return 0;
222}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100223EXPORT_SYMBOL(cros_ec_suspend);
Simon Glass4ab61742013-02-25 14:08:37 -0800224
Vic Yang6f1d9122016-08-10 19:05:24 +0200225static void cros_ec_drain_events(struct cros_ec_device *ec_dev)
226{
227 while (cros_ec_get_next_event(ec_dev) > 0)
228 blocking_notifier_call_chain(&ec_dev->event_notifier,
229 1, ec_dev);
230}
231
Simon Glass4ab61742013-02-25 14:08:37 -0800232int cros_ec_resume(struct cros_ec_device *ec_dev)
233{
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100234 int ret;
235 u8 sleep_event;
236
Joseph Loa9eb1862016-12-16 18:57:36 +0100237 ec_dev->suspended = false;
Simon Glass4ab61742013-02-25 14:08:37 -0800238 enable_irq(ec_dev->irq);
239
Shawn Nematbakhsh9c576bd2017-01-13 16:04:32 +0100240 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
241 HOST_SLEEP_EVENT_S3_RESUME :
242 HOST_SLEEP_EVENT_S0IX_RESUME;
Shawn Nematbakhshf00c06f2016-12-16 18:57:37 +0100243
244 ret = cros_ec_sleep_event(ec_dev, sleep_event);
245 if (ret < 0)
246 dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
247 ret);
248
Vic Yang6f1d9122016-08-10 19:05:24 +0200249 /*
250 * In some cases, we need to distinguish between events that occur
251 * during suspend if the EC is not a wake source. For example,
252 * keypresses during suspend should be discarded if it does not wake
253 * the system.
254 *
255 * If the EC is not a wake source, drain the event queue and mark them
256 * as "queued during suspend".
257 */
Simon Glass4ab61742013-02-25 14:08:37 -0800258 if (ec_dev->wake_enabled) {
259 disable_irq_wake(ec_dev->irq);
260 ec_dev->wake_enabled = 0;
Vic Yang6f1d9122016-08-10 19:05:24 +0200261 } else {
262 cros_ec_drain_events(ec_dev);
Simon Glass4ab61742013-02-25 14:08:37 -0800263 }
264
265 return 0;
266}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100267EXPORT_SYMBOL(cros_ec_resume);
268
Simon Glass4ab61742013-02-25 14:08:37 -0800269#endif
Bill Richardsona865a582014-04-17 12:32:17 -0700270
271MODULE_LICENSE("GPL");
272MODULE_DESCRIPTION("ChromeOS EC core driver");