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> |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 26 | #include <asm/unaligned.h> |
Andrew Bresticker | a6551a7 | 2014-09-18 17:18:56 +0200 | [diff] [blame] | 27 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 28 | #define CROS_EC_DEV_EC_INDEX 0 |
| 29 | #define CROS_EC_DEV_PD_INDEX 1 |
| 30 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 31 | static struct cros_ec_platform ec_p = { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 32 | .ec_name = CROS_EC_DEV_NAME, |
| 33 | .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX), |
| 34 | }; |
| 35 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 36 | static struct cros_ec_platform pd_p = { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 37 | .ec_name = CROS_EC_DEV_PD_NAME, |
| 38 | .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX), |
| 39 | }; |
| 40 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 41 | static const struct mfd_cell ec_cell = { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 42 | .name = "cros-ec-ctl", |
| 43 | .platform_data = &ec_p, |
| 44 | .pdata_size = sizeof(ec_p), |
| 45 | }; |
| 46 | |
Lee Jones | cf649e0 | 2015-06-15 21:39:39 +0800 | [diff] [blame] | 47 | static const struct mfd_cell ec_pd_cell = { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 48 | .name = "cros-ec-ctl", |
| 49 | .platform_data = &pd_p, |
| 50 | .pdata_size = sizeof(pd_p), |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 53 | static irqreturn_t ec_irq_thread(int irq, void *data) |
| 54 | { |
| 55 | struct cros_ec_device *ec_dev = data; |
| 56 | int ret; |
| 57 | |
| 58 | if (device_may_wakeup(ec_dev->dev)) |
| 59 | pm_wakeup_event(ec_dev->dev, 0); |
| 60 | |
| 61 | ret = cros_ec_get_next_event(ec_dev); |
| 62 | if (ret > 0) |
| 63 | blocking_notifier_call_chain(&ec_dev->event_notifier, |
| 64 | 0, ec_dev); |
| 65 | return IRQ_HANDLED; |
| 66 | } |
| 67 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 68 | int cros_ec_register(struct cros_ec_device *ec_dev) |
| 69 | { |
| 70 | struct device *dev = ec_dev->dev; |
| 71 | int err = 0; |
| 72 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 73 | BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier); |
| 74 | |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 75 | ec_dev->max_request = sizeof(struct ec_params_hello); |
| 76 | ec_dev->max_response = sizeof(struct ec_response_get_protocol_info); |
| 77 | ec_dev->max_passthru = 0; |
| 78 | |
| 79 | ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL); |
| 80 | if (!ec_dev->din) |
| 81 | return -ENOMEM; |
| 82 | |
| 83 | ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL); |
| 84 | if (!ec_dev->dout) |
| 85 | return -ENOMEM; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 86 | |
Andrew Bresticker | 6342753 | 2014-09-18 17:18:57 +0200 | [diff] [blame] | 87 | mutex_init(&ec_dev->lock); |
| 88 | |
Vincent Palatin | 5e0b8c1 | 2018-04-18 12:23:58 +0200 | [diff] [blame] | 89 | err = cros_ec_query_all(ec_dev); |
| 90 | if (err) { |
| 91 | dev_err(dev, "Cannot identify the EC: error %d\n", err); |
| 92 | return err; |
| 93 | } |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 94 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 95 | if (ec_dev->irq) { |
| 96 | err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread, |
| 97 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 98 | "chromeos-ec", ec_dev); |
| 99 | if (err) { |
| 100 | dev_err(dev, "Failed to request IRQ %d: %d", |
| 101 | ec_dev->irq, err); |
| 102 | return err; |
| 103 | } |
| 104 | } |
| 105 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 106 | err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1, |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 107 | NULL, ec_dev->irq, NULL); |
| 108 | if (err) { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 109 | dev_err(dev, |
| 110 | "Failed to register Embedded Controller subdevice %d\n", |
| 111 | err); |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 112 | goto fail_mfd; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 115 | if (ec_dev->max_passthru) { |
| 116 | /* |
| 117 | * Register a PD device as well on top of this device. |
| 118 | * We make the following assumptions: |
| 119 | * - behind an EC, we have a pd |
| 120 | * - only one device added. |
| 121 | * - the EC is responsive at init time (it is not true for a |
| 122 | * sensor hub. |
| 123 | */ |
| 124 | err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, |
| 125 | &ec_pd_cell, 1, NULL, ec_dev->irq, NULL); |
| 126 | if (err) { |
| 127 | dev_err(dev, |
| 128 | "Failed to register Power Delivery subdevice %d\n", |
| 129 | err); |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 130 | goto fail_mfd; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 134 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 135 | err = of_platform_populate(dev->of_node, NULL, NULL, dev); |
| 136 | if (err) { |
| 137 | mfd_remove_devices(dev); |
| 138 | dev_err(dev, "Failed to register sub-devices\n"); |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 139 | goto fail_mfd; |
Todd Broch | bb03ffb | 2015-05-20 11:31:28 +0200 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Bill Richardson | 533cec8 | 2014-06-18 11:14:03 -0700 | [diff] [blame] | 143 | dev_info(dev, "Chrome EC device registered\n"); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 144 | |
| 145 | return 0; |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 146 | |
| 147 | fail_mfd: |
| 148 | if (ec_dev->irq) |
| 149 | free_irq(ec_dev->irq, ec_dev); |
| 150 | return err; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 151 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 152 | EXPORT_SYMBOL(cros_ec_register); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 153 | |
| 154 | int cros_ec_remove(struct cros_ec_device *ec_dev) |
| 155 | { |
| 156 | mfd_remove_devices(ec_dev->dev); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 160 | EXPORT_SYMBOL(cros_ec_remove); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 161 | |
| 162 | #ifdef CONFIG_PM_SLEEP |
| 163 | int cros_ec_suspend(struct cros_ec_device *ec_dev) |
| 164 | { |
| 165 | struct device *dev = ec_dev->dev; |
| 166 | |
| 167 | if (device_may_wakeup(dev)) |
| 168 | ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq); |
| 169 | |
| 170 | disable_irq(ec_dev->irq); |
| 171 | ec_dev->was_wake_device = ec_dev->wake_enabled; |
| 172 | |
| 173 | return 0; |
| 174 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 175 | EXPORT_SYMBOL(cros_ec_suspend); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 176 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 177 | static void cros_ec_drain_events(struct cros_ec_device *ec_dev) |
| 178 | { |
| 179 | while (cros_ec_get_next_event(ec_dev) > 0) |
| 180 | blocking_notifier_call_chain(&ec_dev->event_notifier, |
| 181 | 1, ec_dev); |
| 182 | } |
| 183 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 184 | int cros_ec_resume(struct cros_ec_device *ec_dev) |
| 185 | { |
| 186 | enable_irq(ec_dev->irq); |
| 187 | |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 188 | /* |
| 189 | * In some cases, we need to distinguish between events that occur |
| 190 | * during suspend if the EC is not a wake source. For example, |
| 191 | * keypresses during suspend should be discarded if it does not wake |
| 192 | * the system. |
| 193 | * |
| 194 | * If the EC is not a wake source, drain the event queue and mark them |
| 195 | * as "queued during suspend". |
| 196 | */ |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 197 | if (ec_dev->wake_enabled) { |
| 198 | disable_irq_wake(ec_dev->irq); |
| 199 | ec_dev->wake_enabled = 0; |
Vic Yang | 6f1d912 | 2016-08-10 19:05:24 +0200 | [diff] [blame] | 200 | } else { |
| 201 | cros_ec_drain_events(ec_dev); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 206 | EXPORT_SYMBOL(cros_ec_resume); |
| 207 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 208 | #endif |
Bill Richardson | a865a58 | 2014-04-17 12:32:17 -0700 | [diff] [blame] | 209 | |
| 210 | MODULE_LICENSE("GPL"); |
| 211 | MODULE_DESCRIPTION("ChromeOS EC core driver"); |