blob: abd83424b498800921642efcb21b9ca4d6157436 [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>
Vic Yang6f1d9122016-08-10 19:05:24 +020026#include <asm/unaligned.h>
Andrew Brestickera6551a72014-09-18 17:18:56 +020027
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020028#define CROS_EC_DEV_EC_INDEX 0
29#define CROS_EC_DEV_PD_INDEX 1
30
Lee Jonescf649e02015-06-15 21:39:39 +080031static struct cros_ec_platform ec_p = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020032 .ec_name = CROS_EC_DEV_NAME,
33 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
34};
35
Lee Jonescf649e02015-06-15 21:39:39 +080036static struct cros_ec_platform pd_p = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020037 .ec_name = CROS_EC_DEV_PD_NAME,
38 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
39};
40
Lee Jonescf649e02015-06-15 21:39:39 +080041static const struct mfd_cell ec_cell = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020042 .name = "cros-ec-ctl",
43 .platform_data = &ec_p,
44 .pdata_size = sizeof(ec_p),
45};
46
Lee Jonescf649e02015-06-15 21:39:39 +080047static const struct mfd_cell ec_pd_cell = {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020048 .name = "cros-ec-ctl",
49 .platform_data = &pd_p,
50 .pdata_size = sizeof(pd_p),
Simon Glass4ab61742013-02-25 14:08:37 -080051};
52
Vic Yang6f1d9122016-08-10 19:05:24 +020053static 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 Glass4ab61742013-02-25 14:08:37 -080068int cros_ec_register(struct cros_ec_device *ec_dev)
69{
70 struct device *dev = ec_dev->dev;
71 int err = 0;
72
Vic Yang6f1d9122016-08-10 19:05:24 +020073 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
74
Stephen Barber2c7589a2015-06-09 13:04:45 +020075 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 Glass4ab61742013-02-25 14:08:37 -080086
Andrew Bresticker63427532014-09-18 17:18:57 +020087 mutex_init(&ec_dev->lock);
88
Stephen Barber2c7589a2015-06-09 13:04:45 +020089 cros_ec_query_all(ec_dev);
90
Vic Yang6f1d9122016-08-10 19:05:24 +020091 if (ec_dev->irq) {
92 err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread,
93 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
94 "chromeos-ec", ec_dev);
95 if (err) {
96 dev_err(dev, "Failed to request IRQ %d: %d",
97 ec_dev->irq, err);
98 return err;
99 }
100 }
101
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200102 err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1,
Simon Glass4ab61742013-02-25 14:08:37 -0800103 NULL, ec_dev->irq, NULL);
104 if (err) {
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200105 dev_err(dev,
106 "Failed to register Embedded Controller subdevice %d\n",
107 err);
Vic Yang6f1d9122016-08-10 19:05:24 +0200108 goto fail_mfd;
Simon Glass4ab61742013-02-25 14:08:37 -0800109 }
110
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200111 if (ec_dev->max_passthru) {
112 /*
113 * Register a PD device as well on top of this device.
114 * We make the following assumptions:
115 * - behind an EC, we have a pd
116 * - only one device added.
117 * - the EC is responsive at init time (it is not true for a
118 * sensor hub.
119 */
120 err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
121 &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
122 if (err) {
123 dev_err(dev,
124 "Failed to register Power Delivery subdevice %d\n",
125 err);
Vic Yang6f1d9122016-08-10 19:05:24 +0200126 goto fail_mfd;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200127 }
128 }
129
Todd Brochbb03ffb2015-05-20 11:31:28 +0200130 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
131 err = of_platform_populate(dev->of_node, NULL, NULL, dev);
132 if (err) {
133 mfd_remove_devices(dev);
134 dev_err(dev, "Failed to register sub-devices\n");
Vic Yang6f1d9122016-08-10 19:05:24 +0200135 goto fail_mfd;
Todd Brochbb03ffb2015-05-20 11:31:28 +0200136 }
137 }
138
Bill Richardson533cec82014-06-18 11:14:03 -0700139 dev_info(dev, "Chrome EC device registered\n");
Simon Glass4ab61742013-02-25 14:08:37 -0800140
141 return 0;
Vic Yang6f1d9122016-08-10 19:05:24 +0200142
143fail_mfd:
144 if (ec_dev->irq)
145 free_irq(ec_dev->irq, ec_dev);
146 return err;
Simon Glass4ab61742013-02-25 14:08:37 -0800147}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100148EXPORT_SYMBOL(cros_ec_register);
Simon Glass4ab61742013-02-25 14:08:37 -0800149
150int cros_ec_remove(struct cros_ec_device *ec_dev)
151{
152 mfd_remove_devices(ec_dev->dev);
Simon Glass4ab61742013-02-25 14:08:37 -0800153
154 return 0;
155}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100156EXPORT_SYMBOL(cros_ec_remove);
Simon Glass4ab61742013-02-25 14:08:37 -0800157
158#ifdef CONFIG_PM_SLEEP
159int cros_ec_suspend(struct cros_ec_device *ec_dev)
160{
161 struct device *dev = ec_dev->dev;
162
163 if (device_may_wakeup(dev))
164 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
165
166 disable_irq(ec_dev->irq);
167 ec_dev->was_wake_device = ec_dev->wake_enabled;
168
169 return 0;
170}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100171EXPORT_SYMBOL(cros_ec_suspend);
Simon Glass4ab61742013-02-25 14:08:37 -0800172
Vic Yang6f1d9122016-08-10 19:05:24 +0200173static void cros_ec_drain_events(struct cros_ec_device *ec_dev)
174{
175 while (cros_ec_get_next_event(ec_dev) > 0)
176 blocking_notifier_call_chain(&ec_dev->event_notifier,
177 1, ec_dev);
178}
179
Simon Glass4ab61742013-02-25 14:08:37 -0800180int cros_ec_resume(struct cros_ec_device *ec_dev)
181{
182 enable_irq(ec_dev->irq);
183
Vic Yang6f1d9122016-08-10 19:05:24 +0200184 /*
185 * In some cases, we need to distinguish between events that occur
186 * during suspend if the EC is not a wake source. For example,
187 * keypresses during suspend should be discarded if it does not wake
188 * the system.
189 *
190 * If the EC is not a wake source, drain the event queue and mark them
191 * as "queued during suspend".
192 */
Simon Glass4ab61742013-02-25 14:08:37 -0800193 if (ec_dev->wake_enabled) {
194 disable_irq_wake(ec_dev->irq);
195 ec_dev->wake_enabled = 0;
Vic Yang6f1d9122016-08-10 19:05:24 +0200196 } else {
197 cros_ec_drain_events(ec_dev);
Simon Glass4ab61742013-02-25 14:08:37 -0800198 }
199
200 return 0;
201}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100202EXPORT_SYMBOL(cros_ec_resume);
203
Simon Glass4ab61742013-02-25 14:08:37 -0800204#endif
Bill Richardsona865a582014-04-17 12:32:17 -0700205
206MODULE_LICENSE("GPL");
207MODULE_DESCRIPTION("ChromeOS EC core driver");