Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ChromeOS EC multi-function device
  4 *
  5 * Copyright (C) 2012 Google, Inc
  6 *
  7 * The ChromeOS EC multi function device is used to mux all the requests
  8 * to the EC device for its multiple features: keyboard controller,
  9 * battery charging and regulator control, firmware update.
 10 */
 11
 
 12#include <linux/interrupt.h>
 
 13#include <linux/module.h>
 14#include <linux/of_platform.h>
 15#include <linux/platform_device.h>
 16#include <linux/platform_data/cros_ec_commands.h>
 17#include <linux/platform_data/cros_ec_proto.h>
 18#include <linux/slab.h>
 19#include <linux/suspend.h>
 20
 21#include "cros_ec.h"
 22
 
 
 
 23static struct cros_ec_platform ec_p = {
 24	.ec_name = CROS_EC_DEV_NAME,
 25	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
 26};
 27
 28static struct cros_ec_platform pd_p = {
 29	.ec_name = CROS_EC_DEV_PD_NAME,
 30	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
 31};
 32
 33/**
 34 * cros_ec_irq_handler() - top half part of the interrupt handler
 35 * @irq: IRQ id
 36 * @data: (ec_dev) Device with events to process.
 37 *
 38 * Return: Wakeup the bottom half
 39 */
 40static irqreturn_t cros_ec_irq_handler(int irq, void *data)
 41{
 42	struct cros_ec_device *ec_dev = data;
 43
 44	ec_dev->last_event_time = cros_ec_get_time_ns();
 45
 46	return IRQ_WAKE_THREAD;
 47}
 48
 49/**
 50 * cros_ec_handle_event() - process and forward pending events on EC
 51 * @ec_dev: Device with events to process.
 52 *
 53 * Call this function in a loop when the kernel is notified that the EC has
 54 * pending events.
 55 *
 56 * Return: true if more events are still pending and this function should be
 57 * called again.
 58 */
 59static bool cros_ec_handle_event(struct cros_ec_device *ec_dev)
 60{
 61	bool wake_event;
 62	bool ec_has_more_events;
 63	int ret;
 64
 65	ret = cros_ec_get_next_event(ec_dev, &wake_event, &ec_has_more_events);
 66
 67	/*
 68	 * Signal only if wake host events or any interrupt if
 69	 * cros_ec_get_next_event() returned an error (default value for
 70	 * wake_event is true)
 71	 */
 72	if (wake_event && device_may_wakeup(ec_dev->dev))
 73		pm_wakeup_event(ec_dev->dev, 0);
 74
 75	if (ret > 0)
 76		blocking_notifier_call_chain(&ec_dev->event_notifier,
 77					     0, ec_dev);
 78
 79	return ec_has_more_events;
 80}
 
 81
 82/**
 83 * cros_ec_irq_thread() - bottom half part of the interrupt handler
 84 * @irq: IRQ id
 85 * @data: (ec_dev) Device with events to process.
 86 *
 87 * Return: Interrupt handled.
 88 */
 89irqreturn_t cros_ec_irq_thread(int irq, void *data)
 90{
 91	struct cros_ec_device *ec_dev = data;
 92	bool ec_has_more_events;
 93
 94	do {
 95		ec_has_more_events = cros_ec_handle_event(ec_dev);
 96	} while (ec_has_more_events);
 97
 98	return IRQ_HANDLED;
 99}
