This source file includes following definitions.
- ec_irq_thread
- cros_ec_sleep_event
- cros_ec_register
- cros_ec_unregister
- cros_ec_suspend
- cros_ec_report_events_during_suspend
- cros_ec_resume
1
2
3
4
5
6
7
8
9
10
11
12 #include <linux/of_platform.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/platform_data/cros_ec_commands.h>
17 #include <linux/platform_data/cros_ec_proto.h>
18 #include <linux/suspend.h>
19 #include <asm/unaligned.h>
20
21 #define CROS_EC_DEV_EC_INDEX 0
22 #define CROS_EC_DEV_PD_INDEX 1
23
24 static struct cros_ec_platform ec_p = {
25 .ec_name = CROS_EC_DEV_NAME,
26 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
27 };
28
29 static struct cros_ec_platform pd_p = {
30 .ec_name = CROS_EC_DEV_PD_NAME,
31 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
32 };
33
34 static irqreturn_t ec_irq_thread(int irq, void *data)
35 {
36 struct cros_ec_device *ec_dev = data;
37 bool wake_event = true;
38 int ret;
39
40 ret = cros_ec_get_next_event(ec_dev, &wake_event);
41
42
43
44
45
46
47 if (wake_event && device_may_wakeup(ec_dev->dev))
48 pm_wakeup_event(ec_dev->dev, 0);
49
50 if (ret > 0)
51 blocking_notifier_call_chain(&ec_dev->event_notifier,
52 0, ec_dev);
53 return IRQ_HANDLED;
54 }
55
56 static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
57 {
58 int ret;
59 struct {
60 struct cros_ec_command msg;
61 union {
62 struct ec_params_host_sleep_event req0;
63 struct ec_params_host_sleep_event_v1 req1;
64 struct ec_response_host_sleep_event_v1 resp1;
65 } u;
66 } __packed buf;
67
68 memset(&buf, 0, sizeof(buf));
69
70 if (ec_dev->host_sleep_v1) {
71 buf.u.req1.sleep_event = sleep_event;
72 buf.u.req1.suspend_params.sleep_timeout_ms =
73 EC_HOST_SLEEP_TIMEOUT_DEFAULT;
74
75 buf.msg.outsize = sizeof(buf.u.req1);
76 if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
77 (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
78 buf.msg.insize = sizeof(buf.u.resp1);
79
80 buf.msg.version = 1;
81
82 } else {
83 buf.u.req0.sleep_event = sleep_event;
84 buf.msg.outsize = sizeof(buf.u.req0);
85 }
86
87 buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
88
89 ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
90
91
92 if (ret >= 0 && ec_dev->host_sleep_v1 &&
93 (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME)) {
94 ec_dev->last_resume_result =
95 buf.u.resp1.resume_response.sleep_transitions;
96
97 WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
98 EC_HOST_RESUME_SLEEP_TIMEOUT,
99 "EC detected sleep transition timeout. Total slp_s0 transitions: %d",
100 buf.u.resp1.resume_response.sleep_transitions &
101 EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
102 }
103
104 return ret;
105 }
106
107 int cros_ec_register(struct cros_ec_device *ec_dev)
108 {
109 struct device *dev = ec_dev->dev;
110 int err = 0;
111
112 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
113
114 ec_dev->max_request = sizeof(struct ec_params_hello);
115 ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
116 ec_dev->max_passthru = 0;
117
118 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
119 if (!ec_dev->din)
120 return -ENOMEM;
121
122 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
123 if (!ec_dev->dout)
124 return -ENOMEM;
125
126 mutex_init(&ec_dev->lock);
127
128 err = cros_ec_query_all(ec_dev);
129 if (err) {
130 dev_err(dev, "Cannot identify the EC: error %d\n", err);
131 return err;
132 }
133
134 if (ec_dev->irq) {
135 err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
136 ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
137 "chromeos-ec", ec_dev);
138 if (err) {
139 dev_err(dev, "Failed to request IRQ %d: %d",
140 ec_dev->irq, err);
141 return err;
142 }
143 }
144
145
146 ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
147 PLATFORM_DEVID_AUTO, &ec_p,
148 sizeof(struct cros_ec_platform));
149 if (IS_ERR(ec_dev->ec)) {
150 dev_err(ec_dev->dev,
151 "Failed to create CrOS EC platform device\n");
152 return PTR_ERR(ec_dev->ec);
153 }
154
155 if (ec_dev->max_passthru) {
156
157
158
159
160
161
162
163
164 ec_dev->pd = platform_device_register_data(ec_dev->dev,
165 "cros-ec-dev",
166 PLATFORM_DEVID_AUTO, &pd_p,
167 sizeof(struct cros_ec_platform));
168 if (IS_ERR(ec_dev->pd)) {
169 dev_err(ec_dev->dev,
170 "Failed to create CrOS PD platform device\n");
171 platform_device_unregister(ec_dev->ec);
172 return PTR_ERR(ec_dev->pd);
173 }
174 }
175
176 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
177 err = devm_of_platform_populate(dev);
178 if (err) {
179 platform_device_unregister(ec_dev->pd);
180 platform_device_unregister(ec_dev->ec);
181 dev_err(dev, "Failed to register sub-devices\n");
182 return err;
183 }
184 }
185
186
187
188
189
190 err = cros_ec_sleep_event(ec_dev, 0);
191 if (err < 0)
192 dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
193 err);
194
195 dev_info(dev, "Chrome EC device registered\n");
196
197 return 0;
198 }
199 EXPORT_SYMBOL(cros_ec_register);
200
201 int cros_ec_unregister(struct cros_ec_device *ec_dev)
202 {
203 if (ec_dev->pd)
204 platform_device_unregister(ec_dev->pd);
205 platform_device_unregister(ec_dev->ec);
206
207 return 0;
208 }
209 EXPORT_SYMBOL(cros_ec_unregister);
210
211 #ifdef CONFIG_PM_SLEEP
212 int cros_ec_suspend(struct cros_ec_device *ec_dev)
213 {
214 struct device *dev = ec_dev->dev;
215 int ret;
216 u8 sleep_event;
217
218 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
219 HOST_SLEEP_EVENT_S3_SUSPEND :
220 HOST_SLEEP_EVENT_S0IX_SUSPEND;
221
222 ret = cros_ec_sleep_event(ec_dev, sleep_event);
223 if (ret < 0)
224 dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
225 ret);
226
227 if (device_may_wakeup(dev))
228 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
229
230 disable_irq(ec_dev->irq);
231 ec_dev->was_wake_device = ec_dev->wake_enabled;
232 ec_dev->suspended = true;
233
234 return 0;
235 }
236 EXPORT_SYMBOL(cros_ec_suspend);
237
238 static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
239 {
240 while (ec_dev->mkbp_event_supported &&
241 cros_ec_get_next_event(ec_dev, NULL) > 0)
242 blocking_notifier_call_chain(&ec_dev->event_notifier,
243 1, ec_dev);
244 }
245
246 int cros_ec_resume(struct cros_ec_device *ec_dev)
247 {
248 int ret;
249 u8 sleep_event;
250
251 ec_dev->suspended = false;
252 enable_irq(ec_dev->irq);
253
254 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
255 HOST_SLEEP_EVENT_S3_RESUME :
256 HOST_SLEEP_EVENT_S0IX_RESUME;
257
258 ret = cros_ec_sleep_event(ec_dev, sleep_event);
259 if (ret < 0)
260 dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
261 ret);
262
263 if (ec_dev->wake_enabled) {
264 disable_irq_wake(ec_dev->irq);
265 ec_dev->wake_enabled = 0;
266 }
267
268
269
270
271 cros_ec_report_events_during_suspend(ec_dev);
272
273
274 return 0;
275 }
276 EXPORT_SYMBOL(cros_ec_resume);
277
278 #endif
279
280 MODULE_LICENSE("GPL");
281 MODULE_DESCRIPTION("ChromeOS EC core driver");