Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 1 | /* |
| 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 Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 20 | #include <linux/of_platform.h> |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/slab.h> |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 23 | #include <linux/module.h> |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 24 | #include <linux/mfd/core.h> |
| 25 | #include <linux/mfd/cros_ec.h> |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 26 | #include <linux/suspend.h> |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 27 | #include <asm/unaligned.h> |
Andrew Bresticker | a6551a7 | 2014-09-18 17:18:56 +0200 | [diff] [blame] | 28 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 29 | #define CROS_EC_DEV_EC_INDEX 0 |
| 30 | #define CROS_EC_DEV_PD_INDEX 1 |
| 31 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 32 | static struct cros_ec_platform ec_p = { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 33 | .ec_name = CROS_EC_DEV_NAME, |
| 34 | .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX), |
| 35 | }; |
| 36 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 37 | static struct cros_ec_platform pd_p = { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 38 | .ec_name = CROS_EC_DEV_PD_NAME, |
| 39 | .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX), |
| 40 | }; |
| 41 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 42 | static const struct mfd_cell ec_cell = { |
Thierry Escande | ea01a31 | 2017-11-20 17:15:25 +0100 | [diff] [blame] | 43 | .name = "cros-ec-dev", |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 44 | .platform_data = &ec_p, |
| 45 | .pdata_size = sizeof(ec_p), |
| 46 | }; |
| 47 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 48 | static const struct mfd_cell ec_pd_cell = { |
Thierry Escande | ea01a31 | 2017-11-20 17:15:25 +0100 | [diff] [blame] | 49 | .name = "cros-ec-dev", |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 50 | .platform_data = &pd_p, |
| 51 | .pdata_size = sizeof(pd_p), |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 54 | static irqreturn_t ec_irq_thread(int irq, void *data) |
| 55 | { |
| 56 | struct cros_ec_device *ec_dev = data; |
Shawn Nematbakhsh | 29d99b9 | 2017-02-14 20:58:02 +0100 | [diff] [blame] | 57 | bool wake_event = true; |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 58 | int ret; |
| 59 | |
Shawn Nematbakhsh | 29d99b9 | 2017-02-14 20:58:02 +0100 | [diff] [blame] | 60 | 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 Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 68 | pm_wakeup_event(ec_dev->dev, 0); |
| 69 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 70 | if (ret > 0) |
| 71 | blocking_notifier_call_chain(&ec_dev->event_notifier, |
| 72 | 0, ec_dev); |
| 73 | return IRQ_HANDLED; |
| 74 | } |
| 75 | |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 76 | static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event) |
| 77 | { |
Evan Green | 7235560 | 2019-04-03 14:34:28 -0700 | [diff] [blame] | 78 | int ret; |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 79 | struct { |
| 80 | struct cros_ec_command msg; |
Evan Green | 7235560 | 2019-04-03 14:34:28 -0700 | [diff] [blame] | 81 | union { |
| 82 | struct ec_params_host_sleep_event req0; |
| 83 | struct ec_params_host_sleep_event_v1 req1; |
| 84 | struct ec_response_host_sleep_event_v1 resp1; |
| 85 | } u; |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 86 | } __packed buf; |
| 87 | |
| 88 | memset(&buf, 0, sizeof(buf)); |
| 89 | |
Evan Green | 7235560 | 2019-04-03 14:34:28 -0700 | [diff] [blame] | 90 | if (ec_dev->host_sleep_v1) { |
| 91 | buf.u.req1.sleep_event = sleep_event; |
| 92 | buf.u.req1.suspend_params.sleep_timeout_ms = |
| 93 | EC_HOST_SLEEP_TIMEOUT_DEFAULT; |
| 94 | |
| 95 | buf.msg.outsize = sizeof(buf.u.req1); |
| 96 | if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) || |
| 97 | (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME)) |
| 98 | buf.msg.insize = sizeof(buf.u.resp1); |
| 99 | |
| 100 | buf.msg.version = 1; |
| 101 | |
| 102 | } else { |
| 103 | buf.u.req0.sleep_event = sleep_event; |
| 104 | buf.msg.outsize = sizeof(buf.u.req0); |
| 105 | } |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 106 | |
| 107 | buf.msg.command = EC_CMD_HOST_SLEEP_EVENT; |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 108 | |
Evan Green | 7235560 | 2019-04-03 14:34:28 -0700 | [diff] [blame] | 109 | ret = cros_ec_cmd_xfer(ec_dev, &buf.msg); |
| 110 | |
| 111 | /* For now, report failure to transition to S0ix with a warning. */ |
| 112 | if (ret >= 0 && ec_dev->host_sleep_v1 && |
| 113 | (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME)) |
| 114 | WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions & |
| 115 | EC_HOST_RESUME_SLEEP_TIMEOUT, |
| 116 | "EC detected sleep transition timeout. Total slp_s0 transitions: %d", |
| 117 | buf.u.resp1.resume_response.sleep_transitions & |
| 118 | EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK); |
| 119 | |
| 120 | return ret; |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 123 | int cros_ec_register(struct cros_ec_device *ec_dev) |
| 124 | { |
| 125 | struct device *dev = ec_dev->dev; |
| 126 | int err = 0; |
| 127 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 128 | BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier); |
| 129 | |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 130 | ec_dev->max_request = sizeof(struct ec_params_hello); |
| 131 | ec_dev->max_response = sizeof(struct ec_response_get_protocol_info); |
| 132 | ec_dev->max_passthru = 0; |
| 133 | |
| 134 | ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL); |
| 135 | if (!ec_dev->din) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL); |
| 139 | if (!ec_dev->dout) |
| 140 | return -ENOMEM; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 141 | |
Andrew Bresticker | 6342753 | 2014-09-18 17:18:57 +0200 | [diff] [blame] | 142 | mutex_init(&ec_dev->lock); |
| 143 | |
Vincent Palatin | 0dbbf25 | 2018-04-18 12:23:58 +0200 | [diff] [blame] | 144 | err = cros_ec_query_all(ec_dev); |
| 145 | if (err) { |
| 146 | dev_err(dev, "Cannot identify the EC: error %d\n", err); |
| 147 | return err; |
| 148 | } |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 149 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 150 | if (ec_dev->irq) { |
Vincent Palatin | eb3f2f2 | 2018-04-18 12:23:59 +0200 | [diff] [blame] | 151 | err = devm_request_threaded_irq(dev, ec_dev->irq, NULL, |
| 152 | ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 153 | "chromeos-ec", ec_dev); |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 154 | if (err) { |
| 155 | dev_err(dev, "Failed to request IRQ %d: %d", |
| 156 | ec_dev->irq, err); |
| 157 | return err; |
| 158 | } |
| 159 | } |
| 160 | |
Enric Balletbo i Serra | 4bc59c2f | 2018-12-12 18:33:56 +0100 | [diff] [blame] | 161 | err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, |
| 162 | 1, NULL, ec_dev->irq, NULL); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 163 | if (err) { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 164 | dev_err(dev, |
| 165 | "Failed to register Embedded Controller subdevice %d\n", |
| 166 | err); |
Vincent Palatin | eb3f2f2 | 2018-04-18 12:23:59 +0200 | [diff] [blame] | 167 | return err; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 170 | if (ec_dev->max_passthru) { |
| 171 | /* |
| 172 | * Register a PD device as well on top of this device. |
| 173 | * We make the following assumptions: |
| 174 | * - behind an EC, we have a pd |
| 175 | * - only one device added. |
| 176 | * - the EC is responsive at init time (it is not true for a |
| 177 | * sensor hub. |
| 178 | */ |
Enric Balletbo i Serra | 4bc59c2f | 2018-12-12 18:33:56 +0100 | [diff] [blame] | 179 | err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 180 | &ec_pd_cell, 1, NULL, ec_dev->irq, NULL); |
| 181 | if (err) { |
| 182 | dev_err(dev, |
| 183 | "Failed to register Power Delivery subdevice %d\n", |
| 184 | err); |
Vincent Palatin | eb3f2f2 | 2018-04-18 12:23:59 +0200 | [diff] [blame] | 185 | return err; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 189 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
Benjamin Gaignard | 2eb131e | 2017-05-29 17:45:55 +0200 | [diff] [blame] | 190 | err = devm_of_platform_populate(dev); |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 191 | if (err) { |
| 192 | mfd_remove_devices(dev); |
| 193 | dev_err(dev, "Failed to register sub-devices\n"); |
Vincent Palatin | eb3f2f2 | 2018-04-18 12:23:59 +0200 | [diff] [blame] | 194 | return err; |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 198 | /* |
| 199 | * Clear sleep event - this will fail harmlessly on platforms that |
| 200 | * don't implement the sleep event host command. |
| 201 | */ |
| 202 | err = cros_ec_sleep_event(ec_dev, 0); |
| 203 | if (err < 0) |
| 204 | dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec", |
| 205 | err); |
| 206 | |
Bill Richardson | 533cec8 | 2014-06-18 11:14:03 -0700 | [diff] [blame] | 207 | dev_info(dev, "Chrome EC device registered\n"); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 208 | |
| 209 | return 0; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 210 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 211 | EXPORT_SYMBOL(cros_ec_register); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 212 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 213 | #ifdef CONFIG_PM_SLEEP |
| 214 | int cros_ec_suspend(struct cros_ec_device *ec_dev) |
| 215 | { |
| 216 | struct device *dev = ec_dev->dev; |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 217 | int ret; |
| 218 | u8 sleep_event; |
| 219 | |
Wenkai Du | 99fb0f2 | 2018-02-22 13:30:52 -0800 | [diff] [blame] | 220 | sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ? |
| 221 | HOST_SLEEP_EVENT_S3_SUSPEND : |
| 222 | HOST_SLEEP_EVENT_S0IX_SUSPEND; |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 223 | |
| 224 | ret = cros_ec_sleep_event(ec_dev, sleep_event); |
| 225 | if (ret < 0) |
| 226 | dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec", |
| 227 | ret); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 228 | |
| 229 | if (device_may_wakeup(dev)) |
| 230 | ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq); |
| 231 | |
| 232 | disable_irq(ec_dev->irq); |
| 233 | ec_dev->was_wake_device = ec_dev->wake_enabled; |
Joseph Lo | a9eb186 | 2016-12-16 18:57:36 +0100 | [diff] [blame] | 234 | ec_dev->suspended = true; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 238 | EXPORT_SYMBOL(cros_ec_suspend); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 239 | |
Ravi Chandra Sadineni | 38ba34a | 2018-05-30 12:22:04 -0700 | [diff] [blame] | 240 | static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev) |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 241 | { |
RaviChandra Sadineni | 61cc15d | 2018-08-20 08:34:19 -0700 | [diff] [blame] | 242 | while (ec_dev->mkbp_event_supported && |
| 243 | cros_ec_get_next_event(ec_dev, NULL) > 0) |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 244 | blocking_notifier_call_chain(&ec_dev->event_notifier, |
| 245 | 1, ec_dev); |
| 246 | } |
| 247 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 248 | int cros_ec_resume(struct cros_ec_device *ec_dev) |
| 249 | { |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 250 | int ret; |
| 251 | u8 sleep_event; |
| 252 | |
Joseph Lo | a9eb186 | 2016-12-16 18:57:36 +0100 | [diff] [blame] | 253 | ec_dev->suspended = false; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 254 | enable_irq(ec_dev->irq); |
| 255 | |
Shawn Nematbakhsh | 9c576bd | 2017-01-13 16:04:32 +0100 | [diff] [blame] | 256 | sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ? |
| 257 | HOST_SLEEP_EVENT_S3_RESUME : |
| 258 | HOST_SLEEP_EVENT_S0IX_RESUME; |
Shawn Nematbakhsh | f00c06f | 2016-12-16 18:57:37 +0100 | [diff] [blame] | 259 | |
| 260 | ret = cros_ec_sleep_event(ec_dev, sleep_event); |
| 261 | if (ret < 0) |
| 262 | dev_dbg(ec_dev->dev, "Error %d sending resume event to ec", |
| 263 | ret); |
| 264 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 265 | if (ec_dev->wake_enabled) { |
| 266 | disable_irq_wake(ec_dev->irq); |
| 267 | ec_dev->wake_enabled = 0; |
| 268 | } |
Ravi Chandra Sadineni | 38ba34a | 2018-05-30 12:22:04 -0700 | [diff] [blame] | 269 | /* |
| 270 | * Let the mfd devices know about events that occur during |
| 271 | * suspend. This way the clients know what to do with them. |
| 272 | */ |
| 273 | cros_ec_report_events_during_suspend(ec_dev); |
| 274 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 275 | |
| 276 | return 0; |
| 277 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 278 | EXPORT_SYMBOL(cros_ec_resume); |
| 279 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 280 | #endif |
Bill Richardson | a865a58 | 2014-04-17 12:32:17 -0700 | [diff] [blame] | 281 | |
| 282 | MODULE_LICENSE("GPL"); |
| 283 | MODULE_DESCRIPTION("ChromeOS EC core driver"); |