100EXPORT_SYMBOL(cros_ec_irq_thread);
101
102static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
103{
104	int ret;
105	struct {
106		struct cros_ec_command msg;
107		union {
108			struct ec_params_host_sleep_event req0;
109			struct ec_params_host_sleep_event_v1 req1;
110			struct ec_response_host_sleep_event_v1 resp1;
111		} u;
112	} __packed buf;
113
114	memset(&buf, 0, sizeof(buf));
115
116	if (ec_dev->host_sleep_v1) {
117		buf.u.req1.sleep_event = sleep_event;
118		buf.u.req1.suspend_params.sleep_timeout_ms =
119				ec_dev->suspend_timeout_ms;
120
121		buf.msg.outsize = sizeof(buf.u.req1);
122		if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
123		    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
124			buf.msg.insize = sizeof(buf.u.resp1);
125
126		buf.msg.version = 1;
127
128	} else {
129		buf.u.req0.sleep_event = sleep_event;
130		buf.msg.outsize = sizeof(buf.u.req0);
131	}
132
133	buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
134
135	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
136	/* Report failure to transition to system wide suspend with a warning. */
 
137	if (ret >= 0 && ec_dev->host_sleep_v1 &&
138	    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME ||
139	     sleep_event == HOST_SLEEP_EVENT_S3_RESUME)) {
140		ec_dev->last_resume_result =
141			buf.u.resp1.resume_response.sleep_transitions;
142
143		WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
144			  EC_HOST_RESUME_SLEEP_TIMEOUT,
145			  "EC detected sleep transition timeout. Total sleep transitions: %d",
146			  buf.u.resp1.resume_response.sleep_transitions &
147			  EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
148	}
149
150	return ret;
151}
152
153static int cros_ec_ready_event(struct notifier_block *nb,
154			       unsigned long queued_during_suspend,
155			       void *_notify)
156{
157	struct cros_ec_device *ec_dev = container_of(nb, struct cros_ec_device,
158						     notifier_ready);
159	u32 host_event = cros_ec_get_host_event(ec_dev);
160
161	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INTERFACE_READY)) {
162		mutex_lock(&ec_dev->lock);
163		cros_ec_query_all(ec_dev);
164		mutex_unlock(&ec_dev->lock);
165		return NOTIFY_OK;
166	}
167
168	return NOTIFY_DONE;
169}
170
171/**
172 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
173 * @ec_dev: Device to register.
174 *
175 * Before calling this, allocate a pointer to a new device and then fill
176 * in all the fields up to the --private-- marker.
177 *
178 * Return: 0 on success or negative error code.
179 */
180int cros_ec_register(struct cros_ec_device *ec_dev)
181{
182	struct device *dev = ec_dev->dev;
183	int err = 0;
184
185	BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
186	BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->panic_notifier);
187
188	ec_dev->max_request = sizeof(struct ec_params_hello);
189	ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
190	ec_dev->max_passthru = 0;
191	ec_dev->ec = NULL;
192	ec_dev->pd = NULL;
193	ec_dev->suspend_timeout_ms = EC_HOST_SLEEP_TIMEOUT_DEFAULT;
194
195	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
196	if (!ec_dev->din)
197		return -ENOMEM;
198
199	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
200	if (!ec_dev->dout)
201		return -ENOMEM;
202
203	lockdep_register_key(&ec_dev->lockdep_key);
204	mutex_init(&ec_dev->lock);
205	lockdep_set_class(&ec_dev->lock, &ec_dev->lockdep_key);
206
207	err = cros_ec_query_all(ec_dev);
208	if (err) {
209		dev_err(dev, "Cannot identify the EC: error %d\n", err);
210		goto exit;
211	}
212
213	if (ec_dev->irq > 0) {
214		err = devm_request_threaded_irq(dev, ec_dev->irq,
215						cros_ec_irq_handler,
216						cros_ec_irq_thread,
217						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
218						"chromeos-ec", ec_dev);
219		if (err) {
220			dev_err(dev, "Failed to request IRQ %d: %d\n",
221				ec_dev->irq, err);
222			goto exit;
223		}
224	}
225
226	/* Register a platform device for the main EC instance */
227	ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
228					PLATFORM_DEVID_AUTO, &ec_p,
229					sizeof(struct cros_ec_platform));
230	if (IS_ERR(ec_dev->ec)) {
231		dev_err(ec_dev->dev,
232			"Failed to create CrOS EC platform device\n");
233		err = PTR_ERR(ec_dev->ec);
234		goto exit;
235	}
236
237	if (ec_dev->max_passthru) {
238		/*
239		 * Register a platform device for the PD behind the main EC.
240		 * We make the following assumptions:
241		 * - behind an EC, we have a pd
242		 * - only one device added.
243		 * - the EC is responsive at init time (it is not true for a
244		 *   sensor hub).
245		 */
246		ec_dev->pd = platform_device_register_data(ec_dev->dev,
247					"cros-ec-dev",
248					PLATFORM_DEVID_AUTO, &pd_p,
249					sizeof(struct cros_ec_platform));
250		if (IS_ERR(ec_dev->pd)) {
251			dev_err(ec_dev->dev,
252				"Failed to create CrOS PD platform device\n");
253			err = PTR_ERR(ec_dev->pd);
254			goto exit;
255		}
256	}
257
258	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
259		err = devm_of_platform_populate(dev);
260		if (err) {
 
 
261			dev_err(dev, "Failed to register sub-devices\n");
262			goto exit;
263		}
264	}
265
266	/*
267	 * Clear sleep event - this will fail harmlessly on platforms that
268	 * don't implement the sleep event host command.
269	 */
270	err = cros_ec_sleep_event(ec_dev, 0);
271	if (err < 0)
272		dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec\n",
273			err);
274
275	if (ec_dev->mkbp_event_supported) {
276		/*
277		 * Register the notifier for EC_HOST_EVENT_INTERFACE_READY
278		 * event.
279		 */
280		ec_dev->notifier_ready.notifier_call = cros_ec_ready_event;
281		err = blocking_notifier_chain_register(&ec_dev->event_notifier,
282						      &ec_dev->notifier_ready);
283		if (err)
284			goto exit;
285	}
286
287	dev_info(dev, "Chrome EC device registered\n");
288
289	/*
290	 * Unlock EC that may be waiting for AP to process MKBP events.
291	 * If the AP takes to long to answer, the EC would stop sending events.
292	 */
293	if (ec_dev->mkbp_event_supported)
294		cros_ec_irq_thread(0, ec_dev);
295
296	return 0;
297exit:
298	platform_device_unregister(ec_dev->ec);
299	platform_device_unregister(ec_dev->pd);
300	mutex_destroy(&ec_dev->lock);
301	lockdep_unregister_key(&ec_dev->lockdep_key);
302	return err;
303}
304EXPORT_SYMBOL(cros_ec_register);
305
306/**
307 * cros_ec_unregister() - Remove a ChromeOS EC.
308 * @ec_dev: Device to unregister.
309 *
310 * Call this to deregister a ChromeOS EC, then clean up any private data.
311 *
312 * Return: 0 on success or negative error code.
313 */
314void cros_ec_unregister(struct cros_ec_device *ec_dev)
315{
316	platform_device_unregister(ec_dev->pd);
 
317	platform_device_unregister(ec_dev->ec);
318	mutex_destroy(&ec_dev->lock);
319	lockdep_unregister_key(&ec_dev->lockdep_key);
320}
321EXPORT_SYMBOL(cros_ec_unregister);
322
323#ifdef CONFIG_PM_SLEEP
324static void cros_ec_send_suspend_event(struct cros_ec_device *ec_dev)
 
 
 
 
 
 
 
 
325{
 
326	int ret;
327	u8 sleep_event;
328
329	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
330		      HOST_SLEEP_EVENT_S3_SUSPEND :
331		      HOST_SLEEP_EVENT_S0IX_SUSPEND;
332
333	ret = cros_ec_sleep_event(ec_dev, sleep_event);
334	if (ret < 0)
335		dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec\n",
336			ret);
337}
338
339/**
340 * cros_ec_suspend_prepare() - Handle a suspend prepare operation for the ChromeOS EC device.
341 * @ec_dev: Device to suspend.
342 *
343 * This can be called by drivers to handle a suspend prepare stage of suspend.
344 *
345 * Return: 0 always.
346 */
347int cros_ec_suspend_prepare(struct cros_ec_device *ec_dev)
348{
349	cros_ec_send_suspend_event(ec_dev);
350	return 0;
351}
352EXPORT_SYMBOL(cros_ec_suspend_prepare);
353
354static void cros_ec_disable_irq(struct cros_ec_device *ec_dev)
355{
356	struct device *dev = ec_dev->dev;
357	if (device_may_wakeup(dev))
358		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
359	else
360		ec_dev->wake_enabled = false;
361
362	disable_irq(ec_dev->irq);
 
363	ec_dev->suspended = true;
364}
365
366/**
367 * cros_ec_suspend_late() - Handle a suspend late operation for the ChromeOS EC device.
368 * @ec_dev: Device to suspend.
369 *
370 * This can be called by drivers to handle a suspend late stage of suspend.
371 *
372 * Return: 0 always.
373 */
374int cros_ec_suspend_late(struct cros_ec_device *ec_dev)
375{
376	cros_ec_disable_irq(ec_dev);
377	return 0;
378}
379EXPORT_SYMBOL(cros_ec_suspend_late);
380
381/**
382 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
383 * @ec_dev: Device to suspend.
384 *
385 * This can be called by drivers to handle a suspend event.
386 *
387 * Return: 0 always.
388 */
389int cros_ec_suspend(struct cros_ec_device *ec_dev)
390{
391	cros_ec_suspend_prepare(ec_dev);
392	cros_ec_suspend_late(ec_dev);
393	return 0;
394}
395EXPORT_SYMBOL(cros_ec_suspend);
396
397static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
398{
399	bool wake_event;
400
401	while (ec_dev->mkbp_event_supported &&
402	       cros_ec_get_next_event(ec_dev, &wake_event, NULL) > 0) {
403		blocking_notifier_call_chain(&ec_dev->event_notifier,
404					     1, ec_dev);
405
406		if (wake_event && device_may_wakeup(ec_dev->dev))
407			pm_wakeup_event(ec_dev->dev, 0);
408	}
409}
410
411static void cros_ec_send_resume_event(struct cros_ec_device *ec_dev)
 
 
 
 
 
 
 
 
412{
413	int ret;
414	u8 sleep_event;
415
 
 
 
416	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
417		      HOST_SLEEP_EVENT_S3_RESUME :
418		      HOST_SLEEP_EVENT_S0IX_RESUME;
419
420	ret = cros_ec_sleep_event(ec_dev, sleep_event);
421	if (ret < 0)
422		dev_dbg(ec_dev->dev, "Error %d sending resume event to ec\n",
423			ret);
424}
425
426/**
427 * cros_ec_resume_complete() - Handle a resume complete operation for the ChromeOS EC device.
428 * @ec_dev: Device to resume.
429 *
430 * This can be called by drivers to handle a resume complete stage of resume.
431 */
432void cros_ec_resume_complete(struct cros_ec_device *ec_dev)
433{
434	cros_ec_send_resume_event(ec_dev);
435
 
 
 
 
436	/*
437	 * Let the mfd devices know about events that occur during
438	 * suspend. This way the clients know what to do with them.
439	 */
440	cros_ec_report_events_during_suspend(ec_dev);
441}
442EXPORT_SYMBOL(cros_ec_resume_complete);
443
444static void cros_ec_enable_irq(struct cros_ec_device *ec_dev)
445{
446	ec_dev->suspended = false;
447	enable_irq(ec_dev->irq);
448
449	if (ec_dev->wake_enabled)
450		disable_irq_wake(ec_dev->irq);
451}
452
453/**
454 * cros_ec_resume_early() - Handle a resume early operation for the ChromeOS EC device.
455 * @ec_dev: Device to resume.
456 *
457 * This can be called by drivers to handle a resume early stage of resume.
458 *
459 * Return: 0 always.
460 */
461int cros_ec_resume_early(struct cros_ec_device *ec_dev)
462{
463	cros_ec_enable_irq(ec_dev);
464	return 0;
465}
466EXPORT_SYMBOL(cros_ec_resume_early);
467
468/**
469 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
470 * @ec_dev: Device to resume.
471 *
472 * This can be called by drivers to handle a resume event.
473 *
474 * Return: 0 always.
475 */
476int cros_ec_resume(struct cros_ec_device *ec_dev)
477{
478	cros_ec_resume_early(ec_dev);
479	cros_ec_resume_complete(ec_dev);
480	return 0;
481}
482EXPORT_SYMBOL(cros_ec_resume);
483
484#endif
485
486MODULE_LICENSE("GPL");
487MODULE_DESCRIPTION("ChromeOS EC core driver");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ChromeOS EC multi-function device
  4 *
  5 * Copyright (C) 2012 Google, Inc
  6 *
  7 * The ChromeOS EC multi function device is used to mux all the requests
  8 * to the EC device for its multiple features: keyboard controller,
  9 * battery charging and regulator control, firmware update.
 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
 20#include "cros_ec.h"
 21
 22#define CROS_EC_DEV_EC_INDEX 0
 23#define CROS_EC_DEV_PD_INDEX 1
 24
 25static struct cros_ec_platform ec_p = {
 26	.ec_name = CROS_EC_DEV_NAME,
 27	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
 28};
 29
 30static struct cros_ec_platform pd_p = {
 31	.ec_name = CROS_EC_DEV_PD_NAME,
 32	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
 33};
 34
 35static irqreturn_t ec_irq_handler(int irq, void *data)
 
 
 
 
 
 
 
 36{
 37	struct cros_ec_device *ec_dev = data;
 38
 39	ec_dev->last_event_time = cros_ec_get_time_ns();
 40
 41	return IRQ_WAKE_THREAD;
 42}
 43
 44/**
 45 * cros_ec_handle_event() - process and forward pending events on EC
 46 * @ec_dev: Device with events to process.
 47 *
 48 * Call this function in a loop when the kernel is notified that the EC has
 49 * pending events.
 50 *
 51 * Return: true if more events are still pending and this function should be
 52 * called again.
 53 */
 54bool cros_ec_handle_event(struct cros_ec_device *ec_dev)
 55{
 56	bool wake_event;
 57	bool ec_has_more_events;
 58	int ret;
 59
 60	ret = cros_ec_get_next_event(ec_dev, &wake_event, &ec_has_more_events);
 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))
 68		pm_wakeup_event(ec_dev->dev, 0);
 69
 70	if (ret > 0)
 71		blocking_notifier_call_chain(&ec_dev->event_notifier,
 72					     0, ec_dev);
 73
 74	return ec_has_more_events;
 75}
 76EXPORT_SYMBOL(cros_ec_handle_event);
 77
 78static irqreturn_t ec_irq_thread(int irq, void *data)
 
 
 
 
 
 
 
 79{
 80	struct cros_ec_device *ec_dev = data;
 81	bool ec_has_more_events;
 82
 83	do {
 84		ec_has_more_events = cros_ec_handle_event(ec_dev);
 85	} while (ec_has_more_events);
 86
 87	return IRQ_HANDLED;
 88}
 
 89
 90static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
 91{
 92	int ret;
 93	struct {
 94		struct cros_ec_command msg;
 95		union {
 96			struct ec_params_host_sleep_event req0;
 97			struct ec_params_host_sleep_event_v1 req1;
 98			struct ec_response_host_sleep_event_v1 resp1;
 99		} u;
100	} __packed buf;
101
102	memset(&buf, 0, sizeof(buf));
103
104	if (ec_dev->host_sleep_v1) {
105		buf.u.req1.sleep_event = sleep_event;
106		buf.u.req1.suspend_params.sleep_timeout_ms =
107				EC_HOST_SLEEP_TIMEOUT_DEFAULT;
108
109		buf.msg.outsize = sizeof(buf.u.req1);
110		if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
111		    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
112			buf.msg.insize = sizeof(buf.u.resp1);
113
114		buf.msg.version = 1;
115
116	} else {
117		buf.u.req0.sleep_event = sleep_event;
118		buf.msg.outsize = sizeof(buf.u.req0);
119	}
120
121	buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
122
123	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
124
125	/* For now, report failure to transition to S0ix with a warning. */
126	if (ret >= 0 && ec_dev->host_sleep_v1 &&
127	    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME)) {
 
128		ec_dev->last_resume_result =
129			buf.u.resp1.resume_response.sleep_transitions;
130
131		WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
132			  EC_HOST_RESUME_SLEEP_TIMEOUT,
133			  "EC detected sleep transition timeout. Total slp_s0 transitions: %d",
134			  buf.u.resp1.resume_response.sleep_transitions &
135			  EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
136	}
137
138	return ret;
139}
140
141static int cros_ec_ready_event(struct notifier_block *nb,
142			       unsigned long queued_during_suspend,
143			       void *_notify)
144{
145	struct cros_ec_device *ec_dev = container_of(nb, struct cros_ec_device,
146						     notifier_ready);
147	u32 host_event = cros_ec_get_host_event(ec_dev);
148
149	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INTERFACE_READY)) {
150		mutex_lock(&ec_dev->lock);
151		cros_ec_query_all(ec_dev);
152		mutex_unlock(&ec_dev->lock);
153		return NOTIFY_OK;
154	}
155
156	return NOTIFY_DONE;
157}
158
159/**
160 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
161 * @ec_dev: Device to register.
162 *
163 * Before calling this, allocate a pointer to a new device and then fill
164 * in all the fields up to the --private-- marker.
165 *
166 * Return: 0 on success or negative error code.
167 */
168int cros_ec_register(struct cros_ec_device *ec_dev)
169{
170	struct device *dev = ec_dev->dev;
171	int err = 0;
172
173	BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
 
174
175	ec_dev->max_request = sizeof(struct ec_params_hello);
176	ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
177	ec_dev->max_passthru = 0;
 
 
 
178
179	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
180	if (!ec_dev->din)
181		return -ENOMEM;
182
183	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
184	if (!ec_dev->dout)
185		return -ENOMEM;
186
 
187	mutex_init(&ec_dev->lock);
 
188
189	err = cros_ec_query_all(ec_dev);
190	if (err) {
191		dev_err(dev, "Cannot identify the EC: error %d\n", err);
192		return err;
193	}
194
195	if (ec_dev->irq > 0) {
196		err = devm_request_threaded_irq(dev, ec_dev->irq,
197						ec_irq_handler,
198						ec_irq_thread,
199						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
200						"chromeos-ec", ec_dev);
201		if (err) {
202			dev_err(dev, "Failed to request IRQ %d: %d",
203				ec_dev->irq, err);
204			return err;
205		}
206	}
207
208	/* Register a platform device for the main EC instance */
209	ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
210					PLATFORM_DEVID_AUTO, &ec_p,
211					sizeof(struct cros_ec_platform));
212	if (IS_ERR(ec_dev->ec)) {
213		dev_err(ec_dev->dev,
214			"Failed to create CrOS EC platform device\n");
215		return PTR_ERR(ec_dev->ec);
 
216	}
217
218	if (ec_dev->max_passthru) {
219		/*
220		 * Register a platform device for the PD behind the main EC.
221		 * We make the following assumptions:
222		 * - behind an EC, we have a pd
223		 * - only one device added.
224		 * - the EC is responsive at init time (it is not true for a
225		 *   sensor hub).
226		 */
227		ec_dev->pd = platform_device_register_data(ec_dev->dev,
228					"cros-ec-dev",
229					PLATFORM_DEVID_AUTO, &pd_p,
230					sizeof(struct cros_ec_platform));
231		if (IS_ERR(ec_dev->pd)) {
232			dev_err(ec_dev->dev,
233				"Failed to create CrOS PD platform device\n");
234			platform_device_unregister(ec_dev->ec);
235			return PTR_ERR(ec_dev->pd);
236		}
237	}
238
239	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
240		err = devm_of_platform_populate(dev);
241		if (err) {
242			platform_device_unregister(ec_dev->pd);
243			platform_device_unregister(ec_dev->ec);
244			dev_err(dev, "Failed to register sub-devices\n");
245			return err;
246		}
247	}
248
249	/*
250	 * Clear sleep event - this will fail harmlessly on platforms that
251	 * don't implement the sleep event host command.
252	 */
253	err = cros_ec_sleep_event(ec_dev, 0);
254	if (err < 0)
255		dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
256			err);
257
258	if (ec_dev->mkbp_event_supported) {
259		/*
260		 * Register the notifier for EC_HOST_EVENT_INTERFACE_READY
261		 * event.
262		 */
263		ec_dev->notifier_ready.notifier_call = cros_ec_ready_event;
264		err = blocking_notifier_chain_register(&ec_dev->event_notifier,
265						      &ec_dev->notifier_ready);
266		if (err)
267			return err;
268	}
269
270	dev_info(dev, "Chrome EC device registered\n");
271
 
 
 
 
 
 
 
272	return 0;
 
 
 
 
 
 
273}
274EXPORT_SYMBOL(cros_ec_register);
275
276/**
277 * cros_ec_unregister() - Remove a ChromeOS EC.
278 * @ec_dev: Device to unregister.
279 *
280 * Call this to deregister a ChromeOS EC, then clean up any private data.
281 *
282 * Return: 0 on success or negative error code.
283 */
284int cros_ec_unregister(struct cros_ec_device *ec_dev)
285{
286	if (ec_dev->pd)
287		platform_device_unregister(ec_dev->pd);
288	platform_device_unregister(ec_dev->ec);
289
290	return 0;
291}
292EXPORT_SYMBOL(cros_ec_unregister);
293
294#ifdef CONFIG_PM_SLEEP
295/**
296 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
297 * @ec_dev: Device to suspend.
298 *
299 * This can be called by drivers to handle a suspend event.
300 *
301 * Return: 0 on success or negative error code.
302 */
303int cros_ec_suspend(struct cros_ec_device *ec_dev)
304{
305	struct device *dev = ec_dev->dev;
306	int ret;
307	u8 sleep_event;
308
309	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
310		      HOST_SLEEP_EVENT_S3_SUSPEND :
311		      HOST_SLEEP_EVENT_S0IX_SUSPEND;
312
313	ret = cros_ec_sleep_event(ec_dev, sleep_event);
314	if (ret < 0)
315		dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
316			ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
 
 
318	if (device_may_wakeup(dev))
319		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
 
 
320
321	disable_irq(ec_dev->irq);
322	ec_dev->was_wake_device = ec_dev->wake_enabled;
323	ec_dev->suspended = true;
 
324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325	return 0;
326}
327EXPORT_SYMBOL(cros_ec_suspend);
328
329static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
330{
 
 
331	while (ec_dev->mkbp_event_supported &&
332	       cros_ec_get_next_event(ec_dev, NULL, NULL) > 0)
333		blocking_notifier_call_chain(&ec_dev->event_notifier,
334					     1, ec_dev);
 
 
 
 
335}
336
337/**
338 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
339 * @ec_dev: Device to resume.
340 *
341 * This can be called by drivers to handle a resume event.
342 *
343 * Return: 0 on success or negative error code.
344 */
345int cros_ec_resume(struct cros_ec_device *ec_dev)
346{
347	int ret;
348	u8 sleep_event;
349
350	ec_dev->suspended = false;
351	enable_irq(ec_dev->irq);
352
353	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
354		      HOST_SLEEP_EVENT_S3_RESUME :
355		      HOST_SLEEP_EVENT_S0IX_RESUME;
356
357	ret = cros_ec_sleep_event(ec_dev, sleep_event);
358	if (ret < 0)
359		dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
360			ret);
 
 
 
 
 
 
 
 
 
 
 
361
362	if (ec_dev->wake_enabled) {
363		disable_irq_wake(ec_dev->irq);
364		ec_dev->wake_enabled = 0;
365	}
366	/*
367	 * Let the mfd devices know about events that occur during
368	 * suspend. This way the clients know what to do with them.
369	 */
370	cros_ec_report_events_during_suspend(ec_dev);
 
 
 
 
 
 
 
371
 
 
 
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373	return 0;
374}
375EXPORT_SYMBOL(cros_ec_resume);
376
377#endif
378
379MODULE_LICENSE("GPL");
380MODULE_DESCRIPTION("ChromeOS EC core driver");